react-native-roku 0.0.0-stage → 0.2.0-preview.1

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 (200) hide show
  1. package/CHANGELOG.md +318 -0
  2. package/CONTRIBUTING.md +34 -0
  3. package/LICENSE +21 -0
  4. package/NOTICE.md +10 -0
  5. package/README.md +66 -2
  6. package/bin/react-native-roku.mjs +91 -0
  7. package/build/assets.cjs +311 -0
  8. package/build/assets.d.cts +50 -0
  9. package/build/compatibility.d.mts +40 -0
  10. package/build/compatibility.mjs +272 -0
  11. package/build/dev.d.mts +34 -0
  12. package/build/dev.mjs +297 -0
  13. package/build/devWorkspace.d.mts +11 -0
  14. package/build/devWorkspace.mjs +86 -0
  15. package/build/device.d.mts +47 -0
  16. package/build/device.mjs +310 -0
  17. package/build/environment.d.mts +29 -0
  18. package/build/environment.mjs +144 -0
  19. package/build/esbuild.d.mts +5 -0
  20. package/build/esbuild.mjs +118 -0
  21. package/build/fontVariants.d.mts +27 -0
  22. package/build/fontVariants.mjs +424 -0
  23. package/build/frontend.d.mts +52 -0
  24. package/build/frontend.mjs +75 -0
  25. package/build/identity.mjs +45 -0
  26. package/build/index.d.mts +22 -0
  27. package/build/index.mjs +87 -0
  28. package/build/intl.mjs +121 -0
  29. package/build/metro.cjs +38 -0
  30. package/build/metro.d.cts +37 -0
  31. package/build/metroFrontend.mjs +222 -0
  32. package/build/metroMetadata.mjs +119 -0
  33. package/build/metroModules.mjs +397 -0
  34. package/build/metroRefresh.d.mts +64 -0
  35. package/build/metroRefresh.mjs +346 -0
  36. package/build/metroTransformer.cjs +146 -0
  37. package/build/nativeTextMemo.mjs +296 -0
  38. package/build/nativewind.d.mts +9 -0
  39. package/build/nativewind.mjs +92 -0
  40. package/build/project.d.mts +111 -0
  41. package/build/project.mjs +615 -0
  42. package/build/reactCompiler.mjs +113 -0
  43. package/build/reactProfile.cjs +35 -0
  44. package/build/refreshRuntime.mjs +187 -0
  45. package/build/refreshSourceMap.mjs +53 -0
  46. package/build/release.mjs +99 -0
  47. package/build/resolution.d.mts +3 -0
  48. package/build/resolution.mjs +25 -0
  49. package/build/surfaces.d.mts +1 -0
  50. package/build/surfaces.mjs +42 -0
  51. package/build/testing/node.d.mts +29 -0
  52. package/build/testing/node.mjs +106 -0
  53. package/build/yoga.d.mts +8 -0
  54. package/build/yoga.mjs +186 -0
  55. package/compatibility/public-api.json +327 -0
  56. package/compatibility/registry.json +2258 -0
  57. package/config/release.json +15 -0
  58. package/config/toolchain.json +34 -0
  59. package/docs/compatibility.md +813 -0
  60. package/docs/getting-started.md +460 -0
  61. package/docs/maintenance.md +362 -0
  62. package/docs/qualification.md +241 -0
  63. package/docs/standalone-testing.md +56 -0
  64. package/examples/hello-roku/README.md +35 -0
  65. package/examples/hello-roku/package.template.json +31 -0
  66. package/examples/hello-roku/pnpm-workspace.yaml +11 -0
  67. package/examples/hello-roku/roku.config.json +22 -0
  68. package/examples/hello-roku/src/App.tsx +54 -0
  69. package/examples/hello-roku/src/Catalog.tsx +200 -0
  70. package/examples/hello-roku/src/assets.d.ts +16 -0
  71. package/examples/hello-roku/src/catalog-cover.png +0 -0
  72. package/examples/hello-roku/tsconfig.json +1 -0
  73. package/examples/hello-roku/tsconfig.react.json +1 -0
  74. package/native/components/RNRAppControl.brs +215 -0
  75. package/native/components/RNRAppControl.xml +13 -0
  76. package/native/components/RNRDeviceFacts.brs +19 -0
  77. package/native/components/RNRDeviceFacts.xml +9 -0
  78. package/native/components/RNRDeviceFactsTask.brs +74 -0
  79. package/native/components/RNRDeviceFactsTask.xml +9 -0
  80. package/native/components/RNRTextRaster.brs +15 -0
  81. package/native/components/RNRTextRaster.xml +9 -0
  82. package/native/components/RNRTextRasterTask.brs +77 -0
  83. package/native/components/RNRTextRasterTask.xml +9 -0
  84. package/native/manifest.json +7 -0
  85. package/package.json +183 -3
  86. package/src/animation/clock.ts +10 -0
  87. package/src/animation/derivedValue.ts +163 -0
  88. package/src/animation/legacyAnimated.ts +255 -0
  89. package/src/animation/reanimated.ts +243 -0
  90. package/src/animation/sharedValue.ts +285 -0
  91. package/src/animation/worklets.ts +23 -0
  92. package/src/application/AppRegistry.ts +31 -0
  93. package/src/application/mountApplication.ts +136 -0
  94. package/src/graphics/BitmapLayer.tsx +68 -0
  95. package/src/graphics/RoundedBorder.tsx +116 -0
  96. package/src/graphics/TextRasterCache.ts +259 -0
  97. package/src/graphics/gradient.ts +37 -0
  98. package/src/graphics/inlineRaster.ts +85 -0
  99. package/src/graphics/png.ts +77 -0
  100. package/src/graphics/rasterPixels.ts +89 -0
  101. package/src/graphics/surfaces.ts +105 -0
  102. package/src/graphics/textRaster.types.ts +60 -0
  103. package/src/graphics/textRasterGeometry.ts +116 -0
  104. package/src/host/host.types.ts +57 -0
  105. package/src/index.ts +22 -0
  106. package/src/interop/nativewind.ts +2 -0
  107. package/src/layout/FontResources.ts +252 -0
  108. package/src/layout/affine.ts +89 -0
  109. package/src/layout/flexLayout.ts +398 -0
  110. package/src/layout/fontFaces.d.mts +19 -0
  111. package/src/layout/fontFaces.mjs +85 -0
  112. package/src/layout/fontVariants.d.mts +2 -0
  113. package/src/layout/fontVariants.mjs +8 -0
  114. package/src/layout/inlineParagraph.ts +158 -0
  115. package/src/layout/inlineText.ts +68 -0
  116. package/src/layout/layoutReuse.ts +117 -0
  117. package/src/layout/nativeNodeFields.ts +179 -0
  118. package/src/layout/preparedFields.ts +78 -0
  119. package/src/layout/textLayout.ts +138 -0
  120. package/src/layout/transform.ts +113 -0
  121. package/src/media/SceneGraphAudio.ts +161 -0
  122. package/src/media/SceneGraphVideo.ts +167 -0
  123. package/src/media/audioSession.ts +84 -0
  124. package/src/media/index.ts +2 -0
  125. package/src/media/videoErrorDetails.ts +40 -0
  126. package/src/media/videoSession.ts +136 -0
  127. package/src/navigation/RouterStore.ts +244 -0
  128. package/src/navigation/expoRouter.ts +115 -0
  129. package/src/react-native/NativeInput.ts +494 -0
  130. package/src/react-native/NativeSystemState.ts +95 -0
  131. package/src/react-native/accessibility.ts +265 -0
  132. package/src/react-native/activityIndicator.ts +72 -0
  133. package/src/react-native/compilerPressable.ts +26 -0
  134. package/src/react-native/createNativeRoot.ts +2126 -0
  135. package/src/react-native/directionalFocus.ts +55 -0
  136. package/src/react-native/environment.ts +275 -0
  137. package/src/react-native/flatList.ts +1001 -0
  138. package/src/react-native/flatListViewability.ts +231 -0
  139. package/src/react-native/focusEvents.ts +85 -0
  140. package/src/react-native/image.ts +247 -0
  141. package/src/react-native/index.ts +54 -0
  142. package/src/react-native/interactionManager.ts +76 -0
  143. package/src/react-native/keyboard.ts +60 -0
  144. package/src/react-native/linearGradient.ts +94 -0
  145. package/src/react-native/modal.tsx +148 -0
  146. package/src/react-native/nativeHostContext.ts +80 -0
  147. package/src/react-native/nativeModules.ts +86 -0
  148. package/src/react-native/nativeRoot.types.ts +22 -0
  149. package/src/react-native/nativeViewHandle.ts +191 -0
  150. package/src/react-native/nativewindPressable.ts +38 -0
  151. package/src/react-native/panResponder.ts +73 -0
  152. package/src/react-native/primitives.ts +685 -0
  153. package/src/react-native/safeArea.ts +76 -0
  154. package/src/react-native/scrollView.ts +289 -0
  155. package/src/react-native/styleInterop.ts +8 -0
  156. package/src/react-native/styleSheet.ts +54 -0
  157. package/src/react-native/textInput.ts +334 -0
  158. package/src/react-native/unsupported.ts +25 -0
  159. package/src/renderer/SceneRoot.ts +1006 -0
  160. package/src/scene/SdkDeviceFacts.ts +151 -0
  161. package/src/scene/SdkDeviceServices.ts +72 -0
  162. package/src/scene/SdkSceneGraph.ts +124 -0
  163. package/src/scene/SdkTextEntry.ts +321 -0
  164. package/src/scene/SdkTextMetrics.ts +136 -0
  165. package/src/scene/SdkTextRasterTransport.ts +191 -0
  166. package/src/scene/SdkWebTransport.ts +122 -0
  167. package/src/scene/createRokuHost.ts +213 -0
  168. package/src/scene/sceneGraph.types.ts +38 -0
  169. package/src/services/DeviceServices.ts +53 -0
  170. package/src/services/RegistryStorage.ts +85 -0
  171. package/src/services/RokuKeySession.ts +29 -0
  172. package/src/services/asyncStorage.ts +23 -0
  173. package/src/services/base64.ts +52 -0
  174. package/src/services/crypto.ts +18 -0
  175. package/src/services/expoConstants.ts +11 -0
  176. package/src/services/expoLinking.ts +12 -0
  177. package/src/services/expoStatusBar.ts +4 -0
  178. package/src/services/installWebRuntime.ts +105 -0
  179. package/src/services/intlEnglish.ts +54 -0
  180. package/src/services/languageRuntime.ts +65 -0
  181. package/src/services/nativeTimers.ts +96 -0
  182. package/src/services/secureStore.ts +23 -0
  183. package/src/services/textEntryRequest.ts +22 -0
  184. package/src/services/utf8.ts +57 -0
  185. package/src/services/webRuntime.ts +346 -0
  186. package/src/testing/CanvasResources.ts +141 -0
  187. package/src/testing/CanvasSceneGraph.ts +496 -0
  188. package/src/testing/CanvasTextEntry.ts +237 -0
  189. package/src/testing/PreviewTextObservations.ts +82 -0
  190. package/src/testing/RecordingSceneGraph.ts +136 -0
  191. package/src/types/normalize-colors.d.ts +4 -0
  192. package/src/vector/dynamicSvg.tsx +226 -0
  193. package/src/vector/svg.ts +222 -0
  194. package/src/vector/svg.types.ts +4 -0
  195. package/src/vector/svgElements.ts +58 -0
  196. package/src/vector/svgKey.d.mts +4 -0
  197. package/src/vector/svgKey.mjs +16 -0
  198. package/src/vector/svgPath.ts +157 -0
  199. package/src/vector/svgPixels.ts +133 -0
  200. package/tsconfig.consumer.json +29 -0
