Merge pull request #15507 from Snuffleupagus/FontLoader-cleanup

Improve the `FontLoader` code
This commit is contained in:
Tim van der Meij 2022-09-25 13:31:33 +02:00 committed by GitHub
commit dab81f5981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,42 +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.loadingContext = {
requests: [],
nextRequestId: 0,
};
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") {
@ -183,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;
} }
@ -196,33 +159,39 @@ 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;
// Sending all completed requests in order of how they were queued. // Sending all completed requests in order of how they were queued.
while (context.requests.length > 0 && context.requests[0].done) { while (loadingRequests.length > 0 && loadingRequests[0].done) {
const otherRequest = context.requests.shift(); const otherRequest = loadingRequests.shift();
setTimeout(otherRequest.callback, 0); setTimeout(otherRequest.callback, 0);
} }
} }
const context = this.loadingContext; const { loadingRequests } = this;
const request = { const request = {
id: `pdfjs-font-loading-${context.nextRequestId++}`,
done: false, done: false,
complete: completeRequest, complete: completeRequest,
callback, callback,
}; };
context.requests.push(request); loadingRequests.push(request);
return request; return request;
} }
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" +
@ -246,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.
@ -320,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));
@ -355,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(