1 | import { __awaiter } from '../../node_modules/.pnpm/registry.npmjs.org_tslib@2.5.0/node_modules/tslib/tslib.es6.js';
|
2 | import { MethodHandler } from '../../utils/handler.js';
|
3 |
|
4 |
|
5 | const loadFontFace = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
6 | options = Object.assign({ global: false }, options);
|
7 | const { success, fail, complete, family, source, desc = {} } = options;
|
8 | const handle = new MethodHandler({ name: 'loadFontFace', success, fail, complete });
|
9 |
|
10 | const fonts = document.fonts;
|
11 | if (fonts) {
|
12 |
|
13 | const fontFace = new FontFace(family, source, desc);
|
14 | try {
|
15 | yield fontFace.load();
|
16 | fonts.add(fontFace);
|
17 | return handle.success({});
|
18 | }
|
19 | catch (error) {
|
20 | return handle.fail({
|
21 | errMsg: error.message || error
|
22 | });
|
23 | }
|
24 | }
|
25 | else {
|
26 | const style = document.createElement('style');
|
27 | let innerText = `font-family:"${family}";src:${source};font-style:${desc.style || 'normal'};font-weight:${desc.weight || 'normal'};font-variant:${desc.variant || 'normal'};`;
|
28 | if (desc.ascentOverride) {
|
29 | innerText += `ascent-override:${desc.ascentOverride};`;
|
30 | }
|
31 | if (desc.descentOverride) {
|
32 | innerText += `descent-override:${desc.descentOverride};`;
|
33 | }
|
34 | if (desc.featureSettings) {
|
35 | innerText += `font-feature-settings:${desc.featureSettings};`;
|
36 | }
|
37 | if (desc.lineGapOverride) {
|
38 | innerText += `line-gap-override:${desc.lineGapOverride};`;
|
39 | }
|
40 | if (desc.stretch) {
|
41 | innerText += `font-stretch:${desc.stretch};`;
|
42 | }
|
43 | if (desc.unicodeRange) {
|
44 | innerText += `unicode-range:${desc.unicodeRange};`;
|
45 | }
|
46 | if (desc.variationSettings) {
|
47 | innerText += `font-variation-settings:${desc.variationSettings};`;
|
48 | }
|
49 | style.innerText = `@font-face{${innerText}}`;
|
50 | document.head.appendChild(style);
|
51 | return handle.success();
|
52 | }
|
53 | });
|
54 |
|
55 | export { loadFontFace };
|
56 |
|