[api-minor] Limit the PDFDocumentLoadingTask.onUnsupportedFeature functionality to GENERIC builds (PR 15758 follow-up)

This was deprecated in PR 15758 but it's unfortunately quite difficult to tell if third-party users are depending on this, e.g. to implement custom error reporting, and if so to what extent.
However, thanks to the pre-processor we can limit *most* of this code to GENERIC builds which still seem like a worthwhile change.

These changes reduce the bundle size of the Firefox PDF Viewer by 3.8 kB in total.
This commit is contained in:
Jonas Jenwald 2023-01-01 16:57:57 +01:00
parent 0c1fb4e740
commit 1a69d537c1
6 changed files with 159 additions and 105 deletions

View File

@ -222,11 +222,13 @@ class Page {
*/ */
_onSubStreamError(handler, reason, objId) { _onSubStreamError(handler, reason, objId) {
if (this.evaluatorOptions.ignoreErrors) { if (this.evaluatorOptions.ignoreErrors) {
// Error(s) when reading one of the /Contents sub-streams -- sending if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// unsupported feature notification and allow parsing to continue. // Error(s) when reading one of the /Contents sub-streams -- sending
handler.send("UnsupportedFeature", { // unsupported feature notification and allow parsing to continue.
featureId: UNSUPPORTED_FEATURES.errorContentSubStream, handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.errorContentSubStream,
});
}
warn(`getContentStream - ignoring sub-stream (${objId}): "${reason}".`); warn(`getContentStream - ignoring sub-stream (${objId}): "${reason}".`);
return; return;
} }

View File

@ -929,11 +929,13 @@ class PartialEvaluator {
return; return;
} }
if (this.options.ignoreErrors) { if (this.options.ignoreErrors) {
// Error(s) in the TilingPattern -- sending unsupported feature if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// notification and allow rendering to continue. // Error(s) in the TilingPattern -- sending unsupported feature
this.handler.send("UnsupportedFeature", { // notification and allow rendering to continue.
featureId: UNSUPPORTED_FEATURES.errorTilingPattern, this.handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.errorTilingPattern,
});
}
warn(`handleTilingType - ignoring pattern: "${reason}".`); warn(`handleTilingType - ignoring pattern: "${reason}".`);
return; return;
} }
@ -975,11 +977,13 @@ class PartialEvaluator {
return translated; return translated;
}) })
.catch(reason => { .catch(reason => {
// Error in the font data -- sending unsupported feature if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// notification. // Error in the font data -- sending unsupported feature
this.handler.send("UnsupportedFeature", { // notification.
featureId: UNSUPPORTED_FEATURES.errorFontLoadType3, this.handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.errorFontLoadType3,
});
}
return new TranslatedFont({ return new TranslatedFont({
loadedName: "g_font_error", loadedName: "g_font_error",
font: new ErrorFont(`Type3 font load error: ${reason}`), font: new ErrorFont(`Type3 font load error: ${reason}`),
@ -1029,11 +1033,13 @@ class PartialEvaluator {
); );
if (this.options.ignoreErrors) { if (this.options.ignoreErrors) {
// Missing setFont operator before text rendering operator -- sending if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// unsupported feature notification and allow rendering to continue. // Missing setFont operator before text rendering operator -- sending
this.handler.send("UnsupportedFeature", { // unsupported feature notification and allow rendering to continue.
featureId: UNSUPPORTED_FEATURES.errorFontState, this.handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.errorFontState,
});
}
warn(`ensureStateFont: "${reason}".`); warn(`ensureStateFont: "${reason}".`);
return; return;
} }
@ -1191,10 +1197,12 @@ class PartialEvaluator {
warn(`${partialMsg}.`); warn(`${partialMsg}.`);
return errorFont(); return errorFont();
} }
// Font not found -- sending unsupported feature notification. if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
this.handler.send("UnsupportedFeature", { // Font not found -- sending unsupported feature notification.
featureId: UNSUPPORTED_FEATURES.errorFontMissing, this.handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.errorFontMissing,
});
}
warn(`${partialMsg} -- attempting to fallback to a default font.`); warn(`${partialMsg} -- attempting to fallback to a default font.`);
// Falling back to a default font to avoid completely broken rendering, // Falling back to a default font to avoid completely broken rendering,
@ -1313,10 +1321,12 @@ class PartialEvaluator {
}) })
.catch(reason => { .catch(reason => {
// TODO fontCapability.reject? // TODO fontCapability.reject?
// Error in the font data -- sending unsupported feature notification. if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
this.handler.send("UnsupportedFeature", { // Error in the font data -- sending unsupported feature notification.
featureId: UNSUPPORTED_FEATURES.errorFontTranslate, this.handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.errorFontTranslate,
});
}
warn(`loadFont - translateFont failed: "${reason}".`); warn(`loadFont - translateFont failed: "${reason}".`);
fontCapability.resolve( fontCapability.resolve(
@ -1422,11 +1432,13 @@ class PartialEvaluator {
return null; return null;
} }
if (this.options.ignoreErrors) { if (this.options.ignoreErrors) {
// Error(s) in the ColorSpace -- sending unsupported feature if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// notification and allow rendering to continue. // Error(s) in the ColorSpace -- sending unsupported feature
this.handler.send("UnsupportedFeature", { // notification and allow rendering to continue.
featureId: UNSUPPORTED_FEATURES.errorColorSpace, this.handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.errorColorSpace,
});
}
warn(`parseColorSpace - ignoring ColorSpace: "${reason}".`); warn(`parseColorSpace - ignoring ColorSpace: "${reason}".`);
return null; return null;
} }
@ -1811,11 +1823,16 @@ class PartialEvaluator {
return; return;
} }
if (self.options.ignoreErrors) { if (self.options.ignoreErrors) {
// Error(s) in the XObject -- sending unsupported feature if (
// notification and allow rendering to continue. typeof PDFJSDev === "undefined" ||
self.handler.send("UnsupportedFeature", { PDFJSDev.test("GENERIC")
featureId: UNSUPPORTED_FEATURES.errorXObject, ) {
}); // Error(s) in the XObject -- sending unsupported feature
// notification and allow rendering to continue.
self.handler.send("UnsupportedFeature", {
featureId: UNSUPPORTED_FEATURES.errorXObject,
});
}
warn(`getOperatorList - ignoring XObject: "${reason}".`); warn(`getOperatorList - ignoring XObject: "${reason}".`);
return; return;
} }
@ -2130,11 +2147,16 @@ class PartialEvaluator {
return; return;
} }
if (self.options.ignoreErrors) { if (self.options.ignoreErrors) {
// Error(s) in the ExtGState -- sending unsupported feature if (
// notification and allow parsing/rendering to continue. typeof PDFJSDev === "undefined" ||
self.handler.send("UnsupportedFeature", { PDFJSDev.test("GENERIC")
featureId: UNSUPPORTED_FEATURES.errorExtGState, ) {
}); // Error(s) in the ExtGState -- sending unsupported feature
// notification and allow parsing/rendering to continue.
self.handler.send("UnsupportedFeature", {
featureId: UNSUPPORTED_FEATURES.errorExtGState,
});
}
warn(`getOperatorList - ignoring ExtGState: "${reason}".`); warn(`getOperatorList - ignoring ExtGState: "${reason}".`);
return; return;
} }
@ -2182,9 +2204,14 @@ class PartialEvaluator {
return; return;
} }
if (self.options.ignoreErrors) { if (self.options.ignoreErrors) {
self.handler.send("UnsupportedFeature", { if (
featureId: UNSUPPORTED_FEATURES.errorMarkedContent, typeof PDFJSDev === "undefined" ||
}); PDFJSDev.test("GENERIC")
) {
self.handler.send("UnsupportedFeature", {
featureId: UNSUPPORTED_FEATURES.errorMarkedContent,
});
}
warn( warn(
`getOperatorList - ignoring beginMarkedContentProps: "${reason}".` `getOperatorList - ignoring beginMarkedContentProps: "${reason}".`
); );
@ -2235,11 +2262,13 @@ class PartialEvaluator {
return; return;
} }
if (this.options.ignoreErrors) { if (this.options.ignoreErrors) {
// Error(s) in the OperatorList -- sending unsupported feature if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// notification and allow rendering to continue. // Error(s) in the OperatorList -- sending unsupported feature
this.handler.send("UnsupportedFeature", { // notification and allow rendering to continue.
featureId: UNSUPPORTED_FEATURES.errorOperatorList, this.handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.errorOperatorList,
});
}
warn( warn(
`getOperatorList - ignoring errors during "${task.name}" ` + `getOperatorList - ignoring errors during "${task.name}" ` +
`task: "${reason}".` `task: "${reason}".`
@ -3734,11 +3763,13 @@ class PartialEvaluator {
return null; return null;
} }
if (this.options.ignoreErrors) { if (this.options.ignoreErrors) {
// Error in the ToUnicode data -- sending unsupported feature if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// notification and allow font parsing to continue. // Error in the ToUnicode data -- sending unsupported feature
this.handler.send("UnsupportedFeature", { // notification and allow font parsing to continue.
featureId: UNSUPPORTED_FEATURES.errorFontToUnicode, this.handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.errorFontToUnicode,
});
}
warn(`readToUnicode - ignoring ToUnicode data: "${reason}".`); warn(`readToUnicode - ignoring ToUnicode data: "${reason}".`);
return null; return null;
} }
@ -4312,11 +4343,13 @@ class PartialEvaluator {
]); ]);
} catch (reason) { } catch (reason) {
if (evaluatorOptions.ignoreErrors) { if (evaluatorOptions.ignoreErrors) {
// Error in the font data -- sending unsupported feature notification if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// and allow glyph path building to continue. // Error in the font data -- sending unsupported feature
handler.send("UnsupportedFeature", { // notification and allow glyph path building to continue.
featureId: UNSUPPORTED_FEATURES.errorFontBuildPath, handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.errorFontBuildPath,
});
}
warn(`buildFontPaths - ignoring ${glyphName} glyph: "${reason}".`); warn(`buildFontPaths - ignoring ${glyphName} glyph: "${reason}".`);
return; return;
} }

