Remove the ability to pass in more than one font to BaseFontLoader.bind
- The only existing call-site, of this method, is never passing more than *one* font at a time anyway. - As far as I can remember, this functionality has never actually been used (caveat: I didn't check the git history). - This allows simplification of the method, especially by making use of the fact that it's now asynchronous. - It should be just as easy to call `BaseFontLoader.bind` from within a loop, rather than having the loop in the method itself.
This commit is contained in:
parent
af3fcca88d
commit
13230a1123
@ -1945,7 +1945,7 @@ class WorkerTransport {
|
||||
fontRegistry,
|
||||
});
|
||||
|
||||
this.fontLoader.bind([font]).then(() => {
|
||||
this.fontLoader.bind(font).then(() => {
|
||||
this.commonObjs.resolve(id, font);
|
||||
});
|
||||
break;
|
||||
|
@ -60,22 +60,10 @@ class BaseFontLoader {
|
||||
}
|
||||
}
|
||||
|
||||
async bind(fonts) {
|
||||
const rules = [];
|
||||
const fontsToLoad = [];
|
||||
const fontLoadPromises = [];
|
||||
const getNativeFontPromise = function(nativeFontFace) {
|
||||
// Return a promise that is always fulfilled, even when the font fails to
|
||||
// load.
|
||||
return nativeFontFace.loaded.catch(function(reason) {
|
||||
warn(`Failed to load font "${nativeFontFace.family}": ${reason}`);
|
||||
});
|
||||
};
|
||||
|
||||
for (const font of fonts) {
|
||||
async bind(font) {
|
||||
// Add the font to the DOM only once; skip if the font is already loaded.
|
||||
if (font.attached || font.missingFile) {
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
font.attached = true;
|
||||
|
||||
@ -83,27 +71,30 @@ class BaseFontLoader {
|
||||
const nativeFontFace = font.createNativeFontFace();
|
||||
if (nativeFontFace) {
|
||||
this.addNativeFontFace(nativeFontFace);
|
||||
fontLoadPromises.push(getNativeFontPromise(nativeFontFace));
|
||||
try {
|
||||
await nativeFontFace.loaded;
|
||||
} catch (ex) {
|
||||
// Return a promise that is always fulfilled, even when the font
|
||||
// failed to load.
|
||||
warn(`Failed to load font '${nativeFontFace.family}': '${ex}'.`);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
return; // The font was, asynchronously, loaded.
|
||||
}
|
||||
|
||||
// !this.isFontLoadingAPISupported
|
||||
const rule = font.createFontFaceRule();
|
||||
if (rule) {
|
||||
this.insertRule(rule);
|
||||
rules.push(rule);
|
||||
fontsToLoad.push(font);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.isFontLoadingAPISupported) {
|
||||
return Promise.all(fontLoadPromises);
|
||||
} else if (rules.length > 0 && !this.isSyncFontLoadingSupported) {
|
||||
if (this.isSyncFontLoadingSupported) {
|
||||
return; // The font was, synchronously, loaded.
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
const request = this._queueLoadingCallback(resolve);
|
||||
this._prepareFontLoadEvent(rules, fontsToLoad, request);
|
||||
this._prepareFontLoadEvent([rule], [font], request);
|
||||
});
|
||||
}
|
||||
return; // Synchronous font loading supported.
|
||||
}
|
||||
|
||||
_queueLoadingCallback(callback) {
|
||||
|
Loading…
Reference in New Issue
Block a user