1 | import FontObserver from 'fontfaceobserver';
|
2 | export 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 | };
|
16 | function _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 |
|
24 |
|
25 | if (styleElement.styleSheet) {
|
26 |
|
27 | styleElement.styleSheet.cssText = fontStyle;
|
28 | }
|
29 | else {
|
30 | const textNode = document.createTextNode(fontStyle);
|
31 | styleElement.appendChild(textNode);
|
32 | }
|
33 | return styleElement;
|
34 | }
|
35 |
|
\ | No newline at end of file |