Re-factor FontFaceObject.getPathGenerator to use Arrays instead of strings

This is similar to a lot of other code, where we use "Array + join" rather than repeated string concatenation.
This commit is contained in:
Jonas Jenwald 2021-05-01 17:59:33 +02:00
parent 3538ef017f
commit dcbb23d7fa

View File

@ -422,7 +422,7 @@ class FontFaceObject {
return this.compiledGlyphs[character]; return this.compiledGlyphs[character];
} }
let cmds, current; let cmds;
try { try {
cmds = objs.get(this.loadedName + "_path_" + character); cmds = objs.get(this.loadedName + "_path_" + character);
} catch (ex) { } catch (ex) {
@ -441,27 +441,22 @@ class FontFaceObject {
// If we can, compile cmds into JS for MAXIMUM SPEED... // If we can, compile cmds into JS for MAXIMUM SPEED...
if (this.isEvalSupported && IsEvalSupportedCached.value) { if (this.isEvalSupported && IsEvalSupportedCached.value) {
let args, const jsBuf = [];
js = ""; for (const current of cmds) {
for (let i = 0, ii = cmds.length; i < ii; i++) { const args = current.args !== undefined ? current.args.join(",") : "";
current = cmds[i]; jsBuf.push("c.", current.cmd, "(", args, ");\n");
if (current.args !== undefined) {
args = current.args.join(",");
} else {
args = "";
}
js += "c." + current.cmd + "(" + args + ");\n";
} }
// eslint-disable-next-line no-new-func // eslint-disable-next-line no-new-func
return (this.compiledGlyphs[character] = new Function("c", "size", js)); return (this.compiledGlyphs[character] = new Function(
"c",
"size",
jsBuf.join("")
));
} }
// ... but fall back on using Function.prototype.apply() if we're // ... but fall back on using Function.prototype.apply() if we're
// blocked from using eval() for whatever reason (like CSP policies). // blocked from using eval() for whatever reason (like CSP policies).
return (this.compiledGlyphs[character] = function (c, size) { return (this.compiledGlyphs[character] = function (c, size) {
for (let i = 0, ii = cmds.length; i < ii; i++) { for (const current of cmds) {
current = cmds[i];
if (current.cmd === "scale") { if (current.cmd === "scale") {
current.args = [size, -size]; current.args = [size, -size];
} }