Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reference error for certain fonts #286

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions src/glyph/CFFGlyph.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export default class CFFGlyph extends Glyph {
let op = stream.readUInt8();
if (op < 32) {
let index, subr, phase;
let c1x, c1y, c2x, c2y, c3x, c3y;
let c4x, c4y, c5x, c5y, c6x, c6y;
let pts;

switch (op) {
case 1: // hstem
case 3: // vstem
Expand Down Expand Up @@ -128,10 +132,10 @@ export default class CFFGlyph extends Glyph {

case 8: // rrcurveto
while (stack.length > 0) {
var c1x = x + stack.shift();
var c1y = y + stack.shift();
var c2x = c1x + stack.shift();
var c2y = c1y + stack.shift();
c1x = x + stack.shift();
c1y = y + stack.shift();
c2x = c1x + stack.shift();
c2y = c1y + stack.shift();
x = c2x + stack.shift();
y = c2y + stack.shift();
path.bezierCurveTo(c1x, c1y, c2x, c2y, x, y);
Expand All @@ -143,8 +147,8 @@ export default class CFFGlyph extends Glyph {
subr = subrs[index];
if (subr) {
usedSubrs[index] = true;
var p = stream.pos;
var e = end;
let p = stream.pos;
let e = end;
stream.pos = subr.offset;
end = subr.offset + subr.length;
parse();
Expand Down Expand Up @@ -241,10 +245,10 @@ export default class CFFGlyph extends Glyph {

case 24: // rcurveline
while (stack.length >= 8) {
var c1x = x + stack.shift();
var c1y = y + stack.shift();
var c2x = c1x + stack.shift();
var c2y = c1y + stack.shift();
c1x = x + stack.shift();
c1y = y + stack.shift();
c2x = c1x + stack.shift();
c2y = c1y + stack.shift();
x = c2x + stack.shift();
y = c2y + stack.shift();
path.bezierCurveTo(c1x, c1y, c2x, c2y, x, y);
Expand All @@ -262,10 +266,10 @@ export default class CFFGlyph extends Glyph {
path.lineTo(x, y);
}

var c1x = x + stack.shift();
var c1y = y + stack.shift();
var c2x = c1x + stack.shift();
var c2y = c1y + stack.shift();
c1x = x + stack.shift();
c1y = y + stack.shift();
c2x = c1x + stack.shift();
c2y = c1y + stack.shift();
x = c2x + stack.shift();
y = c2y + stack.shift();
path.bezierCurveTo(c1x, c1y, c2x, c2y, x, y);
Expand Down Expand Up @@ -312,8 +316,8 @@ export default class CFFGlyph extends Glyph {
subr = gsubrs[index];
if (subr) {
usedGsubrs[index] = true;
var p = stream.pos;
var e = end;
let p = stream.pos;
let e = end;
stream.pos = subr.offset;
end = subr.offset + subr.length;
parse();
Expand Down Expand Up @@ -493,14 +497,14 @@ export default class CFFGlyph extends Glyph {
c1y = y;
c2x = c1x + stack.shift();
c2y = c1y + stack.shift();
let c3x = c2x + stack.shift();
let c3y = c2y;
let c4x = c3x + stack.shift();
let c4y = c3y;
let c5x = c4x + stack.shift();
let c5y = c4y;
let c6x = c5x + stack.shift();
let c6y = c5y;
c3x = c2x + stack.shift();
c3y = c2y;
c4x = c3x + stack.shift();
c4y = c3y;
c5x = c4x + stack.shift();
c5y = c4y;
c6x = c5x + stack.shift();
c6y = c5y;
x = c6x;
y = c6y;

Expand All @@ -509,7 +513,7 @@ export default class CFFGlyph extends Glyph {
break;

case 35: // flex
let pts = [];
pts = [];

for (let i = 0; i <= 5; i++) {
x += stack.shift();
Expand Down
13 changes: 13 additions & 0 deletions test/issues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as fontkit from 'fontkit';

describe('issues', function () {
describe('#282 - ReferenceError: Cannot access \'c3x\' before initialization', function () {
it('should not throw a ReferenceError', function () {
let font = fontkit.openSync(new URL('data/PlayfairDisplay/PlayfairDisplay-Regular.otf', import.meta.url));

let glyph = font.getGlyph(5);

glyph.path;
});
});
});