Skip to content

Commit 228159e

Browse files
committed
4.0.0
1 parent 0d0c942 commit 228159e

File tree

15 files changed

+515
-690
lines changed

15 files changed

+515
-690
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ new TextMeshBuilder(BABYLON, earcut);
8888
```
8989

9090
- `.create(options, scene)` : create extruded text mesh.
91-
92-
Ex.
91+
- returns `Mesh` if a mesh is created.
92+
- returns `null` if no mesh is created, e.g. text are all whitespaces.
93+
- returns `undefined` if wasm failed to process (e.g. out of mem)
9394

9495
```js
9596
builder.create({
@@ -110,6 +111,13 @@ builder.create({
110111
}, scene);
111112
```
112113

114+
ex. fallback
115+
116+
```js
117+
var mesh = builder.create(opt1, scene)
118+
if (mesh === undefined) mesh = builder.create(opt2, scene)
119+
```
120+
113121

114122

115123
# Thanks

assembly/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,11 @@ function isPolygonInsidePolygon(
612612
}
613613
return isPointInsidePolygon(A[0], B);
614614
}
615+
616+
617+
618+
//
619+
// Export memoryBase
620+
//
621+
622+
export const MEMORY_BASE = ASC_MEMORY_BASE

build/babylon.font.d.ts

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
import * as loader from '@assemblyscript/loader';
22
import * as BABYLON from 'babylonjs';
33

4-
interface IPathCommand {
5-
type: string;
6-
x?: number;
7-
y?: number;
8-
x1?: number;
9-
y1?: number;
10-
x2?: number;
11-
y2?: number;
12-
}
4+
declare type PathCommand = {
5+
type: 'M' | 'L';
6+
x: number;
7+
y: number;
8+
} | {
9+
type: 'Q';
10+
x: number;
11+
y: number;
12+
x1: number;
13+
y1: number;
14+
} | {
15+
type: 'C';
16+
x: number;
17+
y: number;
18+
x1: number;
19+
y1: number;
20+
x2: number;
21+
y2: number;
22+
} | {
23+
type: 'Z';
24+
};
1325
declare type Header = {
26+
MEMORY_BASE: {
27+
value: number;
28+
};
1429
compile(bytesUsed: number, fmt: string, ppc: number, eps: number): number;
1530
};
16-
declare type Instaniated = loader.ResultObject & {
31+
declare type Wasm = loader.ResultObject & {
1732
exports: loader.ASUtil & Header;
1833
};
1934
declare type Vertex = [number, number];
@@ -24,14 +39,33 @@ declare type Shape = {
2439
};
2540
declare class Compiler {
2641
private wasm;
27-
constructor(wasm: Instaniated);
42+
constructor(wasm: Wasm);
2843
static Build(wasmUrl?: string): Promise<Compiler>;
29-
encode(cmds: IPathCommand[], buffer: ArrayBuffer): number;
30-
compileEncoded(buffer: ArrayBuffer, bytesUsed: number, fmt: string, ppc: number, eps: number): Shape[];
31-
compile(cmds: IPathCommand[], fmt: string, ppc: number, eps: number): Shape[];
44+
encode(cmds: PathCommand[]): number;
45+
compile(cmds: PathCommand[], fmt: string, ppc: number, eps: number): Shape[] | null;
46+
private bytesRequired;
3247
}
3348

34-
declare type PolygonMeshOption = {
49+
declare class Metrics {
50+
private font;
51+
private name;
52+
private size;
53+
constructor(font: Font, name: string, size: number);
54+
get ascender(): number;
55+
get descender(): number;
56+
get advanceWidth(): number;
57+
}
58+
59+
declare class Font {
60+
raw: opentype.Font;
61+
private compiler;
62+
private constructor();
63+
static Install(fontUrl: string, compiler: Compiler, opentype: any): Promise<Font>;
64+
measure(name: string, size: number): Metrics;
65+
static Compile(font: Font, name: string, size: number, ppc: number, eps: number): Shape[] | null;
66+
}
67+
68+
declare type CreatePolygonOptions = {
3569
backUVs?: BABYLON.Vector4;
3670
depth?: number;
3771
faceColors?: BABYLON.Color4[];
@@ -40,40 +74,19 @@ declare type PolygonMeshOption = {
4074
sideOrientation?: number;
4175
updatable?: boolean;
4276
};
43-
declare class Font {
44-
raw: opentype.Font;
45-
private compiler;
46-
private constructor();
47-
static Install(fontUrl: string, compiler: Compiler, opentype: any): Promise<Font>;
48-
measure(name: string, size: number): Metrics;
49-
static Compile(font: Font, name: string, size: number, ppc: number, eps: number): Shape[];
50-
}
51-
interface IBabylon {
52-
Mesh: typeof BABYLON.Mesh;
53-
MeshBuilder: typeof BABYLON.MeshBuilder;
54-
Vector3: typeof BABYLON.Vector3;
55-
}
77+
declare type TextBuilderCreateOptions = {
78+
font: Font;
79+
text: string;
80+
size: number;
81+
ppc: number;
82+
eps: number;
83+
} & CreatePolygonOptions;
5684
declare class TextMeshBuilder {
5785
private babylon;
5886
private earcut;
59-
constructor(babylon: IBabylon, earcut: any);
87+
constructor(babylon: typeof BABYLON, earcut: any);
6088
private createFromShapes;
61-
create({ font, text, size, ppc, eps, ...option }: {
62-
font: Font;
63-
text: string;
64-
size: number;
65-
ppc: number;
66-
eps: number;
67-
} & PolygonMeshOption, scene?: BABYLON.Scene): BABYLON.Mesh;
68-
}
69-
declare class Metrics {
70-
private font;
71-
private name;
72-
private size;
73-
constructor(font: Font, name: string, size: number);
74-
get ascender(): number;
75-
get descender(): number;
76-
get advanceWidth(): number;
89+
create({ font, text, size, ppc, eps, ...createPolygonOptions }: TextBuilderCreateOptions, scene?: BABYLON.Scene): BABYLON.Nullable<BABYLON.Mesh> | undefined;
7790
}
7891

79-
export { Compiler, Font, Metrics, Shape, TextMeshBuilder };
92+
export { Compiler, Font, Polygon, Shape, TextMeshBuilder, Vertex };

build/babylon.font.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/optimized.wasm

1 Byte
Binary file not shown.

examples/bare/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8">
55
<link rel="icon" href="data:,">
6-
<title>Bare Example</title>
6+
<title>Bare Example (v4.0.0)</title>
77
<style>
88
html,
99
body {

examples/bare/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const fontUrl = "../font/NotoSansMono-Thin.ttf";
1414

1515
// Env
1616
const canvas = document.querySelector("canvas");
17-
const engine = new BABYLON.Engine(canvas, true, null, true);
17+
const engine = new BABYLON.Engine(canvas, true, undefined, true);
1818
const scene = new BABYLON.Scene(engine);
1919
engine.runRenderLoop(() => scene.render());
2020
window.addEventListener("resize", () => engine.resize());
@@ -31,7 +31,7 @@ const fontUrl = "../font/NotoSansMono-Thin.ttf";
3131

3232
// Params
3333
const params = {
34-
text: "BF3.0",
34+
text: "BF4.0",
3535
size: 100,
3636
ppc: 2,
3737
eps: 0.001,
@@ -51,6 +51,15 @@ const fontUrl = "../font/NotoSansMono-Thin.ttf";
5151
depth: params.depth,
5252
sideOrientation: BABYLON.Mesh.DOUBLESIDE
5353
}, scene);
54+
55+
if (mesh === undefined) { // wasm fails to process
56+
return null
57+
}
58+
59+
if (mesh === null) { // no mesh created, e.g. text are all whtiespaces
60+
return null
61+
}
62+
5463
const info = mesh.getBoundingInfo();
5564
mesh.position.copyFrom(info.boundingBox.center.scale(-1));
5665
return mesh;

0 commit comments

Comments
 (0)