UNPKG

1.29 kBJavaScriptView Raw
1import FontObserver from 'fontfaceobserver';
2export default {
3 get name() {
4 return 'ExpoFontLoader';
5 },
6 loadAsync(fontFamilyName, resource) {
7 const canInjectStyle = document.head && typeof document.head.appendChild === 'function';
8 if (!canInjectStyle) {
9 throw new Error('E_FONT_CREATION_FAILED : document element cannot support injecting fonts');
10 }
11 const style = _createWebStyle(fontFamilyName, resource);
12 document.head.appendChild(style);
13 return new FontObserver(fontFamilyName).load();
14 },
15};
16function _createWebStyle(fontFamily, resource) {
17 const fontStyle = `@font-face {
18 font-family: ${fontFamily};
19 src: url(${resource});
20 }`;
21 const styleElement = document.createElement('style');
22 styleElement.type = 'text/css';
23 // @ts-ignore: TypeScript does not define HTMLStyleElement::styleSheet. This is just for IE and
24 // possibly can be removed if it's unnecessary on IE 11.
25 if (styleElement.styleSheet) {
26 // @ts-ignore
27 styleElement.styleSheet.cssText = fontStyle;
28 }
29 else {
30 const textNode = document.createTextNode(fontStyle);
31 styleElement.appendChild(textNode);
32 }
33 return styleElement;
34}
35//# sourceMappingURL=ExpoFontLoader.web.js.map
\No newline at end of file