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:
Jonas Jenwald 2019-02-10 14:11:23 +01:00
parent af3fcca88d
commit 13230a1123
2 changed files with 29 additions and 38 deletions

View File

@ -1945,7 +1945,7 @@ class WorkerTransport {
fontRegistry, fontRegistry,
}); });
this.fontLoader.bind([font]).then(() => { this.fontLoader.bind(font).then(() => {
this.commonObjs.resolve(id, font); this.commonObjs.resolve(id, font);
}); });
break; break;

View File

@ -60,50 +60,41 @@ class BaseFontLoader {
} }
} }
async bind(fonts) { async bind(font) {
const rules = []; // Add the font to the DOM only once; skip if the font is already loaded.
const fontsToLoad = []; if (font.attached || font.missingFile) {
const fontLoadPromises = []; return;
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) {
// Add the font to the DOM only once; skip if the font is already loaded.
if (font.attached || font.missingFile) {
continue;
}
font.attached = true;
if (this.isFontLoadingAPISupported) {
const nativeFontFace = font.createNativeFontFace();
if (nativeFontFace) {
this.addNativeFontFace(nativeFontFace);
fontLoadPromises.push(getNativeFontPromise(nativeFontFace));
}
} else {
const rule = font.createFontFaceRule();
if (rule) {
this.insertRule(rule);
rules.push(rule);
fontsToLoad.push(font);
}
}
} }
font.attached = true;
if (this.isFontLoadingAPISupported) { if (this.isFontLoadingAPISupported) {
return Promise.all(fontLoadPromises); const nativeFontFace = font.createNativeFontFace();
} else if (rules.length > 0 && !this.isSyncFontLoadingSupported) { if (nativeFontFace) {
this.addNativeFontFace(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}'.`);
}
}
return; // The font was, asynchronously, loaded.
}
// !this.isFontLoadingAPISupported
const rule = font.createFontFaceRule();
if (rule) {
this.insertRule(rule);
if (this.isSyncFontLoadingSupported) {
return; // The font was, synchronously, loaded.
}
return new Promise((resolve) => { return new Promise((resolve) => {
const request = this._queueLoadingCallback(resolve); const request = this._queueLoadingCallback(resolve);
this._prepareFontLoadEvent(rules, fontsToLoad, request); this._prepareFontLoadEvent([rule], [font], request);
}); });
} }
return; // Synchronous font loading supported.
} }
_queueLoadingCallback(callback) { _queueLoadingCallback(callback) {