Replace the BaseFontLoader classes with one FontLoader class

By having just *one* class, and using pre-processor blocks directly in the relevant methods, we reduce the size of this code in the *built* `pdf.js` file.
Originally, when the `BaseFontLoader` abstraction was added in PR 9982, the idea was probably that additional build-targets would get their own implementations. Given that this hasn't happened in the four years since that landed, it doesn't seem meaningful to keep it around.
This commit is contained in:
Jonas Jenwald 2022-09-25 11:54:43 +02:00
parent 9a03d36af4
commit ad83fbe2a1

View File

@ -19,22 +19,17 @@ import {
FeatureTest,
shadow,
string32,
unreachable,
UNSUPPORTED_FEATURES,
warn,
} from "../shared/util.js";
class BaseFontLoader {
class FontLoader {
constructor({
docId,
onUnsupportedFeature,
ownerDocument = globalThis.document,
// For testing only.
styleElement = null,
styleElement = null, // For testing only.
}) {
if (this.constructor === BaseFontLoader) {
unreachable("Cannot initialize BaseFontLoader.");
}
this.docId = docId;
this._onUnsupportedFeature = onUnsupportedFeature;
this._document = ownerDocument;
@ -44,6 +39,11 @@ class BaseFontLoader {
typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")
? styleElement
: null;
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
this.loadingRequests = [];
this.loadTestFontId = 0;
}
}
addNativeFontFace(nativeFontFace) {
@ -120,10 +120,6 @@ class BaseFontLoader {
}
}
_queueLoadingCallback(callback) {
unreachable("Abstract method `_queueLoadingCallback`.");
}
get isFontLoadingAPISupported() {
const hasFonts = !!this._document?.fonts;
if (
@ -139,39 +135,11 @@ class BaseFontLoader {
return shadow(this, "isFontLoadingAPISupported", hasFonts);
}
// eslint-disable-next-line getter-return
get isSyncFontLoadingSupported() {
unreachable("Abstract method `isSyncFontLoadingSupported`.");
}
// eslint-disable-next-line getter-return
get _loadTestFont() {
unreachable("Abstract method `_loadTestFont`.");
}
_prepareFontLoadEvent(rules, fontsToLoad, request) {
unreachable("Abstract method `_prepareFontLoadEvent`.");
}
}
let FontLoader;
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
FontLoader = class MozcentralFontLoader extends BaseFontLoader {
get isSyncFontLoadingSupported() {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
return shadow(this, "isSyncFontLoadingSupported", true);
}
};
} else {
// PDFJSDev.test('CHROME || GENERIC')
FontLoader = class GenericFontLoader extends BaseFontLoader {
constructor(params) {
super(params);
this.loadingRequests = [];
this.loadTestFontId = 0;
}
get isSyncFontLoadingSupported() {
let supported = false;
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("CHROME")) {
if (typeof navigator === "undefined") {
@ -180,9 +148,7 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
} else {
// User agent string sniffing is bad, but there is no reliable way to
// tell if the font is fully loaded and ready to be used with canvas.
const m = /Mozilla\/5.0.*?rv:(\d+).*? Gecko/.exec(
navigator.userAgent
);
const m = /Mozilla\/5.0.*?rv:(\d+).*? Gecko/.exec(navigator.userAgent);
if (m?.[1] >= 14) {
supported = true;
}
@ -193,6 +159,10 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
}
_queueLoadingCallback(callback) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: _queueLoadingCallback");
}
function completeRequest() {
assert(!request.done, "completeRequest() cannot be called twice.");
request.done = true;
@ -215,10 +185,13 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
}
get _loadTestFont() {
const getLoadTestFont = function () {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: _loadTestFont");
}
// This is a CFF font with 1 glyph for '.' that fills its entire width
// and height.
return atob(
const testFont = atob(
"T1RUTwALAIAAAwAwQ0ZGIDHtZg4AAAOYAAAAgUZGVE1lkzZwAAAEHAAAABxHREVGABQA" +
"FQAABDgAAAAeT1MvMlYNYwkAAAEgAAAAYGNtYXABDQLUAAACNAAAAUJoZWFk/xVFDQAA" +
"ALwAAAA2aGhlYQdkA+oAAAD0AAAAJGhtdHgD6AAAAAAEWAAAAAZtYXhwAAJQAAAAARgA" +
@ -242,11 +215,14 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
"Dov6fAH6fAT+fPp8+nwHDosMCvm1Cvm1DAz6fBQAAAAAAAABAAAAAMmJbzEAAAAAzgTj" +
"FQAAAADOBOQpAAEAAAAAAAAADAAUAAQAAAABAAAAAgABAAAAAAAAAAAD6AAAAAAAAA=="
);
};
return shadow(this, "_loadTestFont", getLoadTestFont());
return shadow(this, "_loadTestFont", testFont);
}
_prepareFontLoadEvent(rules, fonts, request) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: _prepareFontLoadEvent");
}
/** Hack begin */
// There's currently no event when a font has finished downloading so the
// following code is a dirty hack to 'guess' when a font is ready.
@ -316,8 +292,7 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
}
if (i < loadTestFontId.length) {
// align to 4 bytes boundary
checksum =
(checksum - XXXX_VALUE + int32(loadTestFontId + "XXX", i)) | 0;
checksum = (checksum - XXXX_VALUE + int32(loadTestFontId + "XXX", i)) | 0;
}
data = spliceString(data, CFF_CHECKSUM_OFFSET, 4, string32(checksum));
@ -351,8 +326,7 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
});
/** Hack end */
}
};
} // End of PDFJSDev.test('CHROME || GENERIC')
}
class FontFaceObject {
constructor(