react-native-roku 0.0.0-stage → 0.2.0-preview.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +328 -0
- package/CONTRIBUTING.md +34 -0
- package/LICENSE +21 -0
- package/NOTICE.md +10 -0
- package/README.md +66 -2
- package/bin/react-native-roku.mjs +91 -0
- package/build/assets.cjs +311 -0
- package/build/assets.d.cts +50 -0
- package/build/compatibility.d.mts +40 -0
- package/build/compatibility.mjs +272 -0
- package/build/dev.d.mts +34 -0
- package/build/dev.mjs +297 -0
- package/build/devWorkspace.d.mts +11 -0
- package/build/devWorkspace.mjs +86 -0
- package/build/device.d.mts +47 -0
- package/build/device.mjs +310 -0
- package/build/environment.d.mts +29 -0
- package/build/environment.mjs +144 -0
- package/build/esbuild.d.mts +5 -0
- package/build/esbuild.mjs +118 -0
- package/build/fontVariants.d.mts +27 -0
- package/build/fontVariants.mjs +424 -0
- package/build/frontend.d.mts +52 -0
- package/build/frontend.mjs +75 -0
- package/build/identity.mjs +45 -0
- package/build/index.d.mts +22 -0
- package/build/index.mjs +87 -0
- package/build/intl.mjs +121 -0
- package/build/metro.cjs +38 -0
- package/build/metro.d.cts +37 -0
- package/build/metroFrontend.mjs +222 -0
- package/build/metroMetadata.mjs +119 -0
- package/build/metroModules.mjs +397 -0
- package/build/metroRefresh.d.mts +64 -0
- package/build/metroRefresh.mjs +346 -0
- package/build/metroTransformer.cjs +146 -0
- package/build/nativeTextMemo.mjs +296 -0
- package/build/nativewind.d.mts +9 -0
- package/build/nativewind.mjs +92 -0
- package/build/project.d.mts +111 -0
- package/build/project.mjs +615 -0
- package/build/reactCompiler.mjs +113 -0
- package/build/reactProfile.cjs +35 -0
- package/build/refreshRuntime.mjs +187 -0
- package/build/refreshSourceMap.mjs +53 -0
- package/build/release.mjs +99 -0
- package/build/resolution.d.mts +3 -0
- package/build/resolution.mjs +25 -0
- package/build/surfaces.d.mts +1 -0
- package/build/surfaces.mjs +42 -0
- package/build/testing/node.d.mts +29 -0
- package/build/testing/node.mjs +106 -0
- package/build/yoga.d.mts +8 -0
- package/build/yoga.mjs +186 -0
- package/compatibility/public-api.json +327 -0
- package/compatibility/registry.json +2258 -0
- package/config/release.json +15 -0
- package/config/toolchain.json +34 -0
- package/docs/compatibility.md +813 -0
- package/docs/getting-started.md +460 -0
- package/docs/maintenance.md +368 -0
- package/docs/qualification.md +241 -0
- package/docs/standalone-testing.md +56 -0
- package/examples/hello-roku/README.md +35 -0
- package/examples/hello-roku/package.template.json +31 -0
- package/examples/hello-roku/pnpm-workspace.yaml +11 -0
- package/examples/hello-roku/roku.config.json +22 -0
- package/examples/hello-roku/src/App.tsx +54 -0
- package/examples/hello-roku/src/Catalog.tsx +200 -0
- package/examples/hello-roku/src/assets.d.ts +16 -0
- package/examples/hello-roku/src/catalog-cover.png +0 -0
- package/examples/hello-roku/tsconfig.json +1 -0
- package/examples/hello-roku/tsconfig.react.json +1 -0
- package/native/components/RNRAppControl.brs +215 -0
- package/native/components/RNRAppControl.xml +13 -0
- package/native/components/RNRDeviceFacts.brs +19 -0
- package/native/components/RNRDeviceFacts.xml +9 -0
- package/native/components/RNRDeviceFactsTask.brs +74 -0
- package/native/components/RNRDeviceFactsTask.xml +9 -0
- package/native/components/RNRTextRaster.brs +15 -0
- package/native/components/RNRTextRaster.xml +9 -0
- package/native/components/RNRTextRasterTask.brs +77 -0
- package/native/components/RNRTextRasterTask.xml +9 -0
- package/native/manifest.json +7 -0
- package/package.json +183 -3
- package/src/animation/clock.ts +10 -0
- package/src/animation/derivedValue.ts +163 -0
- package/src/animation/legacyAnimated.ts +255 -0
- package/src/animation/reanimated.ts +243 -0
- package/src/animation/sharedValue.ts +285 -0
- package/src/animation/worklets.ts +23 -0
- package/src/application/AppRegistry.ts +31 -0
- package/src/application/mountApplication.ts +136 -0
- package/src/graphics/BitmapLayer.tsx +68 -0
- package/src/graphics/RoundedBorder.tsx +116 -0
- package/src/graphics/TextRasterCache.ts +259 -0
- package/src/graphics/gradient.ts +37 -0
- package/src/graphics/inlineRaster.ts +85 -0
- package/src/graphics/png.ts +77 -0
- package/src/graphics/rasterPixels.ts +89 -0
- package/src/graphics/surfaces.ts +105 -0
- package/src/graphics/textRaster.types.ts +60 -0
- package/src/graphics/textRasterGeometry.ts +116 -0
- package/src/host/host.types.ts +57 -0
- package/src/index.ts +22 -0
- package/src/interop/nativewind.ts +2 -0
- package/src/layout/FontResources.ts +252 -0
- package/src/layout/affine.ts +89 -0
- package/src/layout/flexLayout.ts +398 -0
- package/src/layout/fontFaces.d.mts +19 -0
- package/src/layout/fontFaces.mjs +85 -0
- package/src/layout/fontVariants.d.mts +2 -0
- package/src/layout/fontVariants.mjs +8 -0
- package/src/layout/inlineParagraph.ts +158 -0
- package/src/layout/inlineText.ts +68 -0
- package/src/layout/layoutReuse.ts +117 -0
- package/src/layout/nativeNodeFields.ts +179 -0
- package/src/layout/preparedFields.ts +78 -0
- package/src/layout/textLayout.ts +138 -0
- package/src/layout/transform.ts +113 -0
- package/src/media/SceneGraphAudio.ts +161 -0
- package/src/media/SceneGraphVideo.ts +167 -0
- package/src/media/audioSession.ts +84 -0
- package/src/media/index.ts +2 -0
- package/src/media/videoErrorDetails.ts +40 -0
- package/src/media/videoSession.ts +136 -0
- package/src/navigation/RouterStore.ts +244 -0
- package/src/navigation/expoRouter.ts +115 -0
- package/src/react-native/NativeInput.ts +494 -0
- package/src/react-native/NativeSystemState.ts +95 -0
- package/src/react-native/accessibility.ts +265 -0
- package/src/react-native/activityIndicator.ts +72 -0
- package/src/react-native/compilerPressable.ts +26 -0
- package/src/react-native/createNativeRoot.ts +2126 -0
- package/src/react-native/directionalFocus.ts +55 -0
- package/src/react-native/environment.ts +275 -0
- package/src/react-native/flatList.ts +1001 -0
- package/src/react-native/flatListViewability.ts +231 -0
- package/src/react-native/focusEvents.ts +85 -0
- package/src/react-native/image.ts +247 -0
- package/src/react-native/index.ts +54 -0
- package/src/react-native/interactionManager.ts +76 -0
- package/src/react-native/keyboard.ts +60 -0
- package/src/react-native/linearGradient.ts +94 -0
- package/src/react-native/modal.tsx +148 -0
- package/src/react-native/nativeHostContext.ts +80 -0
- package/src/react-native/nativeModules.ts +86 -0
- package/src/react-native/nativeRoot.types.ts +22 -0
- package/src/react-native/nativeViewHandle.ts +191 -0
- package/src/react-native/nativewindPressable.ts +38 -0
- package/src/react-native/panResponder.ts +73 -0
- package/src/react-native/primitives.ts +685 -0
- package/src/react-native/safeArea.ts +76 -0
- package/src/react-native/scrollView.ts +289 -0
- package/src/react-native/styleInterop.ts +8 -0
- package/src/react-native/styleSheet.ts +54 -0
- package/src/react-native/textInput.ts +334 -0
- package/src/react-native/unsupported.ts +25 -0
- package/src/renderer/SceneRoot.ts +1006 -0
- package/src/scene/SdkDeviceFacts.ts +151 -0
- package/src/scene/SdkDeviceServices.ts +72 -0
- package/src/scene/SdkSceneGraph.ts +124 -0
- package/src/scene/SdkTextEntry.ts +321 -0
- package/src/scene/SdkTextMetrics.ts +136 -0
- package/src/scene/SdkTextRasterTransport.ts +191 -0
- package/src/scene/SdkWebTransport.ts +122 -0
- package/src/scene/createRokuHost.ts +213 -0
- package/src/scene/sceneGraph.types.ts +38 -0
- package/src/services/DeviceServices.ts +53 -0
- package/src/services/RegistryStorage.ts +85 -0
- package/src/services/RokuKeySession.ts +29 -0
- package/src/services/asyncStorage.ts +23 -0
- package/src/services/base64.ts +52 -0
- package/src/services/crypto.ts +18 -0
- package/src/services/expoConstants.ts +11 -0
- package/src/services/expoLinking.ts +12 -0
- package/src/services/expoStatusBar.ts +4 -0
- package/src/services/installWebRuntime.ts +105 -0
- package/src/services/intlEnglish.ts +54 -0
- package/src/services/languageRuntime.ts +65 -0
- package/src/services/nativeTimers.ts +96 -0
- package/src/services/secureStore.ts +23 -0
- package/src/services/textEntryRequest.ts +22 -0
- package/src/services/utf8.ts +57 -0
- package/src/services/webRuntime.ts +346 -0
- package/src/testing/CanvasResources.ts +141 -0
- package/src/testing/CanvasSceneGraph.ts +496 -0
- package/src/testing/CanvasTextEntry.ts +237 -0
- package/src/testing/PreviewTextObservations.ts +82 -0
- package/src/testing/RecordingSceneGraph.ts +136 -0
- package/src/types/normalize-colors.d.ts +4 -0
- package/src/vector/dynamicSvg.tsx +226 -0
- package/src/vector/svg.ts +222 -0
- package/src/vector/svg.types.ts +4 -0
- package/src/vector/svgElements.ts +58 -0
- package/src/vector/svgKey.d.mts +4 -0
- package/src/vector/svgKey.mjs +16 -0
- package/src/vector/svgPath.ts +157 -0
- package/src/vector/svgPixels.ts +133 -0
- package/tsconfig.consumer.json +29 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { LayoutStyle } from "../layout/flexLayout";
|
|
2
|
+
import type { TextLayout, TextMetrics } from "../layout/textLayout";
|
|
3
|
+
export interface TextRasterRequest {
|
|
4
|
+
readonly style: LayoutStyle;
|
|
5
|
+
readonly layout: TextLayout;
|
|
6
|
+
readonly metrics: TextMetrics;
|
|
7
|
+
readonly width: number;
|
|
8
|
+
readonly height: number;
|
|
9
|
+
}
|
|
10
|
+
export interface TextRasterImage {
|
|
11
|
+
readonly uri: string;
|
|
12
|
+
readonly width: number;
|
|
13
|
+
readonly height: number;
|
|
14
|
+
readonly inset: number;
|
|
15
|
+
}
|
|
16
|
+
export interface TextRasterLease {
|
|
17
|
+
readonly image: TextRasterImage;
|
|
18
|
+
release(): void;
|
|
19
|
+
}
|
|
20
|
+
export interface InlineRasterRequest {
|
|
21
|
+
readonly runs: readonly {
|
|
22
|
+
text: string;
|
|
23
|
+
style: LayoutStyle;
|
|
24
|
+
metrics: TextMetrics;
|
|
25
|
+
}[];
|
|
26
|
+
readonly width: number;
|
|
27
|
+
readonly height: number;
|
|
28
|
+
}
|
|
29
|
+
export interface TextRasterProvider {
|
|
30
|
+
acquire(request: TextRasterRequest): Promise<TextRasterLease>;
|
|
31
|
+
acquireInline?(request: InlineRasterRequest): Promise<TextRasterLease>;
|
|
32
|
+
/** Portable RGBA graphics share the same bounded native bitmap/file owner. */
|
|
33
|
+
acquirePixels?(request: {
|
|
34
|
+
key: string;
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
pixels: Uint8Array | (() => Uint8Array);
|
|
38
|
+
}): Promise<TextRasterLease>;
|
|
39
|
+
dispose(): void;
|
|
40
|
+
}
|
|
41
|
+
export interface TextDrawCommand {
|
|
42
|
+
readonly text: string;
|
|
43
|
+
readonly x: number;
|
|
44
|
+
readonly y: number;
|
|
45
|
+
}
|
|
46
|
+
export interface TextRasterSpec {
|
|
47
|
+
readonly width: number;
|
|
48
|
+
readonly height: number;
|
|
49
|
+
readonly inset: number;
|
|
50
|
+
readonly scale: number;
|
|
51
|
+
readonly fontFamily: string;
|
|
52
|
+
readonly fontSize: number;
|
|
53
|
+
readonly lineHeight: number;
|
|
54
|
+
readonly commands: readonly TextDrawCommand[];
|
|
55
|
+
readonly color: string;
|
|
56
|
+
readonly shadowColor: string;
|
|
57
|
+
readonly shadowX: number;
|
|
58
|
+
readonly shadowY: number;
|
|
59
|
+
readonly shadowRadius: number;
|
|
60
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
2
|
+
import { rokuColor } from "../react-native/styleSheet";
|
|
3
|
+
import type { TextRasterRequest, TextRasterSpec, TextDrawCommand } from "./textRaster.types";
|
|
4
|
+
export function textRasterSpec(request: TextRasterRequest): TextRasterSpec {
|
|
5
|
+
const { style, layout, metrics } = request,
|
|
6
|
+
spacing = Number(style.letterSpacing ?? 0),
|
|
7
|
+
size = Number(style.fontSize ?? 14);
|
|
8
|
+
const shadow = style.textShadowOffset as { width?: number; height?: number } | undefined;
|
|
9
|
+
const radius = Number(style.textShadowRadius ?? 0),
|
|
10
|
+
sx = Number(shadow?.width ?? 0),
|
|
11
|
+
sy = Number(shadow?.height ?? 0);
|
|
12
|
+
if (
|
|
13
|
+
![spacing, size, radius, sx, sy, request.width, request.height].every(Number.isFinite) ||
|
|
14
|
+
size <= 0 ||
|
|
15
|
+
radius < 0 ||
|
|
16
|
+
request.width < 0 ||
|
|
17
|
+
request.height < 0
|
|
18
|
+
)
|
|
19
|
+
throw new RokuHostError(
|
|
20
|
+
"C04_RASTER_GEOMETRY",
|
|
21
|
+
"Text raster geometry must be finite and nonnegative",
|
|
22
|
+
);
|
|
23
|
+
if (radius > 32)
|
|
24
|
+
throw new RokuHostError(
|
|
25
|
+
"C04_TEXT_SHADOW",
|
|
26
|
+
"Text shadow radii above 32px exceed the beta raster budget",
|
|
27
|
+
);
|
|
28
|
+
const inset = Math.ceil(radius * 1.5 + Math.max(Math.abs(sx), Math.abs(sy)) + 2);
|
|
29
|
+
const logicalWidth = Math.max(1, Math.ceil(request.width) + inset * 2),
|
|
30
|
+
logicalHeight = Math.max(1, Math.ceil(request.height) + inset * 2);
|
|
31
|
+
if (logicalWidth > 2048 || logicalHeight > 2048)
|
|
32
|
+
throw new RokuHostError(
|
|
33
|
+
"C04_RASTER_SIZE",
|
|
34
|
+
"A text raster exceeds the native 2048px bitmap limit",
|
|
35
|
+
);
|
|
36
|
+
const maximumScale = 2048 / Math.max(logicalWidth, logicalHeight);
|
|
37
|
+
const fontSize = Math.max(
|
|
38
|
+
1,
|
|
39
|
+
Math.min(512, Math.floor(size * maximumScale), Math.round(size * Math.min(2, maximumScale))),
|
|
40
|
+
);
|
|
41
|
+
// The native bitmap font is integral. A fractional bitmap scale preserves
|
|
42
|
+
// the exact authored logical font size without rounding the product style.
|
|
43
|
+
const scale = fontSize / size;
|
|
44
|
+
if (scale > maximumScale)
|
|
45
|
+
throw new RokuHostError(
|
|
46
|
+
"C04_RASTER_FONT_SIZE",
|
|
47
|
+
"The native font cannot fit this bitmap budget",
|
|
48
|
+
);
|
|
49
|
+
const lineHeight = Number(style.lineHeight ?? metrics.lineHeight),
|
|
50
|
+
commands: TextDrawCommand[] = [];
|
|
51
|
+
for (const [index, line] of layout.lines.entries()) {
|
|
52
|
+
const width = metrics.width(line) + Math.max(0, Array.from(line).length - 1) * spacing;
|
|
53
|
+
const align = style.textAlign ?? "left";
|
|
54
|
+
if (!["left", "auto", "center", "right"].includes(String(align)))
|
|
55
|
+
throw new RokuHostError("C04_TEXT_ALIGN", "Justified text needs a shaping adapter");
|
|
56
|
+
const x =
|
|
57
|
+
inset +
|
|
58
|
+
(align === "center"
|
|
59
|
+
? (request.width - width) / 2
|
|
60
|
+
: align === "right"
|
|
61
|
+
? request.width - width
|
|
62
|
+
: 0),
|
|
63
|
+
y = inset + index * lineHeight;
|
|
64
|
+
if (!spacing) {
|
|
65
|
+
commands.push({ text: line, x: x * scale, y: y * scale });
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
// Whole runs retain native shaping. Tracked runs currently admit Latin and
|
|
69
|
+
// punctuation and independent rating stars; complex shaping must never
|
|
70
|
+
// become isolated codepoints.
|
|
71
|
+
const unsupported = Array.from(line).find((char) => {
|
|
72
|
+
const point = char.codePointAt(0)!;
|
|
73
|
+
return (
|
|
74
|
+
point > 0x024f &&
|
|
75
|
+
!(point >= 0x2000 && point <= 0x206f) &&
|
|
76
|
+
point !== 0x2605 &&
|
|
77
|
+
point !== 0x2606
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
if (unsupported)
|
|
81
|
+
throw new RokuHostError(
|
|
82
|
+
"C04_TRACKED_SCRIPT",
|
|
83
|
+
`Tracked U+${unsupported.codePointAt(0)!.toString(16).toUpperCase()} requires grapheme and script shaping`,
|
|
84
|
+
);
|
|
85
|
+
const points = Array.from(line);
|
|
86
|
+
if (points.some((char) => metrics.width(char) + spacing < 0))
|
|
87
|
+
throw new RokuHostError("C04_LETTER_SPACING", "Tracking cannot reverse a glyph advance");
|
|
88
|
+
points.forEach((char, position) =>
|
|
89
|
+
commands.push({
|
|
90
|
+
text: char,
|
|
91
|
+
x:
|
|
92
|
+
(x +
|
|
93
|
+
metrics.width(points.slice(0, position + 1).join("")) -
|
|
94
|
+
metrics.width(char) +
|
|
95
|
+
position * spacing) *
|
|
96
|
+
scale,
|
|
97
|
+
y: y * scale,
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
width: Math.ceil(logicalWidth * scale),
|
|
103
|
+
height: Math.ceil(logicalHeight * scale),
|
|
104
|
+
inset,
|
|
105
|
+
scale,
|
|
106
|
+
fontFamily: String(style.fontFamily ?? "RokuDefault"),
|
|
107
|
+
fontSize,
|
|
108
|
+
lineHeight: metrics.lineHeight * scale,
|
|
109
|
+
commands,
|
|
110
|
+
color: rokuColor(style.color ?? "white"),
|
|
111
|
+
shadowColor: rokuColor(style.textShadowColor ?? "transparent"),
|
|
112
|
+
shadowX: sx * scale,
|
|
113
|
+
shadowY: sy * scale,
|
|
114
|
+
shadowRadius: radius * scale,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { NativeRootOptions } from "../react-native/nativeRoot.types";
|
|
2
|
+
import type { SceneGraphPort } from "../scene/sceneGraph.types";
|
|
3
|
+
import type { TextMetricProvider } from "../layout/textLayout";
|
|
4
|
+
import type { TextRasterProvider } from "../graphics/textRaster.types";
|
|
5
|
+
import type { NativeSystemState, SystemSnapshot } from "../react-native/NativeSystemState";
|
|
6
|
+
import type { NativeRemoteEvent } from "../react-native/NativeInput";
|
|
7
|
+
import type { FontResourcesOptions } from "../layout/FontResources";
|
|
8
|
+
import type { WebTransport } from "../services/webRuntime";
|
|
9
|
+
export interface RokuDeviceSnapshot {
|
|
10
|
+
readonly model: string;
|
|
11
|
+
readonly osVersion: {
|
|
12
|
+
readonly major: string;
|
|
13
|
+
readonly minor: string;
|
|
14
|
+
readonly revision: string;
|
|
15
|
+
readonly build: string;
|
|
16
|
+
};
|
|
17
|
+
readonly graphicsPlatform: string;
|
|
18
|
+
}
|
|
19
|
+
export type RokuHostPhase = "startup" | "device-facts" | "memory" | "dispose";
|
|
20
|
+
export interface MountedRokuApplication {
|
|
21
|
+
remote(event: NativeRemoteEvent): boolean;
|
|
22
|
+
publishSystem(snapshot: Partial<SystemSnapshot>): void;
|
|
23
|
+
whenCommitted(): Promise<void>;
|
|
24
|
+
dispose(): void | Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
export interface RokuHostContext extends NativeRootOptions {
|
|
27
|
+
readonly scene: SceneGraphPort;
|
|
28
|
+
readonly system: NativeSystemState;
|
|
29
|
+
readonly text: TextMetricProvider;
|
|
30
|
+
readonly textRaster: TextRasterProvider;
|
|
31
|
+
readonly webTransport: WebTransport;
|
|
32
|
+
readonly requestExit: () => void;
|
|
33
|
+
}
|
|
34
|
+
export interface RokuMemorySnapshot {
|
|
35
|
+
readonly availableKb: unknown;
|
|
36
|
+
readonly usagePercent: unknown;
|
|
37
|
+
readonly limits: unknown;
|
|
38
|
+
readonly hermesHeap: unknown;
|
|
39
|
+
}
|
|
40
|
+
export interface RokuHostOptions {
|
|
41
|
+
readonly components: {
|
|
42
|
+
readonly AppControl: string;
|
|
43
|
+
readonly TextRaster: string;
|
|
44
|
+
readonly TextRasterTask: string;
|
|
45
|
+
readonly DeviceFacts: string;
|
|
46
|
+
readonly DeviceFactsTask: string;
|
|
47
|
+
};
|
|
48
|
+
readonly fonts: FontResourcesOptions;
|
|
49
|
+
readonly viewport: { readonly width: number; readonly height: number };
|
|
50
|
+
readonly storageNamespace?: string;
|
|
51
|
+
readonly svgAtlas?: NativeRootOptions["svgAtlas"];
|
|
52
|
+
readonly cornerMasks?: NonNullable<NativeRootOptions["graphics"]>["cornerMasks"];
|
|
53
|
+
mount(context: RokuHostContext): MountedRokuApplication;
|
|
54
|
+
onReady?(info: { readonly device: RokuDeviceSnapshot | null }): void | Promise<void>;
|
|
55
|
+
onMemorySnapshot?(snapshot: RokuMemorySnapshot): void;
|
|
56
|
+
onError?(error: Error, phase: RokuHostPhase): void;
|
|
57
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export { createSceneRoot } from "./renderer/SceneRoot";
|
|
2
|
+
export { createNativeRoot } from "./react-native/createNativeRoot";
|
|
3
|
+
export { NativeInput } from "./react-native/NativeInput";
|
|
4
|
+
export { NativeSystemState } from "./react-native/NativeSystemState";
|
|
5
|
+
export type { NativeRemoteEvent, NativeRemoteKey, TVEvent } from "./react-native/NativeInput";
|
|
6
|
+
export type { NativeRootOptions } from "./react-native/createNativeRoot";
|
|
7
|
+
export type { LayoutStyle, LayoutRect } from "./layout/flexLayout";
|
|
8
|
+
export type { TextMetricProvider, TextMetrics } from "./layout/textLayout";
|
|
9
|
+
export type {
|
|
10
|
+
SceneElementProps,
|
|
11
|
+
SceneHandle,
|
|
12
|
+
SceneRoot,
|
|
13
|
+
SceneRootOptions,
|
|
14
|
+
SceneTreeElement,
|
|
15
|
+
} from "./renderer/SceneRoot";
|
|
16
|
+
export { RokuHostError } from "./scene/sceneGraph.types";
|
|
17
|
+
export type {
|
|
18
|
+
SceneFields,
|
|
19
|
+
SceneGraphPort,
|
|
20
|
+
SceneNodeRef,
|
|
21
|
+
SceneValue,
|
|
22
|
+
} from "./scene/sceneGraph.types";
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { RokuHostError, type SceneGraphPort, type SceneNodeRef } from "../scene/sceneGraph.types";
|
|
2
|
+
import type { LayoutStyle } from "./flexLayout";
|
|
3
|
+
import type { TextMetricProvider, TextMetrics } from "./textLayout";
|
|
4
|
+
import { fontVariantFamily } from "./fontVariants.mjs";
|
|
5
|
+
import { prepareFontFamilies, fontFaceFamily, type FontFamilyGroups } from "./fontFaces.mjs";
|
|
6
|
+
export { fontVariantFamily } from "./fontVariants.mjs";
|
|
7
|
+
|
|
8
|
+
export interface FontCancellation {
|
|
9
|
+
readonly aborted: boolean;
|
|
10
|
+
readonly reason?: unknown;
|
|
11
|
+
addEventListener(type: "abort", listener: () => void): void;
|
|
12
|
+
removeEventListener(type: "abort", listener: () => void): void;
|
|
13
|
+
}
|
|
14
|
+
export interface FontEngine {
|
|
15
|
+
ready(font: SceneNodeRef, signal: FontCancellation): Promise<void>;
|
|
16
|
+
metrics(font: SceneNodeRef): TextMetrics;
|
|
17
|
+
}
|
|
18
|
+
export interface FontResourcesOptions {
|
|
19
|
+
readonly assets: Readonly<Record<string, string>>;
|
|
20
|
+
readonly defaultFamily: string;
|
|
21
|
+
readonly families?: FontFamilyGroups;
|
|
22
|
+
readonly timeoutMs?: number;
|
|
23
|
+
readonly maximumFonts?: number;
|
|
24
|
+
}
|
|
25
|
+
interface Entry {
|
|
26
|
+
ref: SceneNodeRef;
|
|
27
|
+
pending: Promise<void>;
|
|
28
|
+
ready: boolean;
|
|
29
|
+
retained: boolean;
|
|
30
|
+
attached: boolean;
|
|
31
|
+
releasing: boolean;
|
|
32
|
+
cancel(reason: unknown): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Owns packaged font lifetimes. Metrics are supplied by the host's font engine. */
|
|
36
|
+
export class FontResources implements TextMetricProvider {
|
|
37
|
+
private readonly entries = new Map<string, Entry>();
|
|
38
|
+
private disposed = false;
|
|
39
|
+
private readonly families: FontFamilyGroups;
|
|
40
|
+
constructor(
|
|
41
|
+
private readonly scene: SceneGraphPort,
|
|
42
|
+
private readonly engine: FontEngine,
|
|
43
|
+
private readonly options: FontResourcesOptions,
|
|
44
|
+
) {
|
|
45
|
+
this.options = { ...options, assets: Object.freeze({ ...options.assets }) };
|
|
46
|
+
this.families = prepareFontFamilies(this.options.assets, this.options.families);
|
|
47
|
+
this.identity({});
|
|
48
|
+
if (!(
|
|
49
|
+
Number.isFinite(this.options.timeoutMs ?? 15_000) && (this.options.timeoutMs ?? 15_000) > 0
|
|
50
|
+
))
|
|
51
|
+
throw new RokuHostError("C04_FONT_TIMEOUT", "Font timeout must be positive and finite");
|
|
52
|
+
if (
|
|
53
|
+
!Number.isInteger(this.options.maximumFonts ?? 128) ||
|
|
54
|
+
(this.options.maximumFonts ?? 128) < 1
|
|
55
|
+
)
|
|
56
|
+
throw new RokuHostError("C04_FONT_LIMIT", "Font resource limit must be a positive integer");
|
|
57
|
+
}
|
|
58
|
+
fontFamily(style: LayoutStyle): string {
|
|
59
|
+
return this.identity(style).family;
|
|
60
|
+
}
|
|
61
|
+
prepare(style: LayoutStyle): void | Promise<void> {
|
|
62
|
+
this.assertActive();
|
|
63
|
+
const { key, family, size } = this.identity(style);
|
|
64
|
+
const existing = this.entries.get(key);
|
|
65
|
+
if (existing) return existing.ready ? undefined : existing.pending;
|
|
66
|
+
if (this.entries.size >= (this.options.maximumFonts ?? 128))
|
|
67
|
+
throw new RokuHostError("C04_FONT_LIMIT", "The font resource budget was exceeded");
|
|
68
|
+
const listeners = new Set<() => void>();
|
|
69
|
+
let aborted = false,
|
|
70
|
+
reason: unknown;
|
|
71
|
+
const signal: FontCancellation = {
|
|
72
|
+
get aborted() {
|
|
73
|
+
return aborted;
|
|
74
|
+
},
|
|
75
|
+
get reason() {
|
|
76
|
+
return reason;
|
|
77
|
+
},
|
|
78
|
+
addEventListener(_type, listener) {
|
|
79
|
+
listeners.add(listener);
|
|
80
|
+
},
|
|
81
|
+
removeEventListener(_type, listener) {
|
|
82
|
+
listeners.delete(listener);
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
const ref = this.scene.create("font");
|
|
86
|
+
const entry: Entry = {
|
|
87
|
+
ref,
|
|
88
|
+
ready: false,
|
|
89
|
+
retained: true,
|
|
90
|
+
attached: false,
|
|
91
|
+
releasing: false,
|
|
92
|
+
pending: Promise.resolve(),
|
|
93
|
+
cancel(value) {
|
|
94
|
+
if (aborted) return;
|
|
95
|
+
aborted = true;
|
|
96
|
+
reason = value;
|
|
97
|
+
for (const listener of [...listeners]) listener();
|
|
98
|
+
listeners.clear();
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
this.entries.set(key, entry);
|
|
102
|
+
const assertCurrent = () => {
|
|
103
|
+
if (this.disposed || signal.aborted || !entry.retained)
|
|
104
|
+
throw (
|
|
105
|
+
signal.reason ?? new RokuHostError("C04_FONT_DISPOSED", "Font resources were disposed")
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
try {
|
|
109
|
+
assertCurrent();
|
|
110
|
+
this.scene.batch(() => {
|
|
111
|
+
// Attach the nonvisual resource before configuring it, so a failed
|
|
112
|
+
// field write still has a confirmed parent for release/disposal.
|
|
113
|
+
this.scene.insert(this.scene.root, ref);
|
|
114
|
+
entry.attached = true;
|
|
115
|
+
this.scene.set(ref, "uri", this.options.assets[family]);
|
|
116
|
+
this.scene.set(ref, "size", size);
|
|
117
|
+
});
|
|
118
|
+
assertCurrent();
|
|
119
|
+
} catch (error) {
|
|
120
|
+
entry.pending = Promise.reject(error);
|
|
121
|
+
void entry.pending.catch(() => undefined);
|
|
122
|
+
try {
|
|
123
|
+
this.release(entry);
|
|
124
|
+
} finally {
|
|
125
|
+
// A failed cleanup remains retained for disposal to retry. It cannot
|
|
126
|
+
// replace the initialization error or leave a resolved placeholder.
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
131
|
+
const deadline = new Promise<never>((_resolve, reject) => {
|
|
132
|
+
signal.addEventListener("abort", () => reject(signal.reason));
|
|
133
|
+
timer = setTimeout(
|
|
134
|
+
() =>
|
|
135
|
+
entry.cancel(
|
|
136
|
+
new RokuHostError(
|
|
137
|
+
"C04_FONT_TIMEOUT",
|
|
138
|
+
`Font ${family} at ${size}px did not become measurable`,
|
|
139
|
+
),
|
|
140
|
+
),
|
|
141
|
+
this.options.timeoutMs ?? 15_000,
|
|
142
|
+
);
|
|
143
|
+
});
|
|
144
|
+
entry.pending = Promise.race([
|
|
145
|
+
Promise.resolve().then(() => {
|
|
146
|
+
assertCurrent();
|
|
147
|
+
return this.engine.ready(ref, signal);
|
|
148
|
+
}),
|
|
149
|
+
deadline,
|
|
150
|
+
])
|
|
151
|
+
.then(() => {
|
|
152
|
+
assertCurrent();
|
|
153
|
+
const metrics = this.engine.metrics(ref);
|
|
154
|
+
assertCurrent();
|
|
155
|
+
if (!(Number.isFinite(metrics.lineHeight) && metrics.lineHeight > 0))
|
|
156
|
+
throw new RokuHostError("C04_FONT_METRICS", "The font engine returned invalid metrics");
|
|
157
|
+
entry.ready = true;
|
|
158
|
+
})
|
|
159
|
+
.catch((error) => {
|
|
160
|
+
try {
|
|
161
|
+
this.release(entry);
|
|
162
|
+
} finally {
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
.finally(() => {
|
|
167
|
+
clearTimeout(timer);
|
|
168
|
+
listeners.clear();
|
|
169
|
+
});
|
|
170
|
+
// The render preparation owns error delivery, including disposal while it is pending.
|
|
171
|
+
void entry.pending.catch(() => undefined);
|
|
172
|
+
return entry.pending;
|
|
173
|
+
}
|
|
174
|
+
metrics(style: LayoutStyle): TextMetrics {
|
|
175
|
+
return this.engine.metrics(this.font(style));
|
|
176
|
+
}
|
|
177
|
+
protected retainedLineHeight(font: SceneNodeRef): number {
|
|
178
|
+
this.assertActive();
|
|
179
|
+
return this.engine.metrics(font).lineHeight;
|
|
180
|
+
}
|
|
181
|
+
font(style: LayoutStyle): SceneNodeRef {
|
|
182
|
+
this.assertActive();
|
|
183
|
+
const entry = this.entries.get(this.identity(style).key);
|
|
184
|
+
if (!entry?.ready)
|
|
185
|
+
throw new RokuHostError("C04_FONT_PENDING", "Font metrics were requested before readiness");
|
|
186
|
+
return entry.ref;
|
|
187
|
+
}
|
|
188
|
+
fontAsset(style: LayoutStyle): string {
|
|
189
|
+
return this.options.assets[this.identity(style).family];
|
|
190
|
+
}
|
|
191
|
+
dispose(): void {
|
|
192
|
+
if (this.disposed && this.entries.size === 0) return;
|
|
193
|
+
this.disposed = true;
|
|
194
|
+
const failures: unknown[] = [];
|
|
195
|
+
for (const [key, entry] of this.entries) {
|
|
196
|
+
try {
|
|
197
|
+
entry.cancel(new RokuHostError("C04_FONT_DISPOSED", "Font resources were disposed"));
|
|
198
|
+
} catch (error) {
|
|
199
|
+
failures.push(error);
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
this.release(entry);
|
|
203
|
+
} catch (error) {
|
|
204
|
+
failures.push(error);
|
|
205
|
+
}
|
|
206
|
+
if (!entry.retained) this.entries.delete(key);
|
|
207
|
+
}
|
|
208
|
+
// Preserve the first failure while retaining only cleanup that still owns
|
|
209
|
+
// a native ref. Further disposal calls can retry it; prepare stays retired.
|
|
210
|
+
if (failures.length) throw failures[0];
|
|
211
|
+
}
|
|
212
|
+
private identity(style: LayoutStyle) {
|
|
213
|
+
const baseFamily = fontFaceFamily(
|
|
214
|
+
String(style.fontFamily ?? this.options.defaultFamily),
|
|
215
|
+
{ fontWeight: style.fontWeight, fontStyle: style.fontStyle },
|
|
216
|
+
this.options.assets,
|
|
217
|
+
this.families,
|
|
218
|
+
),
|
|
219
|
+
family = fontVariantFamily(baseFamily, style.fontVariant),
|
|
220
|
+
size = Number(style.fontSize ?? 14);
|
|
221
|
+
if (!this.options.assets[family])
|
|
222
|
+
throw new RokuHostError(
|
|
223
|
+
family === baseFamily ? "C04_FONT_ASSET" : "C04_FONT_VARIANT_ASSET",
|
|
224
|
+
`No packaged font asset for ${family}`,
|
|
225
|
+
);
|
|
226
|
+
if (!(Number.isFinite(size) && size > 0))
|
|
227
|
+
throw new RokuHostError("C04_FONT_SIZE", "Font size must be positive and finite");
|
|
228
|
+
return { key: `${family}\u0000${size}`, family, size };
|
|
229
|
+
}
|
|
230
|
+
private assertActive() {
|
|
231
|
+
if (this.disposed) throw new RokuHostError("C04_FONT_DISPOSED", "Font resources were disposed");
|
|
232
|
+
}
|
|
233
|
+
private release(entry: Entry) {
|
|
234
|
+
if (!entry.retained || entry.releasing) return;
|
|
235
|
+
entry.releasing = true;
|
|
236
|
+
try {
|
|
237
|
+
this.scene.batch(() => {
|
|
238
|
+
// An insertion may fail before or after attaching. Explicitly move
|
|
239
|
+
// the retained ref to this root before removal when no attachment was
|
|
240
|
+
// acknowledged; insert supports both unattached and already owned refs.
|
|
241
|
+
if (!entry.attached) {
|
|
242
|
+
this.scene.insert(this.scene.root, entry.ref);
|
|
243
|
+
entry.attached = true;
|
|
244
|
+
}
|
|
245
|
+
this.scene.remove(this.scene.root, entry.ref);
|
|
246
|
+
});
|
|
247
|
+
entry.retained = false;
|
|
248
|
+
} finally {
|
|
249
|
+
entry.releasing = false;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { SceneFields } from "../scene/sceneGraph.types";
|
|
2
|
+
import type { LayoutRect } from "./flexLayout";
|
|
3
|
+
export type Affine = readonly [number, number, number, number, number, number];
|
|
4
|
+
export const identityAffine: Affine = [1, 0, 0, 1, 0, 0];
|
|
5
|
+
export function multiplyAffine(a: Affine, b: Affine): Affine {
|
|
6
|
+
return [
|
|
7
|
+
a[0] * b[0] + a[2] * b[1],
|
|
8
|
+
a[1] * b[0] + a[3] * b[1],
|
|
9
|
+
a[0] * b[2] + a[2] * b[3],
|
|
10
|
+
a[1] * b[2] + a[3] * b[3],
|
|
11
|
+
a[0] * b[4] + a[2] * b[5] + a[4],
|
|
12
|
+
a[1] * b[4] + a[3] * b[5] + a[5],
|
|
13
|
+
];
|
|
14
|
+
}
|
|
15
|
+
export function sceneAffine(fields: SceneFields): Affine {
|
|
16
|
+
const translation = (fields.translation as readonly number[] | undefined) ?? [0, 0],
|
|
17
|
+
scale = (fields.scale as readonly number[] | undefined) ?? [1, 1],
|
|
18
|
+
center = (fields.scaleRotateCenter as readonly number[] | undefined) ?? [0, 0],
|
|
19
|
+
angle = Number(fields.rotation ?? 0),
|
|
20
|
+
cosine = Math.cos(angle),
|
|
21
|
+
sine = Math.sin(angle);
|
|
22
|
+
const a = cosine * scale[0],
|
|
23
|
+
b = sine * scale[0],
|
|
24
|
+
c = -sine * scale[1],
|
|
25
|
+
d = cosine * scale[1];
|
|
26
|
+
return [
|
|
27
|
+
a,
|
|
28
|
+
b,
|
|
29
|
+
c,
|
|
30
|
+
d,
|
|
31
|
+
translation[0] + center[0] - a * center[0] - c * center[1],
|
|
32
|
+
translation[1] + center[1] - b * center[0] - d * center[1],
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
/** Layout-only nodes inherit their parent's linear transform. A child at its
|
|
36
|
+
* parent's origin can share that entire immutable matrix. */
|
|
37
|
+
export function sceneWorld(parent: Affine, fields: SceneFields): Affine {
|
|
38
|
+
if (fields.scale == null && fields.rotation == null && fields.scaleRotateCenter == null) {
|
|
39
|
+
const offset = fields.translation as readonly number[] | undefined;
|
|
40
|
+
if (!offset || (offset[0] === 0 && offset[1] === 0)) return parent;
|
|
41
|
+
const [a, b, c, d, x, y] = parent;
|
|
42
|
+
return [a, b, c, d, a * offset[0] + c * offset[1] + x, b * offset[0] + d * offset[1] + y];
|
|
43
|
+
}
|
|
44
|
+
return multiplyAffine(parent, sceneAffine(fields));
|
|
45
|
+
}
|
|
46
|
+
export function affineBounds(matrix: Affine, width: number, height: number): LayoutRect {
|
|
47
|
+
if (
|
|
48
|
+
matrix[0] === 1 &&
|
|
49
|
+
matrix[1] === 0 &&
|
|
50
|
+
matrix[2] === 0 &&
|
|
51
|
+
matrix[3] === 1 &&
|
|
52
|
+
width >= 0 &&
|
|
53
|
+
height >= 0
|
|
54
|
+
) {
|
|
55
|
+
const x = matrix[4],
|
|
56
|
+
y = matrix[5];
|
|
57
|
+
return { x, y, width: x + width - x, height: y + height - y };
|
|
58
|
+
}
|
|
59
|
+
const points = [
|
|
60
|
+
[0, 0],
|
|
61
|
+
[width, 0],
|
|
62
|
+
[width, height],
|
|
63
|
+
[0, height],
|
|
64
|
+
].map(([x, y]) => [
|
|
65
|
+
matrix[0] * x + matrix[2] * y + matrix[4],
|
|
66
|
+
matrix[1] * x + matrix[3] * y + matrix[5],
|
|
67
|
+
]);
|
|
68
|
+
const left = Math.min(...points.map((point) => point[0])),
|
|
69
|
+
top = Math.min(...points.map((point) => point[1]));
|
|
70
|
+
return {
|
|
71
|
+
x: left,
|
|
72
|
+
y: top,
|
|
73
|
+
width: Math.max(...points.map((point) => point[0])) - left,
|
|
74
|
+
height: Math.max(...points.map((point) => point[1])) - top,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export function inverseAffine(matrix: Affine): Affine | null {
|
|
78
|
+
const [a, b, c, d, x, y] = matrix,
|
|
79
|
+
determinant = a * d - b * c;
|
|
80
|
+
if (Math.abs(determinant) < 1e-12) return null;
|
|
81
|
+
return [
|
|
82
|
+
d / determinant,
|
|
83
|
+
-b / determinant,
|
|
84
|
+
-c / determinant,
|
|
85
|
+
a / determinant,
|
|
86
|
+
(c * y - d * x) / determinant,
|
|
87
|
+
(b * x - a * y) / determinant,
|
|
88
|
+
];
|
|
89
|
+
}
|