Skip to content

Commit

Permalink
[PATCH] Add fallback for font loading when eval disabled
Browse files Browse the repository at this point in the history
In some cases, such as in use with a CSP header, constructing a function with a
string of javascript is not allowed. However, compiling the various commands
that need to be done on the canvas element is faster than interpreting them.
This patch changes the font renderer to instead emit commands that are compiled
by the font loader. If, during compilation, we receive an EvalError, we instead
interpret them.
  • Loading branch information
skalnik committed Jul 28, 2015
1 parent 18e1a14 commit 8f044d6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 34 deletions.
63 changes: 32 additions & 31 deletions src/core/font_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,15 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
return 0;
}

function compileGlyf(code, js, font) {
function compileGlyf(code, cmds, font) {
function moveTo(x, y) {
js.push('c.moveTo(' + x + ',' + y + ');');
cmds.push({cmd: 'moveTo', args: [x, y]});
}
function lineTo(x, y) {
js.push('c.lineTo(' + x + ',' + y + ');');
cmds.push({cmd: 'lineTo', args: [x, y]});
}
function quadraticCurveTo(xa, ya, x, y) {
js.push('c.quadraticCurveTo(' + xa + ',' + ya + ',' +
x + ',' + y + ');');
cmds.push({cmd: 'quadraticCurveTo', args: [xa, ya, x, y]});
}

var i = 0;
Expand Down Expand Up @@ -189,11 +188,11 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
}
var subglyph = font.glyphs[glyphIndex];
if (subglyph) {
js.push('c.save();');
js.push('c.transform(' + scaleX + ',' + scale01 + ',' +
scale10 + ',' + scaleY + ',' + x + ',' + y + ');');
compileGlyf(subglyph, js, font);
js.push('c.restore();');
cmds.push({cmd: 'save'});
cmds.push({cmd: 'transform',
args: [scaleX, scale01, scale10, scaleY, x, y]});
compileGlyf(subglyph, cmds, font);
cmds.push({cmd: 'restore'});
}
} while ((flags & 0x20));
} else {
Expand Down Expand Up @@ -289,20 +288,19 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
}
}

function compileCharString(code, js, font) {
function compileCharString(code, cmds, font) {
var stack = [];
var x = 0, y = 0;
var stems = 0;

function moveTo(x, y) {
js.push('c.moveTo(' + x + ',' + y + ');');
cmds.push({cmd: 'moveTo', args: [x, y]});
}
function lineTo(x, y) {
js.push('c.lineTo(' + x + ',' + y + ');');
cmds.push({cmd: 'lineTo', args: [x, y]});
}
function bezierCurveTo(x1, y1, x2, y2, x, y) {
js.push('c.bezierCurveTo(' + x1 + ',' + y1 + ',' + x2 + ',' + y2 + ',' +
x + ',' + y + ');');
cmds.push({cmd: 'bezierCurveTo', args: [x1, y1, x2, y2, x, y]});
}

function parse(code) {
Expand Down Expand Up @@ -431,16 +429,16 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
var bchar = stack.pop();
y = stack.pop();
x = stack.pop();
js.push('c.save();');
js.push('c.translate('+ x + ',' + y + ');');
cmds.push({cmd: 'save'});
cmds.push({cmd: 'translate', args: [x, y]});
var gid = lookupCmap(font.cmap, String.fromCharCode(
font.glyphNameMap[Encodings.StandardEncoding[achar]]));
compileCharString(font.glyphs[gid], js, font);
js.push('c.restore();');
compileCharString(font.glyphs[gid], cmds, font);
cmds.push({cmd: 'restore'});

gid = lookupCmap(font.cmap, String.fromCharCode(
font.glyphNameMap[Encodings.StandardEncoding[bchar]]));
compileCharString(font.glyphs[gid], js, font);
compileCharString(font.glyphs[gid], cmds, font);
}
return;
case 18: // hstemhm
Expand Down Expand Up @@ -609,16 +607,19 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
return noop;
}

var js = [];
js.push('c.save();');
js.push('c.transform(' + this.fontMatrix.join(',') + ');');
js.push('c.scale(size, -size);');
var cmds = [];
cmds.push({cmd: 'save'});
cmds.push({cmd: 'transform',
args: [this.fontMatrix[0], this.fontMatrix[1],
this.fontMatrix[2], this.fontMatrix[3],
this.fontMatrix[4], this.fontMatrix[5]]});
cmds.push({cmd: 'scale', args: ['size', '-size']});

this.compileGlyphImpl(code, js);
this.compileGlyphImpl(code, cmds);

js.push('c.restore();');
cmds.push({cmd: 'restore'});

return js.join('\n');
return cmds;
},

compileGlyphImpl: function () {
Expand All @@ -642,8 +643,8 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
}

Util.inherit(TrueTypeCompiled, CompiledFont, {
compileGlyphImpl: function (code, js) {
compileGlyf(code, js, this);
compileGlyphImpl: function (code, cmds) {
compileGlyf(code, cmds, this);
}
});

Expand All @@ -664,8 +665,8 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
}

Util.inherit(Type2Compiled, CompiledFont, {
compileGlyphImpl: function (code, js) {
compileCharString(code, js, this);
compileGlyphImpl: function (code, cmds) {
compileCharString(code, cmds, this);
}
});

Expand Down
52 changes: 49 additions & 3 deletions src/display/font_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ var FontLoader = {
));
},

get isEvalSupported() {
var evalSupport;
if (PDFJS.isEvalSupported) {
try {
/* jshint evil: true */
new Function('console.log("test");');
evalSupport = true;
} catch (e) {
evalSupport = false;
}
} else {
evalSupport = false;
}
return shadow(this, 'isEvalSupported', evalSupport);
},

loadTestFontId: 0,

loadingContext: {
Expand Down Expand Up @@ -365,9 +381,39 @@ var FontFaceObject = (function FontFaceObjectClosure() {

getPathGenerator: function FontLoader_getPathGenerator(objs, character) {
if (!(character in this.compiledGlyphs)) {
var js = objs.get(this.loadedName + '_path_' + character);
/*jshint -W054 */
this.compiledGlyphs[character] = new Function('c', 'size', js);
var cmds = objs.get(this.loadedName + '_path_' + character);
var js = '';
var current, i, len, j, alen;

// If we can, compile cmds into JS for MAXIMUM SPEED
if (FontLoader.isEvalSupported) {
for (i = 0, len = cmds.length; i < len; i++) {
current = cmds[i];
js += 'c.' + current.cmd + '(' + current.args.join(',') + ');\n';
}
/* jshint -W054 */
this.compiledGlyphs[character] = new Function('c', 'size', js);
} else {
// But fall back on using Function.prototype.apply() if we're
// blocked from using eval() for whatever reason (like CSP policies)
this.compiledGlyphs[character] = function(c, size) {
for (i = 0, len = cmds.length; i < len; i++) {
current = cmds[i];

if (current.cmd === 'scale') {
for (j = 0, alen = current.args.length; j < alen; j++) {
if (current.args[j] === 'size') {
current.args[j] = size;
} else if (current.args[j] === '-size') {
current.args[j] = -size;
}
}
}

c[current.cmd].apply(c, current.args);
}
};
}
}
return this.compiledGlyphs[character];
}
Expand Down

0 comments on commit 8f044d6

Please sign in to comment.