From 6bf57673a3fb6db1b790b61058bb2e612af95a73 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Fri, 3 Feb 2023 18:36:10 +0200 Subject: [PATCH 1/2] fix negative-width diacritics handling --- index.js | 2 +- test/test.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 08bd44a..5a56c46 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/test/test.js b/test/test.js index 2281706..d13084b 100644 --- a/test/test.js +++ b/test/test.js @@ -89,3 +89,11 @@ 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(); + const glyph = sdf.draw('゙'); + t.equal(glyph.glyphWidth, 0); + t.equal(glyph.width, 6); // zero-width glyph with 3px buffer + t.end(); +}); From 6f2f77e6ed8ef056979c921687a0ed48529b6576 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Fri, 3 Feb 2023 18:45:32 +0200 Subject: [PATCH 2/2] fixup for travis --- test/test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/test.js b/test/test.js index d13084b..f197483 100644 --- a/test/test.js +++ b/test/test.js @@ -92,6 +92,17 @@ test('does not crash on diacritic marks', (t) => { 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