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.
This commit is contained in:
parent
b40fb3814a
commit
caf90ff6ee
@ -330,21 +330,21 @@ if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('MOZCENTRAL || CHROME')) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var IsEvalSupportedCached = {
|
const IsEvalSupportedCached = {
|
||||||
get value() {
|
get value() {
|
||||||
return shadow(this, 'value', isEvalSupported());
|
return shadow(this, 'value', isEvalSupported());
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
var FontFaceObject = (function FontFaceObjectClosure() {
|
class FontFaceObject {
|
||||||
function FontFaceObject(translatedData, { isEvalSupported = true,
|
constructor(translatedData, { isEvalSupported = true,
|
||||||
disableFontFace = false,
|
disableFontFace = false,
|
||||||
ignoreErrors = false,
|
ignoreErrors = false,
|
||||||
onUnsupportedFeature = null,
|
onUnsupportedFeature = null,
|
||||||
fontRegistry = null, }) {
|
fontRegistry = null, }) {
|
||||||
this.compiledGlyphs = Object.create(null);
|
this.compiledGlyphs = Object.create(null);
|
||||||
// importing translated data
|
// importing translated data
|
||||||
for (var i in translatedData) {
|
for (let i in translatedData) {
|
||||||
this[i] = translatedData[i];
|
this[i] = translatedData[i];
|
||||||
}
|
}
|
||||||
this.isEvalSupported = isEvalSupported !== false;
|
this.isEvalSupported = isEvalSupported !== false;
|
||||||
@ -353,98 +353,86 @@ var FontFaceObject = (function FontFaceObjectClosure() {
|
|||||||
this._onUnsupportedFeature = onUnsupportedFeature;
|
this._onUnsupportedFeature = onUnsupportedFeature;
|
||||||
this.fontRegistry = fontRegistry;
|
this.fontRegistry = fontRegistry;
|
||||||
}
|
}
|
||||||
FontFaceObject.prototype = {
|
|
||||||
createNativeFontFace: function FontFaceObject_createNativeFontFace() {
|
createNativeFontFace() {
|
||||||
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('MOZCENTRAL')) {
|
if (!this.data || this.disableFontFace) {
|
||||||
throw new Error('Not implemented: createNativeFontFace');
|
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._onUnsupportedFeature) {
|
||||||
if (!this.data || this.disableFontFace) {
|
this._onUnsupportedFeature({ featureId: UNSUPPORTED_FEATURES.font, });
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
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) {
|
return this.compiledGlyphs[character] = function(c, size) {
|
||||||
for (let i = 0, ii = cmds.length; i < ii; i++) {
|
// No-op function, to allow rendering to continue.
|
||||||
current = cmds[i];
|
|
||||||
|
|
||||||
if (current.cmd === 'scale') {
|
|
||||||
current.args = [size, -size];
|
|
||||||
}
|
|
||||||
c[current.cmd].apply(c, current.args);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
};
|
|
||||||
|
|
||||||
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 {
|
export {
|
||||||
FontFaceObject,
|
FontFaceObject,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user