@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
package/core/types.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: every position is in page units from the top-left corner of the upright page, as a reader sees it. A PDF page's
|
|
3
|
+
* unit is the point (1/72 inch); a picture's unit is its own pixel.
|
|
4
|
+
*/
|
|
5
|
+
export type Box = {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
};
|
|
11
|
+
export type PageSource = "layer" | "ocr";
|
|
12
|
+
export type Word = {
|
|
13
|
+
text: string;
|
|
14
|
+
box: Box;
|
|
15
|
+
/** NOTE: the x of every character boundary, one more than the word has characters, so part of a word can be boxed. */
|
|
16
|
+
edges?: number[];
|
|
17
|
+
line: number;
|
|
18
|
+
/** NOTE: where the word sits in the page's `text`. */
|
|
19
|
+
start: number;
|
|
20
|
+
end: number;
|
|
21
|
+
/** NOTE: how sure OCR was of the word, from 0 to 100; absent for a word taken from the text layer. */
|
|
22
|
+
confidence?: number;
|
|
23
|
+
/**
|
|
24
|
+
* NOTE: why a reader cannot see the word though the file carries it: "unseen" when the page shows nothing where it
|
|
25
|
+
* sits, "tiny" when it is too small to read. Absent for a word a reader can see.
|
|
26
|
+
*/
|
|
27
|
+
hidden?: "unseen" | "tiny";
|
|
28
|
+
};
|
|
29
|
+
export type Line = {
|
|
30
|
+
box: Box;
|
|
31
|
+
firstWord: number;
|
|
32
|
+
lastWord: number;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* NOTE: a link a PDF lays over a page: where it lies, and where it leads. `url` is an address a browser may follow
|
|
36
|
+
* safely, as `safeLinkUrl` decides; `page` is another page of the same document, counted from 0.
|
|
37
|
+
*/
|
|
38
|
+
export type PageLink = {
|
|
39
|
+
box: Box;
|
|
40
|
+
url: string;
|
|
41
|
+
} | {
|
|
42
|
+
box: Box;
|
|
43
|
+
page: number;
|
|
44
|
+
};
|
|
45
|
+
export type PageReading = {
|
|
46
|
+
index: number;
|
|
47
|
+
width: number;
|
|
48
|
+
height: number;
|
|
49
|
+
source: PageSource;
|
|
50
|
+
/** NOTE: what is worth knowing about where the words came from, in words a reader can be shown: why OCR read the page, or that its text layer is an earlier OCR's. */
|
|
51
|
+
sourceReason?: string;
|
|
52
|
+
/** NOTE: how many of the page's words a reader cannot see; absent when they can see every one. */
|
|
53
|
+
hiddenWords?: number;
|
|
54
|
+
/**
|
|
55
|
+
* NOTE: why the page has no words though it may well show some: whoever read it could not, as a browser cannot read
|
|
56
|
+
* a scan. Absent for a page that was read, including one that is simply blank.
|
|
57
|
+
*/
|
|
58
|
+
unread?: string;
|
|
59
|
+
/** NOTE: the links the file lays over the page; absent when it has none. */
|
|
60
|
+
links?: PageLink[];
|
|
61
|
+
/** NOTE: the page's words joined by spaces and its lines by line breaks. */
|
|
62
|
+
text: string;
|
|
63
|
+
words: Word[];
|
|
64
|
+
lines: Line[];
|
|
65
|
+
};
|
|
66
|
+
export type ReadingStatus = "reading" | "ready" | "failed";
|
|
67
|
+
export type DocumentSummary = {
|
|
68
|
+
id: string;
|
|
69
|
+
name: string;
|
|
70
|
+
kind: "pdf" | "image";
|
|
71
|
+
status: ReadingStatus;
|
|
72
|
+
error?: string;
|
|
73
|
+
pageCount: number;
|
|
74
|
+
pagesRead: number;
|
|
75
|
+
};
|
|
76
|
+
export type Reading = DocumentSummary & {
|
|
77
|
+
pages: PageReading[];
|
|
78
|
+
};
|
|
79
|
+
/** NOTE: the part of a match that lies on one page: its words and one box per line it runs over. */
|
|
80
|
+
export type MatchPart = {
|
|
81
|
+
page: number;
|
|
82
|
+
firstWord: number;
|
|
83
|
+
lastWord: number;
|
|
84
|
+
rects: Box[];
|
|
85
|
+
};
|
|
86
|
+
/** NOTE: one place a quote occurs. It starts on `page`; when it runs over a page break, `continued` is the rest of it, on the page after. */
|
|
87
|
+
export type Match = MatchPart & {
|
|
88
|
+
continued?: MatchPart;
|
|
89
|
+
};
|
package/core/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type DocumentSummary, type Match, type PageReading } from "../core/index.js";
|
|
2
|
+
import type { DocumentSource } from "./source.js";
|
|
3
|
+
import { type Readable } from "./store.js";
|
|
4
|
+
/** NOTE: a page's size in page units. `sharpest` is the most pixels for each unit the page has to show: 1 for a photograph, whose units are its own pixels. */
|
|
5
|
+
export type PageSize = {
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
sharpest?: number;
|
|
9
|
+
};
|
|
10
|
+
export type DocumentState = {
|
|
11
|
+
/** NOTE: `opening` until the source has answered once, `reading` while it is still reading the file, then `ready` or `failed`. */
|
|
12
|
+
status: "opening" | "reading" | "ready" | "failed";
|
|
13
|
+
/** NOTE: why the document failed, or why some of its words could not be fetched. */
|
|
14
|
+
error: string | null;
|
|
15
|
+
/** NOTE: the file's id, name and kind, and how many pages it has and how many are read, once the source has answered. */
|
|
16
|
+
summary: DocumentSummary | null;
|
|
17
|
+
/** NOTE: every page's size, with one identity from the moment the document is ready, so a viewer is built once. */
|
|
18
|
+
sizes: ReadonlyArray<PageSize>;
|
|
19
|
+
/** NOTE: every page, with one identity until more words arrive. A page whose words have not arrived is there with none. */
|
|
20
|
+
pages: ReadonlyArray<PageReading>;
|
|
21
|
+
pagesLoaded: number;
|
|
22
|
+
/** NOTE: true once every page's words are here, so what is found in `pages` is found in the whole document. */
|
|
23
|
+
isComplete: boolean;
|
|
24
|
+
};
|
|
25
|
+
export type OpenDocumentOptions = {
|
|
26
|
+
/** NOTE: how many pages' words are fetched in one run; 10 unless given. */
|
|
27
|
+
batch?: number;
|
|
28
|
+
/** NOTE: how long to wait before asking a source that is still reading the file again; 300 ms unless given. */
|
|
29
|
+
pollMs?: number;
|
|
30
|
+
/** NOTE: fetch every page's words at once, for a caller that needs the whole document more than it needs the first pictures early, as a comparison does. */
|
|
31
|
+
all?: boolean;
|
|
32
|
+
};
|
|
33
|
+
/** NOTE: what a document's state is before its source has answered, and what a binding shows while it has no document. */
|
|
34
|
+
export declare const NOT_OPEN: DocumentState;
|
|
35
|
+
/**
|
|
36
|
+
* NOTE: an open document: the one thing an application holds for it. It owns its source, fetches the words as they
|
|
37
|
+
* are needed, answers questions about the whole document, and hands on the source's pictures. A viewer, a search and
|
|
38
|
+
* a comparison are all given this, so none of them is wired to another by hand.
|
|
39
|
+
* NOTE: words on demand. The pages' sizes arrive first, so the pages can be shown at once; the words of the first run
|
|
40
|
+
* follow. After that a page's words are fetched only when something needs them: the pages near the pane, told with
|
|
41
|
+
* `near`, or the whole document, for a search, a comparison or hidden words. A long document is never read further
|
|
42
|
+
* than it is used, and reading a page in a browser costs a drawing of it as well.
|
|
43
|
+
* INVARIANT: every member that is a function works when handed on by itself, as `document.pageImage`.
|
|
44
|
+
*/
|
|
45
|
+
export declare class PagemarkDocument implements Readable<DocumentState> {
|
|
46
|
+
#private;
|
|
47
|
+
/** NOTE: resolves once the pages' sizes are known. It rejects, with the reason, when the document fails or is closed first. */
|
|
48
|
+
readonly opened: Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* NOTE: resolves the first time the screen is said to be idle, the moment for work that should not compete with the
|
|
51
|
+
* first pictures, as fetching every page's words for hidden words does.
|
|
52
|
+
*/
|
|
53
|
+
readonly idle: Promise<void>;
|
|
54
|
+
/** NOTE: what the document reads from, for a caller that asks the source something of its own, as how far a handover has come. */
|
|
55
|
+
readonly source: DocumentSource;
|
|
56
|
+
constructor(source: DocumentSource, options?: OpenDocumentOptions);
|
|
57
|
+
readonly get: () => DocumentState;
|
|
58
|
+
readonly subscribe: (listener: () => void) => (() => void);
|
|
59
|
+
get status(): DocumentState["status"];
|
|
60
|
+
get sizes(): ReadonlyArray<PageSize>;
|
|
61
|
+
get pages(): ReadonlyArray<PageReading>;
|
|
62
|
+
get isComplete(): boolean;
|
|
63
|
+
/** NOTE: one page with its words, fetched with the run it is in when they are not here yet. */
|
|
64
|
+
readonly page: (index: number) => Promise<PageReading>;
|
|
65
|
+
/** NOTE: every page with its words, fetching whichever are not here yet. */
|
|
66
|
+
readonly whole: () => Promise<ReadonlyArray<PageReading>>;
|
|
67
|
+
/** NOTE: every place a quote occurs in the whole document, not only in the pages whose words are already here. */
|
|
68
|
+
readonly find: (quote: string) => Promise<Match[]>;
|
|
69
|
+
readonly pageImage: DocumentSource["pageImage"];
|
|
70
|
+
readonly tile: DocumentSource["tile"];
|
|
71
|
+
readonly release: DocumentSource["release"];
|
|
72
|
+
/** NOTE: says the pages on screen are sharp, the moment for whatever the source kept back so the first page came first. */
|
|
73
|
+
readonly whenIdle: () => void;
|
|
74
|
+
/**
|
|
75
|
+
* NOTE: says a reader is at a page, or near several, counted from 0, so the words of the pages around them are
|
|
76
|
+
* fetched, if they are not here, before the reader reaches them: half a run either side, in the runs they are in.
|
|
77
|
+
* It fits a viewer's `pagesNear` event as it is.
|
|
78
|
+
*/
|
|
79
|
+
readonly near: (pages: number | ReadonlyArray<number>) => void;
|
|
80
|
+
readonly close: () => void;
|
|
81
|
+
}
|
|
82
|
+
/** NOTE: opens the document a source gives. It comes back at once; `opened` and its state say how far it has come. */
|
|
83
|
+
export declare function openDocument(source: DocumentSource, options?: OpenDocumentOptions): PagemarkDocument;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { findQuote } from "../core/index.js";
|
|
2
|
+
import { writable } from "./store.js";
|
|
3
|
+
/** NOTE: what a document's state is before its source has answered, and what a binding shows while it has no document. */
|
|
4
|
+
export const NOT_OPEN = { status: "opening", error: null, summary: null, sizes: [], pages: [], pagesLoaded: 0, isComplete: false };
|
|
5
|
+
/**
|
|
6
|
+
* NOTE: an open document: the one thing an application holds for it. It owns its source, fetches the words as they
|
|
7
|
+
* are needed, answers questions about the whole document, and hands on the source's pictures. A viewer, a search and
|
|
8
|
+
* a comparison are all given this, so none of them is wired to another by hand.
|
|
9
|
+
* NOTE: words on demand. The pages' sizes arrive first, so the pages can be shown at once; the words of the first run
|
|
10
|
+
* follow. After that a page's words are fetched only when something needs them: the pages near the pane, told with
|
|
11
|
+
* `near`, or the whole document, for a search, a comparison or hidden words. A long document is never read further
|
|
12
|
+
* than it is used, and reading a page in a browser costs a drawing of it as well.
|
|
13
|
+
* INVARIANT: every member that is a function works when handed on by itself, as `document.pageImage`.
|
|
14
|
+
*/
|
|
15
|
+
export class PagemarkDocument {
|
|
16
|
+
/** NOTE: resolves once the pages' sizes are known. It rejects, with the reason, when the document fails or is closed first. */
|
|
17
|
+
opened;
|
|
18
|
+
/**
|
|
19
|
+
* NOTE: resolves the first time the screen is said to be idle, the moment for work that should not compete with the
|
|
20
|
+
* first pictures, as fetching every page's words for hidden words does.
|
|
21
|
+
*/
|
|
22
|
+
idle;
|
|
23
|
+
/** NOTE: what the document reads from, for a caller that asks the source something of its own, as how far a handover has come. */
|
|
24
|
+
source;
|
|
25
|
+
#batch;
|
|
26
|
+
#pollMs;
|
|
27
|
+
#state = writable(NOT_OPEN);
|
|
28
|
+
#loaded = new Map();
|
|
29
|
+
#runs = new Map();
|
|
30
|
+
#outline = null;
|
|
31
|
+
#summary = null;
|
|
32
|
+
#sizes = [];
|
|
33
|
+
#timer;
|
|
34
|
+
#rest = null;
|
|
35
|
+
#isClosed = false;
|
|
36
|
+
#settle = { resolve: () => undefined, reject: () => undefined };
|
|
37
|
+
#rested = () => undefined;
|
|
38
|
+
constructor(source, options = {}) {
|
|
39
|
+
this.source = source;
|
|
40
|
+
this.#batch = options.batch ?? 10;
|
|
41
|
+
this.#pollMs = options.pollMs ?? 300;
|
|
42
|
+
this.opened = new Promise((resolve, reject) => {
|
|
43
|
+
this.#settle = { resolve, reject };
|
|
44
|
+
});
|
|
45
|
+
/** NOTE: a caller that never waits for `opened` reads the failure from the state instead. */
|
|
46
|
+
this.opened.catch(() => undefined);
|
|
47
|
+
this.idle = new Promise((resolve) => {
|
|
48
|
+
this.#rested = resolve;
|
|
49
|
+
});
|
|
50
|
+
this.#poll(options.all ?? false).catch((problem) => this.#fail(problem));
|
|
51
|
+
}
|
|
52
|
+
get = () => this.#state.get();
|
|
53
|
+
subscribe = (listener) => this.#state.subscribe(listener);
|
|
54
|
+
get status() {
|
|
55
|
+
return this.#state.get().status;
|
|
56
|
+
}
|
|
57
|
+
get sizes() {
|
|
58
|
+
return this.#state.get().sizes;
|
|
59
|
+
}
|
|
60
|
+
get pages() {
|
|
61
|
+
return this.#state.get().pages;
|
|
62
|
+
}
|
|
63
|
+
get isComplete() {
|
|
64
|
+
return this.#state.get().isComplete;
|
|
65
|
+
}
|
|
66
|
+
/** NOTE: one page with its words, fetched with the run it is in when they are not here yet. */
|
|
67
|
+
page = async (index) => {
|
|
68
|
+
await this.opened;
|
|
69
|
+
const count = this.#outline?.pages.length ?? 0;
|
|
70
|
+
if (!Number.isInteger(index) || index < 0 || index >= count)
|
|
71
|
+
throw new RangeError(`This document has no page ${index + 1}.`);
|
|
72
|
+
if (!this.#loaded.has(index))
|
|
73
|
+
await this.#run(Math.floor(index / this.#batch) * this.#batch);
|
|
74
|
+
const page = this.#loaded.get(index);
|
|
75
|
+
if (!page)
|
|
76
|
+
throw new Error(`Page ${index + 1} did not arrive.`);
|
|
77
|
+
return page;
|
|
78
|
+
};
|
|
79
|
+
/** NOTE: every page with its words, fetching whichever are not here yet. */
|
|
80
|
+
whole = async () => {
|
|
81
|
+
await this.opened;
|
|
82
|
+
await this.#fetchRest();
|
|
83
|
+
this.#mustBeOpen();
|
|
84
|
+
return this.#state.get().pages;
|
|
85
|
+
};
|
|
86
|
+
/** NOTE: every place a quote occurs in the whole document, not only in the pages whose words are already here. */
|
|
87
|
+
find = async (quote) => findQuote(await this.whole(), quote);
|
|
88
|
+
pageImage = (page, scale, signal) => this.source.pageImage(page, scale, signal);
|
|
89
|
+
tile = (page, scale, box, signal) => this.source.tile(page, scale, box, signal);
|
|
90
|
+
release = (url) => this.source.release(url);
|
|
91
|
+
/** NOTE: says the pages on screen are sharp, the moment for whatever the source kept back so the first page came first. */
|
|
92
|
+
whenIdle = () => {
|
|
93
|
+
if (this.#isClosed)
|
|
94
|
+
return;
|
|
95
|
+
this.#rested();
|
|
96
|
+
this.source.whenIdle?.();
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* NOTE: says a reader is at a page, or near several, counted from 0, so the words of the pages around them are
|
|
100
|
+
* fetched, if they are not here, before the reader reaches them: half a run either side, in the runs they are in.
|
|
101
|
+
* It fits a viewer's `pagesNear` event as it is.
|
|
102
|
+
*/
|
|
103
|
+
near = (pages) => {
|
|
104
|
+
if (this.#isClosed)
|
|
105
|
+
return;
|
|
106
|
+
this.opened.then(() => {
|
|
107
|
+
const count = this.#outline?.pages.length ?? 0;
|
|
108
|
+
const reach = Math.ceil(this.#batch / 2);
|
|
109
|
+
const runs = new Set();
|
|
110
|
+
for (const page of typeof pages === "number" ? [pages] : pages) {
|
|
111
|
+
if (!Number.isInteger(page) || page < 0 || page >= count)
|
|
112
|
+
continue;
|
|
113
|
+
const first = Math.max(0, page - reach);
|
|
114
|
+
const last = Math.min(count - 1, page + reach);
|
|
115
|
+
for (let from = Math.floor(first / this.#batch) * this.#batch; from <= last; from += this.#batch)
|
|
116
|
+
runs.add(from);
|
|
117
|
+
}
|
|
118
|
+
for (const from of runs)
|
|
119
|
+
this.#run(from).catch(() => undefined);
|
|
120
|
+
}, () => undefined);
|
|
121
|
+
};
|
|
122
|
+
close = () => {
|
|
123
|
+
if (this.#isClosed)
|
|
124
|
+
return;
|
|
125
|
+
this.#isClosed = true;
|
|
126
|
+
clearTimeout(this.#timer);
|
|
127
|
+
this.#settle.reject(new Error("The document was closed."));
|
|
128
|
+
this.source.close();
|
|
129
|
+
};
|
|
130
|
+
#mustBeOpen() {
|
|
131
|
+
if (this.#isClosed)
|
|
132
|
+
throw new Error("The document was closed.");
|
|
133
|
+
}
|
|
134
|
+
#publish(change = {}) {
|
|
135
|
+
const outline = this.#outline;
|
|
136
|
+
const current = this.#state.get();
|
|
137
|
+
this.#state.set({
|
|
138
|
+
status: change.status ?? current.status,
|
|
139
|
+
error: change.error === undefined ? current.error : change.error,
|
|
140
|
+
summary: this.#summary,
|
|
141
|
+
sizes: this.#sizes,
|
|
142
|
+
pages: outline ? outline.pages.map((stub) => this.#loaded.get(stub.index) ?? stub) : [],
|
|
143
|
+
pagesLoaded: this.#loaded.size,
|
|
144
|
+
isComplete: outline?.status === "ready" && this.#loaded.size >= outline.pages.length,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
#fail(problem) {
|
|
148
|
+
if (this.#isClosed)
|
|
149
|
+
return;
|
|
150
|
+
const error = problem instanceof Error ? problem : new Error(String(problem));
|
|
151
|
+
this.#publish({ status: "failed", error: error.message });
|
|
152
|
+
this.#settle.reject(error);
|
|
153
|
+
}
|
|
154
|
+
async #poll(all) {
|
|
155
|
+
const next = await this.source.outline();
|
|
156
|
+
if (this.#isClosed)
|
|
157
|
+
return;
|
|
158
|
+
const { pages: _pages, ...summary } = next;
|
|
159
|
+
this.#outline = next;
|
|
160
|
+
this.#summary = summary;
|
|
161
|
+
if (next.status === "reading") {
|
|
162
|
+
this.#publish({ status: "reading" });
|
|
163
|
+
this.#timer = setTimeout(() => this.#poll(all).catch((problem) => this.#fail(problem)), this.#pollMs);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (next.status === "failed") {
|
|
167
|
+
this.#fail(new Error(next.error ?? "The document could not be read."));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
/** NOTE: a photograph's page units are its own pixels, so it has nothing to show past one pixel for each. */
|
|
171
|
+
const sharpness = next.kind === "image" ? { sharpest: 1 } : {};
|
|
172
|
+
this.#sizes = next.pages.map(({ width, height }) => ({ width, height, ...sharpness }));
|
|
173
|
+
this.#publish({ status: "ready" });
|
|
174
|
+
this.#settle.resolve();
|
|
175
|
+
if (all)
|
|
176
|
+
this.#fetchRest().catch(() => undefined);
|
|
177
|
+
else
|
|
178
|
+
this.#run(0).catch(() => undefined);
|
|
179
|
+
}
|
|
180
|
+
/** NOTE: one run of pages, asked for once however many are waiting for it. A run that failed may be asked for again. */
|
|
181
|
+
#run(from) {
|
|
182
|
+
const known = this.#runs.get(from);
|
|
183
|
+
if (known)
|
|
184
|
+
return known;
|
|
185
|
+
const run = this.source.pages(from, this.#batch).then((pages) => {
|
|
186
|
+
if (this.#isClosed)
|
|
187
|
+
return;
|
|
188
|
+
for (const page of pages)
|
|
189
|
+
this.#loaded.set(page.index, page);
|
|
190
|
+
this.#publish({ error: null });
|
|
191
|
+
}, (problem) => {
|
|
192
|
+
this.#runs.delete(from);
|
|
193
|
+
if (!this.#isClosed)
|
|
194
|
+
this.#publish({ error: problem instanceof Error ? problem.message : String(problem) });
|
|
195
|
+
throw problem;
|
|
196
|
+
});
|
|
197
|
+
this.#runs.set(from, run);
|
|
198
|
+
return run;
|
|
199
|
+
}
|
|
200
|
+
/** NOTE: the runs not here yet, one after another, so a long document never asks for everything at once. */
|
|
201
|
+
#fetchRest() {
|
|
202
|
+
this.#rest ??= (async () => {
|
|
203
|
+
const count = this.#outline?.pages.length ?? 0;
|
|
204
|
+
for (let from = 0; from < count && !this.#isClosed; from += this.#batch)
|
|
205
|
+
await this.#run(from);
|
|
206
|
+
})().finally(() => {
|
|
207
|
+
this.#rest = null;
|
|
208
|
+
});
|
|
209
|
+
return this.#rest;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/** NOTE: opens the document a source gives. It comes back at once; `opened` and its state say how far it has come. */
|
|
213
|
+
export function openDocument(source, options = {}) {
|
|
214
|
+
return new PagemarkDocument(source, options);
|
|
215
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { DocumentSource } from "./source.js";
|
|
2
|
+
import { type Readable } from "./store.js";
|
|
3
|
+
/** NOTE: how far the second source has come: not asked for yet, opening, drawing the pages, or failed, with the reason. */
|
|
4
|
+
export type Handover = {
|
|
5
|
+
second: "waiting" | "opening" | "ready" | "failed";
|
|
6
|
+
error: string | null;
|
|
7
|
+
};
|
|
8
|
+
export type HandoverSource = DocumentSource & {
|
|
9
|
+
readonly state: Readable<Handover>;
|
|
10
|
+
/** NOTE: starts opening the second source, so it never competes with the first page. */
|
|
11
|
+
whenIdle(): void;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* NOTE: draws from `first` until `second` has opened, then from `second`: a server shows the first page at once, and
|
|
15
|
+
* the reader's own browser takes over once its engine has arrived. The words always come from `first`, which may have
|
|
16
|
+
* read what `second` cannot, as a server reads a scan. Should `second` fail to open, `first` goes on drawing.
|
|
17
|
+
*/
|
|
18
|
+
export declare function handoverSource(first: DocumentSource, second: DocumentSource): HandoverSource;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { writable } from "./store.js";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: draws from `first` until `second` has opened, then from `second`: a server shows the first page at once, and
|
|
4
|
+
* the reader's own browser takes over once its engine has arrived. The words always come from `first`, which may have
|
|
5
|
+
* read what `second` cannot, as a server reads a scan. Should `second` fail to open, `first` goes on drawing.
|
|
6
|
+
*/
|
|
7
|
+
export function handoverSource(first, second) {
|
|
8
|
+
const state = writable({ second: "waiting", error: null });
|
|
9
|
+
let isClosed = false;
|
|
10
|
+
const drawing = () => (state.get().second === "ready" ? second : first);
|
|
11
|
+
return {
|
|
12
|
+
state,
|
|
13
|
+
whenIdle() {
|
|
14
|
+
if (isClosed || state.get().second !== "waiting")
|
|
15
|
+
return;
|
|
16
|
+
state.set({ second: "opening", error: null });
|
|
17
|
+
second.outline().then(() => {
|
|
18
|
+
if (!isClosed)
|
|
19
|
+
state.set({ second: "ready", error: null });
|
|
20
|
+
}, (problem) => {
|
|
21
|
+
if (!isClosed)
|
|
22
|
+
state.set({ second: "failed", error: problem instanceof Error ? problem.message : String(problem) });
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
outline: () => first.outline(),
|
|
26
|
+
pages: (from, count) => first.pages(from, count),
|
|
27
|
+
pageImage: (page, scale, signal) => drawing().pageImage(page, scale, signal),
|
|
28
|
+
tile: (page, scale, box, signal) => drawing().tile(page, scale, box, signal),
|
|
29
|
+
release(url) {
|
|
30
|
+
second.release(url);
|
|
31
|
+
first.release(url);
|
|
32
|
+
},
|
|
33
|
+
close() {
|
|
34
|
+
isClosed = true;
|
|
35
|
+
second.close();
|
|
36
|
+
first.close();
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { NOT_OPEN, openDocument, PagemarkDocument } from "./document.js";
|
|
2
|
+
export type { DocumentState, OpenDocumentOptions, PageSize } from "./document.js";
|
|
3
|
+
export { handoverSource } from "./handover-source.js";
|
|
4
|
+
export type { Handover, HandoverSource } from "./handover-source.js";
|
|
5
|
+
export { readingSource } from "./reading-source.js";
|
|
6
|
+
export { serverSource } from "./server-source.js";
|
|
7
|
+
export type { ServerSourceOptions } from "./server-source.js";
|
|
8
|
+
export type { DocumentSource } from "./source.js";
|
|
9
|
+
export { splitSource } from "./split-source.js";
|
|
10
|
+
export type { Readable } from "./store.js";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { NOT_OPEN, openDocument, PagemarkDocument } from "./document.js";
|
|
2
|
+
export { handoverSource } from "./handover-source.js";
|
|
3
|
+
export { readingSource } from "./reading-source.js";
|
|
4
|
+
export { serverSource } from "./server-source.js";
|
|
5
|
+
export { splitSource } from "./split-source.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Reading } from "../core/index.js";
|
|
2
|
+
import type { DocumentSource } from "./source.js";
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: a reading the caller already has, as a source: one its own backend made, or one made up for a test. It has
|
|
5
|
+
* the words and no pictures, so it is usually split with a source that draws, as `splitSource` does.
|
|
6
|
+
*/
|
|
7
|
+
export declare function readingSource(reading: Reading): DocumentSource;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const NO_PICTURES = "A reading has no pictures of its pages. Split it with a source that draws them.";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: a reading the caller already has, as a source: one its own backend made, or one made up for a test. It has
|
|
4
|
+
* the words and no pictures, so it is usually split with a source that draws, as `splitSource` does.
|
|
5
|
+
*/
|
|
6
|
+
export function readingSource(reading) {
|
|
7
|
+
const outline = { ...reading, pages: reading.pages.map((page) => ({ ...page, text: "", words: [], lines: [] })) };
|
|
8
|
+
return {
|
|
9
|
+
outline: async () => outline,
|
|
10
|
+
pages: async (from, count) => reading.pages.slice(from, from + count),
|
|
11
|
+
pageImage: () => Promise.reject(new Error(NO_PICTURES)),
|
|
12
|
+
tile: () => Promise.reject(new Error(NO_PICTURES)),
|
|
13
|
+
release: () => undefined,
|
|
14
|
+
close: () => undefined,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DocumentSource } from "./source.js";
|
|
2
|
+
export type ServerSourceOptions = {
|
|
3
|
+
/** NOTE: what fetches the words, for a caller whose requests carry something of their own, as a session does. The browser fetches the pictures by their address. */
|
|
4
|
+
fetch?: typeof fetch;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* NOTE: a document kept by a pagemark server, at the address the server gives it, such as `/api/documents/<id>`. The
|
|
8
|
+
* server has read it, scans included, and draws its pages, so nothing is fetched to name a picture.
|
|
9
|
+
*/
|
|
10
|
+
export declare function serverSource(url: string, options?: ServerSourceOptions): DocumentSource;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: a document kept by a pagemark server, at the address the server gives it, such as `/api/documents/<id>`. The
|
|
3
|
+
* server has read it, scans included, and draws its pages, so nothing is fetched to name a picture.
|
|
4
|
+
*/
|
|
5
|
+
export function serverSource(url, options = {}) {
|
|
6
|
+
const base = url.replace(/\/+$/, "");
|
|
7
|
+
const ask = async (address) => {
|
|
8
|
+
const response = await (options.fetch ?? fetch)(address);
|
|
9
|
+
const body = (await response.json());
|
|
10
|
+
if (!response.ok)
|
|
11
|
+
throw new Error(body.error ?? `Request failed (${response.status}).`);
|
|
12
|
+
return body;
|
|
13
|
+
};
|
|
14
|
+
const picture = (page, scale) => `${base}/pages/${page}/image?scale=${scale}`;
|
|
15
|
+
return {
|
|
16
|
+
outline: () => ask(base),
|
|
17
|
+
pages: (from, count) => ask(`${base}/pages?from=${from}&count=${count}`),
|
|
18
|
+
pageImage: picture,
|
|
19
|
+
tile: (page, scale, box) => `${picture(page, scale)}&x=${box.x}&y=${box.y}&width=${box.width}&height=${box.height}`,
|
|
20
|
+
release: () => undefined,
|
|
21
|
+
close: () => undefined,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Box, PageReading, Reading } from "../core/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: where an open document's words and pictures come from. A server, the reader's own browser, and a mix of the
|
|
4
|
+
* two are each one of these, so whatever shows or searches a document never knows which it was given.
|
|
5
|
+
* INVARIANT: every member works when handed on by itself, as `source.pageImage`, since a viewer takes them one by one.
|
|
6
|
+
*/
|
|
7
|
+
export type DocumentSource = {
|
|
8
|
+
/** NOTE: the document's state and every page's size, with no words: all that is needed to lay the pages out. */
|
|
9
|
+
outline(): Promise<Reading>;
|
|
10
|
+
/** NOTE: the readings of a run of pages, words included. */
|
|
11
|
+
pages(from: number, count: number): Promise<PageReading[]>;
|
|
12
|
+
/** NOTE: the address of a picture of a page drawn at `scale` pixels per page unit. It may be known at once or only once drawn. */
|
|
13
|
+
pageImage(page: number, scale: number, signal?: AbortSignal): string | Promise<string>;
|
|
14
|
+
/** NOTE: the address of a picture of one box of a page, in page units, drawn at exactly `scale`. */
|
|
15
|
+
tile(page: number, scale: number, box: Box, signal?: AbortSignal): string | Promise<string>;
|
|
16
|
+
/** NOTE: frees a picture this source made. An address it did not make is ignored, so a mix of sources can tell each of them. */
|
|
17
|
+
release(url: string): void;
|
|
18
|
+
/** NOTE: told once the pages on screen are sharp. A source that keeps something back so the first page comes first starts it now; most have nothing to do. */
|
|
19
|
+
whenIdle?(): void;
|
|
20
|
+
close(): void;
|
|
21
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { DocumentSource } from "./source.js";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: a document whose words come from one source and whose pictures from another: a server that has read a scan,
|
|
4
|
+
* say, with the reader's own browser drawing the pages.
|
|
5
|
+
*/
|
|
6
|
+
export declare function splitSource(parts: {
|
|
7
|
+
words: DocumentSource;
|
|
8
|
+
pictures: DocumentSource;
|
|
9
|
+
}): DocumentSource;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: a document whose words come from one source and whose pictures from another: a server that has read a scan,
|
|
3
|
+
* say, with the reader's own browser drawing the pages.
|
|
4
|
+
*/
|
|
5
|
+
export function splitSource(parts) {
|
|
6
|
+
const { words, pictures } = parts;
|
|
7
|
+
return {
|
|
8
|
+
outline: () => words.outline(),
|
|
9
|
+
pages: (from, count) => words.pages(from, count),
|
|
10
|
+
pageImage: (page, scale, signal) => pictures.pageImage(page, scale, signal),
|
|
11
|
+
tile: (page, scale, box, signal) => pictures.tile(page, scale, box, signal),
|
|
12
|
+
release(url) {
|
|
13
|
+
pictures.release(url);
|
|
14
|
+
words.release(url);
|
|
15
|
+
},
|
|
16
|
+
whenIdle() {
|
|
17
|
+
pictures.whenIdle?.();
|
|
18
|
+
words.whenIdle?.();
|
|
19
|
+
},
|
|
20
|
+
close() {
|
|
21
|
+
pictures.close();
|
|
22
|
+
words.close();
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: the smallest thing a framework can follow: a value that can be read now and listened to for changes. A
|
|
3
|
+
* behaviour that changes over time is one of these, so React, Vue or plain code can all bind to it the same way.
|
|
4
|
+
*/
|
|
5
|
+
export type Readable<T> = {
|
|
6
|
+
get(): T;
|
|
7
|
+
subscribe(listener: () => void): () => void;
|
|
8
|
+
};
|
|
9
|
+
export type Writable<T> = Readable<T> & {
|
|
10
|
+
set(next: T | ((current: T) => T)): void;
|
|
11
|
+
};
|
|
12
|
+
export declare function writable<T>(initial: T): Writable<T>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function writable(initial) {
|
|
2
|
+
let value = initial;
|
|
3
|
+
const listeners = new Set();
|
|
4
|
+
return {
|
|
5
|
+
get: () => value,
|
|
6
|
+
subscribe(listener) {
|
|
7
|
+
listeners.add(listener);
|
|
8
|
+
return () => {
|
|
9
|
+
listeners.delete(listener);
|
|
10
|
+
};
|
|
11
|
+
},
|
|
12
|
+
set(next) {
|
|
13
|
+
const updated = typeof next === "function" ? next(value) : next;
|
|
14
|
+
if (Object.is(updated, value))
|
|
15
|
+
return;
|
|
16
|
+
value = updated;
|
|
17
|
+
for (const listener of [...listeners])
|
|
18
|
+
listener();
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { OpenPdf } from "../reader/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: what the engine's thread serves, and what the caller's thread holds a remote of: one file, opened first and
|
|
4
|
+
* then asked page by page. It is the engine's own `OpenPdf` with the opening put in front of it.
|
|
5
|
+
*/
|
|
6
|
+
export type EngineThread = Pick<OpenPdf, "pageSize" | "readPage" | "drawPage"> & {
|
|
7
|
+
open(data: Uint8Array): Promise<{
|
|
8
|
+
pageCount: number;
|
|
9
|
+
}>;
|
|
10
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/engine/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { openPdf } from "./pdfium.js";
|