embedded-react 0.21.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.
Files changed (99) hide show
  1. package/README.md +2 -2
  2. package/aot/compile.mjs +1 -1
  3. package/aot/compiler/alerts.mjs +604 -0
  4. package/aot/compiler/check.mjs +1 -1
  5. package/aot/compiler/collect.mjs +1 -1
  6. package/aot/compiler/elements/button.mjs +3 -3
  7. package/aot/compiler/elements/image-background.mjs +1 -1
  8. package/aot/compiler/elements/props.mjs +10 -6
  9. package/aot/compiler/elements/section-list.mjs +1 -1
  10. package/aot/compiler/expressions.mjs +29 -2
  11. package/aot/compiler/handlers.mjs +73 -16
  12. package/aot/compiler/keyboard-dismiss.mjs +99 -0
  13. package/aot/compiler/keyboard.mjs +1 -1
  14. package/aot/compiler/lower.mjs +12 -0
  15. package/aot/compiler/nodes.mjs +17 -9
  16. package/aot/compiler/out.mjs +4 -0
  17. package/aot/compiler/package/codegen.mjs +36 -2
  18. package/aot/compiler/package/format.mjs +5 -3
  19. package/aot/compiler/package/lower.mjs +53 -19
  20. package/aot/compiler/scroll-to.mjs +112 -0
  21. package/aot/compiler/style-text.mjs +2 -2
  22. package/aot/compiler/svg.mjs +6 -3
  23. package/aot/compiler/updaters.mjs +1 -0
  24. package/aot/style-map.mjs +44 -1
  25. package/assets/bake-font.mjs +187 -244
  26. package/assets/bake-image.mjs +47 -71
  27. package/assets/bake-svg.mjs +465 -533
  28. package/assets/build-builtin-font.mjs +14 -27
  29. package/assets/emit-c.mjs +124 -204
  30. package/assets/emit-container.mjs +129 -197
  31. package/assets/emit-pack.mjs +92 -136
  32. package/assets/font-config.mjs +19 -27
  33. package/assets/font-sizes.mjs +432 -507
  34. package/assets/glyph-coverage.mjs +170 -214
  35. package/assets/index.mjs +52 -97
  36. package/assets/rasterize.mjs +173 -194
  37. package/assets/svg-loader.mjs +42 -48
  38. package/build.mjs +84 -101
  39. package/cli.mjs +482 -621
  40. package/compile-bin.mjs +24 -36
  41. package/flash.mjs +297 -401
  42. package/hotreload/dev-device.mjs +229 -270
  43. package/hotreload/pack-app.mjs +118 -159
  44. package/hotreload/pack-split.mjs +144 -200
  45. package/hotreload/package-transfer.mjs +128 -131
  46. package/hotreload/protocol.mjs +118 -118
  47. package/hotreload/uploader.mjs +132 -159
  48. package/hotreload/watch-tree.mjs +56 -65
  49. package/pack-container.mjs +93 -123
  50. package/package.json +19 -16
  51. package/persist-transform.mjs +89 -105
  52. package/qjsc-wasm.mjs +47 -72
  53. package/sim/embedded-react.wasm +0 -0
  54. package/sim/index.html +1 -1
  55. package/sim-server.mjs +260 -354
  56. package/src/embedded-react/Alert.mjs +112 -0
  57. package/src/embedded-react/Animated.mjs +412 -0
  58. package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
  59. package/src/embedded-react/{Button.js → Button.mjs} +29 -53
  60. package/src/embedded-react/{Dimensions.js → Dimensions.mjs} +12 -17
  61. package/src/embedded-react/Easing.mjs +34 -0
  62. package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
  63. package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
  64. package/src/embedded-react/Keyboard.mjs +82 -0
  65. package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
  66. package/src/embedded-react/PanResponder.mjs +256 -0
  67. package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
  68. package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
  69. package/src/embedded-react/SectionList.mjs +103 -0
  70. package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
  71. package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
  72. package/src/embedded-react/background-image.mjs +321 -0
  73. package/src/embedded-react/{components.js → components.mjs} +1 -3
  74. package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
  75. package/src/embedded-react/index.d.ts +155 -2
  76. package/src/embedded-react/index.js +26 -19
  77. package/src/embedded-react/layout-anim-config.mjs +85 -0
  78. package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
  79. package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
  80. package/src/embedded-react/nativeModule.mjs +371 -0
  81. package/src/embedded-react/split-style.mjs +57 -0
  82. package/src/embedded-react/svg-ops.mjs +1075 -0
  83. package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
  84. package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
  85. package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
  86. package/src/host-config.mjs +512 -0
  87. package/src/{native-ui.js → native-ui.mjs} +1 -8
  88. package/src/props.mjs +341 -0
  89. package/src/{renderer.js → renderer.mjs} +30 -44
  90. package/src/embedded-react/Animated.js +0 -440
  91. package/src/embedded-react/Easing.js +0 -39
  92. package/src/embedded-react/PanResponder.js +0 -330
  93. package/src/embedded-react/SectionList.js +0 -146
  94. package/src/embedded-react/layout-anim-config.js +0 -101
  95. package/src/embedded-react/nativeModule.js +0 -462
  96. package/src/embedded-react/split-style.js +0 -58
  97. package/src/embedded-react/svg-ops.js +0 -1295
  98. package/src/host-config.js +0 -548
  99. package/src/props.js +0 -306
