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);
}
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,15 +1745,8 @@ class PDFDocument {
}
}
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;
}
}
fontFallback(id, handler) {
return this.catalog.fontFallback(id, handler);
}
async cleanup(manuallyTriggered = false) {

View File

@ -1065,6 +1065,7 @@ class PartialEvaluator {
loadedName: "g_font_error",
font: new ErrorFont(`Type3 font load error: ${reason}`),
dict: translated.font,
evaluatorOptions: this.options,
});
}
}
@ -1085,7 +1086,8 @@ class PartialEvaluator {
if (
isAddToPathSet ||
state.fillColorSpace.name === "Pattern" ||
font.disableFontFace
font.disableFontFace ||
this.options.disableFontFace
) {
PartialEvaluator.buildFontPaths(
font,
@ -1236,6 +1238,7 @@ class PartialEvaluator {
loadedName: "g_font_error",
font: new ErrorFont(`Font "${fontName}" is not available.`),
dict: font,
evaluatorOptions: this.options,
});
let fontRef;
@ -1362,6 +1365,7 @@ class PartialEvaluator {
loadedName: font.loadedName,
font: translatedFont,
dict: font,
evaluatorOptions: this.options,
})
);
})
@ -1376,6 +1380,7 @@ class PartialEvaluator {
reason instanceof Error ? reason.message : reason
),
dict: font,
evaluatorOptions: this.options,
})
);
});
@ -4362,7 +4367,7 @@ class PartialEvaluator {
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);
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) {
@ -4602,10 +4607,11 @@ class PartialEvaluator {
}
class TranslatedFont {
constructor({ loadedName, font, dict }) {
constructor({ loadedName, font, dict, evaluatorOptions }) {
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;
@ -4620,11 +4626,11 @@ class TranslatedFont {
handler.send("commonobj", [
this.loadedName,
"Font",
this.font.exportData(),
this.font.exportData(this._evaluatorOptions.fontExtraProperties),
]);
}
fallback(handler, evaluatorOptions) {
fallback(handler) {
if (!this.font.data) {
return;
}
@ -4640,7 +4646,7 @@ class TranslatedFont {
this.font,
/* glyphs = */ this.font.glyphCacheValues,
handler,
evaluatorOptions
this._evaluatorOptions
);
}

View File

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

View File

@ -422,6 +422,8 @@ function getDocument(src = {}) {
},
};
const transportParams = {
disableFontFace,
fontExtraProperties,
ownerDocument,
pdfBug,
styleElement,
@ -2785,6 +2787,8 @@ 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}`);
@ -2793,16 +2797,20 @@ class WorkerTransport {
}
const inspectFont =
this._params.pdfBug && globalThis.FontInspector?.enabled
pdfBug && globalThis.FontInspector?.enabled
? (font, url) => globalThis.FontInspector.fontAdded(font, url)
: null;
const font = new FontFaceObject(exportedData, inspectFont);
const font = new FontFaceObject(exportedData, {
disableFontFace,
fontExtraProperties,
inspectFont,
});
this.fontLoader
.bind(font)
.catch(() => messageHandler.sendWithPromise("FontFallback", { id }))
.finally(() => {
if (!font.fontExtraProperties && font.data) {
if (!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,20 +355,17 @@ class FontLoader {
}
class FontFaceObject {
constructor(translatedData, inspectFont = null) {
constructor(
translatedData,
{ disableFontFace = false, fontExtraProperties = false, inspectFont = null }
) {
this.compiledGlyphs = Object.create(null);
// importing translated data
for (const i in translatedData) {
this[i] = translatedData[i];
}
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.disableFontFace = disableFontFace === true;
this.fontExtraProperties = fontExtraProperties === true;
this._inspectFont = inspectFont;
}

View File

@ -16,22 +16,17 @@ 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);

View File

@ -17,21 +17,16 @@ 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);
@ -44,22 +39,17 @@ 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);

View File

@ -24,22 +24,17 @@ 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);
@ -47,21 +42,16 @@ 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);
@ -69,21 +59,16 @@ 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);