Skip to content

Commit 426b8ad

Browse files
committed
add json sprsheet output
1 parent 0f9d874 commit 426b8ad

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

cli.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ for (const glyphId of argGlyphList.glyphIds)
214214
}
215215
}
216216

217+
const lineMetrics = font.getHorizontalLineMetrics()
218+
217219
// Render glyphs.
218220
for (const glyph of resolvedGlyphList)
219221
{
@@ -314,6 +316,8 @@ for (const glyph of resolvedGlyphList)
314316
height: geometry.yMax - geometry.yMin,
315317
xOrigin: 0,
316318
yOrigin: 0,
319+
metricsY1: lineMetrics.lineTop,
320+
metricsY2: lineMetrics.lineBottom,
317321
xAdvance: geometry.advance,
318322
emToPixels: null,
319323
}
@@ -340,12 +344,41 @@ for (const glyph of resolvedGlyphList)
340344
if (argDataMode != "json")
341345
json.contours = geometry.contours
342346

343-
if (argDataMode != "xml-sprsheet")
344-
{
345-
const jsonStr = JSON.stringify(json, null, 4)
346-
fs.writeFileSync(outputDataFilename + ".json", jsonStr)
347+
if (argDataMode === "json-sprsheet")
348+
{
349+
const filenameWithoutFolder = path.basename(outputDataFilename + ".png")
350+
const json =
351+
`{
352+
sprites:
353+
[
354+
{
355+
id: "${mainUnicodeCodepoint.toString(16)}",
356+
src: "${filenameWithoutFolder}",
357+
x: 0,
358+
y: 0,
359+
w: ${metrics.width},
360+
h: ${metrics.height},
361+
trim_x: 0,
362+
trim_y: 0,
363+
trim_w: 0,
364+
trim_h: 0,
365+
properties:
366+
[
367+
unicode: ${mainUnicodeCodepoint},
368+
xPivot: ${Math.floor(metrics.xOrigin)},
369+
yPivot: ${Math.floor(metrics.yOrigin)},
370+
advance: ${Math.floor(metrics.xAdvance)},
371+
ascender: ${Math.floor(lineMetrics.lineTop * metrics.emToPixels)},
372+
descender: ${Math.floor(lineMetrics.lineBottom * metrics.emToPixels)},
373+
lineGap: ${Math.floor(lineMetrics.lineGap * metrics.emToPixels)}
374+
]
347375
}
348-
else
376+
]
377+
}`
378+
379+
fs.writeFileSync(outputDataFilename + ".json", json)
380+
}
381+
else if (argDataMode === "xml-sprsheet")
349382
{
350383
const filenameWithoutFolder = path.basename(outputDataFilename + ".png")
351384
const xml =
@@ -358,5 +391,10 @@ for (const glyph of resolvedGlyphList)
358391

359392
fs.writeFileSync(outputDataFilename + ".sprsheet", xml)
360393
}
394+
else
395+
{
396+
const jsonStr = JSON.stringify(json, null, 4)
397+
fs.writeFileSync(outputDataFilename + ".json", jsonStr)
398+
}
361399
}
362400
}

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<br>
7474
Sub-font: <select id="selectFontIndex" disabled><option>No font loaded</option></select>
7575
<label for="checkboxDrawMetrics"><input id="checkboxDrawMetrics" type="checkbox" checked></input> Draw Glyph Metrics</label>
76-
<label for="checkboxSortUnicode"><input id="checkboxSortUnicode" type="checkbox"></input> Sort by Unicode</label>
76+
<label for="checkboxSortUnicode"><input id="checkboxSortUnicode" type="checkbox" checked></input> Sort by Unicode</label>
7777
<label for="checkboxCustomRender"><input id="checkboxCustomRender" type="checkbox"></input> DEBUG: Custom Rasterizer</label>
7878
<br>
7979
<span style="font-size:0.75em;">(Double-click a glyph to print geometry data to the Console and inspect its Unicode codepoint.)</span>

0 commit comments

Comments
 (0)