Skip to content

Commit 86bc967

Browse files
authored
Implement FontFace polyfill to expose loading of custom fonts (#280)
* Implement FontFace polyfill * Fix typecheck * Expose status property * Address comment * Update gulpfile.js to include FontFace
1 parent 5c0c8b1 commit 86bc967

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Tools } from '@babylonjs/core';
2+
3+
// Declare _native to get access to NativeCanvas.
4+
declare var _native: any;
5+
6+
/**
7+
* Partial Polyfill for FontFace Web API to wrap the NativeCanvas object.
8+
*/
9+
export class FontFace {
10+
private source: string | ArrayBuffer | undefined;
11+
private _status: "unloaded" | "loading" | "loaded" | "error" = "unloaded";
12+
public get status(): "unloaded" | "loading" | "loaded" | "error" {
13+
return this._status;
14+
}
15+
16+
public get loaded(): boolean {
17+
return this._status === "loaded";
18+
}
19+
20+
public constructor(public readonly family: string, source: string | ArrayBuffer) {
21+
this.source = source;
22+
}
23+
24+
public async load(): Promise<void> {
25+
try {
26+
this._status = "loading";
27+
if (typeof this.source === 'string') {
28+
this.source = await Tools.LoadFileAsync(this.source);
29+
}
30+
31+
await _native.NativeCanvas.loadTTFAsync(this.family, this.source);
32+
this.source = undefined;
33+
this._status = "loaded"
34+
} catch (ex) {
35+
console.error("Error encountered when loading font: " + ex);
36+
this._status = "error";
37+
}
38+
}
39+
}
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './EngineView';
22
export * from './EngineHook';
33
export * from './NativeCapture';
4+
export * from './FontFace';

Package/gulpfile.js

+3
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ const validate = async () => {
493493
'Assembled/NativeCapture.d.ts',
494494
'Assembled/NativeCapture.js',
495495
'Assembled/NativeCapture.js.map',
496+
'Assembled/FontFace.d.ts',
497+
'Assembled/FontFace.js',
498+
'Assembled/FontFace.js.map',
496499
'Assembled/package.json',
497500
'Assembled/react-native-babylon.podspec',
498501
'Assembled/ReactNativeEngine.d.ts',

0 commit comments

Comments
 (0)