Skip to content

Commit

Permalink
(glyphs): move svg optimization to export.py
Browse files Browse the repository at this point in the history
  • Loading branch information
stone-zeng committed Sep 18, 2023
1 parent c3f5f26 commit 429d13e
Show file tree
Hide file tree
Showing 8 changed files with 1,292 additions and 1,354 deletions.
14 changes: 13 additions & 1 deletion glyphs/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import os
import re
import subprocess


def main(jsonPath: str):
Expand All @@ -20,6 +21,8 @@ def main(jsonPath: str):
res[glyph.name] = path
Font.gridSubDivisions = 1

res = optimize(res)

with open(os.path.join(os.path.dirname(Font.filepath), jsonPath), "w") as fp:
json.dump(res, fp, indent=2)

Expand Down Expand Up @@ -59,4 +62,13 @@ def sub(s: str) -> str:
return s


main(jsonPath="glyphs.json")
def optimize(res: dict[str, str]) -> dict[str, str]:
svg = "".join(f"<path d=\"{path}\"/>" for path in res.values())
output = subprocess.run(["svgo", "-s", svg], capture_output=True).stdout.decode()
return dict(zip(res.keys(), output.split("\"")[1:-1:2]))


if __name__ == "__main__":
if "/usr/local/bin/" not in os.environ["PATH"]:
os.environ["PATH"] = os.environ["PATH"] + ":/usr/local/bin/"
main(jsonPath="glyphs.json")
6 changes: 1 addition & 5 deletions glyphs/generate-src.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import { fileURLToPath } from 'url'
import { optimize } from 'svgo'

const __dirname = fileURLToPath(new URL('.', import.meta.url))
const iconsPath = join(__dirname, 'icons.csv')
Expand All @@ -20,9 +19,6 @@ import React from 'react'
import type { DinkieIconProps } from './index'
`

const optimizePath = (path) =>
optimize(`<path d="${path}"/>`).data.replace(/<path d="(.+)"\/>/g, '$1')

const basename = (glyph) => glyph.replace('.small', '').replace('.filled', '')
const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1)
const toComponentName = (map, glyph) =>
Expand Down Expand Up @@ -60,7 +56,7 @@ const main = () => {
([glyph, path]) => ({
name: toComponentName(glyphNameMap, glyph),
size: glyph.includes('.small') ? 10 : 12,
path: optimizePath(path),
path,
})
)
writeFileSync(vueSrcPath, banner + vueImports + icons.map(generateVueComponent).join(''))
Expand Down
2,283 changes: 1,164 additions & 1,119 deletions glyphs/glyphs.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"build": "pnpm -r --filter=./packages/** run build"
},
"devDependencies": {
"svgo": "^3.0.2",
"typescript": "^5.2.2",
"vite": "^4.4.9"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"react": "^18.2.0"
},
"devDependencies": {
"@types/node": "^20.6.0",
"@types/node": "^20.6.2",
"@vitejs/plugin-react": "^4.0.4",
"typescript": "^5.2.2",
"vite": "^4.4.9",
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
},
"devDependencies": {
"@dinkie-icons/vue": "workspace:^",
"@types/node": "^20.6.0",
"@types/node": "^20.6.2",
"@vitejs/plugin-vue": "^4.3.4",
"typescript": "^5.2.2",
"vite": "^4.4.9",
"vue": "^3.3.4",
"vue-tsc": "^1.8.10"
"vue-tsc": "^1.8.11"
}
}
2 changes: 1 addition & 1 deletion packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"vue": "^3.3.4"
},
"devDependencies": {
"@types/node": "^20.6.0",
"@types/node": "^20.6.2",
"typescript": "^5.2.2",
"vite": "^4.4.9",
"vite-plugin-dts": "^3.5.3"
Expand Down
Loading

0 comments on commit 429d13e

Please sign in to comment.