-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFont.ts
More file actions
194 lines (191 loc) · 7.57 KB
/
Font.ts
File metadata and controls
194 lines (191 loc) · 7.57 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/** Source glyph width in pixels. */
const fontWidth = 6
/** Source glyph height in pixels. */
const fontHeight = 8
/** ASCII code of first character. */
const firstChar = 32
/** ASCII code of last character. */
const lastChar = 126
/** Atlas supersampling scale factor. */
const atlasScale = 4
/** Bitmap column data per character. */
const glyphData: number[][] = [
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0x5f, 0x00, 0x00, 0x00],
[0x00, 0x07, 0x00, 0x07, 0x00, 0x00],
[0x14, 0x7f, 0x14, 0x7f, 0x14, 0x00],
[0x24, 0x2a, 0x7f, 0x2a, 0x12, 0x00],
[0x23, 0x13, 0x08, 0x64, 0x62, 0x00],
[0x36, 0x49, 0x55, 0x22, 0x50, 0x00],
[0x00, 0x05, 0x03, 0x00, 0x00, 0x00],
[0x00, 0x1c, 0x22, 0x41, 0x00, 0x00],
[0x00, 0x41, 0x22, 0x1c, 0x00, 0x00],
[0x08, 0x2a, 0x1c, 0x2a, 0x08, 0x00],
[0x08, 0x08, 0x3e, 0x08, 0x08, 0x00],
[0x00, 0x50, 0x30, 0x00, 0x00, 0x00],
[0x08, 0x08, 0x08, 0x08, 0x08, 0x00],
[0x00, 0x60, 0x60, 0x00, 0x00, 0x00],
[0x20, 0x10, 0x08, 0x04, 0x02, 0x00],
[0x3e, 0x51, 0x49, 0x45, 0x3e, 0x00],
[0x00, 0x42, 0x7f, 0x40, 0x00, 0x00],
[0x42, 0x61, 0x51, 0x49, 0x46, 0x00],
[0x21, 0x41, 0x45, 0x4b, 0x31, 0x00],
[0x18, 0x14, 0x12, 0x7f, 0x10, 0x00],
[0x27, 0x45, 0x45, 0x45, 0x39, 0x00],
[0x3c, 0x4a, 0x49, 0x49, 0x30, 0x00],
[0x01, 0x71, 0x09, 0x05, 0x03, 0x00],
[0x36, 0x49, 0x49, 0x49, 0x36, 0x00],
[0x06, 0x49, 0x49, 0x29, 0x1e, 0x00],
[0x00, 0x36, 0x36, 0x00, 0x00, 0x00],
[0x00, 0x56, 0x36, 0x00, 0x00, 0x00],
[0x08, 0x14, 0x22, 0x41, 0x00, 0x00],
[0x14, 0x14, 0x14, 0x14, 0x14, 0x00],
[0x00, 0x41, 0x22, 0x14, 0x08, 0x00],
[0x02, 0x01, 0x51, 0x09, 0x06, 0x00],
[0x32, 0x49, 0x79, 0x41, 0x3e, 0x00],
[0x7e, 0x11, 0x11, 0x11, 0x7e, 0x00],
[0x7f, 0x49, 0x49, 0x49, 0x36, 0x00],
[0x3e, 0x41, 0x41, 0x41, 0x22, 0x00],
[0x7f, 0x41, 0x41, 0x22, 0x1c, 0x00],
[0x7f, 0x49, 0x49, 0x49, 0x41, 0x00],
[0x7f, 0x09, 0x09, 0x09, 0x01, 0x00],
[0x3e, 0x41, 0x49, 0x49, 0x7a, 0x00],
[0x7f, 0x08, 0x08, 0x08, 0x7f, 0x00],
[0x00, 0x41, 0x7f, 0x41, 0x00, 0x00],
[0x20, 0x40, 0x41, 0x3f, 0x01, 0x00],
[0x7f, 0x08, 0x14, 0x22, 0x41, 0x00],
[0x7f, 0x40, 0x40, 0x40, 0x40, 0x00],
[0x7f, 0x02, 0x04, 0x02, 0x7f, 0x00],
[0x7f, 0x04, 0x08, 0x10, 0x7f, 0x00],
[0x3e, 0x41, 0x41, 0x41, 0x3e, 0x00],
[0x7f, 0x09, 0x09, 0x09, 0x06, 0x00],
[0x3e, 0x41, 0x51, 0x21, 0x5e, 0x00],
[0x7f, 0x09, 0x19, 0x29, 0x46, 0x00],
[0x46, 0x49, 0x49, 0x49, 0x31, 0x00],
[0x01, 0x01, 0x7f, 0x01, 0x01, 0x00],
[0x3f, 0x40, 0x40, 0x40, 0x3f, 0x00],
[0x1f, 0x20, 0x40, 0x20, 0x1f, 0x00],
[0x3f, 0x40, 0x38, 0x40, 0x3f, 0x00],
[0x63, 0x14, 0x08, 0x14, 0x63, 0x00],
[0x07, 0x08, 0x70, 0x08, 0x07, 0x00],
[0x61, 0x51, 0x49, 0x45, 0x43, 0x00],
[0x00, 0x7f, 0x41, 0x41, 0x00, 0x00],
[0x02, 0x04, 0x08, 0x10, 0x20, 0x00],
[0x00, 0x41, 0x41, 0x7f, 0x00, 0x00],
[0x04, 0x02, 0x01, 0x02, 0x04, 0x00],
[0x40, 0x40, 0x40, 0x40, 0x40, 0x00],
[0x00, 0x01, 0x02, 0x04, 0x00, 0x00],
[0x20, 0x54, 0x54, 0x54, 0x78, 0x00],
[0x7f, 0x48, 0x44, 0x44, 0x38, 0x00],
[0x38, 0x44, 0x44, 0x44, 0x20, 0x00],
[0x38, 0x44, 0x44, 0x48, 0x7f, 0x00],
[0x38, 0x54, 0x54, 0x54, 0x18, 0x00],
[0x08, 0x7e, 0x09, 0x01, 0x02, 0x00],
[0x08, 0x14, 0x54, 0x54, 0x3c, 0x00],
[0x7f, 0x08, 0x04, 0x04, 0x78, 0x00],
[0x00, 0x44, 0x7d, 0x40, 0x00, 0x00],
[0x20, 0x40, 0x44, 0x3d, 0x00, 0x00],
[0x7f, 0x10, 0x28, 0x44, 0x00, 0x00],
[0x00, 0x41, 0x7f, 0x40, 0x00, 0x00],
[0x7c, 0x04, 0x18, 0x04, 0x78, 0x00],
[0x7c, 0x08, 0x04, 0x04, 0x78, 0x00],
[0x38, 0x44, 0x44, 0x44, 0x38, 0x00],
[0x7c, 0x14, 0x14, 0x14, 0x08, 0x00],
[0x08, 0x14, 0x14, 0x18, 0x7c, 0x00],
[0x7c, 0x08, 0x04, 0x04, 0x08, 0x00],
[0x48, 0x54, 0x54, 0x54, 0x20, 0x00],
[0x04, 0x3f, 0x44, 0x40, 0x20, 0x00],
[0x3c, 0x40, 0x40, 0x20, 0x7c, 0x00],
[0x1c, 0x20, 0x40, 0x20, 0x1c, 0x00],
[0x3c, 0x40, 0x30, 0x40, 0x3c, 0x00],
[0x44, 0x28, 0x10, 0x28, 0x44, 0x00],
[0x0c, 0x50, 0x50, 0x50, 0x3c, 0x00],
[0x44, 0x64, 0x54, 0x4c, 0x44, 0x00],
[0x00, 0x08, 0x36, 0x41, 0x00, 0x00],
[0x00, 0x00, 0x7f, 0x00, 0x00, 0x00],
[0x00, 0x41, 0x36, 0x08, 0x00, 0x00],
[0x08, 0x04, 0x08, 0x10, 0x08, 0x00]
]
/**
* Font atlas builder and char lookup.
* @description Gaussian-blurred supersampled glyph atlas.
*/
export default class Font {
/** Source glyph width in pixels. */
static readonly charWidth = fontWidth
/** Source glyph height in pixels. */
static readonly charHeight = fontHeight
/** Total number of supported characters. */
static readonly charCount = lastChar - firstChar + 1
/**
* Builds font atlas texture with blur.
* @description Supersampled blurred RGBA atlas.
* @returns RGBA pixel data, atlas width and height
*/
static buildAtlas(): { data: Uint8Array; width: number; height: number } {
const scaledCharW = fontWidth * atlasScale
const scaledCharH = fontHeight * atlasScale
const atlasWidth = Font.charCount * scaledCharW
const atlasHeight = scaledCharH
const rawBitmap = new Uint8Array(Font.charCount * fontWidth * fontHeight)
for (let charIdx = 0; charIdx < Font.charCount; charIdx++) {
const columnBits = glyphData[charIdx] ?? glyphData[0] ?? []
for (let colIdx = 0; colIdx < fontWidth; colIdx++) {
const colBitMask = columnBits[colIdx] ?? 0
for (let rowIdx = 0; rowIdx < fontHeight; rowIdx++) {
rawBitmap[charIdx * fontWidth * fontHeight + colIdx * fontHeight + rowIdx] =
(colBitMask >> rowIdx) & 1
}
}
}
const atlasData = new Uint8Array(atlasWidth * atlasHeight * 4)
const gaussianKernel = [0.0625, 0.125, 0.0625, 0.125, 0.25, 0.125, 0.0625, 0.125, 0.0625]
for (let charIdx = 0; charIdx < Font.charCount; charIdx++) {
for (let scaledRow = 0; scaledRow < scaledCharH; scaledRow++) {
for (let scaledCol = 0; scaledCol < scaledCharW; scaledCol++) {
const sourceCol = scaledCol / atlasScale
const sourceRow = scaledRow / atlasScale
let accumWeight = 0
for (let kernelY = -1; kernelY <= 1; kernelY++) {
for (let kernelX = -1; kernelX <= 1; kernelX++) {
const sampleColIdx = Math.min(
fontWidth - 1,
Math.max(0, Math.floor(sourceCol + kernelX * 0.5))
)
const sampleRowIdx = Math.min(
fontHeight - 1,
Math.max(0, Math.floor(sourceRow + kernelY * 0.5))
)
const rawBitValue = rawBitmap[
charIdx * fontWidth * fontHeight + sampleColIdx * fontHeight + sampleRowIdx
] ?? 0
const kernelWeightValue = gaussianKernel[(kernelY + 1) * 3 + (kernelX + 1)] ?? 0
accumWeight += rawBitValue * kernelWeightValue
}
}
const pixelBrightness = Math.round(Math.min(1, accumWeight * 1.4) * 255)
const atlasWriteX = charIdx * scaledCharW + scaledCol
const atlasWriteIdx = (scaledRow * atlasWidth + atlasWriteX) * 4
atlasData[atlasWriteIdx + 0] = pixelBrightness
atlasData[atlasWriteIdx + 1] = pixelBrightness
atlasData[atlasWriteIdx + 2] = pixelBrightness
atlasData[atlasWriteIdx + 3] = pixelBrightness
}
}
}
return { data: atlasData, width: atlasWidth, height: atlasHeight }
}
/**
* Returns atlas index for character.
* @description Zero-based index; fallback 0.
* @param char - Single character to look up
* @returns Zero-based atlas index
*/
static charIndex(char: string): number {
const charCodeValue = char.charCodeAt(0)
if (charCodeValue < firstChar || charCodeValue > lastChar) {
return 0
}
return charCodeValue - firstChar
}
}