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.
Files changed (140) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +3 -2
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +17 -47
  5. package/aot/compiler/alerts.mjs +604 -0
  6. package/aot/compiler/animations.mjs +299 -57
  7. package/aot/compiler/boot.mjs +27 -2
  8. package/aot/compiler/check.mjs +62 -10
  9. package/aot/compiler/collect.mjs +517 -137
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +276 -117
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/elements/button.mjs +182 -0
  14. package/aot/compiler/elements/image-background.mjs +76 -0
  15. package/aot/compiler/elements/image.mjs +2 -1
  16. package/aot/compiler/elements/props.mjs +47 -8
  17. package/aot/compiler/elements/rewrite.mjs +118 -0
  18. package/aot/compiler/elements/section-list.mjs +370 -0
  19. package/aot/compiler/expressions.mjs +1378 -97
  20. package/aot/compiler/handlers.mjs +1807 -354
  21. package/aot/compiler/helper-values.mjs +312 -0
  22. package/aot/compiler/ir/nodes.mjs +40 -1
  23. package/aot/compiler/ir/reads.mjs +206 -5
  24. package/aot/compiler/ir/text.mjs +5 -1
  25. package/aot/compiler/ir/types.mjs +12 -0
  26. package/aot/compiler/kept-states.mjs +177 -0
  27. package/aot/compiler/keyboard-dismiss.mjs +99 -0
  28. package/aot/compiler/keyboard.mjs +1 -1
  29. package/aot/compiler/list-items.mjs +226 -0
  30. package/aot/compiler/list-order.mjs +586 -0
  31. package/aot/compiler/list-split.mjs +259 -0
  32. package/aot/compiler/loops.mjs +582 -0
  33. package/aot/compiler/lower.mjs +203 -53
  34. package/aot/compiler/native-files.mjs +439 -0
  35. package/aot/compiler/native-header.mjs +335 -0
  36. package/aot/compiler/native-modules.mjs +1173 -0
  37. package/aot/compiler/nodes.mjs +45 -31
  38. package/aot/compiler/options.mjs +3 -2
  39. package/aot/compiler/out.mjs +18 -1
  40. package/aot/compiler/package/animations.mjs +133 -23
  41. package/aot/compiler/package/assemble.mjs +2 -4
  42. package/aot/compiler/package/bytecode.mjs +37 -8
  43. package/aot/compiler/package/checks.mjs +4 -0
  44. package/aot/compiler/package/codegen.mjs +906 -33
  45. package/aot/compiler/package/enums.mjs +10 -0
  46. package/aot/compiler/package/format.mjs +16 -3
  47. package/aot/compiler/package/lower.mjs +707 -176
  48. package/aot/compiler/package/string-caps.mjs +515 -0
  49. package/aot/compiler/package/tables.mjs +24 -0
  50. package/aot/compiler/package/writer.mjs +22 -0
  51. package/aot/compiler/parse.mjs +3 -1
  52. package/aot/compiler/record-shapes.mjs +246 -0
  53. package/aot/compiler/records.mjs +654 -0
  54. package/aot/compiler/reducers.mjs +272 -0
  55. package/aot/compiler/scroll-to.mjs +112 -0
  56. package/aot/compiler/static-eval.mjs +315 -7
  57. package/aot/compiler/style-text.mjs +20 -5
  58. package/aot/compiler/svg.mjs +13 -7
  59. package/aot/compiler/switch-cases.mjs +198 -0
  60. package/aot/compiler/tagged-records.mjs +450 -0
  61. package/aot/compiler/typing.mjs +307 -0
  62. package/aot/compiler/updaters.mjs +321 -0
  63. package/aot/compiler/window-dimensions.mjs +94 -0
  64. package/aot/compiler/worked-values.mjs +319 -0
  65. package/aot/demo-file.mjs +30 -0
  66. package/aot/style-map.mjs +94 -3
  67. package/assets/bake-font.mjs +187 -244
  68. package/assets/bake-image.mjs +47 -71
  69. package/assets/bake-svg.mjs +465 -533
  70. package/assets/build-builtin-font.mjs +14 -27
  71. package/assets/emit-c.mjs +124 -204
  72. package/assets/emit-container.mjs +129 -197
  73. package/assets/emit-pack.mjs +92 -136
  74. package/assets/font-config.mjs +19 -27
  75. package/assets/font-sizes.mjs +432 -507
  76. package/assets/glyph-coverage.mjs +170 -214
  77. package/assets/index.mjs +52 -97
  78. package/assets/rasterize.mjs +173 -194
  79. package/assets/svg-loader.mjs +42 -48
  80. package/build.mjs +85 -94
  81. package/cli.mjs +490 -586
  82. package/compile-bin.mjs +24 -36
  83. package/flash.mjs +297 -401
  84. package/hotreload/dev-device.mjs +229 -270
  85. package/hotreload/pack-app.mjs +118 -159
  86. package/hotreload/pack-split.mjs +146 -213
  87. package/hotreload/package-transfer.mjs +128 -131
  88. package/hotreload/protocol.mjs +118 -118
  89. package/hotreload/uploader.mjs +132 -159
  90. package/hotreload/watch-tree.mjs +56 -65
  91. package/pack-container.mjs +94 -116
  92. package/package.json +19 -16
  93. package/persist-transform.mjs +89 -105
  94. package/qjsc-wasm.mjs +47 -72
  95. package/sim/embedded-react.wasm +0 -0
  96. package/sim/index.html +1 -1
  97. package/sim-server.mjs +260 -354
  98. package/src/embedded-react/Alert.mjs +112 -0
  99. package/src/embedded-react/Animated.mjs +412 -0
  100. package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
  101. package/src/embedded-react/{Button.js → Button.mjs} +29 -53
  102. package/src/embedded-react/Dimensions.mjs +32 -0
  103. package/src/embedded-react/Easing.mjs +34 -0
  104. package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
  105. package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
  106. package/src/embedded-react/Keyboard.mjs +82 -0
  107. package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
  108. package/src/embedded-react/PanResponder.mjs +256 -0
  109. package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
  110. package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
  111. package/src/embedded-react/SectionList.mjs +103 -0
  112. package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
  113. package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
  114. package/src/embedded-react/background-image.mjs +321 -0
  115. package/src/embedded-react/{components.js → components.mjs} +1 -3
  116. package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
  117. package/src/embedded-react/index.d.ts +238 -5
  118. package/src/embedded-react/index.js +26 -17
  119. package/src/embedded-react/layout-anim-config.mjs +85 -0
  120. package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
  121. package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
  122. package/src/embedded-react/nativeModule.mjs +371 -0
  123. package/src/embedded-react/split-style.mjs +57 -0
  124. package/src/embedded-react/svg-ops.mjs +1075 -0
  125. package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
  126. package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
  127. package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
  128. package/src/host-config.mjs +512 -0
  129. package/src/{native-ui.js → native-ui.mjs} +1 -8
  130. package/src/props.mjs +341 -0
  131. package/src/{renderer.js → renderer.mjs} +30 -44
  132. package/src/embedded-react/Animated.js +0 -440
  133. package/src/embedded-react/Easing.js +0 -39
  134. package/src/embedded-react/PanResponder.js +0 -330
  135. package/src/embedded-react/SectionList.js +0 -148
  136. package/src/embedded-react/layout-anim-config.js +0 -101
  137. package/src/embedded-react/split-style.js +0 -58
  138. package/src/embedded-react/svg-ops.js +0 -1295
  139. package/src/host-config.js +0 -548
  140. 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 glyph-coverage check: a character the app renders but the bake has no glyph for is
