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,218 +13,197 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
- // Pure-JS glyph rasterizer — fills an opentype.js Path into a grayscale coverage bitmap with no
18
- // native dependencies. Curves are flattened to polylines, then each pixel's coverage is computed by
19
- // scanline-filling at SS× supersampling using the nonzero winding rule (TrueType's convention).
20
- //
21
- // The output convention matches what the engine's text renderer expects (see draw_glyph /
22
- // draw_glyph_aa in engine/text/text_renderer.c): coordinates come from glyph.getPath(0, baseline,
23
- // pixelSize), so xOffset is the glyph's left bearing from the pen and yOffset is the distance from
24
- // the line box top (y = 0) down to the top of the glyph ink.
25
-
26
16
  const DEFAULT_SUPERSAMPLE = 4;
27
17
  const DEFAULT_CURVE_STEPS = 12;
28
18
  const COVERAGE_MAX = 255;
29
19
  const SAMPLE_CENTER = 0.5;
30
-
31
20
  /**
32
21
  * Flattens opentype path commands into closed contours of {x, y} points.
33
22
  *
34
- * @param {Array} commands opentype.js Path commands (M/L/Q/C/Z).
35
- * @param {number} steps Subdivision steps per Bézier curve.
36
- * @returns {Array<Array<{x:number,y:number}>>} One point array per contour.
23
+ * @param commands opentype.js Path commands (M/L/Q/C/Z).
24
+ * @param steps Subdivision steps per Bézier curve.
25
+ * @returns One point array per contour.
37
26
  */
