Skip to content

Commit

Permalink
prevent crash on diacritic marks, close #40
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Nov 11, 2021
1 parent 049f6ae commit 5f166d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class TinySDF {
const width = glyphWidth + 2 * this.buffer;
const height = glyphHeight + 2 * this.buffer;

const len = width * height;
const len = Math.max(width * height, 0);
const data = new Uint8ClampedArray(len);
const glyph = {data, width, height, glyphWidth, glyphHeight, glyphTop, glyphLeft, glyphAdvance};
if (glyphWidth === 0 || glyphHeight === 0) return glyph;
Expand Down
18 changes: 12 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import pixelmatch from 'pixelmatch';

const baseUrl = import.meta.url;

test('draws an SDF given a character', (t) => {

class MockTinySDF extends TinySDF {
_createCanvas(size) {
return nodeCanvas.createCanvas(size, size);
}
class MockTinySDF extends TinySDF {
_createCanvas(size) {
return nodeCanvas.createCanvas(size, size);
}
}

test('draws an SDF given a character', (t) => {

const rawUrl = new URL('./fixtures/1-raw.png', baseUrl);
const sdfUrl = new URL('./fixtures/1-sdf.png', baseUrl);
Expand Down Expand Up @@ -84,3 +84,9 @@ test('draws an SDF given a character', (t) => {
t.end();
});

test('does not crash on diacritic marks', (t) => {
const sdf = new MockTinySDF();
sdf.draw('í'[1]);
sdf.draw('G̱'[1]);
t.end();
});

0 comments on commit 5f166d0

Please sign in to comment.