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

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

These options are needed in the `FontFaceObject` class, and indirectly in `FontLoader` as well, which means that we currently need to pass them around manually in the API.
Given that the options are (obviously) available on the worker-thread, it's very easy to just provide them when creating `Font`-instances and then send them as part of the exported font-data. This way we're able to simplify the code (primarily on the main-thread), and note that `Font`-instances even had a `disableFontFace`-field already (but it wasn't properly initialized).
This commit is contained in:
Jonas Jenwald 2025-02-23 17:32:20 +01:00
parent ed64faa88c
commit 839e23f5c2
7 changed files with 120 additions and 94 deletions

View file

@ -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(),
]);
}

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

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