@seamless-co/pagemark 0.0.0-stage → 0.5.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 +21 -0
- package/README.md +25 -2
- package/behaviours/behaviour.d.ts +43 -0
- package/behaviours/behaviour.js +84 -0
- package/behaviours/comparison.d.ts +62 -0
- package/behaviours/comparison.js +129 -0
- package/behaviours/document-viewer.d.ts +9 -0
- package/behaviours/document-viewer.js +9 -0
- package/behaviours/hidden.d.ts +24 -0
- package/behaviours/hidden.js +38 -0
- package/behaviours/hover.d.ts +34 -0
- package/behaviours/hover.js +49 -0
- package/behaviours/index.d.ts +24 -0
- package/behaviours/index.js +14 -0
- package/behaviours/linked-viewers.d.ts +14 -0
- package/behaviours/linked-viewers.js +25 -0
- package/behaviours/links.d.ts +4 -0
- package/behaviours/links.js +4 -0
- package/behaviours/marks.d.ts +64 -0
- package/behaviours/marks.js +33 -0
- package/behaviours/quotes.d.ts +7 -0
- package/behaviours/quotes.js +9 -0
- package/behaviours/region.d.ts +27 -0
- package/behaviours/region.js +40 -0
- package/behaviours/search.d.ts +38 -0
- package/behaviours/search.js +63 -0
- package/behaviours/selection.d.ts +30 -0
- package/behaviours/selection.js +41 -0
- package/behaviours/text.d.ts +8 -0
- package/behaviours/text.js +24 -0
- package/behaviours/viewer-state.d.ts +36 -0
- package/behaviours/viewer-state.js +58 -0
- package/browser/blob-pieces.d.ts +9 -0
- package/browser/blob-pieces.js +50 -0
- package/browser/browser-source.d.ts +25 -0
- package/browser/browser-source.js +62 -0
- package/browser/index.d.ts +2 -0
- package/browser/index.js +1 -0
- package/browser/local-pdf.d.ts +27 -0
- package/browser/local-pdf.js +61 -0
- package/browser/worker-engine.d.ts +21 -0
- package/browser/worker-engine.js +1 -0
- package/browser/worker.d.ts +1 -0
- package/browser/worker.js +51 -0
- package/core/compare.d.ts +55 -0
- package/core/compare.js +350 -0
- package/core/find.d.ts +12 -0
- package/core/find.js +177 -0
- package/core/hidden.d.ts +12 -0
- package/core/hidden.js +26 -0
- package/core/index.d.ts +11 -0
- package/core/index.js +6 -0
- package/core/links.d.ts +7 -0
- package/core/links.js +19 -0
- package/core/locate.d.ts +21 -0
- package/core/locate.js +140 -0
- package/core/paragraphs.d.ts +37 -0
- package/core/paragraphs.js +141 -0
- package/core/types.d.ts +89 -0
- package/core/types.js +1 -0
- package/document/document.d.ts +83 -0
- package/document/document.js +215 -0
- package/document/handover-source.d.ts +18 -0
- package/document/handover-source.js +39 -0
- package/document/index.d.ts +10 -0
- package/document/index.js +5 -0
- package/document/reading-source.d.ts +7 -0
- package/document/reading-source.js +16 -0
- package/document/server-source.d.ts +10 -0
- package/document/server-source.js +23 -0
- package/document/source.d.ts +21 -0
- package/document/source.js +1 -0
- package/document/split-source.d.ts +9 -0
- package/document/split-source.js +25 -0
- package/document/store.d.ts +12 -0
- package/document/store.js +21 -0
- package/engine/engine-thread.d.ts +10 -0
- package/engine/engine-thread.js +1 -0
- package/engine/index.d.ts +2 -0
- package/engine/index.js +1 -0
- package/engine/pdfium.d.ts +16 -0
- package/engine/pdfium.js +222 -0
- package/engine/remote.d.ts +51 -0
- package/engine/remote.js +109 -0
- package/package.json +65 -4
- package/react/hooks.d.ts +128 -0
- package/react/hooks.js +129 -0
- package/react/index.d.ts +3 -0
- package/react/index.js +2 -0
- package/react/viewer.d.ts +48 -0
- package/react/viewer.js +127 -0
- package/reader/assemble.d.ts +4 -0
- package/reader/assemble.js +29 -0
- package/reader/index.d.ts +7 -0
- package/reader/index.js +5 -0
- package/reader/layer-page.d.ts +30 -0
- package/reader/layer-page.js +40 -0
- package/reader/layer-words.d.ts +4 -0
- package/reader/layer-words.js +102 -0
- package/reader/letter-edges.d.ts +13 -0
- package/reader/letter-edges.js +60 -0
- package/reader/links.d.ts +7 -0
- package/reader/links.js +22 -0
- package/reader/seams.d.ts +92 -0
- package/reader/seams.js +3 -0
- package/reader/seen.d.ts +16 -0
- package/reader/seen.js +67 -0
- package/reader/trust.d.ts +9 -0
- package/reader/trust.js +34 -0
- package/viewer/attachments.d.ts +11 -0
- package/viewer/attachments.js +104 -0
- package/viewer/dom.d.ts +14 -0
- package/viewer/dom.js +38 -0
- package/viewer/events.d.ts +17 -0
- package/viewer/events.js +20 -0
- package/viewer/index.d.ts +17 -0
- package/viewer/index.js +8 -0
- package/viewer/links.d.ts +16 -0
- package/viewer/links.js +59 -0
- package/viewer/marks.d.ts +24 -0
- package/viewer/marks.js +171 -0
- package/viewer/page-layers.d.ts +13 -0
- package/viewer/page-layers.js +33 -0
- package/viewer/pictures.d.ts +24 -0
- package/viewer/pictures.js +293 -0
- package/viewer/pointer.d.ts +21 -0
- package/viewer/pointer.js +244 -0
- package/viewer/stage.d.ts +21 -0
- package/viewer/stage.js +31 -0
- package/viewer/text-between.d.ts +13 -0
- package/viewer/text-between.js +28 -0
- package/viewer/text.d.ts +20 -0
- package/viewer/text.js +180 -0
- package/viewer/tile-grid.d.ts +20 -0
- package/viewer/tile-grid.js +36 -0
- package/viewer/tiles.d.ts +15 -0
- package/viewer/tiles.js +190 -0
- package/viewer/turn.d.ts +18 -0
- package/viewer/turn.js +40 -0
- package/viewer/types.d.ts +238 -0
- package/viewer/types.js +1 -0
- package/viewer/view-over.d.ts +21 -0
- package/viewer/view-over.js +191 -0
- package/viewer/view.d.ts +59 -0
- package/viewer/view.js +1 -0
- package/viewer/viewer.css +301 -0
- package/viewer/viewer.d.ts +11 -0
- package/viewer/viewer.js +104 -0
- package/viewer/viewport.d.ts +37 -0
- package/viewer/viewport.js +429 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { OpenPdf } from "../reader/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: a file the engine reads a piece at a time, as it needs them, so the file never sits whole in the engine's
|
|
4
|
+
* memory. `read` answers at once with the bytes from `offset` on; fewer than asked means the file ended.
|
|
5
|
+
*/
|
|
6
|
+
export type FilePieces = {
|
|
7
|
+
size: number;
|
|
8
|
+
read(offset: number, length: number): Uint8Array;
|
|
9
|
+
};
|
|
10
|
+
/** NOTE: a PDF open in PDFium in the caller's own thread, which always lends a drawn page. */
|
|
11
|
+
export type LendingPdf = OpenPdf & Required<Pick<OpenPdf, "withDrawnPage">>;
|
|
12
|
+
export type OpenPdfOptions = {
|
|
13
|
+
/** NOTE: the engine's WebAssembly file, for a caller that fetched it. Left out, PDFium finds it beside its own code, where a bundler copies it. */
|
|
14
|
+
wasmBinary?: ArrayBuffer;
|
|
15
|
+
};
|
|
16
|
+
export declare function openPdf(data: Uint8Array | FilePieces, options?: OpenPdfOptions): Promise<LendingPdf>;
|
package/engine/pdfium.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { PDFiumModule } from "@hyzyla/pdfium";
|
|
2
|
+
const FPDFBITMAP_BGRA = 4;
|
|
3
|
+
const FPDF_ANNOT = 0x01;
|
|
4
|
+
const FPDF_REVERSE_BYTE_ORDER = 0x10;
|
|
5
|
+
const OPAQUE_WHITE = 0xffffffff;
|
|
6
|
+
/** NOTE: the two ways a PDF says "do not paint this text": neither filled nor outlined, and used only to clip. */
|
|
7
|
+
const TEXT_INVISIBLE = 3;
|
|
8
|
+
const TEXT_CLIP_ONLY = 7;
|
|
9
|
+
/** NOTE: PDFium reports display positions as whole device pixels, so positions are asked for on a device this many times the page's size. */
|
|
10
|
+
const POSITION_PRECISION = 100;
|
|
11
|
+
const SCRATCH_BYTES = 64;
|
|
12
|
+
/** NOTE: where in the scratch a link is read: how far the enumeration has come, the link it found, and that link's box. */
|
|
13
|
+
const LINK_POSITION = 40;
|
|
14
|
+
const LINK_HANDLE = 44;
|
|
15
|
+
const LINK_RECT = 48;
|
|
16
|
+
const ACTION_GOTO = 1;
|
|
17
|
+
const ACTION_URI = 3;
|
|
18
|
+
/** NOTE: the bytes PDFium's file access takes in its memory: the file's length, the function that reads a piece, and a value handed back to it. */
|
|
19
|
+
const FILE_ACCESS_BYTES = 12;
|
|
20
|
+
let loading = null;
|
|
21
|
+
function pdfium(options) {
|
|
22
|
+
loading ??= PDFiumModule(options.wasmBinary ? { wasmBinary: options.wasmBinary } : {}).then((module) => {
|
|
23
|
+
const raw = module;
|
|
24
|
+
raw._FPDF_InitLibrary();
|
|
25
|
+
return raw;
|
|
26
|
+
});
|
|
27
|
+
return loading;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* NOTE: hands PDFium the file: copied whole into its memory, or read through a function of ours a piece at a time.
|
|
31
|
+
* `release` frees what was made for it, once the document is closed.
|
|
32
|
+
*/
|
|
33
|
+
function loadDocument(raw, file) {
|
|
34
|
+
if (file instanceof Uint8Array) {
|
|
35
|
+
const source = raw._malloc(Math.max(1, file.byteLength));
|
|
36
|
+
raw.HEAPU8.set(file, source);
|
|
37
|
+
return { document: raw._FPDF_LoadMemDocument(source, file.byteLength, 0), release: () => raw._free(source) };
|
|
38
|
+
}
|
|
39
|
+
/** CONSTRAINT: PDFium's memory may grow while a piece is copied in, so its view is asked for again each time. */
|
|
40
|
+
const reader = raw.addFunction((_value, offset, into, length) => {
|
|
41
|
+
try {
|
|
42
|
+
const piece = file.read(offset, length);
|
|
43
|
+
if (piece.byteLength !== length)
|
|
44
|
+
return 0;
|
|
45
|
+
raw.HEAPU8.set(piece, into);
|
|
46
|
+
return 1;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return 0;
|
|
50
|
+
}
|
|
51
|
+
}, "iiiii");
|
|
52
|
+
const access = raw._malloc(FILE_ACCESS_BYTES);
|
|
53
|
+
const fields = new DataView(raw.HEAPU8.buffer, access, FILE_ACCESS_BYTES);
|
|
54
|
+
fields.setUint32(0, file.size, true);
|
|
55
|
+
fields.setUint32(4, reader, true);
|
|
56
|
+
fields.setUint32(8, 0, true);
|
|
57
|
+
return {
|
|
58
|
+
document: raw._FPDF_LoadCustomDocument(access, 0),
|
|
59
|
+
release: () => {
|
|
60
|
+
raw._free(access);
|
|
61
|
+
raw.removeFunction(reader);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export async function openPdf(data, options = {}) {
|
|
66
|
+
const raw = await pdfium(options);
|
|
67
|
+
const scratch = raw._malloc(SCRATCH_BYTES);
|
|
68
|
+
const { document, release } = loadDocument(raw, data);
|
|
69
|
+
if (!document) {
|
|
70
|
+
const code = raw._FPDF_GetLastError();
|
|
71
|
+
release();
|
|
72
|
+
raw._free(scratch);
|
|
73
|
+
throw new Error(code === 4 ? "This PDF needs a password." : `PDFium could not open the file (error ${code}).`);
|
|
74
|
+
}
|
|
75
|
+
const withPage = (index, use) => {
|
|
76
|
+
const page = raw._FPDF_LoadPage(document, index);
|
|
77
|
+
if (!page)
|
|
78
|
+
throw new Error(`PDFium could not load page ${index + 1}.`);
|
|
79
|
+
try {
|
|
80
|
+
return use(page);
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
raw._FPDF_ClosePage(page);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const sizeOf = (page) => ({ width: raw._FPDF_GetPageWidthF(page), height: raw._FPDF_GetPageHeightF(page) });
|
|
87
|
+
/** NOTE: an address as the file has it. PDFium says how long it is when asked with no room to write it. */
|
|
88
|
+
const addressOf = (action) => {
|
|
89
|
+
const size = raw._FPDFAction_GetURIPath(document, action, 0, 0);
|
|
90
|
+
if (size <= 1)
|
|
91
|
+
return undefined;
|
|
92
|
+
const buffer = raw._malloc(size);
|
|
93
|
+
try {
|
|
94
|
+
raw._FPDFAction_GetURIPath(document, action, buffer, size);
|
|
95
|
+
return new TextDecoder().decode(raw.HEAPU8.slice(buffer, buffer + size - 1));
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
raw._free(buffer);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
/** NOTE: where a link leads: a page of this file, written as a destination of its own or as an action, or an address. */
|
|
102
|
+
const leadsTo = (link) => {
|
|
103
|
+
let destination = raw._FPDFLink_GetDest(document, link);
|
|
104
|
+
if (!destination) {
|
|
105
|
+
const action = raw._FPDFLink_GetAction(link);
|
|
106
|
+
const type = action ? raw._FPDFAction_GetType(action) : 0;
|
|
107
|
+
if (type === ACTION_URI)
|
|
108
|
+
return { url: addressOf(action) };
|
|
109
|
+
if (type === ACTION_GOTO)
|
|
110
|
+
destination = raw._FPDFAction_GetDest(document, action);
|
|
111
|
+
}
|
|
112
|
+
const page = destination ? raw._FPDFDest_GetDestPageIndex(document, destination) : -1;
|
|
113
|
+
return page >= 0 ? { page } : {};
|
|
114
|
+
};
|
|
115
|
+
/** NOTE: draws a page or a part of it and hands the picture to `use` where PDFium drew it, freeing it afterwards. */
|
|
116
|
+
function lend(index, scale, part, use) {
|
|
117
|
+
return withPage(index, (page) => {
|
|
118
|
+
const size = sizeOf(page);
|
|
119
|
+
const pageWidth = Math.max(1, Math.ceil(size.width * scale));
|
|
120
|
+
const pageHeight = Math.max(1, Math.ceil(size.height * scale));
|
|
121
|
+
const left = part ? Math.round(part.x * scale) : 0;
|
|
122
|
+
const top = part ? Math.round(part.y * scale) : 0;
|
|
123
|
+
const width = part ? Math.max(1, Math.round(part.width * scale)) : pageWidth;
|
|
124
|
+
const height = part ? Math.max(1, Math.round(part.height * scale)) : pageHeight;
|
|
125
|
+
const bitmap = raw._FPDFBitmap_CreateEx(width, height, FPDFBITMAP_BGRA, 0, 0);
|
|
126
|
+
if (!bitmap)
|
|
127
|
+
throw new Error(`PDFium could not allocate a ${width}x${height} page image.`);
|
|
128
|
+
try {
|
|
129
|
+
raw._FPDFBitmap_FillRect(bitmap, 0, 0, width, height, OPAQUE_WHITE);
|
|
130
|
+
raw._FPDF_RenderPageBitmap(bitmap, page, -left, -top, pageWidth, pageHeight, 0, FPDF_ANNOT | FPDF_REVERSE_BYTE_ORDER);
|
|
131
|
+
const buffer = raw._FPDFBitmap_GetBuffer(bitmap);
|
|
132
|
+
const stride = raw._FPDFBitmap_GetStride(bitmap);
|
|
133
|
+
return use({ rgba: rowsOf(buffer, stride, width, height), width, height });
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
raw._FPDFBitmap_Destroy(bitmap);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/** NOTE: the picture's rows end to end: where PDFium drew them, when nothing lies between, and gathered otherwise. */
|
|
141
|
+
function rowsOf(buffer, stride, width, height) {
|
|
142
|
+
if (stride === width * 4)
|
|
143
|
+
return raw.HEAPU8.subarray(buffer, buffer + stride * height);
|
|
144
|
+
const rgba = new Uint8Array(width * height * 4);
|
|
145
|
+
for (let row = 0; row < height; row += 1) {
|
|
146
|
+
const from = buffer + row * stride;
|
|
147
|
+
rgba.set(raw.HEAPU8.subarray(from, from + width * 4), row * width * 4);
|
|
148
|
+
}
|
|
149
|
+
return rgba;
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
pageCount: raw._FPDF_GetPageCount(document),
|
|
153
|
+
pageSize: (index) => withPage(index, sizeOf),
|
|
154
|
+
readPage: (index) => withPage(index, (page) => {
|
|
155
|
+
const { width, height } = sizeOf(page);
|
|
156
|
+
const deviceWidth = Math.round(width * POSITION_PRECISION);
|
|
157
|
+
const deviceHeight = Math.round(height * POSITION_PRECISION);
|
|
158
|
+
const toDisplay = (x, y) => {
|
|
159
|
+
raw._FPDF_PageToDevice(page, 0, 0, deviceWidth, deviceHeight, 0, x, y, scratch + 32, scratch + 36);
|
|
160
|
+
const view = new DataView(raw.HEAPU8.buffer);
|
|
161
|
+
return [view.getInt32(scratch + 32, true) / POSITION_PRECISION, view.getInt32(scratch + 36, true) / POSITION_PRECISION];
|
|
162
|
+
};
|
|
163
|
+
const links = [];
|
|
164
|
+
new DataView(raw.HEAPU8.buffer).setInt32(scratch + LINK_POSITION, 0, true);
|
|
165
|
+
while (raw._FPDFLink_Enumerate(page, scratch + LINK_POSITION, scratch + LINK_HANDLE)) {
|
|
166
|
+
const link = new DataView(raw.HEAPU8.buffer).getUint32(scratch + LINK_HANDLE, true);
|
|
167
|
+
if (!raw._FPDFLink_GetAnnotRect(link, scratch + LINK_RECT))
|
|
168
|
+
continue;
|
|
169
|
+
const rect = new DataView(raw.HEAPU8.buffer);
|
|
170
|
+
const [x0, y0] = toDisplay(rect.getFloat32(scratch + LINK_RECT, true), rect.getFloat32(scratch + LINK_RECT + 4, true));
|
|
171
|
+
const [x1, y1] = toDisplay(rect.getFloat32(scratch + LINK_RECT + 8, true), rect.getFloat32(scratch + LINK_RECT + 12, true));
|
|
172
|
+
links.push({ left: Math.min(x0, x1), top: Math.min(y0, y1), right: Math.max(x0, x1), bottom: Math.max(y0, y1), ...leadsTo(link) });
|
|
173
|
+
}
|
|
174
|
+
const chars = [];
|
|
175
|
+
const textPage = raw._FPDFText_LoadPage(page);
|
|
176
|
+
if (!textPage)
|
|
177
|
+
return { width, height, chars, links };
|
|
178
|
+
try {
|
|
179
|
+
const count = raw._FPDFText_CountChars(textPage);
|
|
180
|
+
let lastObject = 0;
|
|
181
|
+
let isUnpainted = false;
|
|
182
|
+
for (let index = 0; index < count; index += 1) {
|
|
183
|
+
const code = raw._FPDFText_GetUnicode(textPage, index) >>> 0;
|
|
184
|
+
const object = raw._FPDFText_GetTextObject(textPage, index);
|
|
185
|
+
if (object !== lastObject) {
|
|
186
|
+
const mode = object ? raw._FPDFTextObj_GetTextRenderMode(object) : 0;
|
|
187
|
+
isUnpainted = mode === TEXT_INVISIBLE || mode === TEXT_CLIP_ONLY;
|
|
188
|
+
lastObject = object;
|
|
189
|
+
}
|
|
190
|
+
const hasBox = raw._FPDFText_GetCharBox(textPage, index, scratch, scratch + 8, scratch + 16, scratch + 24);
|
|
191
|
+
const view = new DataView(raw.HEAPU8.buffer);
|
|
192
|
+
const left = hasBox ? view.getFloat64(scratch, true) : 0;
|
|
193
|
+
const right = hasBox ? view.getFloat64(scratch + 8, true) : 0;
|
|
194
|
+
const bottom = hasBox ? view.getFloat64(scratch + 16, true) : 0;
|
|
195
|
+
const top = hasBox ? view.getFloat64(scratch + 24, true) : 0;
|
|
196
|
+
const [x0, y0] = toDisplay(left, top);
|
|
197
|
+
const [x1, y1] = toDisplay(right, bottom);
|
|
198
|
+
chars.push({
|
|
199
|
+
code,
|
|
200
|
+
left: Math.min(x0, x1),
|
|
201
|
+
top: Math.min(y0, y1),
|
|
202
|
+
right: Math.max(x0, x1),
|
|
203
|
+
bottom: Math.max(y0, y1),
|
|
204
|
+
hasMapError: raw._FPDFText_HasUnicodeMapError(textPage, index) === 1,
|
|
205
|
+
isUnpainted,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
finally {
|
|
210
|
+
raw._FPDFText_ClosePage(textPage);
|
|
211
|
+
}
|
|
212
|
+
return { width, height, chars, links };
|
|
213
|
+
}),
|
|
214
|
+
drawPage: (index, scale, part) => lend(index, scale, part, (raster) => ({ ...raster, rgba: raster.rgba.slice() })),
|
|
215
|
+
withDrawnPage: lend,
|
|
216
|
+
close: () => {
|
|
217
|
+
raw._FPDF_CloseDocument(document);
|
|
218
|
+
release();
|
|
219
|
+
raw._free(scratch);
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: calling a thing that lives in another thread as if it were here. One side serves an object. The other holds
|
|
3
|
+
* a remote of it, whose every method sends the call across and answers with what came back. Neither side writes a
|
|
4
|
+
* message by hand, so adding a method means adding it to the object and to its type, and nothing else.
|
|
5
|
+
* NOTE: it knows no kind of thread. A `Port` is made from a browser's worker or from Node's, and the rest is the same.
|
|
6
|
+
*/
|
|
7
|
+
/** NOTE: one end of a channel between two threads. */
|
|
8
|
+
export type Port = {
|
|
9
|
+
/** NOTE: sends a message. The memory listed is handed over to the other thread, and is no longer the sender's. */
|
|
10
|
+
post(message: unknown, memory: ArrayBuffer[]): void;
|
|
11
|
+
/** NOTE: calls `listener` with each message that arrives, until the function it returns is called. */
|
|
12
|
+
listen(listener: (message: unknown) => void): () => void;
|
|
13
|
+
};
|
|
14
|
+
/** NOTE: the same methods, each answering later, since every answer crosses between threads. */
|
|
15
|
+
export type Remote<Served> = {
|
|
16
|
+
readonly [Name in keyof Served]: Served[Name] extends (...values: infer Values) => infer Answer ? (...values: Values) => Promise<Awaited<Answer>> : never;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* NOTE: marks a value whose memory is handed to the other thread with it, not copied: a file's bytes going in, a
|
|
20
|
+
* page's picture coming out. The sender's own view of that memory is empty afterwards.
|
|
21
|
+
*/
|
|
22
|
+
export declare function transfer<Value extends object>(value: Value, memory: ArrayBuffer[]): Value;
|
|
23
|
+
/**
|
|
24
|
+
* NOTE: answers every call that arrives on `port` by running the method of that name on `served`. It returns the
|
|
25
|
+
* function that stops it answering.
|
|
26
|
+
* CONSTRAINT: only a method `served` has as its own is run. A name every object answers to, such as `constructor`,
|
|
27
|
+
* is refused like any name it was not given.
|
|
28
|
+
*/
|
|
29
|
+
export declare function serve(port: Port, served: object): () => void;
|
|
30
|
+
/**
|
|
31
|
+
* NOTE: a remote of what the other end of `port` serves. `call` has its methods. `stop` ends it: everyone waiting on
|
|
32
|
+
* an answer is refused with the reason given, and so is every call after.
|
|
33
|
+
* WARNING: other things arrive on a channel besides answers. Node reports the files a thread imports there when a
|
|
34
|
+
* program runs under `node --watch`. Only a reply that carries the id of a call still waiting is taken for an answer.
|
|
35
|
+
*/
|
|
36
|
+
export declare function remote<Served extends object>(port: Port): {
|
|
37
|
+
call: Remote<Served>;
|
|
38
|
+
stop(reason: Error): void;
|
|
39
|
+
};
|
|
40
|
+
/** NOTE: a port over a browser's worker, or over a worker's own `self`. */
|
|
41
|
+
export declare function webPort(end: {
|
|
42
|
+
postMessage(message: unknown, transfer: Transferable[]): void;
|
|
43
|
+
addEventListener(type: "message", listener: (event: MessageEvent) => void): void;
|
|
44
|
+
removeEventListener(type: "message", listener: (event: MessageEvent) => void): void;
|
|
45
|
+
}): Port;
|
|
46
|
+
/** NOTE: a port over one of Node's worker threads, or over a thread's own `parentPort`. */
|
|
47
|
+
export declare function nodePort(end: {
|
|
48
|
+
postMessage(message: unknown, transfer: ArrayBuffer[]): void;
|
|
49
|
+
on(event: "message", listener: (message: unknown) => void): unknown;
|
|
50
|
+
off(event: "message", listener: (message: unknown) => void): unknown;
|
|
51
|
+
}): Port;
|
package/engine/remote.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: calling a thing that lives in another thread as if it were here. One side serves an object. The other holds
|
|
3
|
+
* a remote of it, whose every method sends the call across and answers with what came back. Neither side writes a
|
|
4
|
+
* message by hand, so adding a method means adding it to the object and to its type, and nothing else.
|
|
5
|
+
* NOTE: it knows no kind of thread. A `Port` is made from a browser's worker or from Node's, and the rest is the same.
|
|
6
|
+
*/
|
|
7
|
+
const handedOver = new WeakMap();
|
|
8
|
+
/**
|
|
9
|
+
* NOTE: marks a value whose memory is handed to the other thread with it, not copied: a file's bytes going in, a
|
|
10
|
+
* page's picture coming out. The sender's own view of that memory is empty afterwards.
|
|
11
|
+
*/
|
|
12
|
+
export function transfer(value, memory) {
|
|
13
|
+
handedOver.set(value, memory);
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
const memoryOf = (value) => (typeof value === "object" && value !== null ? (handedOver.get(value) ?? []) : []);
|
|
17
|
+
const isRecord = (message) => typeof message === "object" && message !== null;
|
|
18
|
+
/**
|
|
19
|
+
* NOTE: answers every call that arrives on `port` by running the method of that name on `served`. It returns the
|
|
20
|
+
* function that stops it answering.
|
|
21
|
+
* CONSTRAINT: only a method `served` has as its own is run. A name every object answers to, such as `constructor`,
|
|
22
|
+
* is refused like any name it was not given.
|
|
23
|
+
*/
|
|
24
|
+
export function serve(port, served) {
|
|
25
|
+
const methods = served;
|
|
26
|
+
return port.listen((message) => {
|
|
27
|
+
if (!isRecord(message) || typeof message.id !== "number" || typeof message.method !== "string" || !Array.isArray(message.values))
|
|
28
|
+
return;
|
|
29
|
+
const { id, method, values } = message;
|
|
30
|
+
const run = async () => {
|
|
31
|
+
const own = Object.hasOwn(methods, method) ? methods[method] : undefined;
|
|
32
|
+
if (typeof own !== "function")
|
|
33
|
+
throw new Error(`The other thread has no ${method}.`);
|
|
34
|
+
return own.apply(served, values);
|
|
35
|
+
};
|
|
36
|
+
run().then((value) => port.post({ id, value }, memoryOf(value)), (error) => port.post({ id, error: error instanceof Error ? error.message : String(error) }, []));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* NOTE: a remote of what the other end of `port` serves. `call` has its methods. `stop` ends it: everyone waiting on
|
|
41
|
+
* an answer is refused with the reason given, and so is every call after.
|
|
42
|
+
* WARNING: other things arrive on a channel besides answers. Node reports the files a thread imports there when a
|
|
43
|
+
* program runs under `node --watch`. Only a reply that carries the id of a call still waiting is taken for an answer.
|
|
44
|
+
*/
|
|
45
|
+
export function remote(port) {
|
|
46
|
+
const waiting = new Map();
|
|
47
|
+
let stopped = null;
|
|
48
|
+
let lastId = 0;
|
|
49
|
+
const unlisten = port.listen((message) => {
|
|
50
|
+
if (!isRecord(message) || typeof message.id !== "number")
|
|
51
|
+
return;
|
|
52
|
+
const asked = waiting.get(message.id);
|
|
53
|
+
if (!asked || !("value" in message || typeof message.error === "string"))
|
|
54
|
+
return;
|
|
55
|
+
waiting.delete(message.id);
|
|
56
|
+
if (typeof message.error === "string")
|
|
57
|
+
asked.reject(new Error(message.error));
|
|
58
|
+
else
|
|
59
|
+
asked.resolve(message.value);
|
|
60
|
+
});
|
|
61
|
+
const send = (method, values) => new Promise((resolve, reject) => {
|
|
62
|
+
if (stopped) {
|
|
63
|
+
reject(stopped);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
lastId += 1;
|
|
67
|
+
waiting.set(lastId, { resolve, reject });
|
|
68
|
+
port.post({ id: lastId, method, values }, values.flatMap(memoryOf));
|
|
69
|
+
});
|
|
70
|
+
/** CONSTRAINT: it answers to no `then`, or anything that awaits it would take it for a promise and call that across the threads. */
|
|
71
|
+
const call = new Proxy({}, {
|
|
72
|
+
get: (_target, method) => typeof method === "string" && method !== "then" ? (...values) => send(method, values) : undefined,
|
|
73
|
+
});
|
|
74
|
+
return {
|
|
75
|
+
call,
|
|
76
|
+
stop(reason) {
|
|
77
|
+
if (stopped)
|
|
78
|
+
return;
|
|
79
|
+
stopped = reason;
|
|
80
|
+
unlisten();
|
|
81
|
+
for (const asked of waiting.values())
|
|
82
|
+
asked.reject(reason);
|
|
83
|
+
waiting.clear();
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/** NOTE: a port over a browser's worker, or over a worker's own `self`. */
|
|
88
|
+
export function webPort(end) {
|
|
89
|
+
return {
|
|
90
|
+
post: (message, memory) => end.postMessage(message, memory),
|
|
91
|
+
listen(listener) {
|
|
92
|
+
const onMessage = (event) => listener(event.data);
|
|
93
|
+
end.addEventListener("message", onMessage);
|
|
94
|
+
return () => end.removeEventListener("message", onMessage);
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/** NOTE: a port over one of Node's worker threads, or over a thread's own `parentPort`. */
|
|
99
|
+
export function nodePort(end) {
|
|
100
|
+
return {
|
|
101
|
+
post: (message, memory) => end.postMessage(message, memory),
|
|
102
|
+
listen(listener) {
|
|
103
|
+
end.on("message", listener);
|
|
104
|
+
return () => {
|
|
105
|
+
end.off("message", listener);
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seamless-co/pagemark",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Locates text on PDF pages and marks it, in the browser.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/seamless-co/pagemark.git"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": [
|
|
15
|
+
"*.css"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
"./core": {
|
|
19
|
+
"types": "./core/index.d.ts",
|
|
20
|
+
"default": "./core/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./document": {
|
|
23
|
+
"types": "./document/index.d.ts",
|
|
24
|
+
"default": "./document/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./reader": {
|
|
27
|
+
"types": "./reader/index.d.ts",
|
|
28
|
+
"default": "./reader/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./engine": {
|
|
31
|
+
"types": "./engine/index.d.ts",
|
|
32
|
+
"default": "./engine/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./browser": {
|
|
35
|
+
"types": "./browser/index.d.ts",
|
|
36
|
+
"default": "./browser/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./viewer": {
|
|
39
|
+
"types": "./viewer/index.d.ts",
|
|
40
|
+
"default": "./viewer/index.js"
|
|
41
|
+
},
|
|
42
|
+
"./behaviours": {
|
|
43
|
+
"types": "./behaviours/index.d.ts",
|
|
44
|
+
"default": "./behaviours/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./react": {
|
|
47
|
+
"types": "./react/index.d.ts",
|
|
48
|
+
"default": "./react/index.js"
|
|
49
|
+
},
|
|
50
|
+
"./viewer.css": "./viewer/viewer.css"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@hyzyla/pdfium": "3.0.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"react": ">=18",
|
|
57
|
+
"react-dom": ">=18"
|
|
58
|
+
},
|
|
59
|
+
"peerDependenciesMeta": {
|
|
60
|
+
"react": {
|
|
61
|
+
"optional": true
|
|
62
|
+
},
|
|
63
|
+
"react-dom": {
|
|
64
|
+
"optional": true
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
package/react/hooks.d.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { type WatchableViewer } from "../behaviours/index.js";
|
|
2
|
+
import { type DocumentSource, type DocumentState, type OpenDocumentOptions, type PagemarkDocument, type Readable } from "../document/index.js";
|
|
3
|
+
import type { Viewer, ViewerSpread } from "../viewer/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* NOTE: follows anything that holds a state and says when it changes: a document, a behaviour, one of your own. With
|
|
6
|
+
* a `fallback` it also takes null, and gives the fallback while there is nothing to follow.
|
|
7
|
+
* NOTE: a server renders what the thing holds at that moment, as a browser's first render does.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useReadable<T>(readable: Readable<T>): T;
|
|
10
|
+
export declare function useReadable<T>(readable: Readable<T> | null, fallback: T): T;
|
|
11
|
+
/**
|
|
12
|
+
* NOTE: an open document made for `key` and closed, with its source, when the key changes or the component goes. The
|
|
13
|
+
* key names what is open, as a document's id does, so a caller keeps no identity of its own. The document is null
|
|
14
|
+
* until it is made, and for a null key.
|
|
15
|
+
*/
|
|
16
|
+
export declare function useDocument(key: string | null, source: () => DocumentSource, options?: OpenDocumentOptions): PagemarkDocument | null;
|
|
17
|
+
/** NOTE: how far a document has come: its status, its pages' sizes, and the pages with whatever words have arrived. */
|
|
18
|
+
export declare function useDocumentState(document: PagemarkDocument | null): DocumentState;
|
|
19
|
+
/**
|
|
20
|
+
* NOTE: looks for `quote` in a document: the places it occurs, their marks, the one in focus, and the commands that
|
|
21
|
+
* step through them. A new document starts a new search.
|
|
22
|
+
*/
|
|
23
|
+
export declare function useSearch(document: PagemarkDocument | null, quote: string): {
|
|
24
|
+
matches: import("../core/index.js").Match[];
|
|
25
|
+
marks: import("../behaviours/index.js").MatchMark[];
|
|
26
|
+
focused: number;
|
|
27
|
+
focusedMarkIds: string[];
|
|
28
|
+
quote: string;
|
|
29
|
+
isPartial: boolean;
|
|
30
|
+
next: () => void;
|
|
31
|
+
previous: () => void;
|
|
32
|
+
focus: (index: number) => void;
|
|
33
|
+
};
|
|
34
|
+
/** NOTE: the line under the pointer, described, with its mark. Hand `onPointer` to the viewer. */
|
|
35
|
+
export declare function useLineHover(document: PagemarkDocument | null): {
|
|
36
|
+
hovered: import("../behaviours/index.js").HoveredLine | null;
|
|
37
|
+
line: import("../behaviours/index.js").LineDescription | null;
|
|
38
|
+
marks: import("../viewer/index.js").ViewerMark[];
|
|
39
|
+
onPointer: (point: import("../viewer/index.js").PagePoint | null) => void;
|
|
40
|
+
};
|
|
41
|
+
/** NOTE: the words a drag runs over, in reading order, with their text and marks. Hand `onDrag` to the viewer. */
|
|
42
|
+
export declare function useWordSelection(document: PagemarkDocument | null): {
|
|
43
|
+
range: import("../behaviours/index.js").WordRange | null;
|
|
44
|
+
text: string;
|
|
45
|
+
marks: import("../viewer/index.js").ViewerMark[];
|
|
46
|
+
onDrag: (drag: import("../viewer/index.js").ViewerDrag) => void;
|
|
47
|
+
clear: () => void;
|
|
48
|
+
};
|
|
49
|
+
/** NOTE: a box a reader drags, and the words inside it. Hand `onDrag` to the viewer. */
|
|
50
|
+
export declare function useBoxSelection(document: PagemarkDocument | null): {
|
|
51
|
+
region: import("../behaviours/index.js").Region | null;
|
|
52
|
+
read: import("../core/index.js").RegionReading | null;
|
|
53
|
+
marks: import("../viewer/index.js").ViewerMark[];
|
|
54
|
+
onDrag: (drag: import("../viewer/index.js").ViewerDrag) => void;
|
|
55
|
+
clear: () => void;
|
|
56
|
+
};
|
|
57
|
+
/** NOTE: the words a reader cannot see, in runs, with a mark for each and their count. */
|
|
58
|
+
export declare function useHiddenWords(document: PagemarkDocument | null): import("../behaviours/index.js").HiddenWordsState;
|
|
59
|
+
type Versions = [before: PagemarkDocument | null, after: PagemarkDocument | null];
|
|
60
|
+
/**
|
|
61
|
+
* NOTE: what changed between two versions, told as runs of words: the changes, each side's marks, the change in
|
|
62
|
+
* hand and the ids of its marks on each side, and the commands that step through the changes.
|
|
63
|
+
* NOTE: it fetches every word of both versions itself, and `isPartial` says while it waits, so a caller neither
|
|
64
|
+
* opens a version whole nor watches for it to be complete.
|
|
65
|
+
*/
|
|
66
|
+
export declare function useComparison(...versions: Versions): {
|
|
67
|
+
changes: import("../core/index.js").Change[];
|
|
68
|
+
marksBefore: import("../behaviours/index.js").ChangeMark[];
|
|
69
|
+
marksAfter: import("../behaviours/index.js").ChangeMark[];
|
|
70
|
+
focused: number;
|
|
71
|
+
focusedBefore: string[];
|
|
72
|
+
focusedAfter: string[];
|
|
73
|
+
isPartial: boolean;
|
|
74
|
+
next: () => void;
|
|
75
|
+
previous: () => void;
|
|
76
|
+
focus: (index: number) => void;
|
|
77
|
+
};
|
|
78
|
+
/** NOTE: what changed between two versions, told by paragraph, as `useComparison` tells it by words. */
|
|
79
|
+
export declare function useParagraphComparison(...versions: Versions): {
|
|
80
|
+
changes: import("../core/index.js").ParagraphChange[];
|
|
81
|
+
marksBefore: import("../behaviours/index.js").ParagraphMark[];
|
|
82
|
+
marksAfter: import("../behaviours/index.js").ParagraphMark[];
|
|
83
|
+
focused: number;
|
|
84
|
+
focusedBefore: string[];
|
|
85
|
+
focusedAfter: string[];
|
|
86
|
+
isPartial: boolean;
|
|
87
|
+
next: () => void;
|
|
88
|
+
previous: () => void;
|
|
89
|
+
focus: (index: number) => void;
|
|
90
|
+
};
|
|
91
|
+
/** NOTE: the part of a viewer its controls need, which a viewer has, and which a test or a caller's own viewer can have too. */
|
|
92
|
+
type ControlledViewer = WatchableViewer & Pick<Viewer, "zoomIn" | "zoomOut" | "zoomTo" | "fitWidth" | "fitPage" | "rotateLeft" | "rotateRight" | "setSpread" | "goToPage">;
|
|
93
|
+
/**
|
|
94
|
+
* NOTE: what a bar over a viewer needs, in one object: the viewer's zoom, what its zoom is held to, how far its
|
|
95
|
+
* pages are turned, how many stand side by side, the page in view and how many pages there are, and the commands that change them. A bar of your own shows the first and binds
|
|
96
|
+
* its buttons to the second, and holds nothing itself.
|
|
97
|
+
* NOTE: with no viewer yet, the state is at rest and the commands do nothing.
|
|
98
|
+
* NOTE: the commands keep their identity while the state changes, and take nothing a button's press would hand
|
|
99
|
+
* them, so one can be given to a button as it is.
|
|
100
|
+
*/
|
|
101
|
+
export declare function useViewerControls(viewer: ControlledViewer | null): {
|
|
102
|
+
zoom: number;
|
|
103
|
+
fit: import("../viewer/index.js").ViewerFit | null;
|
|
104
|
+
rotation: import("../viewer/index.js").ViewerRotation;
|
|
105
|
+
spread: ViewerSpread;
|
|
106
|
+
page: number;
|
|
107
|
+
pageCount: number;
|
|
108
|
+
canZoomIn: boolean;
|
|
109
|
+
canZoomOut: boolean;
|
|
110
|
+
zoomIn: () => void | undefined;
|
|
111
|
+
zoomOut: () => void | undefined;
|
|
112
|
+
zoomTo: (zoom: number) => void | undefined;
|
|
113
|
+
fitWidth: () => void | undefined;
|
|
114
|
+
fitPage: () => void | undefined;
|
|
115
|
+
rotateLeft: () => void | undefined;
|
|
116
|
+
rotateRight: () => void | undefined;
|
|
117
|
+
setSpread: (spread: ViewerSpread) => void | undefined;
|
|
118
|
+
goToPage: (page: number) => void | undefined;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* NOTE: keeps viewers side by side in step: a zoom in one is the same zoom in the others, and a reader's scroll in
|
|
122
|
+
* one moves the others as far, each keeping its own place. `scroll: false` links the zoom alone.
|
|
123
|
+
* CONSTRAINT: the list holds the same number of viewers on every render; one that is not built yet is null.
|
|
124
|
+
*/
|
|
125
|
+
export declare function useLinkedViewers(viewers: ReadonlyArray<Viewer | null>, options?: {
|
|
126
|
+
scroll?: boolean;
|
|
127
|
+
}): void;
|
|
128
|
+
export {};
|