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.
- package/CHANGELOG.md +318 -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 +362 -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,222 @@
|
|
|
1
|
+
import { createElement, forwardRef } from "react";
|
|
2
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
3
|
+
import { useNativeHost } from "../react-native/nativeHostContext";
|
|
4
|
+
import { useElement } from "../react-native/primitives";
|
|
5
|
+
import { rokuColor } from "../react-native/styleSheet";
|
|
6
|
+
import type { NativeViewHandle } from "../react-native/nativeViewHandle";
|
|
7
|
+
import { svgShapeKey } from "./svgKey.mjs";
|
|
8
|
+
import { DynamicSvg } from "./dynamicSvg";
|
|
9
|
+
|
|
10
|
+
export * from "./svgElements";
|
|
11
|
+
import { shapes, type ShapeProps, type SvgProps } from "./svgElements";
|
|
12
|
+
|
|
13
|
+
export function rectilinearPath(
|
|
14
|
+
path: string,
|
|
15
|
+
): Array<{ x: number; y: number; width: number; height: number }> {
|
|
16
|
+
const tokens = path.match(/[a-zA-Z]|[-+]?(?:\d*\.\d+|\d+\.?\d*)(?:e[-+]?\d+)?/g) ?? [],
|
|
17
|
+
result = [];
|
|
18
|
+
if (path.replace(/[a-zA-Z]|[-+]?(?:\d*\.\d+|\d+\.?\d*)(?:e[-+]?\d+)?|[\s,]/g, ""))
|
|
19
|
+
throw new RokuHostError("C09_SVG_PATH", "Invalid path data");
|
|
20
|
+
let index = 0,
|
|
21
|
+
x = 0,
|
|
22
|
+
y = 0,
|
|
23
|
+
startX = 0,
|
|
24
|
+
startY = 0,
|
|
25
|
+
command = "",
|
|
26
|
+
points: number[][] = [];
|
|
27
|
+
const number = () => {
|
|
28
|
+
const value = Number(tokens[index++]);
|
|
29
|
+
if (!Number.isFinite(value))
|
|
30
|
+
throw new RokuHostError("C09_SVG_PATH", "Path coordinate is missing");
|
|
31
|
+
return value;
|
|
32
|
+
};
|
|
33
|
+
while (index < tokens.length) {
|
|
34
|
+
if (/^[a-zA-Z]$/.test(tokens[index])) command = tokens[index++];
|
|
35
|
+
const relative = command === command.toLowerCase();
|
|
36
|
+
if (command === "z" || command === "Z") {
|
|
37
|
+
const xs = points.map((point) => point[0]),
|
|
38
|
+
ys = points.map((point) => point[1]),
|
|
39
|
+
left = Math.min(...xs),
|
|
40
|
+
top = Math.min(...ys),
|
|
41
|
+
width = Math.max(...xs) - left,
|
|
42
|
+
height = Math.max(...ys) - top;
|
|
43
|
+
let area = 0;
|
|
44
|
+
for (let i = 0; i < points.length; i++) {
|
|
45
|
+
const a = points[i],
|
|
46
|
+
b = points[(i + 1) % points.length];
|
|
47
|
+
if (a[0] !== b[0] && a[1] !== b[1])
|
|
48
|
+
throw new RokuHostError(
|
|
49
|
+
"C09_SVG_PATH",
|
|
50
|
+
"Dynamic vector paths currently require axis-aligned rectangles",
|
|
51
|
+
);
|
|
52
|
+
area += a[0] * b[1] - b[0] * a[1];
|
|
53
|
+
}
|
|
54
|
+
if (!width || !height || Math.abs(Math.abs(area) / 2 - width * height) > 0.001)
|
|
55
|
+
throw new RokuHostError("C09_SVG_PATH", "Dynamic filled paths must be rectangular");
|
|
56
|
+
result.push({ x: left, y: top, width, height });
|
|
57
|
+
x = startX;
|
|
58
|
+
y = startY;
|
|
59
|
+
points = [];
|
|
60
|
+
command = "";
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (command.toLowerCase() === "m" || command.toLowerCase() === "l") {
|
|
64
|
+
const nextX = number(),
|
|
65
|
+
nextY = number();
|
|
66
|
+
x = (relative ? x : 0) + nextX;
|
|
67
|
+
y = (relative ? y : 0) + nextY;
|
|
68
|
+
if (command.toLowerCase() === "m") {
|
|
69
|
+
if (points.length) throw new RokuHostError("C09_SVG_PATH", "Filled paths must be closed");
|
|
70
|
+
startX = x;
|
|
71
|
+
startY = y;
|
|
72
|
+
command = relative ? "l" : "L";
|
|
73
|
+
}
|
|
74
|
+
} else if (command.toLowerCase() === "h") x = (relative ? x : 0) + number();
|
|
75
|
+
else if (command.toLowerCase() === "v") y = (relative ? y : 0) + number();
|
|
76
|
+
else
|
|
77
|
+
throw new RokuHostError(
|
|
78
|
+
"C09_SVG_PATH",
|
|
79
|
+
`Dynamic SVG command ${command} needs raster support`,
|
|
80
|
+
);
|
|
81
|
+
points.push([x, y]);
|
|
82
|
+
}
|
|
83
|
+
if (points.length) throw new RokuHostError("C09_SVG_PATH", "Filled paths must be closed");
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
export const Svg = forwardRef<NativeViewHandle, SvgProps>((props, ref) => {
|
|
87
|
+
const host = useNativeHost(),
|
|
88
|
+
children = shapes(props.children),
|
|
89
|
+
viewBox = props.viewBox ?? `0 0 ${props.width ?? 24} ${props.height ?? 24}`;
|
|
90
|
+
const classes = props.className
|
|
91
|
+
?.split(/\s+/)
|
|
92
|
+
.filter((value) => !/^lucide(?:-[a-z0-9-]+)?$/.test(value))
|
|
93
|
+
.join(" ");
|
|
94
|
+
const element = useElement(
|
|
95
|
+
{
|
|
96
|
+
...props,
|
|
97
|
+
className: classes,
|
|
98
|
+
style: [
|
|
99
|
+
{
|
|
100
|
+
width: props.width ?? 24,
|
|
101
|
+
height: props.height ?? 24,
|
|
102
|
+
opacity: props.opacity ?? 1,
|
|
103
|
+
},
|
|
104
|
+
props.style,
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
ref,
|
|
108
|
+
);
|
|
109
|
+
const key = svgShapeKey(viewBox, children),
|
|
110
|
+
atlas = host.svgAtlas?.[key];
|
|
111
|
+
const foreground =
|
|
112
|
+
props.color ?? (typeof element.style.color === "string" ? element.style.color : "black"),
|
|
113
|
+
stroke = !props.stroke || props.stroke === "currentColor" ? foreground : props.stroke;
|
|
114
|
+
const filled = props.fill != null && props.fill !== "none" && props.fill !== "transparent";
|
|
115
|
+
if (atlas) {
|
|
116
|
+
const fill = props.fill === "currentColor" ? foreground : props.fill;
|
|
117
|
+
if (filled && rokuColor(fill) !== rokuColor(stroke))
|
|
118
|
+
throw new RokuHostError(
|
|
119
|
+
"C09_SVG_PAINT",
|
|
120
|
+
"Multi-color icon paint needs separate native raster layers",
|
|
121
|
+
);
|
|
122
|
+
if (
|
|
123
|
+
(props.strokeLinecap && props.strokeLinecap !== "round") ||
|
|
124
|
+
(props.strokeLinejoin && props.strokeLinejoin !== "round")
|
|
125
|
+
)
|
|
126
|
+
throw new RokuHostError(
|
|
127
|
+
"C09_SVG_STROKE",
|
|
128
|
+
"The packaged Lucide raster uses its canonical round stroke joins and caps",
|
|
129
|
+
);
|
|
130
|
+
const uri = atlas.variants[`${props.strokeWidth ?? 2}:${filled ? "filled" : "outline"}`];
|
|
131
|
+
if (!uri)
|
|
132
|
+
throw new RokuHostError(
|
|
133
|
+
"C09_SVG_VARIANT",
|
|
134
|
+
`The build has no raster for stroke width ${props.strokeWidth ?? 2}`,
|
|
135
|
+
);
|
|
136
|
+
return createElement(
|
|
137
|
+
"group",
|
|
138
|
+
{ fields: element.fields, layout: element.layout },
|
|
139
|
+
createElement("poster", {
|
|
140
|
+
fields: {
|
|
141
|
+
loadDisplayMode: "scaleToFit",
|
|
142
|
+
blendColor: rokuColor(stroke),
|
|
143
|
+
uri,
|
|
144
|
+
},
|
|
145
|
+
layout: { decoration: "background" },
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
// Dynamic QR matrices are rectangles, so preserve their exact authored geometry without an icon atlas.
|
|
150
|
+
const box = viewBox.trim().split(/[ ,]+/).map(Number);
|
|
151
|
+
if (
|
|
152
|
+
box.length !== 4 ||
|
|
153
|
+
box.some((value) => !Number.isFinite(value)) ||
|
|
154
|
+
!(box[2] > 0 && box[3] > 0)
|
|
155
|
+
)
|
|
156
|
+
throw new RokuHostError("C09_SVG_VIEWBOX", "Svg requires a finite positive viewBox");
|
|
157
|
+
if (
|
|
158
|
+
typeof props.width !== "number" ||
|
|
159
|
+
typeof props.height !== "number" ||
|
|
160
|
+
children.some(
|
|
161
|
+
([tag, attributes]) =>
|
|
162
|
+
(tag !== "rect" && tag !== "path") || (attributes.stroke && attributes.stroke !== "none"),
|
|
163
|
+
)
|
|
164
|
+
)
|
|
165
|
+
return createElement(DynamicSvg, { props, element, children, box });
|
|
166
|
+
const paths = new Map<ShapeProps, ReturnType<typeof rectilinearPath>>();
|
|
167
|
+
for (const [tag, attributes] of children) {
|
|
168
|
+
if (tag !== "path") continue;
|
|
169
|
+
try {
|
|
170
|
+
paths.set(attributes, rectilinearPath(String(attributes.d)));
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (!(error instanceof RokuHostError)) throw error;
|
|
173
|
+
return createElement(DynamicSvg, { props, element, children, box });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const scaleX = props.width / box[2],
|
|
177
|
+
scaleY = props.height / box[3];
|
|
178
|
+
const rectangles = children.flatMap(([tag, attributes]) => {
|
|
179
|
+
if (attributes.stroke && attributes.stroke !== "none")
|
|
180
|
+
throw new RokuHostError(
|
|
181
|
+
"C09_SVG_UNKNOWN",
|
|
182
|
+
`This SVG shape was not included in the build's raster atlas: ${key}`,
|
|
183
|
+
);
|
|
184
|
+
const fill = attributes.fill ?? props.fill ?? "black";
|
|
185
|
+
if (fill === "none" || fill === "transparent") return [];
|
|
186
|
+
const rects =
|
|
187
|
+
tag === "rect" && !attributes.rx && !attributes.ry
|
|
188
|
+
? [
|
|
189
|
+
{
|
|
190
|
+
x: Number(attributes.x ?? 0),
|
|
191
|
+
y: Number(attributes.y ?? 0),
|
|
192
|
+
width: Number(attributes.width),
|
|
193
|
+
height: Number(attributes.height),
|
|
194
|
+
},
|
|
195
|
+
]
|
|
196
|
+
: tag === "path"
|
|
197
|
+
? paths.get(attributes)!
|
|
198
|
+
: null;
|
|
199
|
+
if (!rects)
|
|
200
|
+
throw new RokuHostError("C09_SVG_UNKNOWN", `SVG ${tag} requires an admitted raster adapter`);
|
|
201
|
+
return rects.map((rect) =>
|
|
202
|
+
createElement("rectangle", {
|
|
203
|
+
key: `${tag}:${rect.x}:${rect.y}:${rect.width}:${rect.height}`,
|
|
204
|
+
fields: {
|
|
205
|
+
color: rokuColor(fill === "currentColor" ? foreground : fill),
|
|
206
|
+
},
|
|
207
|
+
layout: {
|
|
208
|
+
style: {
|
|
209
|
+
position: "absolute",
|
|
210
|
+
left: (rect.x - box[0]) * scaleX,
|
|
211
|
+
top: (rect.y - box[1]) * scaleY,
|
|
212
|
+
width: rect.width * scaleX,
|
|
213
|
+
height: rect.height * scaleY,
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
}),
|
|
217
|
+
);
|
|
218
|
+
});
|
|
219
|
+
return createElement("group", { fields: element.fields, layout: element.layout }, ...rectangles);
|
|
220
|
+
});
|
|
221
|
+
Svg.displayName = "RokuSvg";
|
|
222
|
+
export default Svg;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Children, isValidElement, Fragment, type ReactNode } from "react";
|
|
2
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
3
|
+
import type { NativeElementProps } from "../react-native/primitives";
|
|
4
|
+
|
|
5
|
+
export interface ShapeProps {
|
|
6
|
+
readonly children?: ReactNode;
|
|
7
|
+
readonly [key: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
export type Shape = readonly [string, ShapeProps];
|
|
10
|
+
function shape(tag: string) {
|
|
11
|
+
const component = (_props: ShapeProps) => {
|
|
12
|
+
throw new RokuHostError("C09_SVG_CONTEXT", `${tag} must be inside Svg`);
|
|
13
|
+
};
|
|
14
|
+
return Object.assign(component, { svgTag: tag });
|
|
15
|
+
}
|
|
16
|
+
export const Path = shape("path"),
|
|
17
|
+
Rect = shape("rect"),
|
|
18
|
+
Circle = shape("circle"),
|
|
19
|
+
Ellipse = shape("ellipse"),
|
|
20
|
+
Line = shape("line"),
|
|
21
|
+
Polyline = shape("polyline"),
|
|
22
|
+
Polygon = shape("polygon"),
|
|
23
|
+
G = shape("g");
|
|
24
|
+
export const Defs = shape("defs"),
|
|
25
|
+
ClipPath = shape("clipPath"),
|
|
26
|
+
Mask = shape("mask"),
|
|
27
|
+
LinearGradient = shape("linearGradient"),
|
|
28
|
+
RadialGradient = shape("radialGradient"),
|
|
29
|
+
Stop = shape("stop"),
|
|
30
|
+
Pattern = shape("pattern"),
|
|
31
|
+
Text = shape("text"),
|
|
32
|
+
TSpan = shape("tspan"),
|
|
33
|
+
Use = shape("use"),
|
|
34
|
+
Symbol = shape("symbol"),
|
|
35
|
+
Image = shape("image");
|
|
36
|
+
export function shapes(children: ReactNode): Shape[] {
|
|
37
|
+
return Children.toArray(children).flatMap((child) => {
|
|
38
|
+
if (!isValidElement<ShapeProps>(child))
|
|
39
|
+
throw new RokuHostError("C09_SVG_CHILD", "Svg requires vector shape children");
|
|
40
|
+
if (child.type === Fragment) return shapes(child.props.children);
|
|
41
|
+
const tag = (child.type as { svgTag?: string }).svgTag;
|
|
42
|
+
if (!tag)
|
|
43
|
+
throw new RokuHostError("C09_SVG_CHILD", "Custom SVG child components need a vector adapter");
|
|
44
|
+
return [[tag, child.props] as const];
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export interface SvgProps extends NativeElementProps {
|
|
48
|
+
readonly width?: number | string;
|
|
49
|
+
readonly height?: number | string;
|
|
50
|
+
readonly viewBox?: string;
|
|
51
|
+
readonly stroke?: string;
|
|
52
|
+
readonly fill?: string;
|
|
53
|
+
readonly color?: string;
|
|
54
|
+
readonly strokeWidth?: number | string;
|
|
55
|
+
readonly opacity?: number;
|
|
56
|
+
readonly strokeLinecap?: string;
|
|
57
|
+
readonly strokeLinejoin?: string;
|
|
58
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const PAINT =
|
|
2
|
+
/^(?:key|children|style|fill(?:Rule|Opacity)?|stroke.*|color|opacity|vectorEffect|testID|accessible|accessibility.*|pointerEvents)$/;
|
|
3
|
+
export function svgShapeKey(viewBox, nodes) {
|
|
4
|
+
return JSON.stringify([
|
|
5
|
+
String(viewBox).trim().replace(/[ ,]+/g, " "),
|
|
6
|
+
nodes.map(([tag, attributes]) => [
|
|
7
|
+
tag.toLowerCase(),
|
|
8
|
+
Object.fromEntries(
|
|
9
|
+
Object.entries(attributes)
|
|
10
|
+
.filter(([key, value]) => value != null && !PAINT.test(key))
|
|
11
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
12
|
+
.map(([key, value]) => [key, String(value)]),
|
|
13
|
+
),
|
|
14
|
+
]),
|
|
15
|
+
]);
|
|
16
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
2
|
+
export type Point = readonly [number, number];
|
|
3
|
+
const failure = (message: string): never => {
|
|
4
|
+
throw new RokuHostError("C09_SVG_PATH", message);
|
|
5
|
+
};
|
|
6
|
+
const token = /[a-zA-Z]|[-+]?(?:\d*\.\d+|\d+\.?\d*)(?:[eE][-+]?\d+)?/g;
|
|
7
|
+
|
|
8
|
+
/** Bounded SVG path flattening for portable software rasterization. */
|
|
9
|
+
export function svgPath(data: string): Point[][] {
|
|
10
|
+
if (data.length > 200_000 || data.replace(token, "").replace(/[\s,]/g, ""))
|
|
11
|
+
failure("Invalid SVG path data");
|
|
12
|
+
const parts = data.match(token) ?? [],
|
|
13
|
+
paths: Point[][] = [];
|
|
14
|
+
let index = 0,
|
|
15
|
+
command = "",
|
|
16
|
+
previous = "",
|
|
17
|
+
x = 0,
|
|
18
|
+
y = 0,
|
|
19
|
+
control: Point = [0, 0],
|
|
20
|
+
count = 0;
|
|
21
|
+
let current: Point[] = [];
|
|
22
|
+
const push = (point: Point) => {
|
|
23
|
+
if (++count > 16_384 || point.some((value) => !Number.isFinite(value) || Math.abs(value) > 1e7))
|
|
24
|
+
failure("SVG path exceeds its coordinate budget");
|
|
25
|
+
current.push(point);
|
|
26
|
+
[x, y] = point;
|
|
27
|
+
};
|
|
28
|
+
const number = () => {
|
|
29
|
+
const value = Number(parts[index++]);
|
|
30
|
+
if (!Number.isFinite(value)) failure("SVG path coordinate is missing");
|
|
31
|
+
return value;
|
|
32
|
+
};
|
|
33
|
+
while (index < parts.length) {
|
|
34
|
+
if (/^[a-zA-Z]$/.test(parts[index])) command = parts[index++];
|
|
35
|
+
if (!command) failure("SVG path has no command");
|
|
36
|
+
const relative = command === command.toLowerCase(),
|
|
37
|
+
type = command.toUpperCase(),
|
|
38
|
+
start: Point = [x, y];
|
|
39
|
+
const point = (): Point => [
|
|
40
|
+
number() + (relative ? start[0] : 0),
|
|
41
|
+
number() + (relative ? start[1] : 0),
|
|
42
|
+
];
|
|
43
|
+
if (type === "Z") {
|
|
44
|
+
if (current.length) push(current[0]);
|
|
45
|
+
command = "";
|
|
46
|
+
} else if (type === "M") {
|
|
47
|
+
current = [];
|
|
48
|
+
paths.push(current);
|
|
49
|
+
push(point());
|
|
50
|
+
command = relative ? "l" : "L";
|
|
51
|
+
} else if (!current.length) failure("SVG path must begin with move");
|
|
52
|
+
else if (type === "L") push(point());
|
|
53
|
+
else if (type === "H") push([number() + (relative ? x : 0), y]);
|
|
54
|
+
else if (type === "V") push([x, number() + (relative ? y : 0)]);
|
|
55
|
+
else if (["C", "S", "Q", "T"].includes(type)) {
|
|
56
|
+
const reflected: Point = [2 * x - control[0], 2 * y - control[1]];
|
|
57
|
+
const a =
|
|
58
|
+
type === "C" || type === "Q"
|
|
59
|
+
? point()
|
|
60
|
+
: ["S", "T"].includes(type) && (type === "S" ? ["C", "S"] : ["Q", "T"]).includes(previous)
|
|
61
|
+
? reflected
|
|
62
|
+
: start;
|
|
63
|
+
const b = type === "C" || type === "S" ? point() : a,
|
|
64
|
+
end = point();
|
|
65
|
+
const steps = Math.min(
|
|
66
|
+
128,
|
|
67
|
+
Math.max(
|
|
68
|
+
8,
|
|
69
|
+
Math.ceil(
|
|
70
|
+
(Math.hypot(a[0] - x, a[1] - y) +
|
|
71
|
+
Math.hypot(b[0] - a[0], b[1] - a[1]) +
|
|
72
|
+
Math.hypot(end[0] - b[0], end[1] - b[1])) /
|
|
73
|
+
4,
|
|
74
|
+
),
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
for (let step = 1; step <= steps; step++) {
|
|
78
|
+
const t = step / steps,
|
|
79
|
+
u = 1 - t;
|
|
80
|
+
push(
|
|
81
|
+
type === "Q" || type === "T"
|
|
82
|
+
? [
|
|
83
|
+
u * u * start[0] + 2 * u * t * a[0] + t * t * end[0],
|
|
84
|
+
u * u * start[1] + 2 * u * t * a[1] + t * t * end[1],
|
|
85
|
+
]
|
|
86
|
+
: [
|
|
87
|
+
u * u * u * start[0] +
|
|
88
|
+
3 * u * u * t * a[0] +
|
|
89
|
+
3 * u * t * t * b[0] +
|
|
90
|
+
t * t * t * end[0],
|
|
91
|
+
u * u * u * start[1] +
|
|
92
|
+
3 * u * u * t * a[1] +
|
|
93
|
+
3 * u * t * t * b[1] +
|
|
94
|
+
t * t * t * end[1],
|
|
95
|
+
],
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
control = type === "Q" || type === "T" ? a : b;
|
|
99
|
+
} else if (type === "A") {
|
|
100
|
+
let rx = Math.abs(number()),
|
|
101
|
+
ry = Math.abs(number());
|
|
102
|
+
const rotation = (number() * Math.PI) / 180,
|
|
103
|
+
large = number(),
|
|
104
|
+
sweep = number(),
|
|
105
|
+
end = point();
|
|
106
|
+
if (![0, 1].includes(large) || ![0, 1].includes(sweep))
|
|
107
|
+
failure("SVG arc flags must be zero or one");
|
|
108
|
+
if (!rx || !ry || (x === end[0] && y === end[1])) push(end);
|
|
109
|
+
else {
|
|
110
|
+
const cos = Math.cos(rotation),
|
|
111
|
+
sin = Math.sin(rotation),
|
|
112
|
+
dx = (x - end[0]) / 2,
|
|
113
|
+
dy = (y - end[1]) / 2;
|
|
114
|
+
const px = cos * dx + sin * dy,
|
|
115
|
+
py = -sin * dx + cos * dy,
|
|
116
|
+
stretch = (px * px) / (rx * rx) + (py * py) / (ry * ry);
|
|
117
|
+
if (stretch > 1) {
|
|
118
|
+
rx *= Math.sqrt(stretch);
|
|
119
|
+
ry *= Math.sqrt(stretch);
|
|
120
|
+
}
|
|
121
|
+
const factor =
|
|
122
|
+
(large === sweep ? -1 : 1) *
|
|
123
|
+
Math.sqrt(
|
|
124
|
+
Math.max(
|
|
125
|
+
0,
|
|
126
|
+
(rx * rx * ry * ry - rx * rx * py * py - ry * ry * px * px) /
|
|
127
|
+
(rx * rx * py * py + ry * ry * px * px),
|
|
128
|
+
),
|
|
129
|
+
);
|
|
130
|
+
const cx = (factor * rx * py) / ry,
|
|
131
|
+
cy = (-factor * ry * px) / rx;
|
|
132
|
+
const center: Point = [
|
|
133
|
+
cos * cx - sin * cy + (x + end[0]) / 2,
|
|
134
|
+
sin * cx + cos * cy + (y + end[1]) / 2,
|
|
135
|
+
];
|
|
136
|
+
const from = Math.atan2((py - cy) / ry, (px - cx) / rx),
|
|
137
|
+
to = Math.atan2((-py - cy) / ry, (-px - cx) / rx);
|
|
138
|
+
let angle = to - from;
|
|
139
|
+
if (sweep && angle < 0) angle += Math.PI * 2;
|
|
140
|
+
if (!sweep && angle > 0) angle -= Math.PI * 2;
|
|
141
|
+
const steps = Math.max(1, Math.ceil(Math.abs(angle) / (Math.PI / 64)));
|
|
142
|
+
for (let step = 1; step <= steps; step++) {
|
|
143
|
+
const theta = from + (angle * step) / steps,
|
|
144
|
+
ax = rx * Math.cos(theta),
|
|
145
|
+
ay = ry * Math.sin(theta);
|
|
146
|
+
push(
|
|
147
|
+
step === steps
|
|
148
|
+
? end
|
|
149
|
+
: [center[0] + cos * ax - sin * ay, center[1] + sin * ax + cos * ay],
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} else failure(`SVG command ${command} is unavailable`);
|
|
154
|
+
previous = type;
|
|
155
|
+
}
|
|
156
|
+
return paths;
|
|
157
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { Point } from "./svgPath";
|
|
2
|
+
import { rokuColor } from "../react-native/styleSheet";
|
|
3
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
4
|
+
export interface VectorPaint {
|
|
5
|
+
readonly paths: readonly (readonly Point[])[];
|
|
6
|
+
readonly fill?: string;
|
|
7
|
+
readonly stroke?: string;
|
|
8
|
+
readonly strokeWidth?: number;
|
|
9
|
+
readonly opacity?: number;
|
|
10
|
+
readonly fillOpacity?: number;
|
|
11
|
+
readonly strokeOpacity?: number;
|
|
12
|
+
}
|
|
13
|
+
const color = (value?: string): number[] => {
|
|
14
|
+
if (!value || value === "none" || value === "transparent") return [0, 0, 0, 0];
|
|
15
|
+
const packed = Number(rokuColor(value));
|
|
16
|
+
return [(packed >>> 24) & 255, (packed >>> 16) & 255, (packed >>> 8) & 255, (packed & 255) / 255];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Nonzero winding fill and unioned stroke masks; source-over RGBA composition. */
|
|
20
|
+
export function svgPixels(
|
|
21
|
+
width: number,
|
|
22
|
+
height: number,
|
|
23
|
+
commands: readonly VectorPaint[],
|
|
24
|
+
): Uint8Array {
|
|
25
|
+
if (
|
|
26
|
+
!Number.isInteger(width) ||
|
|
27
|
+
!Number.isInteger(height) ||
|
|
28
|
+
width < 1 ||
|
|
29
|
+
height < 1 ||
|
|
30
|
+
width > 2048 ||
|
|
31
|
+
height > 2048 ||
|
|
32
|
+
commands.length > 2048
|
|
33
|
+
)
|
|
34
|
+
throw new RokuHostError(
|
|
35
|
+
"C09_RASTER_BUDGET",
|
|
36
|
+
"SVG raster dimensions or command count exceed the budget",
|
|
37
|
+
);
|
|
38
|
+
const result = new Uint8Array(width * height * 4);
|
|
39
|
+
const blend = (index: number, ink: number[], opacity: number, coverage: number) => {
|
|
40
|
+
const front = ink[3] * opacity * coverage,
|
|
41
|
+
back = result[index + 3] / 255,
|
|
42
|
+
alpha = front + back * (1 - front);
|
|
43
|
+
if (!front) return;
|
|
44
|
+
for (let channel = 0; channel < 3; channel++)
|
|
45
|
+
result[index + channel] = Math.round(
|
|
46
|
+
(ink[channel] * front + result[index + channel] * back * (1 - front)) / alpha,
|
|
47
|
+
);
|
|
48
|
+
result[index + 3] = Math.round(alpha * 255);
|
|
49
|
+
};
|
|
50
|
+
for (const command of commands) {
|
|
51
|
+
const fill = color(command.fill),
|
|
52
|
+
stroke = color(command.stroke),
|
|
53
|
+
opacity = command.opacity ?? 1;
|
|
54
|
+
const edges = command.paths.flatMap((path) =>
|
|
55
|
+
path.slice(1).map((b, index) => [path[index], b] as const),
|
|
56
|
+
);
|
|
57
|
+
const points = command.paths.flat();
|
|
58
|
+
if (!points.length) continue;
|
|
59
|
+
const ys = points.map((point) => point[1]),
|
|
60
|
+
top = Math.max(0, Math.floor(Math.min(...ys))),
|
|
61
|
+
bottom = Math.min(height, Math.ceil(Math.max(...ys)));
|
|
62
|
+
if (fill[3])
|
|
63
|
+
for (let y = top; y < bottom; y++) {
|
|
64
|
+
const coverage = new Float32Array(width);
|
|
65
|
+
for (const sample of [0.25, 0.75]) {
|
|
66
|
+
const scan = y + sample;
|
|
67
|
+
const crossings = command.paths
|
|
68
|
+
.flatMap((path) =>
|
|
69
|
+
path.map((a, index) => {
|
|
70
|
+
const b = path[(index + 1) % path.length];
|
|
71
|
+
return (a[1] <= scan && b[1] > scan) || (b[1] <= scan && a[1] > scan)
|
|
72
|
+
? {
|
|
73
|
+
x: a[0] + ((scan - a[1]) * (b[0] - a[0])) / (b[1] - a[1]),
|
|
74
|
+
winding: b[1] > a[1] ? 1 : -1,
|
|
75
|
+
}
|
|
76
|
+
: null;
|
|
77
|
+
}),
|
|
78
|
+
)
|
|
79
|
+
.filter((value): value is { x: number; winding: number } => value !== null)
|
|
80
|
+
.sort((a, b) => a.x - b.x);
|
|
81
|
+
let winding = 0,
|
|
82
|
+
from = 0;
|
|
83
|
+
for (const crossing of crossings) {
|
|
84
|
+
if (winding)
|
|
85
|
+
for (
|
|
86
|
+
let x = Math.max(0, Math.floor(from));
|
|
87
|
+
x < Math.min(width, Math.ceil(crossing.x));
|
|
88
|
+
x++
|
|
89
|
+
)
|
|
90
|
+
coverage[x] +=
|
|
91
|
+
(Number(x + 0.25 >= from && x + 0.25 < crossing.x) +
|
|
92
|
+
Number(x + 0.75 >= from && x + 0.75 < crossing.x)) /
|
|
93
|
+
4;
|
|
94
|
+
winding += crossing.winding;
|
|
95
|
+
from = crossing.x;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
for (let x = 0; x < width; x++)
|
|
99
|
+
if (coverage[x])
|
|
100
|
+
blend((y * width + x) * 4, fill, opacity * (command.fillOpacity ?? 1), coverage[x]);
|
|
101
|
+
}
|
|
102
|
+
const radius = (command.strokeWidth ?? 1) / 2;
|
|
103
|
+
if (stroke[3] && radius > 0) {
|
|
104
|
+
const coverage = new Float32Array(width * height);
|
|
105
|
+
for (const [a, b] of edges) {
|
|
106
|
+
const dx = b[0] - a[0],
|
|
107
|
+
dy = b[1] - a[1],
|
|
108
|
+
length = dx * dx + dy * dy;
|
|
109
|
+
if (!length) continue;
|
|
110
|
+
const x1 = Math.max(0, Math.floor(Math.min(a[0], b[0]) - radius - 1)),
|
|
111
|
+
x2 = Math.min(width, Math.ceil(Math.max(a[0], b[0]) + radius + 1));
|
|
112
|
+
const y1 = Math.max(0, Math.floor(Math.min(a[1], b[1]) - radius - 1)),
|
|
113
|
+
y2 = Math.min(height, Math.ceil(Math.max(a[1], b[1]) + radius + 1));
|
|
114
|
+
for (let y = y1; y < y2; y++)
|
|
115
|
+
for (let x = x1; x < x2; x++) {
|
|
116
|
+
const t = Math.max(
|
|
117
|
+
0,
|
|
118
|
+
Math.min(1, ((x + 0.5 - a[0]) * dx + (y + 0.5 - a[1]) * dy) / length),
|
|
119
|
+
);
|
|
120
|
+
const distance = Math.hypot(x + 0.5 - a[0] - t * dx, y + 0.5 - a[1] - t * dy);
|
|
121
|
+
coverage[y * width + x] = Math.max(
|
|
122
|
+
coverage[y * width + x],
|
|
123
|
+
Math.max(0, Math.min(1, radius + 0.5 - distance)),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
for (let index = 0; index < coverage.length; index++)
|
|
128
|
+
if (coverage[index])
|
|
129
|
+
blend(index * 4, stroke, opacity * (command.strokeOpacity ?? 1), coverage[index]);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"lib": ["ES2022", "DOM"],
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "Bundler",
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"noEmit": true,
|
|
12
|
+
"paths": {
|
|
13
|
+
"react-native": ["./src/react-native/index.ts"],
|
|
14
|
+
"react-native-svg": ["./src/vector/svg.ts"],
|
|
15
|
+
"react-native-reanimated": ["./src/animation/reanimated.ts"],
|
|
16
|
+
"react-native-worklets": ["./src/animation/worklets.ts"],
|
|
17
|
+
"react-native-safe-area-context": ["./src/react-native/safeArea.ts"],
|
|
18
|
+
"@react-native-async-storage/async-storage": ["./src/services/asyncStorage.ts"],
|
|
19
|
+
"expo-router": ["./src/navigation/expoRouter.ts"],
|
|
20
|
+
"expo-image": ["./src/react-native/image.ts"],
|
|
21
|
+
"expo-linear-gradient": ["./src/react-native/linearGradient.ts"],
|
|
22
|
+
"expo-constants": ["./src/services/expoConstants.ts"],
|
|
23
|
+
"expo-linking": ["./src/services/expoLinking.ts"],
|
|
24
|
+
"expo-status-bar": ["./src/services/expoStatusBar.ts"],
|
|
25
|
+
"expo-secure-store": ["./src/services/secureStore.ts"],
|
|
26
|
+
"expo-crypto": ["./src/services/crypto.ts"]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|