18
17
  // silently substituted with '?' by the engine (font_glyph() in engine/font/font_bitmap.c), so the
19
18
  // only way to find out used to be looking at the device. Every bake path (build, pack, sim, hot
@@ -22,150 +21,132 @@
22
21
  // The check is source-driven: it reads string and template literals (plus JSX element text, for the
23
22
  // unbundled `.jsx` the AOT flow compiles). Non-ASCII from a runtime source — a fetched string,
24
23
  // String.fromCharCode — can't be seen here. Comments are skipped: an em dash in prose is not text.
25
- import {
26
- ASCII_FIRST,
27
- ASCII_LAST,
28
- BUILTIN_EXTRAS,
29
- loadGlyphProbe,
30
- } from './bake-font.mjs';
31
-
24
+ import { ASCII_FIRST, ASCII_LAST, BUILTIN_EXTRAS, loadGlyphProbe, } from './bake-font.mjs';
32
25
  /** Name used for the engine's fallback font in warnings (it has no family of its own). */
33
26
  export const BUILTIN_FAMILY = 'the built-in font';
34
-
35
27
  const MAX_CODEPOINT = 0x10ffff;
36
28
  const CODEPOINT_HEX_DIGITS = 4;
37
29
  const EXCERPT_CONTEXT = 12; // characters shown either side of the one found
38
-
39
30
  // Comments first so an apostrophe inside one can't open a bogus string; then the three literal
40
31
  // forms. Bundled output is machine-generated, so the simple form is enough (a regex literal
41
32
  // containing a lone quote is the only realistic misread, and it costs at most a stray sample).
42
- const LITERALS =
43
- /\/\/[^\n]*|\/\*[\s\S]*?\*\/|'(?:[^'\\\n]|\\[\s\S])*'|"(?:[^"\\\n]|\\[\s\S])*"|`(?:[^`\\]|\\[\s\S])*`/g;
44
-
45
- const SIMPLE_ESCAPES = {n: '\n', t: '\t', r: '\r', b: '\b', f: '\f', v: '\v'};
46
-
33
+ const LITERALS = /\/\/[^\n]*|\/\*[\s\S]*?\*\/|'(?:[^'\\\n]|\\[\s\S])*'|"(?:[^"\\\n]|\\[\s\S])*"|`(?:[^`\\]|\\[\s\S])*`/g;
34
+ const SIMPLE_ESCAPES = {
35
+ n: '\n',
36
+ t: '\t',
37
+ r: '\r',
38
+ b: '\b',
39
+ f: '\f',
40
+ v: '\v',
41
+ };
47
42
  /** Resolves the \u{...}, \uXXXX, \xNN and simple escapes a bundler emits back to characters. */
