@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,27 @@
|
|
|
1
|
+
import type { Box } from "../core/index.js";
|
|
2
|
+
import type { LocalPageRead } from "./worker-engine.js";
|
|
3
|
+
export type { LocalPageRead } from "./worker-engine.js";
|
|
4
|
+
/** NOTE: a PDF opened in this browser: its pages are drawn and its text layer read here, with no server involved. */
|
|
5
|
+
export type LocalPdf = {
|
|
6
|
+
pages: ReadonlyArray<{
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
}>;
|
|
10
|
+
/** NOTE: draws a page and gives the address of the picture; fits the viewer's `imageUrl`. */
|
|
11
|
+
imageUrl(page: number, scale: number, signal?: AbortSignal): Promise<string>;
|
|
12
|
+
/** NOTE: draws one box of a page, in page units, at exactly `scale`, and gives the address of the picture; fits the viewer's `tileUrl`. */
|
|
13
|
+
tileUrl(page: number, scale: number, box: Box, signal?: AbortSignal): Promise<string>;
|
|
14
|
+
/** NOTE: frees a picture `imageUrl` or `tileUrl` made; fits the viewer's `releaseImage`. */
|
|
15
|
+
releaseImage(url: string): void;
|
|
16
|
+
read(page: number): Promise<LocalPageRead>;
|
|
17
|
+
close(): void;
|
|
18
|
+
};
|
|
19
|
+
export type OpenLocalPdfOptions = {
|
|
20
|
+
/** NOTE: where the engine's WebAssembly file is served from, for a bundler that does not copy it from beside the engine's code. A browser caches it after the first document. */
|
|
21
|
+
wasmUrl?: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* NOTE: the file is handed to the engine's thread as a Blob, which costs no copy, and the engine reads it from there a
|
|
25
|
+
* piece at a time, so the file never sits whole in the engine's memory as well as in the Blob.
|
|
26
|
+
*/
|
|
27
|
+
export declare function openLocalPdf(file: Blob, options: OpenLocalPdfOptions): Promise<LocalPdf>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { remote, webPort } from "../engine/remote.js";
|
|
2
|
+
/**
|
|
3
|
+
* CONSTRAINT: one picture is held to this many pixels, since the engine needs several times that in memory to make
|
|
4
|
+
* it. A whole page past it comes back softer than asked; a viewer given `tileUrl` asks for the part on screen
|
|
5
|
+
* instead, which is small however far in it is zoomed.
|
|
6
|
+
*/
|
|
7
|
+
const MAX_PICTURE_PIXELS = 10_000_000;
|
|
8
|
+
/**
|
|
9
|
+
* NOTE: the file is handed to the engine's thread as a Blob, which costs no copy, and the engine reads it from there a
|
|
10
|
+
* piece at a time, so the file never sits whole in the engine's memory as well as in the Blob.
|
|
11
|
+
*/
|
|
12
|
+
export async function openLocalPdf(file, options) {
|
|
13
|
+
const worker = new Worker(new URL("./worker.js", import.meta.url), { type: "module" });
|
|
14
|
+
const engine = remote(webPort(worker));
|
|
15
|
+
/** NOTE: one job at a time, so a job whose page was dropped while it waited is never sent to the engine at all. */
|
|
16
|
+
let line = Promise.resolve();
|
|
17
|
+
const inTurn = (signal, run) => {
|
|
18
|
+
const turn = line.then(() => {
|
|
19
|
+
if (signal?.aborted)
|
|
20
|
+
throw new DOMException("The page is no longer wanted.", "AbortError");
|
|
21
|
+
return run();
|
|
22
|
+
});
|
|
23
|
+
line = turn.catch(() => undefined);
|
|
24
|
+
return turn;
|
|
25
|
+
};
|
|
26
|
+
/** NOTE: a file that cannot be opened leaves no thread behind it. */
|
|
27
|
+
const pages = await engine.call.open(file, options.wasmUrl ? new URL(options.wasmUrl, window.location.href).href : undefined).catch((error) => {
|
|
28
|
+
worker.terminate();
|
|
29
|
+
throw error;
|
|
30
|
+
});
|
|
31
|
+
const made = new Set();
|
|
32
|
+
const draw = (signal, page, scale, part) => inTurn(signal, async () => {
|
|
33
|
+
const url = URL.createObjectURL(await engine.call.draw(page, scale, part));
|
|
34
|
+
made.add(url);
|
|
35
|
+
return url;
|
|
36
|
+
});
|
|
37
|
+
return {
|
|
38
|
+
pages,
|
|
39
|
+
imageUrl: (page, scale, signal) => {
|
|
40
|
+
const size = pages[page];
|
|
41
|
+
const most = size ? Math.sqrt(MAX_PICTURE_PIXELS / (size.width * size.height)) : scale;
|
|
42
|
+
return draw(signal, page, Math.min(scale, most));
|
|
43
|
+
},
|
|
44
|
+
tileUrl: (page, scale, box, signal) => {
|
|
45
|
+
if (box.width * scale * box.height * scale > MAX_PICTURE_PIXELS)
|
|
46
|
+
return Promise.reject(new Error(`One part of a page is drawn up to ${MAX_PICTURE_PIXELS / 1_000_000} megapixels.`));
|
|
47
|
+
return draw(signal, page, scale, box);
|
|
48
|
+
},
|
|
49
|
+
releaseImage: (url) => {
|
|
50
|
+
if (made.delete(url))
|
|
51
|
+
URL.revokeObjectURL(url);
|
|
52
|
+
},
|
|
53
|
+
read: (page) => inTurn(undefined, () => engine.call.read(page)),
|
|
54
|
+
close: () => {
|
|
55
|
+
for (const url of made)
|
|
56
|
+
URL.revokeObjectURL(url);
|
|
57
|
+
made.clear();
|
|
58
|
+
worker.terminate();
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Box, PageReading } from "../core/index.js";
|
|
2
|
+
export type LocalPageRead = {
|
|
3
|
+
/** NOTE: the page's words from its own text layer, hidden words flagged. Not trusted, it has no words and `unread` says why. */
|
|
4
|
+
reading: PageReading;
|
|
5
|
+
/** NOTE: false when the page's own text is missing or garbled, so its words could only come from OCR, which a browser does not have. */
|
|
6
|
+
isTrusted: boolean;
|
|
7
|
+
reason?: string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* NOTE: what the worker serves, and what the page holds a remote of: the engine with one file, opened first. It
|
|
11
|
+
* answers in what a page can use at once: a picture ready to show, and a page's words already read and judged.
|
|
12
|
+
*/
|
|
13
|
+
export type WorkerEngine = {
|
|
14
|
+
/** NOTE: opens the file with the engine, fetched from `wasmUrl` when there is one, reading it a piece at a time, and gives every page's size. */
|
|
15
|
+
open(file: Blob, wasmUrl?: string): Promise<Array<{
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
}>>;
|
|
19
|
+
draw(page: number, scale: number, part?: Box): Promise<Blob>;
|
|
20
|
+
read(page: number): Promise<LocalPageRead>;
|
|
21
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { openPdf } from "../engine/index.js";
|
|
2
|
+
import { serve, webPort } from "../engine/remote.js";
|
|
3
|
+
import { readLayerPage } from "../reader/index.js";
|
|
4
|
+
import { blobPieces } from "./blob-pieces.js";
|
|
5
|
+
/**
|
|
6
|
+
* NOTE: PDFium's calls block until they finish, so the engine lives here, off the page's own thread, and the page stays
|
|
7
|
+
* responsive while a page is drawn.
|
|
8
|
+
*/
|
|
9
|
+
let pdf = null;
|
|
10
|
+
const opened = () => {
|
|
11
|
+
if (!pdf)
|
|
12
|
+
throw new Error("No document is open.");
|
|
13
|
+
return pdf;
|
|
14
|
+
};
|
|
15
|
+
function paint(raster) {
|
|
16
|
+
const canvas = new OffscreenCanvas(raster.width, raster.height);
|
|
17
|
+
const pixels = new ImageData(new Uint8ClampedArray(raster.rgba.buffer, raster.rgba.byteOffset, raster.rgba.byteLength), raster.width, raster.height);
|
|
18
|
+
canvas.getContext("2d")?.putImageData(pixels, 0, 0);
|
|
19
|
+
return canvas;
|
|
20
|
+
}
|
|
21
|
+
function round(value) {
|
|
22
|
+
return Math.round(value * 100) / 100;
|
|
23
|
+
}
|
|
24
|
+
const engine = {
|
|
25
|
+
async open(file, wasmUrl) {
|
|
26
|
+
pdf = await openPdf(blobPieces(file), wasmUrl ? { wasmBinary: await (await fetch(wasmUrl)).arrayBuffer() } : {});
|
|
27
|
+
const pages = [];
|
|
28
|
+
for (let index = 0; index < pdf.pageCount; index += 1) {
|
|
29
|
+
const size = await pdf.pageSize(index);
|
|
30
|
+
pages.push({ width: round(size.width), height: round(size.height) });
|
|
31
|
+
}
|
|
32
|
+
return pages;
|
|
33
|
+
},
|
|
34
|
+
draw(page, scale, part) {
|
|
35
|
+
/**
|
|
36
|
+
* NOTE: painted from the engine's own memory, which lends the page, so no copy of its pixels is left for the worker
|
|
37
|
+
* to collect: a busy worker puts that off, and a reader zooming or scrolling would leave dozens waiting.
|
|
38
|
+
*/
|
|
39
|
+
const canvas = opened().withDrawnPage(page, scale, part, paint);
|
|
40
|
+
/** NOTE: a browser that cannot write WebP, as Safari, writes PNG, which for a page of text is smaller than JPEG and loses nothing. */
|
|
41
|
+
return canvas.convertToBlob({ type: "image/webp", quality: 0.9 });
|
|
42
|
+
},
|
|
43
|
+
async read(page) {
|
|
44
|
+
const read = await readLayerPage(opened(), page);
|
|
45
|
+
if (read.isTrusted)
|
|
46
|
+
return { reading: read.reading, isTrusted: true };
|
|
47
|
+
const links = read.links.length > 0 ? { links: read.links } : {};
|
|
48
|
+
return { reading: { ...read.size, source: "layer", unread: read.reason, ...links, text: "", words: [], lines: [] }, isTrusted: false, reason: read.reason };
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
serve(webPort(self), engine);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type Paragraph } from "./paragraphs.js";
|
|
2
|
+
import type { Box, PageReading } from "./types.js";
|
|
3
|
+
/** NOTE: a run of words on one page, with one box per line it runs over. */
|
|
4
|
+
export type Span = {
|
|
5
|
+
page: number;
|
|
6
|
+
firstWord: number;
|
|
7
|
+
lastWord: number;
|
|
8
|
+
rects: Box[];
|
|
9
|
+
text: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* NOTE: one difference between two versions of a document. `added` has no `before`, `removed` has no `after`, and
|
|
13
|
+
* `changed` has both: the words that were replaced and the words that replaced them. `moved` has both too, and they
|
|
14
|
+
* are the same words: taken out of one place and put in another.
|
|
15
|
+
*/
|
|
16
|
+
export type Change = {
|
|
17
|
+
kind: "added" | "removed" | "changed" | "moved";
|
|
18
|
+
before: Span[];
|
|
19
|
+
after: Span[];
|
|
20
|
+
beforeText: string;
|
|
21
|
+
afterText: string;
|
|
22
|
+
};
|
|
23
|
+
/** NOTE: one side of a difference told by paragraph: the paragraphs it is about, their whole text, and the words inside them that differ. */
|
|
24
|
+
export type ParagraphSide = {
|
|
25
|
+
/** NOTE: where it is: each paragraph's page, its box and a box for each of its lines. None on a side that has no such paragraph. */
|
|
26
|
+
paragraphs: Paragraph[];
|
|
27
|
+
/** NOTE: the paragraphs' whole text, list markers left out, one paragraph to a line. */
|
|
28
|
+
text: string;
|
|
29
|
+
/** NOTE: the words that differ. None on a side where nothing inside the paragraph changed, as when words were only added on the other, or when it only moved. */
|
|
30
|
+
spans: Span[];
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* NOTE: a difference between two versions told by paragraph, so a sentence reworded in five places is one difference
|
|
34
|
+
* with its wording before and after, and not five. `moved` is a paragraph in another place with the same words; one
|
|
35
|
+
* that moved and changed is `changed`, with both places.
|
|
36
|
+
*/
|
|
37
|
+
export type ParagraphChange = {
|
|
38
|
+
kind: "added" | "removed" | "changed" | "moved";
|
|
39
|
+
before: ParagraphSide;
|
|
40
|
+
after: ParagraphSide;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* NOTE: what changed between two versions of a document, compared by their words in reading order: each run of
|
|
44
|
+
* words that was added, removed, replaced, or moved to another place.
|
|
45
|
+
*/
|
|
46
|
+
export declare function compareReadings(before: ReadonlyArray<PageReading>, after: ReadonlyArray<PageReading>): Change[];
|
|
47
|
+
/**
|
|
48
|
+
* NOTE: what changed between two versions, told by paragraph. A paragraph before and one after are taken for the same
|
|
49
|
+
* when a change replaces words of the one with words of the other, or when they keep enough words in common; every
|
|
50
|
+
* difference inside them is then one `ParagraphChange` carrying both wordings whole. A paragraph taken out in one
|
|
51
|
+
* place and put in another is one difference too: `moved` when its words are the same, `changed` when they are not.
|
|
52
|
+
* CONSTRAINT: paragraphs are found from the layout of each page (`paragraphs`), so a paragraph that runs onto the next
|
|
53
|
+
* page counts as two, and lines with nothing to tell them apart count as one.
|
|
54
|
+
*/
|
|
55
|
+
export declare function compareParagraphs(before: ReadonlyArray<PageReading>, after: ReadonlyArray<PageReading>): ParagraphChange[];
|
package/core/compare.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { wordRangeRects, wordRangeText } from "./locate.js";
|
|
2
|
+
import { paragraphs } from "./paragraphs.js";
|
|
3
|
+
/** NOTE: past this many edits the two sides are treated as wholly different, to bound the memory the search keeps. */
|
|
4
|
+
const MAX_EDITS = 4000;
|
|
5
|
+
/** NOTE: two changes with this few unchanged words between them read better as one. */
|
|
6
|
+
const MERGE_GAP = 1;
|
|
7
|
+
/** NOTE: the same words taken out and put in elsewhere are a move from this many words up; fewer are too easily a coincidence. */
|
|
8
|
+
const MOVED_WORDS = 3;
|
|
9
|
+
/**
|
|
10
|
+
* NOTE: a paragraph before and a paragraph after are the same paragraph when they keep this share of the shorter
|
|
11
|
+
* one's words in common, and at least this many, or every word of one too short for that.
|
|
12
|
+
*/
|
|
13
|
+
const SHARED_SHARE = 0.3;
|
|
14
|
+
const SHARED_WORDS = 2;
|
|
15
|
+
/** NOTE: a paragraph taken out and one put in elsewhere are one paragraph, moved and changed, when they keep this share of the longer one's words. */
|
|
16
|
+
const MOVED_SHARE = 0.6;
|
|
17
|
+
/** NOTE: the pairs of positions that hold the same item in both lists, in order: the longest common subsequence, by Myers' search. */
|
|
18
|
+
function commonPairs(a, b) {
|
|
19
|
+
const n = a.length;
|
|
20
|
+
const m = b.length;
|
|
21
|
+
const most = Math.min(n + m, MAX_EDITS);
|
|
22
|
+
const offset = most + 1;
|
|
23
|
+
const reach = new Int32Array(2 * most + 3);
|
|
24
|
+
const trace = [];
|
|
25
|
+
let edits = -1;
|
|
26
|
+
search: for (let d = 0; d <= most; d += 1) {
|
|
27
|
+
trace.push(reach.slice());
|
|
28
|
+
for (let k = -d; k <= d; k += 2) {
|
|
29
|
+
const goesDown = k === -d || (k !== d && (reach[offset + k - 1] ?? 0) < (reach[offset + k + 1] ?? 0));
|
|
30
|
+
let x = goesDown ? (reach[offset + k + 1] ?? 0) : (reach[offset + k - 1] ?? 0) + 1;
|
|
31
|
+
let y = x - k;
|
|
32
|
+
while (x < n && y < m && a[x] === b[y]) {
|
|
33
|
+
x += 1;
|
|
34
|
+
y += 1;
|
|
35
|
+
}
|
|
36
|
+
reach[offset + k] = x;
|
|
37
|
+
if (x >= n && y >= m) {
|
|
38
|
+
edits = d;
|
|
39
|
+
break search;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (edits === -1)
|
|
44
|
+
return [];
|
|
45
|
+
const pairs = [];
|
|
46
|
+
let x = n;
|
|
47
|
+
let y = m;
|
|
48
|
+
for (let d = edits; d > 0; d -= 1) {
|
|
49
|
+
const before = trace[d];
|
|
50
|
+
if (!before)
|
|
51
|
+
break;
|
|
52
|
+
const k = x - y;
|
|
53
|
+
const cameDown = k === -d || (k !== d && (before[offset + k - 1] ?? 0) < (before[offset + k + 1] ?? 0));
|
|
54
|
+
const previousK = cameDown ? k + 1 : k - 1;
|
|
55
|
+
const previousX = before[offset + previousK] ?? 0;
|
|
56
|
+
const previousY = previousX - previousK;
|
|
57
|
+
while (x > previousX && y > previousY) {
|
|
58
|
+
x -= 1;
|
|
59
|
+
y -= 1;
|
|
60
|
+
pairs.push([x, y]);
|
|
61
|
+
}
|
|
62
|
+
x = previousX;
|
|
63
|
+
y = previousY;
|
|
64
|
+
}
|
|
65
|
+
while (x > 0 && y > 0) {
|
|
66
|
+
x -= 1;
|
|
67
|
+
y -= 1;
|
|
68
|
+
pairs.push([x, y]);
|
|
69
|
+
}
|
|
70
|
+
return pairs.reverse();
|
|
71
|
+
}
|
|
72
|
+
function textLines(pages) {
|
|
73
|
+
return pages.flatMap((page) => page.lines.map((line) => {
|
|
74
|
+
const tokens = [];
|
|
75
|
+
for (let word = line.firstWord; word <= line.lastWord; word += 1)
|
|
76
|
+
tokens.push({ page: page.index, word, text: page.words[word]?.text ?? "" });
|
|
77
|
+
return { key: tokens.map((token) => token.text).join(" "), tokens };
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
function spans(pages, tokens) {
|
|
81
|
+
const found = [];
|
|
82
|
+
let run = null;
|
|
83
|
+
const flush = () => {
|
|
84
|
+
const page = run ? pages.find((entry) => entry.index === run?.page) : undefined;
|
|
85
|
+
if (run && page) {
|
|
86
|
+
found.push({
|
|
87
|
+
page: run.page,
|
|
88
|
+
firstWord: run.first,
|
|
89
|
+
lastWord: run.last,
|
|
90
|
+
rects: wordRangeRects(page, run.first, run.last),
|
|
91
|
+
text: wordRangeText(page, run.first, run.last),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
run = null;
|
|
95
|
+
};
|
|
96
|
+
for (const token of tokens) {
|
|
97
|
+
if (run && run.page === token.page && token.word === run.last + 1)
|
|
98
|
+
run.last = token.word;
|
|
99
|
+
else {
|
|
100
|
+
flush();
|
|
101
|
+
run = { page: token.page, first: token.word, last: token.word };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
flush();
|
|
105
|
+
return found;
|
|
106
|
+
}
|
|
107
|
+
function textOf(tokens) {
|
|
108
|
+
return tokens.map((token) => token.text).join(" ");
|
|
109
|
+
}
|
|
110
|
+
/** NOTE: the word-level differences inside one stretch of lines that differ, with near neighbours merged, and the words the two sides keep in common. */
|
|
111
|
+
function stretchChanges(before, after, common = []) {
|
|
112
|
+
const pairs = commonPairs(before.map((token) => token.text), after.map((token) => token.text));
|
|
113
|
+
for (const [x, y] of pairs) {
|
|
114
|
+
const [was, is] = [before[x], after[y]];
|
|
115
|
+
if (was && is)
|
|
116
|
+
common.push([was, is]);
|
|
117
|
+
}
|
|
118
|
+
const gaps = [];
|
|
119
|
+
let i = 0;
|
|
120
|
+
let j = 0;
|
|
121
|
+
let kept = 0;
|
|
122
|
+
for (const [x, y] of [...pairs, [before.length, after.length]]) {
|
|
123
|
+
if (x > i || y > j) {
|
|
124
|
+
const last = gaps[gaps.length - 1];
|
|
125
|
+
if (last && kept <= MERGE_GAP) {
|
|
126
|
+
last.before.push(...before.slice(i - kept, x));
|
|
127
|
+
last.after.push(...after.slice(j - kept, y));
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
gaps.push({ before: before.slice(i, x), after: after.slice(j, y), kept: 0 });
|
|
131
|
+
}
|
|
132
|
+
kept = 0;
|
|
133
|
+
}
|
|
134
|
+
kept += 1;
|
|
135
|
+
i = x + 1;
|
|
136
|
+
j = y + 1;
|
|
137
|
+
}
|
|
138
|
+
return gaps;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* NOTE: the two versions set against each other by their words in reading order: every stretch that differs, and
|
|
142
|
+
* every pair of words they keep. Lines that match exactly are skipped first, so a paragraph that only re-wrapped is
|
|
143
|
+
* compared word by word and shows just its real edits.
|
|
144
|
+
*/
|
|
145
|
+
function align(before, after) {
|
|
146
|
+
const a = textLines(before);
|
|
147
|
+
const b = textLines(after);
|
|
148
|
+
const pairs = commonPairs(a.map((line) => line.key), b.map((line) => line.key));
|
|
149
|
+
const gaps = [];
|
|
150
|
+
const kept = [];
|
|
151
|
+
let i = 0;
|
|
152
|
+
let j = 0;
|
|
153
|
+
for (const [x, y] of [...pairs, [a.length, b.length]]) {
|
|
154
|
+
if (x > i || y > j) {
|
|
155
|
+
const removed = a.slice(i, x).flatMap((line) => line.tokens);
|
|
156
|
+
const added = b.slice(j, y).flatMap((line) => line.tokens);
|
|
157
|
+
gaps.push(...stretchChanges(removed, added, kept));
|
|
158
|
+
}
|
|
159
|
+
const [was, is] = [a[x], b[y]];
|
|
160
|
+
was?.tokens.forEach((token, order) => {
|
|
161
|
+
const same = is?.tokens[order];
|
|
162
|
+
if (same)
|
|
163
|
+
kept.push([token, same]);
|
|
164
|
+
});
|
|
165
|
+
i = x + 1;
|
|
166
|
+
j = y + 1;
|
|
167
|
+
}
|
|
168
|
+
return { gaps, kept };
|
|
169
|
+
}
|
|
170
|
+
/** NOTE: the same words removed in one place and added in another are one change, a move, standing where the first of the two stood. */
|
|
171
|
+
function withMoves(changes) {
|
|
172
|
+
const merged = [...changes];
|
|
173
|
+
const wordCount = (text) => text.split(" ").length;
|
|
174
|
+
merged.forEach((change, index) => {
|
|
175
|
+
if (!change || (change.kind !== "removed" && change.kind !== "added"))
|
|
176
|
+
return;
|
|
177
|
+
const text = change.kind === "removed" ? change.beforeText : change.afterText;
|
|
178
|
+
if (wordCount(text) < MOVED_WORDS)
|
|
179
|
+
return;
|
|
180
|
+
const other = merged.findIndex((candidate, at) => at > index && candidate !== null && candidate.kind !== change.kind && (candidate.kind === "removed" ? candidate.beforeText : candidate.kind === "added" ? candidate.afterText : null) === text);
|
|
181
|
+
const pair = merged[other];
|
|
182
|
+
if (!pair)
|
|
183
|
+
return;
|
|
184
|
+
const [removed, added] = change.kind === "removed" ? [change, pair] : [pair, change];
|
|
185
|
+
merged[index] = { kind: "moved", before: removed.before, after: added.after, beforeText: text, afterText: text };
|
|
186
|
+
merged[other] = null;
|
|
187
|
+
});
|
|
188
|
+
return merged.filter((change) => change !== null);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* NOTE: what changed between two versions of a document, compared by their words in reading order: each run of
|
|
192
|
+
* words that was added, removed, replaced, or moved to another place.
|
|
193
|
+
*/
|
|
194
|
+
export function compareReadings(before, after) {
|
|
195
|
+
return withMoves(align(before, after).gaps.map((gap) => ({
|
|
196
|
+
kind: gap.before.length === 0 ? "added" : gap.after.length === 0 ? "removed" : "changed",
|
|
197
|
+
before: spans(before, gap.before),
|
|
198
|
+
after: spans(after, gap.after),
|
|
199
|
+
beforeText: textOf(gap.before),
|
|
200
|
+
afterText: textOf(gap.after),
|
|
201
|
+
})));
|
|
202
|
+
}
|
|
203
|
+
/** NOTE: one version's paragraphs, in reading order, and which of them each word belongs to. */
|
|
204
|
+
function paragraphIndex(pages) {
|
|
205
|
+
const all = [];
|
|
206
|
+
const byWord = new Map();
|
|
207
|
+
for (const page of pages) {
|
|
208
|
+
const owners = new Int32Array(page.words.length).fill(-1);
|
|
209
|
+
for (const paragraph of paragraphs(page)) {
|
|
210
|
+
owners.fill(all.length, paragraph.firstWord, paragraph.lastWord + 1);
|
|
211
|
+
all.push(paragraph);
|
|
212
|
+
}
|
|
213
|
+
byWord.set(page.index, owners);
|
|
214
|
+
}
|
|
215
|
+
const tokensOf = (paragraph) => {
|
|
216
|
+
const page = pages.find((entry) => entry.index === paragraph.page);
|
|
217
|
+
const tokens = [];
|
|
218
|
+
for (let word = paragraph.firstWord + (paragraph.marker === undefined ? 0 : 1); word <= paragraph.lastWord; word += 1) {
|
|
219
|
+
tokens.push({ page: paragraph.page, word, text: page?.words[word]?.text ?? "" });
|
|
220
|
+
}
|
|
221
|
+
return tokens;
|
|
222
|
+
};
|
|
223
|
+
return { all, tokensOf, of: (token) => byWord.get(token.page)?.[token.word] ?? -1 };
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* NOTE: what changed between two versions, told by paragraph. A paragraph before and one after are taken for the same
|
|
227
|
+
* when a change replaces words of the one with words of the other, or when they keep enough words in common; every
|
|
228
|
+
* difference inside them is then one `ParagraphChange` carrying both wordings whole. A paragraph taken out in one
|
|
229
|
+
* place and put in another is one difference too: `moved` when its words are the same, `changed` when they are not.
|
|
230
|
+
* CONSTRAINT: paragraphs are found from the layout of each page (`paragraphs`), so a paragraph that runs onto the next
|
|
231
|
+
* page counts as two, and lines with nothing to tell them apart count as one.
|
|
232
|
+
*/
|
|
233
|
+
export function compareParagraphs(before, after) {
|
|
234
|
+
const { gaps, kept } = align(before, after);
|
|
235
|
+
const was = paragraphIndex(before);
|
|
236
|
+
const is = paragraphIndex(after);
|
|
237
|
+
const group = Array.from({ length: was.all.length + is.all.length }, (_, index) => index);
|
|
238
|
+
const groupOf = (node) => {
|
|
239
|
+
let root = node;
|
|
240
|
+
while (group[root] !== root)
|
|
241
|
+
root = group[root] ?? root;
|
|
242
|
+
group[node] = root;
|
|
243
|
+
return root;
|
|
244
|
+
};
|
|
245
|
+
const join = (earlier, later) => {
|
|
246
|
+
group[groupOf(was.all.length + later)] = groupOf(earlier);
|
|
247
|
+
};
|
|
248
|
+
const shared = new Map();
|
|
249
|
+
for (const [earlier, later] of kept) {
|
|
250
|
+
const [from, to] = [was.of(earlier), is.of(later)];
|
|
251
|
+
if (from < 0 || to < 0)
|
|
252
|
+
continue;
|
|
253
|
+
const counts = shared.get(from) ?? new Map();
|
|
254
|
+
counts.set(to, (counts.get(to) ?? 0) + 1);
|
|
255
|
+
shared.set(from, counts);
|
|
256
|
+
}
|
|
257
|
+
const wordCount = (paragraph) => (paragraph ? paragraph.lastWord - paragraph.firstWord + (paragraph.marker === undefined ? 1 : 0) : 0);
|
|
258
|
+
for (const [from, counts] of shared) {
|
|
259
|
+
for (const [to, count] of counts) {
|
|
260
|
+
const shorter = Math.min(wordCount(was.all[from]), wordCount(is.all[to]));
|
|
261
|
+
if (count >= Math.max(Math.min(SHARED_WORDS, shorter), shorter * SHARED_SHARE))
|
|
262
|
+
join(from, to);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const touched = { before: new Set(), after: new Set() };
|
|
266
|
+
for (const gap of gaps) {
|
|
267
|
+
const from = [...new Set(gap.before.map(was.of))].filter((paragraph) => paragraph >= 0);
|
|
268
|
+
const to = [...new Set(gap.after.map(is.of))].filter((paragraph) => paragraph >= 0);
|
|
269
|
+
for (const paragraph of from)
|
|
270
|
+
touched.before.add(paragraph);
|
|
271
|
+
for (const paragraph of to)
|
|
272
|
+
touched.after.add(paragraph);
|
|
273
|
+
if (from.length === 1 && to.length === 1 && from[0] !== undefined && to[0] !== undefined)
|
|
274
|
+
join(from[0], to[0]);
|
|
275
|
+
}
|
|
276
|
+
/** NOTE: rows come in the order their first difference does, which is reading order. */
|
|
277
|
+
const rows = new Map();
|
|
278
|
+
const rowOf = (node) => {
|
|
279
|
+
const root = groupOf(node);
|
|
280
|
+
const known = rows.get(root);
|
|
281
|
+
if (known)
|
|
282
|
+
return known;
|
|
283
|
+
const row = { before: [], after: [], beforeTokens: [], afterTokens: [] };
|
|
284
|
+
was.all.forEach((_, paragraph) => {
|
|
285
|
+
if (groupOf(paragraph) === root)
|
|
286
|
+
row.before.push(paragraph);
|
|
287
|
+
});
|
|
288
|
+
is.all.forEach((_, paragraph) => {
|
|
289
|
+
if (groupOf(was.all.length + paragraph) === root)
|
|
290
|
+
row.after.push(paragraph);
|
|
291
|
+
});
|
|
292
|
+
rows.set(root, row);
|
|
293
|
+
return row;
|
|
294
|
+
};
|
|
295
|
+
/** NOTE: a list marker is not a word of its paragraph, so it is never among the words that differ. */
|
|
296
|
+
const isMarker = (index, token) => {
|
|
297
|
+
const paragraph = index.all[index.of(token)];
|
|
298
|
+
return paragraph?.marker !== undefined && paragraph.firstWord === token.word;
|
|
299
|
+
};
|
|
300
|
+
for (const gap of gaps) {
|
|
301
|
+
for (const token of gap.before)
|
|
302
|
+
if (was.of(token) >= 0 && !isMarker(was, token))
|
|
303
|
+
rowOf(was.of(token)).beforeTokens.push(token);
|
|
304
|
+
for (const token of gap.after)
|
|
305
|
+
if (is.of(token) >= 0 && !isMarker(is, token))
|
|
306
|
+
rowOf(was.all.length + is.of(token)).afterTokens.push(token);
|
|
307
|
+
}
|
|
308
|
+
const sideOf = (index, pages, members, tokens) => {
|
|
309
|
+
const own = members.flatMap((paragraph) => index.all[paragraph] ?? []);
|
|
310
|
+
return { paragraphs: own, text: own.map((paragraph) => paragraph.text).join("\n"), spans: spans(pages, tokens) };
|
|
311
|
+
};
|
|
312
|
+
const told = [...rows.values()].map((row) => ({
|
|
313
|
+
kind: row.before.length === 0 ? "added" : row.after.length === 0 ? "removed" : "changed",
|
|
314
|
+
before: sideOf(was, before, row.before, row.beforeTokens),
|
|
315
|
+
after: sideOf(is, after, row.after, row.afterTokens),
|
|
316
|
+
}));
|
|
317
|
+
/** NOTE: a paragraph taken out and one put in are set against each other; the closest pair that keeps enough words is one paragraph that moved. */
|
|
318
|
+
const wordsOf = (index, side) => side.paragraphs.flatMap(index.tokensOf);
|
|
319
|
+
told.forEach((change, at) => {
|
|
320
|
+
if (!change || change.kind !== "removed")
|
|
321
|
+
return;
|
|
322
|
+
const taken = wordsOf(was, change.before);
|
|
323
|
+
let best = -1;
|
|
324
|
+
let most = 0;
|
|
325
|
+
told.forEach((candidate, other) => {
|
|
326
|
+
if (!candidate || candidate.kind !== "added")
|
|
327
|
+
return;
|
|
328
|
+
const put = wordsOf(is, candidate.after);
|
|
329
|
+
const same = commonPairs(taken.map((token) => token.text), put.map((token) => token.text)).length;
|
|
330
|
+
const share = same / Math.max(taken.length, put.length, 1);
|
|
331
|
+
if (share >= MOVED_SHARE && share > most) {
|
|
332
|
+
most = share;
|
|
333
|
+
best = other;
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
const pair = told[best];
|
|
337
|
+
if (!pair)
|
|
338
|
+
return;
|
|
339
|
+
const put = wordsOf(is, pair.after);
|
|
340
|
+
const inside = stretchChanges(taken, put);
|
|
341
|
+
const first = Math.min(at, best);
|
|
342
|
+
told[first] = {
|
|
343
|
+
kind: inside.length === 0 ? "moved" : "changed",
|
|
344
|
+
before: { ...change.before, spans: spans(before, inside.flatMap((gap) => gap.before)) },
|
|
345
|
+
after: { ...pair.after, spans: spans(after, inside.flatMap((gap) => gap.after)) },
|
|
346
|
+
};
|
|
347
|
+
told[Math.max(at, best)] = null;
|
|
348
|
+
});
|
|
349
|
+
return told.filter((change) => change !== null);
|
|
350
|
+
}
|
package/core/find.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Match, MatchPart, PageReading } from "./types.js";
|
|
2
|
+
/** NOTE: case, accents, punctuation and spacing folded away, so only letters and digits are compared. */
|
|
3
|
+
export declare function fold(text: string): string;
|
|
4
|
+
export declare function findQuoteOnPage(page: PageReading, quote: string): Match[];
|
|
5
|
+
/**
|
|
6
|
+
* NOTE: every occurrence in reading order. A quote that runs from one page onto the next is found too: first as the
|
|
7
|
+
* two pages stand, then with what stands between their own texts left out on both pages, then on either one, which is
|
|
8
|
+
* how a line printed at the foot of one page and again at the head of the next is read once.
|
|
9
|
+
*/
|
|
10
|
+
export declare function findQuote(pages: ReadonlyArray<PageReading>, quote: string): Match[];
|
|
11
|
+
/** NOTE: a match's parts, page by page: one, or two when it runs over a page break. */
|
|
12
|
+
export declare function matchParts(match: Match): MatchPart[];
|