Merge pull request #15507 from Snuffleupagus/FontLoader-cleanup
Improve the `FontLoader` code
This commit is contained in:
commit
dab81f5981
@ -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,42 +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() {
|
||||
return shadow(this, "isSyncFontLoadingSupported", true);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
// PDFJSDev.test('CHROME || GENERIC')
|
||||
|
||||
FontLoader = class GenericFontLoader extends BaseFontLoader {
|
||||
constructor(params) {
|
||||
super(params);
|
||||
this.loadingContext = {
|
||||
requests: [],
|
||||
nextRequestId: 0,
|
||||
};
|
||||
this.loadTestFontId = 0;
|
||||
}
|
||||
|
||||
get isSyncFontLoadingSupported() {
|
||||
let supported = false;
|
||||
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("CHROME")) {
|
||||
if (typeof navigator === "undefined") {
|
||||
@ -183,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;
|
||||
}
|
||||
@ -196,33 +159,39 @@ 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;
|
||||
|
||||
// Sending all completed requests in order of how they were queued.
|
||||
while (context.requests.length > 0 && context.requests[0].done) {
|
||||
const otherRequest = context.requests.shift();
|
||||
while (loadingRequests.length > 0 && loadingRequests[0].done) {
|
||||
const otherRequest = loadingRequests.shift();
|
||||
setTimeout(otherRequest.callback, 0);
|
||||
}
|
||||
}
|
||||
|
||||
const context = this.loadingContext;
|
||||
const { loadingRequests } = this;
|
||||
const request = {
|
||||
id: `pdfjs-font-loading-${context.nextRequestId++}`,
|
||||
done: false,
|
||||
complete: completeRequest,
|
||||
callback,
|
||||
};
|
||||
context.requests.push(request);
|
||||
loadingRequests.push(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
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" +
|
||||
@ -246,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.
|
||||
@ -320,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));
|
||||
|
||||
@ -355,8 +326,7 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
||||
});
|
||||
/** Hack end */
|
||||
}
|
||||
};
|
||||
} // End of PDFJSDev.test('CHROME || GENERIC')
|
||||
}
|
||||
|
||||
class FontFaceObject {
|
||||
constructor(
|
||||
|
Loading…
x
Reference in New Issue
Block a user