Skip to content

Commit

Permalink
Remove the ability to pass in more than one font to `BaseFontLoader.b…
Browse files Browse the repository at this point in the history
…ind`

 - There's currently no call-site which pass more than *one* font anywhere in the code-base.
 - As far as I can remember, this functionality has never actually been used (caveat: I didn't check the git history).
 - The ability to pass in multiple fonts doesn't work well together with the newly implemented fallback to the built-in PDF.js font renderer.
 - It should be just as easy to call `BaseFontLoader.bind` from within a loop, rather than having the loop in the method itself.
  • Loading branch information
Snuffleupagus committed Feb 4, 2019
1 parent 986cb23 commit 127d0e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ class WorkerTransport {
fontRegistry,
});

this.fontLoader.bind([font]).then((fontObjs) => {
this.fontLoader.bind(font).then(() => {
this.commonObjs.resolve(id, font);
resolve();
}, (reason) => {
Expand Down
52 changes: 25 additions & 27 deletions src/display/font_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,36 @@ class BaseFontLoader {
}
}

async bind(fonts) {
async bind(font) {
const rules = [];
const fontsToLoad = [];

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);
try {
fontsToLoad.push(await nativeFontFace.loaded);
} catch (ex) {
warn(`Failed to load font "${nativeFontFace.family}": ${ex}`);
// If font loading failed, fall back to the built-in font renderer.
font.disableFontFace = true;
throw ex;
}
}
} else {
const rule = font.createFontFaceRule();
if (rule) {
this.insertRule(rule);
rules.push(rule);
fontsToLoad.push(font);
// Add the font to the DOM only once; skip if the font is already loaded.
if (font.attached || font.missingFile) {
return;
}
font.attached = true;

if (this.isFontLoadingAPISupported) {
const nativeFontFace = font.createNativeFontFace();
if (nativeFontFace) {
this.addNativeFontFace(nativeFontFace);
try {
fontsToLoad.push(await nativeFontFace.loaded);
} catch (ex) {
warn(`Failed to load font "${nativeFontFace.family}": ${ex}`);
// If font loading failed, fall back to the built-in font renderer.
font.disableFontFace = true;
throw ex;
}
}
} else {
const rule = font.createFontFaceRule();
if (rule) {
this.insertRule(rule);
rules.push(rule);
fontsToLoad.push(font);
}
}

if (this.isFontLoadingAPISupported) {
Expand Down

0 comments on commit 127d0e1

Please sign in to comment.