48
43
  function decodeEscapes(body) {
49
- return body.replace(
50
- /\\u\{([0-9a-fA-F]{1,6})\}|\\u([0-9a-fA-F]{4})|\\x([0-9a-fA-F]{2})|\\([\s\S])/g,
51
- (_match, braced, unicode, hex, other) => {
52
- if (braced !== undefined) {
53
- const codepoint = parseInt(braced, 16);
54
- return codepoint <= MAX_CODEPOINT
55
- ? String.fromCodePoint(codepoint)
56
- : '';
57
- }
58
- if (unicode !== undefined)
59
- return String.fromCharCode(parseInt(unicode, 16));
60
- if (hex !== undefined) return String.fromCharCode(parseInt(hex, 16));
61
- return SIMPLE_ESCAPES[other] ?? other; // \\ \' \" \` and friends: the character itself
62
- },
63
- );
44
+ return body.replace(/\\u\{([0-9a-fA-F]{1,6})\}|\\u([0-9a-fA-F]{4})|\\x([0-9a-fA-F]{2})|\\([\s\S])/g, (_match, braced, unicode, hex, other) => {
45
+ if (braced !== undefined) {
46
+ const codepoint = parseInt(braced, 16);
47
+ return codepoint <= MAX_CODEPOINT
48
+ ? String.fromCodePoint(codepoint)
49
+ : '';
50
+ }
51
+ if (unicode !== undefined)
52
+ return String.fromCharCode(parseInt(unicode, 16));
53
+ if (hex !== undefined)
54
+ return String.fromCharCode(parseInt(hex, 16));
55
+ return SIMPLE_ESCAPES[other] ?? other; // \\ \' \" \` and friends: the character itself
56
+ });
64
57
  }
65
-
66
58
  /** Invisible/formatting characters that are never baked and never render — not worth warning about. */
67
59
  function ignorable(codepoint) {
68
- return (
69
- (codepoint >= 0x7f && codepoint <= 0x9f) || // DEL + C1 controls
70
- (codepoint >= 0x200b && codepoint <= 0x200f) || // zero-width + bidi marks
71
- (codepoint >= 0x2028 && codepoint <= 0x202e) || // line/paragraph separators + bidi overrides
72
- (codepoint >= 0xfe00 && codepoint <= 0xfe0f) || // variation selectors
73
- codepoint === 0xfeff // BOM
74
- );
60
+ return ((codepoint >= 0x7f && codepoint <= 0x9f) || // DEL + C1 controls
61
+ (codepoint >= 0x200b && codepoint <= 0x200f) || // zero-width + bidi marks
62
+ (codepoint >= 0x2028 && codepoint <= 0x202e) || // line/paragraph separators + bidi overrides
63
+ (codepoint >= 0xfe00 && codepoint <= 0xfe0f) || // variation selectors
64
+ codepoint === 0xfeff // BOM
65
+ );
75
66
  }
76
-
77
67
  // Text between two tags in unbundled JSX: `<Text>24° out</Text>`. Braces are excluded so an
78
68
  // expression child stays out of it — its own literals are already covered by LITERALS.
79
69
  const JSX_TEXT = />([^<>{}]+)</g;
80
-
81
70
  /**
82
71
  * Collects every non-ASCII character that appears in the app's rendered text.
83
72
  *
84
- * @param {string} source JS source (a bundle, a chunk, or a single component file).
85
- * @param {object} [opts]
86
- * @param {boolean} [opts.jsx] Also read JSX element text. Set it for unbundled `.jsx` — a bundler
87
- * has already turned element text into string literals, and on bundled output the extra
88
- * pass would only misread `a > b` comparisons.
89
- * @returns {Map<number, string>} Codepoint → a short excerpt of the text it was found in.
73
+ * @param source JS source (a bundle, a chunk, or a single component file).
74
+ * @returns Codepoint → a short excerpt of the text it was found in.
90
75
  */
