Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48ba0c7

Browse files
committed
refactor(soba/loaders): use fontResource in injectFont
1 parent eadbf53 commit 48ba0c7

File tree

2 files changed

+41
-67
lines changed

2 files changed

+41
-67
lines changed
Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,21 @@
1-
import { effect, Injector, signal } from '@angular/core';
1+
import { Injector } from '@angular/core';
22
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';
404

415
/**
426
* @deprecated Use fontResource instead. Will be removed in v5.0.0
437
* @since v4.0.0
448
*/
459
export function injectFont(input: () => NgtsFontInput, { injector }: { injector?: Injector } = {}) {
4610
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();
6513
});
6614
}
6715

6816
injectFont.preload = (input: () => NgtsFontInput) => {
69-
loadFontData(input()).then((data) => {
70-
const parsed = parseFontData(data);
71-
cache.set(input(), parsed);
72-
});
17+
fontResource.preload(input());
7318
};
7419
injectFont.clear = (input?: () => NgtsFontInput) => {
75-
if (input) {
76-
cache.delete(input());
77-
} else {
78-
cache.clear();
79-
}
20+
fontResource.clear(input?.());
8021
};

libs/soba/loaders/src/lib/font-resource.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
11
import { Injector, resource } from '@angular/core';
22
import { assertInjector } from 'ngxtension/assert-injector';
3-
import { Font } from 'three-stdlib';
4-
import { loadFontData, NgtsFontInput, parseFontData } from './font-loader';
3+
import { Font, FontLoader } from 'three-stdlib';
4+
5+
type Glyph = {
6+
_cachedOutline: string[];
7+
ha: number;
8+
o: string;
9+
};
10+
11+
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+
async function loadFontData(font: NgtsFontInput): Promise<FontData> {
29+
return typeof font === 'string' ? await (await fetch(font)).json() : font;
30+
}
31+
32+
function parseFontData(fontData: FontData) {
33+
if (!fontLoader) {
34+
fontLoader = new FontLoader();
35+
}
36+
return fontLoader.parse(fontData);
37+
}
538

639
const cache = new Map<NgtsFontInput, Font>();
740

0 commit comments

Comments
 (0)