-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
362 lines (297 loc) · 10.7 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import { FontCollection, Font, GlyphRenderer } from "./index.js"
import { parseGlyphRange } from "./src/glyphRangeParser.js"
import fs from "fs"
import minimist from "minimist"
import PNG from "pngjs"
import path from "path"
//import PackageJson from "./package.json"
//console.log(PackageJson.name + " v" + PackageJson.version)
const usage =
`
Usage:
font-inspect <FONT_FILE> [options]
Options:
--glyphs <GLYPH_LIST> (default "*")
The list of glyphs to extract.
You can use decimal glyph IDs (#1234), hex Unicode codepoints (U+1abcd), ranges (..), and commas.
You can also use an asterisk (*) to specify all glyphs, and (U+*) to specify all Unicode codepoints.
Example: "U+0..U+7f,#500..#650,U+1e000"
--img-mode <MODE> (default "png-grayscale")
The image format to which glyphs will be rendered.
Available formats:
"none"
Does not output image files.
"png-binary"
PNG file with a black-and-white rasterization of the glyph,
where white corresponds to areas on the inside of contours.
Can be combined with the '--use-alpha' option.
"png-grayscale"
PNG file with a 256-level grayscale rasterization of the glyph,
where white corresponds to areas on the inside of contours.
Can be combined with the '--use-alpha' and '--gamma' options.
"png-sdf"
PNG file with a 256-level grayscale signed distance field,
mapped to colors using the '--sdf-min' and '--sdf-max' options.
Can be combined with the '--use-alpha' option.
--data-mode <MODE> (default "json")
The data format to which glyph metadata will be extracted.
Available formats:
"none"
Does not output glyph metadata.
"json"
JSON file containing character mapping and metrics.
"json-full"
JSON file containing character mapping, metrics, and full geometry data.
"json-simplified"
JSON file as above, but with simplified geometry data where curves have been
converted to line segments, using the '--curve-precision' option.
-o / --out <FILENAME> (default "./glyph_[glyphid]")
Shortcut for both '--img-out' and '--data-out'.
The output filename of each glyph extracted, without the file extension.
You can include the following tags, which will automatically be replaced
by their respective values:
"[glyphid]" Decimal glyph ID of the current glyph, without the # prefix.
"[unicode]" Hex Unicode codepoint of the current glyph, without the U+ prefix.
--img-out <FILENAME> (default "./glyph_[glyphid]")
The output filename of image files, without the file extension.
You can include the same tags as described above in the '--out' option.
--data-out <FILENAME> (default "./glyph_[glyphid]")
The output filename of data files, without the file extension.
You can include the same tags as described above in the '--out' option.
--size <PIXELS> (default 256)
The size of 1 font unit in pixels (usually the height of a line of text)
for image generation.
--outline-min <PIXELS>
--outline-max <PIXELS>
Render glyphs as an outline.
--sdf-min <PIXELS>
--sdf-max <PIXELS>
Distance range for signed distance fields.
--coallesce-unicode
For glyphs that map to many Unicode codepoints, export only one entry under
the most common codepoint, but specify all codepoints in their data files.
--use-alpha
Use the alpha channel in generated images, instead of the color channels.
--gamma <VALUE> (default 2.2)
The gamma correction value for grayscale image output.
--curve-precision <VALUE> (default 100)
The number of line segments to which curves will be converted
for rendering.
--ignore-img-metrics
Force use normalized EM units in the data output, disregarding any rendered images.
--ignore-existing
Skips over glyphs which already have a corresponding file in the output location.
`
const exitWithUsage = () =>
{
console.error(usage)
process.exit(0)
}
// Parse command line arguments.
const opts = minimist(process.argv.slice(2))
if (opts._.length != 1)
exitWithUsage()
const argFontFile = opts._[0]
const argOut = opts["out"] || opts["o"] || "./glyph_[glyphid]"
const argImgMode = opts["img-mode"] || "png-grayscale"
const argImgOut = opts["img-out"] || argOut
const argSize = parseInt(opts.size) || 256
const argDataMode = opts["data-mode"] || "json"
const argDataOut = opts["data-out"] || argOut
const argCoallesceUnicode = !!opts["coallesce-unicode"]
const argUseAlpha = !!opts["use-alpha"]
const argGamma = parseFloat(opts.gamma) || 2.2
const argCurvePrecision = parseInt(opts["curve-precision"]) || 100
const argIgnoreImgMetrics = !!opts["ignore-img-metrics"]
const argIgnoreExisting = !!opts["ignore-existing"]
const argSDFMin = parseFloat(opts["sdf-min"])
const argSDFMax = parseFloat(opts["sdf-max"])
const argOutlineMin = parseFloat(opts["outline-min"])
const argOutlineMax = parseFloat(opts["outline-max"])
// Load the font file.
const bytes = fs.readFileSync(argFontFile)
const fontCollection = FontCollection.fromBytes(bytes)
const font = fontCollection.fonts[0]
// Load the glyph list.
const unicodeMap = font.getUnicodeMap()
const argGlyphs = (opts.glyphs || "*").toLowerCase()
let argGlyphList = parseGlyphRange(argGlyphs)
if (argGlyphs == "*")
{
for (const id of font.enumerateGlyphIds())
argGlyphList.glyphIds.push(id)
}
else if (argGlyphs == "u+*")
{
for (const [codepoint, glyphId] of unicodeMap)
argGlyphList.unicodeCodepoints.push(codepoint)
}
for (const unicodeCodepoint of argGlyphList.unicodeCodepoints)
{
if (unicodeMap.has(unicodeCodepoint))
argGlyphList.glyphIds.push(unicodeMap.get(unicodeCodepoint))
}
argGlyphList.glyphIds = [...new Set(argGlyphList.glyphIds)]
let resolvedGlyphList = []
for (const glyphId of argGlyphList.glyphIds)
{
if (argCoallesceUnicode)
{
let unicodeCodepoints = []
for (const [codepoint, id] of unicodeMap)
{
if (glyphId == id)
unicodeCodepoints.push(codepoint)
}
resolvedGlyphList.push(
{
glyphId,
unicodeCodepoints
})
}
else
{
let hadACodepoint = false
for (const [codepoint, id] of unicodeMap)
{
if (glyphId == id)
{
hadACodepoint = true
resolvedGlyphList.push(
{
glyphId,
unicodeCodepoints: [codepoint]
})
}
}
if (!hadACodepoint)
resolvedGlyphList.push(
{
glyphId,
unicodeCodepoints: []
})
}
}
// Render glyphs.
for (const glyph of resolvedGlyphList)
{
const mainUnicodeCodepoint = (glyph.unicodeCodepoints.length == 0 ? null : glyph.unicodeCodepoints[0])
const outputImgFilename = argImgOut
.replace(/\[glyphid\]/g, glyph.glyphId.toString())
.replace(/\[unicode\]/g, (glyph.unicodeCodepoints.length == 0 ? "" : glyph.unicodeCodepoints[0].toString(16)))
const outputDataFilename = argDataOut
.replace(/\[glyphid\]/g, glyph.glyphId.toString())
.replace(/\[unicode\]/g, mainUnicodeCodepoint == null ? "" : mainUnicodeCodepoint.toString(16))
let shouldSkip = false
if (argIgnoreExisting)
{
if (argImgMode != "none" && fs.existsSync(outputImgFilename))
shouldSkip = true
if (argDataMode == "json" && fs.existsSync(outputDataFilename + ".json"))
shouldSkip = true
if (argDataMode == "xml-sprsheet" && fs.existsSync(outputDataFilename + ".sprsheet"))
shouldSkip = true
}
console.log((shouldSkip ? "skipping" : "extracting") + " glyph #" + glyph.glyphId + ": [" + glyph.unicodeCodepoints.map(c => "U+" + c.toString(16)).join(",") + "]...")
if (shouldSkip)
continue
let renderedImage = null
if (argImgMode != "none")
{
const geometry = font.getGlyphGeometry(glyph.glyphId, argCurvePrecision)
renderedImage = GlyphRenderer.render(geometry, argSize * 16)
if (argImgMode == "png-sdf")
{
renderedImage = renderedImage.getWithBorder(argSDFMax * 16)
renderedImage = renderedImage.getSignedDistanceField()
renderedImage.normalizeSignedDistance(argSDFMin * 16, argSDFMax * 16)
}
else if (!isNaN(argOutlineMin) && !isNaN(argOutlineMax))
{
renderedImage = renderedImage.getWithBorder(argOutlineMax * 16)
renderedImage = renderedImage.getSignedDistanceField()
renderedImage.outline(argOutlineMin * 16, argOutlineMax * 16)
}
renderedImage = renderedImage.getDownsampled(argImgMode == "png-sdf" ? 1 : argGamma)
if (argImgMode == "png-binary")
renderedImage.binarize()
renderedImage.normalizeColorRange()
let png = new PNG.PNG({ width: renderedImage.width, height: renderedImage.height, colorType: 6 })
for (let y = 0; y < renderedImage.height; y++)
{
for (let x = 0; x < renderedImage.width; x++)
{
const i = (y * renderedImage.width + x)
if (argUseAlpha)
{
png.data[i * 4 + 0] = 255
png.data[i * 4 + 1] = 255
png.data[i * 4 + 2] = 255
png.data[i * 4 + 3] = renderedImage.buffer[i]
}
else
{
png.data[i * 4 + 0] = renderedImage.buffer[i]
png.data[i * 4 + 1] = renderedImage.buffer[i]
png.data[i * 4 + 2] = renderedImage.buffer[i]
png.data[i * 4 + 3] = 255
}
}
}
fs.writeFileSync(outputImgFilename + ".png", PNG.PNG.sync.write(png))
}
if (argDataMode != "none")
{
const geometry = font.getGlyphGeometry(glyph.glyphId, argDataMode != "json-simplified" ? 0 : argCurvePrecision)
geometry.xMin = geometry.xMin || 0
geometry.xMax = geometry.xMax || 0
geometry.yMin = geometry.yMin || 0
geometry.yMax = geometry.yMax || 0
geometry.advance = geometry.advance || 0
let metrics =
{
width: geometry.xMax - geometry.xMin,
height: geometry.yMax - geometry.yMin,
xOrigin: 0,
yOrigin: 0,
xAdvance: geometry.advance,
emToPixels: null,
}
if (renderedImage && !argIgnoreImgMetrics)
{
metrics =
{
width: renderedImage.width,
height: renderedImage.height,
xOrigin: renderedImage.xOrigin,
yOrigin: renderedImage.yOrigin,
xAdvance: renderedImage.emScale * geometry.advance,
emToPixels: renderedImage.emScale,
}
}
let json =
{
...glyph,
...metrics,
}
if (argDataMode != "json")
json.contours = geometry.contours
if (argDataMode != "xml-sprsheet")
{
const jsonStr = JSON.stringify(json, null, 4)
fs.writeFileSync(outputDataFilename + ".json", jsonStr)
}
else
{
const filenameWithoutFolder = path.basename(outputDataFilename + ".png")
const xml =
`<sprite-sheet src="` + filenameWithoutFolder + `">
<sprite name="` + mainUnicodeCodepoint.toString(16) + `" x="0" y="0" width="` + metrics.width + `" height="` + metrics.height + `">
<guide name="unicode" kind="string" value="` + mainUnicodeCodepoint + `"></guide>
<guide name="base-advance" kind="vector" x1="` + Math.floor(metrics.xOrigin) + `" x2="` + Math.floor(metrics.xOrigin + metrics.xAdvance) + `" y1="` + Math.floor(metrics.yOrigin) + `" y2="` + Math.floor(metrics.yOrigin) + `"></guide>
</sprite>
</sprite-sheet>`
fs.writeFileSync(outputDataFilename + ".sprsheet", xml)
}
}
}