1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-23 08:38:06 +02:00

Merge pull request #19548 from Snuffleupagus/Font-exportData-disableFontFace-fontExtraProperties

Send `disableFontFace` and `fontExtraProperties` as part of the exported font-data
This commit is contained in:
Jonas Jenwald 2025-02-26 11:25:29 +01:00 committed by GitHub
commit da17c7b82f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 132 additions and 115 deletions

View file

@ -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);

View file

@ -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) {

View file

@ -1065,7 +1065,6 @@ class PartialEvaluator {
loadedName: "g_font_error",
font: new ErrorFont(`Type3 font load error: ${reason}`),
dict: translated.font,
evaluatorOptions: this.options,
});
}
}
@ -1086,8 +1085,7 @@ class PartialEvaluator {
if (
isAddToPathSet ||
state.fillColorSpace.name === "Pattern" ||
font.disableFontFace ||
this.options.disableFontFace
font.disableFontFace
) {
PartialEvaluator.buildFontPaths(
font,
@ -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;
@ -1365,7 +1362,6 @@ class PartialEvaluator {
loadedName: font.loadedName,
font: translatedFont,
dict: font,
evaluatorOptions: this.options,
})
);
})
@ -1380,7 +1376,6 @@ class PartialEvaluator {
reason instanceof Error ? reason.message : reason
),
dict: font,
evaluatorOptions: this.options,
})
);
});
@ -4367,7 +4362,7 @@ class PartialEvaluator {
newProperties
);
}
return new Font(baseFontName, file, newProperties);
return new Font(baseFontName, file, newProperties, this.options);
}
}
@ -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) {
@ -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;
@ -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;
}
@ -4646,7 +4640,7 @@ class TranslatedFont {
this.font,
/* glyphs = */ this.font.glyphCacheValues,
handler,
this._evaluatorOptions
evaluatorOptions
);
}

View file

@ -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 };
}
}

View file

@ -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.

View file

@ -355,17 +355,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;
}