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
| 1 | // Adapted from opencode (MIT). Copyright (c) 2025 opencode. See LICENSE.opencode. |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | import { |
| 4 | buildResidentRegionLayoutRequest, |
| 5 | getLayoutKernelInputs, |
| 6 | workerLayoutComputation, |
| 7 | } from "@betteroffice/docx/editor"; |
| 8 | import { createRustMeasureSource } from "@betteroffice/docx/layout"; |
| 9 | import { |
| 10 | applyFrameDelta, |
| 11 | decodeFrameDelta, |
| 12 | encodeDisplayListFrameExtras, |
| 13 | GlyphCache, |
| 14 | rasterizeDisplayPageToBackBuffer, |
| 15 | type DisplayPage, |
| 16 | type RetainedFrame, |
| 17 | } from "@betteroffice/docx/layout/render"; |
| 18 | import { |
| 19 | createYrsPositionProjection, |
| 20 | createYrsSession, |
| 21 | type YrsPositionProjection, |
| 22 | } from "@betteroffice/docx/yrs"; |
| 23 | import { loadFace, sheetFace, wordFonts } from "./fonts"; |
| 24 | import { maxImageSide, type VectorBitmap } from "./vector-pictures"; |
| 25 | import { withWholeMissingGlyphs } from "./word-glyphs"; |
| 26 | import { createBitmapImageResolver } from "./word-images"; |
| 27 | import { layerFont, type WordPageSize, type WordSpan } from "./word-protocol"; |
| 28 | import { pageText, positionTarget, type TextMeasure } from "./word-text"; |
| 29 | import { PreviewError } from "../previewFailure"; |
| 30 | |
| 31 | // An open Word document: its engine session, fonts, layout, built pages and painting. The document worker holds one. |
| 32 | |
| 33 | const FontRequirements = Schema.fromJsonString( |
| 34 | Schema.Array( |
| 35 | Schema.Struct({ |
| 36 | key: Schema.String, |
| 37 | family: Schema.String, |
| 38 | bold: Schema.Boolean, |
| 39 | italic: Schema.Boolean, |
| 40 | scripts: Schema.optional( |
| 41 | Schema.Array(Schema.Literals(["cjk-sc", "cjk-tc", "cjk-jp", "cjk-kr", "arabic", "hebrew"])), |
| 42 | ), |
| 43 | }), |
| 44 | ), |
| 45 | ); |
| 46 | |
| 47 | const decodeFontRequirements = Schema.decodeUnknownSync(FontRequirements); |
| 48 | |
| 49 | /** A prefix layout's reply marks itself provisional when the document goes on past it. */ |
| 50 | const decodeLayoutReply = Schema.decodeUnknownSync( |
| 51 | Schema.fromJsonString(Schema.Struct({ provisional: Schema.optional(Schema.Boolean) })), |
| 52 | ); |
| 53 | |
| 54 | /** Pages laid out and built before the first paint; later pages are built as they near the viewport. */ |
| 55 | const firstPages = 3; |
| 56 | |
| 57 | /** How long one layout step may hold back a paint that waits behind it, in milliseconds. */ |
| 58 | const stepBudget = 24; |
| 59 | |
| 60 | /** |
| 61 | * How many pages either side of the page painted last stay built. A built page holds about 5 MB in the engine and |
| 62 | * builds again in about 100 ms. This keeps every page near the viewport, which paints a viewport ahead in both |
| 63 | * directions, and lets a reader scroll back a few screens without waiting, while a long read holds at most 21 pages. |
| 64 | */ |
| 65 | const keptAround = 10; |
| 66 | |
| 67 | /** A value built on first use. */ |
| 68 | type Lazy<T> = { value: T | undefined }; |
| 69 | |
| 70 | /** |
| 71 | * Opens the document and lays out its first pages; the rest lay out a `step` at a time. `fonts` is the font set the |
| 72 | * painting canvas draws text with, if it has one. |
| 73 | */ |
| 74 | export async function openWordDocument(bytes: Uint8Array, fonts: FontFaceSet | undefined) { |
| 75 | const session = await createYrsSession(); |
| 76 | const document = session.openDocx(bytes, true, { mediaTokens: true }).document; |
| 77 | const settings = document.package.settings; |
| 78 | |
| 79 | const measure = createRustMeasureSource({ |
| 80 | engine: { |
| 81 | registerFont: (font) => session.registerFont(font), |
| 82 | registerSubstituteFont: (id, family) => session.registerSubstituteFont(id, family), |
| 83 | clearFonts: () => session.clearFonts(), |
| 84 | }, |
| 85 | bundled: wordFonts(), |
| 86 | }); |
| 87 | |
| 88 | measure.setCompat(settings?.compatibilityFlags); |
| 89 | |
| 90 | const request = buildResidentRegionLayoutRequest(document, 0, { |
| 91 | themeColors: Object.fromEntries( |
| 92 | Object.entries(document.package.theme?.colorScheme ?? {}).filter( |
| 93 | (entry): entry is [string, string] => entry[1] !== undefined, |
| 94 | ), |
| 95 | ), |
| 96 | defaultTabStopTwips: settings?.defaultTabStop ?? null, |
| 97 | numericIds: {}, |
| 98 | mediaTokens: true, |
| 99 | }); |
| 100 | |
| 101 | const requirements = decodeFontRequirements( |
| 102 | session.layoutFontRequirementsJson(JSON.stringify(request)), |
| 103 | ).map(({ scripts, ...requirement }) => ({ |
| 104 | ...requirement, |
| 105 | ...(scripts ? { scripts: [...scripts] } : {}), |
| 106 | })); |
| 107 | |
| 108 | await Promise.all([ |
| 109 | measure.prepareFontRequirements(requirements), |
| 110 | fonts && addCanvasFaces(fonts, requirements), |
| 111 | ]); |
| 112 | |
| 113 | const measurement = measure.measurementConfigForRequirements(requirements); |
| 114 | |
| 115 | if (!measurement) throw new PreviewError({ code: "word-fonts-unprepared" }); |
| 116 | |
| 117 | const input = JSON.stringify({ ...request, measurement }); |
| 118 | |
| 119 | // Word shows resolved comments without the active highlight. |
| 120 | const resolvedCommentIds = (document.package.document.comments ?? []) |
| 121 | .filter( |
| 122 | (comment) => |
| 123 | (comment.done || comment.status === "resolved") && comment.parentId === undefined, |
| 124 | ) |
| 125 | .map((comment) => comment.id); |
| 126 | |
| 127 | // The session keeps the layout it last ran and builds display frames from it: only the display extras go in, and a |
| 128 | // binary frame comes back with the window's pages built and every other page a placeholder carrying its geometry. |
| 129 | const display = (previous: RetainedFrame | null, reply: string) => { |
| 130 | const layout = workerLayoutComputation(reply).layout; |
| 131 | |
| 132 | session.setDisplayWindow(0, firstPages); |
| 133 | |
| 134 | const extras = encodeDisplayListFrameExtras({ |
| 135 | measured: [], |
| 136 | options: {}, |
| 137 | layout, |
| 138 | fontChains: measurement.fontChains, |
| 139 | ...(getLayoutKernelInputs(layout)?.headersFooters |
| 140 | ? { headersFooters: getLayoutKernelInputs(layout)!.headersFooters! } |
| 141 | : {}), |
| 142 | resolvedCommentIds, |
| 143 | }); |
| 144 | |
| 145 | return applyFrameDelta( |
| 146 | previous, |
| 147 | decodeFrameDelta(session.buildDisplayListFrame(extras, previous?.frameEpoch ?? 0)), |
| 148 | ); |
| 149 | }; |
| 150 | |
| 151 | // Only as much of the body as fills the first pages; a long document goes on past it. |
| 152 | const prefix = session.layoutDocumentWithRegionsPrefixRetainedJson(input, firstPages); |
| 153 | const retained = { frame: display(null, prefix) }; |
| 154 | |
| 155 | if (retained.frame.displayList.pages.length === 0) |
| 156 | throw new PreviewError({ code: "word-no-pages" }); |
| 157 | |
| 158 | // The region layout that finishes the document: begun on the first step, `cost` the running average milliseconds a |
| 159 | // body block takes. |
| 160 | const layout = { loading: decodeLayoutReply(prefix).provisional ?? false, begun: false, cost: 3 }; |
| 161 | |
| 162 | // Builds a page's content when it is needed and not built, the first time or again after `release`. |
| 163 | const built = (index: number) => { |
| 164 | const page = retained.frame.displayList.pages[index]; |
| 165 | |
| 166 | if (!page?.unbuilt) return page; |
| 167 | |
| 168 | retained.frame = applyFrameDelta( |
| 169 | retained.frame, |
| 170 | decodeFrameDelta(session.buildDisplayPagesFrame([index], retained.frame.frameEpoch)), |
| 171 | ); |
| 172 | |
| 173 | return retained.frame.displayList.pages[index]; |
| 174 | }; |
| 175 | |
| 176 | // Gives back the built pages far from the page about to paint, so a long read keeps only the pages around the |
| 177 | // viewport. A released page keeps its geometry and document positions, which bookmarks search, and builds again |
| 178 | // when it is needed. |
| 179 | const release = (painted: number) => { |
| 180 | // The engine releases pages of the whole document's layout only, and few pages show before it. |
| 181 | if (layout.loading) return; |
| 182 | |
| 183 | const far = retained.frame.displayList.pages.flatMap((page, index) => |
| 184 | !page.unbuilt && Math.abs(painted - index) > keptAround ? [index] : [], |
| 185 | ); |
| 186 | |
| 187 | if (far.length === 0) return; |
| 188 | |
| 189 | const frame = session.releaseDisplayPagesFrame(far, retained.frame.frameEpoch); |
| 190 | |
| 191 | // A superseded release changes nothing; the next one tries again. |
| 192 | if (frame) retained.frame = applyFrameDelta(retained.frame, decodeFrameDelta(frame)); |
| 193 | }; |
| 194 | |
| 195 | // The whole document's layout replaces the prefix's frame. |
| 196 | const complete = (reply: string) => { |
| 197 | const before = retained.frame.displayList.pages; |
| 198 | |
| 199 | retained.frame = display(retained.frame, reply); |
| 200 | layout.loading = false; |
| 201 | |
| 202 | const pages = retained.frame.displayList.pages; |
| 203 | |
| 204 | return { |
| 205 | done: true as const, |
| 206 | pages: pages.map(pageSize), |
| 207 | kept: pages.map((page, index) => { |
| 208 | const was = before[index]; |
| 209 | |
| 210 | return was === page || (!!was?.unbuilt && !!page.unbuilt); |
| 211 | }), |
| 212 | }; |
| 213 | }; |
| 214 | |
| 215 | const glyphCache = new GlyphCache({ |
| 216 | // The engine sets glyphs no bundled face has, such as Thai ones or emoji, as missing glyphs, which outline as empty |
| 217 | // boxes. A run whose outlines fail paints whole with the canvas's fonts instead, which the system's cover. |
| 218 | provider: (font, glyph) => { |
| 219 | if (glyph === 0) throw new Error("No bundled face has this glyph"); |
| 220 | |
| 221 | return session.outlineGlyphJson(font, glyph); |
| 222 | }, |
| 223 | }); |
| 224 | const images = createBitmapImageResolver((token) => session.mediaSource(token)); |
| 225 | const surface = new OffscreenCanvas(1, 1); |
| 226 | const fitting = new OffscreenCanvas(1, 1).getContext("2d"); |
| 227 | const painted = new WeakMap<DisplayPage, DisplayPage>(); |
| 228 | const textMeasure = layerMeasure(); |
| 229 | const texts = new WeakMap<DisplayPage, readonly WordSpan[]>(); |
| 230 | // Built on the first followed bookmark: most documents are read without one. |
| 231 | const projection: Lazy<YrsPositionProjection | null> = { value: undefined }; |
| 232 | |
| 233 | return { |
| 234 | pages: () => retained.frame.displayList.pages, |
| 235 | loading: () => layout.loading, |
| 236 | built, |
| 237 | /** |
| 238 | * Lays out about `stepBudget` milliseconds more of the body. Blocks vary widely, and sizing a step by the last one |
| 239 | * alone overshoots after a run of cheap blocks. |
| 240 | */ |
| 241 | step: () => { |
| 242 | if (!layout.loading) { |
| 243 | const pages = retained.frame.displayList.pages; |
| 244 | |
| 245 | return { done: true as const, pages: pages.map(pageSize), kept: pages.map(() => true) }; |
| 246 | } |
| 247 | |
| 248 | if (!layout.begun) { |
| 249 | layout.begun = true; |
| 250 | |
| 251 | const begun = session.beginRegionLayout(input); |
| 252 | |
| 253 | return begun.layoutJson === undefined |
| 254 | ? { done: false as const } |
| 255 | : complete(begun.layoutJson); |
| 256 | } |
| 257 | |
| 258 | const blocks = Math.min(8192, Math.max(1, Math.round(stepBudget / layout.cost))); |
| 259 | const started = performance.now(); |
| 260 | const progress = session.resumeRegionLayout(blocks); |
| 261 | |
| 262 | layout.cost = layout.cost * 0.7 + ((performance.now() - started) / blocks) * 0.3; |
| 263 | |
| 264 | return progress.layoutJson === undefined |
| 265 | ? { done: false as const } |
| 266 | : complete(progress.layoutJson); |
| 267 | }, |
| 268 | paint: async (index: number, scale: number, ratio: number, signal: AbortSignal) => { |
| 269 | // A page that scrolled away before its paint began, or while it rasterized, needs no bitmap. |
| 270 | if (signal.aborted) return undefined; |
| 271 | |
| 272 | // Released first, so the page builds in the memory they free. |
| 273 | release(index); |
| 274 | |
| 275 | const page = built(index); |
| 276 | |
| 277 | if (!page) return undefined; |
| 278 | |
| 279 | const paintable = |
| 280 | painted.get(page) ?? (fitting ? withWholeMissingGlyphs(page, fitting) : page); |
| 281 | |
| 282 | painted.set(page, paintable); |
| 283 | |
| 284 | // The window drew image elements, which shrink smoothly at the default quality; a bitmap needs medium quality to |
| 285 | // look the same. Sizing the surface first keeps the setting: the painter resizes it, which resets the context, |
| 286 | // only when its size differs. |
| 287 | const context = surface.getContext("2d"); |
| 288 | const width = Math.ceil(page.width * ratio * scale); |
| 289 | const height = Math.ceil(page.height * ratio * scale); |
| 290 | |
| 291 | if (surface.width !== width) surface.width = width; |
| 292 | |
| 293 | if (surface.height !== height) surface.height = height; |
| 294 | |
| 295 | if (context) context.imageSmoothingQuality = "medium"; |
| 296 | |
| 297 | await rasterizeDisplayPageToBackBuffer( |
| 298 | surface, |
| 299 | paintable, |
| 300 | { glyphCache, resolveImage: images.resolve }, |
| 301 | ratio, |
| 302 | scale, |
| 303 | ); |
| 304 | |
| 305 | const vectors = images.vectors(pictureSizes(paintable, ratio * scale)); |
| 306 | |
| 307 | images.trim(); |
| 308 | |
| 309 | if (signal.aborted) return undefined; |
| 310 | |
| 311 | return { bitmap: surface.transferToImageBitmap(), vectors }; |
| 312 | }, |
| 313 | vectors: (bitmaps: readonly VectorBitmap[]) => images.provide(bitmaps), |
| 314 | text: (index: number) => { |
| 315 | const page = built(index); |
| 316 | |
| 317 | if (!page || !textMeasure) return []; |
| 318 | |
| 319 | const cached = texts.get(page); |
| 320 | |
| 321 | if (cached) return cached; |
| 322 | |
| 323 | const spans = pageText(page, textMeasure); |
| 324 | |
| 325 | texts.set(page, spans); |
| 326 | |
| 327 | return spans; |
| 328 | }, |
| 329 | target: (name: string) => { |
| 330 | projection.value ??= createYrsPositionProjection(session, "body"); |
| 331 | |
| 332 | const position = projection.value?.bookmarkPosition(name) ?? undefined; |
| 333 | |
| 334 | if (position !== undefined) |
| 335 | return positionTarget(retained.frame.displayList.pages, position, built); |
| 336 | |
| 337 | // Word's reserved bookmark for the start of the document. |
| 338 | return name === "_top" ? { index: 0, y: 0 } : undefined; |
| 339 | }, |
| 340 | }; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * The canvas draws list numbers, and runs no bundled face has glyphs for, with its own fonts, by their Word family. |
| 345 | * Adding the document's faces under those names draws them like the rest of its text. A family the app ships no face |
| 346 | * for stays with the system's, and CJK faces, tens of megabytes each, are not held twice. |
| 347 | */ |
| 348 | async function addCanvasFaces( |
| 349 | fonts: FontFaceSet, |
| 350 | requirements: readonly { family: string; bold: boolean; italic: boolean }[], |
| 351 | ) { |
| 352 | const faces = new Map<string, Promise<FontFace>>(); |
| 353 | |
| 354 | for (const { family, bold, italic } of requirements) { |
| 355 | const face = sheetFace(family, bold, italic); |
| 356 | |
| 357 | if (!face || face.script?.startsWith("cjk-")) continue; |
| 358 | |
| 359 | const key = `${family}\u0000${face.weight}\u0000${face.style}`; |
| 360 | |
| 361 | if (faces.has(key)) continue; |
| 362 | |
| 363 | // A family without the requested style gets the face it has, which the browser then slants or emboldens. |
| 364 | faces.set( |
| 365 | key, |
| 366 | loadFace(face).then((source) => |
| 367 | new FontFace(family, source, { |
| 368 | weight: face.weight === 700 ? "700" : "400", |
| 369 | style: face.style === "italic" ? "italic" : "normal", |
| 370 | }).load(), |
| 371 | ), |
| 372 | ); |
| 373 | } |
| 374 | |
| 375 | await Promise.all( |
| 376 | [...faces.values()].map((face) => |
| 377 | face.then((loaded) => void fonts.add(loaded)).catch(() => undefined), |
| 378 | ), |
| 379 | ); |
| 380 | } |
| 381 | |
| 382 | /** Each picture's drawn size on a page in device pixels at `density`, up to `maxImageSide`. */ |
| 383 | function pictureSizes(page: DisplayPage, density: number) { |
| 384 | const sizes = new Map<string, { width: number; height: number }>(); |
| 385 | const primitives = [ |
| 386 | ...page.primitives, |
| 387 | ...(page.header?.primitives ?? []), |
| 388 | ...(page.footer?.primitives ?? []), |
| 389 | ...(page.noteAreas ?? []).flatMap((area) => area.primitives ?? []), |
| 390 | ]; |
| 391 | |
| 392 | for (const primitive of primitives) { |
| 393 | if (primitive.kind !== "image") continue; |
| 394 | |
| 395 | const width = (primitive.contentFrame?.w ?? primitive.w) * density; |
| 396 | const height = (primitive.contentFrame?.h ?? primitive.h) * density; |
| 397 | const shrink = Math.min(1, maxImageSide / Math.max(width, height)); |
| 398 | const previous = sizes.get(primitive.relId); |
| 399 | |
| 400 | sizes.set(primitive.relId, { |
| 401 | width: Math.max(previous?.width ?? 1, Math.round(width * shrink)), |
| 402 | height: Math.max(previous?.height ?? 1, Math.round(height * shrink)), |
| 403 | }); |
| 404 | } |
| 405 | |
| 406 | return sizes; |
| 407 | } |
| 408 | |
| 409 | export function pageSize(page: DisplayPage): WordPageSize { |
| 410 | return { width: page.width, height: page.height, background: page.background }; |
| 411 | } |
| 412 | |
| 413 | /** Measures the text layer's font with a detached canvas; undefined where the worker offers no 2D context. */ |
| 414 | function layerMeasure(): TextMeasure | undefined { |
| 415 | const context = new OffscreenCanvas(1, 1).getContext("2d"); |
| 416 | |
| 417 | if (!context) return undefined; |
| 418 | |
| 419 | // Measured large and scaled down, so rounding stays well below a pixel at any zoom. |
| 420 | const size = 100; |
| 421 | |
| 422 | context.font = `${size}px ${layerFont}`; |
| 423 | |
| 424 | const metrics = context.measureText("Hg"); |
| 425 | |
| 426 | return { |
| 427 | width: (text) => context.measureText(text).width / size, |
| 428 | ascent: metrics.fontBoundingBoxAscent / size, |
| 429 | descent: metrics.fontBoundingBoxDescent / size, |
| 430 | }; |
| 431 | } |