From caf90ff6eeb902ebb93848aea8c248928157ec24 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 16 Aug 2018 20:34:31 +0200 Subject: [PATCH] Convert `FontFaceObject` to an ES6 class Also changes `var` to `let`/`const` in code already touched in the patch, and makes use of template strings in a few spots. --- src/display/font_loader.js | 176 +++++++++++++++++-------------------- 1 file changed, 82 insertions(+), 94 deletions(-) diff --git a/src/display/font_loader.js b/src/display/font_loader.js index 1e73925d9..5556bc708 100644 --- a/src/display/font_loader.js +++ b/src/display/font_loader.js @@ -330,21 +330,21 @@ if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('MOZCENTRAL || CHROME')) { }); } -var IsEvalSupportedCached = { +const IsEvalSupportedCached = { get value() { return shadow(this, 'value', isEvalSupported()); }, }; -var FontFaceObject = (function FontFaceObjectClosure() { - function FontFaceObject(translatedData, { isEvalSupported = true, - disableFontFace = false, - ignoreErrors = false, - onUnsupportedFeature = null, - fontRegistry = null, }) { +class FontFaceObject { + constructor(translatedData, { isEvalSupported = true, + disableFontFace = false, + ignoreErrors = false, + onUnsupportedFeature = null, + fontRegistry = null, }) { this.compiledGlyphs = Object.create(null); // importing translated data - for (var i in translatedData) { + for (let i in translatedData) { this[i] = translatedData[i]; } this.isEvalSupported = isEvalSupported !== false; @@ -353,98 +353,86 @@ var FontFaceObject = (function FontFaceObjectClosure() { this._onUnsupportedFeature = onUnsupportedFeature; this.fontRegistry = fontRegistry; } - FontFaceObject.prototype = { - createNativeFontFace: function FontFaceObject_createNativeFontFace() { - if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('MOZCENTRAL')) { - throw new Error('Not implemented: createNativeFontFace'); + + createNativeFontFace() { + if (!this.data || this.disableFontFace) { + return null; + } + const nativeFontFace = new FontFace(this.loadedName, this.data, {}); + + if (this.fontRegistry) { + this.fontRegistry.registerFont(this); + } + return nativeFontFace; + } + + createFontFaceRule() { + if (!this.data || this.disableFontFace) { + return null; + } + const data = bytesToString(new Uint8Array(this.data)); + // Add the @font-face rule to the document. + const url = `url(data:${this.mimetype};base64,${btoa(data)});`; + const rule = `@font-face {font-family:"${this.loadedName}";src:${url}}`; + + if (this.fontRegistry) { + this.fontRegistry.registerFont(this, url); + } + return rule; + } + + getPathGenerator(objs, character) { + if (this.compiledGlyphs[character] !== undefined) { + return this.compiledGlyphs[character]; + } + + let cmds, current; + try { + cmds = objs.get(this.loadedName + '_path_' + character); + } catch (ex) { + if (!this.ignoreErrors) { + throw ex; } - - if (!this.data || this.disableFontFace) { - return null; + if (this._onUnsupportedFeature) { + this._onUnsupportedFeature({ featureId: UNSUPPORTED_FEATURES.font, }); } + warn(`getPathGenerator - ignoring character: "${ex}".`); - var nativeFontFace = new FontFace(this.loadedName, this.data, {}); - - if (this.fontRegistry) { - this.fontRegistry.registerFont(this); - } - return nativeFontFace; - }, - - createFontFaceRule: function FontFaceObject_createFontFaceRule() { - if (!this.data || this.disableFontFace) { - return null; - } - - var data = bytesToString(new Uint8Array(this.data)); - var fontName = this.loadedName; - - // Add the font-face rule to the document - var url = ('url(data:' + this.mimetype + ';base64,' + btoa(data) + ');'); - var rule = '@font-face { font-family:"' + fontName + '";src:' + url + '}'; - - if (this.fontRegistry) { - this.fontRegistry.registerFont(this, url); - } - - return rule; - }, - - getPathGenerator(objs, character) { - if (this.compiledGlyphs[character] !== undefined) { - return this.compiledGlyphs[character]; - } - - let cmds, current; - try { - cmds = objs.get(this.loadedName + '_path_' + character); - } catch (ex) { - if (!this.ignoreErrors) { - throw ex; - } - if (this._onUnsupportedFeature) { - this._onUnsupportedFeature({ featureId: UNSUPPORTED_FEATURES.font, }); - } - warn(`getPathGenerator - ignoring character: "${ex}".`); - - return this.compiledGlyphs[character] = function(c, size) { - // No-op function, to allow rendering to continue. - }; - } - - // 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); - } + // No-op function, to allow rendering to continue. }; - }, - }; + } - return FontFaceObject; -})(); + // 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); + } + }; + } +} export { FontFaceObject,