Skip to content

Commit 4cef5bc

Browse files
committed
feat: enhance parser to extract all glyphs and complete font metadata
- Extract all glyphs from font.characterSet instead of hardcoded characters - Add comprehensive font metadata (version, copyright, weight, italic, styleName) - Improve font name fallback handling - Simplify parser logic by removing unnecessary special glyph handling
1 parent 3508fa7 commit 4cef5bc

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

packages/unglyph/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unglyph",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Unified font glyph manipulation library with simple API for parsing, creating, and modifying fonts",
55
"main": "dist/index.mjs",
66
"types": "dist/index.d.ts",

packages/unglyph/src/parser.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,35 @@ export function parseFont(input: FontInput): FontData {
1717
const font = "fonts" in fontResult ? fontResult.fonts[0] : fontResult;
1818

1919
const fontData: FontData = {
20-
familyName: font.familyName,
20+
familyName: font.familyName || font.postscriptName || "Unknown",
2121
format: font.type,
2222
unitsPerEm: font.unitsPerEm,
2323
ascent: font.ascent,
2424
descent: font.descent,
2525
lineGap: font.lineGap,
2626
capHeight: font.capHeight,
2727
xHeight: font.xHeight,
28+
// Additional metadata from fontkit
29+
version: font.version ? font.version.toString() : undefined,
30+
copyright: font.copyright || undefined,
31+
weight: font["OS/2"]?.usWeightClass || undefined,
32+
italic: font.italicAngle !== 0,
33+
styleName: font.subfamilyName || "Regular",
2834
glyphs: [],
2935
};
3036

31-
// Extract specific glyphs we need (A, B, C, space)
32-
const targetChars = [
33-
{ char: "A", unicode: 65 },
34-
{ char: "B", unicode: 66 },
35-
{ char: "C", unicode: 67 },
36-
{ char: " ", unicode: 32 },
37-
];
38-
39-
for (const { unicode } of targetChars) {
40-
const fontkitGlyph = font.glyphForCodePoint(unicode);
37+
// Extract all glyphs from the font's character set
38+
// This ensures we only get glyphs that actually have Unicode mappings
39+
for (const codePoint of font.characterSet) {
40+
const fontkitGlyph = font.glyphForCodePoint(codePoint);
4141

4242
if (fontkitGlyph) {
4343
// Convert fontkit glyph to our unified format
44-
// Space characters may not have path commands, which is normal
4544
const glyph = convertFromFontkitGlyph(
4645
fontkitGlyph,
4746
fontkitGlyph.id,
4847
font.ascent,
49-
unicode,
48+
codePoint,
5049
);
5150
fontData.glyphs.push(glyph);
5251
}

0 commit comments

Comments
 (0)