From c609b356392e8278e355379f35b9c924479040dd Mon Sep 17 00:00:00 2001 From: Nick Doiron Date: Fri, 1 Aug 2025 23:56:32 -0500 Subject: [PATCH 1/3] include lang option in constructor and pass to ctx --- index.d.ts | 1 + index.js | 6 +++++- test/test.js | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 6f6629f..679ee9f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,6 +6,7 @@ export declare type TinySDFOptions = { fontFamily?: string; fontWeight?: string; fontStyle?: string; + lang?: string; }; export default class TinySDF { diff --git a/index.js b/index.js index 5a56c46..9547b3e 100644 --- a/index.js +++ b/index.js @@ -8,11 +8,13 @@ export default class TinySDF { cutoff = 0.25, fontFamily = 'sans-serif', fontWeight = 'normal', - fontStyle = 'normal' + fontStyle = 'normal', + lang = null } = {}) { this.buffer = buffer; this.cutoff = cutoff; this.radius = radius; + this.lang = lang; // make the canvas size big enough to both have the specified buffer around the glyph // for "halo", and account for some glyphs possibly being larger than their font size @@ -20,6 +22,7 @@ export default class TinySDF { const canvas = this._createCanvas(size); const ctx = this.ctx = canvas.getContext('2d', {willReadFrequently: true}); + ctx.lang = lang || ctx.lang; ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`; ctx.textBaseline = 'alphabetic'; @@ -67,6 +70,7 @@ export default class TinySDF { if (glyphWidth === 0 || glyphHeight === 0) return glyph; const {ctx, buffer, gridInner, gridOuter} = this; + ctx.lang = this.lang || ctx.lang; ctx.clearRect(buffer, buffer, glyphWidth, glyphHeight); ctx.fillText(char, buffer, buffer + glyphTop); const imgData = ctx.getImageData(buffer, buffer, glyphWidth, glyphHeight); diff --git a/test/test.js b/test/test.js index ad4484f..7a2b09c 100644 --- a/test/test.js +++ b/test/test.js @@ -105,3 +105,11 @@ test('does not return negative-width glylphs', () => { assert.equal(glyph.glyphWidth, 0); assert.equal(glyph.width, 6); // zero-width glyph with 3px buffer }); + +test('renders Simplified and Traditional Chinese', () => { + const sdf1 = new MockTinySDF({lang: 'zh'}); + const glyph1 = sdf1.draw('门'); + const sdf2 = new MockTinySDF({lang: 'ja'}); + const glyph2 = sdf2.draw('门'); + assert.deepStrictEqual(glyph1.data, glyph2.data); +}); From 6be31771f57f96e6e571d19d1bff4cd8f7672041 Mon Sep 17 00:00:00 2001 From: Nick Doiron Date: Sat, 2 Aug 2025 00:01:27 -0500 Subject: [PATCH 2/3] concept for test --- test/test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test.js b/test/test.js index 7a2b09c..3a9481f 100644 --- a/test/test.js +++ b/test/test.js @@ -106,10 +106,12 @@ test('does not return negative-width glylphs', () => { assert.equal(glyph.width, 6); // zero-width glyph with 3px buffer }); -test('renders Simplified and Traditional Chinese', () => { +/* +test('renders Chinese and Japanese versions of characters', () => { const sdf1 = new MockTinySDF({lang: 'zh'}); const glyph1 = sdf1.draw('门'); const sdf2 = new MockTinySDF({lang: 'ja'}); const glyph2 = sdf2.draw('门'); - assert.deepStrictEqual(glyph1.data, glyph2.data); + assert.notDeepStrictEqual(glyph1.data, glyph2.data); }); +*/ From cac50c66c281c15e70e8fcdedd5d5ade83de96a7 Mon Sep 17 00:00:00 2001 From: Nick Doiron Date: Mon, 4 Aug 2025 23:15:12 -0500 Subject: [PATCH 3/3] remove extra setting --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index 9547b3e..b452b7f 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,6 @@ export default class TinySDF { const canvas = this._createCanvas(size); const ctx = this.ctx = canvas.getContext('2d', {willReadFrequently: true}); - ctx.lang = lang || ctx.lang; ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`; ctx.textBaseline = 'alphabetic'; @@ -70,7 +69,7 @@ export default class TinySDF { if (glyphWidth === 0 || glyphHeight === 0) return glyph; const {ctx, buffer, gridInner, gridOuter} = this; - ctx.lang = this.lang || ctx.lang; + if (this.lang) ctx.lang = this.lang; ctx.clearRect(buffer, buffer, glyphWidth, glyphHeight); ctx.fillText(char, buffer, buffer + glyphTop); const imgData = ctx.getImageData(buffer, buffer, glyphWidth, glyphHeight);