|
1 |
| -import { effect, Injector, signal } from '@angular/core'; |
| 1 | +import { Injector } from '@angular/core'; |
2 | 2 | import { assertInjector } from 'ngxtension/assert-injector';
|
3 |
| -import { Font, FontLoader } from 'three-stdlib'; |
4 |
| - |
5 |
| -export type Glyph = { |
6 |
| - _cachedOutline: string[]; |
7 |
| - ha: number; |
8 |
| - o: string; |
9 |
| -}; |
10 |
| - |
11 |
| -export type FontData = { |
12 |
| - boundingBox: { |
13 |
| - yMax: number; |
14 |
| - yMin: number; |
15 |
| - }; |
16 |
| - familyName: string; |
17 |
| - glyphs: { |
18 |
| - [k: string]: Glyph; |
19 |
| - }; |
20 |
| - resolution: number; |
21 |
| - underlineThickness: number; |
22 |
| -}; |
23 |
| - |
24 |
| -export type NgtsFontInput = string | FontData; |
25 |
| - |
26 |
| -let fontLoader: FontLoader | null = null; |
27 |
| - |
28 |
| -export async function loadFontData(font: NgtsFontInput): Promise<FontData> { |
29 |
| - return typeof font === 'string' ? await (await fetch(font)).json() : font; |
30 |
| -} |
31 |
| - |
32 |
| -export function parseFontData(fontData: FontData) { |
33 |
| - if (!fontLoader) { |
34 |
| - fontLoader = new FontLoader(); |
35 |
| - } |
36 |
| - return fontLoader.parse(fontData); |
37 |
| -} |
38 |
| - |
39 |
| -const cache = new Map<NgtsFontInput, Font>(); |
| 3 | +import { fontResource, type NgtsFontInput } from './font-resource'; |
40 | 4 |
|
41 | 5 | /**
|
42 | 6 | * @deprecated Use fontResource instead. Will be removed in v5.0.0
|
43 | 7 | * @since v4.0.0
|
44 | 8 | */
|
45 | 9 | export function injectFont(input: () => NgtsFontInput, { injector }: { injector?: Injector } = {}) {
|
46 | 10 | return assertInjector(injectFont, injector, () => {
|
47 |
| - const font = signal<Font | null>(null); |
48 |
| - |
49 |
| - effect(() => { |
50 |
| - const fontInput = input(); |
51 |
| - |
52 |
| - if (cache.has(fontInput)) { |
53 |
| - font.set(cache.get(fontInput) as Font); |
54 |
| - return; |
55 |
| - } |
56 |
| - |
57 |
| - loadFontData(input()).then((data) => { |
58 |
| - const parsed = parseFontData(data); |
59 |
| - cache.set(fontInput, parsed); |
60 |
| - font.set(parsed); |
61 |
| - }); |
62 |
| - }); |
63 |
| - |
64 |
| - return font.asReadonly(); |
| 11 | + const resource = fontResource(input, { injector }); |
| 12 | + return resource.value.asReadonly(); |
65 | 13 | });
|
66 | 14 | }
|
67 | 15 |
|
68 | 16 | injectFont.preload = (input: () => NgtsFontInput) => {
|
69 |
| - loadFontData(input()).then((data) => { |
70 |
| - const parsed = parseFontData(data); |
71 |
| - cache.set(input(), parsed); |
72 |
| - }); |
| 17 | + fontResource.preload(input()); |
73 | 18 | };
|
74 | 19 | injectFont.clear = (input?: () => NgtsFontInput) => {
|
75 |
| - if (input) { |
76 |
| - cache.delete(input()); |
77 |
| - } else { |
78 |
| - cache.clear(); |
79 |
| - } |
| 20 | + fontResource.clear(input?.()); |
80 | 21 | };
|
0 commit comments