Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const describe = (id: number) =>
? `'${String.fromCharCode(id)}' (${id})`
: `U+${id.toString(16).toUpperCase().padStart(4, '0')}`;

/** Combining marks and format characters such as joiners are meant to take no room. */
const zeroWidth = (id: number) =>
id <= 0x10ffff && /^[\p{M}\p{Cf}]$/u.test(String.fromCodePoint(id));

/** Problems that show up as missing or garbled text in a game. */
export function checkFont(
font: BMFont,
Expand Down Expand Up @@ -42,7 +46,7 @@ export function checkFont(
`${name} at ${g.x},${g.y} ${g.width}x${g.height} reaches outside the ${common.scaleW}x${common.scaleH} page`,
);
}
if (g.xadvance <= 0 && g.id !== 0x200b)
if (g.xadvance < 0 || (g.xadvance === 0 && !zeroWidth(g.id)))
warning(`${name} has xadvance ${g.xadvance}, text after it will overlap`);
}
if (!seen.has(32))
Expand Down
16 changes: 16 additions & 0 deletions test/font.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ describe('checkFont', () => {
expect(checkFont(demo(), [{ width: 64, height: 32 }])).toEqual([]);
});

it('warns about xadvance 0 only for characters that should take room', () => {
const font = demo();
const a = font.chars.find((c) => c.id === 65)!;
font.chars.push(
{ ...a, id: 0x301, xadvance: 0 },
{ ...a, id: 0x200d, xadvance: 0 },
{ ...a, id: 0x200b, xadvance: 0 },
{ ...a, id: 0x302, xadvance: -1 },
);
a.xadvance = 0;
expect(checkFont(font).map((p) => p.message)).toEqual([
"'A' (65) has xadvance 0, text after it will overlap",
'U+0302 has xadvance -1, text after it will overlap',
]);
});

it('reports glyphs off the page, bad pages, duplicates and stray kerning', () => {
const font = demo();
font.chars.push({ ...font.chars[10]!, x: 62 });
Expand Down
Loading