Fix font rerender test when font is a system font

This commit is contained in:
Maximilian Krög
2022-05-15 22:22:18 +02:00
parent 72d61f887d
commit 744324859e
4 changed files with 66 additions and 16 deletions
+30
View File
@@ -14,3 +14,33 @@ export function overrideRAF() {
window.cancelAnimationFrame = caf;
};
}
export function createFontStyle(options) {
const styleNode = document.createElement('style');
const src = Array.isArray(options.src) ? options.src : [options.src];
function toCssSource(src) {
const url = typeof src === 'string' ? src : src.url;
const format = typeof src === 'string' ? undefined : src.format;
return `url('${url}')${format ? ` format('${format}')` : ''}`;
}
const ruleText = `
@font-face {
font-family: '${options.fontFamily}';
font-style: ${options.fontStyle || 'normal'};
font-weight: ${
options.fontWeight === undefined ? 400 : options.fontWeight
};
src: ${src.map(toCssSource).join(',\n ')};
}`;
return {
add() {
document.head.appendChild(styleNode);
if (styleNode.sheet.cssRules.length === 0) {
styleNode.sheet.insertRule(ruleText);
}
},
remove() {
document.head.removeChild(styleNode);
},
};
}