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,496 @@
|
|
|
1
|
+
import { RokuHostError, type SceneNodeRef, type SceneValue } from "../scene/sceneGraph.types";
|
|
2
|
+
import { FontResources, type FontResourcesOptions } from "../layout/FontResources";
|
|
3
|
+
import { RecordingSceneGraph, type RecordedSceneNode } from "./RecordingSceneGraph";
|
|
4
|
+
import { TextRasterCache } from "../graphics/TextRasterCache";
|
|
5
|
+
import { encodeBase64 } from "../services/base64";
|
|
6
|
+
import type { TextMetricProvider } from "../layout/textLayout";
|
|
7
|
+
export { createCanvasTextEntry, type CanvasTextEntryPort } from "./CanvasTextEntry";
|
|
8
|
+
export { createCanvasFontRegistry } from "./CanvasResources";
|
|
9
|
+
export { observePreviewText } from "./PreviewTextObservations";
|
|
10
|
+
|
|
11
|
+
export interface CanvasFactory {
|
|
12
|
+
image(): HTMLImageElement;
|
|
13
|
+
canvas(width: number, height: number): HTMLCanvasElement;
|
|
14
|
+
uri(uri: string): string;
|
|
15
|
+
requestFrame(callback: () => void): number;
|
|
16
|
+
cancelFrame(handle: number): void;
|
|
17
|
+
}
|
|
18
|
+
const browserFactory: CanvasFactory = {
|
|
19
|
+
image: () => new globalThis.Image(),
|
|
20
|
+
canvas(width, height) {
|
|
21
|
+
const value = document.createElement("canvas");
|
|
22
|
+
value.width = width;
|
|
23
|
+
value.height = height;
|
|
24
|
+
return value;
|
|
25
|
+
},
|
|
26
|
+
uri: (uri) => (uri.startsWith("pkg:/images/") ? `/assets/${uri.slice(12)}` : uri),
|
|
27
|
+
requestFrame: (callback) => requestAnimationFrame(callback),
|
|
28
|
+
cancelFrame: (handle) => cancelAnimationFrame(handle),
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function color(value: unknown): string {
|
|
32
|
+
if (typeof value !== "string" || !/^0x[\da-f]{8}$/i.test(value))
|
|
33
|
+
throw new RokuHostError("PREVIEW_COLOR", `Unrecognized SceneGraph color ${String(value)}`);
|
|
34
|
+
return `#${value.slice(2)}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function releaseBitmap(image?: HTMLImageElement): void {
|
|
38
|
+
if (!image) return;
|
|
39
|
+
image.onload = null;
|
|
40
|
+
image.onerror = null;
|
|
41
|
+
image.removeAttribute("src");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A visual host harness using the same React and Yoga renderer. It is not a Roku emulator. */
|
|
45
|
+
export class CanvasSceneGraph extends RecordingSceneGraph {
|
|
46
|
+
readonly focusRequests: string[] = [];
|
|
47
|
+
focusRequestSequence = 0;
|
|
48
|
+
private animationFrame: number | undefined;
|
|
49
|
+
private disposed = false;
|
|
50
|
+
private readonly images = new Map<
|
|
51
|
+
number,
|
|
52
|
+
{
|
|
53
|
+
image: HTMLImageElement;
|
|
54
|
+
ready: boolean;
|
|
55
|
+
failed?: boolean;
|
|
56
|
+
tint?: string;
|
|
57
|
+
tinted?: HTMLCanvasElement;
|
|
58
|
+
}
|
|
59
|
+
>();
|
|
60
|
+
private readonly maskLoads = new Map<number, HTMLImageElement>();
|
|
61
|
+
readonly context: CanvasRenderingContext2D;
|
|
62
|
+
constructor(
|
|
63
|
+
readonly canvas: HTMLCanvasElement,
|
|
64
|
+
readonly factory: CanvasFactory = browserFactory,
|
|
65
|
+
) {
|
|
66
|
+
super();
|
|
67
|
+
const context = canvas.getContext("2d");
|
|
68
|
+
if (!context) throw new RokuHostError("PREVIEW_CANVAS", "A Canvas 2D context is required");
|
|
69
|
+
this.context = context;
|
|
70
|
+
}
|
|
71
|
+
override batch(work: () => void): void {
|
|
72
|
+
super.batch(work);
|
|
73
|
+
// Recording is bounded in a long-running interactive preview.
|
|
74
|
+
this.operations.length = 0;
|
|
75
|
+
if (this.animationFrame === undefined && !this.disposed)
|
|
76
|
+
this.animationFrame = this.factory.requestFrame(() => {
|
|
77
|
+
this.animationFrame = undefined;
|
|
78
|
+
this.draw();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
override set(ref: SceneNodeRef, field: string, value: SceneValue): void {
|
|
82
|
+
super.set(ref, field, value);
|
|
83
|
+
if (field === "gainFocus" && value === true) {
|
|
84
|
+
const id = this.nodes.get(ref.id)?.fields.id;
|
|
85
|
+
if (typeof id === "string") {
|
|
86
|
+
this.focusRequests.push(id);
|
|
87
|
+
this.focusRequestSequence++;
|
|
88
|
+
if (this.focusRequests.length > 100) this.focusRequests.shift();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (ref.type === "maskGroup" && field === "maskUri") {
|
|
92
|
+
releaseBitmap(this.maskLoads.get(ref.id));
|
|
93
|
+
this.maskLoads.delete(ref.id);
|
|
94
|
+
if (value) {
|
|
95
|
+
const image = this.factory.image();
|
|
96
|
+
this.maskLoads.set(ref.id, image);
|
|
97
|
+
image.onload = () => {
|
|
98
|
+
// Cached host decoders may invoke onload synchronously. Native bridge
|
|
99
|
+
// observations arrive after the transaction, once its nodes are live.
|
|
100
|
+
queueMicrotask(() => {
|
|
101
|
+
if (this.disposed || this.maskLoads.get(ref.id) !== image || !this.nodes.has(ref.id))
|
|
102
|
+
return;
|
|
103
|
+
this.emit(ref, "maskBitmapWidth", image.naturalWidth);
|
|
104
|
+
this.emit(ref, "maskBitmapHeight", image.naturalHeight);
|
|
105
|
+
this.batch(() => {});
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
image.src = this.factory.uri(String(value));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (ref.type !== "poster" || field !== "uri") return;
|
|
112
|
+
releaseBitmap(this.images.get(ref.id)?.image);
|
|
113
|
+
this.images.delete(ref.id);
|
|
114
|
+
if (!value) return;
|
|
115
|
+
const image = this.factory.image(),
|
|
116
|
+
entry = { image, ready: false, failed: false };
|
|
117
|
+
this.images.set(ref.id, entry);
|
|
118
|
+
image.onload = () => {
|
|
119
|
+
queueMicrotask(() => {
|
|
120
|
+
if (this.disposed || this.images.get(ref.id) !== entry || !this.nodes.has(ref.id)) return;
|
|
121
|
+
entry.ready = true;
|
|
122
|
+
this.emit(ref, "bitmapWidth", image.naturalWidth);
|
|
123
|
+
this.emit(ref, "bitmapHeight", image.naturalHeight);
|
|
124
|
+
this.emit(ref, "loadStatus", "ready");
|
|
125
|
+
this.batch(() => {});
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
image.onerror = () => {
|
|
129
|
+
queueMicrotask(() => {
|
|
130
|
+
entry.failed = true;
|
|
131
|
+
if (!this.disposed && this.images.get(ref.id) === entry)
|
|
132
|
+
this.emit(ref, "loadStatus", "failed");
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
image.src = this.factory.uri(String(value ?? ""));
|
|
136
|
+
}
|
|
137
|
+
override remove(parent: SceneNodeRef, child: SceneNodeRef): void {
|
|
138
|
+
const retire = (node: RecordedSceneNode) => {
|
|
139
|
+
releaseBitmap(this.images.get(node.id)?.image);
|
|
140
|
+
releaseBitmap(this.maskLoads.get(node.id));
|
|
141
|
+
this.images.delete(node.id);
|
|
142
|
+
this.maskLoads.delete(node.id);
|
|
143
|
+
node.children.forEach(retire);
|
|
144
|
+
};
|
|
145
|
+
const node = this.nodes.get(child.id);
|
|
146
|
+
if (node) retire(node);
|
|
147
|
+
super.remove(parent, child);
|
|
148
|
+
}
|
|
149
|
+
fontDescription(ref: SceneNodeRef) {
|
|
150
|
+
const font = this.nodes.get(ref.id);
|
|
151
|
+
if (!font || font !== ref)
|
|
152
|
+
throw new RokuHostError("STALE_NODE", "The preview font was retired");
|
|
153
|
+
// A font family identifies the asset, while size remains a separate Canvas
|
|
154
|
+
// property. Stable names avoid re-registering the same font for every root
|
|
155
|
+
// and every text size in sustained host qualification.
|
|
156
|
+
let hash = 2166136261;
|
|
157
|
+
for (const character of String(font.fields.uri))
|
|
158
|
+
hash = Math.imul(hash ^ character.charCodeAt(0), 16777619);
|
|
159
|
+
return {
|
|
160
|
+
family: `react-native-roku-font-${(hash >>> 0).toString(36)}`,
|
|
161
|
+
size: Number(font.fields.size),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
imageDiagnostics() {
|
|
165
|
+
return [...this.images].map(([id, entry]) => ({
|
|
166
|
+
id,
|
|
167
|
+
uri: String(this.nodes.get(id)?.fields.uri ?? "").startsWith("data:")
|
|
168
|
+
? "data:image/[generated raster]"
|
|
169
|
+
: String(this.nodes.get(id)?.fields.uri ?? ""),
|
|
170
|
+
status: entry.failed ? "failed" : entry.ready ? "ready" : "loading",
|
|
171
|
+
width: entry.image.naturalWidth,
|
|
172
|
+
height: entry.image.naturalHeight,
|
|
173
|
+
}));
|
|
174
|
+
}
|
|
175
|
+
dispose() {
|
|
176
|
+
if (this.disposed) return;
|
|
177
|
+
this.disposed = true;
|
|
178
|
+
for (const entry of this.images.values()) releaseBitmap(entry.image);
|
|
179
|
+
for (const image of this.maskLoads.values()) releaseBitmap(image);
|
|
180
|
+
this.images.clear();
|
|
181
|
+
this.maskLoads.clear();
|
|
182
|
+
if (this.animationFrame !== undefined) this.factory.cancelFrame(this.animationFrame);
|
|
183
|
+
}
|
|
184
|
+
draw() {
|
|
185
|
+
const ctx = this.context;
|
|
186
|
+
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
187
|
+
const visit = (node: RecordedSceneNode, ctx = this.context) => {
|
|
188
|
+
const fields = node.fields;
|
|
189
|
+
if (fields.visible === false) return;
|
|
190
|
+
ctx.save();
|
|
191
|
+
const translation = fields.translation as number[] | undefined,
|
|
192
|
+
scale = fields.scale as number[] | undefined,
|
|
193
|
+
center = fields.scaleRotateCenter as number[] | undefined;
|
|
194
|
+
if (translation) ctx.translate(translation[0], translation[1]);
|
|
195
|
+
if (center) ctx.translate(center[0], center[1]);
|
|
196
|
+
if (typeof fields.rotation === "number") ctx.rotate(fields.rotation);
|
|
197
|
+
if (scale) ctx.scale(scale[0], scale[1]);
|
|
198
|
+
if (center) ctx.translate(-center[0], -center[1]);
|
|
199
|
+
if (typeof fields.opacity === "number") ctx.globalAlpha *= fields.opacity;
|
|
200
|
+
if (Array.isArray(fields.clippingRect)) {
|
|
201
|
+
const clip = fields.clippingRect as number[];
|
|
202
|
+
ctx.beginPath();
|
|
203
|
+
ctx.rect(clip[0], clip[1], clip[2], clip[3]);
|
|
204
|
+
ctx.clip();
|
|
205
|
+
}
|
|
206
|
+
if (
|
|
207
|
+
node.type === "maskGroup" &&
|
|
208
|
+
fields.maskUri &&
|
|
209
|
+
(fields.maskSize as number[] | undefined)?.[0]
|
|
210
|
+
) {
|
|
211
|
+
const size = fields.maskSize as number[],
|
|
212
|
+
offset = fields.maskOffset as number[],
|
|
213
|
+
radius = (size[0] * 256) / 257;
|
|
214
|
+
const corner = /corner-(\d)\.png$/.exec(String(fields.maskUri));
|
|
215
|
+
if (!corner) {
|
|
216
|
+
const image = this.maskLoads.get(node.id);
|
|
217
|
+
if (image?.naturalWidth && image.naturalHeight) {
|
|
218
|
+
const surface = this.factory.canvas(image.naturalWidth, image.naturalHeight);
|
|
219
|
+
const layer = surface.getContext("2d")!;
|
|
220
|
+
layer.scale(image.naturalWidth / size[0], image.naturalHeight / size[1]);
|
|
221
|
+
for (const child of node.children) visit(child, layer);
|
|
222
|
+
layer.globalCompositeOperation = "destination-in";
|
|
223
|
+
layer.drawImage(image, 0, 0, size[0], size[1]);
|
|
224
|
+
ctx.drawImage(surface, 0, 0, size[0], size[1]);
|
|
225
|
+
}
|
|
226
|
+
ctx.restore();
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const index = Number(corner[1]),
|
|
230
|
+
extent = 100_000,
|
|
231
|
+
left = index === 1 || index === 2 ? -extent : 0,
|
|
232
|
+
top = index === 2 || index === 3 ? -extent : 0;
|
|
233
|
+
const right = index === 1 || index === 2 ? offset[0] + radius : extent,
|
|
234
|
+
bottom = index === 2 || index === 3 ? offset[1] + radius : extent;
|
|
235
|
+
const radii = [0, 0, 0, 0];
|
|
236
|
+
radii[index] = radius;
|
|
237
|
+
ctx.beginPath();
|
|
238
|
+
ctx.roundRect(left, top, right - left, bottom - top, radii);
|
|
239
|
+
ctx.clip();
|
|
240
|
+
}
|
|
241
|
+
const effect = fields.effect ? this.nodes.get((fields.effect as SceneNodeRef).id) : undefined;
|
|
242
|
+
const border = Number(effect?.fields.borderWidth ?? 0),
|
|
243
|
+
radii = effect?.fields.borderRadius as number[] | undefined;
|
|
244
|
+
const rounded = () => {
|
|
245
|
+
ctx.beginPath();
|
|
246
|
+
ctx.roundRect(0, 0, Number(fields.width), Number(fields.height), radii ?? 0);
|
|
247
|
+
};
|
|
248
|
+
if (node.type === "rectangle") {
|
|
249
|
+
ctx.fillStyle = color(fields.color);
|
|
250
|
+
if (effect?.fields.gradientStyle === "linear") {
|
|
251
|
+
const angle = (Number(effect.fields.gradientAngle ?? 0) * Math.PI) / 180,
|
|
252
|
+
dx = Math.sin(angle),
|
|
253
|
+
dy = -Math.cos(angle),
|
|
254
|
+
width = Number(fields.width),
|
|
255
|
+
height = Number(fields.height);
|
|
256
|
+
const minimum = Math.min(0, width * dx) + Math.min(0, height * dy),
|
|
257
|
+
maximum = Math.max(0, width * dx) + Math.max(0, height * dy),
|
|
258
|
+
cx = width / 2,
|
|
259
|
+
cy = height / 2,
|
|
260
|
+
center = cx * dx + cy * dy;
|
|
261
|
+
const gradient = ctx.createLinearGradient(
|
|
262
|
+
cx + dx * (minimum - center),
|
|
263
|
+
cy + dy * (minimum - center),
|
|
264
|
+
cx + dx * (maximum - center),
|
|
265
|
+
cy + dy * (maximum - center),
|
|
266
|
+
);
|
|
267
|
+
const colors = effect.fields.gradientColors as string[],
|
|
268
|
+
stops = effect.fields.gradientStops as number[] | undefined;
|
|
269
|
+
colors.forEach((value, index) =>
|
|
270
|
+
gradient.addColorStop(stops?.[index] ?? index / (colors.length - 1), color(value)),
|
|
271
|
+
);
|
|
272
|
+
ctx.fillStyle = gradient;
|
|
273
|
+
}
|
|
274
|
+
if (effect) {
|
|
275
|
+
rounded();
|
|
276
|
+
ctx.fill();
|
|
277
|
+
} else ctx.fillRect(0, 0, Number(fields.width), Number(fields.height));
|
|
278
|
+
if (border) {
|
|
279
|
+
ctx.beginPath();
|
|
280
|
+
ctx.roundRect(
|
|
281
|
+
-border / 2,
|
|
282
|
+
-border / 2,
|
|
283
|
+
Number(fields.width) + border,
|
|
284
|
+
Number(fields.height) + border,
|
|
285
|
+
radii?.map((radius) => radius + border / 2) ?? 0,
|
|
286
|
+
);
|
|
287
|
+
ctx.strokeStyle = color(effect!.fields.borderColor);
|
|
288
|
+
ctx.lineWidth = border;
|
|
289
|
+
ctx.stroke();
|
|
290
|
+
}
|
|
291
|
+
} else if (node.type === "poster") {
|
|
292
|
+
if (effect) {
|
|
293
|
+
rounded();
|
|
294
|
+
ctx.clip();
|
|
295
|
+
}
|
|
296
|
+
const entry = this.images.get(node.id);
|
|
297
|
+
if (entry?.ready) {
|
|
298
|
+
let bitmap: CanvasImageSource = entry.image;
|
|
299
|
+
if (typeof fields.blendColor === "string") {
|
|
300
|
+
const tint = color(fields.blendColor);
|
|
301
|
+
if (entry.tint !== tint) {
|
|
302
|
+
const surface = this.factory.canvas(
|
|
303
|
+
entry.image.naturalWidth,
|
|
304
|
+
entry.image.naturalHeight,
|
|
305
|
+
);
|
|
306
|
+
const painting = surface.getContext("2d")!;
|
|
307
|
+
painting.drawImage(entry.image, 0, 0);
|
|
308
|
+
painting.globalCompositeOperation = "source-in";
|
|
309
|
+
painting.fillStyle = tint;
|
|
310
|
+
painting.fillRect(0, 0, surface.width, surface.height);
|
|
311
|
+
entry.tinted = surface;
|
|
312
|
+
entry.tint = tint;
|
|
313
|
+
}
|
|
314
|
+
bitmap = entry.tinted!;
|
|
315
|
+
}
|
|
316
|
+
const width = Number(fields.width),
|
|
317
|
+
height = Number(fields.height),
|
|
318
|
+
iw = entry.image.naturalWidth,
|
|
319
|
+
ih = entry.image.naturalHeight;
|
|
320
|
+
const mode = fields.loadDisplayMode ?? "scaleToFill";
|
|
321
|
+
const scale =
|
|
322
|
+
mode === "scaleToZoom"
|
|
323
|
+
? Math.max(width / iw, height / ih)
|
|
324
|
+
: Math.min(width / iw, height / ih);
|
|
325
|
+
const dw = mode === "scaleToFill" || mode === "noScale" ? width : iw * scale,
|
|
326
|
+
dh = mode === "scaleToFill" || mode === "noScale" ? height : ih * scale;
|
|
327
|
+
ctx.beginPath();
|
|
328
|
+
ctx.rect(0, 0, width, height);
|
|
329
|
+
ctx.clip();
|
|
330
|
+
ctx.drawImage(bitmap, (width - dw) / 2, (height - dh) / 2, dw, dh);
|
|
331
|
+
}
|
|
332
|
+
} else if (node.type === "multiStyleLabel") {
|
|
333
|
+
const styles = fields.drawingStyles as Record<
|
|
334
|
+
string,
|
|
335
|
+
{ fontUri: string; fontSize: number; color: string }
|
|
336
|
+
>;
|
|
337
|
+
const runs = [...String(fields.text ?? "").matchAll(/<(s\d+)>([\s\S]*?)<\/\1>/g)].map(
|
|
338
|
+
(match) => ({
|
|
339
|
+
style: styles[match[1]],
|
|
340
|
+
text: match[2].replaceAll("<", "<").replaceAll(">", ">").replaceAll("&", "&"),
|
|
341
|
+
}),
|
|
342
|
+
);
|
|
343
|
+
const selectFont = (style: (typeof runs)[number]["style"]) => {
|
|
344
|
+
const ref = [...this.nodes.values()].find(
|
|
345
|
+
(node) =>
|
|
346
|
+
node.type === "font" &&
|
|
347
|
+
node.fields.uri === style.fontUri &&
|
|
348
|
+
Number(node.fields.size) === style.fontSize,
|
|
349
|
+
);
|
|
350
|
+
if (!ref)
|
|
351
|
+
throw new RokuHostError("PREVIEW_INLINE_FONT", "A mixed text run has no prepared font");
|
|
352
|
+
ctx.font = `${style.fontSize}px "${this.fontDescription(ref).family}"`;
|
|
353
|
+
ctx.fillStyle = color(style.color);
|
|
354
|
+
ctx.textBaseline = "top";
|
|
355
|
+
};
|
|
356
|
+
const width = Number(fields.width),
|
|
357
|
+
height = Number(fields.height),
|
|
358
|
+
total = runs.reduce((sum, run) => {
|
|
359
|
+
selectFont(run.style);
|
|
360
|
+
return sum + ctx.measureText(run.text).width;
|
|
361
|
+
}, 0);
|
|
362
|
+
ctx.beginPath();
|
|
363
|
+
ctx.rect(0, 0, width, height);
|
|
364
|
+
ctx.clip();
|
|
365
|
+
let x =
|
|
366
|
+
fields.horizAlign === "center"
|
|
367
|
+
? (width - Math.min(width, total)) / 2
|
|
368
|
+
: fields.horizAlign === "right"
|
|
369
|
+
? width - Math.min(width, total)
|
|
370
|
+
: 0,
|
|
371
|
+
truncated = false;
|
|
372
|
+
for (const run of runs) {
|
|
373
|
+
selectFont(run.style);
|
|
374
|
+
let value = run.text;
|
|
375
|
+
if (total > width) {
|
|
376
|
+
const points = Array.from(value),
|
|
377
|
+
ellipsis = ctx.measureText("…").width;
|
|
378
|
+
let count = 0;
|
|
379
|
+
while (
|
|
380
|
+
count < points.length &&
|
|
381
|
+
x + ctx.measureText(points.slice(0, count + 1).join("")).width + ellipsis <= width
|
|
382
|
+
)
|
|
383
|
+
count++;
|
|
384
|
+
if (count < points.length) {
|
|
385
|
+
value = points.slice(0, count).join("") + "…";
|
|
386
|
+
truncated = true;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
ctx.fillText(value, x, 0);
|
|
390
|
+
x += ctx.measureText(value).width;
|
|
391
|
+
if (truncated) break;
|
|
392
|
+
}
|
|
393
|
+
} else if (node.type === "label" && fields.font) {
|
|
394
|
+
const { family, size } = this.fontDescription(fields.font as SceneNodeRef);
|
|
395
|
+
ctx.font = `${size}px "${family}"`;
|
|
396
|
+
ctx.fillStyle = color(fields.color);
|
|
397
|
+
ctx.textBaseline = "top";
|
|
398
|
+
const width = Number(fields.width),
|
|
399
|
+
alignment = String(fields.horizAlign ?? "left");
|
|
400
|
+
ctx.textAlign =
|
|
401
|
+
alignment === "center" ? "center" : alignment === "right" ? "right" : "left";
|
|
402
|
+
const x = alignment === "center" ? width / 2 : alignment === "right" ? width : 0;
|
|
403
|
+
const native = ctx.measureText("Mg"),
|
|
404
|
+
height = native.fontBoundingBoxAscent + native.fontBoundingBoxDescent;
|
|
405
|
+
String(fields.text ?? "")
|
|
406
|
+
.split("\n")
|
|
407
|
+
.forEach((line, index) =>
|
|
408
|
+
ctx.fillText(line, x, index * (height + Number(fields.lineSpacing ?? 0))),
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
for (const child of node.children) visit(child, ctx);
|
|
412
|
+
ctx.restore();
|
|
413
|
+
};
|
|
414
|
+
for (const node of this.root.children) visit(node);
|
|
415
|
+
// The standalone preview owns this profiling timeline. Keep React's debug
|
|
416
|
+
// User Timing details bounded rather than retaining every past component tree.
|
|
417
|
+
performance.clearMeasures();
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** Host font-mask source for the same portable Gaussian/PNG pipeline as the native adapter. */
|
|
422
|
+
export function createCanvasTextRaster(
|
|
423
|
+
scene: CanvasSceneGraph,
|
|
424
|
+
text: TextMetricProvider,
|
|
425
|
+
): TextRasterCache {
|
|
426
|
+
return new TextRasterCache({
|
|
427
|
+
async mask(spec) {
|
|
428
|
+
const ref = text.font?.({
|
|
429
|
+
fontFamily: spec.fontFamily,
|
|
430
|
+
fontSize: spec.fontSize / spec.scale,
|
|
431
|
+
});
|
|
432
|
+
if (!ref)
|
|
433
|
+
throw new RokuHostError(
|
|
434
|
+
"PREVIEW_FONT",
|
|
435
|
+
"Canvas text rasterization requires a prepared font reference",
|
|
436
|
+
);
|
|
437
|
+
const { family } = scene.fontDescription(ref),
|
|
438
|
+
surface = scene.factory.canvas(spec.width, spec.height),
|
|
439
|
+
context = surface.getContext("2d")!;
|
|
440
|
+
context.font = `${spec.fontSize}px "${family}"`;
|
|
441
|
+
context.textBaseline = "top";
|
|
442
|
+
context.fillStyle = "white";
|
|
443
|
+
for (const command of spec.commands) context.fillText(command.text, command.x, command.y);
|
|
444
|
+
const pixels = context.getImageData(0, 0, spec.width, spec.height).data,
|
|
445
|
+
alpha = new Uint8Array(spec.width * spec.height);
|
|
446
|
+
for (let index = 0; index < alpha.length; index++) alpha[index] = pixels[index * 4 + 3];
|
|
447
|
+
return alpha;
|
|
448
|
+
},
|
|
449
|
+
async write(_name, bytes) {
|
|
450
|
+
return `data:image/png;base64,${encodeBase64(bytes)}`;
|
|
451
|
+
},
|
|
452
|
+
async delete() {},
|
|
453
|
+
dispose() {},
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export function createCanvasTextMetrics(
|
|
458
|
+
scene: CanvasSceneGraph,
|
|
459
|
+
options: FontResourcesOptions,
|
|
460
|
+
): FontResources {
|
|
461
|
+
const loaded = new Map<number, FontFace>();
|
|
462
|
+
return new (class extends FontResources {
|
|
463
|
+
override dispose() {
|
|
464
|
+
super.dispose();
|
|
465
|
+
for (const font of loaded.values()) document.fonts.delete(font);
|
|
466
|
+
loaded.clear();
|
|
467
|
+
}
|
|
468
|
+
})(
|
|
469
|
+
scene,
|
|
470
|
+
{
|
|
471
|
+
async ready(ref, signal) {
|
|
472
|
+
const node = scene.nodes.get(ref.id)!,
|
|
473
|
+
{ family } = scene.fontDescription(ref);
|
|
474
|
+
const font = new FontFace(family, `url(${JSON.stringify(String(node.fields.uri))})`);
|
|
475
|
+
await font.load();
|
|
476
|
+
if (signal.aborted) throw signal.reason;
|
|
477
|
+
document.fonts.add(font);
|
|
478
|
+
loaded.set(ref.id, font);
|
|
479
|
+
},
|
|
480
|
+
metrics(ref) {
|
|
481
|
+
const { family, size } = scene.fontDescription(ref),
|
|
482
|
+
context = scene.context;
|
|
483
|
+
context.font = `${size}px "${family}"`;
|
|
484
|
+
const measured = context.measureText("Mg");
|
|
485
|
+
return {
|
|
486
|
+
lineHeight: measured.fontBoundingBoxAscent + measured.fontBoundingBoxDescent,
|
|
487
|
+
width(text) {
|
|
488
|
+
context.font = `${size}px "${family}"`;
|
|
489
|
+
return context.measureText(text).width;
|
|
490
|
+
},
|
|
491
|
+
};
|
|
492
|
+
},
|
|
493
|
+
},
|
|
494
|
+
options,
|
|
495
|
+
);
|
|
496
|
+
}
|