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