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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class TinySDF {
const glyphLeft = 0;

// If the glyph overflows the canvas size, it will be clipped at the bottom/right
const glyphWidth = Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxRight - actualBoundingBoxLeft));
const glyphWidth = Math.max(0, Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxRight - actualBoundingBoxLeft)));
const glyphHeight = Math.min(this.size - this.buffer, glyphTop + Math.ceil(actualBoundingBoxDescent));

const width = glyphWidth + 2 * this.buffer;
Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,22 @@ test('does not crash on diacritic marks', (t) => {
sdf.draw('G̱'[1]);
t.end();
});

test('does not return negative-width glylphs', (t) => {
const sdf = new MockTinySDF();
// stub these because they vary across environments
sdf.ctx.measureText = () => ({
width: 0,
actualBoundingBoxLeft: 23.3759765625,
actualBoundingBoxRight: -17.6162109375,
actualBoundingBoxAscent: 20.2080078125,
actualBoundingBoxDescent: -14.51953125,
emHeightAscent: 26,
emHeightDescent: 9,
alphabeticBaseline: 7.51953125
});
const glyph = sdf.draw('゙');
t.equal(glyph.glyphWidth, 0);
t.equal(glyph.width, 6); // zero-width glyph with 3px buffer
t.end();
});