@@ -0,0 +1,398 @@
1
+ import Yoga from "yoga-layout-prebuilt";
2
+ import { RokuHostError } from "../scene/sceneGraph.types";
3
+
4
+ export type LayoutStyle = Readonly<Record<string, unknown>>;
5
+ export interface LayoutInput {
6
+ readonly style: LayoutStyle;
7
+ readonly children: readonly LayoutInput[];
8
+ readonly text?: string;
9
+ }
10
+ export interface LayoutRect {
11
+ readonly x: number;
12
+ readonly y: number;
13
+ readonly width: number;
14
+ readonly height: number;
15
+ }
16
+ export type TextMeasure = (
17
+ node: LayoutInput,
18
+ width: number | undefined,
19
+ height: number | undefined,
20
+ ) => { width: number; height: number };
21
+
22
+ const DIMENSIONS = [
23
+ ["width", "setWidth"],
24
+ ["height", "setHeight"],
25
+ ["minWidth", "setMinWidth"],
26
+ ["minHeight", "setMinHeight"],
27
+ ["maxWidth", "setMaxWidth"],
28
+ ["maxHeight", "setMaxHeight"],
29
+ ["flexBasis", "setFlexBasis"],
30
+ ] as const;
31
+ const EDGES = {
32
+ Left: Yoga.EDGE_LEFT,
33
+ Top: Yoga.EDGE_TOP,
34
+ Right: Yoga.EDGE_RIGHT,
35
+ Bottom: Yoga.EDGE_BOTTOM,
36
+ Start: Yoga.EDGE_START,
37
+ End: Yoga.EDGE_END,
38
+ Horizontal: Yoga.EDGE_HORIZONTAL,
39
+ Vertical: Yoga.EDGE_VERTICAL,
40
+ "": Yoga.EDGE_ALL,
41
+ } as const;
42
+ const ALIGNMENTS = {
43
+ auto: Yoga.ALIGN_AUTO,
44
+ "flex-start": Yoga.ALIGN_FLEX_START,
45
+ center: Yoga.ALIGN_CENTER,
46
+ "flex-end": Yoga.ALIGN_FLEX_END,
47
+ stretch: Yoga.ALIGN_STRETCH,
48
+ baseline: Yoga.ALIGN_BASELINE,
49
+ "space-between": Yoga.ALIGN_SPACE_BETWEEN,
50
+ "space-around": Yoga.ALIGN_SPACE_AROUND,
51
+ };
52
+ const DIRECTIONS = {
53
+ row: Yoga.FLEX_DIRECTION_ROW,
54
+ column: Yoga.FLEX_DIRECTION_COLUMN,
55
+ "row-reverse": Yoga.FLEX_DIRECTION_ROW_REVERSE,
56
+ "column-reverse": Yoga.FLEX_DIRECTION_COLUMN_REVERSE,
57
+ };
58
+ const JUSTIFY = {
59
+ "flex-start": Yoga.JUSTIFY_FLEX_START,
60
+ center: Yoga.JUSTIFY_CENTER,
61
+ "flex-end": Yoga.JUSTIFY_FLEX_END,
62
+ "space-between": Yoga.JUSTIFY_SPACE_BETWEEN,
63
+ "space-around": Yoga.JUSTIFY_SPACE_AROUND,
64
+ "space-evenly": Yoga.JUSTIFY_SPACE_EVENLY,
65
+ };
66
+
67
+ function dimension(value: unknown, name: string): number | string {
68
+ if (typeof value === "number" && Number.isFinite(value)) return value;
69
+ if (typeof value === "string" && (value === "auto" || /^-?\d+(?:\.\d+)?%$/.test(value)))
70
+ return value;
71
+ throw new RokuHostError(
72
+ "C03_LAYOUT_VALUE",
73
+ `${name}=${String(value)} is not a supported dimension`,
74
+ );
75
+ }
76
+ function numeric(value: unknown, name: string): number {
77
+ if (typeof value === "number" && Number.isFinite(value)) return value;
78
+ throw new RokuHostError("C03_LAYOUT_VALUE", `${name} must be finite`);
79
+ }
80
+ function enumValue<T extends Record<string, number>>(
81
+ values: T,
82
+ value: unknown,
83
+ name: string,
84
+ ): T[keyof T] {
85
+ if (typeof value === "string" && value in values) return values[value] as T[keyof T];
86
+ throw new RokuHostError("C03_LAYOUT_VALUE", `${name}=${String(value)} is not implemented`);
87
+ }
88
+
89
+ function applyStyle(node: Yoga.YogaNode, style: LayoutStyle): void {
90
+ for (const [name, setter] of DIMENSIONS) {
91
+ const authored = style[name];
92
+ if (authored == null) continue;
93
+ const value = dimension(authored, name);
94
+ if (value === "auto") {
95
+ if (name === "width") node.setWidthAuto();
96
+ else if (name === "height") node.setHeightAuto();
97
+ else if (name !== "flexBasis")
98
+ throw new RokuHostError("C03_LAYOUT_VALUE", `${name} does not admit auto`);
99
+ // Every Yoga node is fresh. Its default flex basis is already auto;
100
+ // the old JS setter accepts numbers/percentages but rejects this token.
101
+ } else node[setter](value);
102
+ }
103
+ node.setFlexDirection(enumValue(DIRECTIONS, style.flexDirection ?? "column", "flexDirection"));
104
+ if (style.flex != null) node.setFlex(numeric(style.flex, "flex"));
105
+ if (style.flexGrow != null) node.setFlexGrow(numeric(style.flexGrow, "flexGrow"));
106
+ if (style.flexShrink != null) node.setFlexShrink(numeric(style.flexShrink, "flexShrink"));
107
+ if (style.aspectRatio != null) node.setAspectRatio(numeric(style.aspectRatio, "aspectRatio"));
108
+ if (style.alignItems != null)
109
+ node.setAlignItems(enumValue(ALIGNMENTS, style.alignItems, "alignItems"));
110
+ if (style.alignSelf != null)
111
+ node.setAlignSelf(enumValue(ALIGNMENTS, style.alignSelf, "alignSelf"));
112
+ if (style.alignContent != null)
113
+ node.setAlignContent(enumValue(ALIGNMENTS, style.alignContent, "alignContent"));
114
+ if (style.justifyContent != null)
115
+ node.setJustifyContent(enumValue(JUSTIFY, style.justifyContent, "justifyContent"));
116
+ node.setFlexWrap(
117
+ enumValue(
118
+ {
119
+ nowrap: Yoga.WRAP_NO_WRAP,
120
+ wrap: Yoga.WRAP_WRAP,
121
+ "wrap-reverse": Yoga.WRAP_WRAP_REVERSE,
122
+ },
123
+ style.flexWrap ?? "nowrap",
124
+ "flexWrap",
125
+ ),
126
+ );
127
+ node.setPositionType(
128
+ enumValue(
129
+ {
130
+ relative: Yoga.POSITION_TYPE_RELATIVE,
131
+ absolute: Yoga.POSITION_TYPE_ABSOLUTE,
132
+ },
133
+ style.position ?? "relative",
134
+ "position",
135
+ ),
136
+ );
137
+ if (style.display != null)
138
+ node.setDisplay(
139
+ enumValue({ flex: Yoga.DISPLAY_FLEX, none: Yoga.DISPLAY_NONE }, style.display, "display"),
140
+ );
141
+ if (style.overflow != null)
142
+ node.setOverflow(
143
+ enumValue(
144
+ {
145
+ visible: Yoga.OVERFLOW_VISIBLE,
146
+ hidden: Yoga.OVERFLOW_HIDDEN,
147
+ scroll: Yoga.OVERFLOW_SCROLL,
148
+ },
149
+ style.overflow,
150
+ "overflow",
151
+ ),
152
+ );
153
+ for (const [suffix, edge] of Object.entries(EDGES)) {
154
+ const margin = style[`margin${suffix}`];
155
+ if (margin != null) {
156
+ const value = dimension(margin, `margin${suffix}`);
157
+ if (value === "auto") node.setMarginAuto(edge);
158
+ else if (typeof value === "string") node.setMarginPercent(edge, Number.parseFloat(value));
159
+ else node.setMargin(edge, value);
160
+ }
161
+ const padding = style[`padding${suffix}`];
162
+ if (padding != null) node.setPadding(edge, dimension(padding, `padding${suffix}`));
163
+ const border = style[`border${suffix}Width`];
164
+ if (border != null) node.setBorder(edge, numeric(border, `border${suffix}Width`));
165
+ }
166
+ // Resolve shorthand precedence independently of object insertion order.
167
+ // Old Yoga otherwise lets its start/end fallback to EDGE_ALL mask an explicit
168
+ // physical side. RN's inline aliases override start/end, while physical
169
+ // top/bottom override the corresponding block aliases.
170
+ const positions = {
171
+ left: style.left ?? style.insetInline ?? style.inset,
172
+ right: style.right ?? style.insetInline ?? style.inset,
173
+ top: style.top ?? style.insetBlockStart ?? style.insetBlock ?? style.inset,
174
+ bottom: style.bottom ?? style.insetBlockEnd ?? style.insetBlock ?? style.inset,
175
+ start: style.insetInlineStart ?? style.start,
176
+ end: style.insetInlineEnd ?? style.end,
177
+ };
178
+ for (const [name, edge] of Object.entries({
179
+ left: Yoga.EDGE_LEFT,
180
+ right: Yoga.EDGE_RIGHT,
181
+ top: Yoga.EDGE_TOP,
182
+ bottom: Yoga.EDGE_BOTTOM,
183
+ start: Yoga.EDGE_START,
184
+ end: Yoga.EDGE_END,
185
+ })) {
186
+ const value = positions[name as keyof typeof positions];
187
+ if (value != null) node.setPosition(edge, dimension(value, name));
188
+ }
189
+ }
190
+
191
+ function trailingMargin(style: LayoutStyle, edge: "Right" | "Left" | "Top" | "Bottom"): number {
192
+ const axis = edge === "Left" || edge === "Right" ? "Horizontal" : "Vertical";
193
+ const value = style[`margin${edge}`] ?? style[`margin${axis}`] ?? style.margin ?? 0;
194
+ return numeric(value, `margin${edge} with gap`);
195
+ }
196
+
197
+ /** Executes asm.js Yoga without assuming Wasm support. Its admitted subset is explicitly tested. */
198
+ export function calculateFlexLayout(
199
+ input: LayoutInput,
200
+ viewport: { width: number; height: number },
201
+ measureText?: TextMeasure,
202
+ retainedChildren?: ReadonlyMap<LayoutInput, unknown>,
203
+ ): Map<LayoutInput, LayoutRect> {
204
+ if (
205
+ !(viewport.width > 0) ||
206
+ !(viewport.height > 0) ||
207
+ !Number.isFinite(viewport.width) ||
208
+ !Number.isFinite(viewport.height)
209
+ ) {
210
+ throw new RokuHostError("C03_VIEWPORT", "Layout requires a finite, positive viewport");
211
+ }
212
+ const nodes = new Map<LayoutInput, Yoga.YogaNode>();
213
+ const config = Yoga.Config.create();
214
+ config.setPointScaleFactor(0);
215
+ const allocations: Yoga.YogaNode[] = [];
216
+ const shimParents = new Map<Yoga.YogaNode, Yoga.YogaNode>();
217
+ const percentages: Array<{
218
+ node: Yoga.YogaNode;
219
+ parent: Yoga.YogaNode;
220
+ name: "width" | "minWidth" | "maxWidth" | "flexBasis";
221
+ ratio: number;
222
+ applied?: number;
223
+ }> = [];
224
+ const build = (entry: LayoutInput): Yoga.YogaNode => {
225
+ const node = Yoga.Node.createWithConfig(config);
226
+ allocations.push(node);
227
+ nodes.set(entry, node);
228
+ applyStyle(node, entry.style);
229
+ if (entry.text != null) {
230
+ if (entry.children.length)
231
+ throw new RokuHostError(
232
+ "C03_TEXT_CHILDREN",
233
+ "Measured text cannot also own layout children",
234
+ );
235
+ if (!measureText)
236
+ throw new RokuHostError("C04_TEXT_METRICS", "A real text-metric adapter must be supplied");
237
+ node.setMeasureFunc((width, widthMode, height, heightMode) => {
238
+ const result = measureText(
239
+ entry,
240
+ widthMode === Yoga.MEASURE_MODE_UNDEFINED ? undefined : width,
241
+ heightMode === Yoga.MEASURE_MODE_UNDEFINED ? undefined : height,
242
+ );
243
+ if (
244
+ !Number.isFinite(result.width) ||
245
+ !Number.isFinite(result.height) ||
246
+ result.width < 0 ||
247
+ result.height < 0
248
+ )
249
+ throw new RokuHostError(
250
+ "C04_TEXT_METRICS",
251
+ "Text measurement returned invalid dimensions",
252
+ );
253
+ return result;
254
+ });
255
+ }
256
+ if (retainedChildren?.has(entry)) return node;
257
+ const relative = entry.children.filter(
258
+ (child) => child.style.position !== "absolute" && child.style.display !== "none",
259
+ );
260
+ const direction = String(entry.style.flexDirection ?? "column");
261
+ const horizontal = direction.startsWith("row");
262
+ const gap = numeric(
263
+ entry.style[horizontal ? "columnGap" : "rowGap"] ?? entry.style.gap ?? 0,
264
+ "gap",
265
+ );
266
+ if (gap < 0) throw new RokuHostError("C03_GAP", "Negative gaps are unsupported");
267
+ const crossGap = numeric(
268
+ entry.style[horizontal ? "rowGap" : "columnGap"] ?? entry.style.gap ?? 0,
269
+ "cross gap",
270
+ );
271
+ if (crossGap < 0) throw new RokuHostError("C03_GAP", "Negative gaps are unsupported");
272
+ const wrappedGap =
273
+ (gap || crossGap) && entry.style.flexWrap != null && entry.style.flexWrap !== "nowrap";
274
+ let content = node;
275
+ if (wrappedGap) {
276
+ if (
277
+ direction !== "row" ||
278
+ entry.style.flexWrap !== "wrap" ||
279
+ (entry.style.alignContent != null && entry.style.alignContent !== "flex-start")
280
+ )
281
+ throw new RokuHostError(
282
+ "C03_WRAP_GAP",
283
+ "This wrapped-gap direction/alignment requires a modern layout adapter",
284
+ );
285
+ // Internal content extends by one trailing gutter. Every real child owns
286
+ // a right/bottom gutter, and the negative outer margins remove the last
287
+ // gutter from the measured size. The public parent keeps its original
288
+ // padding, border, bounds and clip; no product JSX wrapper is introduced.
289
+ content = Yoga.Node.createWithConfig(config);
290
+ allocations.push(content);
291
+ content.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
292
+ content.setFlexWrap(Yoga.WRAP_WRAP);
293
+ content.setAlignSelf(Yoga.ALIGN_STRETCH);
294
+ if (entry.style.alignItems != null)
295
+ content.setAlignItems(enumValue(ALIGNMENTS, entry.style.alignItems, "alignItems"));
296
+ if (entry.style.justifyContent != null)
297
+ content.setJustifyContent(enumValue(JUSTIFY, entry.style.justifyContent, "justifyContent"));
298
+ content.setMargin(Yoga.EDGE_RIGHT, -gap);
299
+ content.setMargin(Yoga.EDGE_BOTTOM, -crossGap);
300
+ node.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN);
301
+ node.setAlignItems(Yoga.ALIGN_STRETCH);
302
+ node.setJustifyContent(Yoga.JUSTIFY_FLEX_START);
303
+ node.insertChild(content, 0);
304
+ }
305
+ const edge = horizontal
306
+ ? direction.endsWith("reverse")
307
+ ? "Left"
308
+ : "Right"
309
+ : direction.endsWith("reverse")
310
+ ? "Top"
311
+ : "Bottom";
312
+ let contentIndex = 0,
313
+ absoluteIndex = wrappedGap ? 1 : 0;
314
+ entry.children.forEach((child, index) => {
315
+ const yogaChild = build(child);
316
+ if (wrappedGap && child.style.position !== "absolute") {
317
+ if (child.style.display !== "none") {
318
+ yogaChild.setMargin(Yoga.EDGE_RIGHT, trailingMargin(child.style, "Right") + gap);
319
+ yogaChild.setMargin(Yoga.EDGE_BOTTOM, trailingMargin(child.style, "Bottom") + crossGap);
320
+ }
321
+ content.insertChild(yogaChild, contentIndex++);
322
+ shimParents.set(yogaChild, content);
323
+ for (const name of ["width", "minWidth", "maxWidth", "flexBasis"] as const) {
324
+ const value = child.style[name];
325
+ if (typeof value === "string" && value.endsWith("%"))
326
+ percentages.push({
327
+ node: yogaChild,
328
+ parent: node,
329
+ name,
330
+ ratio: Number.parseFloat(value) / 100,
331
+ });
332
+ }
333
+ } else if (wrappedGap) node.insertChild(yogaChild, absoluteIndex++);
334
+ else {
335
+ if (gap && relative.includes(child) && child !== relative[relative.length - 1]) {
336
+ yogaChild.setMargin(EDGES[edge], trailingMargin(child.style, edge) + gap);
337
+ }
338
+ node.insertChild(yogaChild, index);
339
+ }
340
+ });
341
+ return node;
342
+ };
343
+ try {
344
+ const root = build(input);
345
+ root.setWidth(viewport.width);
346
+ root.setHeight(viewport.height);
347
+ root.calculateLayout(viewport.width, viewport.height, Yoga.DIRECTION_LTR);
348
+ // Percentages refer to the original content box, not the shim's extra
349
+ // trailing gutter. Resolve those constraints and recalculate to convergence.
350
+ for (let pass = 0; percentages.length; pass++) {
351
+ let changed = false;
352
+ for (const entry of percentages) {
353
+ const width =
354
+ entry.parent.getComputedWidth() -
355
+ entry.parent.getComputedPadding(Yoga.EDGE_LEFT) -
356
+ entry.parent.getComputedPadding(Yoga.EDGE_RIGHT) -
357
+ entry.parent.getComputedBorder(Yoga.EDGE_LEFT) -
358
+ entry.parent.getComputedBorder(Yoga.EDGE_RIGHT);
359
+ const value = Math.max(0, width * entry.ratio);
360
+ if (entry.applied != null && Math.abs(entry.applied - value) < 1e-5) continue;
361
+ entry.applied = value;
362
+ changed = true;
363
+ if (entry.name === "width") entry.node.setWidth(value);
364
+ else if (entry.name === "minWidth") entry.node.setMinWidth(value);
365
+ else if (entry.name === "maxWidth") entry.node.setMaxWidth(value);
366
+ else entry.node.setFlexBasis(value);
367
+ }
368
+ if (!changed) break;
369
+ if (pass >= 3)
370
+ throw new RokuHostError("C03_WRAP_GAP", "Wrapped percentage constraints did not converge");
371
+ root.calculateLayout(viewport.width, viewport.height, Yoga.DIRECTION_LTR);
372
+ }
373
+ return new Map(
374
+ [...nodes].map(([entry, node]) => {
375
+ const layout = node.getComputedLayout();
376
+ const shim = shimParents.get(node)?.getComputedLayout();
377
+ return [
378
+ entry,
379
+ {
380
+ x: layout.left + (shim?.left ?? 0),
381
+ y: layout.top + (shim?.top ?? 0),
382
+ width: layout.width,
383
+ height: layout.height,
384
+ },
385
+ ];
386
+ }),
387
+ );
388
+ } finally {
389
+ // Individual frees also cover partially constructed trees when admission fails.
390
+ for (let index = allocations.length - 1; index >= 0; index--) {
391
+ const node = allocations[index],
392
+ parent = node.getParent();
393
+ if (parent) parent.removeChild(node);
394
+ node.free();
395
+ }
396
+ Yoga.Config.destroy(config);
397
+ }
398
+ }
@@ -0,0 +1,19 @@
1
+ export type FontFamilyGroups = Readonly<
2
+ Record<
3
+ string,
4
+ {
5
+ readonly normal?: Readonly<Record<string, string>>;
6
+ readonly italic?: Readonly<Record<string, string>>;
7
+ }
8
+ >
9
+ >;
10
+ export declare function prepareFontFamilies(
11
+ assets: Readonly<Record<string, string>>,
12
+ families?: FontFamilyGroups,
13
+ ): FontFamilyGroups;
14
+ export declare function fontFaceFamily(
15
+ family: string,
16
+ style: { readonly fontWeight?: unknown; readonly fontStyle?: unknown },
17
+ assets: Readonly<Record<string, string>>,
18
+ families: FontFamilyGroups,
19
+ ): string;
@@ -0,0 +1,85 @@
1
+ const record = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
2
+ const fail = (code, message) => {
3
+ throw Error(`${code}: ${message}`);
4
+ };
5
+
6
+ /** Explicit physical assets, never filename conventions or synthetic styling. */
7
+ export function prepareFontFamilies(assets, families = {}) {
8
+ if (!record(families) || Object.keys(families).length > 64)
9
+ fail("C04_FONT_FACE_CONFIG", "Font families require a bounded declaration map");
10
+ const result = Object.create(null);
11
+ for (const [name, group] of Object.entries(families)) {
12
+ if (
13
+ !name.trim() ||
14
+ Object.hasOwn(assets, name) ||
15
+ !record(group) ||
16
+ !Object.keys(group).length ||
17
+ Object.keys(group).some((key) => !["normal", "italic"].includes(key))
18
+ )
19
+ fail(
20
+ "C04_FONT_FACE_CONFIG",
21
+ "A logical font family must have unique normal/italic face declarations",
22
+ );
23
+ const copied = Object.create(null);
24
+ for (const [style, weights] of Object.entries(group)) {
25
+ if (!record(weights) || !Object.keys(weights).length || Object.keys(weights).length > 9)
26
+ fail("C04_FONT_FACE_CONFIG", "A font style needs explicit static weight faces");
27
+ const faces = Object.create(null);
28
+ for (const [weight, family] of Object.entries(weights)) {
29
+ if (
30
+ !/^[1-9]00$/.test(weight) ||
31
+ typeof family !== "string" ||
32
+ !Object.hasOwn(assets, family)
33
+ )
34
+ fail(
35
+ "C04_FONT_FACE_CONFIG",
36
+ "Each static weight must name a configured physical font asset",
37
+ );
38
+ faces[weight] = family;
39
+ }
40
+ copied[style] = Object.freeze(faces);
41
+ }
42
+ result[name] = Object.freeze(copied);
43
+ }
44
+ return Object.freeze(result);
45
+ }
46
+
47
+ export function fontFaceFamily(family, style, assets, families) {
48
+ const authored = style.fontWeight ?? "normal";
49
+ const weight = authored === "normal" ? "400" : authored === "bold" ? "700" : String(authored);
50
+ if (!/^[1-9]00$/.test(weight))
51
+ fail("C04_FONT_WEIGHT", "Static font weights must be normal, bold or 100–900 in steps of 100");
52
+ const slant = style.fontStyle ?? "normal";
53
+ if (slant !== "normal" && slant !== "italic")
54
+ fail("C04_FONT_STYLE", "Only configured normal and italic faces are admitted");
55
+ if (Object.hasOwn(families, family)) {
56
+ const selected = families[family][slant]?.[weight];
57
+ if (!selected) fail("C04_FONT_FACE", `No configured ${slant} ${weight} face for ${family}`);
58
+ return selected;
59
+ }
60
+ if (!Object.hasOwn(assets, family))
61
+ fail("C04_FONT_ASSET", `No packaged font asset for ${family}`);
62
+ // Named physical families remain usable by shared controls that deliberately
63
+ // normalize weight to 400 after selecting their own concrete face.
64
+ if (slant === "normal" && weight === "400") return family;
65
+ for (const group of Object.values(families)) {
66
+ const faces = group[slant] ?? {};
67
+ if (faces[weight] === family || (weight === "400" && Object.values(faces).includes(family)))
68
+ return family;
69
+ }
70
+ // A control may select a physical weight first, then apply italic styling
71
+ // while normalizing fontWeight to 400. Preserve that declared physical weight
72
+ // when choosing its corresponding real italic file.
73
+ const matches = new Set();
74
+ if (slant === "italic")
75
+ for (const group of Object.values(families))
76
+ for (const [physicalWeight, asset] of Object.entries(group.normal ?? {}))
77
+ if (asset === family && (weight === "400" || weight === physicalWeight)) {
78
+ const selected = group.italic?.[physicalWeight];
79
+ if (selected) matches.add(selected);
80
+ }
81
+ if (matches.size === 1) return matches.values().next().value;
82
+ if (matches.size > 1)
83
+ fail("C04_FONT_FACE", `Physical family ${family} has ambiguous italic selections`);
84
+ fail("C04_FONT_FACE", `Physical family ${family} has no configured ${slant} ${weight} selection`);
85
+ }
@@ -0,0 +1,2 @@
1
+ /** Stable font asset key for the admitted RN fontVariant subset. */
2
+ export declare function fontVariantFamily(family: string, variants?: unknown): string;
@@ -0,0 +1,8 @@
1
+ /** Shared asset identity for runtime selection and build-time face generation.
2
+ * A variant needs an actual font asset; no native font feature field is assumed. */
3
+ export function fontVariantFamily(family, variants) {
4
+ if (variants == null) return family;
5
+ if (!Array.isArray(variants) || variants.some((value) => value !== "tabular-nums"))
6
+ throw Error("C04_FONT_VARIANT: Only the configured tabular-nums face is implemented");
7
+ return variants.length ? `${family}::tabular-nums` : family;
8
+ }
@@ -0,0 +1,158 @@
1
+ import type { InlineTextRun } from "./inlineText";
2
+ import { layoutText, type TextMetricProvider } from "./textLayout";
3
+ import { RokuHostError } from "../scene/sceneGraph.types";
4
+
5
+ export interface InlineTextLine {
6
+ readonly runs: readonly InlineTextRun[];
7
+ readonly width: number;
8
+ readonly height: number;
9
+ readonly paintHeight: number;
10
+ readonly y: number;
11
+ }
12
+
13
+ /** Attributed line breaks use whole-run font measurements, retaining kerning
14
+ * inside each styled run. Native styled labels own shaping within each line. */
15
+ export function layoutInlineParagraph(
16
+ source: readonly InlineTextRun[],
17
+ provider: TextMetricProvider,
18
+ maximumWidth = Infinity,
19
+ numberOfLines = 0,
20
+ ) {
21
+ if (source.length > 64)
22
+ throw new RokuHostError("C04_INLINE_RUNS", "Mixed text supports at most 64 runs");
23
+ if (maximumWidth < 0 || Number.isNaN(maximumWidth))
24
+ throw new RokuHostError("C04_TEXT_METRICS", "Mixed text requires a valid width");
25
+ if (!Number.isInteger(numberOfLines) || numberOfLines < 0)
26
+ throw new RokuHostError("C04_TEXT_LINES", "numberOfLines must be a nonnegative integer");
27
+ if (!source.length) return { lines: [], width: 0, height: 0, truncated: false };
28
+ const runs = source.map((run) => {
29
+ if (
30
+ Number(run.style.letterSpacing ?? 0) ||
31
+ run.style.textShadowColor != null ||
32
+ run.style.textShadowRadius != null
33
+ )
34
+ throw new RokuHostError(
35
+ "C04_INLINE_RASTER",
36
+ "Tracked or shadowed mixed text requires the multi-run raster adapter",
37
+ );
38
+ const metrics = provider.metrics(run.style);
39
+ return { ...run, text: layoutText(run.text, run.style, metrics).text };
40
+ });
41
+ const metrics = new Map(runs.map((run) => [run.style, provider.metrics(run.style)]));
42
+ const points = runs.flatMap((run) =>
43
+ Array.from(run.text, (value) => ({ value, style: run.style })),
44
+ );
45
+ if (points.length > 32768)
46
+ throw new RokuHostError("C04_INLINE_SIZE", "A mixed paragraph exceeds 32768 code points");
47
+ const fits = (width: number) => Math.fround(width) <= Math.fround(maximumWidth);
48
+ const widthOf = (fragments: readonly InlineTextRun[]) =>
49
+ fragments.reduce((sum, run) => {
50
+ const width = metrics.get(run.style)!.width(run.text);
51
+ if (!Number.isFinite(width) || width < 0)
52
+ throw new RokuHostError("C04_FONT_METRICS", "Mixed text received an invalid font width");
53
+ return sum + width;
54
+ }, 0);
55
+ type Fragment = { text: string; style: InlineTextRun["style"] };
56
+ const fragments = (start: number, end: number): Fragment[] => {
57
+ const result: { text: string; style: InlineTextRun["style"] }[] = [];
58
+ for (let index = start; index < end; index++) {
59
+ const point = points[index],
60
+ previous = result.at(-1);
61
+ if (previous?.style === point.style) previous.text += point.value;
62
+ else result.push({ text: point.value, style: point.style });
63
+ }
64
+ return result;
65
+ };
66
+ const raw: Fragment[][] = [];
67
+ const add = (start: number, end: number) => {
68
+ if (raw.length >= 512)
69
+ throw new RokuHostError("C04_INLINE_LINES", "A mixed paragraph exceeds 512 native lines");
70
+ raw.push(fragments(start, end));
71
+ };
72
+ if (maximumWidth === 0) raw.push([]);
73
+ else if (numberOfLines === 1) {
74
+ raw.push(runs.map((run) => ({ ...run, text: run.text.replaceAll("\n", " ") })));
75
+ } else {
76
+ let start = 0;
77
+ for (let paragraphEnd = 0; paragraphEnd <= points.length; paragraphEnd++) {
78
+ if (paragraphEnd < points.length && points[paragraphEnd].value !== "\n") continue;
79
+ if (start === paragraphEnd) add(start, start);
80
+ while (start < paragraphEnd) {
81
+ if (fits(widthOf(fragments(start, paragraphEnd)))) {
82
+ add(start, paragraphEnd);
83
+ break;
84
+ }
85
+ let low = 0,
86
+ high = paragraphEnd - start;
87
+ while (low < high) {
88
+ const middle = Math.ceil((low + high) / 2);
89
+ if (fits(widthOf(fragments(start, start + middle)))) low = middle;
90
+ else high = middle - 1;
91
+ }
92
+ // Consume a code point even when no glyph fits, so a narrow constraint
93
+ // cannot stall layout. The one-line native leaf clips that glyph.
94
+ let end = start + Math.max(1, low);
95
+ if (low && !/\s/u.test(points[end]?.value ?? "")) {
96
+ for (let index = end - 1; index > start; index--) {
97
+ if (/\s/u.test(points[index].value)) {
98
+ end = index;
99
+ break;
100
+ }
101
+ if (points[index].value === "-") {
102
+ end = index + 1;
103
+ break;
104
+ }
105
+ }
106
+ }
107
+ let visibleEnd = end;
108
+ while (visibleEnd > start && /\s/u.test(points[visibleEnd - 1].value)) visibleEnd--;
109
+ add(start, visibleEnd);
110
+ start = end;
111
+ while (start < paragraphEnd && /\s/u.test(points[start].value)) start++;
112
+ }
113
+ start = paragraphEnd + 1;
114
+ }
115
+ }
116
+ const truncated =
117
+ (numberOfLines > 0 && raw.length > numberOfLines) ||
118
+ (numberOfLines === 1 && !fits(widthOf(raw[0])));
119
+ if (truncated) {
120
+ raw.splice(numberOfLines);
121
+ const last = raw.at(-1)!;
122
+ while (last.length) {
123
+ const tail = last.at(-1)!;
124
+ tail.text = tail.text.trimEnd();
125
+ if (tail.text) break;
126
+ last.pop();
127
+ }
128
+ while (true) {
129
+ const style = last.at(-1)?.style ?? runs[0].style;
130
+ const ellipsis = { text: "…", style };
131
+ if (fits(widthOf([...last, ellipsis]))) {
132
+ last.push(ellipsis);
133
+ break;
134
+ }
135
+ const tail = last.at(-1);
136
+ if (!tail) break;
137
+ tail.text = Array.from(tail.text).slice(0, -1).join("");
138
+ if (!tail.text) last.pop();
139
+ }
140
+ }
141
+ let y = 0;
142
+ const lines: InlineTextLine[] = raw.map((fragments) => {
143
+ const styles = fragments.length ? fragments.map((run) => run.style) : [runs[0].style];
144
+ const height = Math.max(
145
+ ...styles.map((style) => Number(style.lineHeight ?? metrics.get(style)!.lineHeight)),
146
+ );
147
+ const paintHeight = Math.max(height, ...styles.map((style) => metrics.get(style)!.lineHeight));
148
+ const line = { runs: fragments, width: widthOf(fragments), height, paintHeight, y };
149
+ y += height;
150
+ return line;
151
+ });
152
+ return {
153
+ lines,
154
+ width: Math.min(maximumWidth, Math.max(0, ...lines.map((line) => line.width))),
155
+ height: y,
156
+ truncated,
157
+ };
158
+ }