Fix font rerender test when font is a system font
This commit is contained in:
@@ -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);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user