91
- export function scanTextCodepoints(source, {jsx = false} = {}) {
92
- const found = new Map();
93
- const collect = text => {
94
- let offset = 0;
95
- for (const character of text) {
96
- const codepoint = character.codePointAt(0);
97
- if (
98
- codepoint > ASCII_LAST &&
99
- !ignorable(codepoint) &&
100
- !found.has(codepoint)
101
- )
102
- found.set(codepoint, excerpt(text, offset, character.length));
103
- offset += character.length;
104
- }
105
- };
106
-
107
- // Blank each token out as it is read, so the JSX pass below sees neither strings (already
108
- // collected) nor comments (never rendered) — only real element text.
109
- const bare = source.replace(LITERALS, token => {
110
- if (token[0] !== '/') collect(decodeEscapes(token.slice(1, -1)));
111
- return ' '.repeat(token.length);
112
- });
113
- if (jsx) for (const match of bare.matchAll(JSX_TEXT)) collect(match[1]);
114
- return found;
76
+ export function scanTextCodepoints(source, { jsx = false } = {}) {
77
+ const found = new Map();
78
+ const collect = (text) => {
79
+ let offset = 0;
80
+ for (const character of text) {
81
+ const codepoint = character.codePointAt(0);
82
+ if (codepoint > ASCII_LAST &&
83
+ !ignorable(codepoint) &&
84
+ !found.has(codepoint))
85
+ found.set(codepoint, excerpt(text, offset, character.length));
86
+ offset += character.length;
87
+ }
88
+ };
89
+ // Blank each token out as it is read, so the JSX pass below sees neither strings (already
90
+ // collected) nor comments (never rendered) — only real element text.
91
+ const bare = source.replace(LITERALS, token => {
92
+ if (token[0] !== '/')
93
+ collect(decodeEscapes(token.slice(1, -1)));
94
+ return ' '.repeat(token.length);
95
+ });
96
+ if (jsx)
97
+ for (const match of bare.matchAll(JSX_TEXT))
98
+ collect(match[1]);
99
+ return found;
115
100
  }
116
-
117
101
  /**
118
102
  * Compact signature of the non-ASCII characters a source renders. Watch loops fold this into their
119
103
  * "did the asset inputs change?" key so new text is re-checked, not just new fonts.
120
104
  *
121
- * @param {string} source JS source to scan.
122
- * @param {object} [opts] Passed through to scanTextCodepoints (e.g. `{jsx: true}`).
123
- * @returns {string} Sorted codepoints, comma-joined.
105
+ * @param source JS source to scan.
106
+ * @param opts Passed through to scanTextCodepoints (e.g. `{jsx: true}`).
107
+ * @returns Sorted codepoints, comma-joined.
124
108
  */
125
109
  export function textSignature(source, opts) {
126
- return [...scanTextCodepoints(source, opts).keys()]
127
- .sort((left, right) => left - right)
128
- .join(',');
110
+ return [...scanTextCodepoints(source, opts).keys()]
111
+ .sort((left, right) => left - right)
112
+ .join(',');
129
113
  }
130
-
131
114
  /** A one-line, printable window of `text` around the character at `at` (a UTF-16 index). */
132
115
  function excerpt(text, at, length) {
133
- const start = Math.max(0, at - EXCERPT_CONTEXT);
134
- const end = Math.min(text.length, at + length + EXCERPT_CONTEXT);
135
- const body = text
136
- .slice(start, end)
137
- .replace(/[\n\r\t]/g, ' ')
138
- .replace(/^[\udc00-\udfff]|[\ud800-\udbff]$/g, '') // a pair split by the window
139
- .trim();
140
- return `${start > 0 ? '…' : ''}${body}${end < text.length ? '…' : ''}`;
116
+ const start = Math.max(0, at - EXCERPT_CONTEXT);
117
+ const end = Math.min(text.length, at + length + EXCERPT_CONTEXT);
118
+ const body = text
119
+ .slice(start, end)
120
+ .replace(/[\n\r\t]/g, ' ')
121
+ .replace(/^[\udc00-\udfff]|[\ud800-\udbff]$/g, '') // a pair split by the window
122
+ .trim();
123
+ return `${start > 0 ? '…' : ''}${body}${end < text.length ? '…' : ''}`;
141
124
  }
142
-
143
125
  /** The set of codepoints a baked font can render: the dense ASCII range plus its baked extras. */
144
126
  function coverageOf(baked) {
145
- const size = baked.sizes?.[0];
146
- const covered = new Set();
147
- const first = size ? size.first : ASCII_FIRST;
148
- const last = size ? size.last : ASCII_LAST;
149
- for (let codepoint = first; codepoint <= last; codepoint++)
150
- covered.add(codepoint);
151
- for (const extra of size?.extras ?? []) covered.add(extra.codepoint);
152
- return covered;
127
+ const size = baked.sizes?.[0];
128
+ const covered = new Set();
129
+ const first = size ? size.first : ASCII_FIRST;
130
+ const last = size ? size.last : ASCII_LAST;
131
+ for (let codepoint = first; codepoint <= last; codepoint++)
132
+ covered.add(codepoint);
133
+ for (const extra of size?.extras ?? [])
134
+ covered.add(extra.codepoint);
135
+ return covered;
153
136
  }
154
-
155
137
  /** The engine's fallback font, shaped like a bakeFont() result so both go through one code path. */
156
138
  function builtinFont() {
157
- return {
158
- family: BUILTIN_FAMILY,
159
- sizes: [
160
- {
161
- first: ASCII_FIRST,
162
- last: ASCII_LAST,
163
- extras: BUILTIN_EXTRAS.map(codepoint => ({codepoint})),
164
- },
165
- ],
166
- };
139
+ return {
140
+ family: BUILTIN_FAMILY,
141
+ sizes: [
142
+ {
143
+ first: ASCII_FIRST,
144
+ last: ASCII_LAST,
145
+ extras: BUILTIN_EXTRAS.map(codepoint => ({ codepoint })),
146
+ },
147
+ ],
148
+ };
167
149
  }
168
-
169
150
  /**
170
151
  * Finds the characters the app renders that some font in the bake has no glyph for.
171
152
  *
@@ -174,128 +155,103 @@ function builtinFont() {
174
155
  * character the app's own font lacks is exactly the bug worth reporting); with none, the engine's
175
156
  * built-in fallback is checked instead.
176
157
  *
177
- * @param {object} opts
178
- * @param {string} opts.source App/bundle JS to scan for rendered text.
179
- * @param {Array<object>} [opts.fonts] bakeFont() results; empty means built-in font only.
180
- * @param {boolean} [opts.jsx] Source is unbundled JSX — read element text too.
181
- * @returns {Array<{codepoint:number, char:string, sample:string, missingFrom:string[],
182
- * noGlyphIn:string[]}>} Sorted by codepoint. `noGlyphIn` narrows `missingFrom` to the
183
- * fonts whose file has no glyph for the character at all — baking it there can't help.
158
+ * @returns Sorted by codepoint.
184
159
  */
185
- export function findMissingGlyphs({source, fonts = [], jsx = false}) {
186
- const checked = fonts.length ? fonts : [builtinFont()];
187
- const coverage = checked.map(font => ({
188
- family: font.family,
189
- path: font.path,
190
- covered: coverageOf(font),
191
- }));
192
- // Opened lazily and only for a font with a gap: most builds have none and pay nothing.
193
- const probes = new Map();
194
- const hasGlyph = (font, codepoint) => {
195
- if (!font.path) return true; // the built-in font's coverage is its bake, nothing more to ask
196
- if (!probes.has(font.path))
197
- probes.set(font.path, loadGlyphProbe(font.path));
198
- return probes.get(font.path)(codepoint);
199
- };
200
-
201
- const missing = [];
202
- for (const [codepoint, sample] of scanTextCodepoints(source, {jsx})) {
203
- const gaps = coverage.filter(entry => !entry.covered.has(codepoint));
204
- if (!gaps.length) continue;
205
- missing.push({
206
- codepoint,
207
- char: String.fromCodePoint(codepoint),
208
- sample,
209
- missingFrom: gaps.map(gap => gap.family),
210
- noGlyphIn: gaps
211
- .filter(gap => !hasGlyph(gap, codepoint))
212
- .map(gap => gap.family),
213
- });
214
- }
215
- return missing.sort((left, right) => left.codepoint - right.codepoint);
160
+ export function findMissingGlyphs({ source, fonts = [], jsx = false, }) {
161
+ const checked = fonts.length ? fonts : [builtinFont()];
162
+ const coverage = checked.map(font => ({
163
+ family: font.family,
164
+ path: font.path,
165
+ covered: coverageOf(font),
166
+ }));
167
+ // Opened lazily and only for a font with a gap: most builds have none and pay nothing.
168
+ const probes = new Map();
169
+ const hasGlyph = (font, codepoint) => {
170
+ if (!font.path)
171
+ return true; // the built-in font's coverage is its bake, nothing more to ask
172
+ if (!probes.has(font.path))
173
+ probes.set(font.path, loadGlyphProbe(font.path));
174
+ return probes.get(font.path)(codepoint);
175
+ };
176
+ const missing = [];
177
+ for (const [codepoint, sample] of scanTextCodepoints(source, { jsx })) {
178
+ const gaps = coverage.filter(entry => !entry.covered.has(codepoint));
179
+ if (!gaps.length)
180
+ continue;
181
+ missing.push({
182
+ codepoint,
183
+ char: String.fromCodePoint(codepoint),
184
+ sample,
185
+ missingFrom: gaps.map(gap => gap.family),
186
+ noGlyphIn: gaps
187
+ .filter(gap => !hasGlyph(gap, codepoint))
188
+ .map(gap => gap.family),
189
+ });
190
+ }
191
+ return missing.sort((left, right) => left.codepoint - right.codepoint);
216
192
  }
217
-
218
193
  const MAX_LISTED = 12;
219
-
220
194
  /**
221
195
  * Renders findMissingGlyphs() output as a developer-facing warning, ending with the config change
222
196
  * that fixes it.
223
197
  *
224
- * @param {Array<object>} missing Result of findMissingGlyphs().
225
- * @param {Array<object>} [fonts] The same bakeFont() results, for the suggested config line.
226
- * @returns {string} The warning text (empty when nothing is missing).
198
+ * @param missing Result of findMissingGlyphs().
199
+ * @param fonts The same bakeFont() results, for the suggested config line.
200
+ * @returns The warning text (empty when nothing is missing).
227
201
  */
228
202
  export function formatMissingGlyphs(missing, fonts = []) {
229
- if (!missing.length) return '';
230
- const lines = missing.slice(0, MAX_LISTED).map(glyph => {
231
- const hex = glyph.codepoint
232
- .toString(16)
233
- .toUpperCase()
234
- .padStart(CODEPOINT_HEX_DIGITS, '0');
235
- const note = glyph.noGlyphIn.length
236
- ? ` (the font file itself has no glyph for it — pick a font that covers it)`
237
- : '';
238
- return ` U+${hex} '${glyph.char}' missing from ${glyph.missingFrom.join(', ')}${note} in "${glyph.sample}"`;
239
- });
240
- if (missing.length > MAX_LISTED)
241
- lines.push(` … and ${missing.length - MAX_LISTED} more`);
242
-
243
- // One config entry per font, listing only what baking it there would actually fix.
244
- const perFamily = fonts
245
- .map(font => {
246
- const characters = missing
247
- .filter(
248
- glyph =>
249
- glyph.missingFrom.includes(font.family) &&
250
- !glyph.noGlyphIn.includes(font.family),
251
- )
252
- .map(glyph => glyph.char)
253
- .join('');
254
- // A family is a file basename, so it often needs quoting to be a valid key ('Inter-Regular').
255
- const key = /^[A-Za-z_$][\w$]*$/.test(font.family)
256
- ? font.family
257
- : JSON.stringify(font.family);
258
- return characters
259
- ? `${key}: {extraGlyphs: ${JSON.stringify(characters)}}`
260
- : null;
203
+ if (!missing.length)
204
+ return '';
205
+ const lines = missing.slice(0, MAX_LISTED).map(glyph => {
206
+ const hex = glyph.codepoint
207
+ .toString(16)
208
+ .toUpperCase()
209
+ .padStart(CODEPOINT_HEX_DIGITS, '0');
210
+ const note = glyph.noGlyphIn.length
211
+ ? ` (the font file itself has no glyph for it — pick a font that covers it)`
212
+ : '';
213
+ return ` U+${hex} '${glyph.char}' missing from ${glyph.missingFrom.join(', ')}${note} in "${glyph.sample}"`;
214
+ });
215
+ if (missing.length > MAX_LISTED)
216
+ lines.push(` … and ${missing.length - MAX_LISTED} more`);
217
+ // One config entry per font, listing only what baking it there would actually fix.
218
+ const perFamily = fonts
219
+ .map(font => {
220
+ const characters = missing
221
+ .filter(glyph => glyph.missingFrom.includes(font.family) &&
222
+ !glyph.noGlyphIn.includes(font.family))
223
+ .map(glyph => glyph.char)
224
+ .join('');
225
+ // A family is a file basename, so it often needs quoting to be a valid key ('Inter-Regular').
226
+ const key = /^[A-Za-z_$][\w$]*$/.test(font.family)
227
+ ? font.family
228
+ : JSON.stringify(font.family);
229
+ return characters
230
+ ? `${key}: {extraGlyphs: ${JSON.stringify(characters)}}`
231
+ : null;
261
232
  })
262
- .filter(Boolean);
263
-
264
- const fix = perFamily.length
265
- ? ` fix: assets.config.js → fonts: {${perFamily.join(', ')}}`
266
- : fonts.length
267
- ? ` fix: bake these from a font that has them (assets.config.js → fonts: {<Family>: {extraGlyphs: ...}})`
268
- : ` fix: the built-in font covers ASCII + common symbols only — import a .ttf/.otf and bake the\n` +
269
- ` rest with assets.config.js → fonts: {<Family>: {extraGlyphs: ${JSON.stringify(missing.map(glyph => glyph.char).join(''))}}}`;
270
-
271
- return (
272
- `embedded-react: ${missing.length} character(s) in the app's text have no baked glyph and will ` +
273
- `render as '?':\n${lines.join('\n')}\n${fix}`
274
- );
233
+ .filter(Boolean);
234
+ const fix = perFamily.length
235
+ ? ` fix: assets.config.js → fonts: {${perFamily.join(', ')}}`
236
+ : fonts.length
237
+ ? ` fix: bake these from a font that has them (assets.config.js → fonts: {<Family>: {extraGlyphs: ...}})`
238
+ : ` fix: the built-in font covers ASCII + common symbols only — import a .ttf/.otf and bake the\n` +
239
+ ` rest with assets.config.js → fonts: {<Family>: {extraGlyphs: ${JSON.stringify(missing.map(glyph => glyph.char).join(''))}}}`;
240
+ return (`embedded-react: ${missing.length} character(s) in the app's text have no baked glyph and will ` +
241
+ `render as '?':\n${lines.join('\n')}\n${fix}`);
275
242
  }
276
-
277
243
  let lastWarning = null; // dev loops re-bake on every save — don't reprint an unchanged warning
278
-
279
244
  /**
280
245
  * Checks glyph coverage and warns. Repeats are suppressed while the warning is unchanged, so a
281
246
  * watch/hot-reload loop reports each gap once rather than on every save.
282
247
  *
283
- * @param {object} opts
284
- * @param {string} opts.source App/bundle JS to scan for rendered text.
285
- * @param {Array<object>} [opts.fonts] bakeFont() results; empty means built-in font only.
286
- * @param {boolean} [opts.jsx] Source is unbundled JSX — read element text too.
287
- * @param {Function} [opts.log] Sink for the warning (default console.warn).
288
- * @returns {Array<object>} The missing-glyph records (empty when coverage is complete).
248
+ * @returns The missing-glyph records (empty when coverage is complete).
289
249
  */
290
- export function warnMissingGlyphs({
291
- source,
292
- fonts = [],
293
- jsx = false,
294
- log = console.warn,
295
- }) {
296
- const missing = findMissingGlyphs({source, fonts, jsx});
297
- const text = formatMissingGlyphs(missing, fonts);
298
- if (text && text !== lastWarning) log(text);
299
- lastWarning = text || null;
300
- return missing;
250
+ export function warnMissingGlyphs({ source, fonts = [], jsx = false, log = console.warn, }) {
251
+ const missing = findMissingGlyphs({ source, fonts, jsx });
252
+ const text = formatMissingGlyphs(missing, fonts);
253
+ if (text && text !== lastWarning)
254
+ log(text);
255
+ lastWarning = text || null;
256
+ return missing;
301
257
  }
package/assets/index.mjs CHANGED
@@ -13,116 +13,71 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
16
  // Asset baking orchestrator: turns the images and fonts an app imports into a single generated C
18
- // translation unit (assets.generated.c + .h) exposing er_register_assets(). Invoked by build.mjs
17
+ // translation unit (assets.generated.c + .h) exposing er_register_assets(). Invoked by build.mts
19
18
  // after bundling; the example firmware compiles the .c and calls er_register_assets() at boot.
20
19
  import fs from 'node:fs';
21
20
  import path from 'node:path';
22
- import {bakeImage} from './bake-image.mjs';
23
- import {bakeFont} from './bake-font.mjs';
24
- import {emitAssetsC} from './emit-c.mjs';
25
- import {emitAssetPack} from './emit-pack.mjs';
26
- import {warnMissingGlyphs} from './glyph-coverage.mjs';
27
- import {warnFontSizes} from './font-sizes.mjs';
28
-
21
+ import { bakeImage } from './bake-image.mjs';
22
+ import { bakeFont } from './bake-font.mjs';
23
+ import { emitAssetsC } from './emit-c.mjs';
24
+ import { emitAssetPack } from './emit-pack.mjs';
25
+ import { warnMissingGlyphs } from './glyph-coverage.mjs';
26
+ import { warnFontSizes } from './font-sizes.mjs';
29
27
  /**
30
- * Bakes the given assets and writes assets.generated.{c,h} into outDir.
31
- *
32
- * @param {object} opts
33
- * @param {Array<{path:string,name:string,format?:string}>} [opts.images] Discovered image
34
- * imports; format 'rgb565' bakes 16-bit (see bake-image.mjs), default ARGB8888.
35
- * @param {Array<{path:string,family:string,sizes:number[],bpp:number,glyphs:any,extraGlyphs:any}>}
36
- * [opts.fonts] Discovered font imports with their resolved size/bpp/glyph config.
37
- * @param {string} opts.outDir Directory to write the generated files into.
38
- * @param {string} [opts.source] Bundled app JS — scanned for text the bake has no glyph for.
39
- * @param {{sizes:number[],dynamic:Array<object>}} [opts.usedSizes] analyzeFontSizes() result,
40
- * checked against what was baked so an unbaked size is reported rather than silently snapped.
41
- * @returns {{cPath:string, hPath:string, images:number, fonts:number}}
28
+ * Bakes the given assets and writes assets.generated.{c,h} into outDir, the directory to write into.
42
29
  */
43
- export function bakeAssets({
44
- images = [],
45
- fonts = [],
46
- outDir,
47
- source,
48
- usedSizes,
49
- }) {
50
- const bakedImages = images.map(image => bakeImage(image));
51
- const bakedFonts = fonts.map(font => bakeFont(font));
52
- if (source) warnMissingGlyphs({source, fonts: bakedFonts});
53
- if (usedSizes) warnFontSizes({used: usedSizes, fonts: bakedFonts});
54
-
55
- const headerName = 'assets.generated.h';
56
- const {c: cSource, h: header} = emitAssetsC({
57
- headerName,
58
- images: bakedImages,
59
- fonts: bakedFonts,
60
- });
61
-
62
- fs.mkdirSync(outDir, {recursive: true});
63
- const cPath = path.join(outDir, 'assets.generated.c');
64
- const hPath = path.join(outDir, headerName);
65
- fs.writeFileSync(cPath, cSource);
66
- fs.writeFileSync(hPath, header);
67
-
68
- const fontSizes = bakedFonts.reduce(
69
- (total, font) => total + font.sizes.length,
70
- 0,
71
- );
72
- return {cPath, hPath, images: bakedImages.length, fonts: fontSizes};
30
+ export function bakeAssets({ images = [], fonts = [], outDir, source, usedSizes, }) {
31
+ const bakedImages = images.map(image => bakeImage(image));
32
+ const bakedFonts = fonts.map(font => bakeFont(font));
33
+ if (source)
34
+ warnMissingGlyphs({ source, fonts: bakedFonts });
35
+ if (usedSizes)
36
+ warnFontSizes({ used: usedSizes, fonts: bakedFonts });
37
+ const headerName = 'assets.generated.h';
38
+ const { c: cSource, h: header } = emitAssetsC({
39
+ headerName,
40
+ images: bakedImages,
41
+ fonts: bakedFonts,
42
+ });
43
+ fs.mkdirSync(outDir, { recursive: true });
44
+ const cPath = path.join(outDir, 'assets.generated.c');
45
+ const hPath = path.join(outDir, headerName);
46
+ fs.writeFileSync(cPath, cSource);
47
+ fs.writeFileSync(hPath, header);
48
+ const fontSizes = bakedFonts.reduce((total, font) => total + font.sizes.length, 0);
49
+ return { cPath, hPath, images: bakedImages.length, fonts: fontSizes };
73
50
  }
74
-
75
51
  /**
76
52
  * Bakes the given assets into ERPK pack bytes, with the same bakers as bakeAssets, without writing a file.
77
53
  *
78
- * @param {object} opts
79
- * @param {Array<{path:string,name:string}>} [opts.images]
80
- * @param {Array<{path:string,family:string,sizes:number[],bpp:number,glyphs:any,extraGlyphs:any}>}
81
- * [opts.fonts]
82
- * @returns {Buffer} The pack.
54
+ * @returns The pack.
83
55
  */
84
- export function bakeAssetPackBytes({images = [], fonts = []}) {
85
- return emitAssetPack({
86
- images: images.map(image => bakeImage(image)),
87
- fonts: fonts.map(font => bakeFont(font)),
88
- });
56
+ export function bakeAssetPackBytes({ images = [], fonts = [], }) {
57
+ return emitAssetPack({
58
+ images: images.map(image => bakeImage(image)),
59
+ fonts: fonts.map(font => bakeFont(font)),
60
+ });
89
61
  }
90
-
91
62
  /**
92
63
  * Bakes the given assets into a binary ERPK pack the simulator loads at runtime (hot-reloadable),
93
- * using the same bakers as bakeAssets so the result is pixel-identical.
94
- *
95
- * @param {object} opts
96
- * @param {Array<{path:string,name:string}>} [opts.images]
97
- * @param {Array<{path:string,family:string,sizes:number[],bpp:number,glyphs:any,extraGlyphs:any}>}
98
- * [opts.fonts]
99
- * @param {string} opts.outPath Path to write the .pack file.
100
- * @param {string} [opts.source] Bundled app JS — scanned for text the bake has no glyph for.
101
- * @param {{sizes:number[],dynamic:Array<object>}} [opts.usedSizes] analyzeFontSizes() result.
102
- * @returns {{path:string, bytes:number, images:number, fonts:number}}
64
+ * using the same bakers as bakeAssets so the result is pixel-identical. outPath is the .pack file to write.
103
65
  */
104
- export function bakeAssetPack({
105
- images = [],
106
- fonts = [],
107
- outPath,
108
- source,
109
- usedSizes,
110
- }) {
111
- const bakedImages = images.map(image => bakeImage(image));
112
- const bakedFonts = fonts.map(font => bakeFont(font));
113
- if (source) warnMissingGlyphs({source, fonts: bakedFonts});
114
- if (usedSizes) warnFontSizes({used: usedSizes, fonts: bakedFonts});
115
- const pack = emitAssetPack({images: bakedImages, fonts: bakedFonts});
116
- fs.mkdirSync(path.dirname(outPath), {recursive: true});
117
- fs.writeFileSync(outPath, pack);
118
- const fontSizes = bakedFonts.reduce(
119
- (total, font) => total + font.sizes.length,
120
- 0,
121
- );
122
- return {
123
- path: outPath,
124
- bytes: pack.length,
125
- images: bakedImages.length,
126
- fonts: fontSizes,
127
- };
66
+ export function bakeAssetPack({ images = [], fonts = [], outPath, source, usedSizes, }) {
67
+ const bakedImages = images.map(image => bakeImage(image));
68
+ const bakedFonts = fonts.map(font => bakeFont(font));
69
+ if (source)
70
+ warnMissingGlyphs({ source, fonts: bakedFonts });
71
+ if (usedSizes)
72
+ warnFontSizes({ used: usedSizes, fonts: bakedFonts });
73
+ const pack = emitAssetPack({ images: bakedImages, fonts: bakedFonts });
74
+ fs.mkdirSync(path.dirname(outPath), { recursive: true });
75
+ fs.writeFileSync(outPath, pack);
76
+ const fontSizes = bakedFonts.reduce((total, font) => total + font.sizes.length, 0);
77
+ return {
78
+ path: outPath,
79
+ bytes: pack.length,
80
+ images: bakedImages.length,
81
+ fonts: fontSizes,
82
+ };
128
83
  }