@f-office/wasm 0.0.0-stage → 0.2.0
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/LICENSE +168 -0
- package/NOTICE +31 -0
- package/README.md +79 -2
- package/dist/index.d.ts +113 -0
- package/dist/index.js +342 -0
- package/dist/source.d.ts +30 -0
- package/dist/source.js +64 -0
- package/dist/types.d.ts +92 -0
- package/dist/types.js +1 -0
- package/dist/wasm/f_office_wasm.d.ts +398 -0
- package/dist/wasm/f_office_wasm.js +1189 -0
- package/dist/wasm/f_office_wasm_bg.wasm +0 -0
- package/dist/wasm/f_office_wasm_bg.wasm.d.ts +60 -0
- package/dist/worker.d.ts +55 -0
- package/dist/worker.js +325 -0
- package/package.json +51 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import wasmInit, { initSync, Renderer as RawRenderer, } from "./wasm/f_office_wasm.js";
|
|
2
|
+
export { createBlobSource, createMemorySource, createOpfsSource, } from "./source.js";
|
|
3
|
+
const DEFAULT_TARGET = "word-win";
|
|
4
|
+
let initPromise = null;
|
|
5
|
+
let wasmMemory = null;
|
|
6
|
+
function isNodeRuntime() {
|
|
7
|
+
return (typeof process !== "undefined" &&
|
|
8
|
+
typeof process.versions === "object" &&
|
|
9
|
+
typeof process.versions.node === "string");
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Returns the current byte length of the WebAssembly linear memory buffer, or `0` before
|
|
13
|
+
* `init()` completes. Useful for monitoring peak memory on mobile browsers.
|
|
14
|
+
*/
|
|
15
|
+
export function wasmMemoryBytes() {
|
|
16
|
+
return wasmMemory ? wasmMemory.buffer.byteLength : 0;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Initializes the F-Office WebAssembly module. Safe to call multiple times;
|
|
20
|
+
* subsequent calls resolve immediately once initialized.
|
|
21
|
+
*/
|
|
22
|
+
export function init(source) {
|
|
23
|
+
if (initPromise !== null && source === undefined) {
|
|
24
|
+
return initPromise;
|
|
25
|
+
}
|
|
26
|
+
const pending = (async () => {
|
|
27
|
+
if (source !== undefined) {
|
|
28
|
+
const output = await wasmInit({ module_or_path: source });
|
|
29
|
+
wasmMemory = output.memory;
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const wasmUrl = new URL("./wasm/f_office_wasm_bg.wasm", import.meta.url);
|
|
33
|
+
if (isNodeRuntime()) {
|
|
34
|
+
const { readFile } = await import("node:fs/promises");
|
|
35
|
+
const bytes = await readFile(wasmUrl);
|
|
36
|
+
const output = initSync({ module: bytes });
|
|
37
|
+
wasmMemory = output.memory;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const output = await wasmInit({ module_or_path: wasmUrl });
|
|
41
|
+
wasmMemory = output.memory;
|
|
42
|
+
})();
|
|
43
|
+
if (source === undefined) {
|
|
44
|
+
initPromise = pending;
|
|
45
|
+
}
|
|
46
|
+
return pending;
|
|
47
|
+
}
|
|
48
|
+
function unwrapFontNeeds(raw) {
|
|
49
|
+
const out = [];
|
|
50
|
+
for (const item of raw) {
|
|
51
|
+
try {
|
|
52
|
+
out.push({
|
|
53
|
+
family: item.family ?? null,
|
|
54
|
+
tried: item.tried,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
item.free();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
function unwrapOutline(raw) {
|
|
64
|
+
const out = [];
|
|
65
|
+
for (const item of raw) {
|
|
66
|
+
try {
|
|
67
|
+
out.push({
|
|
68
|
+
level: item.level,
|
|
69
|
+
text: item.text,
|
|
70
|
+
page: item.page,
|
|
71
|
+
address: item.address,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
item.free();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
function unwrapTextRuns(raw) {
|
|
81
|
+
const out = [];
|
|
82
|
+
for (const item of raw) {
|
|
83
|
+
try {
|
|
84
|
+
out.push({
|
|
85
|
+
text: item.text,
|
|
86
|
+
size: item.size,
|
|
87
|
+
left: item.left,
|
|
88
|
+
top: item.top,
|
|
89
|
+
width: item.width,
|
|
90
|
+
height: item.height,
|
|
91
|
+
baseline: item.baseline,
|
|
92
|
+
glyphLefts: item.glyphLefts,
|
|
93
|
+
glyphWidths: item.glyphWidths,
|
|
94
|
+
glyphEnds: item.glyphEnds,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
item.free();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
/** A rendered page image (RGBA bytes, dimensions, and drawing warnings). */
|
|
104
|
+
export class PageImage {
|
|
105
|
+
#raw;
|
|
106
|
+
#cachedPixels = null;
|
|
107
|
+
constructor(raw) {
|
|
108
|
+
this.#raw = raw;
|
|
109
|
+
}
|
|
110
|
+
/** Width in pixels. */
|
|
111
|
+
get width() {
|
|
112
|
+
return this.#raw.width;
|
|
113
|
+
}
|
|
114
|
+
/** Height in pixels. */
|
|
115
|
+
get height() {
|
|
116
|
+
return this.#raw.height;
|
|
117
|
+
}
|
|
118
|
+
/** Distinct warnings produced while drawing this page. */
|
|
119
|
+
warnings() {
|
|
120
|
+
return this.#raw.warnings();
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* RGBA pixels copied out of WebAssembly memory, cached on first read so repeated
|
|
124
|
+
* accesses do not copy the buffer again.
|
|
125
|
+
*/
|
|
126
|
+
get pixels() {
|
|
127
|
+
let cached = this.#cachedPixels;
|
|
128
|
+
if (cached === null) {
|
|
129
|
+
cached = this.#raw.pixels;
|
|
130
|
+
this.#cachedPixels = cached;
|
|
131
|
+
}
|
|
132
|
+
return cached;
|
|
133
|
+
}
|
|
134
|
+
/** Builds a browser `ImageData` backed by this page's RGBA pixels. */
|
|
135
|
+
toImageData() {
|
|
136
|
+
const data = new Uint8ClampedArray(this.pixels);
|
|
137
|
+
return new ImageData(data, this.width, this.height);
|
|
138
|
+
}
|
|
139
|
+
/** Releases WebAssembly memory held by this page image. */
|
|
140
|
+
free() {
|
|
141
|
+
this.#raw.free();
|
|
142
|
+
}
|
|
143
|
+
[Symbol.dispose]() {
|
|
144
|
+
this.free();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/** A laid-out DOCX document. Page indices are zero-based. */
|
|
148
|
+
export class Document {
|
|
149
|
+
#raw;
|
|
150
|
+
constructor(raw) {
|
|
151
|
+
this.#raw = raw;
|
|
152
|
+
}
|
|
153
|
+
/** Distinct layout warnings (such as substituted fonts or missing pictures). */
|
|
154
|
+
warnings() {
|
|
155
|
+
return this.#raw.warnings();
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Fonts looked up during layout that were missing from the renderer, including
|
|
159
|
+
* glyph-fallback families discovered after primary fonts were loaded.
|
|
160
|
+
*/
|
|
161
|
+
fontNeeds() {
|
|
162
|
+
return unwrapFontNeeds(this.#raw.fontNeeds());
|
|
163
|
+
}
|
|
164
|
+
/** Number of laid-out pages. */
|
|
165
|
+
pageCount() {
|
|
166
|
+
return this.#raw.pageCount();
|
|
167
|
+
}
|
|
168
|
+
/** Width in pixels of page `index` when rendered at `dpi`. */
|
|
169
|
+
pageWidth(index, dpi) {
|
|
170
|
+
return this.#raw.pageWidth(index, dpi);
|
|
171
|
+
}
|
|
172
|
+
/** Height in pixels of page `index` when rendered at `dpi`. */
|
|
173
|
+
pageHeight(index, dpi) {
|
|
174
|
+
return this.#raw.pageHeight(index, dpi);
|
|
175
|
+
}
|
|
176
|
+
/** Width and height in points of every page in order. */
|
|
177
|
+
pageSizes() {
|
|
178
|
+
const flat = this.#raw.pageSizes();
|
|
179
|
+
const out = [];
|
|
180
|
+
for (let i = 0; i + 1 < flat.length; i += 2) {
|
|
181
|
+
out.push({ width: flat[i], height: flat[i + 1] });
|
|
182
|
+
}
|
|
183
|
+
return out;
|
|
184
|
+
}
|
|
185
|
+
/** Upright text runs of page `index` in painting order. */
|
|
186
|
+
textRuns(index) {
|
|
187
|
+
return unwrapTextRuns(this.#raw.textRuns(index));
|
|
188
|
+
}
|
|
189
|
+
/** Document headings in reading order. */
|
|
190
|
+
outline() {
|
|
191
|
+
return unwrapOutline(this.#raw.outline());
|
|
192
|
+
}
|
|
193
|
+
/** Renders page `index` at `dpi` pixels per inch on a white background. */
|
|
194
|
+
renderPage(index, dpi) {
|
|
195
|
+
return new PageImage(this.#raw.renderPage(index, dpi));
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Renders only the rectangular pixel tile `region` of page `index` at `dpi` pixels per inch.
|
|
199
|
+
* Ideal for mobile viewports and high-zoom viewing with constant memory.
|
|
200
|
+
*/
|
|
201
|
+
renderRegion(index, region, dpi) {
|
|
202
|
+
return new PageImage(this.#raw.renderRegion(index, region.x, region.y, region.width, region.height, dpi));
|
|
203
|
+
}
|
|
204
|
+
/** Writes all pages to a PDF file with fonts subset and embedded. */
|
|
205
|
+
writePdf() {
|
|
206
|
+
return this.#raw.writePdf();
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Streams all pages as PDF 1.7 chunks into `onChunk` without allocating the full PDF in
|
|
210
|
+
* WebAssembly linear memory. Returns distinct PDF warnings.
|
|
211
|
+
*/
|
|
212
|
+
writePdfTo(onChunk) {
|
|
213
|
+
return this.#raw.writePdfTo(onChunk);
|
|
214
|
+
}
|
|
215
|
+
/** Releases WebAssembly memory held by this document. */
|
|
216
|
+
free() {
|
|
217
|
+
this.#raw.free();
|
|
218
|
+
}
|
|
219
|
+
[Symbol.dispose]() {
|
|
220
|
+
this.free();
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/** Holds loaded fonts and opens DOCX documents into laid-out pages. */
|
|
224
|
+
export class Renderer {
|
|
225
|
+
#raw;
|
|
226
|
+
constructor() {
|
|
227
|
+
this.#raw = new RawRenderer();
|
|
228
|
+
}
|
|
229
|
+
/** Returns the supported Word target IDs. */
|
|
230
|
+
static targets() {
|
|
231
|
+
return RawRenderer.targets();
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Lists fonts requested by a DOCX file before any font is added.
|
|
235
|
+
* Defaults to `"word-win"`.
|
|
236
|
+
*/
|
|
237
|
+
static fontsNeeded(docx, target = DEFAULT_TARGET) {
|
|
238
|
+
return unwrapFontNeeds(RawRenderer.fontsNeeded(docx, target));
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Lists fonts requested by a DOCX file read lazily from a random-access `ByteSource` without
|
|
242
|
+
* copying the archive into WebAssembly linear memory.
|
|
243
|
+
*/
|
|
244
|
+
static fontsNeededSource(source, target = DEFAULT_TARGET) {
|
|
245
|
+
return unwrapFontNeeds(RawRenderer.fontsNeededSource(source.byteLength, (offset, length) => source.readAt(offset, length), target));
|
|
246
|
+
}
|
|
247
|
+
/** Adds all faces from an OpenType font or collection (`.ttf`, `.otf`, `.ttc`, `.otc`). */
|
|
248
|
+
addFont(data) {
|
|
249
|
+
return this.#raw.addFont(data);
|
|
250
|
+
}
|
|
251
|
+
/** Adds art page border pictures. */
|
|
252
|
+
addArtPictures(data) {
|
|
253
|
+
return this.#raw.addArtPictures(data);
|
|
254
|
+
}
|
|
255
|
+
/** Lays out a DOCX file with the fonts currently added to this renderer. */
|
|
256
|
+
openDocx(docx, options) {
|
|
257
|
+
const target = options?.target ?? DEFAULT_TARGET;
|
|
258
|
+
const now = options?.now;
|
|
259
|
+
const raw = options?.limits === "mobile"
|
|
260
|
+
? this.#raw.openDocxMobile(docx, target, now)
|
|
261
|
+
: this.#raw.openDocx(docx, target, now);
|
|
262
|
+
return new Document(raw);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Lays out a DOCX file read lazily from a random-access `ByteSource` (such as
|
|
266
|
+
* `createBlobSource(file)` or `createOpfsSource(handle)` inside a Web Worker) without copying
|
|
267
|
+
* the full archive into WebAssembly linear memory.
|
|
268
|
+
*/
|
|
269
|
+
openDocxSource(source, options) {
|
|
270
|
+
const target = options?.target ?? DEFAULT_TARGET;
|
|
271
|
+
const now = options?.now;
|
|
272
|
+
const mobile = options?.limits === "mobile";
|
|
273
|
+
const raw = this.#raw.openDocxSource(source.byteLength, (offset, length) => source.readAt(offset, length), target, now, mobile);
|
|
274
|
+
return new Document(raw);
|
|
275
|
+
}
|
|
276
|
+
/** Releases WebAssembly memory held by this renderer. */
|
|
277
|
+
free() {
|
|
278
|
+
this.#raw.free();
|
|
279
|
+
}
|
|
280
|
+
[Symbol.dispose]() {
|
|
281
|
+
this.free();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Discovers the fonts requested by `docx` (including glyph-fallback families),
|
|
286
|
+
* fetches each missing family once via `fetchFont`, and registers them on `renderer`.
|
|
287
|
+
*/
|
|
288
|
+
export async function loadFontsFor(renderer, docx, fetchFont, options) {
|
|
289
|
+
const target = options?.target ?? DEFAULT_TARGET;
|
|
290
|
+
const attempted = new Set();
|
|
291
|
+
const added = [];
|
|
292
|
+
const fetchNeeds = async (needs) => {
|
|
293
|
+
let addedHere = 0;
|
|
294
|
+
for (const need of needs) {
|
|
295
|
+
for (const family of need.tried) {
|
|
296
|
+
const key = family.toLowerCase();
|
|
297
|
+
if (attempted.has(key)) {
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
attempted.add(key);
|
|
301
|
+
const result = await fetchFont(family);
|
|
302
|
+
if (!result) {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const files = Array.isArray(result) ? result : [result];
|
|
306
|
+
let loaded = false;
|
|
307
|
+
for (const bytes of files) {
|
|
308
|
+
if (bytes.byteLength > 0) {
|
|
309
|
+
renderer.addFont(bytes);
|
|
310
|
+
loaded = true;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (loaded) {
|
|
314
|
+
added.push(family);
|
|
315
|
+
addedHere += 1;
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return addedHere;
|
|
321
|
+
};
|
|
322
|
+
await fetchNeeds(Renderer.fontsNeeded(docx, target));
|
|
323
|
+
// Second pass catches fallback fonts requested for glyphs absent from the primary fonts.
|
|
324
|
+
for (let pass = 0; pass < 3; pass += 1) {
|
|
325
|
+
const doc = renderer.openDocx(docx, options);
|
|
326
|
+
let remaining;
|
|
327
|
+
try {
|
|
328
|
+
remaining = doc.fontNeeds();
|
|
329
|
+
}
|
|
330
|
+
finally {
|
|
331
|
+
doc.free();
|
|
332
|
+
}
|
|
333
|
+
if (remaining.length === 0) {
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
const newlyAdded = await fetchNeeds(remaining);
|
|
337
|
+
if (newlyAdded === 0) {
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return added;
|
|
342
|
+
}
|
package/dist/source.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ByteSource } from "./types.js";
|
|
2
|
+
/** Minimal synchronous OPFS handle interface available inside Dedicated Web Workers. */
|
|
3
|
+
export interface OpfsSyncAccessHandle {
|
|
4
|
+
getSize(): number;
|
|
5
|
+
read(buffer: ArrayBufferView, options?: {
|
|
6
|
+
at?: number;
|
|
7
|
+
}): number;
|
|
8
|
+
close(): void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Wraps a browser `File` or `Blob` in a synchronous random-access `ByteSource` backed by
|
|
12
|
+
* `FileReaderSync` + `Blob.slice()`.
|
|
13
|
+
*
|
|
14
|
+
* Must be called inside a Dedicated Web Worker (where `FileReaderSync` is available). Opening a
|
|
15
|
+
* DOCX through this source reads only the ZIP central directory and requested parts, avoiding
|
|
16
|
+
* copying the entire archive into WebAssembly linear memory.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createBlobSource(blob: Blob): ByteSource;
|
|
19
|
+
/**
|
|
20
|
+
* Wraps an Origin Private File System `FileSystemSyncAccessHandle` in a zero-copy synchronous
|
|
21
|
+
* random-access `ByteSource`.
|
|
22
|
+
*
|
|
23
|
+
* Must be called inside a Dedicated Web Worker. Callers remain responsible for calling
|
|
24
|
+
* `handle.close()` after the opened `Document` has been freed.
|
|
25
|
+
*/
|
|
26
|
+
export declare function createOpfsSource(handle: OpfsSyncAccessHandle): ByteSource;
|
|
27
|
+
/**
|
|
28
|
+
* Wraps an in-memory `Uint8Array` in a `ByteSource` (useful for tests or custom adapters).
|
|
29
|
+
*/
|
|
30
|
+
export declare function createMemorySource(bytes: Uint8Array): ByteSource;
|
package/dist/source.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a browser `File` or `Blob` in a synchronous random-access `ByteSource` backed by
|
|
3
|
+
* `FileReaderSync` + `Blob.slice()`.
|
|
4
|
+
*
|
|
5
|
+
* Must be called inside a Dedicated Web Worker (where `FileReaderSync` is available). Opening a
|
|
6
|
+
* DOCX through this source reads only the ZIP central directory and requested parts, avoiding
|
|
7
|
+
* copying the entire archive into WebAssembly linear memory.
|
|
8
|
+
*/
|
|
9
|
+
export function createBlobSource(blob) {
|
|
10
|
+
const ReaderCtor = globalThis.FileReaderSync;
|
|
11
|
+
if (typeof ReaderCtor !== "function") {
|
|
12
|
+
throw new Error("createBlobSource requires FileReaderSync (call inside a Dedicated Web Worker)");
|
|
13
|
+
}
|
|
14
|
+
const reader = new ReaderCtor();
|
|
15
|
+
const byteLength = blob.size;
|
|
16
|
+
return {
|
|
17
|
+
byteLength,
|
|
18
|
+
readAt(offset, length) {
|
|
19
|
+
if (offset >= byteLength || length <= 0) {
|
|
20
|
+
return new Uint8Array(0);
|
|
21
|
+
}
|
|
22
|
+
const end = Math.min(byteLength, offset + length);
|
|
23
|
+
const buffer = reader.readAsArrayBuffer(blob.slice(offset, end));
|
|
24
|
+
return new Uint8Array(buffer);
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Wraps an Origin Private File System `FileSystemSyncAccessHandle` in a zero-copy synchronous
|
|
30
|
+
* random-access `ByteSource`.
|
|
31
|
+
*
|
|
32
|
+
* Must be called inside a Dedicated Web Worker. Callers remain responsible for calling
|
|
33
|
+
* `handle.close()` after the opened `Document` has been freed.
|
|
34
|
+
*/
|
|
35
|
+
export function createOpfsSource(handle) {
|
|
36
|
+
const byteLength = handle.getSize();
|
|
37
|
+
return {
|
|
38
|
+
byteLength,
|
|
39
|
+
readAt(offset, length) {
|
|
40
|
+
if (offset >= byteLength || length <= 0) {
|
|
41
|
+
return new Uint8Array(0);
|
|
42
|
+
}
|
|
43
|
+
const toRead = Math.min(length, byteLength - offset);
|
|
44
|
+
const out = new Uint8Array(toRead);
|
|
45
|
+
const n = handle.read(out, { at: offset });
|
|
46
|
+
return n === toRead ? out : out.subarray(0, n);
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Wraps an in-memory `Uint8Array` in a `ByteSource` (useful for tests or custom adapters).
|
|
52
|
+
*/
|
|
53
|
+
export function createMemorySource(bytes) {
|
|
54
|
+
return {
|
|
55
|
+
byteLength: bytes.byteLength,
|
|
56
|
+
readAt(offset, length) {
|
|
57
|
+
if (offset >= bytes.byteLength || length <= 0) {
|
|
58
|
+
return new Uint8Array(0);
|
|
59
|
+
}
|
|
60
|
+
const end = Math.min(bytes.byteLength, offset + length);
|
|
61
|
+
return bytes.subarray(offset, end);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in Microsoft Word rendering targets.
|
|
3
|
+
*
|
|
4
|
+
* `"word-win"` (Microsoft Word for Windows) is the default and only stable target in v0.1.
|
|
5
|
+
* Preview targets (`"word-mac"`, `"word-web"`) require building `f-office-wasm` with
|
|
6
|
+
* `--features preview-targets`.
|
|
7
|
+
*/
|
|
8
|
+
export type TargetId = "word-win" | (string & {});
|
|
9
|
+
/** Resource limit preset when opening a DOCX file. */
|
|
10
|
+
export type LimitProfile = "default" | "mobile";
|
|
11
|
+
/** Options when opening a DOCX file. */
|
|
12
|
+
export interface OpenOptions {
|
|
13
|
+
/** Word target whose layout to reproduce. Defaults to `"word-win"`. */
|
|
14
|
+
target?: TargetId;
|
|
15
|
+
/**
|
|
16
|
+
* Local clock timestamp (`YYYY-MM-DDTHH:MM:SSZ`) for `DATE` and `TIME` fields.
|
|
17
|
+
* When omitted, fields show the cached result stored in the DOCX file.
|
|
18
|
+
*/
|
|
19
|
+
now?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Resource limits preset (`"default"` or `"mobile"`). Use `"mobile"` on phones and
|
|
22
|
+
* memory-constrained browser tabs to fail gracefully before exhausting tab memory.
|
|
23
|
+
*/
|
|
24
|
+
limits?: LimitProfile;
|
|
25
|
+
}
|
|
26
|
+
/** A rectangular pixel tile on a rasterized page, in whole pixels at the requested DPI. */
|
|
27
|
+
export interface PixelRegion {
|
|
28
|
+
x: number;
|
|
29
|
+
y: number;
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
}
|
|
33
|
+
/** A font family requested by a document and the ordered fallback families tried for it. */
|
|
34
|
+
export interface FontNeed {
|
|
35
|
+
/** Requested family name, or `null` when the text names no family. */
|
|
36
|
+
family: string | null;
|
|
37
|
+
/** Families tried by the target, in priority order. */
|
|
38
|
+
tried: string[];
|
|
39
|
+
}
|
|
40
|
+
/** A heading entry in the document outline. */
|
|
41
|
+
export interface OutlineEntry {
|
|
42
|
+
/** Heading depth from 0 (top level) to 8. */
|
|
43
|
+
level: number;
|
|
44
|
+
/** Single-line heading text. */
|
|
45
|
+
text: string;
|
|
46
|
+
/** Zero-based page index where the heading starts. */
|
|
47
|
+
page: number;
|
|
48
|
+
/** Stable body paragraph address (for example `"p12"`). */
|
|
49
|
+
address: string;
|
|
50
|
+
}
|
|
51
|
+
/** Dimensions of a page in typographic points (1/72 inch). */
|
|
52
|
+
export interface PageSizePoints {
|
|
53
|
+
width: number;
|
|
54
|
+
height: number;
|
|
55
|
+
}
|
|
56
|
+
/** An upright text run on a page, in points from the top-left corner. */
|
|
57
|
+
export interface TextRun {
|
|
58
|
+
text: string;
|
|
59
|
+
size: number;
|
|
60
|
+
left: number;
|
|
61
|
+
top: number;
|
|
62
|
+
width: number;
|
|
63
|
+
height: number;
|
|
64
|
+
baseline: number;
|
|
65
|
+
glyphLefts: Float64Array;
|
|
66
|
+
glyphWidths: Float64Array;
|
|
67
|
+
glyphEnds: Uint32Array;
|
|
68
|
+
}
|
|
69
|
+
/** Async callback that fetches font file bytes (`.ttf`, `.otf`, `.ttc`, `.otc`) for a family. */
|
|
70
|
+
export type FontFetcher = (family: string) => Promise<Uint8Array[] | Uint8Array | null | undefined>;
|
|
71
|
+
/** Optional source override passed to `init()`. */
|
|
72
|
+
export type WasmInitSource = URL | string | Request | Response | BufferSource | WebAssembly.Module;
|
|
73
|
+
/** Plain RGBA page image returned by the Web Worker client. */
|
|
74
|
+
export interface RenderedPageData {
|
|
75
|
+
width: number;
|
|
76
|
+
height: number;
|
|
77
|
+
pixels: Uint8ClampedArray;
|
|
78
|
+
warnings: string[];
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Synchronous random-access byte source for opening DOCX archives without copying the full file
|
|
82
|
+
* into WebAssembly linear memory (for example, backed by `FileReaderSync` over `Blob.slice()` or
|
|
83
|
+
* an OPFS `FileSystemSyncAccessHandle` in a Web Worker).
|
|
84
|
+
*/
|
|
85
|
+
export interface ByteSource {
|
|
86
|
+
/** Total archive size in bytes. */
|
|
87
|
+
readonly byteLength: number;
|
|
88
|
+
/** Reads up to `length` bytes starting at `offset` (returns an empty array at EOF). */
|
|
89
|
+
readAt(offset: number, length: number): Uint8Array;
|
|
90
|
+
}
|
|
91
|
+
/** Callback invoked with each streamed chunk of PDF bytes by `Document.writePdfTo()`. */
|
|
92
|
+
export type PdfChunkCallback = (chunk: Uint8Array) => void;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|