/
FontFaceAssetLoader.js
148 lines (126 loc) · 3.42 KB
/
FontFaceAssetLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { AssetLoader } from "./AssetLoader";
import { FontMetrics } from "../../display/text/FontMetrics";
/**
* Responsible for loading local or Google fonts.
*
* @cat assets.loaders
* @extends AssetLoader
*/
export class FontFaceAssetLoader extends AssetLoader {
/**
* Creates new FontFaceAssetLoader instance.
*
* @param {string} name The custom name of the font
* @param {string} url The path to the font
* @param {boolean} isLocal Pass `true` if font is local otherwise Google Fonts service is used.
*/
constructor(name, url, isLocal) {
super(url);
/**
* @private
* @type {string}
*/
this.mName = name;
/**
* @private
* @type {string}
*/
this.mTestingFontName = 'Courier New';
/**
* @private
* @type {boolean}
*/
this.mIsLocal = isLocal;
/**
* @private
* @type {string}
*/
this.mTestingString = '~ GHBDTN,.#$Mlck';
/**
* @private
* @type {number}
*/
this.mCheckDelay = 50;
/**
* @private
* @type {HTMLElement}
*/
this.mTestingElement = this.__getTestingElement();
/**
* @private
* @type {FontMetrics|null}
*/
this.metrics = null;
/**
* @private
* @type {Element}
*/
this.mLoaderElement = this.__getLoaderElement(this.mIsLocal);
this.mTestingElement.style.fontFamily = this.mTestingFontName;
/**
* @private
* @type {number}
*/
this.mDefaultFontWidth = this.mTestingElement.offsetWidth;
this.mTestingElement.style.fontFamily = '"' + name + '",' + this.mTestingFontName;
/**
* @private
* @type {number}
*/
this.mTimeoutHandle = -1;
}
load() {
if (this.mIsLocal)
this.mLoaderElement.innerHTML += (`\n @font-face {font-family: "${this.mName}"; src: url(${this.mUrl});}`);
else
this.mLoaderElement.href = this.mUrl;
this.__checkLoadingStatus();
}
onAbort() {
clearTimeout(this.mTimeoutHandle);
this.mTestingElement.parentNode.removeChild(this.mTestingElement);
}
/**
* @ignore
* @private
* @return {Element}
*/
__getLoaderElement(local) {
let loaderElement = document.createElement(local ? 'style' : 'link');
loaderElement.type = 'text/css';
loaderElement.media = 'all';
loaderElement.rel = 'stylesheet';
loaderElement.onerror = () => { this.onError(); };
document.getElementsByTagName('head')[0].appendChild(loaderElement);
return loaderElement;
}
/**
* @ignore
* @private
* @return {HTMLElement}
*/
__getTestingElement() {
let testingElement = /** @type {HTMLElement}*/ (document.createElement('span'));
testingElement.style.position = 'absolute';
testingElement.style.top = '-9999px';
testingElement.style.left = '-9999px';
testingElement.style.visibility = 'hidden';
testingElement.style.fontSize = '250px';
testingElement.innerHTML = this.mTestingString;
document.body.appendChild(testingElement);
return testingElement;
}
/**
* @private
* @return {void}
*/
__checkLoadingStatus() {
if (this.mDefaultFontWidth === this.mTestingElement.offsetWidth) {
this.mTimeoutHandle = setTimeout(this.__checkLoadingStatus.bind(this), this.mCheckDelay);
return;
}
this.metrics = FontMetrics.get(this.mName);
this.mTestingElement.parentNode.removeChild(this.mTestingElement);
this.onLoad();
}
}