1- #!/usr/bin/env node --experimental-modules
2-
3-
41import { FontCollection , Font , GlyphRenderer } from "./index.js"
52import { parseGlyphRange } from "./src/glyphRangeParser.js"
63import fs from "fs"
@@ -9,8 +6,8 @@ import PNG from "pngjs"
96import 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
1613const 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
114114const exitWithUsage = ( ) =>
@@ -135,6 +135,7 @@ const argUseAlpha = !!opts["use-alpha"]
135135const argGamma = parseFloat ( opts . gamma ) || 2.2
136136const argCurvePrecision = parseInt ( opts [ "curve-precision" ] ) || 100
137137const argIgnoreImgMetrics = ! ! opts [ "ignore-img-metrics" ]
138+ const argIgnoreExisting = ! ! opts [ "ignore-existing" ]
138139
139140const argSDFMin = parseFloat ( opts [ "sdf-min" ] )
140141const argSDFMax = parseFloat ( opts [ "sdf-max" ] )
@@ -216,7 +217,32 @@ for (const glyphId of argGlyphList.glyphIds)
216217// Render glyphs.
217218for ( 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 ( / \[ g l y p h i d \] / g, glyph . glyphId . toString ( ) )
224+ . replace ( / \[ u n i c o d e \] / g, ( glyph . unicodeCodepoints . length == 0 ? "" : glyph . unicodeCodepoints [ 0 ] . toString ( 16 ) ) )
225+
226+ const outputDataFilename = argDataOut
227+ . replace ( / \[ g l y p h i d \] / g, glyph . glyphId . toString ( ) )
228+ . replace ( / \[ u n i c o d e \] / 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 ( / \[ g l y p h i d \] / g, glyph . glyphId . toString ( ) )
275- . replace ( / \[ u n i c o d e \] / 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 ( / \[ g l y p h i d \] / g, glyph . glyphId . toString ( ) )
325- . replace ( / \[ u n i c o d e \] / 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}
0 commit comments