@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/find.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { rangeRects } from "./locate.js";
|
|
2
|
+
const COMBINING_MARKS = /\p{M}/gu;
|
|
3
|
+
const NOT_LETTER_OR_DIGIT = /[^\p{L}\p{N}]/gu;
|
|
4
|
+
/** NOTE: case, accents, punctuation and spacing folded away, so only letters and digits are compared. */
|
|
5
|
+
export function fold(text) {
|
|
6
|
+
return text.normalize("NFKD").replace(COMBINING_MARKS, "").toLowerCase().replace(NOT_LETTER_OR_DIGIT, "");
|
|
7
|
+
}
|
|
8
|
+
const foldedPages = new WeakMap();
|
|
9
|
+
function foldedPage(page) {
|
|
10
|
+
const known = foldedPages.get(page);
|
|
11
|
+
if (known)
|
|
12
|
+
return known;
|
|
13
|
+
let folded = "";
|
|
14
|
+
const word = [];
|
|
15
|
+
const char = [];
|
|
16
|
+
page.words.forEach((entry, wordIndex) => {
|
|
17
|
+
[...entry.text].forEach((character, charIndex) => {
|
|
18
|
+
for (const piece of fold(character)) {
|
|
19
|
+
folded += piece;
|
|
20
|
+
word.push(wordIndex);
|
|
21
|
+
char.push(charIndex);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
const built = { folded, word: Int32Array.from(word), char: Int32Array.from(char) };
|
|
26
|
+
foldedPages.set(page, built);
|
|
27
|
+
return built;
|
|
28
|
+
}
|
|
29
|
+
/** NOTE: a folded quote shorter than this would land inside nearly every word. */
|
|
30
|
+
const MIN_FOLDED_QUOTE = 2;
|
|
31
|
+
export function findQuoteOnPage(page, quote) {
|
|
32
|
+
const needle = fold(quote);
|
|
33
|
+
if (needle.length < MIN_FOLDED_QUOTE)
|
|
34
|
+
return [];
|
|
35
|
+
const { folded, word, char } = foldedPage(page);
|
|
36
|
+
const matches = [];
|
|
37
|
+
let from = 0;
|
|
38
|
+
for (;;) {
|
|
39
|
+
const at = folded.indexOf(needle, from);
|
|
40
|
+
if (at === -1)
|
|
41
|
+
break;
|
|
42
|
+
const end = at + needle.length - 1;
|
|
43
|
+
const first = { word: word[at] ?? 0, char: char[at] ?? 0 };
|
|
44
|
+
const last = { word: word[end] ?? 0, char: char[end] ?? 0 };
|
|
45
|
+
matches.push({ page: page.index, firstWord: first.word, lastWord: last.word, rects: rangeRects(page, first, last) });
|
|
46
|
+
from = at + needle.length;
|
|
47
|
+
}
|
|
48
|
+
return matches;
|
|
49
|
+
}
|
|
50
|
+
/** NOTE: how many lines from a page's first and from its last may be a heading, a foot or a page number. */
|
|
51
|
+
const EDGE_LINES = 3;
|
|
52
|
+
const DIGITS = /\p{N}/gu;
|
|
53
|
+
const pageEdges = new WeakMap();
|
|
54
|
+
function edgesOf(page) {
|
|
55
|
+
const known = pageEdges.get(page);
|
|
56
|
+
if (known)
|
|
57
|
+
return known;
|
|
58
|
+
const shape = (lineIndex) => {
|
|
59
|
+
const line = page.lines[lineIndex];
|
|
60
|
+
const from = line ? page.words[line.firstWord] : undefined;
|
|
61
|
+
const to = line ? page.words[line.lastWord] : undefined;
|
|
62
|
+
return from && to ? fold(page.text.slice(from.start, to.end)).replace(DIGITS, "") : "";
|
|
63
|
+
};
|
|
64
|
+
const count = Math.min(EDGE_LINES, page.lines.length);
|
|
65
|
+
const built = {
|
|
66
|
+
first: Array.from({ length: count }, (_, order) => shape(order)),
|
|
67
|
+
last: Array.from({ length: count }, (_, order) => shape(page.lines.length - 1 - order)),
|
|
68
|
+
};
|
|
69
|
+
pageEdges.set(page, built);
|
|
70
|
+
return built;
|
|
71
|
+
}
|
|
72
|
+
const repeated = new WeakMap();
|
|
73
|
+
/** NOTE: on how many pages each shape stands among the first few lines, and among the last few. */
|
|
74
|
+
function repeatedEdges(pages) {
|
|
75
|
+
const known = repeated.get(pages);
|
|
76
|
+
if (known)
|
|
77
|
+
return known;
|
|
78
|
+
const count = (side) => {
|
|
79
|
+
const seen = new Map();
|
|
80
|
+
for (const page of pages)
|
|
81
|
+
for (const shape of new Set(edgesOf(page)[side]))
|
|
82
|
+
seen.set(shape, (seen.get(shape) ?? 0) + 1);
|
|
83
|
+
return seen;
|
|
84
|
+
};
|
|
85
|
+
const built = { first: count("first"), last: count("last") };
|
|
86
|
+
repeated.set(pages, built);
|
|
87
|
+
return built;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* NOTE: where one page's own text ends and the next page's begins, in folded characters, with whatever stands between
|
|
91
|
+
* them left out. A line stands between when it is among the first page's last few lines or the next page's first few
|
|
92
|
+
* and it says nothing but digits, or says the same, digits aside, at the same end of another page, or says what a line
|
|
93
|
+
* at the other side of this very break says, as a "continued" line does.
|
|
94
|
+
* TRADEOFF: a heading that appears on one page only cannot be told from the page's own text, so it is not left out.
|
|
95
|
+
*/
|
|
96
|
+
function ownText(pages, page, next) {
|
|
97
|
+
const repeats = repeatedEdges(pages);
|
|
98
|
+
const [here, there] = [edgesOf(page), edgesOf(next)];
|
|
99
|
+
const standsBetween = (shape, side) => shape === "" || (repeats[side].get(shape) ?? 0) >= 2 || (side === "last" ? there.first : here.last).includes(shape);
|
|
100
|
+
let trailing = 0;
|
|
101
|
+
while (trailing < here.last.length && trailing < page.lines.length - 1 && standsBetween(here.last[trailing] ?? "", "last"))
|
|
102
|
+
trailing += 1;
|
|
103
|
+
let leading = 0;
|
|
104
|
+
while (leading < there.first.length && leading < next.lines.length - 1 && standsBetween(there.first[leading] ?? "", "first"))
|
|
105
|
+
leading += 1;
|
|
106
|
+
const lastWord = page.lines[page.lines.length - 1 - trailing]?.lastWord ?? -1;
|
|
107
|
+
const firstWord = next.lines[leading]?.firstWord ?? next.words.length;
|
|
108
|
+
const before = foldedPage(page).word;
|
|
109
|
+
const after = foldedPage(next).word;
|
|
110
|
+
let end = before.length;
|
|
111
|
+
while (end > 0 && (before[end - 1] ?? 0) > lastWord)
|
|
112
|
+
end -= 1;
|
|
113
|
+
let start = 0;
|
|
114
|
+
while (start < after.length && (after[start] ?? 0) < firstWord)
|
|
115
|
+
start += 1;
|
|
116
|
+
return { end, start };
|
|
117
|
+
}
|
|
118
|
+
/** NOTE: the one place, if any, where the quote starts before `end` on one page and finishes from `start` on the next. */
|
|
119
|
+
function overBreak(page, end, next, start, needle) {
|
|
120
|
+
const before = foldedPage(page);
|
|
121
|
+
const after = foldedPage(next);
|
|
122
|
+
const reach = needle.length - 1;
|
|
123
|
+
const tailStart = Math.max(0, end - reach);
|
|
124
|
+
const tail = before.folded.slice(tailStart, end);
|
|
125
|
+
const joined = tail + after.folded.slice(start, start + reach);
|
|
126
|
+
for (let at = joined.indexOf(needle); at !== -1 && at < tail.length; at = joined.indexOf(needle, at + 1)) {
|
|
127
|
+
if (at + needle.length <= tail.length)
|
|
128
|
+
continue;
|
|
129
|
+
const from = tailStart + at;
|
|
130
|
+
const to = start + at + needle.length - 1 - tail.length;
|
|
131
|
+
const place = (folded, index) => ({ word: folded.word[index] ?? 0, char: folded.char[index] ?? 0 });
|
|
132
|
+
const [first, lastHere, firstThere, last] = [place(before, from), place(before, end - 1), place(after, start), place(after, to)];
|
|
133
|
+
return {
|
|
134
|
+
page: page.index,
|
|
135
|
+
firstWord: first.word,
|
|
136
|
+
lastWord: lastHere.word,
|
|
137
|
+
rects: rangeRects(page, first, lastHere),
|
|
138
|
+
continued: { page: next.index, firstWord: firstThere.word, lastWord: last.word, rects: rangeRects(next, firstThere, last) },
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* NOTE: every occurrence in reading order. A quote that runs from one page onto the next is found too: first as the
|
|
145
|
+
* two pages stand, then with what stands between their own texts left out on both pages, then on either one, which is
|
|
146
|
+
* how a line printed at the foot of one page and again at the head of the next is read once.
|
|
147
|
+
*/
|
|
148
|
+
export function findQuote(pages, quote) {
|
|
149
|
+
const needle = fold(quote);
|
|
150
|
+
if (needle.length < MIN_FOLDED_QUOTE)
|
|
151
|
+
return [];
|
|
152
|
+
return pages.flatMap((page, order) => {
|
|
153
|
+
const matches = findQuoteOnPage(page, quote);
|
|
154
|
+
const next = pages[order + 1];
|
|
155
|
+
if (!next || next.index !== page.index + 1 || page.words.length === 0 || next.words.length === 0)
|
|
156
|
+
return matches;
|
|
157
|
+
const whole = foldedPage(page).folded.length;
|
|
158
|
+
const own = ownText(pages, page, next);
|
|
159
|
+
const tries = [
|
|
160
|
+
[whole, 0],
|
|
161
|
+
[own.end, own.start],
|
|
162
|
+
[own.end, 0],
|
|
163
|
+
[whole, own.start],
|
|
164
|
+
];
|
|
165
|
+
for (const [end, start] of tries) {
|
|
166
|
+
const found = overBreak(page, end, next, start, needle);
|
|
167
|
+
if (found)
|
|
168
|
+
return [...matches, found];
|
|
169
|
+
}
|
|
170
|
+
return matches;
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/** NOTE: a match's parts, page by page: one, or two when it runs over a page break. */
|
|
174
|
+
export function matchParts(match) {
|
|
175
|
+
const { continued, ...first } = match;
|
|
176
|
+
return continued ? [first, continued] : [first];
|
|
177
|
+
}
|
package/core/hidden.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Box, PageReading, Word } from "./types.js";
|
|
2
|
+
/** NOTE: hidden words that follow one another on one line and are hidden for the same reason, taken together as a reader would read them. */
|
|
3
|
+
export type HiddenRun = {
|
|
4
|
+
page: number;
|
|
5
|
+
firstWord: number;
|
|
6
|
+
lastWord: number;
|
|
7
|
+
hidden: NonNullable<Word["hidden"]>;
|
|
8
|
+
text: string;
|
|
9
|
+
rects: Box[];
|
|
10
|
+
};
|
|
11
|
+
/** NOTE: everything a document carries that a reader cannot see, in reading order. */
|
|
12
|
+
export declare function hiddenRuns(pages: ReadonlyArray<PageReading>): HiddenRun[];
|
package/core/hidden.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { wordRangeRects, wordRangeText } from "./locate.js";
|
|
2
|
+
/** NOTE: everything a document carries that a reader cannot see, in reading order. */
|
|
3
|
+
export function hiddenRuns(pages) {
|
|
4
|
+
const runs = [];
|
|
5
|
+
for (const page of pages) {
|
|
6
|
+
let open = null;
|
|
7
|
+
const close = () => {
|
|
8
|
+
if (!open)
|
|
9
|
+
return;
|
|
10
|
+
const { firstWord, lastWord, hidden } = open;
|
|
11
|
+
runs.push({ page: page.index, firstWord, lastWord, hidden, text: wordRangeText(page, firstWord, lastWord), rects: wordRangeRects(page, firstWord, lastWord) });
|
|
12
|
+
open = null;
|
|
13
|
+
};
|
|
14
|
+
page.words.forEach((word, index) => {
|
|
15
|
+
if (open && word.hidden === open.hidden && word.line === open.line) {
|
|
16
|
+
open.lastWord = index;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
close();
|
|
20
|
+
if (word.hidden)
|
|
21
|
+
open = { firstWord: index, lastWord: index, hidden: word.hidden, line: word.line };
|
|
22
|
+
});
|
|
23
|
+
close();
|
|
24
|
+
}
|
|
25
|
+
return runs;
|
|
26
|
+
}
|
package/core/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type { Box, DocumentSummary, Line, Match, MatchPart, PageLink, PageReading, PageSource, Reading, ReadingStatus, Word } from "./types.js";
|
|
2
|
+
export { findQuote, findQuoteOnPage, fold, matchParts } from "./find.js";
|
|
3
|
+
export { lineAt, lineText, nearestWord, readRegion, wordRangeRects, wordRangeText } from "./locate.js";
|
|
4
|
+
export type { RegionReading } from "./locate.js";
|
|
5
|
+
export { hiddenRuns } from "./hidden.js";
|
|
6
|
+
export { safeLinkUrl } from "./links.js";
|
|
7
|
+
export type { HiddenRun } from "./hidden.js";
|
|
8
|
+
export { compareParagraphs, compareReadings } from "./compare.js";
|
|
9
|
+
export type { Change, ParagraphChange, ParagraphSide, Span } from "./compare.js";
|
|
10
|
+
export { paragraphs } from "./paragraphs.js";
|
|
11
|
+
export type { Paragraph } from "./paragraphs.js";
|
package/core/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { findQuote, findQuoteOnPage, fold, matchParts } from "./find.js";
|
|
2
|
+
export { lineAt, lineText, nearestWord, readRegion, wordRangeRects, wordRangeText } from "./locate.js";
|
|
3
|
+
export { hiddenRuns } from "./hidden.js";
|
|
4
|
+
export { safeLinkUrl } from "./links.js";
|
|
5
|
+
export { compareParagraphs, compareReadings } from "./compare.js";
|
|
6
|
+
export { paragraphs } from "./paragraphs.js";
|
package/core/links.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: the address a link in a file may lead to, or null for one no viewer should follow. A file is not to be
|
|
3
|
+
* trusted with where it sends a reader: an address may run a script in the page that shows it, or open a file on the
|
|
4
|
+
* reader's machine. Only web, mail and telephone addresses pass, and one written without its scheme, as
|
|
5
|
+
* "www.example.com", is taken for a web address.
|
|
6
|
+
*/
|
|
7
|
+
export declare function safeLinkUrl(raw: string): string | null;
|
package/core/links.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const FOLLOWED = new Set(["http:", "https:", "mailto:", "tel:"]);
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: the address a link in a file may lead to, or null for one no viewer should follow. A file is not to be
|
|
4
|
+
* trusted with where it sends a reader: an address may run a script in the page that shows it, or open a file on the
|
|
5
|
+
* reader's machine. Only web, mail and telephone addresses pass, and one written without its scheme, as
|
|
6
|
+
* "www.example.com", is taken for a web address.
|
|
7
|
+
*/
|
|
8
|
+
export function safeLinkUrl(raw) {
|
|
9
|
+
const address = raw.replace(/\p{Cc}/gu, "").trim();
|
|
10
|
+
if (address === "")
|
|
11
|
+
return null;
|
|
12
|
+
try {
|
|
13
|
+
const url = new URL(/^www\./i.test(address) ? `http://${address}` : address);
|
|
14
|
+
return FOLLOWED.has(url.protocol) ? url.href : null;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
package/core/locate.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Box, PageReading } from "./types.js";
|
|
2
|
+
type WordEnd = {
|
|
3
|
+
word: number;
|
|
4
|
+
char: number;
|
|
5
|
+
};
|
|
6
|
+
/** NOTE: one box per line the words run over, from a character of the first word to a character of the last. */
|
|
7
|
+
export declare function rangeRects(page: PageReading, first: WordEnd, last: WordEnd): Box[];
|
|
8
|
+
export declare function wordRangeRects(page: PageReading, firstWord: number, lastWord: number): Box[];
|
|
9
|
+
export declare function wordRangeText(page: PageReading, firstWord: number, lastWord: number): string;
|
|
10
|
+
export declare function lineText(page: PageReading, lineIndex: number): string;
|
|
11
|
+
/** NOTE: the line under a point, preferring the shortest when boxes overlap, as a stamp over a paragraph does. */
|
|
12
|
+
export declare function lineAt(page: PageReading, x: number, y: number): number | null;
|
|
13
|
+
export declare function nearestWord(page: PageReading, x: number, y: number): number | null;
|
|
14
|
+
export type RegionReading = {
|
|
15
|
+
text: string;
|
|
16
|
+
rects: Box[];
|
|
17
|
+
wordCount: number;
|
|
18
|
+
};
|
|
19
|
+
/** NOTE: the words whose centre lies inside the box, in reading order, with a line break wherever the page has one. */
|
|
20
|
+
export declare function readRegion(page: PageReading, box: Box): RegionReading;
|
|
21
|
+
export {};
|
package/core/locate.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
function charLeft(word, char) {
|
|
2
|
+
return word.edges?.[char] ?? word.box.x;
|
|
3
|
+
}
|
|
4
|
+
function charRight(word, char) {
|
|
5
|
+
return word.edges?.[char + 1] ?? word.box.x + word.box.width;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* NOTE: a line that slopes, as on a photographed page, has a box far taller than its words, so its words' own heights
|
|
9
|
+
* are used instead. A word more than twice as tall as the line's middle word, such as a stamp OCR read into the line,
|
|
10
|
+
* is left out of the comparison: counted, it would make the line look level and every mark on it as tall as the stamp.
|
|
11
|
+
*/
|
|
12
|
+
function isLevelLine(page, lineIndex) {
|
|
13
|
+
const line = page.lines[lineIndex];
|
|
14
|
+
if (!line)
|
|
15
|
+
return false;
|
|
16
|
+
const heights = page.words
|
|
17
|
+
.slice(line.firstWord, line.lastWord + 1)
|
|
18
|
+
.map((word) => word.box.height)
|
|
19
|
+
.sort((a, b) => a - b);
|
|
20
|
+
const middle = heights[Math.floor(heights.length / 2)] ?? 0;
|
|
21
|
+
const tallest = Math.max(0, ...heights.filter((height) => height <= middle * 2));
|
|
22
|
+
return line.box.height <= tallest * 1.5;
|
|
23
|
+
}
|
|
24
|
+
/** NOTE: one box per line the words run over, from a character of the first word to a character of the last. */
|
|
25
|
+
export function rangeRects(page, first, last) {
|
|
26
|
+
const rects = [];
|
|
27
|
+
let current = null;
|
|
28
|
+
const flush = () => {
|
|
29
|
+
if (!current)
|
|
30
|
+
return;
|
|
31
|
+
const line = page.lines[current.line];
|
|
32
|
+
const top = line && isLevelLine(page, current.line) ? line.box.y : current.top;
|
|
33
|
+
const bottom = line && isLevelLine(page, current.line) ? line.box.y + line.box.height : current.bottom;
|
|
34
|
+
rects.push({ x: current.left, y: top, width: current.right - current.left, height: bottom - top });
|
|
35
|
+
current = null;
|
|
36
|
+
};
|
|
37
|
+
for (let index = first.word; index <= last.word; index += 1) {
|
|
38
|
+
const word = page.words[index];
|
|
39
|
+
if (!word)
|
|
40
|
+
continue;
|
|
41
|
+
const left = index === first.word ? charLeft(word, first.char) : word.box.x;
|
|
42
|
+
const right = index === last.word ? charRight(word, last.char) : word.box.x + word.box.width;
|
|
43
|
+
if (current && current.line === word.line) {
|
|
44
|
+
current.left = Math.min(current.left, left);
|
|
45
|
+
current.right = Math.max(current.right, right);
|
|
46
|
+
current.top = Math.min(current.top, word.box.y);
|
|
47
|
+
current.bottom = Math.max(current.bottom, word.box.y + word.box.height);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
flush();
|
|
51
|
+
current = { line: word.line, left, right, top: word.box.y, bottom: word.box.y + word.box.height };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
flush();
|
|
55
|
+
return rects;
|
|
56
|
+
}
|
|
57
|
+
export function wordRangeRects(page, firstWord, lastWord) {
|
|
58
|
+
const last = page.words[lastWord];
|
|
59
|
+
return rangeRects(page, { word: firstWord, char: 0 }, { word: lastWord, char: last ? [...last.text].length - 1 : 0 });
|
|
60
|
+
}
|
|
61
|
+
export function wordRangeText(page, firstWord, lastWord) {
|
|
62
|
+
const first = page.words[firstWord];
|
|
63
|
+
const last = page.words[lastWord];
|
|
64
|
+
return first && last ? page.text.slice(first.start, last.end) : "";
|
|
65
|
+
}
|
|
66
|
+
export function lineText(page, lineIndex) {
|
|
67
|
+
const line = page.lines[lineIndex];
|
|
68
|
+
return line ? wordRangeText(page, line.firstWord, line.lastWord) : "";
|
|
69
|
+
}
|
|
70
|
+
function contains(box, x, y, slack = 0) {
|
|
71
|
+
return x >= box.x - slack && x <= box.x + box.width + slack && y >= box.y - slack && y <= box.y + box.height + slack;
|
|
72
|
+
}
|
|
73
|
+
function distance(box, x, y) {
|
|
74
|
+
const dx = Math.max(box.x - x, 0, x - (box.x + box.width));
|
|
75
|
+
const dy = Math.max(box.y - y, 0, y - (box.y + box.height));
|
|
76
|
+
return Math.hypot(dx, dy);
|
|
77
|
+
}
|
|
78
|
+
/** NOTE: the line under a point, preferring the shortest when boxes overlap, as a stamp over a paragraph does. */
|
|
79
|
+
export function lineAt(page, x, y) {
|
|
80
|
+
let found = null;
|
|
81
|
+
let smallest = Number.POSITIVE_INFINITY;
|
|
82
|
+
page.lines.forEach((line, index) => {
|
|
83
|
+
if (!contains(line.box, x, y, 1.5))
|
|
84
|
+
return;
|
|
85
|
+
const hasWordNear = page.words.slice(line.firstWord, line.lastWord + 1).some((word) => distance(word.box, x, y) <= word.box.height * 2);
|
|
86
|
+
if (!hasWordNear)
|
|
87
|
+
return;
|
|
88
|
+
const area = line.box.width * line.box.height;
|
|
89
|
+
if (area < smallest) {
|
|
90
|
+
smallest = area;
|
|
91
|
+
found = index;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return found;
|
|
95
|
+
}
|
|
96
|
+
export function nearestWord(page, x, y) {
|
|
97
|
+
let found = null;
|
|
98
|
+
let nearest = Number.POSITIVE_INFINITY;
|
|
99
|
+
page.words.forEach((word, index) => {
|
|
100
|
+
const away = distance(word.box, x, y);
|
|
101
|
+
if (away < nearest) {
|
|
102
|
+
nearest = away;
|
|
103
|
+
found = index;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return found;
|
|
107
|
+
}
|
|
108
|
+
/** NOTE: the words whose centre lies inside the box, in reading order, with a line break wherever the page has one. */
|
|
109
|
+
export function readRegion(page, box) {
|
|
110
|
+
const rects = [];
|
|
111
|
+
let text = "";
|
|
112
|
+
let wordCount = 0;
|
|
113
|
+
let run = null;
|
|
114
|
+
let previousLine = -1;
|
|
115
|
+
const flush = () => {
|
|
116
|
+
if (run)
|
|
117
|
+
rects.push(...wordRangeRects(page, run.first, run.last));
|
|
118
|
+
run = null;
|
|
119
|
+
};
|
|
120
|
+
page.words.forEach((word, index) => {
|
|
121
|
+
const isInside = contains(box, word.box.x + word.box.width / 2, word.box.y + word.box.height / 2);
|
|
122
|
+
if (!isInside) {
|
|
123
|
+
flush();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (wordCount > 0)
|
|
127
|
+
text += word.line === previousLine ? " " : "\n";
|
|
128
|
+
text += word.text;
|
|
129
|
+
wordCount += 1;
|
|
130
|
+
previousLine = word.line;
|
|
131
|
+
if (run && word.line === page.words[run.last]?.line)
|
|
132
|
+
run.last = index;
|
|
133
|
+
else {
|
|
134
|
+
flush();
|
|
135
|
+
run = { first: index, last: index };
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
flush();
|
|
139
|
+
return { text, rects, wordCount };
|
|
140
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Box, PageReading } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: a run of lines on one page that belong together: a paragraph of prose, one item of a list, a heading.
|
|
4
|
+
* It is worked out from where the lines sit and how they begin, since a reading carries no such thing itself.
|
|
5
|
+
*/
|
|
6
|
+
export type Paragraph = {
|
|
7
|
+
page: number;
|
|
8
|
+
firstLine: number;
|
|
9
|
+
lastLine: number;
|
|
10
|
+
/** NOTE: its words, the list marker among them when it has one. */
|
|
11
|
+
firstWord: number;
|
|
12
|
+
lastWord: number;
|
|
13
|
+
/** NOTE: the box around all of it. */
|
|
14
|
+
box: Box;
|
|
15
|
+
/** NOTE: one box for each of its lines. */
|
|
16
|
+
rects: Box[];
|
|
17
|
+
/** NOTE: its words in reading order with one space between, without the list marker. */
|
|
18
|
+
text: string;
|
|
19
|
+
/** NOTE: what a list item begins with, such as a bullet or "2.", when it begins with one. */
|
|
20
|
+
marker?: string;
|
|
21
|
+
/**
|
|
22
|
+
* NOTE: true when it is set as a heading is: one short line, not a list item, a labelled line or a sentence, that
|
|
23
|
+
* stands nearer to the line under it than to the line over it, or is in capitals or in larger print, or has nothing
|
|
24
|
+
* over it. It is a reading of the layout and not something the file says, so a caller that lists changes can leave
|
|
25
|
+
* out a heading whose own paragraph is listed too, and should not lean on it for more.
|
|
26
|
+
*/
|
|
27
|
+
heading?: true;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* NOTE: the paragraphs of a page, in reading order. A line starts a paragraph when it is not below the last one (a
|
|
31
|
+
* new column), is in other print, begins with a list marker or with a label ending in a colon, sits further below
|
|
32
|
+
* than lines usually do, follows a line that had plenty of room left for its first word, or leaves the left edge the
|
|
33
|
+
* paragraph keeps. That edge is where a list item's words begin, or its marker; for prose it is the edge of the last
|
|
34
|
+
* line, and a first line may be set in from it.
|
|
35
|
+
* CONSTRAINT: a paragraph ends with its page, so one that runs onto the next page is two.
|
|
36
|
+
*/
|
|
37
|
+
export declare function paragraphs(page: PageReading): Paragraph[];
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/** NOTE: a bullet, a dash, a number or a letter set off as a list's does, standing as a word of its own. */
|
|
2
|
+
const MARKER = /^(?:[•◦▪▫‣⁃∙·●○■□▸▹►*+–—-]|\(?\d{1,3}[.)]|\([a-z]\)|[a-z]\))$/;
|
|
3
|
+
/** NOTE: a line this many times further below the last than lines usually are starts a paragraph. */
|
|
4
|
+
const WIDER_GAP = 1.25;
|
|
5
|
+
/** NOTE: a line this many times as tall as its neighbour is in other print, as a heading is. */
|
|
6
|
+
const OTHER_PRINT = 1.6;
|
|
7
|
+
/**
|
|
8
|
+
* CONSTRAINT: a line's height is that of the letters it happens to hold, so two lines in the same print differ by up
|
|
9
|
+
* to a third. Heights within this of each other are taken for the same print when the usual pitch is worked out.
|
|
10
|
+
*/
|
|
11
|
+
const SAME_PRINT = 1.5;
|
|
12
|
+
/** NOTE: the share of a line's height by which a left edge may wander and still be the same edge. */
|
|
13
|
+
const EDGE_SLACK = 0.35;
|
|
14
|
+
/** NOTE: a label ends within this many words of the start of its line. */
|
|
15
|
+
const LABEL_WORDS = 5;
|
|
16
|
+
/** NOTE: with too few lines to tell the usual pitch from, it is taken as this many times a line's height. */
|
|
17
|
+
const UNKNOWN_PITCH = 1.6;
|
|
18
|
+
const FEWEST_PITCHES = 2;
|
|
19
|
+
/**
|
|
20
|
+
* NOTE: a line is broken for want of room, so when the first word of the next line had this many line heights of
|
|
21
|
+
* room to spare beside the last, the next line was not carried over: it starts a paragraph.
|
|
22
|
+
* TRADEOFF: programs that even out a paragraph's lines sometimes leave one a word short, so a small shortfall is not
|
|
23
|
+
* taken for the end of a paragraph; a paragraph whose last line is nearly full is then missed when nothing else marks
|
|
24
|
+
* the next one.
|
|
25
|
+
*/
|
|
26
|
+
const SPARE_ROOM = 3;
|
|
27
|
+
/** NOTE: a heading is at most this share of the page's widest line, and this many times nearer to the line under it than to the line over it. */
|
|
28
|
+
const HEADING_WIDTH = 0.5;
|
|
29
|
+
/** CONSTRAINT: lines of one print start within about a hundredth of the same distance apart, so a twentieth further is a real difference and not the measuring. */
|
|
30
|
+
const NEARER = 1.05;
|
|
31
|
+
const ENDS_A_CLAUSE = /[.,;]$/;
|
|
32
|
+
const apart = (first, second) => Math.max(first, second) / Math.max(Math.min(first, second), Number.EPSILON);
|
|
33
|
+
/**
|
|
34
|
+
* NOTE: how far below one another lines of this print usually start on the page: the lower quarter of the distances
|
|
35
|
+
* between neighbouring lines of that print, so the gaps between paragraphs, which are the larger ones, do not count.
|
|
36
|
+
*/
|
|
37
|
+
function usualPitch(lines, height) {
|
|
38
|
+
const pitches = [];
|
|
39
|
+
for (let index = 1; index < lines.length; index += 1) {
|
|
40
|
+
const [above, below] = [lines[index - 1], lines[index]];
|
|
41
|
+
if (!above || !below)
|
|
42
|
+
continue;
|
|
43
|
+
const pitch = below.box.y - above.box.y;
|
|
44
|
+
const tallest = Math.max(above.box.height, below.box.height);
|
|
45
|
+
if (pitch > 0 && apart(above.box.height, below.box.height) <= SAME_PRINT && apart(tallest, height) <= SAME_PRINT)
|
|
46
|
+
pitches.push(pitch);
|
|
47
|
+
}
|
|
48
|
+
if (pitches.length < FEWEST_PITCHES)
|
|
49
|
+
return height * UNKNOWN_PITCH;
|
|
50
|
+
pitches.sort((first, second) => first - second);
|
|
51
|
+
return pitches[Math.floor(pitches.length / 4)] ?? height * UNKNOWN_PITCH;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* NOTE: the paragraphs of a page, in reading order. A line starts a paragraph when it is not below the last one (a
|
|
55
|
+
* new column), is in other print, begins with a list marker or with a label ending in a colon, sits further below
|
|
56
|
+
* than lines usually do, follows a line that had plenty of room left for its first word, or leaves the left edge the
|
|
57
|
+
* paragraph keeps. That edge is where a list item's words begin, or its marker; for prose it is the edge of the last
|
|
58
|
+
* line, and a first line may be set in from it.
|
|
59
|
+
* CONSTRAINT: a paragraph ends with its page, so one that runs onto the next page is two.
|
|
60
|
+
*/
|
|
61
|
+
export function paragraphs(page) {
|
|
62
|
+
const { lines, words } = page;
|
|
63
|
+
const wordsOf = (line) => words.slice(line.firstWord, line.lastWord + 1);
|
|
64
|
+
const markerOf = (line) => {
|
|
65
|
+
const first = words[line.firstWord]?.text ?? "";
|
|
66
|
+
return line.lastWord > line.firstWord && MARKER.test(first) ? first : undefined;
|
|
67
|
+
};
|
|
68
|
+
const hasLabel = (line) => {
|
|
69
|
+
const texts = wordsOf(line).map((word) => word.text);
|
|
70
|
+
if (!/^[\p{Lu}\p{Nd}]/u.test(texts[0] ?? ""))
|
|
71
|
+
return false;
|
|
72
|
+
const end = texts.slice(0, LABEL_WORDS).findIndex((text) => text.endsWith(":"));
|
|
73
|
+
return end !== -1 && end < texts.length - 1;
|
|
74
|
+
};
|
|
75
|
+
const farEdgeOf = (line) => line.box.x + line.box.width;
|
|
76
|
+
const found = [];
|
|
77
|
+
let open = null;
|
|
78
|
+
const startsParagraph = (line, above, current) => {
|
|
79
|
+
if (line.box.y < above.box.y + above.box.height / 2)
|
|
80
|
+
return true;
|
|
81
|
+
if (apart(line.box.height, above.box.height) > OTHER_PRINT)
|
|
82
|
+
return true;
|
|
83
|
+
if (markerOf(line) !== undefined || hasLabel(line))
|
|
84
|
+
return true;
|
|
85
|
+
const tallest = Math.max(line.box.height, above.box.height);
|
|
86
|
+
if (line.box.y - above.box.y > usualPitch(lines, tallest) * WIDER_GAP)
|
|
87
|
+
return true;
|
|
88
|
+
const room = Math.max(current.farEdge, farEdgeOf(line)) - farEdgeOf(above);
|
|
89
|
+
if (room >= (words[line.firstWord]?.box.width ?? Number.POSITIVE_INFINITY) + tallest * SPARE_ROOM)
|
|
90
|
+
return true;
|
|
91
|
+
const slack = tallest * EDGE_SLACK;
|
|
92
|
+
if (current.marker !== undefined)
|
|
93
|
+
return line.box.x < current.edge - slack || line.box.x > current.wordsEdge + slack;
|
|
94
|
+
if (line.box.x > above.box.x + slack)
|
|
95
|
+
return true;
|
|
96
|
+
return line.box.x < above.box.x - slack && current.lastLine > current.firstLine;
|
|
97
|
+
};
|
|
98
|
+
lines.forEach((line, index) => {
|
|
99
|
+
const above = lines[index - 1];
|
|
100
|
+
if (open && above && !startsParagraph(line, above, open)) {
|
|
101
|
+
open.lastLine = index;
|
|
102
|
+
open.farEdge = Math.max(open.farEdge, farEdgeOf(line));
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const marker = markerOf(line);
|
|
106
|
+
open = { firstLine: index, lastLine: index, marker, edge: line.box.x, wordsEdge: marker === undefined ? line.box.x : (words[line.firstWord + 1]?.box.x ?? line.box.x), farEdge: farEdgeOf(line) };
|
|
107
|
+
found.push(open);
|
|
108
|
+
});
|
|
109
|
+
const widest = Math.max(0, ...lines.map((line) => line.box.width));
|
|
110
|
+
const isHeading = ({ firstLine, lastLine, marker }) => {
|
|
111
|
+
const [over, line, under] = [lines[firstLine - 1], lines[firstLine], lines[firstLine + 1]];
|
|
112
|
+
if (lastLine !== firstLine || marker !== undefined || !line || !under || under.box.y <= line.box.y)
|
|
113
|
+
return false;
|
|
114
|
+
if (hasLabel(line) || line.box.width > widest * HEADING_WIDTH || ENDS_A_CLAUSE.test(words[line.lastWord]?.text ?? ""))
|
|
115
|
+
return false;
|
|
116
|
+
if (line.box.height > under.box.height * OTHER_PRINT || !over || over.box.y >= line.box.y)
|
|
117
|
+
return true;
|
|
118
|
+
const text = wordsOf(line)
|
|
119
|
+
.map((word) => word.text)
|
|
120
|
+
.join("");
|
|
121
|
+
if (/\p{Lu}.*\p{Lu}/u.test(text) && !/\p{Ll}/u.test(text))
|
|
122
|
+
return true;
|
|
123
|
+
return line.box.y - over.box.y >= (under.box.y - line.box.y) * NEARER;
|
|
124
|
+
};
|
|
125
|
+
return found.flatMap((paragraph) => {
|
|
126
|
+
const { firstLine, lastLine, marker } = paragraph;
|
|
127
|
+
const [first, last] = [lines[firstLine], lines[lastLine]];
|
|
128
|
+
if (!first || !last)
|
|
129
|
+
return [];
|
|
130
|
+
const rects = lines.slice(firstLine, lastLine + 1).map((line) => ({ ...line.box }));
|
|
131
|
+
const left = Math.min(...rects.map((rect) => rect.x));
|
|
132
|
+
const top = Math.min(...rects.map((rect) => rect.y));
|
|
133
|
+
const box = { x: left, y: top, width: Math.max(...rects.map((rect) => rect.x + rect.width)) - left, height: Math.max(...rects.map((rect) => rect.y + rect.height)) - top };
|
|
134
|
+
const text = words
|
|
135
|
+
.slice(first.firstWord + (marker === undefined ? 0 : 1), last.lastWord + 1)
|
|
136
|
+
.map((word) => word.text)
|
|
137
|
+
.join(" ");
|
|
138
|
+
const told = { page: page.index, firstLine, lastLine, firstWord: first.firstWord, lastWord: last.lastWord, box, rects, text };
|
|
139
|
+
return [{ ...told, ...(marker === undefined ? {} : { marker }), ...(isHeading(paragraph) ? { heading: true } : {}) }];
|
|
140
|
+
});
|
|
141
|
+
}
|