forked from Kos/js13k-2021
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.ts
131 lines (128 loc) · 3.4 KB
/
text.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { max } from "./math";
import { makeMeshDrawCall, preprocessModel, Props, TModel } from "./models";
import { gl } from "./regl";
const font: Record<string, number[][]> = {
A: [
[-3, 0, 0, 9, 3, 0],
[-1, 5, 1, 5],
],
B: [[-2, 0, -2, 9, 1, 9, 3, 7, 1, 5, 3, 3, 0, 0, 0, 0, -3]],
C: [[2, 9, -1, 9, -2, 7, -2, 2, 0, 0, 2, 0]],
D: [[-2, 9, 0, 9, 2, 7, 2, 3, -1, 0, -2, 0, -2, 9]],
E: [
[2, 8, 2, 9, -2, 9, -2, 0, 2, 0, 2, 1],
[-2, 5, 1, 5],
],
F: [
[2, 8, 2, 9, -2, 9, -2, 0],
[-2, 5, 1, 5],
],
G: [[2, 9, -1, 9, -2, 7, -2, 2, 0, 0, 2, 0, 3, 4, 1, 4]],
H: [
[-3, 9, -2, 9, -2, 0, -3, 0],
[3, 9, 2, 9, 2, 0, 3, 0],
[-2, 5, 2, 5],
],
I: [[0, 9.5, 0, -0.5]],
J: [[1, 9, 1, 1, 0, 0, -1, 0, -2, 1, -2, 2]],
K: [
[-2, 9, -2, 0],
[-2, 5, 2, 9],
[-2, 5, 2, 0],
],
L: [[-2, 9, -2, 0, 1, 0, 1, 1]],
M: [[-2, 0, -2, 9, 0, 3, 2, 9, 2, 0]],
N: [[-2, 0, -2, 9, 2, 0, 2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2, 6, 2, 2, -1, 0]],
"/": [[2, 9, -2, 0]],
".": [[0, 0, 1, 0]],
};
function makeTextModel(text: string): TModel {
const verts: number[][] = [],
elements: number[][] = [];
let caret = 0;
let dy = 0;
for (const letter of text.toUpperCase()) {
const dx = caret;
caret += 5.5;
if (letter === "\n") {
caret = 0;
dy -= 11;
}
const glyph = font[letter];
if (!glyph) continue;
glyph.forEach((stroke) => {
for (let i = 0; i < stroke.length; i += 2) {
// add vertex
verts.push([stroke[i] + dx, stroke[i + 1] + dy, 0]);
// if second or further vertex, add element pair
if (i) {
elements.push([verts.length - 2, verts.length - 1]);
}
}
});
}
// Centering
const offset = max(...verts.map((x) => x[0]));
for (let a of verts) {
a[0] -= offset / 2;
}
return { verts, elements };
}
export function makeTextDrawcall(text: string) {
return makeMeshDrawCall(preprocessModel(makeTextModel(text)));
}
export function makeOneOffText(text: string) {
const m = preprocessModel(makeTextModel(text));
return (props: Props) => {
makeMeshDrawCall(m)(props);
[m.Elements.buffer, m.Normal.buf, m.Position.buf, m.Side.buf].map((x) =>
gl.deleteBuffer(x)
);
};
}