@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 423 lines 14.4 KB
Raw
1 // Adapted from opencode (MIT). Copyright (c) 2025 opencode. See LICENSE.opencode.
2 import {
3 initWasm,
4 needsElementDecode,
5 openPresentation,
6 paintSlide,
7 presentationImageBlob,
8 type PresentationHandle,
9 type SlideDisplayList,
10 type SlidePrimitive,
11 } from "@betteroffice/pptx";
12 import { slideFonts } from "./slide-fonts";
13 import type { SlideMethods, SlidePainted, SlideSummary } from "./slide-protocol";
14 import { maxImageSide, type VectorBitmap, type VectorRequest } from "./vector-pictures";
15 import { deckText } from "./slide-text";
16 import { officeImageBitmap } from "./image-bitmap";
17 import { serveWorker, Transfer } from "./worker-rpc";
18 import { PreviewError } from "../previewFailure";
19
20 // One worker holds one open PowerPoint deck: its engine, fonts, laid-out slides, decoded pictures and painting. The
21 // window terminates the worker when the deck closes, which is the only way the engine's memory is given back.
22
23 /** English Metric Units per CSS pixel. */
24 const emuPerPixel = 9525;
25
26 /** The most device pixels one slide's bitmap holds, about a 4K screen: a zoomed slide stays sharp up to it. */
27 const maxBitmapPixels = 3840 * 2160;
28
29 /** The longest side a canvas takes in every browser. */
30 const maxBitmapSide = 16384;
31
32 /**
33 * Shadow pixels a slide may blur, per pixel of its bitmap: sixteen slide-sized shadows before they are dropped. Past
34 * it a slide gives up its shadows within about half a second, rather than painting for many seconds.
35 */
36 const shadowPixelsPerPixel = 16;
37
38 /**
39 * How many bytes of decoded pictures stay for repaints; the least recently drawn go first past it. A deck of camera
40 * photos holds about 17 MB per picture, even scaled down to `maxImageSide`.
41 */
42 const pictureBudget = 128 * 1024 * 1024;
43
44 type Size = { readonly width: number; readonly height: number };
45
46 /**
47 * A picture as the painter draws it: a bitmap, an SVG that the window rasterizes (a worker has no SVG decoder) with
48 * the bitmap it last returned, or nothing when the browser cannot decode it.
49 */
50 type Picture =
51 | { readonly kind: "bitmap"; readonly bitmap: ImageBitmap; readonly size: Size }
52 | {
53 readonly kind: "vector";
54 readonly blob: Blob;
55 bitmap: ImageBitmap | undefined;
56 failed: boolean;
57 }
58 | { readonly kind: "missing" };
59
60 type Deck = ReturnType<typeof createDeck>;
61
62 /** The deck this worker opened, once it has. */
63 type WorkerState = { deck: Deck | undefined };
64
65 const state: WorkerState = { deck: undefined };
66
67 serveWorker<SlideMethods>({
68 open: async (input) => {
69 if (state.deck) throw new Error("The worker already holds a deck");
70
71 await initWasm();
72
73 const bytes = new Uint8Array(input.bytes);
74 const handle = openPresentation(bytes);
75 const snapshot = handle.snapshot();
76 const text = deckText(bytes, snapshot);
77 const fonts = slideFonts(handle, workerFonts());
78
79 await fonts.load(text);
80
81 state.deck = createDeck(handle, fonts.layout);
82
83 const size = {
84 width: snapshot.widthEmu / emuPerPixel,
85 height: snapshot.heightEmu / emuPerPixel,
86 };
87
88 return snapshot.slides.map((slide, index): SlideSummary => ({
89 ...size,
90 hidden: text.hidden[index] ?? false,
91 notes: slide.notes?.trim() || undefined,
92 }));
93 },
94 paint: async (input, signal) => {
95 const deck = required();
96 const painted = await deck.paint(input.index, input.density, signal).finally(deck.trim);
97
98 return painted.kind === "painted" ? new Transfer(painted, [painted.bitmap]) : painted;
99 },
100 vectors: (input) => required().vectors(input),
101 });
102
103 function required() {
104 if (!state.deck) throw new Error("The worker holds no deck");
105
106 return state.deck;
107 }
108
109 /** The worker's own font set, which its canvases draw text with. */
110 function workerFonts() {
111 if (!("fonts" in self)) throw new PreviewError({ code: "presentation-no-worker-fonts" });
112
113 // SAFETY: a worker's global scope is a FontFaceSource, whose `fonts` is a FontFaceSet (CSS Font Loading, exposed to
114 // workers). The window's types declare it on documents only, and workers expose no FontFaceSet to check against.
115 return self.fonts as FontFaceSet;
116 }
117
118 function createDeck(
119 handle: PresentationHandle,
120 layout: (index: number) => Promise<SlideDisplayList>,
121 ) {
122 // A frame depends on the slide alone, so a new scale or pixel ratio paints the frame laid out before.
123 const frames = new Map<number, Promise<SlideDisplayList | undefined>>();
124 const pictures = new Map<string, Promise<Picture>>();
125 // The decoded bitmaps, least recently drawn first, which `trim` keeps within `pictureBudget`.
126 const drawn = new Map<string, ImageBitmap>();
127 const surface = new OffscreenCanvas(1, 1);
128
129 const frame = (index: number) => {
130 const cached = frames.get(index);
131
132 if (cached) return cached;
133
134 const laid = layout(index).catch(() => undefined);
135
136 frames.set(index, laid);
137
138 return laid;
139 };
140
141 const decode = async (asset: string): Promise<Picture> => {
142 const blob = presentationImageBlob(handle.mediaBytes(asset));
143
144 // SVG decodes through an image element, which a worker lacks.
145 if (needsElementDecode(blob)) return { kind: "vector", blob, bitmap: undefined, failed: false };
146
147 const source = await officeImageBitmap(blob).catch(() => undefined);
148
149 if (!source) return { kind: "missing" };
150 const size = { width: source.width, height: source.height };
151 return { kind: "bitmap", bitmap: await bounded(source), size };
152 };
153
154 const picture = (asset: string) => {
155 const cached = pictures.get(asset);
156
157 if (cached) return cached;
158
159 const decoded = decode(asset).catch((): Picture => ({ kind: "missing" }));
160
161 pictures.set(asset, decoded);
162
163 return decoded;
164 };
165
166 const tileScale = async (primitive: SlidePrimitive): Promise<SlidePrimitive> => {
167 if (primitive.kind === "table" || primitive.kind === "chart") {
168 const primitives: SlidePrimitive[] = [];
169 for (const child of primitive.primitives) primitives.push(await tileScale(child));
170 return { ...primitive, primitives };
171 }
172 if (primitive.kind !== "image" || !primitive.tile || !primitive.assetId) return primitive;
173 const found = await picture(primitive.assetId);
174 if (found.kind !== "bitmap") return primitive;
175 return {
176 ...primitive,
177 tile: {
178 ...primitive.tile,
179 scaleX: primitive.tile.scaleX * (found.size.width / found.bitmap.width),
180 scaleY: primitive.tile.scaleY * (found.size.height / found.bitmap.height),
181 },
182 };
183 };
184
185 return {
186 paint: async (index: number, density: number, signal: AbortSignal): Promise<SlidePainted> => {
187 const laid = await frame(index);
188
189 if (!laid) return { kind: "failed" };
190
191 if (signal.aborted) return { kind: "cancelled" };
192
193 const scale = Math.min(
194 density,
195 Math.sqrt(maxBitmapPixels / (laid.width * laid.height)),
196 maxBitmapSide / Math.max(laid.width, laid.height),
197 );
198
199 // One canvas paints every slide: a paint that was withdrawn leaves its pixels for the next to clear, rather than a
200 // slide-sized canvas each until the worker collects garbage. A reset context starts as a new canvas's does.
201 const canvas = surface;
202 const width = Math.max(1, Math.round(laid.width * scale));
203 const height = Math.max(1, Math.round(laid.height * scale));
204
205 if (canvas.width !== width) canvas.width = width;
206
207 if (canvas.height !== height) canvas.height = height;
208
209 const context = canvas.getContext("2d");
210
211 if (!context) return { kind: "failed" };
212
213 context.reset();
214
215 const needs = vectorSizes(laid.primitives, scale);
216 const wanted = new Map<string, VectorRequest>();
217
218 const resolveImage = async (asset: string) => {
219 // A paint that was withdrawn finishes without pictures, and its reply is dropped.
220 if (signal.aborted) return null;
221
222 const found = await picture(asset);
223
224 if (found.kind === "bitmap") {
225 drawn.delete(asset);
226 drawn.set(asset, found.bitmap);
227
228 return found.bitmap;
229 }
230
231 if (found.kind === "missing" || found.failed) return null;
232
233 const need = needs.get(asset);
234
235 if (!covers(found.bitmap, need)) wanted.set(asset, { asset, blob: found.blob, size: need });
236
237 if (found.bitmap) {
238 drawn.delete(asset);
239 drawn.set(asset, found.bitmap);
240 }
241
242 return found.bitmap ?? null;
243 };
244
245 const options = {
246 resolveImage,
247 maxShadowPixels: canvas.width * canvas.height * shadowPixelsPerPixel,
248 };
249
250 const attempt = (list: SlideDisplayList) =>
251 // SAFETY: the painter draws through the members a worker's 2D context shares with the window's, and makes its
252 // scratch canvases with OffscreenCanvas wherever it exists, as it does in a worker. Only the window's context
253 // type is declared, and it has two focus and attribute members the worker's lacks, hence the double cast.
254 paintSlide(context as unknown as CanvasRenderingContext2D, list, scale, 1, options).then(
255 () => true,
256 () => false,
257 );
258
259 const primitives: SlidePrimitive[] = [];
260 for (const primitive of laid.primitives) primitives.push(await tileScale(primitive));
261 const scaled = { ...laid, primitives };
262 const shadowed = await attempt(scaled);
263
264 // Past its shadow budget a slide throws, and paints again without shadows rather than not at all. It first lets
265 // through a cancellation that arrived meanwhile, such as a scroll that moved the slide away.
266 if (!shadowed) await new Promise((resolve) => setTimeout(resolve));
267
268 if (signal.aborted) return { kind: "cancelled" };
269
270 const done = shadowed || (await attempt(withoutShadows(scaled)));
271
272 if (!done) return { kind: "failed" };
273
274 if (signal.aborted) return { kind: "cancelled" };
275
276 return {
277 kind: "painted",
278 bitmap: canvas.transferToImageBitmap(),
279 vectors: [...wanted.values()],
280 };
281 },
282
283 /** Closes the least recently drawn pictures past the budget; they decode again when drawn. Call it between paints. */
284 trim: () => {
285 [...drawn].reduce(
286 (total, [asset, bitmap]) => {
287 if (total <= pictureBudget) return total;
288
289 const freed = pixelBytes(bitmap);
290
291 bitmap.close();
292 drawn.delete(asset);
293 pictures.delete(asset);
294
295 return total - freed;
296 },
297 [...drawn.values()].reduce((total, bitmap) => total + pixelBytes(bitmap), 0),
298 );
299 },
300
301 /**
302 * Keeps the window's rasterized SVG pictures in place of coarser ones. A finer one that a paint at a larger scale
303 * returned stays, and so does any one when the window could not rasterize the picture again.
304 */
305 vectors: (bitmaps: readonly VectorBitmap[]) =>
306 Promise.all(
307 bitmaps.map(async (entry) => {
308 const found = await picture(entry.asset);
309
310 if (found.kind !== "vector") return entry.bitmap?.close();
311
312 if (found.bitmap && (!entry.bitmap || covers(found.bitmap, entry.bitmap)))
313 return entry.bitmap?.close();
314
315 found.bitmap?.close();
316 drawn.delete(entry.asset);
317 found.bitmap = entry.bitmap;
318 found.failed = !entry.bitmap;
319 if (entry.bitmap) drawn.set(entry.asset, entry.bitmap);
320 }),
321 ).then(() => undefined),
322 };
323 }
324
325 /**
326 * The bitmap size each SVG picture on a slide needs at `scale`: its drawn size in device pixels, before cropping and
327 * up to `maxImageSide`. A tiled picture repeats at its own size, so it needs exactly that (undefined).
328 */
329 function vectorSizes(primitives: readonly SlidePrimitive[], scale: number) {
330 const sizes = new Map<string, Size | undefined>();
331
332 const visit = (primitive: SlidePrimitive): void => {
333 if (primitive.kind === "table" || primitive.kind === "chart")
334 return primitive.primitives.forEach(visit);
335
336 if (primitive.kind !== "image" || !primitive.assetId) return;
337
338 const asset = primitive.assetId;
339 const previous = sizes.get(asset);
340
341 if (primitive.tile || (sizes.has(asset) && !previous)) return void sizes.set(asset, undefined);
342
343 const visibleX = 1 - fraction(primitive.crop?.left) - fraction(primitive.crop?.right);
344 const visibleY = 1 - fraction(primitive.crop?.top) - fraction(primitive.crop?.bottom);
345
346 if (visibleX <= 0 || visibleY <= 0) return;
347
348 const width = Math.max(previous?.width ?? 0, (primitive.w * scale) / visibleX);
349 const height = Math.max(previous?.height ?? 0, (primitive.h * scale) / visibleY);
350 const shrink = Math.min(1, maxImageSide / Math.max(width, height));
351
352 sizes.set(asset, {
353 width: Math.max(1, Math.ceil(width * shrink)),
354 height: Math.max(1, Math.ceil(height * shrink)),
355 });
356 };
357
358 primitives.forEach(visit);
359
360 return sizes;
361 }
362
363 function pixelBytes(bitmap: ImageBitmap) {
364 return bitmap.width * bitmap.height * 4;
365 }
366
367 /** Whether a rasterized SVG picture is as fine as `need`; undefined needs the picture's own size, rasterized once. */
368 function covers(bitmap: ImageBitmap | undefined, need: Size | undefined) {
369 if (!bitmap) return false;
370
371 if (!need) return true;
372
373 return bitmap.width >= need.width && bitmap.height >= need.height;
374 }
375
376 /** A crop fraction as the painter reads it. */
377 function fraction(value: number | undefined) {
378 return value === undefined || !Number.isFinite(value) ? 0 : Math.min(Math.max(value, 0), 1);
379 }
380
381 function withoutShadows(frame: SlideDisplayList): SlideDisplayList {
382 const strip = (primitive: SlidePrimitive): SlidePrimitive => {
383 if (primitive.kind === "shape" || primitive.kind === "image") {
384 const { shadow: _shadow, ...rest } = primitive;
385 return rest;
386 }
387
388 if (primitive.kind === "textBox") {
389 const { textShadow: _shadow, ...rest } = primitive;
390 return rest;
391 }
392
393 if (primitive.kind === "table" || primitive.kind === "chart")
394 return { ...primitive, primitives: primitive.primitives.map(strip) };
395
396 return primitive;
397 };
398
399 return { ...frame, primitives: frame.primitives.map(strip) };
400 }
401
402 /**
403 * The picture scaled down to `maxImageSide` when it is larger. Decks of camera photos would otherwise hold about 100 MB
404 * per decoded picture for as long as the deck is open.
405 */
406 async function bounded(source: ImageBitmap) {
407 const shrink = maxImageSide / Math.max(source.width, source.height);
408
409 if (shrink >= 1) return source;
410
411 return createImageBitmap(source, {
412 resizeWidth: Math.max(1, Math.round(source.width * shrink)),
413 resizeHeight: Math.max(1, Math.round(source.height * shrink)),
414 resizeQuality: "high",
415 }).then(
416 (resized) => {
417 source.close();
418
419 return resized;
420 },
421 () => source,
422 );
423 }