View File

@ -82,9 +82,11 @@ class Pattern {
if (ex instanceof MissingDataException) { if (ex instanceof MissingDataException) {
throw ex; throw ex;
} }
handler.send("UnsupportedFeature", { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
featureId: UNSUPPORTED_FEATURES.shadingPattern, handler.send("UnsupportedFeature", {
}); featureId: UNSUPPORTED_FEATURES.shadingPattern,
});
}
warn(ex); warn(ex);
return new DummyShading(); return new DummyShading();
} }

View File

@ -609,10 +609,12 @@ class PDFDocumentLoadingTask {
* @type {function} * @type {function}
*/ */
set onUnsupportedFeature(callback) { set onUnsupportedFeature(callback) {
deprecated( if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
"The PDFDocumentLoadingTask onUnsupportedFeature property will be removed in the future." deprecated(
); "The PDFDocumentLoadingTask onUnsupportedFeature property will be removed in the future."
this.#onUnsupportedFeature = callback; );
this.#onUnsupportedFeature = callback;
}
} }
/** /**
@ -2748,10 +2750,12 @@ class WorkerTransport {
}); });
}); });
messageHandler.on( if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
"UnsupportedFeature", messageHandler.on(
this._onUnsupportedFeature.bind(this) "UnsupportedFeature",
); this._onUnsupportedFeature.bind(this)
);
}
messageHandler.on("FetchBuiltInCMap", data => { messageHandler.on("FetchBuiltInCMap", data => {
if (this.destroyed) { if (this.destroyed) {
@ -2783,10 +2787,12 @@ class WorkerTransport {
} }
_onUnsupportedFeature({ featureId }) { _onUnsupportedFeature({ featureId }) {
if (this.destroyed) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
return; // Ignore any pending requests if the worker was terminated. if (this.destroyed) {
return; // Ignore any pending requests if the worker was terminated.
}
this.loadingTask.onUnsupportedFeature?.(featureId);
} }
this.loadingTask.onUnsupportedFeature?.(featureId);
} }
getData() { getData() {

View File

@ -30,7 +30,9 @@ class FontLoader {
ownerDocument = globalThis.document, ownerDocument = globalThis.document,
styleElement = null, // For testing only. styleElement = null, // For testing only.
}) { }) {
this._onUnsupportedFeature = onUnsupportedFeature; if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
this._onUnsupportedFeature = onUnsupportedFeature;
}
this._document = ownerDocument; this._document = ownerDocument;
this.nativeFontFaces = []; this.nativeFontFaces = [];
@ -88,9 +90,11 @@ class FontLoader {
try { try {
await nativeFontFace.loaded; await nativeFontFace.loaded;
} catch (ex) { } catch (ex) {
this._onUnsupportedFeature({ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
featureId: UNSUPPORTED_FEATURES.errorFontLoadNative, this._onUnsupportedFeature({
}); featureId: UNSUPPORTED_FEATURES.errorFontLoadNative,
});
}
warn(`Failed to load font '${nativeFontFace.family}': '${ex}'.`); warn(`Failed to load font '${nativeFontFace.family}': '${ex}'.`);
// When font loading failed, fall back to the built-in font renderer. // When font loading failed, fall back to the built-in font renderer.
@ -340,7 +344,9 @@ class FontFaceObject {
this.isEvalSupported = isEvalSupported !== false; this.isEvalSupported = isEvalSupported !== false;
this.disableFontFace = disableFontFace === true; this.disableFontFace = disableFontFace === true;
this.ignoreErrors = ignoreErrors === true; this.ignoreErrors = ignoreErrors === true;
this._onUnsupportedFeature = onUnsupportedFeature; if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
this._onUnsupportedFeature = onUnsupportedFeature;
}
this.fontRegistry = fontRegistry; this.fontRegistry = fontRegistry;
} }
@ -403,9 +409,11 @@ class FontFaceObject {
if (!this.ignoreErrors) { if (!this.ignoreErrors) {
throw ex; throw ex;
} }
this._onUnsupportedFeature({ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
featureId: UNSUPPORTED_FEATURES.errorFontGetPath, this._onUnsupportedFeature({
}); featureId: UNSUPPORTED_FEATURES.errorFontGetPath,
});
}
warn(`getPathGenerator - ignoring character: "${ex}".`); warn(`getPathGenerator - ignoring character: "${ex}".`);
return (this.compiledGlyphs[character] = function (c, size) { return (this.compiledGlyphs[character] = function (c, size) {

View File

@ -346,28 +346,31 @@ const OPS = {
constructPath: 91, constructPath: 91,
}; };
const UNSUPPORTED_FEATURES = { const UNSUPPORTED_FEATURES =
forms: "forms", typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")
javaScript: "javaScript", ? {
signatures: "signatures", forms: "forms",
smask: "smask", javaScript: "javaScript",
shadingPattern: "shadingPattern", signatures: "signatures",
errorTilingPattern: "errorTilingPattern", smask: "smask",
errorExtGState: "errorExtGState", shadingPattern: "shadingPattern",
errorXObject: "errorXObject", errorTilingPattern: "errorTilingPattern",
errorFontLoadType3: "errorFontLoadType3", errorExtGState: "errorExtGState",
errorFontState: "errorFontState", errorXObject: "errorXObject",
errorFontMissing: "errorFontMissing", errorFontLoadType3: "errorFontLoadType3",
errorFontTranslate: "errorFontTranslate", errorFontState: "errorFontState",
errorColorSpace: "errorColorSpace", errorFontMissing: "errorFontMissing",
errorOperatorList: "errorOperatorList", errorFontTranslate: "errorFontTranslate",
errorFontToUnicode: "errorFontToUnicode", errorColorSpace: "errorColorSpace",
errorFontLoadNative: "errorFontLoadNative", errorOperatorList: "errorOperatorList",
errorFontBuildPath: "errorFontBuildPath", errorFontToUnicode: "errorFontToUnicode",
errorFontGetPath: "errorFontGetPath", errorFontLoadNative: "errorFontLoadNative",
errorMarkedContent: "errorMarkedContent", errorFontBuildPath: "errorFontBuildPath",
errorContentSubStream: "errorContentSubStream", errorFontGetPath: "errorFontGetPath",
}; errorMarkedContent: "errorMarkedContent",
errorContentSubStream: "errorContentSubStream",
}
: null;
const PasswordResponses = { const PasswordResponses = {
NEED_PASSWORD: 1, NEED_PASSWORD: 1,