@@ -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
- 0x00b0, 0x00b1, 0x00b5, 0x00d7, 0x00f7, 0x2013, 0x2014, 0x2022, 0x2026,
51
- 0x2190, 0x2191, 0x2192, 0x2193, 0x2713, 0x2717,
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
- 0x00a2, 0x00a3, 0x00a5, 0x00a7, 0x00a9, 0x00ae, 0x00b0, 0x00b1, 0x00b5,
55
- 0x00d7, 0x00f7, 0x2013, 0x2014, 0x2018, 0x2019, 0x201c, 0x201d, 0x2020,
56
- 0x2021, 0x2022, 0x2026, 0x2030, 0x20ac, 0x2122, 0x2190, 0x2191, 0x2192,
57
- 0x2193, 0x2194, 0x21b5, 0x2202, 0x2206, 0x221a, 0x221e, 0x2211, 0x2212,
58
- 0x2248, 0x2260, 0x2264, 0x2265, 0x25a0, 0x25cf, 0x25c6, 0x2605, 0x2606,
59
- 0x2713, 0x2717,
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
- if (codepoint !== 0x03a2) SYMBOLS_GREEK.push(codepoint); // uppercase (skip gap)
59
+ if (codepoint !== 0x03a2)
60
+ SYMBOLS_GREEK.push(codepoint); // uppercase (skip gap)
64
61
  for (let codepoint = 0x03b1; codepoint <= 0x03c9; codepoint++)
