Skip to content

Commit 510f790

Browse files
authored
chore: Move addFont to DOM Utils (#8533)
1 parent 45e7c39 commit 510f790

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

lib/player.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8612,33 +8612,11 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
86128612
*
86138613
* @param {string} name
86148614
* @param {string} url
8615+
* @return {!Promise<void>}
86158616
* @export
86168617
*/
8617-
async addFont(name, url) {
8618-
if ('fonts' in document && 'FontFace' in window ) {
8619-
await document.fonts.ready;
8620-
if (!('entries' in document.fonts)) {
8621-
return;
8622-
}
8623-
const fontFaceSetIteratorToArray = (target) => {
8624-
const iterable = target.entries();
8625-
const results = [];
8626-
let iterator = iterable.next();
8627-
while (iterator.done === false) {
8628-
results.push(iterator.value);
8629-
iterator = iterable.next();
8630-
}
8631-
return results;
8632-
};
8633-
for (const fontFace of fontFaceSetIteratorToArray(document.fonts)) {
8634-
if (fontFace.family == name && fontFace.display == 'swap') {
8635-
// Font already loaded.
8636-
return;
8637-
}
8638-
}
8639-
const fontFace = new FontFace(name, `url(${url})`, {display: 'swap'});
8640-
document.fonts.add(fontFace);
8641-
}
8618+
addFont(name, url) {
8619+
return shaka.util.Dom.addFont(name, url);
86428620
}
86438621

86448622
/**

lib/util/dom_utils.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,41 @@ shaka.util.Dom = class {
158158
element.removeChild(element.firstChild);
159159
}
160160
}
161+
162+
/**
163+
* Load a new font on the page. If the font was already loaded, it does
164+
* nothing.
165+
*
166+
* @param {string} name
167+
* @param {string} url
168+
* @return {!Promise<void>}
169+
*/
170+
static async addFont(name, url) {
171+
if (!('fonts' in document && 'FontFace' in window)) {
172+
return;
173+
}
174+
await document.fonts.ready;
175+
if (!('entries' in document.fonts)) {
176+
return;
177+
}
178+
const fontFaceSetIteratorToArray = (target) => {
179+
const iterable = target.entries();
180+
const results = [];
181+
let iterator = iterable.next();
182+
while (iterator.done === false) {
183+
results.push(iterator.value);
184+
iterator = iterable.next();
185+
}
186+
return results;
187+
};
188+
for (const fontFace of fontFaceSetIteratorToArray(document.fonts)) {
189+
if (fontFace.family === name && fontFace.display === 'swap') {
190+
// Font already loaded.
191+
return;
192+
}
193+
}
194+
const fontFace = new FontFace(name, `url(${url})`, {display: 'swap'});
195+
document.fonts.add(fontFace);
196+
}
161197
};
162198

0 commit comments

Comments
 (0)