Files
engine/packages/loader/src/FontLoader.ts
云游君 f0fdb2452b ci: faster with lock & better practice by github actions (#1508)
* ci: faster with lock & better practice by github actions

* ci: add move os test & use build instead of ci
2023-05-27 14:13:05 +08:00

45 lines
1.2 KiB
TypeScript

import {
AssetPromise,
AssetType,
Font,
Loader,
LoadItem,
resourceLoader,
ResourceManager
} from "@galacean/engine-core";
@resourceLoader(AssetType.Font, ["font"], false)
class FontLoader extends Loader<Font> {
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<Font> {
return new AssetPromise((resolve, reject) => {
this.request<any>(item.url, { type: "json" })
.then((data) => {
const { fontName, fontUrl } = data;
if (fontUrl) {
this._registerFont(fontName, fontUrl)
.then(() => {
const font = new Font(resourceManager.engine, fontName);
resolve(font);
})
.catch((e) => {
reject(`load font ${fontUrl} fail`);
});
} else {
const font = new Font(resourceManager.engine, fontName);
resolve(font);
}
})
.catch((e) => {
reject(e);
});
});
}
private async _registerFont(fontName: string, fontUrl: string): Promise<void> {
const fontFace = new FontFace(fontName, `url(${fontUrl})`);
await fontFace.load();
document.fonts.add(fontFace);
}
}