65
- SYMBOLS_GREEK.push(codepoint); // lowercase
62
+ SYMBOLS_GREEK.push(codepoint); // lowercase
66
63
  const SYMBOL_SETS = {
67
- none: [],
68
- minimal: SYMBOLS_MINIMAL,
69
- common: SYMBOLS_COMMON,
70
- greek: SYMBOLS_GREEK,
71
- 'common-greek': [...new Set([...SYMBOLS_COMMON, ...SYMBOLS_GREEK])].sort(
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.mjs bakes from it and the coverage check
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
- 0x00a2, 0x00a3, 0x00a5, 0x00a7, 0x00a9, 0x00ae, 0x00b0, 0x00b1, 0x00b5,
82
- 0x00d7, 0x00f7, 0x2013, 0x2014, 0x2018, 0x2019, 0x201c, 0x201d, 0x2020,
83
- 0x2021, 0x2022, 0x2026, 0x2030, 0x20ac, 0x2122, 0x2190, 0x2191, 0x2192,
84
- 0x2193, 0x2194, 0x21b5, 0x2202, 0x2206, 0x2211, 0x2212, 0x221a, 0x221e,
85
- 0x2248, 0x2260, 0x2264, 0x2265, 0x25a0, 0x25c6, 0x25cf, 0x2605, 0x2606,
86
- 0x2713, 0x2717,
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.mjs bakes from it and the build-time size
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 {number|string|Array<number|string>|undefined} spec
102
- * @param {string} field Config field name, for error messages.
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
- if (spec === undefined || spec === null) return [];
107
- const codepoints = [];
108
- for (const item of Array.isArray(spec) ? spec : [spec]) {
109
- if (typeof item === 'string') {
110
- for (const character of item) codepoints.push(character.codePointAt(0));
111
- } else if (
112
- typeof item === 'number' &&
113
- Number.isInteger(item) &&
114
- item >= 0 &&
115
- item <= MAX_CODEPOINT
116
- ) {
117
- codepoints.push(item);
118
- } else {
119
- throw new Error(
120
- `${field}: expected a codepoint number or a string of characters, got ${JSON.stringify(item)}`,
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 {string|Array<number|string>|undefined} glyphs 'ascii', a named set, or explicit codepoints.
134
- * @param {number|string|Array<number|string>} [extraGlyphs] Per-app additions, on top of `glyphs`.
135
- * @returns {number[]} Sorted extra codepoints outside the dense ASCII range.
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
- let codepoints;
139
- if (!glyphs || glyphs === 'ascii') codepoints = [];
140
- else if (typeof glyphs === 'string') {
141
- if (!SYMBOL_SETS[glyphs])
142
- throw new Error(
143
- `unknown glyph set "${glyphs}" (use 'ascii', one of [${Object.keys(SYMBOL_SETS).join(', ')}], or a list of codepoints/characters)`,
144
- );
145
- codepoints = SYMBOL_SETS[glyphs].slice();
146
- } else codepoints = toCodepoints(glyphs, 'glyphs');
147
-
148
- // A bare set name here is almost certainly meant for `glyphs` — as characters it would be a
149
- // silent no-op (all ASCII), so say so instead of baking nothing.
150
- if (typeof extraGlyphs === 'string' && SYMBOL_SETS[extraGlyphs])
151
- throw new Error(
152
- `extraGlyphs: "${extraGlyphs}" is a named glyph set — put it in \`glyphs\` (extraGlyphs takes the characters themselves)`,
153
- );
154
- codepoints.push(...toCodepoints(extraGlyphs, 'extraGlyphs'));
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 {string} path Path to the .ttf/.otf file.
167
- * @returns {(codepoint:number)=>boolean} Always true if the file can't be read, so a probe failure never
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
- try {
172
- const font = opentype.parse(fs.readFileSync(path).buffer);
173
- return codepoint =>
174
- font.charToGlyphIndex(String.fromCodePoint(codepoint)) !== 0;
175
- } catch {
176
- return () => true;
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 {Uint8Array} coverage Row-major coverage, width*height bytes.
184
- * @param {number} width Glyph width in pixels.
185
- * @param {number} height Glyph height in pixels.
186
- * @param {number} bpp Bits per pixel (1, 2, 4, or 8).
187
- * @returns {number[]} Packed bytes (row-major, each row padded to a whole byte).
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
- const packed = [];
191
- if (bpp === BITS_PER_BYTE) {
192
- for (let pixel = 0; pixel < width * height; pixel++)
193
- packed.push(coverage[pixel]);
194
- return packed;
195
- }
196
- const maxLevel = (1 << bpp) - 1;
197
- const pixelsPerByte = BITS_PER_BYTE / bpp;
198
- const rowBytes = Math.ceil(width / pixelsPerByte);
199
- for (let row = 0; row < height; row++) {
200
- for (let byteIndex = 0; byteIndex < rowBytes; byteIndex++) {
201
- let byte = 0;
202
- for (let slot = 0; slot < pixelsPerByte; slot++) {
203
- const column = byteIndex * pixelsPerByte + slot;
204
- if (column >= width) continue;
205
- const sample = coverage[row * width + column];
206
- const level =
207
- bpp === 1
208
- ? sample >= COVERAGE_THRESHOLD_1BPP
209
- ? 1
210
- : 0
211
- : Math.round((sample * maxLevel) / COVERAGE_MAX);
212
- byte |= level << (BITS_PER_BYTE - bpp - slot * bpp); // first pixel in the high bits
213
- }
214
- packed.push(byte);
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 {{glyph:object, bytes:number[]}|null} null when the font has no glyph for the codepoint.
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
- if (
227
- font.charToGlyphIndex(String.fromCodePoint(codepoint)) === 0 &&
228
- codepoint !== SPACE_CODEPOINT
229
- )
230
- return null;
231
- const glyph = font.charToGlyph(String.fromCodePoint(codepoint));
232
- const advance = Math.round(glyph.advanceWidth * scale);
233
- const supersample = bpp === BITS_PER_BYTE ? SUPERSAMPLE_8BPP : SUPERSAMPLE;
234
- const steps = Math.max(
235
- MIN_CURVE_STEPS,
236
- Math.ceil(pixelSize / PIXELS_PER_CURVE_STEP),
237
- );
238
- const raster = rasterize(glyph.getPath(0, baseline, pixelSize), {
239
- ss: supersample,
240
- steps,
241
- });
242
-
243
- if (raster.width > UINT8_MAX || raster.height > UINT8_MAX) {
244
- throw new Error(
245
- `glyph U+${codepoint.toString(16)} is ${raster.width}x${raster.height}px — exceeds the uint8 glyph limit`,
246
- );
247
- }
248
- const glyphRecord = {
249
- bitmapOffset: 0, // filled in by the caller once the running offset is known
250
- width: raster.width,
251
- height: raster.height,
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
- * @param {object} opts
267
- * @param {string} opts.path Path to the .ttf/.otf file.
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
- path,
278
- family,
279
- sizes,
280
- bpp = DEFAULT_BPP,
281
- glyphs = 'ascii',
282
- extraGlyphs,
283
- }) {
284
- if (!SUPPORTED_BPP.includes(bpp))
285
- throw new Error(`bpp must be 1, 2, 4, or 8 (got ${bpp})`);
286
- const font = opentype.parse(fs.readFileSync(path).buffer);
287
- const extraCodepoints = resolveExtras(glyphs, extraGlyphs);
288
-
289
- const bakedSizes = [];
290
- for (const pixelSize of sizes) {
291
- const scale = pixelSize / font.unitsPerEm;
292
- const baseline = Math.round(font.ascender * scale);
293
- const lineHeight = baseline + Math.round(-font.descender * scale);
294
-
295
- const bitmap = [];
296
- const dense = [];
297
- for (let codepoint = ASCII_FIRST; codepoint <= ASCII_LAST; codepoint++) {
298
- const bakedGlyph = bakeGlyph(
299
- font,
300
- codepoint,
301
- pixelSize,
302
- baseline,
303
- scale,
304
- bpp,
305
- ) || {
306
- glyph: {
307
- bitmapOffset: 0,
308
- width: 0,
309
- height: 0,
310
- xOffset: 0,
311
- yOffset: 0,
312
- advance: Math.round(pixelSize / 2),
313
- },
314
- bytes: [],
315
- };
316
- bakedGlyph.glyph.bitmapOffset =
317
- bakedGlyph.bytes.length > 0 ? bitmap.length : 0;
318
- bitmap.push(...bakedGlyph.bytes);
319
- dense.push(bakedGlyph.glyph);
320
- }
321
-
322
- const extras = [];
323
- for (const codepoint of extraCodepoints) {
324
- const bakedGlyph = bakeGlyph(
325
- font,
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
  }