Open History to see the last commit
1// Adapted from opencode (MIT). Copyright (c) 2025 opencode. See LICENSE.opencode.
2import type { ImageResolver } from "@betteroffice/docx/layout/render";
3import type { YrsMediaSource } from "@betteroffice/docx/yrs";
4import { officeImageBitmap } from "./image-bitmap";
5import type { VectorBitmap, VectorRequest } from "./vector-pictures";
6
7/** An embedded image's source, which names a part of the package the session reads. */
8const mediaToken = /^media:(0|[1-9]\d*)$/u;
9
10/** How many bytes of decoded images stay for repaints; the least recently drawn go first past it. */
11const budget = 256 * 1024 * 1024;
12
13type Size = { readonly width: number; readonly height: number };
14
15type Decoded = {
16 image: Promise<CanvasImageSource | null>;
17 /** The decoded image once it is one; the shared placeholder is never closed. */
18 bitmap: ImageBitmap | undefined;
19 /** An SVG picture, which the window rasterizes into `bitmap`; `failed` once it could not. */
20 vector: { readonly blob: Blob; failed: boolean } | undefined;
21};
22
23/**
24 * Resolves the display list's images in a worker: `media:{n}` sources decode from the session's bytes, `data:` and
25 * `blob:` ones in place, and nothing else, so painting never makes a request. Undecodable parts paint a grey box. SVG
26 * pictures, which the engine converts WMF and EMF ones to, need the window: `vectors` lists them, `provide` takes them.
27 */
28export function createBitmapImageResolver(media: (token: string) => YrsMediaSource | null) {
29 const cache = new Map<string, Decoded>();
30 const placeholder = greyBox();
31 // The SVG pictures the paint since the last `vectors` drew.
32 const drawn = new Set<string>();
33
34 const decode = (source: string, entry: Decoded): Promise<CanvasImageSource | null> => {
35 const token = mediaToken.test(source);
36 const found = token ? media(source) : undefined;
37
38 if (token && !found) return Promise.resolve(placeholder);
39
40 const blob = found
41 ? Promise.resolve(new Blob([new Uint8Array(found.bytes)], { type: found.mimeType }))
42 : fetch(source).then((response) => response.blob());
43
44 return blob.then((decoded) => {
45 if (decoded.type === "image/svg+xml") {
46 entry.vector = { blob: decoded, failed: false };
47
48 return null;
49 }
50
51 return officeImageBitmap(decoded).then(
52 (bitmap): CanvasImageSource => bitmap,
53 () => (token ? placeholder : null),
54 );
55 });
56 };
57
58 const resolve: ImageResolver = (source) => {
59 if (!mediaToken.test(source) && !source.startsWith("blob:") && !source.startsWith("data:"))
60 return null;
61
62 const cached = cache.get(source);
63
64 if (cached) {
65 // Map order is drawing order: the least recently drawn come first.
66 cache.delete(source);
67 cache.set(source, cached);
68
69 if (cached.vector) drawn.add(source);
70
71 return cached.vector
72 ? Promise.resolve(cached.bitmap ?? (cached.vector.failed ? placeholder : null))
73 : cached.image;
74 }
75
76 const entry: Decoded = { image: Promise.resolve(null), bitmap: undefined, vector: undefined };
77
78 entry.image = decode(source, entry).then((decoded) => {
79 if (decoded instanceof ImageBitmap) entry.bitmap = decoded;
80
81 if (entry.vector) drawn.add(source);
82
83 return decoded;
84 });
85 cache.set(source, entry);
86
87 return entry.image;
88 };
89
90 return {
91 resolve,
92 /**
93 * The SVG pictures the paints since the last call drew without a bitmap, or with one smaller than `sizes` asks, at
94 * the size to rasterize them; a picture absent from `sizes` rasterizes at its own size.
95 */
96 vectors(sizes: ReadonlyMap<string, Size>): VectorRequest[] {
97 const requests = [...drawn].flatMap((source) => {
98 const entry = cache.get(source);
99 const size = sizes.get(source);
100
101 if (!entry?.vector || entry.vector.failed) return [];
102
103 if (entry.bitmap && (!size || covers(entry.bitmap, size))) return [];
104
105 return [{ asset: source, blob: entry.vector.blob, size }];
106 });
107
108 drawn.clear();
109
110 return requests;
111 },
112 /** Keeps the window's rasterized SVG pictures in place of coarser ones. */
113 provide(bitmaps: readonly VectorBitmap[]) {
114 for (const { asset, bitmap } of bitmaps) {
115 const entry = cache.get(asset);
116
117 if (!entry?.vector || (bitmap && entry.bitmap && covers(entry.bitmap, bitmap))) {
118 bitmap?.close();
119 continue;
120 }
121
122 if (!bitmap) {
123 entry.vector.failed = !entry.bitmap;
124 continue;
125 }
126
127 entry.bitmap?.close();
128 entry.bitmap = bitmap;
129 }
130 },
131 /** Closes the least recently drawn images past the budget. Call it between paints, never during one. */
132 trim() {
133 const size = (entry: Decoded) =>
134 entry.bitmap ? entry.bitmap.width * entry.bitmap.height * 4 : 0;
135
136 [...cache].reduce(
137 (total, [source, entry]) => {
138 if (total <= budget || !entry.bitmap) return total;
139
140 const freed = size(entry);
141
142 entry.bitmap.close();
143 cache.delete(source);
144
145 return total - freed;
146 },
147 [...cache.values()].reduce((total, entry) => total + size(entry), 0),
148 );
149 },
150 };
151}
152
153function covers(bitmap: Size, size: Size) {
154 return bitmap.width >= size.width && bitmap.height >= size.height;
155}
156
157/** The grey box a missing image paints: one pixel the painter stretches over the image's place. */
158function greyBox() {
159 const canvas = new OffscreenCanvas(1, 1);
160 const context = canvas.getContext("2d");
161
162 if (!context) return null;
163
164 context.fillStyle = "#e6e6e6";
165 context.fillRect(0, 0, 1, 1);
166
167 return canvas;
168}