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,259 @@
|
|
|
1
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
2
|
+
import type {
|
|
3
|
+
TextRasterProvider,
|
|
4
|
+
TextRasterSpec,
|
|
5
|
+
TextRasterRequest,
|
|
6
|
+
TextRasterImage,
|
|
7
|
+
TextRasterLease,
|
|
8
|
+
InlineRasterRequest,
|
|
9
|
+
} from "./textRaster.types";
|
|
10
|
+
import { inlineRasterSpecs, overlayRgba } from "./inlineRaster";
|
|
11
|
+
import { textRasterSpec } from "./textRasterGeometry";
|
|
12
|
+
import { paintTextMask } from "./rasterPixels";
|
|
13
|
+
import { rgbaPng } from "./png";
|
|
14
|
+
export interface TextRasterTransport {
|
|
15
|
+
mask(spec: TextRasterSpec): Promise<Uint8Array>;
|
|
16
|
+
write(name: string, bytes: Uint8Array): Promise<string>;
|
|
17
|
+
delete(uri: string): Promise<void>;
|
|
18
|
+
dispose(): void;
|
|
19
|
+
}
|
|
20
|
+
interface Entry {
|
|
21
|
+
readonly key: string;
|
|
22
|
+
readonly size: number;
|
|
23
|
+
readonly pending: Promise<TextRasterImage>;
|
|
24
|
+
references: number;
|
|
25
|
+
image?: TextRasterImage;
|
|
26
|
+
}
|
|
27
|
+
/** Bounded whole-text rasters. Visible/committing text retains its own lease. */
|
|
28
|
+
export class TextRasterCache implements TextRasterProvider {
|
|
29
|
+
private readonly entries = new Map<string, Entry>();
|
|
30
|
+
private disposed = false;
|
|
31
|
+
private transportDisposed = false;
|
|
32
|
+
private disposing = false;
|
|
33
|
+
private sequence = 0;
|
|
34
|
+
private bytes = 0;
|
|
35
|
+
constructor(
|
|
36
|
+
private readonly transport: TextRasterTransport,
|
|
37
|
+
private readonly maximumBytes = 8 * 1024 * 1024,
|
|
38
|
+
) {}
|
|
39
|
+
async acquire(request: TextRasterRequest): Promise<TextRasterLease> {
|
|
40
|
+
const spec = textRasterSpec(request),
|
|
41
|
+
key = JSON.stringify(spec);
|
|
42
|
+
return this.acquireImage(key, spec.width * spec.height * 4, (name) => this.render(spec, name));
|
|
43
|
+
}
|
|
44
|
+
async acquireInline(request: InlineRasterRequest): Promise<TextRasterLease> {
|
|
45
|
+
const specs = inlineRasterSpecs(request),
|
|
46
|
+
spec = specs[0];
|
|
47
|
+
return this.acquireImage(
|
|
48
|
+
`inline:${JSON.stringify(specs)}`,
|
|
49
|
+
spec.width * spec.height * 4,
|
|
50
|
+
async (name) => {
|
|
51
|
+
const pixels = new Uint8Array(spec.width * spec.height * 4);
|
|
52
|
+
for (const run of specs) {
|
|
53
|
+
const mask = await this.transport.mask(run);
|
|
54
|
+
if (this.disposed)
|
|
55
|
+
throw new RokuHostError("C04_RASTER_DISPOSED", "Mixed text retired during drawing");
|
|
56
|
+
overlayRgba(
|
|
57
|
+
pixels,
|
|
58
|
+
paintTextMask(
|
|
59
|
+
mask,
|
|
60
|
+
run.width,
|
|
61
|
+
run.height,
|
|
62
|
+
run.color,
|
|
63
|
+
run.shadowColor,
|
|
64
|
+
run.shadowRadius,
|
|
65
|
+
run.shadowX,
|
|
66
|
+
run.shadowY,
|
|
67
|
+
),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
const uri = await this.transport.write(name, rgbaPng(spec.width, spec.height, pixels));
|
|
71
|
+
return {
|
|
72
|
+
uri,
|
|
73
|
+
width: spec.width / spec.scale,
|
|
74
|
+
height: spec.height / spec.scale,
|
|
75
|
+
inset: spec.inset,
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
async acquirePixels(request: {
|
|
81
|
+
key: string;
|
|
82
|
+
width: number;
|
|
83
|
+
height: number;
|
|
84
|
+
pixels: Uint8Array | (() => Uint8Array);
|
|
85
|
+
}): Promise<TextRasterLease> {
|
|
86
|
+
const { key, width, height, pixels } = request;
|
|
87
|
+
if (
|
|
88
|
+
!Number.isInteger(width) ||
|
|
89
|
+
!Number.isInteger(height) ||
|
|
90
|
+
width < 1 ||
|
|
91
|
+
height < 1 ||
|
|
92
|
+
width > 2048 ||
|
|
93
|
+
height > 2048 ||
|
|
94
|
+
(typeof pixels !== "function" && pixels.length !== width * height * 4)
|
|
95
|
+
)
|
|
96
|
+
throw new RokuHostError("C09_RASTER_SIZE", "Invalid vector bitmap dimensions or byte count");
|
|
97
|
+
return this.acquireImage(
|
|
98
|
+
`bitmap:${width}:${height}:${key}`,
|
|
99
|
+
width * height * 4,
|
|
100
|
+
async (name) => {
|
|
101
|
+
const values = typeof pixels === "function" ? pixels() : pixels;
|
|
102
|
+
if (this.disposed)
|
|
103
|
+
throw new RokuHostError("C04_RASTER_DISPOSED", "Bitmap resources retired during drawing");
|
|
104
|
+
if (values.length !== width * height * 4)
|
|
105
|
+
throw new RokuHostError("C09_RASTER_SIZE", "Invalid vector bitmap byte count");
|
|
106
|
+
return {
|
|
107
|
+
uri: await this.transport.write(name, rgbaPng(width, height, values)),
|
|
108
|
+
width,
|
|
109
|
+
height,
|
|
110
|
+
inset: 0,
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
private async acquireImage(
|
|
116
|
+
key: string,
|
|
117
|
+
size: number,
|
|
118
|
+
render: (name: string) => Promise<TextRasterImage>,
|
|
119
|
+
): Promise<TextRasterLease> {
|
|
120
|
+
if (this.disposed)
|
|
121
|
+
throw new RokuHostError("C04_RASTER_DISPOSED", "Text raster resources have retired");
|
|
122
|
+
let entry = this.entries.get(key);
|
|
123
|
+
if (!entry) {
|
|
124
|
+
// Plan eviction without invoking transport/user code. Publish the new
|
|
125
|
+
// reservation before any delete or draw callback can reenter this key.
|
|
126
|
+
const retired = this.trim(size);
|
|
127
|
+
this.bytes += size;
|
|
128
|
+
const name = `text-${++this.sequence}.png`;
|
|
129
|
+
let resolveImage!: (image: TextRasterImage) => void, rejectImage!: (error: unknown) => void;
|
|
130
|
+
const pending = new Promise<TextRasterImage>((resolve, reject) => {
|
|
131
|
+
resolveImage = resolve;
|
|
132
|
+
rejectImage = reject;
|
|
133
|
+
});
|
|
134
|
+
entry = { key, size, pending, references: 1 };
|
|
135
|
+
this.entries.set(key, entry);
|
|
136
|
+
const owned = entry;
|
|
137
|
+
void pending.then(
|
|
138
|
+
(image) => {
|
|
139
|
+
owned.image = image;
|
|
140
|
+
},
|
|
141
|
+
() => {
|
|
142
|
+
if (this.entries.get(key) === owned) {
|
|
143
|
+
this.entries.delete(key);
|
|
144
|
+
this.bytes -= size;
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
);
|
|
148
|
+
try {
|
|
149
|
+
for (const uri of retired) {
|
|
150
|
+
if (this.disposed)
|
|
151
|
+
throw new RokuHostError(
|
|
152
|
+
"C04_RASTER_DISPOSED",
|
|
153
|
+
"Text raster resources retired during eviction",
|
|
154
|
+
);
|
|
155
|
+
void this.transport.delete(uri).catch(() => undefined);
|
|
156
|
+
}
|
|
157
|
+
if (this.disposed)
|
|
158
|
+
throw new RokuHostError(
|
|
159
|
+
"C04_RASTER_DISPOSED",
|
|
160
|
+
"Text raster resources retired before drawing",
|
|
161
|
+
);
|
|
162
|
+
// Keep invocation synchronous; only its result joins the published
|
|
163
|
+
// pending identity shared by every current acquisition.
|
|
164
|
+
void render(name).then(resolveImage, rejectImage);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
rejectImage(error);
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
this.entries.delete(key);
|
|
170
|
+
this.entries.set(key, entry);
|
|
171
|
+
entry.references++;
|
|
172
|
+
}
|
|
173
|
+
let image: TextRasterImage;
|
|
174
|
+
try {
|
|
175
|
+
image = await entry.pending;
|
|
176
|
+
} catch (error) {
|
|
177
|
+
entry.references--;
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
if (this.disposed) {
|
|
181
|
+
entry.references--;
|
|
182
|
+
throw new RokuHostError(
|
|
183
|
+
"C04_RASTER_DISPOSED",
|
|
184
|
+
"Text raster resources retired during preparation",
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
let active = true;
|
|
188
|
+
return {
|
|
189
|
+
image,
|
|
190
|
+
release: () => {
|
|
191
|
+
if (!active) return;
|
|
192
|
+
active = false;
|
|
193
|
+
entry!.references--;
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
private async render(spec: TextRasterSpec, name: string): Promise<TextRasterImage> {
|
|
198
|
+
const mask = await this.transport.mask(spec);
|
|
199
|
+
if (this.disposed)
|
|
200
|
+
throw new RokuHostError(
|
|
201
|
+
"C04_RASTER_DISPOSED",
|
|
202
|
+
"Text raster resources retired during drawing",
|
|
203
|
+
);
|
|
204
|
+
const pixels = paintTextMask(
|
|
205
|
+
mask,
|
|
206
|
+
spec.width,
|
|
207
|
+
spec.height,
|
|
208
|
+
spec.color,
|
|
209
|
+
spec.shadowColor,
|
|
210
|
+
spec.shadowRadius,
|
|
211
|
+
spec.shadowX,
|
|
212
|
+
spec.shadowY,
|
|
213
|
+
);
|
|
214
|
+
const uri = await this.transport.write(name, rgbaPng(spec.width, spec.height, pixels));
|
|
215
|
+
return {
|
|
216
|
+
uri,
|
|
217
|
+
width: spec.width / spec.scale,
|
|
218
|
+
height: spec.height / spec.scale,
|
|
219
|
+
inset: spec.inset,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
private trim(additional: number): string[] {
|
|
223
|
+
const retired: Entry[] = [];
|
|
224
|
+
let retainedBytes = this.bytes;
|
|
225
|
+
for (const entry of this.entries.values()) {
|
|
226
|
+
if (retainedBytes + additional <= this.maximumBytes) break;
|
|
227
|
+
if (entry.references || !entry.image) continue;
|
|
228
|
+
retired.push(entry);
|
|
229
|
+
retainedBytes -= entry.size;
|
|
230
|
+
}
|
|
231
|
+
if (retainedBytes + additional > this.maximumBytes)
|
|
232
|
+
throw new RokuHostError("C04_RASTER_BUDGET", "Visible text exceeds the beta raster budget");
|
|
233
|
+
for (const entry of retired) this.entries.delete(entry.key);
|
|
234
|
+
this.bytes = retainedBytes;
|
|
235
|
+
return retired.map((entry) => entry.image!.uri);
|
|
236
|
+
}
|
|
237
|
+
diagnostics() {
|
|
238
|
+
const entries = [...this.entries.values()];
|
|
239
|
+
return {
|
|
240
|
+
entries: entries.length,
|
|
241
|
+
bytes: this.bytes,
|
|
242
|
+
pending: entries.filter((entry) => !entry.image).length,
|
|
243
|
+
leased: entries.filter((entry) => entry.references > 0).length,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
dispose(): void {
|
|
247
|
+
if (this.transportDisposed || this.disposing) return;
|
|
248
|
+
this.disposed = true;
|
|
249
|
+
this.entries.clear();
|
|
250
|
+
this.bytes = 0;
|
|
251
|
+
this.disposing = true;
|
|
252
|
+
try {
|
|
253
|
+
this.transport.dispose();
|
|
254
|
+
this.transportDisposed = true;
|
|
255
|
+
} finally {
|
|
256
|
+
this.disposing = false;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { RokuHostError, type SceneFields } from "../scene/sceneGraph.types";
|
|
2
|
+
export interface GradientSpec {
|
|
3
|
+
readonly start: { x: number; y: number };
|
|
4
|
+
readonly end: { x: number; y: number };
|
|
5
|
+
readonly locations: readonly number[];
|
|
6
|
+
}
|
|
7
|
+
/** Project authored endpoints onto the Effect's full-box gradient axis. */
|
|
8
|
+
export function gradientFields(spec: GradientSpec, width: number, height: number): SceneFields {
|
|
9
|
+
if (
|
|
10
|
+
![spec.start.x, spec.start.y, spec.end.x, spec.end.y, ...spec.locations].every(
|
|
11
|
+
(value) => Number.isFinite(value) && value >= 0 && value <= 1,
|
|
12
|
+
)
|
|
13
|
+
)
|
|
14
|
+
throw new RokuHostError(
|
|
15
|
+
"C03_GRADIENT",
|
|
16
|
+
"Gradient endpoints and stops must be finite within [0,1]",
|
|
17
|
+
);
|
|
18
|
+
if (spec.locations.some((value, index) => index > 0 && value < spec.locations[index - 1]))
|
|
19
|
+
throw new RokuHostError("C03_GRADIENT", "Gradient stops must be ordered");
|
|
20
|
+
if (!width || !height) return { gradientAngle: 0, gradientStops: spec.locations };
|
|
21
|
+
const gx = (spec.end.x - spec.start.x) * width,
|
|
22
|
+
gy = (spec.end.y - spec.start.y) * height,
|
|
23
|
+
length = Math.hypot(gx, gy);
|
|
24
|
+
if (!length) throw new RokuHostError("C03_GRADIENT", "A gradient requires distinct endpoints");
|
|
25
|
+
const dx = gx / length,
|
|
26
|
+
dy = gy / length,
|
|
27
|
+
minimum = Math.min(0, width * dx) + Math.min(0, height * dy),
|
|
28
|
+
span = Math.abs(width * dx) + Math.abs(height * dy);
|
|
29
|
+
const start = spec.start.x * width * dx + spec.start.y * height * dy;
|
|
30
|
+
return {
|
|
31
|
+
// SceneGraph measures clockwise from up in screen coordinates.
|
|
32
|
+
gradientAngle: ((Math.atan2(dx, -dy) * 180) / Math.PI + 360) % 360,
|
|
33
|
+
gradientStops: spec.locations.map((stop) =>
|
|
34
|
+
Math.max(0, Math.min(1, (start + stop * length - minimum) / span)),
|
|
35
|
+
),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { InlineRasterRequest, TextRasterSpec } from "./textRaster.types";
|
|
2
|
+
import { textRasterSpec } from "./textRasterGeometry";
|
|
3
|
+
import { layoutText } from "../layout/textLayout";
|
|
4
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
5
|
+
|
|
6
|
+
/** Fitting single-line spans use their own fonts on one common bitmap scale. */
|
|
7
|
+
export function inlineRasterSpecs(request: InlineRasterRequest): TextRasterSpec[] {
|
|
8
|
+
if (!request.runs.length || request.runs.length > 64)
|
|
9
|
+
throw new RokuHostError("C04_INLINE_RUNS", "Mixed text requires 1–64 runs");
|
|
10
|
+
const runs = request.runs.map((run) => ({
|
|
11
|
+
...run,
|
|
12
|
+
text: layoutText(run.text, run.style, run.metrics, Infinity, 1).text,
|
|
13
|
+
}));
|
|
14
|
+
const widths = runs.map((run) => layoutText(run.text, run.style, run.metrics, Infinity, 1).width);
|
|
15
|
+
const total = widths.reduce(
|
|
16
|
+
(sum, width, index) =>
|
|
17
|
+
sum + width + (index < runs.length - 1 ? Number(runs[index].style.letterSpacing ?? 0) : 0),
|
|
18
|
+
0,
|
|
19
|
+
);
|
|
20
|
+
if (Math.fround(total) > Math.fround(request.width))
|
|
21
|
+
throw new RokuHostError("C04_INLINE_WRAP", "Mixed raster text must fit one line");
|
|
22
|
+
const specs = runs.map((run) =>
|
|
23
|
+
textRasterSpec({
|
|
24
|
+
...run,
|
|
25
|
+
style: { ...run.style, textAlign: "left" },
|
|
26
|
+
width: request.width,
|
|
27
|
+
height: request.height,
|
|
28
|
+
layout: {
|
|
29
|
+
text: run.text,
|
|
30
|
+
lines: [run.text],
|
|
31
|
+
width: request.width,
|
|
32
|
+
height: request.height,
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
if (
|
|
37
|
+
specs.some(
|
|
38
|
+
(spec) =>
|
|
39
|
+
spec.width !== specs[0].width ||
|
|
40
|
+
spec.height !== specs[0].height ||
|
|
41
|
+
spec.scale !== specs[0].scale ||
|
|
42
|
+
spec.inset !== specs[0].inset ||
|
|
43
|
+
Number(spec.shadowColor) & 255,
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
throw new RokuHostError(
|
|
47
|
+
"C04_INLINE_RASTER",
|
|
48
|
+
"Mixed tracking requires a common bitmap scale and no shadow",
|
|
49
|
+
);
|
|
50
|
+
const align = runs[0].style.textAlign;
|
|
51
|
+
let left =
|
|
52
|
+
align === "center"
|
|
53
|
+
? (request.width - total) / 2
|
|
54
|
+
: align === "right"
|
|
55
|
+
? request.width - total
|
|
56
|
+
: 0;
|
|
57
|
+
return specs.map((spec, index) => {
|
|
58
|
+
const result = {
|
|
59
|
+
...spec,
|
|
60
|
+
commands: spec.commands.map((command) => ({
|
|
61
|
+
...command,
|
|
62
|
+
x: command.x + left * spec.scale,
|
|
63
|
+
})),
|
|
64
|
+
};
|
|
65
|
+
left += widths[index] + Number(runs[index].style.letterSpacing ?? 0);
|
|
66
|
+
return result;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Source-over ink layers share one bitmap geometry and alpha convention. */
|
|
71
|
+
export function overlayRgba(target: Uint8Array, layer: Uint8Array) {
|
|
72
|
+
if (target.length !== layer.length || target.length % 4)
|
|
73
|
+
throw new RokuHostError("C04_RASTER_PIXELS", "Mismatched mixed-text layers");
|
|
74
|
+
for (let index = 0; index < target.length; index += 4) {
|
|
75
|
+
const front = layer[index + 3] / 255,
|
|
76
|
+
back = target[index + 3] / 255;
|
|
77
|
+
const alpha = front + back * (1 - front);
|
|
78
|
+
if (!front) continue;
|
|
79
|
+
for (let channel = 0; channel < 3; channel++)
|
|
80
|
+
target[index + channel] = Math.round(
|
|
81
|
+
(layer[index + channel] * front + target[index + channel] * back * (1 - front)) / alpha,
|
|
82
|
+
);
|
|
83
|
+
target[index + 3] = Math.round(alpha * 255);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
2
|
+
const table = new Uint32Array(256);
|
|
3
|
+
for (let index = 0; index < 256; index++) {
|
|
4
|
+
let value = index;
|
|
5
|
+
for (let bit = 0; bit < 8; bit++) value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
|
|
6
|
+
table[index] = value >>> 0;
|
|
7
|
+
}
|
|
8
|
+
function crc(bytes: Uint8Array): number {
|
|
9
|
+
let value = 0xffffffff;
|
|
10
|
+
for (const byte of bytes) value = table[(value ^ byte) & 255] ^ (value >>> 8);
|
|
11
|
+
return (value ^ 0xffffffff) >>> 0;
|
|
12
|
+
}
|
|
13
|
+
function u32(value: number): number[] {
|
|
14
|
+
return [(value >>> 24) & 255, (value >>> 16) & 255, (value >>> 8) & 255, value & 255];
|
|
15
|
+
}
|
|
16
|
+
function chunk(name: string, data: Uint8Array): Uint8Array {
|
|
17
|
+
const payload = new Uint8Array(data.length + 4),
|
|
18
|
+
result = new Uint8Array(data.length + 12);
|
|
19
|
+
payload.set(Array.from(name, (char) => char.charCodeAt(0)));
|
|
20
|
+
payload.set(data, 4);
|
|
21
|
+
result.set(u32(data.length));
|
|
22
|
+
result.set(payload, 4);
|
|
23
|
+
result.set(u32(crc(payload)), data.length + 8);
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
/** Portable RGBA PNG writer using bounded stored DEFLATE blocks; no eval, Buffer or native codec. */
|
|
27
|
+
export function rgbaPng(width: number, height: number, rgba: Uint8Array): Uint8Array {
|
|
28
|
+
if (
|
|
29
|
+
![width, height].every((value) => Number.isInteger(value) && value > 0 && value <= 2048) ||
|
|
30
|
+
rgba.length !== width * height * 4
|
|
31
|
+
)
|
|
32
|
+
throw new RokuHostError("C04_PNG", "Invalid RGBA bitmap geometry");
|
|
33
|
+
const stride = width * 4,
|
|
34
|
+
raw = new Uint8Array((stride + 1) * height);
|
|
35
|
+
for (let row = 0; row < height; row++)
|
|
36
|
+
raw.set(rgba.subarray(row * stride, (row + 1) * stride), row * (stride + 1) + 1);
|
|
37
|
+
const blocks = Math.ceil(raw.length / 65535),
|
|
38
|
+
zlib = new Uint8Array(2 + blocks * 5 + raw.length + 4);
|
|
39
|
+
zlib.set([0x78, 0x01]);
|
|
40
|
+
let cursor = 2,
|
|
41
|
+
a = 1,
|
|
42
|
+
b = 0;
|
|
43
|
+
for (let start = 0; start < raw.length; start += 65535) {
|
|
44
|
+
const length = Math.min(65535, raw.length - start);
|
|
45
|
+
zlib.set(
|
|
46
|
+
[
|
|
47
|
+
start + length === raw.length ? 1 : 0,
|
|
48
|
+
length & 255,
|
|
49
|
+
length >>> 8,
|
|
50
|
+
~length & 255,
|
|
51
|
+
(~length >>> 8) & 255,
|
|
52
|
+
],
|
|
53
|
+
cursor,
|
|
54
|
+
);
|
|
55
|
+
cursor += 5;
|
|
56
|
+
zlib.set(raw.subarray(start, start + length), cursor);
|
|
57
|
+
cursor += length;
|
|
58
|
+
}
|
|
59
|
+
for (const byte of raw) {
|
|
60
|
+
a = (a + byte) % 65521;
|
|
61
|
+
b = (b + a) % 65521;
|
|
62
|
+
}
|
|
63
|
+
zlib.set(u32(((b << 16) | a) >>> 0), cursor);
|
|
64
|
+
const chunks = [
|
|
65
|
+
Uint8Array.from([137, 80, 78, 71, 13, 10, 26, 10]),
|
|
66
|
+
chunk("IHDR", Uint8Array.from([...u32(width), ...u32(height), 8, 6, 0, 0, 0])),
|
|
67
|
+
chunk("IDAT", zlib),
|
|
68
|
+
chunk("IEND", new Uint8Array()),
|
|
69
|
+
];
|
|
70
|
+
const result = new Uint8Array(chunks.reduce((sum, value) => sum + value.length, 0));
|
|
71
|
+
cursor = 0;
|
|
72
|
+
for (const value of chunks) {
|
|
73
|
+
result.set(value, cursor);
|
|
74
|
+
cursor += value.length;
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
2
|
+
/** Separable Gaussian convolution of the complete text mask, preserving overlap. */
|
|
3
|
+
export function gaussianAlpha(
|
|
4
|
+
source: Uint8Array,
|
|
5
|
+
width: number,
|
|
6
|
+
height: number,
|
|
7
|
+
radius: number,
|
|
8
|
+
): Float32Array {
|
|
9
|
+
if (
|
|
10
|
+
source.length !== width * height ||
|
|
11
|
+
!Number.isSafeInteger(width) ||
|
|
12
|
+
!Number.isSafeInteger(height) ||
|
|
13
|
+
width < 1 ||
|
|
14
|
+
height < 1 ||
|
|
15
|
+
!Number.isFinite(radius) ||
|
|
16
|
+
radius < 0 ||
|
|
17
|
+
radius > 64
|
|
18
|
+
)
|
|
19
|
+
throw new RokuHostError("C04_RASTER_PIXELS", "Invalid alpha raster or blur radius");
|
|
20
|
+
if (!radius) return Float32Array.from(source, (value) => value / 255);
|
|
21
|
+
const sigma = radius / 2,
|
|
22
|
+
reach = Math.ceil(sigma * 3),
|
|
23
|
+
kernel = Array.from({ length: reach * 2 + 1 }, (_, index) =>
|
|
24
|
+
Math.exp(-((index - reach) ** 2) / (2 * sigma * sigma)),
|
|
25
|
+
),
|
|
26
|
+
sum = kernel.reduce((a, b) => a + b, 0);
|
|
27
|
+
const horizontal = new Float32Array(source.length),
|
|
28
|
+
result = new Float32Array(source.length);
|
|
29
|
+
for (let y = 0; y < height; y++)
|
|
30
|
+
for (let x = 0; x < width; x++) {
|
|
31
|
+
let value = 0;
|
|
32
|
+
for (let k = -reach; k <= reach; k++)
|
|
33
|
+
if (x + k >= 0 && x + k < width) value += source[y * width + x + k] * kernel[k + reach];
|
|
34
|
+
horizontal[y * width + x] = value / (sum * 255);
|
|
35
|
+
}
|
|
36
|
+
for (let y = 0; y < height; y++)
|
|
37
|
+
for (let x = 0; x < width; x++) {
|
|
38
|
+
let value = 0;
|
|
39
|
+
for (let k = -reach; k <= reach; k++)
|
|
40
|
+
if (y + k >= 0 && y + k < height)
|
|
41
|
+
value += horizontal[(y + k) * width + x] * kernel[k + reach];
|
|
42
|
+
result[y * width + x] = value / sum;
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
const rgba = (color: string) => {
|
|
47
|
+
const value = Number(color);
|
|
48
|
+
return [(value >>> 24) & 255, (value >>> 16) & 255, (value >>> 8) & 255, (value & 255) / 255];
|
|
49
|
+
};
|
|
50
|
+
/** Source-over composition in straight RGBA, with subpixel shadow translation. */
|
|
51
|
+
export function paintTextMask(
|
|
52
|
+
mask: Uint8Array,
|
|
53
|
+
width: number,
|
|
54
|
+
height: number,
|
|
55
|
+
color: string,
|
|
56
|
+
shadowColor: string,
|
|
57
|
+
radius: number,
|
|
58
|
+
offsetX: number,
|
|
59
|
+
offsetY: number,
|
|
60
|
+
): Uint8Array {
|
|
61
|
+
const ink = rgba(color),
|
|
62
|
+
shadow = rgba(shadowColor),
|
|
63
|
+
blurred = gaussianAlpha(mask, width, height, shadow[3] ? radius : 0),
|
|
64
|
+
result = new Uint8Array(mask.length * 4);
|
|
65
|
+
const sample = (x: number, y: number) =>
|
|
66
|
+
x < 0 || y < 0 || x >= width || y >= height ? 0 : blurred[y * width + x];
|
|
67
|
+
for (let y = 0; y < height; y++)
|
|
68
|
+
for (let x = 0; x < width; x++) {
|
|
69
|
+
const px = x - offsetX,
|
|
70
|
+
py = y - offsetY,
|
|
71
|
+
ix = Math.floor(px),
|
|
72
|
+
iy = Math.floor(py),
|
|
73
|
+
fx = px - ix,
|
|
74
|
+
fy = py - iy;
|
|
75
|
+
const back =
|
|
76
|
+
((sample(ix, iy) * (1 - fx) + sample(ix + 1, iy) * fx) * (1 - fy) +
|
|
77
|
+
(sample(ix, iy + 1) * (1 - fx) + sample(ix + 1, iy + 1) * fx) * fy) *
|
|
78
|
+
shadow[3];
|
|
79
|
+
const front = (mask[y * width + x] / 255) * ink[3],
|
|
80
|
+
alpha = front + back * (1 - front),
|
|
81
|
+
index = (y * width + x) * 4;
|
|
82
|
+
for (let channel = 0; channel < 3; channel++)
|
|
83
|
+
result[index + channel] = alpha
|
|
84
|
+
? Math.round((ink[channel] * front + shadow[channel] * back * (1 - front)) / alpha)
|
|
85
|
+
: 0;
|
|
86
|
+
result[index + 3] = Math.round(alpha * 255);
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { LayoutStyle } from "../layout/flexLayout";
|
|
2
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
3
|
+
export interface NativeGraphics {
|
|
4
|
+
readonly effects: boolean;
|
|
5
|
+
/** The standalone hosts provide a bounded bitmap transport for composed masks. */
|
|
6
|
+
readonly composedMasks?: boolean;
|
|
7
|
+
/** Four alpha corner rasters in TL, TR, BR, BL order, generated from the same circle geometry. */
|
|
8
|
+
readonly cornerMasks?: readonly [string, string, string, string];
|
|
9
|
+
}
|
|
10
|
+
const cornerFields = [
|
|
11
|
+
"borderTopLeftRadius",
|
|
12
|
+
"borderTopRightRadius",
|
|
13
|
+
"borderBottomRightRadius",
|
|
14
|
+
"borderBottomLeftRadius",
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
export function hasRoundedCorners(style: LayoutStyle): boolean {
|
|
18
|
+
let rounded = false;
|
|
19
|
+
for (const field of cornerFields) {
|
|
20
|
+
const value = style[field] ?? style.borderRadius ?? 0;
|
|
21
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0)
|
|
22
|
+
throw new RokuHostError(
|
|
23
|
+
"C03_ROUNDED_SURFACE",
|
|
24
|
+
"Corner radii must be finite nonnegative numbers",
|
|
25
|
+
);
|
|
26
|
+
if (value > 0) rounded = true;
|
|
27
|
+
}
|
|
28
|
+
return rounded;
|
|
29
|
+
}
|
|
30
|
+
export function surfaceRadii(
|
|
31
|
+
style: LayoutStyle,
|
|
32
|
+
width: number,
|
|
33
|
+
height: number,
|
|
34
|
+
): [number, number, number, number] {
|
|
35
|
+
const radii = cornerFields.map((field) => {
|
|
36
|
+
const value = style[field] ?? style.borderRadius ?? 0;
|
|
37
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0)
|
|
38
|
+
throw new RokuHostError(
|
|
39
|
+
"C03_ROUNDED_SURFACE",
|
|
40
|
+
"Corner radii must be finite nonnegative numbers",
|
|
41
|
+
);
|
|
42
|
+
return value;
|
|
43
|
+
});
|
|
44
|
+
const factor = Math.min(
|
|
45
|
+
1,
|
|
46
|
+
...[
|
|
47
|
+
[width, radii[0] + radii[1]],
|
|
48
|
+
[width, radii[2] + radii[3]],
|
|
49
|
+
[height, radii[0] + radii[3]],
|
|
50
|
+
[height, radii[1] + radii[2]],
|
|
51
|
+
].map(([limit, sum]) => (sum ? limit / sum : 1)),
|
|
52
|
+
);
|
|
53
|
+
return radii.map((radius) => radius * Math.max(0, factor)) as [number, number, number, number];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A single alpha surface clips the entire subtree in SceneGraph. */
|
|
57
|
+
export function roundedMask(style: LayoutStyle, width: number, height: number) {
|
|
58
|
+
const radii = surfaceRadii(style, width, height);
|
|
59
|
+
const bitmapWidth = Math.max(1, Math.min(2048, Math.ceil(width)));
|
|
60
|
+
const bitmapHeight = Math.max(1, Math.min(2048, Math.ceil(height)));
|
|
61
|
+
return {
|
|
62
|
+
key: `rounded-mask:${width}:${height}:${radii.join(",")}`,
|
|
63
|
+
width: bitmapWidth,
|
|
64
|
+
height: bitmapHeight,
|
|
65
|
+
pixels() {
|
|
66
|
+
const pixels = new Uint8Array(bitmapWidth * bitmapHeight * 4).fill(255);
|
|
67
|
+
const stepX = width / bitmapWidth,
|
|
68
|
+
stepY = height / bitmapHeight;
|
|
69
|
+
const aa = Math.hypot(stepX, stepY) / 2;
|
|
70
|
+
radii.forEach((radius, corner) => {
|
|
71
|
+
if (!radius || !aa) return;
|
|
72
|
+
const right = corner === 1 || corner === 2;
|
|
73
|
+
const bottom = corner >= 2;
|
|
74
|
+
for (let y = 0; y < Math.min(bitmapHeight, Math.ceil(radius / stepY)); y++) {
|
|
75
|
+
for (let x = 0; x < Math.min(bitmapWidth, Math.ceil(radius / stepX)); x++) {
|
|
76
|
+
const edgeX = radius - (x + 0.5) * stepX;
|
|
77
|
+
const edgeY = radius - (y + 0.5) * stepY;
|
|
78
|
+
if (edgeX <= 0 || edgeY <= 0) continue;
|
|
79
|
+
const coverage = (radius - Math.hypot(edgeX, edgeY) + aa) / (2 * aa);
|
|
80
|
+
const row = bottom ? bitmapHeight - y - 1 : y;
|
|
81
|
+
const column = right ? bitmapWidth - x - 1 : x;
|
|
82
|
+
const alpha = (row * bitmapWidth + column) * 4 + 3;
|
|
83
|
+
pixels[alpha] = Math.min(pixels[alpha], Math.round(255 * Math.max(0, coverage)));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return pixels;
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export function uniformBorder(style: LayoutStyle): number {
|
|
92
|
+
const width = style.borderWidth ?? 0;
|
|
93
|
+
if (typeof width !== "number" || width < 0 || !Number.isFinite(width))
|
|
94
|
+
throw new RokuHostError("C03_BORDER", "Border widths must be finite and nonnegative");
|
|
95
|
+
for (const edge of ["Top", "Right", "Bottom", "Left"])
|
|
96
|
+
if (
|
|
97
|
+
(style[`border${edge}Width`] != null && style[`border${edge}Width`] !== width) ||
|
|
98
|
+
(style[`border${edge}Color`] != null && style[`border${edge}Color`] !== style.borderColor)
|
|
99
|
+
)
|
|
100
|
+
throw new RokuHostError(
|
|
101
|
+
"C03_ROUNDED_BORDER",
|
|
102
|
+
"Asymmetric rounded borders need a per-edge paint adapter",
|
|
103
|
+
);
|
|
104
|
+
return width;
|
|
105
|
+
}
|