Skip to content

Commit 936f423

Browse files
authored
Accepting a lang option and applying it to canvas's ctx.lang (#58)
* include lang option in constructor and pass to ctx * concept for test * remove extra setting
1 parent 6a66ff0 commit 936f423

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export declare type TinySDFOptions = {
66
fontFamily?: string;
77
fontWeight?: string;
88
fontStyle?: string;
9+
lang?: string;
910
};
1011

1112
export default class TinySDF {

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export default class TinySDF {
88
cutoff = 0.25,
99
fontFamily = 'sans-serif',
1010
fontWeight = 'normal',
11-
fontStyle = 'normal'
11+
fontStyle = 'normal',
12+
lang = null
1213
} = {}) {
1314
this.buffer = buffer;
1415
this.cutoff = cutoff;
1516
this.radius = radius;
17+
this.lang = lang;
1618

1719
// make the canvas size big enough to both have the specified buffer around the glyph
1820
// for "halo", and account for some glyphs possibly being larger than their font size
@@ -67,6 +69,7 @@ export default class TinySDF {
6769
if (glyphWidth === 0 || glyphHeight === 0) return glyph;
6870

6971
const {ctx, buffer, gridInner, gridOuter} = this;
72+
if (this.lang) ctx.lang = this.lang;
7073
ctx.clearRect(buffer, buffer, glyphWidth, glyphHeight);
7174
ctx.fillText(char, buffer, buffer + glyphTop);
7275
const imgData = ctx.getImageData(buffer, buffer, glyphWidth, glyphHeight);

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,13 @@ test('does not return negative-width glylphs', () => {
105105
assert.equal(glyph.glyphWidth, 0);
106106
assert.equal(glyph.width, 6); // zero-width glyph with 3px buffer
107107
});
108+
109+
/*
110+
test('renders Chinese and Japanese versions of characters', () => {
111+
const sdf1 = new MockTinySDF({lang: 'zh'});
112+
const glyph1 = sdf1.draw('门');
113+
const sdf2 = new MockTinySDF({lang: 'ja'});
114+
const glyph2 = sdf2.draw('门');
115+
assert.notDeepStrictEqual(glyph1.data, glyph2.data);
116+
});
117+
*/

0 commit comments

Comments
 (0)