Skip to content

Commit fe37449

Browse files
authored
Fix for new Function call (foliojs#223)
1 parent f3f55a0 commit fe37449

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/glyph/Path.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ export default class Path {
2727
* @return {string}
2828
*/
2929
toFunction() {
30-
let cmds = this.commands.map(c => ` ctx.${c.command}(${c.args.join(', ')});`);
31-
return new Function('ctx', cmds.join('\n'));
30+
return ctx => {
31+
this.commands.forEach(c => {
32+
return ctx[c.command].apply(ctx, c.args)
33+
})
34+
};
3235
}
3336

3437
/**

test/glyphs.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,28 @@ describe('glyphs', function() {
1212
return assert.equal(glyph.constructor.name, 'TTFGlyph');
1313
});
1414

15-
it('should get a path for the glyph', function() {
15+
it('should get a path for the glyph as SVG', function() {
1616
let glyph = font.getGlyph(39);
1717
return assert.equal(glyph.path.toSVG(), 'M1368 745Q1368 383 1171.5 191.5Q975 0 606 0L201 0L201 1462L649 1462Q990 1462 1179 1273Q1368 1084 1368 745ZM1188 739Q1188 1025 1044.5 1170Q901 1315 618 1315L371 1315L371 147L578 147Q882 147 1035 296.5Q1188 446 1188 739Z');
1818
});
1919

20+
it('should get a path for the glyph as function', function() {
21+
let glyph = font.getGlyph(39);
22+
let results = [];
23+
let ctx = {
24+
moveTo: (...args) => results.push('moveTo(' + args.join(', ') + ');'),
25+
lineTo: (...args) => results.push('lineTo(' + args.join(', ') + ');'),
26+
quadraticCurveTo: (...args) => results.push('quadraticCurveTo(' + args.join(', ') + ');'),
27+
bezierCurveTo: (...args) => results.push('bezierCurveTo(' + args.join(', ') + ');'),
28+
closePath: (...args) => results.push('closePath(' + args.join(', ') + ');')
29+
};
30+
31+
let fn = glyph.path.toFunction();
32+
fn(ctx);
33+
34+
return assert.equal(results.join('\n'), 'moveTo(1368, 745);\nquadraticCurveTo(1368, 383, 1171.5, 191.5);\nquadraticCurveTo(975, 0, 606, 0);\nlineTo(201, 0);\nlineTo(201, 1462);\nlineTo(649, 1462);\nquadraticCurveTo(990, 1462, 1179, 1273);\nquadraticCurveTo(1368, 1084, 1368, 745);\nclosePath();\nmoveTo(1188, 739);\nquadraticCurveTo(1188, 1025, 1044.5, 1170);\nquadraticCurveTo(901, 1315, 618, 1315);\nlineTo(371, 1315);\nlineTo(371, 147);\nlineTo(578, 147);\nquadraticCurveTo(882, 147, 1035, 296.5);\nquadraticCurveTo(1188, 446, 1188, 739);\nclosePath();');
35+
});
36+
2037
it('should get a composite glyph', function() {
2138
let glyph = font.getGlyph(171); // é
2239
return assert.equal(glyph.path.toSVG(), 'M639 -20Q396 -20 255.5 128Q115 276 115 539Q115 804 245.5 960Q376 1116 596 1116Q802 1116 922 980.5Q1042 845 1042 623L1042 518L287 518Q292 325 384.5 225Q477 125 645 125Q822 125 995 199L995 51Q907 13 828.5 -3.5Q750 -20 639 -20ZM594 977Q462 977 383.5 891Q305 805 291 653L864 653Q864 810 794 893.5Q724 977 594 977ZM471 1266Q519 1328 574.5 1416Q630 1504 662 1569L864 1569L864 1548Q820 1483 733 1388Q646 1293 582 1241L471 1241Z');
@@ -71,11 +88,28 @@ describe('glyphs', function() {
7188
return assert.equal(glyph.constructor.name, 'CFFGlyph');
7289
});
7390

74-
it('should get a path for the glyph', function() {
91+
it('should get a path for the glyph as SVG', function() {
7592
let glyph = font.getGlyph(5);
7693
return assert.equal(glyph.path.toSVG(), 'M90 0L258 0C456 0 564 122 564 331C564 539 456 656 254 656L90 656ZM173 68L173 588L248 588C401 588 478 496 478 331C478 165 401 68 248 68Z');
7794
});
7895

96+
it('should get a path for the glyph as function', function() {
97+
let glyph = font.getGlyph(5);
98+
let results = [];
99+
let ctx = {
100+
moveTo: (...args) => results.push('moveTo(' + args.join(', ') + ');'),
101+
lineTo: (...args) => results.push('lineTo(' + args.join(', ') + ');'),
102+
quadraticCurveTo: (...args) => results.push('quadraticCurveTo(' + args.join(', ') + ');'),
103+
bezierCurveTo: (...args) => results.push('bezierCurveTo(' + args.join(', ') + ');'),
104+
closePath: (...args) => results.push('closePath(' + args.join(', ') + ');')
105+
};
106+
107+
let fn = glyph.path.toFunction();
108+
fn(ctx);
109+
110+
return assert.equal(results.join('\n'), 'moveTo(90, 0);\nlineTo(258, 0);\nbezierCurveTo(456, 0, 564, 122, 564, 331);\nbezierCurveTo(564, 539, 456, 656, 254, 656);\nlineTo(90, 656);\nclosePath();\nmoveTo(173, 68);\nlineTo(173, 588);\nlineTo(248, 588);\nbezierCurveTo(401, 588, 478, 496, 478, 331);\nbezierCurveTo(478, 165, 401, 68, 248, 68);\nclosePath();');
111+
});
112+
79113
it('should get the glyph cbox', function() {
80114
let glyph = font.getGlyph(5);
81115
return assert.deepEqual(glyph.cbox, new BBox(90, 0, 564, 656));

0 commit comments

Comments
 (0)