diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 2f9e6897e..4c63dc7d7 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -1086,8 +1086,7 @@ class PartialEvaluator { if ( isAddToPathSet || state.fillColorSpace.name === "Pattern" || - font.disableFontFace || - this.options.disableFontFace + font.disableFontFace ) { PartialEvaluator.buildFontPaths( font, @@ -4367,7 +4366,7 @@ class PartialEvaluator { newProperties ); } - return new Font(baseFontName, file, newProperties); + return new Font(baseFontName, file, newProperties, this.options); } } @@ -4559,7 +4558,7 @@ class PartialEvaluator { const newProperties = await this.extractDataStructures(dict, properties); this.extractWidths(dict, descriptor, newProperties); - return new Font(fontName.name, fontFile, newProperties); + return new Font(fontName.name, fontFile, newProperties, this.options); } static buildFontPaths(font, glyphs, handler, evaluatorOptions) { @@ -4626,7 +4625,7 @@ class TranslatedFont { handler.send("commonobj", [ this.loadedName, "Font", - this.font.exportData(this._evaluatorOptions.fontExtraProperties), + this.font.exportData(), ]); } diff --git a/src/core/fonts.js b/src/core/fonts.js index 3769c88a6..a57a3762b 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -88,7 +88,9 @@ const EXPORT_DATA_PROPERTIES = [ "defaultVMetrics", "defaultWidth", "descent", + "disableFontFace", "fallbackName", + "fontExtraProperties", "fontMatrix", "isInvalidPDFjsFont", "isType3Font", @@ -970,11 +972,12 @@ function createNameTable(name, proto) { * decoding logics whatever type it is (assuming the font type is supported). */ class Font { - constructor(name, file, properties) { + constructor(name, file, properties, evaluatorOptions) { this.name = name; this.psName = null; this.mimetype = null; - this.disableFontFace = false; + this.disableFontFace = evaluatorOptions.disableFontFace; + this.fontExtraProperties = evaluatorOptions.fontExtraProperties; this.loadedName = properties.loadedName; this.isType3Font = properties.isType3Font; @@ -1141,18 +1144,17 @@ class Font { return shadow(this, "renderer", renderer); } - exportData(extraProperties = false) { - const exportDataProperties = extraProperties + exportData() { + const exportDataProps = this.fontExtraProperties ? [...EXPORT_DATA_PROPERTIES, ...EXPORT_DATA_EXTRA_PROPERTIES] : EXPORT_DATA_PROPERTIES; const data = Object.create(null); - let property, value; - for (property of exportDataProperties) { - value = this[property]; + for (const prop of exportDataProps) { + const value = this[prop]; // Ignore properties that haven't been explicitly set. if (value !== undefined) { - data[property] = value; + data[prop] = value; } } return data; @@ -3602,7 +3604,7 @@ class ErrorFont { return [chars]; } - exportData(extraProperties = false) { + exportData() { return { error: this.error }; } } diff --git a/src/display/api.js b/src/display/api.js index 99ee4ba06..d5aff8bc9 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -422,8 +422,6 @@ function getDocument(src = {}) { }, }; const transportParams = { - disableFontFace, - fontExtraProperties, ownerDocument, pdfBug, styleElement, @@ -2787,8 +2785,6 @@ class WorkerTransport { switch (type) { case "Font": - const { disableFontFace, fontExtraProperties, pdfBug } = this._params; - if ("error" in exportedData) { const exportedError = exportedData.error; warn(`Error during font loading: ${exportedError}`); @@ -2797,20 +2793,16 @@ class WorkerTransport { } const inspectFont = - pdfBug && globalThis.FontInspector?.enabled + this._params.pdfBug && globalThis.FontInspector?.enabled ? (font, url) => globalThis.FontInspector.fontAdded(font, url) : null; - const font = new FontFaceObject(exportedData, { - disableFontFace, - fontExtraProperties, - inspectFont, - }); + const font = new FontFaceObject(exportedData, inspectFont); this.fontLoader .bind(font) .catch(() => messageHandler.sendWithPromise("FontFallback", { id })) .finally(() => { - if (!fontExtraProperties && font.data) { + if (!font.fontExtraProperties && font.data) { // Immediately release the `font.data` property once the font // has been attached to the DOM, since it's no longer needed, // rather than waiting for a `PDFDocumentProxy.cleanup` call. diff --git a/src/display/font_loader.js b/src/display/font_loader.js index 06efa39e2..94946614d 100644 --- a/src/display/font_loader.js +++ b/src/display/font_loader.js @@ -351,17 +351,20 @@ class FontLoader { } class FontFaceObject { - constructor( - translatedData, - { disableFontFace = false, fontExtraProperties = false, inspectFont = null } - ) { + constructor(translatedData, inspectFont = null) { this.compiledGlyphs = Object.create(null); // importing translated data for (const i in translatedData) { this[i] = translatedData[i]; } - this.disableFontFace = disableFontFace === true; - this.fontExtraProperties = fontExtraProperties === true; + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { + if (typeof this.disableFontFace !== "boolean") { + unreachable("disableFontFace must be available."); + } + if (typeof this.fontExtraProperties !== "boolean") { + unreachable("fontExtraProperties must be available."); + } + } this._inspectFont = inspectFont; } diff --git a/test/font/font_fpgm_spec.js b/test/font/font_fpgm_spec.js index 66ca8752b..45501608e 100644 --- a/test/font/font_fpgm_spec.js +++ b/test/font/font_fpgm_spec.js @@ -16,17 +16,22 @@ describe("font_fpgm", function () { const cMap = await CMapFactory.create({ encoding: Name.get("Identity-H"), }); - const font = new Font("font", new Stream(font2324), { - loadedName: "font", - type: "CIDFontType2", - differences: [], - defaultEncoding: [], - cMap, - toUnicode: new ToUnicodeMap([]), - xHeight: 0, - capHeight: 0, - italicAngle: 0, - }); + const font = new Font( + "font", + new Stream(font2324), + { + loadedName: "font", + type: "CIDFontType2", + differences: [], + defaultEncoding: [], + cMap, + toUnicode: new ToUnicodeMap([]), + xHeight: 0, + capHeight: 0, + italicAngle: 0, + }, + {} + ); const output = await ttx(font.data); verifyTtxOutput(output); diff --git a/test/font/font_os2_spec.js b/test/font/font_os2_spec.js index 04359d316..d348a6b4c 100644 --- a/test/font/font_os2_spec.js +++ b/test/font/font_os2_spec.js @@ -17,16 +17,21 @@ describe("font_post", function () { describe("OS/2 table removal on bad post table values", function () { it("has invalid version number", async function () { - const font = new Font("font", new Stream(font2154), { - loadedName: "font", - type: "TrueType", - differences: [], - defaultEncoding: [], - toUnicode: new ToUnicodeMap([]), - xHeight: 0, - capHeight: 0, - italicAngle: 0, - }); + const font = new Font( + "font", + new Stream(font2154), + { + loadedName: "font", + type: "TrueType", + differences: [], + defaultEncoding: [], + toUnicode: new ToUnicodeMap([]), + xHeight: 0, + capHeight: 0, + italicAngle: 0, + }, + {} + ); const output = await ttx(font.data); verifyTtxOutput(output); @@ -39,17 +44,22 @@ describe("font_post", function () { const cMap = await CMapFactory.create({ encoding: Name.get("Identity-H"), }); - const font = new Font("font", new Stream(font1282), { - loadedName: "font", - type: "CIDFontType2", - differences: [], - defaultEncoding: [], - cMap, - toUnicode: new ToUnicodeMap([]), - xHeight: 0, - capHeight: 0, - italicAngle: 0, - }); + const font = new Font( + "font", + new Stream(font1282), + { + loadedName: "font", + type: "CIDFontType2", + differences: [], + defaultEncoding: [], + cMap, + toUnicode: new ToUnicodeMap([]), + xHeight: 0, + capHeight: 0, + italicAngle: 0, + }, + {} + ); const output = await ttx(font.data); verifyTtxOutput(output); diff --git a/test/font/font_post_spec.js b/test/font/font_post_spec.js index d00ddcdbe..116d77169 100644 --- a/test/font/font_post_spec.js +++ b/test/font/font_post_spec.js @@ -24,17 +24,22 @@ describe("font_post", function () { const cMap = await CMapFactory.create({ encoding: Name.get("Identity-H"), }); - const font = new Font("font", new Stream(font2109), { - loadedName: "font", - type: "CIDFontType2", - differences: [], - defaultEncoding: [], - cMap, - toUnicode: new ToUnicodeMap([]), - xHeight: 0, - capHeight: 0, - italicAngle: 0, - }); + const font = new Font( + "font", + new Stream(font2109), + { + loadedName: "font", + type: "CIDFontType2", + differences: [], + defaultEncoding: [], + cMap, + toUnicode: new ToUnicodeMap([]), + xHeight: 0, + capHeight: 0, + italicAngle: 0, + }, + {} + ); const output = await ttx(font.data); verifyTtxOutput(output); @@ -42,16 +47,21 @@ describe("font_post", function () { }); it("has invalid glyph name indexes", async function () { - const font = new Font("font", new Stream(font2189), { - loadedName: "font", - type: "TrueType", - differences: [], - defaultEncoding: [], - toUnicode: new ToUnicodeMap([]), - xHeight: 0, - capHeight: 0, - italicAngle: 0, - }); + const font = new Font( + "font", + new Stream(font2189), + { + loadedName: "font", + type: "TrueType", + differences: [], + defaultEncoding: [], + toUnicode: new ToUnicodeMap([]), + xHeight: 0, + capHeight: 0, + italicAngle: 0, + }, + {} + ); const output = await ttx(font.data); verifyTtxOutput(output); @@ -59,16 +69,21 @@ describe("font_post", function () { }); it("has right amount of glyphs specified", async function () { - const font = new Font("font", new Stream(font2374), { - loadedName: "font", - type: "TrueType", - differences: [], - defaultEncoding: [], - toUnicode: new ToUnicodeMap([]), - xHeight: 0, - capHeight: 0, - italicAngle: 0, - }); + const font = new Font( + "font", + new Stream(font2374), + { + loadedName: "font", + type: "TrueType", + differences: [], + defaultEncoding: [], + toUnicode: new ToUnicodeMap([]), + xHeight: 0, + capHeight: 0, + italicAngle: 0, + }, + {} + ); const output = await ttx(font.data); verifyTtxOutput(output);