Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send disableFontFace and fontExtraProperties as part of the exported font-data #19548

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -1169,17 +1169,6 @@ class Catalog {
return shadow(this, "jsActions", actions);
}

async fontFallback(id, handler) {
const translatedFonts = await Promise.all(this.fontCache);

for (const translatedFont of translatedFonts) {
if (translatedFont.loadedName === id) {
translatedFont.fallback(handler);
return;
}
}
}

async cleanup(manuallyTriggered = false) {
clearGlobalCaches();
this.globalImageCache.clear(/* onlyData = */ manuallyTriggered);
Expand Down
11 changes: 9 additions & 2 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1745,8 +1745,15 @@ class PDFDocument {
}
}

fontFallback(id, handler) {
return this.catalog.fontFallback(id, handler);
async fontFallback(id, handler) {
const { catalog, pdfManager } = this;

for (const translatedFont of await Promise.all(catalog.fontCache)) {
if (translatedFont.loadedName === id) {
translatedFont.fallback(handler, pdfManager.evaluatorOptions);
return;
}
}
}

async cleanup(manuallyTriggered = false) {
Expand Down
20 changes: 7 additions & 13 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,6 @@ class PartialEvaluator {
loadedName: "g_font_error",
font: new ErrorFont(`Type3 font load error: ${reason}`),
dict: translated.font,
evaluatorOptions: this.options,
});
}
}
Expand All @@ -1086,8 +1085,7 @@ class PartialEvaluator {
if (
isAddToPathSet ||
state.fillColorSpace.name === "Pattern" ||
font.disableFontFace ||
this.options.disableFontFace
font.disableFontFace
) {
PartialEvaluator.buildFontPaths(
font,
Expand Down Expand Up @@ -1238,7 +1236,6 @@ class PartialEvaluator {
loadedName: "g_font_error",
font: new ErrorFont(`Font "${fontName}" is not available.`),
dict: font,
evaluatorOptions: this.options,
});

let fontRef;
Expand Down Expand Up @@ -1365,7 +1362,6 @@ class PartialEvaluator {
loadedName: font.loadedName,
font: translatedFont,
dict: font,
evaluatorOptions: this.options,
})
);
})
Expand All @@ -1380,7 +1376,6 @@ class PartialEvaluator {
reason instanceof Error ? reason.message : reason
),
dict: font,
evaluatorOptions: this.options,
})
);
});
Expand Down Expand Up @@ -4367,7 +4362,7 @@ class PartialEvaluator {
newProperties
);
}
return new Font(baseFontName, file, newProperties);
return new Font(baseFontName, file, newProperties, this.options);
}
}

Expand Down Expand Up @@ -4559,7 +4554,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) {
Expand Down Expand Up @@ -4607,11 +4602,10 @@ class PartialEvaluator {
}

class TranslatedFont {
constructor({ loadedName, font, dict, evaluatorOptions }) {
constructor({ loadedName, font, dict }) {
this.loadedName = loadedName;
this.font = font;
this.dict = dict;
this._evaluatorOptions = evaluatorOptions || DefaultPartialEvaluatorOptions;
this.type3Loaded = null;
this.type3Dependencies = font.isType3Font ? new Set() : null;
this.sent = false;
Expand All @@ -4626,11 +4620,11 @@ class TranslatedFont {
handler.send("commonobj", [
this.loadedName,
"Font",
this.font.exportData(this._evaluatorOptions.fontExtraProperties),
this.font.exportData(),
]);
}

fallback(handler) {
fallback(handler, evaluatorOptions) {
if (!this.font.data) {
return;
}
Expand All @@ -4646,7 +4640,7 @@ class TranslatedFont {
this.font,
/* glyphs = */ this.font.glyphCacheValues,
handler,
this._evaluatorOptions
evaluatorOptions
);
}

Expand Down
20 changes: 11 additions & 9 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ const EXPORT_DATA_PROPERTIES = [
"defaultVMetrics",
"defaultWidth",
"descent",
"disableFontFace",
"fallbackName",
"fontExtraProperties",
"fontMatrix",
"isInvalidPDFjsFont",
"isType3Font",
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -3602,7 +3604,7 @@ class ErrorFont {
return [chars];
}

exportData(extraProperties = false) {
exportData() {
return { error: this.error };
}
}
Expand Down
14 changes: 3 additions & 11 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,6 @@ function getDocument(src = {}) {
},
};
const transportParams = {
disableFontFace,
fontExtraProperties,
ownerDocument,
pdfBug,
styleElement,
Expand Down Expand Up @@ -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}`);
Expand All @@ -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.
Expand Down
15 changes: 9 additions & 6 deletions src/display/font_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
27 changes: 16 additions & 11 deletions test/font/font_fpgm_spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 31 additions & 21 deletions test/font/font_os2_spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading