Refactor the FontFaceObject.getPathGenerator method

- Reduce the overall indentation level, by making use of early returns.

 - Replace `var` with `let`.
This commit is contained in:
Jonas Jenwald 2018-06-13 11:02:02 +02:00
parent 778981ec89
commit fe288bb872

View File

@ -385,45 +385,41 @@ var FontFaceObject = (function FontFaceObjectClosure() {
return rule;
},
getPathGenerator:
function FontFaceObject_getPathGenerator(objs, character) {
if (!(character in this.compiledGlyphs)) {
var cmds = objs.get(this.loadedName + '_path_' + character);
var current, i, len;
// If we can, compile cmds into JS for MAXIMUM SPEED
if (this.isEvalSupported && IsEvalSupportedCached.value) {
var args, js = '';
for (i = 0, len = cmds.length; i < len; i++) {
current = cmds[i];
if (current.args !== undefined) {
args = current.args.join(',');
} else {
args = '';
}
js += 'c.' + current.cmd + '(' + args + ');\n';
}
// eslint-disable-next-line no-new-func
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') {
current.args = [size, -size];
}
c[current.cmd].apply(c, current.args);
}
};
}
getPathGenerator(objs, character) {
if (this.compiledGlyphs[character] !== undefined) {
return this.compiledGlyphs[character];
}
return this.compiledGlyphs[character];
let cmds = objs.get(this.loadedName + '_path_' + character), current;
// If we can, compile cmds into JS for MAXIMUM SPEED...
if (this.isEvalSupported && IsEvalSupportedCached.value) {
let args, js = '';
for (let i = 0, ii = cmds.length; i < ii; i++) {
current = cmds[i];
if (current.args !== undefined) {
args = current.args.join(',');
} else {
args = '';
}
js += 'c.' + current.cmd + '(' + args + ');\n';
}
// eslint-disable-next-line no-new-func
return this.compiledGlyphs[character] = new Function('c', 'size', js);
}
// ... but fall back on using Function.prototype.apply() if we're
// blocked from using eval() for whatever reason (like CSP policies).
return this.compiledGlyphs[character] = function(c, size) {
for (let i = 0, ii = cmds.length; i < ii; i++) {
current = cmds[i];
if (current.cmd === 'scale') {
current.args = [size, -size];
}
c[current.cmd].apply(c, current.args);
}
};
},
};