Compare commits

..

No commits in common. "da17c7b82f5146cdcceb231e8f3e8b81e04085b7" and "0e0872288ee449333da75361951e9f0daca56cda" have entirely different histories.

9 changed files with 115 additions and 132 deletions

View File

@ -1169,6 +1169,17 @@ class Catalog {
return shadow(this, "jsActions", actions); 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) { async cleanup(manuallyTriggered = false) {
clearGlobalCaches(); clearGlobalCaches();
this.globalImageCache.clear(/* onlyData = */ manuallyTriggered); this.globalImageCache.clear(/* onlyData = */ manuallyTriggered);

View File

@ -1745,15 +1745,8 @@ class PDFDocument {
} }
} }
async fontFallback(id, handler) { fontFallback(id, handler) {
const { catalog, pdfManager } = this; return this.catalog.fontFallback(id, handler);
for (const translatedFont of await Promise.all(catalog.fontCache)) {
if (translatedFont.loadedName === id) {
translatedFont.fallback(handler, pdfManager.evaluatorOptions);
return;
}
}
} }
async cleanup(manuallyTriggered = false) { async cleanup(manuallyTriggered = false) {

View File

@ -1065,6 +1065,7 @@ class PartialEvaluator {
loadedName: "g_font_error", loadedName: "g_font_error",
font: new ErrorFont(`Type3 font load error: ${reason}`), font: new ErrorFont(`Type3 font load error: ${reason}`),
dict: translated.font, dict: translated.font,
evaluatorOptions: this.options,
}); });
} }
} }
@ -1085,7 +1086,8 @@ class PartialEvaluator {
if ( if (
isAddToPathSet || isAddToPathSet ||
state.fillColorSpace.name === "Pattern" || state.fillColorSpace.name === "Pattern" ||
font.disableFontFace font.disableFontFace ||
this.options.disableFontFace
) { ) {
PartialEvaluator.buildFontPaths( PartialEvaluator.buildFontPaths(
font, font,
@ -1236,6 +1238,7 @@ class PartialEvaluator {
loadedName: "g_font_error", loadedName: "g_font_error",
font: new ErrorFont(`Font "${fontName}" is not available.`), font: new ErrorFont(`Font "${fontName}" is not available.`),
dict: font, dict: font,
evaluatorOptions: this.options,
}); });
let fontRef; let fontRef;
@ -1362,6 +1365,7 @@ class PartialEvaluator {
loadedName: font.loadedName, loadedName: font.loadedName,
font: translatedFont, font: translatedFont,
dict: font, dict: font,
evaluatorOptions: this.options,
}) })
); );
}) })
@ -1376,6 +1380,7 @@ class PartialEvaluator {
reason instanceof Error ? reason.message : reason reason instanceof Error ? reason.message : reason
), ),
dict: font, dict: font,
evaluatorOptions: this.options,
}) })
); );
}); });
@ -4362,7 +4367,7 @@ class PartialEvaluator {
newProperties newProperties
); );
} }
return new Font(baseFontName, file, newProperties, this.options); return new Font(baseFontName, file, newProperties);
} }
} }
@ -4554,7 +4559,7 @@ class PartialEvaluator {
const newProperties = await this.extractDataStructures(dict, properties); const newProperties = await this.extractDataStructures(dict, properties);
this.extractWidths(dict, descriptor, newProperties); this.extractWidths(dict, descriptor, newProperties);
return new Font(fontName.name, fontFile, newProperties, this.options); return new Font(fontName.name, fontFile, newProperties);
} }
static buildFontPaths(font, glyphs, handler, evaluatorOptions) { static buildFontPaths(font, glyphs, handler, evaluatorOptions) {
@ -4602,10 +4607,11 @@ class PartialEvaluator {
} }
class TranslatedFont { class TranslatedFont {
constructor({ loadedName, font, dict }) { constructor({ loadedName, font, dict, evaluatorOptions }) {
this.loadedName = loadedName; this.loadedName = loadedName;
this.font = font; this.font = font;
this.dict = dict; this.dict = dict;
this._evaluatorOptions = evaluatorOptions || DefaultPartialEvaluatorOptions;
this.type3Loaded = null; this.type3Loaded = null;
this.type3Dependencies = font.isType3Font ? new Set() : null; this.type3Dependencies = font.isType3Font ? new Set() : null;
this.sent = false; this.sent = false;
@ -4620,11 +4626,11 @@ class TranslatedFont {
handler.send("commonobj", [ handler.send("commonobj", [
this.loadedName, this.loadedName,
"Font", "Font",
this.font.exportData(), this.font.exportData(this._evaluatorOptions.fontExtraProperties),
]); ]);
} }
fallback(handler, evaluatorOptions) { fallback(handler) {
if (!this.font.data) { if (!this.font.data) {
return; return;
} }
@ -4640,7 +4646,7 @@ class TranslatedFont {
this.font, this.font,
/* glyphs = */ this.font.glyphCacheValues, /* glyphs = */ this.font.glyphCacheValues,
handler, handler,
evaluatorOptions this._evaluatorOptions
); );
} }

View File

@ -88,9 +88,7 @@ const EXPORT_DATA_PROPERTIES = [
"defaultVMetrics", "defaultVMetrics",
"defaultWidth", "defaultWidth",
"descent", "descent",
"disableFontFace",
"fallbackName", "fallbackName",
"fontExtraProperties",
"fontMatrix", "fontMatrix",
"isInvalidPDFjsFont", "isInvalidPDFjsFont",
"isType3Font", "isType3Font",
@ -972,12 +970,11 @@ function createNameTable(name, proto) {
* decoding logics whatever type it is (assuming the font type is supported). * decoding logics whatever type it is (assuming the font type is supported).
*/ */
class Font { class Font {
constructor(name, file, properties, evaluatorOptions) { constructor(name, file, properties) {
this.name = name; this.name = name;
this.psName = null; this.psName = null;
this.mimetype = null; this.mimetype = null;
this.disableFontFace = evaluatorOptions.disableFontFace; this.disableFontFace = false;
this.fontExtraProperties = evaluatorOptions.fontExtraProperties;
this.loadedName = properties.loadedName; this.loadedName = properties.loadedName;
this.isType3Font = properties.isType3Font; this.isType3Font = properties.isType3Font;
@ -1144,17 +1141,18 @@ class Font {
return shadow(this, "renderer", renderer); return shadow(this, "renderer", renderer);
} }
exportData() { exportData(extraProperties = false) {
const exportDataProps = this.fontExtraProperties const exportDataProperties = extraProperties
? [...EXPORT_DATA_PROPERTIES, ...EXPORT_DATA_EXTRA_PROPERTIES] ? [...EXPORT_DATA_PROPERTIES, ...EXPORT_DATA_EXTRA_PROPERTIES]
: EXPORT_DATA_PROPERTIES; : EXPORT_DATA_PROPERTIES;
const data = Object.create(null); const data = Object.create(null);
for (const prop of exportDataProps) { let property, value;
const value = this[prop]; for (property of exportDataProperties) {
value = this[property];
// Ignore properties that haven't been explicitly set. // Ignore properties that haven't been explicitly set.
if (value !== undefined) { if (value !== undefined) {
data[prop] = value; data[property] = value;
} }
} }
return data; return data;
@ -3604,7 +3602,7 @@ class ErrorFont {
return [chars]; return [chars];
} }
exportData() { exportData(extraProperties = false) {
return { error: this.error }; return { error: this.error };
} }
} }

View File

@ -422,6 +422,8 @@ function getDocument(src = {}) {
}, },
}; };
const transportParams = { const transportParams = {
disableFontFace,
fontExtraProperties,
ownerDocument, ownerDocument,
pdfBug, pdfBug,
styleElement, styleElement,
@ -2785,6 +2787,8 @@ class WorkerTransport {
switch (type) { switch (type) {
case "Font": case "Font":
const { disableFontFace, fontExtraProperties, pdfBug } = this._params;
if ("error" in exportedData) { if ("error" in exportedData) {
const exportedError = exportedData.error; const exportedError = exportedData.error;
warn(`Error during font loading: ${exportedError}`); warn(`Error during font loading: ${exportedError}`);
@ -2793,16 +2797,20 @@ class WorkerTransport {
} }
const inspectFont = const inspectFont =
this._params.pdfBug && globalThis.FontInspector?.enabled pdfBug && globalThis.FontInspector?.enabled
? (font, url) => globalThis.FontInspector.fontAdded(font, url) ? (font, url) => globalThis.FontInspector.fontAdded(font, url)
: null; : null;
const font = new FontFaceObject(exportedData, inspectFont); const font = new FontFaceObject(exportedData, {
disableFontFace,
fontExtraProperties,
inspectFont,
});
this.fontLoader this.fontLoader
.bind(font) .bind(font)
.catch(() => messageHandler.sendWithPromise("FontFallback", { id })) .catch(() => messageHandler.sendWithPromise("FontFallback", { id }))
.finally(() => { .finally(() => {
if (!font.fontExtraProperties && font.data) { if (!fontExtraProperties && font.data) {
// Immediately release the `font.data` property once the font // Immediately release the `font.data` property once the font
// has been attached to the DOM, since it's no longer needed, // has been attached to the DOM, since it's no longer needed,
// rather than waiting for a `PDFDocumentProxy.cleanup` call. // rather than waiting for a `PDFDocumentProxy.cleanup` call.

View File

@ -355,20 +355,17 @@ class FontLoader {
} }
class FontFaceObject { class FontFaceObject {
constructor(translatedData, inspectFont = null) { constructor(
translatedData,
{ disableFontFace = false, fontExtraProperties = false, inspectFont = null }
) {
this.compiledGlyphs = Object.create(null); this.compiledGlyphs = Object.create(null);
// importing translated data // importing translated data
for (const i in translatedData) { for (const i in translatedData) {
this[i] = translatedData[i]; this[i] = translatedData[i];
} }
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { this.disableFontFace = disableFontFace === true;
if (typeof this.disableFontFace !== "boolean") { this.fontExtraProperties = fontExtraProperties === true;
unreachable("disableFontFace must be available.");
}
if (typeof this.fontExtraProperties !== "boolean") {
unreachable("fontExtraProperties must be available.");
}
}
this._inspectFont = inspectFont; this._inspectFont = inspectFont;
} }

View File

@ -16,22 +16,17 @@ describe("font_fpgm", function () {
const cMap = await CMapFactory.create({ const cMap = await CMapFactory.create({
encoding: Name.get("Identity-H"), encoding: Name.get("Identity-H"),
}); });
const font = new Font( const font = new Font("font", new Stream(font2324), {
"font", loadedName: "font",
new Stream(font2324), type: "CIDFontType2",
{ differences: [],
loadedName: "font", defaultEncoding: [],
type: "CIDFontType2", cMap,
differences: [], toUnicode: new ToUnicodeMap([]),
defaultEncoding: [], xHeight: 0,
cMap, capHeight: 0,
toUnicode: new ToUnicodeMap([]), italicAngle: 0,
xHeight: 0, });
capHeight: 0,
italicAngle: 0,
},
{}
);
const output = await ttx(font.data); const output = await ttx(font.data);
verifyTtxOutput(output); verifyTtxOutput(output);

View File

@ -17,21 +17,16 @@ describe("font_post", function () {
describe("OS/2 table removal on bad post table values", function () { describe("OS/2 table removal on bad post table values", function () {
it("has invalid version number", async function () { it("has invalid version number", async function () {
const font = new Font( const font = new Font("font", new Stream(font2154), {
"font", loadedName: "font",
new Stream(font2154), type: "TrueType",
{ differences: [],
loadedName: "font", defaultEncoding: [],
type: "TrueType", toUnicode: new ToUnicodeMap([]),
differences: [], xHeight: 0,
defaultEncoding: [], capHeight: 0,
toUnicode: new ToUnicodeMap([]), italicAngle: 0,
xHeight: 0, });
capHeight: 0,
italicAngle: 0,
},
{}
);
const output = await ttx(font.data); const output = await ttx(font.data);
verifyTtxOutput(output); verifyTtxOutput(output);
@ -44,22 +39,17 @@ describe("font_post", function () {
const cMap = await CMapFactory.create({ const cMap = await CMapFactory.create({
encoding: Name.get("Identity-H"), encoding: Name.get("Identity-H"),
}); });
const font = new Font( const font = new Font("font", new Stream(font1282), {
"font", loadedName: "font",
new Stream(font1282), type: "CIDFontType2",
{ differences: [],
loadedName: "font", defaultEncoding: [],
type: "CIDFontType2", cMap,
differences: [], toUnicode: new ToUnicodeMap([]),
defaultEncoding: [], xHeight: 0,
cMap, capHeight: 0,
toUnicode: new ToUnicodeMap([]), italicAngle: 0,
xHeight: 0, });
capHeight: 0,
italicAngle: 0,
},
{}
);
const output = await ttx(font.data); const output = await ttx(font.data);
verifyTtxOutput(output); verifyTtxOutput(output);

View File

@ -24,22 +24,17 @@ describe("font_post", function () {
const cMap = await CMapFactory.create({ const cMap = await CMapFactory.create({
encoding: Name.get("Identity-H"), encoding: Name.get("Identity-H"),
}); });
const font = new Font( const font = new Font("font", new Stream(font2109), {
"font", loadedName: "font",
new Stream(font2109), type: "CIDFontType2",
{ differences: [],
loadedName: "font", defaultEncoding: [],
type: "CIDFontType2", cMap,
differences: [], toUnicode: new ToUnicodeMap([]),
defaultEncoding: [], xHeight: 0,
cMap, capHeight: 0,
toUnicode: new ToUnicodeMap([]), italicAngle: 0,
xHeight: 0, });
capHeight: 0,
italicAngle: 0,
},
{}
);
const output = await ttx(font.data); const output = await ttx(font.data);
verifyTtxOutput(output); verifyTtxOutput(output);
@ -47,21 +42,16 @@ describe("font_post", function () {
}); });
it("has invalid glyph name indexes", async function () { it("has invalid glyph name indexes", async function () {
const font = new Font( const font = new Font("font", new Stream(font2189), {
"font", loadedName: "font",
new Stream(font2189), type: "TrueType",
{ differences: [],
loadedName: "font", defaultEncoding: [],
type: "TrueType", toUnicode: new ToUnicodeMap([]),
differences: [], xHeight: 0,
defaultEncoding: [], capHeight: 0,
toUnicode: new ToUnicodeMap([]), italicAngle: 0,
xHeight: 0, });
capHeight: 0,
italicAngle: 0,
},
{}
);
const output = await ttx(font.data); const output = await ttx(font.data);
verifyTtxOutput(output); verifyTtxOutput(output);
@ -69,21 +59,16 @@ describe("font_post", function () {
}); });
it("has right amount of glyphs specified", async function () { it("has right amount of glyphs specified", async function () {
const font = new Font( const font = new Font("font", new Stream(font2374), {
"font", loadedName: "font",
new Stream(font2374), type: "TrueType",
{ differences: [],
loadedName: "font", defaultEncoding: [],
type: "TrueType", toUnicode: new ToUnicodeMap([]),
differences: [], xHeight: 0,
defaultEncoding: [], capHeight: 0,
toUnicode: new ToUnicodeMap([]), italicAngle: 0,
xHeight: 0, });
capHeight: 0,
italicAngle: 0,
},
{}
);
const output = await ttx(font.data); const output = await ttx(font.data);
verifyTtxOutput(output); verifyTtxOutput(output);