embedded-react 0.20.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +5 -0
- package/README.md +3 -2
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +17 -47
- package/aot/compiler/alerts.mjs +604 -0
- package/aot/compiler/animations.mjs +299 -57
- package/aot/compiler/boot.mjs +27 -2
- package/aot/compiler/check.mjs +62 -10
- package/aot/compiler/collect.mjs +517 -137
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +276 -117
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +2 -1
- package/aot/compiler/elements/props.mjs +47 -8
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/expressions.mjs +1378 -97
- package/aot/compiler/handlers.mjs +1807 -354
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/nodes.mjs +40 -1
- package/aot/compiler/ir/reads.mjs +206 -5
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +12 -0
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/keyboard-dismiss.mjs +99 -0
- package/aot/compiler/keyboard.mjs +1 -1
- package/aot/compiler/list-items.mjs +226 -0
- package/aot/compiler/list-order.mjs +586 -0
- package/aot/compiler/list-split.mjs +259 -0
- package/aot/compiler/loops.mjs +582 -0
- package/aot/compiler/lower.mjs +203 -53
- package/aot/compiler/native-files.mjs +439 -0
- package/aot/compiler/native-header.mjs +335 -0
- package/aot/compiler/native-modules.mjs +1173 -0
- package/aot/compiler/nodes.mjs +45 -31
- package/aot/compiler/options.mjs +3 -2
- package/aot/compiler/out.mjs +18 -1
- package/aot/compiler/package/animations.mjs +133 -23
- package/aot/compiler/package/assemble.mjs +2 -4
- package/aot/compiler/package/bytecode.mjs +37 -8
- package/aot/compiler/package/checks.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +906 -33
- package/aot/compiler/package/enums.mjs +10 -0
- package/aot/compiler/package/format.mjs +16 -3
- package/aot/compiler/package/lower.mjs +707 -176
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +24 -0
- package/aot/compiler/package/writer.mjs +22 -0
- package/aot/compiler/parse.mjs +3 -1
- package/aot/compiler/record-shapes.mjs +246 -0
- package/aot/compiler/records.mjs +654 -0
- package/aot/compiler/reducers.mjs +272 -0
- package/aot/compiler/scroll-to.mjs +112 -0
- package/aot/compiler/static-eval.mjs +315 -7
- package/aot/compiler/style-text.mjs +20 -5
- package/aot/compiler/svg.mjs +13 -7
- package/aot/compiler/switch-cases.mjs +198 -0
- package/aot/compiler/tagged-records.mjs +450 -0
- package/aot/compiler/typing.mjs +307 -0
- package/aot/compiler/updaters.mjs +321 -0
- package/aot/compiler/window-dimensions.mjs +94 -0
- package/aot/compiler/worked-values.mjs +319 -0
- package/aot/demo-file.mjs +30 -0
- package/aot/style-map.mjs +94 -3
- package/assets/bake-font.mjs +187 -244
- package/assets/bake-image.mjs +47 -71
- package/assets/bake-svg.mjs +465 -533
- package/assets/build-builtin-font.mjs +14 -27
- package/assets/emit-c.mjs +124 -204
- package/assets/emit-container.mjs +129 -197
- package/assets/emit-pack.mjs +92 -136
- package/assets/font-config.mjs +19 -27
- package/assets/font-sizes.mjs +432 -507
- package/assets/glyph-coverage.mjs +170 -214
- package/assets/index.mjs +52 -97
- package/assets/rasterize.mjs +173 -194
- package/assets/svg-loader.mjs +42 -48
- package/build.mjs +85 -94
- package/cli.mjs +490 -586
- package/compile-bin.mjs +24 -36
- package/flash.mjs +297 -401
- package/hotreload/dev-device.mjs +229 -270
- package/hotreload/pack-app.mjs +118 -159
- package/hotreload/pack-split.mjs +146 -213
- package/hotreload/package-transfer.mjs +128 -131
- package/hotreload/protocol.mjs +118 -118
- package/hotreload/uploader.mjs +132 -159
- package/hotreload/watch-tree.mjs +56 -65
- package/pack-container.mjs +94 -116
- package/package.json +19 -16
- package/persist-transform.mjs +89 -105
- package/qjsc-wasm.mjs +47 -72
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +1 -1
- package/sim-server.mjs +260 -354
- package/src/embedded-react/Alert.mjs +112 -0
- package/src/embedded-react/Animated.mjs +412 -0
- package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
- package/src/embedded-react/{Button.js → Button.mjs} +29 -53
- package/src/embedded-react/Dimensions.mjs +32 -0
- package/src/embedded-react/Easing.mjs +34 -0
- package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
- package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
- package/src/embedded-react/Keyboard.mjs +82 -0
- package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
- package/src/embedded-react/PanResponder.mjs +256 -0
- package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
- package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
- package/src/embedded-react/SectionList.mjs +103 -0
- package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
- package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
- package/src/embedded-react/background-image.mjs +321 -0
- package/src/embedded-react/{components.js → components.mjs} +1 -3
- package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
- package/src/embedded-react/index.d.ts +238 -5
- package/src/embedded-react/index.js +26 -17
- package/src/embedded-react/layout-anim-config.mjs +85 -0
- package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
- package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
- package/src/embedded-react/nativeModule.mjs +371 -0
- package/src/embedded-react/split-style.mjs +57 -0
- package/src/embedded-react/svg-ops.mjs +1075 -0
- package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
- package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
- package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
- package/src/host-config.mjs +512 -0
- package/src/{native-ui.js → native-ui.mjs} +1 -8
- package/src/props.mjs +341 -0
- package/src/{renderer.js → renderer.mjs} +30 -44
- package/src/embedded-react/Animated.js +0 -440
- package/src/embedded-react/Easing.js +0 -39
- package/src/embedded-react/PanResponder.js +0 -330
- package/src/embedded-react/SectionList.js +0 -148
- package/src/embedded-react/layout-anim-config.js +0 -101
- package/src/embedded-react/split-style.js +0 -58
- package/src/embedded-react/svg-ops.js +0 -1295
- package/src/host-config.js +0 -548
- package/src/props.js +0 -306
package/assets/bake-font.mjs
CHANGED
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
16
|
// Build-time font baker: TrueType/OpenType → the engine's BitmapFont glyph data, at the exact pixel
|
|
18
17
|
// sizes the app uses. Pure JS (opentype.js + the local rasterizer) — no native deps, no Python.
|
|
19
18
|
//
|
|
@@ -26,12 +25,10 @@
|
|
|
26
25
|
// bpp 8 → 1 byte/px, raw coverage
|
|
27
26
|
import fs from 'node:fs';
|
|
28
27
|
import opentype from 'opentype.js';
|
|
29
|
-
import {rasterize} from './rasterize.mjs';
|
|
30
|
-
|
|
28
|
+
import { rasterize } from './rasterize.mjs';
|
|
31
29
|
/** Default dense glyph range: printable ASCII (0x20..0x7E). */
|
|
32
30
|
export const ASCII_FIRST = 0x20;
|
|
33
31
|
export const ASCII_LAST = 0x7e;
|
|
34
|
-
|
|
35
32
|
const SPACE_CODEPOINT = 0x20;
|
|
36
33
|
const MAX_CODEPOINT = 0x10ffff;
|
|
37
34
|
const SUPPORTED_BPP = [1, 2, 4, 8];
|
|
@@ -44,309 +41,255 @@ const SUPERSAMPLE = 4;
|
|
|
44
41
|
const SUPERSAMPLE_8BPP = 8;
|
|
45
42
|
const MIN_CURVE_STEPS = 6;
|
|
46
43
|
const PIXELS_PER_CURVE_STEP = 3;
|
|
47
|
-
|
|
48
44
|
// Named extra-symbol sets selectable from assets.config.js (glyphs: 'common' | 'minimal' | ...).
|
|
49
45
|
const SYMBOLS_MINIMAL = [
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
0x00b0, 0x00b1, 0x00b5, 0x00d7, 0x00f7, 0x2013, 0x2014, 0x2022, 0x2026,
|
|
47
|
+
0x2190, 0x2191, 0x2192, 0x2193, 0x2713, 0x2717,
|
|
52
48
|
];
|
|
53
49
|
const SYMBOLS_COMMON = [
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
50
|
+
0x00a2, 0x00a3, 0x00a5, 0x00a7, 0x00a9, 0x00ae, 0x00b0, 0x00b1, 0x00b5,
|
|
51
|
+
0x00d7, 0x00f7, 0x2013, 0x2014, 0x2018, 0x2019, 0x201c, 0x201d, 0x2020,
|
|
52
|
+
0x2021, 0x2022, 0x2026, 0x2030, 0x20ac, 0x2122, 0x2190, 0x2191, 0x2192,
|
|
53
|
+
0x2193, 0x2194, 0x21b5, 0x2202, 0x2206, 0x221a, 0x221e, 0x2211, 0x2212,
|
|
54
|
+
0x2248, 0x2260, 0x2264, 0x2265, 0x25a0, 0x25cf, 0x25c6, 0x2605, 0x2606,
|
|
55
|
+
0x2713, 0x2717,
|
|
60
56
|
];
|
|
61
57
|
const SYMBOLS_GREEK = [];
|
|
62
58
|
for (let codepoint = 0x0391; codepoint <= 0x03a9; codepoint++)
|
|
63
|
-
|
|
59
|
+
if (codepoint !== 0x03a2)
|
|
60
|
+
SYMBOLS_GREEK.push(codepoint); // uppercase (skip gap)
|
|
64
61
|
for (let codepoint = 0x03b1; codepoint <= 0x03c9; codepoint++)
|
|
65
|
-
|
|
62
|
+
SYMBOLS_GREEK.push(codepoint); // lowercase
|
|
66
63
|
const SYMBOL_SETS = {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
(left, right) => left - right,
|
|
73
|
-
),
|
|
64
|
+
none: [],
|
|
65
|
+
minimal: SYMBOLS_MINIMAL,
|
|
66
|
+
common: SYMBOLS_COMMON,
|
|
67
|
+
greek: SYMBOLS_GREEK,
|
|
68
|
+
'common-greek': [...new Set([...SYMBOLS_COMMON, ...SYMBOLS_GREEK])].sort((left, right) => left - right),
|
|
74
69
|
};
|
|
75
|
-
|
|
76
70
|
// The engine's built-in font (engine/font/font_data.c) covers printable ASCII plus these. It is its
|
|
77
|
-
// own list, not an alias of a named set: build-builtin-font.
|
|
71
|
+
// own list, not an alias of a named set: build-builtin-font.mts bakes from it and the coverage check
|
|
78
72
|
// reports against it, so the two can never drift — while the named sets stay free to evolve.
|
|
79
73
|
// Changing it changes the engine's fallback font; re-run `npm run build:builtin-font` if you do.
|
|
80
74
|
export const BUILTIN_EXTRAS = Object.freeze([
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
75
|
+
0x00a2, 0x00a3, 0x00a5, 0x00a7, 0x00a9, 0x00ae, 0x00b0, 0x00b1, 0x00b5,
|
|
76
|
+
0x00d7, 0x00f7, 0x2013, 0x2014, 0x2018, 0x2019, 0x201c, 0x201d, 0x2020,
|
|
77
|
+
0x2021, 0x2022, 0x2026, 0x2030, 0x20ac, 0x2122, 0x2190, 0x2191, 0x2192,
|
|
78
|
+
0x2193, 0x2194, 0x21b5, 0x2202, 0x2206, 0x2211, 0x2212, 0x221a, 0x221e,
|
|
79
|
+
0x2248, 0x2260, 0x2264, 0x2265, 0x25a0, 0x25c6, 0x25cf, 0x2605, 0x2606,
|
|
80
|
+
0x2713, 0x2717,
|
|
87
81
|
]);
|
|
88
|
-
|
|
89
82
|
/**
|
|
90
83
|
* Pixel sizes the engine's built-in font is baked at (engine/font/font_data.c). Like BUILTIN_EXTRAS
|
|
91
|
-
* this is the single source of truth: build-builtin-font.
|
|
84
|
+
* this is the single source of truth: build-builtin-font.mts bakes from it and the build-time size
|
|
92
85
|
* check reports against it, so an app rendering at some other size is told what it will snap to.
|
|
93
86
|
* Changing it changes the engine's fallback font; re-run `npm run build:builtin-font` if you do.
|
|
94
87
|
*/
|
|
95
88
|
export const BUILTIN_SIZES = Object.freeze([10, 12, 16, 20, 24, 32, 48]);
|
|
96
|
-
|
|
97
89
|
/**
|
|
98
90
|
* Normalizes one codepoint list: numbers are codepoints, a string contributes every character in
|
|
99
91
|
* it — so `'°±'` and `[0x00b0, 0x00b1]` mean the same thing.
|
|
100
92
|
*
|
|
101
|
-
* @param
|
|
102
|
-
* @
|
|
103
|
-
* @returns {number[]} Codepoints, unsorted and possibly duplicated.
|
|
93
|
+
* @param field Config field name, for error messages.
|
|
94
|
+
* @returns Codepoints, unsorted and possibly duplicated.
|
|
104
95
|
*/
|
|
105
96
|
function toCodepoints(spec, field) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
97
|
+
if (spec === undefined || spec === null)
|
|
98
|
+
return [];
|
|
99
|
+
const codepoints = [];
|
|
100
|
+
for (const item of Array.isArray(spec) ? spec : [spec]) {
|
|
101
|
+
if (typeof item === 'string') {
|
|
102
|
+
for (const character of item)
|
|
103
|
+
codepoints.push(character.codePointAt(0));
|
|
104
|
+
}
|
|
105
|
+
else if (typeof item === 'number' &&
|
|
106
|
+
Number.isInteger(item) &&
|
|
107
|
+
item >= 0 &&
|
|
108
|
+
item <= MAX_CODEPOINT) {
|
|
109
|
+
codepoints.push(item);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
throw new Error(`${field}: expected a codepoint number or a string of characters, got ${JSON.stringify(item)}`);
|
|
113
|
+
}
|
|
122
114
|
}
|
|
123
|
-
|
|
124
|
-
return codepoints;
|
|
115
|
+
return codepoints;
|
|
125
116
|
}
|
|
126
|
-
|
|
127
117
|
/**
|
|
128
118
|
* Resolves a glyph-coverage spec into a sorted list of extra (sparse) codepoints.
|
|
129
119
|
*
|
|
130
120
|
* `glyphs` picks the baseline: 'ascii' (none), a named set, or an explicit list. `extraGlyphs` adds
|
|
131
121
|
* the per-app characters no named set can cover, as codepoints or as the characters themselves.
|
|
132
122
|
*
|
|
133
|
-
* @param
|
|
134
|
-
* @param
|
|
135
|
-
* @returns
|
|
123
|
+
* @param glyphs 'ascii', a named set, or explicit codepoints.
|
|
124
|
+
* @param extraGlyphs Per-app additions, on top of `glyphs`.
|
|
125
|
+
* @returns Sorted extra codepoints outside the dense ASCII range.
|
|
136
126
|
*/
|
|
137
127
|
export function resolveExtras(glyphs, extraGlyphs) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
)
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
return [...new Set(codepoints)]
|
|
157
|
-
.filter(codepoint => codepoint < ASCII_FIRST || codepoint > ASCII_LAST)
|
|
158
|
-
.sort((left, right) => left - right);
|
|
128
|
+
let codepoints;
|
|
129
|
+
if (!glyphs || glyphs === 'ascii')
|
|
130
|
+
codepoints = [];
|
|
131
|
+
else if (typeof glyphs === 'string') {
|
|
132
|
+
if (!SYMBOL_SETS[glyphs])
|
|
133
|
+
throw new Error(`unknown glyph set "${glyphs}" (use 'ascii', one of [${Object.keys(SYMBOL_SETS).join(', ')}], or a list of codepoints/characters)`);
|
|
134
|
+
codepoints = SYMBOL_SETS[glyphs].slice();
|
|
135
|
+
}
|
|
136
|
+
else
|
|
137
|
+
codepoints = toCodepoints(glyphs, 'glyphs');
|
|
138
|
+
// A bare set name here is almost certainly meant for `glyphs` — as characters it would be a
|
|
139
|
+
// silent no-op (all ASCII), so say so instead of baking nothing.
|
|
140
|
+
if (typeof extraGlyphs === 'string' && SYMBOL_SETS[extraGlyphs])
|
|
141
|
+
throw new Error(`extraGlyphs: "${extraGlyphs}" is a named glyph set — put it in \`glyphs\` (extraGlyphs takes the characters themselves)`);
|
|
142
|
+
codepoints.push(...toCodepoints(extraGlyphs, 'extraGlyphs'));
|
|
143
|
+
return [...new Set(codepoints)]
|
|
144
|
+
.filter(codepoint => codepoint < ASCII_FIRST || codepoint > ASCII_LAST)
|
|
145
|
+
.sort((left, right) => left - right);
|
|
159
146
|
}
|
|
160
|
-
|
|
161
147
|
/**
|
|
162
148
|
* Opens a font file and returns a predicate for "does this file have a glyph for this codepoint?".
|
|
163
149
|
* The coverage check uses it to tell a missing bake ("add it to extraGlyphs") apart from a font that
|
|
164
150
|
* simply cannot draw the character ("pick a different font").
|
|
165
151
|
*
|
|
166
|
-
* @param
|
|
167
|
-
* @returns
|
|
152
|
+
* @param path Path to the .ttf/.otf file.
|
|
153
|
+
* @returns Always true if the file can't be read, so a probe failure never
|
|
168
154
|
* turns into a misleading claim about the font.
|
|
169
155
|
*/
|
|
170
156
|
export function loadGlyphProbe(path) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
157
|
+
try {
|
|
158
|
+
const font = opentype.parse(fs.readFileSync(path).buffer);
|
|
159
|
+
return codepoint => font.charToGlyphIndex(String.fromCodePoint(codepoint)) !== 0;
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return () => true;
|
|
163
|
+
}
|
|
178
164
|
}
|
|
179
|
-
|
|
180
165
|
/**
|
|
181
166
|
* Packs an 8-bit coverage bitmap (one byte per pixel) into the engine's per-row bpp layout.
|
|
182
167
|
*
|
|
183
|
-
* @param
|
|
184
|
-
* @param
|
|
185
|
-
* @param
|
|
186
|
-
* @param
|
|
187
|
-
* @returns
|
|
168
|
+
* @param coverage Row-major coverage, width*height bytes.
|
|
169
|
+
* @param width Glyph width in pixels.
|
|
170
|
+
* @param height Glyph height in pixels.
|
|
171
|
+
* @param bpp Bits per pixel (1, 2, 4, or 8).
|
|
172
|
+
* @returns Packed bytes (row-major, each row padded to a whole byte).
|
|
188
173
|
*/
|
|
189
174
|
function packCoverage(coverage, width, height, bpp) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
175
|
+
const packed = [];
|
|
176
|
+
if (bpp === BITS_PER_BYTE) {
|
|
177
|
+
for (let pixel = 0; pixel < width * height; pixel++)
|
|
178
|
+
packed.push(coverage[pixel]);
|
|
179
|
+
return packed;
|
|
180
|
+
}
|
|
181
|
+
const maxLevel = (1 << bpp) - 1;
|
|
182
|
+
const pixelsPerByte = BITS_PER_BYTE / bpp;
|
|
183
|
+
const rowBytes = Math.ceil(width / pixelsPerByte);
|
|
184
|
+
for (let row = 0; row < height; row++) {
|
|
185
|
+
for (let byteIndex = 0; byteIndex < rowBytes; byteIndex++) {
|
|
186
|
+
let byte = 0;
|
|
187
|
+
for (let slot = 0; slot < pixelsPerByte; slot++) {
|
|
188
|
+
const column = byteIndex * pixelsPerByte + slot;
|
|
189
|
+
if (column >= width)
|
|
190
|
+
continue;
|
|
191
|
+
const sample = coverage[row * width + column];
|
|
192
|
+
const level = bpp === 1
|
|
193
|
+
? sample >= COVERAGE_THRESHOLD_1BPP
|
|
194
|
+
? 1
|
|
195
|
+
: 0
|
|
196
|
+
: Math.round((sample * maxLevel) / COVERAGE_MAX);
|
|
197
|
+
byte |= level << (BITS_PER_BYTE - bpp - slot * bpp); // first pixel in the high bits
|
|
198
|
+
}
|
|
199
|
+
packed.push(byte);
|
|
200
|
+
}
|
|
215
201
|
}
|
|
216
|
-
|
|
217
|
-
return packed;
|
|
202
|
+
return packed;
|
|
218
203
|
}
|
|
219
|
-
|
|
220
204
|
/**
|
|
221
205
|
* Rasterizes a single codepoint into a glyph record + appended bitmap bytes.
|
|
222
206
|
*
|
|
223
|
-
* @returns
|
|
207
|
+
* @returns null when the font has no glyph for the codepoint.
|
|
224
208
|
*/
|
|
225
209
|
function bakeGlyph(font, codepoint, pixelSize, baseline, scale, bpp) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
xOffset: raster.xOffset,
|
|
253
|
-
yOffset: raster.yOffset,
|
|
254
|
-
advance: Math.min(advance, UINT8_MAX),
|
|
255
|
-
};
|
|
256
|
-
const bytes =
|
|
257
|
-
raster.width > 0 && raster.height > 0
|
|
258
|
-
? packCoverage(raster.coverage, raster.width, raster.height, bpp)
|
|
259
|
-
: [];
|
|
260
|
-
return {glyph: glyphRecord, bytes};
|
|
210
|
+
if (font.charToGlyphIndex(String.fromCodePoint(codepoint)) === 0 &&
|
|
211
|
+
codepoint !== SPACE_CODEPOINT)
|
|
212
|
+
return null;
|
|
213
|
+
const glyph = font.charToGlyph(String.fromCodePoint(codepoint));
|
|
214
|
+
const advance = Math.round(glyph.advanceWidth * scale);
|
|
215
|
+
const supersample = bpp === BITS_PER_BYTE ? SUPERSAMPLE_8BPP : SUPERSAMPLE;
|
|
216
|
+
const steps = Math.max(MIN_CURVE_STEPS, Math.ceil(pixelSize / PIXELS_PER_CURVE_STEP));
|
|
217
|
+
const raster = rasterize(glyph.getPath(0, baseline, pixelSize), {
|
|
218
|
+
ss: supersample,
|
|
219
|
+
steps,
|
|
220
|
+
});
|
|
221
|
+
if (raster.width > UINT8_MAX || raster.height > UINT8_MAX) {
|
|
222
|
+
throw new Error(`glyph U+${codepoint.toString(16)} is ${raster.width}x${raster.height}px — exceeds the uint8 glyph limit`);
|
|
223
|
+
}
|
|
224
|
+
const glyphRecord = {
|
|
225
|
+
bitmapOffset: 0, // filled in by the caller once the running offset is known
|
|
226
|
+
width: raster.width,
|
|
227
|
+
height: raster.height,
|
|
228
|
+
xOffset: raster.xOffset,
|
|
229
|
+
yOffset: raster.yOffset,
|
|
230
|
+
advance: Math.min(advance, UINT8_MAX),
|
|
231
|
+
};
|
|
232
|
+
const bytes = raster.width > 0 && raster.height > 0
|
|
233
|
+
? packCoverage(raster.coverage, raster.width, raster.height, bpp)
|
|
234
|
+
: [];
|
|
235
|
+
return { glyph: glyphRecord, bytes };
|
|
261
236
|
}
|
|
262
|
-
|
|
263
237
|
/**
|
|
264
238
|
* Bakes one font into per-size BitmapFont data.
|
|
265
239
|
*
|
|
266
|
-
* @
|
|
267
|
-
*
|
|
268
|
-
* @param {string} opts.family Family name to register under (used by fontFamily lookups).
|
|
269
|
-
* @param {number[]} opts.sizes Pixel sizes to bake.
|
|
270
|
-
* @param {number} [opts.bpp] Bits per pixel (default 4).
|
|
271
|
-
* @param {string|Array<number|string>} [opts.glyphs] Extra glyph coverage beyond ASCII (default 'ascii').
|
|
272
|
-
* @param {number|string|Array<number|string>} [opts.extraGlyphs] Per-app additions on top of `glyphs`.
|
|
273
|
-
* @returns {{family:string, path:string, sizes:Array<object>}} Baked data ready for the C emitter.
|
|
274
|
-
* Extras the font file has no glyph for are silently absent from each size's `extras`.
|
|
240
|
+
* @returns Baked data ready for the emitters. Extras the font file has no glyph for are silently absent
|
|
241
|
+
* from each size's `extras`.
|
|
275
242
|
*/
|
|
276
|
-
export function bakeFont({
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
codepoint,
|
|
327
|
-
pixelSize,
|
|
328
|
-
baseline,
|
|
329
|
-
scale,
|
|
330
|
-
bpp,
|
|
331
|
-
);
|
|
332
|
-
if (!bakedGlyph) continue; // font has no such glyph
|
|
333
|
-
bakedGlyph.glyph.bitmapOffset =
|
|
334
|
-
bakedGlyph.bytes.length > 0 ? bitmap.length : 0;
|
|
335
|
-
bitmap.push(...bakedGlyph.bytes);
|
|
336
|
-
extras.push({codepoint, info: bakedGlyph.glyph});
|
|
243
|
+
export function bakeFont({ path, family, sizes, bpp = DEFAULT_BPP, glyphs = 'ascii', extraGlyphs, }) {
|
|
244
|
+
if (!SUPPORTED_BPP.includes(bpp))
|
|
245
|
+
throw new Error(`bpp must be 1, 2, 4, or 8 (got ${bpp})`);
|
|
246
|
+
const font = opentype.parse(fs.readFileSync(path).buffer);
|
|
247
|
+
const extraCodepoints = resolveExtras(glyphs, extraGlyphs);
|
|
248
|
+
const bakedSizes = [];
|
|
249
|
+
for (const pixelSize of sizes) {
|
|
250
|
+
const scale = pixelSize / font.unitsPerEm;
|
|
251
|
+
const baseline = Math.round(font.ascender * scale);
|
|
252
|
+
const lineHeight = baseline + Math.round(-font.descender * scale);
|
|
253
|
+
const bitmap = [];
|
|
254
|
+
const dense = [];
|
|
255
|
+
for (let codepoint = ASCII_FIRST; codepoint <= ASCII_LAST; codepoint++) {
|
|
256
|
+
const bakedGlyph = bakeGlyph(font, codepoint, pixelSize, baseline, scale, bpp) || {
|
|
257
|
+
glyph: {
|
|
258
|
+
bitmapOffset: 0,
|
|
259
|
+
width: 0,
|
|
260
|
+
height: 0,
|
|
261
|
+
xOffset: 0,
|
|
262
|
+
yOffset: 0,
|
|
263
|
+
advance: Math.round(pixelSize / 2),
|
|
264
|
+
},
|
|
265
|
+
bytes: [],
|
|
266
|
+
};
|
|
267
|
+
bakedGlyph.glyph.bitmapOffset =
|
|
268
|
+
bakedGlyph.bytes.length > 0 ? bitmap.length : 0;
|
|
269
|
+
bitmap.push(...bakedGlyph.bytes);
|
|
270
|
+
dense.push(bakedGlyph.glyph);
|
|
271
|
+
}
|
|
272
|
+
const extras = [];
|
|
273
|
+
for (const codepoint of extraCodepoints) {
|
|
274
|
+
const bakedGlyph = bakeGlyph(font, codepoint, pixelSize, baseline, scale, bpp);
|
|
275
|
+
if (!bakedGlyph)
|
|
276
|
+
continue; // font has no such glyph
|
|
277
|
+
bakedGlyph.glyph.bitmapOffset =
|
|
278
|
+
bakedGlyph.bytes.length > 0 ? bitmap.length : 0;
|
|
279
|
+
bitmap.push(...bakedGlyph.bytes);
|
|
280
|
+
extras.push({ codepoint, info: bakedGlyph.glyph });
|
|
281
|
+
}
|
|
282
|
+
bakedSizes.push({
|
|
283
|
+
pixelSize,
|
|
284
|
+
lineHeight: Math.min(lineHeight, UINT8_MAX),
|
|
285
|
+
baseline: Math.min(baseline, UINT8_MAX),
|
|
286
|
+
first: ASCII_FIRST,
|
|
287
|
+
last: ASCII_LAST,
|
|
288
|
+
bpp,
|
|
289
|
+
dense,
|
|
290
|
+
extras,
|
|
291
|
+
bitmap,
|
|
292
|
+
});
|
|
337
293
|
}
|
|
338
|
-
|
|
339
|
-
bakedSizes.push({
|
|
340
|
-
pixelSize,
|
|
341
|
-
lineHeight: Math.min(lineHeight, UINT8_MAX),
|
|
342
|
-
baseline: Math.min(baseline, UINT8_MAX),
|
|
343
|
-
first: ASCII_FIRST,
|
|
344
|
-
last: ASCII_LAST,
|
|
345
|
-
bpp,
|
|
346
|
-
dense,
|
|
347
|
-
extras,
|
|
348
|
-
bitmap,
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
return {family, path, sizes: bakedSizes};
|
|
294
|
+
return { family, path, sizes: bakedSizes };
|
|
352
295
|
}
|