Skip to content

Commit

Permalink
Better handle possibility of slow rendering:
Browse files Browse the repository at this point in the history
- use document.fonts.ready.then() after the font load
- use window.requestAnimationFrame() after each style change
  • Loading branch information
Lorp committed Jul 25, 2024
1 parent 290ee65 commit 730332b
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src/fencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2027,31 +2027,34 @@ function initFencer() {
// load the font and attach it to the document
const fontFace = new FontFace("avar2-checker", arrayBuffer);
document.fonts.add(fontFace);

// prepare the test div
const testDiv = EL("div");
testDiv.style.fontFamily = "avar2-checker";
testDiv.style.fontSize = "100px";
testDiv.style.display = "inline-block";
testDiv.textContent = "B"; // the B glyph varies in width with avar2
Q(".window-canvas").append(testDiv); // add the test div to the document

// perform the test on the div and store result
testDiv.style.fontVariationSettings = "'AVAR' 0";
const width0 = testDiv.getBoundingClientRect().width;
testDiv.style.fontVariationSettings = "'AVAR' 100";
const width100 = testDiv.getBoundingClientRect().width;
GLOBAL.avar2Active = (width100 > width0); // assign the global

// clean up
testDiv.remove(); // remove the test div

// switch to harfbuzz if avar2 is unsupported by this browser
if (!GLOBAL.avar2Active) {
Q("#renderer").value = "harfbuzz";
Q("#renderer").dispatchEvent(new Event("change"));
}

document.fonts.ready.then(() => {

// prepare the test div
const testDiv = EL("div");
testDiv.style.fontFamily = "avar2-checker";
testDiv.style.fontSize = "100px";
testDiv.style.display = "inline-block";
testDiv.textContent = "B"; // the B glyph varies in width with avar2
Q(".window-canvas").append(testDiv); // add the test div to the document

// perform tests on the div and store results
testDiv.style.fontVariationSettings = "'AVAR' 0";
window.requestAnimationFrame(() => { // ensures the browser has had a chance to render the content in the div
const width0 = testDiv.getBoundingClientRect().width;
testDiv.style.fontVariationSettings = "'AVAR' 100";
window.requestAnimationFrame(() => { // ensures the browser has had a chance to render the content in the div
const width100 = testDiv.getBoundingClientRect().width;
GLOBAL.avar2Active = (width100 > width0); // assign the global
testDiv.remove(); // clean up: remove the test div

// switch to harfbuzz if avar2 is unsupported by this browser
if (!GLOBAL.avar2Active) {
Q("#renderer").value = "harfbuzz";
Q("#renderer").dispatchEvent(new Event("change"));
}
});
});
});
});


Expand Down

0 comments on commit 730332b

Please sign in to comment.