Skip to content

Commit e3f49b3

Browse files
committed
add ignore existing option
1 parent 5645047 commit e3f49b3

2 files changed

Lines changed: 39 additions & 23 deletions

File tree

cli.js

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#!/usr/bin/env node --experimental-modules
2-
3-
41
import { FontCollection, Font, GlyphRenderer } from "./index.js"
52
import { parseGlyphRange } from "./src/glyphRangeParser.js"
63
import fs from "fs"
@@ -9,8 +6,8 @@ import PNG from "pngjs"
96
import path from "path"
107

118

12-
import PackageJson from "./package.json"
13-
console.log(PackageJson.name + " v" + PackageJson.version)
9+
//import PackageJson from "./package.json"
10+
//console.log(PackageJson.name + " v" + PackageJson.version)
1411

1512

1613
const usage =
@@ -109,6 +106,9 @@ Options:
109106
110107
--ignore-img-metrics
111108
Force use normalized EM units in the data output, disregarding any rendered images.
109+
110+
--ignore-existing
111+
Skips over glyphs which already have a corresponding file in the output location.
112112
`
113113

114114
const exitWithUsage = () =>
@@ -135,6 +135,7 @@ const argUseAlpha = !!opts["use-alpha"]
135135
const argGamma = parseFloat(opts.gamma) || 2.2
136136
const argCurvePrecision = parseInt(opts["curve-precision"]) || 100
137137
const argIgnoreImgMetrics = !!opts["ignore-img-metrics"]
138+
const argIgnoreExisting = !!opts["ignore-existing"]
138139

139140
const argSDFMin = parseFloat(opts["sdf-min"])
140141
const argSDFMax = parseFloat(opts["sdf-max"])
@@ -216,7 +217,32 @@ for (const glyphId of argGlyphList.glyphIds)
216217
// Render glyphs.
217218
for (const glyph of resolvedGlyphList)
218219
{
219-
console.log("extracting glyph #" + glyph.glyphId + ": [" + glyph.unicodeCodepoints.map(c => "U+" + c.toString(16)).join(",") + "]...")
220+
const mainUnicodeCodepoint = (glyph.unicodeCodepoints.length == 0 ? null : glyph.unicodeCodepoints[0])
221+
222+
const outputImgFilename = argImgOut
223+
.replace(/\[glyphid\]/g, glyph.glyphId.toString())
224+
.replace(/\[unicode\]/g, (glyph.unicodeCodepoints.length == 0 ? "" : glyph.unicodeCodepoints[0].toString(16)))
225+
226+
const outputDataFilename = argDataOut
227+
.replace(/\[glyphid\]/g, glyph.glyphId.toString())
228+
.replace(/\[unicode\]/g, mainUnicodeCodepoint == null ? "" : mainUnicodeCodepoint.toString(16))
229+
230+
let shouldSkip = false
231+
if (argIgnoreExisting)
232+
{
233+
if (argImgMode != "none" && fs.existsSync(outputImgFilename))
234+
shouldSkip = true
235+
236+
if (argDataMode == "json" && fs.existsSync(outputDataFilename + ".json"))
237+
shouldSkip = true
238+
239+
if (argDataMode == "xml-sprsheet" && fs.existsSync(outputDataFilename + ".sprsheet"))
240+
shouldSkip = true
241+
}
242+
243+
console.log((shouldSkip ? "skipping" : "extracting") + " glyph #" + glyph.glyphId + ": [" + glyph.unicodeCodepoints.map(c => "U+" + c.toString(16)).join(",") + "]...")
244+
if (shouldSkip)
245+
continue
220246

221247
let renderedImage = null
222248
if (argImgMode != "none")
@@ -270,11 +296,7 @@ for (const glyph of resolvedGlyphList)
270296
}
271297
}
272298

273-
const outputFilename = argImgOut
274-
.replace(/\[glyphid\]/g, glyph.glyphId.toString())
275-
.replace(/\[unicode\]/g, (glyph.unicodeCodepoints.length == 0 ? "" : glyph.unicodeCodepoints[0].toString(16)))
276-
277-
fs.writeFileSync(outputFilename + ".png", PNG.PNG.sync.write(png))
299+
fs.writeFileSync(outputImgFilename + ".png", PNG.PNG.sync.write(png))
278300
}
279301

280302
if (argDataMode != "none")
@@ -318,20 +340,14 @@ for (const glyph of resolvedGlyphList)
318340
if (argDataMode != "json")
319341
json.contours = geometry.contours
320342

321-
const mainUnicodeCodepoint = (glyph.unicodeCodepoints.length == 0 ? null : glyph.unicodeCodepoints[0])
322-
323-
const outputFilename = argDataOut
324-
.replace(/\[glyphid\]/g, glyph.glyphId.toString())
325-
.replace(/\[unicode\]/g, mainUnicodeCodepoint == null ? "" : mainUnicodeCodepoint.toString(16))
326-
327343
if (argDataMode != "xml-sprsheet")
328344
{
329345
const jsonStr = JSON.stringify(json, null, 4)
330-
fs.writeFileSync(outputFilename + ".json", jsonStr)
346+
fs.writeFileSync(outputDataFilename + ".json", jsonStr)
331347
}
332348
else
333349
{
334-
const filenameWithoutFolder = path.basename(outputFilename + ".png")
350+
const filenameWithoutFolder = path.basename(outputDataFilename + ".png")
335351
const xml =
336352
`<sprite-sheet src="` + filenameWithoutFolder + `">
337353
<sprite name="` + mainUnicodeCodepoint.toString(16) + `" x="0" y="0" width="` + metrics.width + `" height="` + metrics.height + `">
@@ -340,7 +356,7 @@ for (const glyph of resolvedGlyphList)
340356
</sprite>
341357
</sprite-sheet>`
342358

343-
fs.writeFileSync(outputFilename + ".sprsheet", xml)
359+
fs.writeFileSync(outputDataFilename + ".sprsheet", xml)
344360
}
345361
}
346362
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hlorenzi/font",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "Utilities and a CLI for extracting glyphs and metadata from font files.",
55
"repository": "github:hlorenzi/font-js",
66
"homepage": "https://github.com/hlorenzi/font-js#readme",
@@ -16,10 +16,10 @@
1616
"type": "module",
1717
"main": "./index.js",
1818
"scripts": {
19-
"start": "node --experimental-modules cli.js",
19+
"start": "node cli.js",
2020
"build": "webpack",
2121
"watch": "webpack --watch --mode development",
22-
"test": "node --experimental-modules test.js"
22+
"test": "node test.js"
2323
},
2424
"author": "hlorenzi",
2525
"license": "ISC",

0 commit comments

Comments
 (0)