38
27
  function flattenCommands(commands, steps) {
39
- const contours = [];
40
- let contour = null;
41
- let currentX = 0;
42
- let currentY = 0;
43
- let startX = 0;
44
- let startY = 0;
45
- for (const command of commands) {
46
- if (command.type === 'M') {
47
- if (contour && contour.length > 1) contours.push(contour);
48
- contour = [{x: command.x, y: command.y}];
49
- currentX = startX = command.x;
50
- currentY = startY = command.y;
51
- } else if (command.type === 'L') {
52
- contour.push({x: command.x, y: command.y});
53
- currentX = command.x;
54
- currentY = command.y;
55
- } else if (command.type === 'Q') {
56
- for (let step = 1; step <= steps; step++) {
57
- const t = step / steps;
58
- const oneMinusT = 1 - t;
59
- contour.push({
60
- x:
61
- oneMinusT * oneMinusT * currentX +
62
- 2 * oneMinusT * t * command.x1 +
63
- t * t * command.x,
64
- y:
65
- oneMinusT * oneMinusT * currentY +
66
- 2 * oneMinusT * t * command.y1 +
67
- t * t * command.y,
68
- });
69
- }
70
- currentX = command.x;
71
- currentY = command.y;
72
- } else if (command.type === 'C') {
73
- for (let step = 1; step <= steps; step++) {
74
- const t = step / steps;
75
- const oneMinusT = 1 - t;
76
- contour.push({
77
- x:
78
- oneMinusT * oneMinusT * oneMinusT * currentX +
79
- 3 * oneMinusT * oneMinusT * t * command.x1 +
80
- 3 * oneMinusT * t * t * command.x2 +
81
- t * t * t * command.x,
82
- y:
83
- oneMinusT * oneMinusT * oneMinusT * currentY +
84
- 3 * oneMinusT * oneMinusT * t * command.y1 +
85
- 3 * oneMinusT * t * t * command.y2 +
86
- t * t * t * command.y,
87
- });
88
- }
89
- currentX = command.x;
90
- currentY = command.y;
91
- } else if (command.type === 'Z') {
92
- if (contour) {
93
- contour.push({x: startX, y: startY}); // close the contour
94
- if (contour.length > 1) contours.push(contour);
95
- contour = null;
96
- }
28
+ const contours = [];
29
+ let contour = null;
30
+ let currentX = 0;
31
+ let currentY = 0;
32
+ let startX = 0;
33
+ let startY = 0;
34
+ for (const command of commands) {
35
+ if (command.type === 'M') {
36
+ if (contour && contour.length > 1)
37
+ contours.push(contour);
38
+ contour = [{ x: command.x, y: command.y }];
39
+ currentX = startX = command.x;
40
+ currentY = startY = command.y;
41
+ }
42
+ else if (command.type === 'L') {
43
+ contour.push({ x: command.x, y: command.y });
44
+ currentX = command.x;
45
+ currentY = command.y;
46
+ }
47
+ else if (command.type === 'Q') {
48
+ for (let step = 1; step <= steps; step++) {
49
+ const t = step / steps;
50
+ const oneMinusT = 1 - t;
51
+ contour.push({
52
+ x: oneMinusT * oneMinusT * currentX +
53
+ 2 * oneMinusT * t * command.x1 +
54
+ t * t * command.x,
55
+ y: oneMinusT * oneMinusT * currentY +
56
+ 2 * oneMinusT * t * command.y1 +
57
+ t * t * command.y,
58
+ });
59
+ }
60
+ currentX = command.x;
61
+ currentY = command.y;
62
+ }
63
+ else if (command.type === 'C') {
64
+ for (let step = 1; step <= steps; step++) {
65
+ const t = step / steps;
66
+ const oneMinusT = 1 - t;
67
+ contour.push({
68
+ x: oneMinusT * oneMinusT * oneMinusT * currentX +
69
+ 3 * oneMinusT * oneMinusT * t * command.x1 +
70
+ 3 * oneMinusT * t * t * command.x2 +
71
+ t * t * t * command.x,
72
+ y: oneMinusT * oneMinusT * oneMinusT * currentY +
73
+ 3 * oneMinusT * oneMinusT * t * command.y1 +
74
+ 3 * oneMinusT * t * t * command.y2 +
75
+ t * t * t * command.y,
76
+ });
77
+ }
78
+ currentX = command.x;
79
+ currentY = command.y;
80
+ }
81
+ else if (command.type === 'Z') {
82
+ if (contour) {
83
+ contour.push({ x: startX, y: startY }); // close the contour
84
+ if (contour.length > 1)
85
+ contours.push(contour);
86
+ contour = null;
87
+ }
88
+ }
97
89
  }
98
- }
99
- if (contour && contour.length > 1) contours.push(contour);
100
- return contours;
90
+ if (contour && contour.length > 1)
91
+ contours.push(contour);
92
+ return contours;
101
93
  }
102
-
103
94
  /**
104
95
  * Rasterizes a glyph path to an 8-bit coverage bitmap (0-255 per pixel).
105
96
  *
106
- * @param {object} path opentype.js Path (from glyph.getPath(0, baseline, pixelSize)).
107
- * @param {object} [opts]
108
- * @param {number} [opts.ss] Supersample factor per axis (coverage levels = ss*ss). Default 4.
109
- * @param {number} [opts.steps] Bézier subdivision steps. Default 12.
110
- * @returns {{width:number,height:number,xOffset:number,yOffset:number,coverage:Uint8Array}}
111
- * An empty (width 0, height 0) result for whitespace / glyphs with no ink.
97
+ * @param path opentype.js Path (from glyph.getPath(0, baseline, pixelSize)).
98
+ * @returns An empty (width 0, height 0) result for whitespace / glyphs with no ink.
112
99
  */
113
100
  export function rasterize(path, opts = {}) {
114
- const supersample = opts.ss || DEFAULT_SUPERSAMPLE;
115
- const steps = opts.steps || DEFAULT_CURVE_STEPS;
116
- const contours = flattenCommands(path.commands, steps);
117
-
118
- let minX = Infinity;
119
- let minY = Infinity;
120
- let maxX = -Infinity;
121
- let maxY = -Infinity;
122
- for (const contour of contours) {
123
- for (const point of contour) {
124
- if (point.x < minX) minX = point.x;
125
- if (point.x > maxX) maxX = point.x;
126
- if (point.y < minY) minY = point.y;
127
- if (point.y > maxY) maxY = point.y;
101
+ const supersample = opts.ss || DEFAULT_SUPERSAMPLE;
102
+ const steps = opts.steps || DEFAULT_CURVE_STEPS;
103
+ const contours = flattenCommands(path.commands, steps);
104
+ let minX = Infinity;
105
+ let minY = Infinity;
106
+ let maxX = -Infinity;
107
+ let maxY = -Infinity;
108
+ for (const contour of contours) {
109
+ for (const point of contour) {
110
+ if (point.x < minX)
111
+ minX = point.x;
112
+ if (point.x > maxX)
113
+ maxX = point.x;
114
+ if (point.y < minY)
115
+ minY = point.y;
116
+ if (point.y > maxY)
117
+ maxY = point.y;
118
+ }
128
119
  }
129
- }
130
- if (!Number.isFinite(minX)) {
131
- return {
132
- width: 0,
133
- height: 0,
134
- xOffset: 0,
135
- yOffset: 0,
136
- coverage: new Uint8Array(0),
137
- };
138
- }
139
-
140
- const originX = Math.floor(minX);
141
- const originY = Math.floor(minY);
142
- const width = Math.ceil(maxX) - originX;
143
- const height = Math.ceil(maxY) - originY;
144
- if (width <= 0 || height <= 0) {
145
- return {
146
- width: 0,
147
- height: 0,
148
- xOffset: 0,
149
- yOffset: 0,
150
- coverage: new Uint8Array(0),
151
- };
152
- }
153
-
154
- // Non-horizontal edges, normalized so yLow < yHigh; winding is the edge's winding contribution.
155
- const edges = [];
156
- for (const contour of contours) {
157
- for (let index = 0; index + 1 < contour.length; index++) {
158
- const from = contour[index];
159
- const to = contour[index + 1];
160
- if (from.y === to.y) continue;
161
- if (from.y < to.y)
162
- edges.push({
163
- yLow: from.y,
164
- yHigh: to.y,
165
- x: from.x,
166
- slope: (to.x - from.x) / (to.y - from.y),
167
- winding: 1,
168
- });
169
- else
170
- edges.push({
171
- yLow: to.y,
172
- yHigh: from.y,
173
- x: to.x,
174
- slope: (from.x - to.x) / (from.y - to.y),
175
- winding: -1,
176
- });
120
+ if (!Number.isFinite(minX)) {
121
+ return {
122
+ width: 0,
123
+ height: 0,
124
+ xOffset: 0,
125
+ yOffset: 0,
126
+ coverage: new Uint8Array(0),
127
+ };
177
128
  }
178
- }
179
-
180
- const counts = new Uint16Array(width * height); // covered subsamples per output pixel
181
- const subsampleWidth = width * supersample;
182
- for (
183
- let subsampleRow = 0;
184
- subsampleRow < height * supersample;
185
- subsampleRow++
186
- ) {
187
- const sampleY = originY + (subsampleRow + SAMPLE_CENTER) / supersample;
188
- const crossings = [];
189
- for (const edge of edges) {
190
- if (sampleY >= edge.yLow && sampleY < edge.yHigh) {
191
- crossings.push({
192
- x: edge.x + (sampleY - edge.yLow) * edge.slope,
193
- winding: edge.winding,
194
- });
195
- }
129
+ const originX = Math.floor(minX);
130
+ const originY = Math.floor(minY);
131
+ const width = Math.ceil(maxX) - originX;
132
+ const height = Math.ceil(maxY) - originY;
133
+ if (width <= 0 || height <= 0) {
134
+ return {
135
+ width: 0,
136
+ height: 0,
137
+ xOffset: 0,
138
+ yOffset: 0,
139
+ coverage: new Uint8Array(0),
140
+ };
196
141
  }
197
- if (crossings.length < 2) continue;
198
- crossings.sort((left, right) => left.x - right.x);
199
- const outputRow = (subsampleRow / supersample) | 0;
200
- let winding = 0;
201
- for (let index = 0; index + 1 < crossings.length; index++) {
202
- winding += crossings[index].winding;
203
- if (winding === 0) continue;
204
- // Interior span [crossings[index].x, crossings[index+1].x): tally the subsample columns whose
205
- // centre falls inside.
206
- let spanStart = Math.ceil(
207
- (crossings[index].x - originX) * supersample - SAMPLE_CENTER,
208
- );
209
- let spanEnd = Math.ceil(
210
- (crossings[index + 1].x - originX) * supersample - SAMPLE_CENTER,
211
- );
212
- if (spanStart < 0) spanStart = 0;
213
- if (spanEnd > subsampleWidth) spanEnd = subsampleWidth;
214
- for (
215
- let subsampleColumn = spanStart;
216
- subsampleColumn < spanEnd;
217
- subsampleColumn++
218
- ) {
219
- counts[outputRow * width + ((subsampleColumn / supersample) | 0)]++;
220
- }
142
+ // Non-horizontal edges, normalized so yLow < yHigh; winding is the edge's winding contribution.
143
+ const edges = [];
144
+ for (const contour of contours) {
145
+ for (let index = 0; index + 1 < contour.length; index++) {
146
+ const from = contour[index];
147
+ const to = contour[index + 1];
148
+ if (from.y === to.y)
149
+ continue;
150
+ if (from.y < to.y)
151
+ edges.push({
152
+ yLow: from.y,
153
+ yHigh: to.y,
154
+ x: from.x,
155
+ slope: (to.x - from.x) / (to.y - from.y),
156
+ winding: 1,
157
+ });
158
+ else
159
+ edges.push({
160
+ yLow: to.y,
161
+ yHigh: from.y,
162
+ x: to.x,
163
+ slope: (from.x - to.x) / (from.y - to.y),
164
+ winding: -1,
165
+ });
166
+ }
221
167
  }
222
- }
223
-
224
- const maxCoverage = supersample * supersample;
225
- const coverage = new Uint8Array(width * height);
226
- for (let index = 0; index < coverage.length; index++) {
227
- coverage[index] = Math.round((counts[index] * COVERAGE_MAX) / maxCoverage);
228
- }
229
- return {width, height, xOffset: originX, yOffset: originY, coverage};
168
+ const counts = new Uint16Array(width * height); // covered subsamples per output pixel
169
+ const subsampleWidth = width * supersample;
170
+ for (let subsampleRow = 0; subsampleRow < height * supersample; subsampleRow++) {
171
+ const sampleY = originY + (subsampleRow + SAMPLE_CENTER) / supersample;
172
+ const crossings = [];
173
+ for (const edge of edges) {
174
+ if (sampleY >= edge.yLow && sampleY < edge.yHigh) {
175
+ crossings.push({
176
+ x: edge.x + (sampleY - edge.yLow) * edge.slope,
177
+ winding: edge.winding,
178
+ });
179
+ }
180
+ }
181
+ if (crossings.length < 2)
182
+ continue;
183
+ crossings.sort((left, right) => left.x - right.x);
184
+ const outputRow = (subsampleRow / supersample) | 0;
185
+ let winding = 0;
186
+ for (let index = 0; index + 1 < crossings.length; index++) {
187
+ winding += crossings[index].winding;
188
+ if (winding === 0)
189
+ continue;
190
+ // Interior span [crossings[index].x, crossings[index+1].x): tally the subsample columns whose
191
+ // centre falls inside.
192
+ let spanStart = Math.ceil((crossings[index].x - originX) * supersample - SAMPLE_CENTER);
193
+ let spanEnd = Math.ceil((crossings[index + 1].x - originX) * supersample - SAMPLE_CENTER);
194
+ if (spanStart < 0)
195
+ spanStart = 0;
196
+ if (spanEnd > subsampleWidth)
197
+ spanEnd = subsampleWidth;
198
+ for (let subsampleColumn = spanStart; subsampleColumn < spanEnd; subsampleColumn++) {
199
+ counts[outputRow * width + ((subsampleColumn / supersample) | 0)]++;
200
+ }
201
+ }
202
+ }
203
+ const maxCoverage = supersample * supersample;
204
+ const coverage = new Uint8Array(width * height);
205
+ for (let index = 0; index < coverage.length; index++) {
206
+ coverage[index] = Math.round((counts[index] * COVERAGE_MAX) / maxCoverage);
207
+ }
208
+ return { width, height, xOffset: originX, yOffset: originY, coverage };
230
209
  }
@@ -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
  // esbuild loader for `import logo from './logo.svg'`. Two outcomes, decided at bake time:
18
17
  // - VECTOR: the SVG is fully representable as the engine's op-tape — bake it and INLINE a
19
18
  // {kind:'vector', ops, paints, gradients, width, height} artifact (small numeric data) into the bundle.
@@ -23,59 +22,54 @@
23
22
  // {kind:'raster', name, width, height} artifact. A <Svg source> renders kind:'vector' as a vector node
24
23
  // and kind:'raster' as an image node (host-config), so the fallback is transparent to the app.
25
24
  // Shared by every Flow A bundler so the .svg handling lives in one place.
26
- import {readFileSync} from 'node:fs';
27
- import {basename} from 'node:path';
28
- import {svgToVector, svgToRaster, writeRasterPng} from './bake-svg.mjs';
29
-
30
- const assetName = filePath => basename(filePath).replace(/\.[^.]+$/, '');
31
-
25
+ import { readFileSync } from 'node:fs';
26
+ import { basename } from 'node:path';
27
+ import { svgToVector, svgToRaster, writeRasterPng } from './bake-svg.mjs';
28
+ const assetName = (filePath) => basename(filePath).replace(/\.[^.]+$/, '');
32
29
  /**
33
30
  * Registers a `.svg` onLoad on an esbuild build.
34
31
  *
35
- * @param {import('esbuild').PluginBuild} build The esbuild build passed to a plugin's setup().
36
- * @param {(name:string, pngPath:string)=>void} [addRasterAsset] Registers a rasterized-SVG PNG into the
32
+ * @param build The esbuild build passed to a plugin's setup().
33
+ * @param addRasterAsset Registers a rasterized-SVG PNG into the
37
34
  * bundler's image collection (same mechanism as `import x from './x.png'`). When omitted, an SVG that
38
35
  * needs raster fallback is baked as a (lossy) vector and a warning notes the dropped content.
39
36
  */
40
37
  export function registerSvgVectorLoader(build, addRasterAsset) {
41
- build.onLoad({filter: /\.svg$/i}, async args => {
42
- try {
43
- const svg = readFileSync(args.path, 'utf8');
44
- const {dropped = [], ...vector} = await svgToVector(svg);
45
- if (dropped.length) {
46
- const features = dropped.join(', ');
47
- if (addRasterAsset) {
48
- console.warn(
49
- `embedded-react: ${basename(args.path)} uses unsupported SVG feature(s) [${features}] — rasterizing ` +
50
- `it as a fallback image. (Raster loses scalability and costs RAM; simplify the SVG to keep it a ` +
51
- `live vector.)`,
52
- );
53
- const {width, height, png} = await svgToRaster(svg);
54
- const name = assetName(args.path);
55
- addRasterAsset(name, writeRasterPng(name, png));
56
- return {
57
- contents: `module.exports = ${JSON.stringify({kind: 'raster', name, width, height})};`,
58
- loader: 'js',
59
- };
38
+ build.onLoad({ filter: /\.svg$/i }, async (args) => {
39
+ try {
40
+ const svg = readFileSync(args.path, 'utf8');
41
+ const { dropped = [], ...vector } = await svgToVector(svg);
42
+ if (dropped.length) {
43
+ const features = dropped.join(', ');
44
+ if (addRasterAsset) {
45
+ console.warn(`embedded-react: ${basename(args.path)} uses unsupported SVG feature(s) [${features}] — rasterizing ` +
46
+ `it as a fallback image. (Raster loses scalability and costs RAM; simplify the SVG to keep it a ` +
47
+ `live vector.)`);
48
+ const { width, height, png } = await svgToRaster(svg);
49
+ const name = assetName(args.path);
50
+ addRasterAsset(name, writeRasterPng(name, png));
51
+ return {
52
+ contents: `module.exports = ${JSON.stringify({ kind: 'raster', name, width, height })};`,
53
+ loader: 'js',
54
+ };
55
+ }
56
+ console.warn(`embedded-react: ${basename(args.path)} uses unsupported SVG feature(s) [${features}] and this build has ` +
57
+ `no raster fallback — that content will NOT render. Simplify the SVG, or import it via a bundler that ` +
58
+ `supports the image pipeline.`);
59
+ }
60
+ return {
61
+ contents: `module.exports = ${JSON.stringify({ kind: 'vector', ...vector })};`,
62
+ loader: 'js',
63
+ };
60
64
  }
61
- console.warn(
62
- `embedded-react: ${basename(args.path)} uses unsupported SVG feature(s) [${features}] and this build has ` +
63
- `no raster fallback — that content will NOT render. Simplify the SVG, or import it via a bundler that ` +
64
- `supports the image pipeline.`,
65
- );
66
- }
67
- return {
68
- contents: `module.exports = ${JSON.stringify({kind: 'vector', ...vector})};`,
69
- loader: 'js',
70
- };
71
- } catch (error) {
72
- return {
73
- errors: [
74
- {
75
- text: `embedded-react: failed to bake SVG ${args.path}: ${error.message}`,
76
- },
77
- ],
78
- };
79
- }
80
- });
65
+ catch (error) {
66
+ return {
67
+ errors: [
68
+ {
69
+ text: `embedded-react: failed to bake SVG ${args.path}: ${error.message}`,
70
+ },
71
+ ],
72
+ };
73
+ }
74
+ });
81
75
  }