Skip to content

Commit 5c28889

Browse files
committed
Actually call destroy() in tests
It seems that this.blob etc are always null when afterEach callback is called, so we now use global variables instead.
1 parent ddc9eae commit 5c28889

File tree

1 file changed

+136
-135
lines changed

1 file changed

+136
-135
lines changed

test/index.js

Lines changed: 136 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -2,197 +2,196 @@ const fs = require('fs');
22
const path = require('path');
33
const { expect } = require('chai');
44
let hb;
5+
let blob, face, font, buffer;
56

67
before(async function () {
78
hb = await require('..');
89
});
910

1011
afterEach(function () {
11-
if (this.blob) this.blob.destroy();
12-
if (this.face) this.face.destroy();
13-
if (this.font) this.font.destroy();
14-
if (this.buffer) this.buffer.destroy();
15-
this.blob = this.face = this.font = this.buffer = null;
12+
if (blob) blob.destroy();
13+
if (face) face.destroy();
14+
if (font) font.destroy();
15+
if (buffer) buffer.destroy();
16+
blob = face = font = buffer = undefined;
1617
});
1718

1819
describe('Face', function () {
1920
it('collectUnicodes reflects codepoints supported by the font', function () {
20-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
21-
this.face = hb.createFace(this.blob);
22-
const codepoints = [...this.face.collectUnicodes()];
21+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
22+
face = hb.createFace(blob);
23+
const codepoints = [...face.collectUnicodes()];
2324
expect(codepoints).to.include('a'.codePointAt(0));
2425
expect(codepoints).not.to.include('ا'.codePointAt(0));
25-
this.face.destroy();
26-
this.blob.destroy();
2726
});
2827

2928
it('exposes upem', function () {
30-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
31-
this.face = hb.createFace(this.blob);
32-
expect(this.face.upem).to.equal(1000);
29+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
30+
face = hb.createFace(blob);
31+
expect(face.upem).to.equal(1000);
3332
});
3433

3534
it('getAxisInfos returns details of a variable font', function () {
36-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansArabic-Variable.ttf')));
37-
this.face = hb.createFace(this.blob);
38-
expect(this.face.getAxisInfos()).to.deep.equal({
35+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansArabic-Variable.ttf')));
36+
face = hb.createFace(blob);
37+
expect(face.getAxisInfos()).to.deep.equal({
3938
wght: { min: 100, default: 400, max: 900 },
4039
wdth: { min: 62.5, default: 100, max: 100 }
4140
});
4241
});
4342

4443
it('getAxisInfos returns an empty object for a non-variable font', function () {
45-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
46-
this.face = hb.createFace(this.blob);
47-
expect(Object.keys(this.face.getAxisInfos())).to.have.lengthOf(0);
44+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
45+
face = hb.createFace(blob);
46+
expect(Object.keys(face.getAxisInfos())).to.have.lengthOf(0);
4847
});
4948
});
5049

5150
describe('Font', function () {
5251
it('glyphName returns names for glyph ids', function () {
53-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
54-
this.face = hb.createFace(this.blob);
55-
this.font = hb.createFont(this.face);
56-
expect(this.font.glyphName(20)).to.equal('one');
52+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
53+
face = hb.createFace(blob);
54+
font = hb.createFont(face);
55+
expect(font.glyphName(20)).to.equal('one');
5756
});
5857

5958
it('setScale affects advances', function () {
60-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
61-
this.face = hb.createFace(this.blob);
62-
this.font = hb.createFont(this.face);
63-
this.buffer = hb.createBuffer();
64-
this.buffer.addText('a');
65-
this.buffer.guessSegmentProperties();
66-
this.font.setScale(1000 * 2, 1000 * 2);
67-
hb.shape(this.font, this.buffer)
68-
const glyphs = this.buffer.json();
59+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
60+
face = hb.createFace(blob);
61+
font = hb.createFont(face);
62+
buffer = hb.createBuffer();
63+
buffer.addText('a');
64+
buffer.guessSegmentProperties();
65+
font.setScale(1000 * 2, 1000 * 2);
66+
hb.shape(font, buffer)
67+
const glyphs = buffer.json();
6968
expect(glyphs[0].ax).to.equal(561 * 2);
7069
});
7170

7271
it('setVariations affects advances', function () {
73-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansArabic-Variable.ttf')));
74-
this.face = hb.createFace(this.blob);
75-
this.font = hb.createFont(this.face);
76-
this.font.setVariations({ 'wght': 789 });
77-
this.buffer = hb.createBuffer();
78-
this.buffer.addText('آلو');
79-
this.buffer.guessSegmentProperties();
80-
hb.shape(this.font, this.buffer)
81-
const glyphs = this.buffer.json();
72+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansArabic-Variable.ttf')));
73+
face = hb.createFace(blob);
74+
font = hb.createFont(face);
75+
font.setVariations({ 'wght': 789 });
76+
buffer = hb.createBuffer();
77+
buffer.addText('آلو');
78+
buffer.guessSegmentProperties();
79+
hb.shape(font, buffer)
80+
const glyphs = buffer.json();
8281
expect(glyphs[0].ax).to.equal(526);
8382
});
8483

8584
it('glyphToPath converts quadratic glyph to path', function () {
86-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
87-
this.face = hb.createFace(this.blob);
88-
this.font = hb.createFont(this.face);
85+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
86+
face = hb.createFace(blob);
87+
font = hb.createFont(face);
8988
const expected21 = 'M520,0L48,0L48,73L235,262Q289,316 326,358Q363,400 382,440.5Q401,481 401,529Q401,\
9089
588 366,618.5Q331,649 275,649Q223,649 183.5,631Q144,613 103,581L56,640Q98,675 152.5,699.5Q207,724 275,\
9190
724Q375,724 433,673.5Q491,623 491,534Q491,478 468,429Q445,380 404,332.5Q363,285 308,231L159,84L159,80L520,80L520,0Z';
92-
expect(this.font.glyphToPath(21)).to.equal(expected21);
91+
expect(font.glyphToPath(21)).to.equal(expected21);
9392
const expected22 = 'M493,547Q493,475 453,432.5Q413,390 345,376L345,372Q431,362 473,318Q515,274 515,203Q515,\
9493
141 486,92.5Q457,44 396.5,17Q336,-10 241,-10Q185,-10 137,-1.5Q89,7 45,29L45,111Q90,89 142,76.5Q194,64 242,64Q338,\
9594
64 380.5,101.5Q423,139 423,205Q423,272 370.5,301.5Q318,331 223,331L154,331L154,406L224,406Q312,406 357.5,443Q403,\
9695
480 403,541Q403,593 368,621.5Q333,650 273,650Q215,650 174,633Q133,616 93,590L49,650Q87,680 143.5,702Q200,724 272,\
9796
724Q384,724 438.5,674Q493,624 493,547Z';
98-
expect(this.font.glyphToPath(22)).to.equal(expected22);
97+
expect(font.glyphToPath(22)).to.equal(expected22);
9998
});
10099

101100
it('glyphToPath converts cubic glyph to path', function () {
102-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.otf')));
103-
this.face = hb.createFace(this.blob);
104-
this.font = hb.createFont(this.face);
101+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.otf')));
102+
face = hb.createFace(blob);
103+
font = hb.createFont(face);
105104
const expected21 = 'M520,0L520,80L159,80L159,84L308,231C418,338 491,422 491,534C491,652 408,724 275,724C184,724 112,\
106105
687 56,640L103,581C158,624 205,649 275,649C350,649 401,607 401,529C401,432 342,370 235,262L48,73L48,0L520,0Z';
107-
expect(this.font.glyphToPath(21)).to.equal(expected21);
106+
expect(font.glyphToPath(21)).to.equal(expected21);
108107
const expected22 = 'M493,547C493,649 421,724 272,724C176,724 100,690 49,650L93,590C146,625 196,650 273,650C353,\
109108
650 403,610 403,541C403,460 341,406 224,406L154,406L154,331L223,331C349,331 423,294 423,205C423,117 370,64 242,64C178,\
110109
64 105,81 45,111L45,29C104,0 166,-10 241,-10C430,-10 515,78 515,203C515,297 459,358 345,372L345,376C435,394 493,451 493,547Z';
111-
expect(this.font.glyphToPath(22)).to.equal(expected22);
110+
expect(font.glyphToPath(22)).to.equal(expected22);
112111
});
113112
});
114113

115114
describe('Buffer', function () {
116115
it('setDirection controls direction of glyphs', function () {
117-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
118-
this.face = hb.createFace(this.blob);
119-
this.font = hb.createFont(this.face);
120-
this.buffer = hb.createBuffer();
121-
this.buffer.addText('rtl');
122-
this.buffer.setDirection('rtl');
123-
hb.shape(this.font, this.buffer)
124-
const glyphs = this.buffer.json();
116+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
117+
face = hb.createFace(blob);
118+
font = hb.createFont(face);
119+
buffer = hb.createBuffer();
120+
buffer.addText('rtl');
121+
buffer.setDirection('rtl');
122+
hb.shape(font, buffer)
123+
const glyphs = buffer.json();
125124
expect(glyphs[0].g).to.equal(79); // l
126125
expect(glyphs[1].g).to.equal(87); // t
127126
expect(glyphs[2].g).to.equal(85); // r
128127
});
129128

130129
it('setClusterLevel affects cluster merging', function () {
131-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
132-
this.face = hb.createFace(this.blob);
133-
this.font = hb.createFont(this.face);
134-
this.buffer = hb.createBuffer();
135-
this.buffer.setClusterLevel(1);
136-
this.buffer.addText('x́');
137-
this.buffer.guessSegmentProperties();
138-
hb.shape(this.font, this.buffer)
139-
const glyphs = this.buffer.json();
130+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
131+
face = hb.createFace(blob);
132+
font = hb.createFont(face);
133+
buffer = hb.createBuffer();
134+
buffer.setClusterLevel(1);
135+
buffer.addText('x́');
136+
buffer.guessSegmentProperties();
137+
hb.shape(font, buffer)
138+
const glyphs = buffer.json();
140139
expect(glyphs[0].cl).to.equal(0);
141140
expect(glyphs[1].cl).to.equal(1);
142141
});
143142

144143
it('setFlags with PRESERVE_DEFAULT_IGNORABLES affects glyph ids', function () {
145-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
146-
this.face = hb.createFace(this.blob);
147-
this.font = hb.createFont(this.face);
148-
this.buffer = hb.createBuffer();
149-
this.buffer.addText('\u200dhi');
150-
this.buffer.setFlags(['PRESERVE_DEFAULT_IGNORABLES']);
151-
this.buffer.guessSegmentProperties();
152-
hb.shape(this.font, this.buffer)
153-
const glyphs = this.buffer.json();
144+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
145+
face = hb.createFace(blob);
146+
font = hb.createFont(face);
147+
buffer = hb.createBuffer();
148+
buffer.addText('\u200dhi');
149+
buffer.setFlags(['PRESERVE_DEFAULT_IGNORABLES']);
150+
buffer.guessSegmentProperties();
151+
hb.shape(font, buffer)
152+
const glyphs = buffer.json();
154153
expect(glyphs[0].g).not.to.equal(3 /* space */);
155154
});
156155
});
157156

158157
describe('shape', function () {
159158
it('shape Latin string', function () {
160-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
161-
this.face = hb.createFace(this.blob);
162-
this.font = hb.createFont(this.face);
163-
this.buffer = hb.createBuffer();
164-
this.buffer.addText('abc');
165-
this.buffer.guessSegmentProperties();
166-
hb.shape(this.font, this.buffer)
167-
const glyphs = this.buffer.json();
159+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
160+
face = hb.createFace(blob);
161+
font = hb.createFont(face);
162+
buffer = hb.createBuffer();
163+
buffer.addText('abc');
164+
buffer.guessSegmentProperties();
165+
hb.shape(font, buffer)
166+
const glyphs = buffer.json();
168167
expect(glyphs[0]).to.deep.equal({ cl: 0, g: 68, ax: 561, ay: 0, dx: 0, dy: 0, flags: 0 } /* a */);
169168
expect(glyphs[1]).to.deep.equal({ cl: 1, g: 69, ax: 615, ay: 0, dx: 0, dy: 0, flags: 0 } /* b */);
170169
expect(glyphs[2]).to.deep.equal({ cl: 2, g: 70, ax: 480, ay: 0, dx: 0, dy: 0, flags: 0 } /* c */);
171170
});
172171

173172
it('shape Arabic string', function () {
174-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansArabic-Variable.ttf')));
175-
this.face = hb.createFace(this.blob);
176-
this.font = hb.createFont(this.face);
177-
this.buffer = hb.createBuffer();
178-
this.buffer.addText('أبجد');
179-
this.buffer.guessSegmentProperties();
180-
hb.shape(this.font, this.buffer)
181-
const glyphs = this.buffer.json();
173+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansArabic-Variable.ttf')));
174+
face = hb.createFace(blob);
175+
font = hb.createFont(face);
176+
buffer = hb.createBuffer();
177+
buffer.addText('أبجد');
178+
buffer.guessSegmentProperties();
179+
hb.shape(font, buffer)
180+
const glyphs = buffer.json();
182181
expect(glyphs[0]).to.deep.equal({ cl: 3, g: 213, ax: 532, ay: 0, dx: 0, dy: 0, flags: 1 } /* د */);
183182
expect(glyphs[1]).to.deep.equal({ cl: 2, g: 529, ax: 637, ay: 0, dx: 0, dy: 0, flags: 1 } /* ج */);
184183
expect(glyphs[2]).to.deep.equal({ cl: 1, g: 101, ax: 269, ay: 0, dx: 0, dy: 0, flags: 0 } /* ب */);
185184
expect(glyphs[3]).to.deep.equal({ cl: 0, g: 50, ax: 235, ay: 0, dx: 0, dy: 0, flags: 0 } /* أ */);
186185
});
187186

188187
it('shape with tracing', function () {
189-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
190-
this.face = hb.createFace(this.blob);
191-
this.font = hb.createFont(this.face);
192-
this.buffer = hb.createBuffer();
193-
this.buffer.addText('abc');
194-
this.buffer.guessSegmentProperties();
195-
const result = hb.shapeWithTrace(this.font, this.buffer, "", 0, 0)
188+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
189+
face = hb.createFace(blob);
190+
font = hb.createFont(face);
191+
buffer = hb.createBuffer();
192+
buffer.addText('abc');
193+
buffer.guessSegmentProperties();
194+
const result = hb.shapeWithTrace(font, buffer, "", 0, 0)
196195
expect(result).to.have.lengthOf(42);
197196
expect(result[0]).to.deep.equal({
198197
"m": "start table GSUB script tag 'latn'",
@@ -215,13 +214,13 @@ describe('shape', function () {
215214
});
216215

217216
it('shape with tracing and features', function () {
218-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
219-
this.face = hb.createFace(this.blob);
220-
this.font = hb.createFont(this.face);
221-
this.buffer = hb.createBuffer();
222-
this.buffer.addText('fi AV');
223-
this.buffer.guessSegmentProperties();
224-
const result = hb.shapeWithTrace(this.font, this.buffer, "-liga,-kern", 0, 0)
217+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
218+
face = hb.createFace(blob);
219+
font = hb.createFont(face);
220+
buffer = hb.createBuffer();
221+
buffer.addText('fi AV');
222+
buffer.guessSegmentProperties();
223+
const result = hb.shapeWithTrace(font, buffer, "-liga,-kern", 0, 0)
225224
expect(result).to.have.lengthOf(29);
226225
expect(result[0]).to.deep.equal({
227226
"m": "start table GSUB script tag 'latn'",
@@ -248,45 +247,47 @@ describe('shape', function () {
248247
});
249248

250249
it('shape with 3-letter languae tag', function () {
251-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansDevanagari-Regular.otf')));
252-
this.face = hb.createFace(this.blob);
253-
this.font = hb.createFont(this.face);
254-
this.buffer = hb.createBuffer();
255-
this.buffer.addText('५ल');
256-
this.buffer.guessSegmentProperties();
257-
hb.shape(this.font, this.buffer)
258-
var glyphs = this.buffer.json();
250+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansDevanagari-Regular.otf')));
251+
face = hb.createFace(blob);
252+
font = hb.createFont(face);
253+
buffer = hb.createBuffer();
254+
buffer.addText('५ल');
255+
buffer.guessSegmentProperties();
256+
hb.shape(font, buffer)
257+
var glyphs = buffer.json();
259258
expect(glyphs).to.have.lengthOf(2);
260259
expect(glyphs[0].g).to.equal(118);
260+
buffer.destroy();
261261

262-
this.buffer = hb.createBuffer();
263-
this.buffer.addText('५ल');
264-
this.buffer.setLanguage('dty');
265-
this.buffer.guessSegmentProperties();
266-
hb.shape(this.font, this.buffer)
267-
var glyphs = this.buffer.json();
262+
buffer = hb.createBuffer();
263+
buffer.addText('५ल');
264+
buffer.setLanguage('dty');
265+
buffer.guessSegmentProperties();
266+
hb.shape(font, buffer)
267+
var glyphs = buffer.json();
268268
expect(glyphs).to.have.lengthOf(2);
269269
expect(glyphs[0].g).to.equal(123);
270270
});
271271

272272
it('shape with OpenType language tag', function () {
273-
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansDevanagari-Regular.otf')));
274-
this.face = hb.createFace(this.blob);
275-
this.font = hb.createFont(this.face);
276-
this.buffer = hb.createBuffer();
277-
this.buffer.addText('५ल');
278-
this.buffer.guessSegmentProperties();
279-
hb.shape(this.font, this.buffer)
280-
var glyphs = this.buffer.json();
273+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansDevanagari-Regular.otf')));
274+
face = hb.createFace(blob);
275+
font = hb.createFont(face);
276+
buffer = hb.createBuffer();
277+
buffer.addText('५ल');
278+
buffer.guessSegmentProperties();
279+
hb.shape(font, buffer)
280+
var glyphs = buffer.json();
281281
expect(glyphs).to.have.lengthOf(2);
282282
expect(glyphs[0].g).to.equal(118);
283+
buffer.destroy();
283284

284-
this.buffer = hb.createBuffer();
285-
this.buffer.addText('५ल');
286-
this.buffer.setLanguage('x-hbot-4e455020'); // 'NEP '
287-
this.buffer.guessSegmentProperties();
288-
hb.shape(this.font, this.buffer)
289-
var glyphs = this.buffer.json();
285+
buffer = hb.createBuffer();
286+
buffer.addText('५ल');
287+
buffer.setLanguage('x-hbot-4e455020'); // 'NEP '
288+
buffer.guessSegmentProperties();
289+
hb.shape(font, buffer)
290+
var glyphs = buffer.json();
290291
expect(glyphs).to.have.lengthOf(2);
291292
expect(glyphs[0].g).to.equal(123);
292293
});

0 commit comments

Comments
 (0)