@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,92 @@
|
|
|
1
|
+
import type { Box } from "../core/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: the shapes of everything the reader is handed. It never imports an implementation of one, so any of them can be
|
|
4
|
+
* replaced without touching the reader: another PDF engine, another OCR, a converter, a caller's own.
|
|
5
|
+
*/
|
|
6
|
+
/** NOTE: one character of a page's text layer, boxed in page units from the top-left of the upright page. */
|
|
7
|
+
export type PdfChar = {
|
|
8
|
+
code: number;
|
|
9
|
+
left: number;
|
|
10
|
+
top: number;
|
|
11
|
+
right: number;
|
|
12
|
+
bottom: number;
|
|
13
|
+
/** NOTE: true when the file gives no usable letter for the character, the sign of a broken font map. */
|
|
14
|
+
hasMapError: boolean;
|
|
15
|
+
/** NOTE: true when the file says not to paint the character, as an OCR does with the words it writes over a scan. An engine that cannot tell leaves it out. */
|
|
16
|
+
isUnpainted?: boolean;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* NOTE: a link the file lays over a page, boxed like a character, and where it leads: an address exactly as the file
|
|
20
|
+
* has it, or a page of the same file counted from 0. Whether an address is safe to follow is the reader's call.
|
|
21
|
+
*/
|
|
22
|
+
export type PdfLink = {
|
|
23
|
+
left: number;
|
|
24
|
+
top: number;
|
|
25
|
+
right: number;
|
|
26
|
+
bottom: number;
|
|
27
|
+
url?: string;
|
|
28
|
+
page?: number;
|
|
29
|
+
};
|
|
30
|
+
/** NOTE: `links` is left out by an engine that reads none. */
|
|
31
|
+
export type PdfPageText = {
|
|
32
|
+
width: number;
|
|
33
|
+
height: number;
|
|
34
|
+
chars: PdfChar[];
|
|
35
|
+
links?: PdfLink[];
|
|
36
|
+
};
|
|
37
|
+
export type PdfRaster = {
|
|
38
|
+
rgba: Uint8Array;
|
|
39
|
+
width: number;
|
|
40
|
+
height: number;
|
|
41
|
+
};
|
|
42
|
+
/** NOTE: a box on a page, in page units from the top-left of the upright page. */
|
|
43
|
+
export type PagePart = Box;
|
|
44
|
+
/** NOTE: each may answer at once or later, so an engine can run in the caller's thread or in one of its own. */
|
|
45
|
+
export type OpenPdf = {
|
|
46
|
+
pageCount: number;
|
|
47
|
+
pageSize(index: number): {
|
|
48
|
+
width: number;
|
|
49
|
+
height: number;
|
|
50
|
+
} | Promise<{
|
|
51
|
+
width: number;
|
|
52
|
+
height: number;
|
|
53
|
+
}>;
|
|
54
|
+
readPage(index: number): PdfPageText | Promise<PdfPageText>;
|
|
55
|
+
/**
|
|
56
|
+
* NOTE: draws a page at `scale` pixels per page unit. Given a `part` inside the page, it draws that box alone, as
|
|
57
|
+
* the very pixels the whole page would have there, so a page can be drawn far sharper than a whole picture of it
|
|
58
|
+
* could be held.
|
|
59
|
+
*/
|
|
60
|
+
drawPage(index: number, scale: number, part?: PagePart): PdfRaster | Promise<PdfRaster>;
|
|
61
|
+
/**
|
|
62
|
+
* NOTE: draws a page as `drawPage` does and lends the picture to `use` while it is still in the engine's memory, so
|
|
63
|
+
* no copy of it is made. An engine in the caller's own thread can; one in a thread of its own leaves this out.
|
|
64
|
+
* CONSTRAINT: the picture is the engine's only during `use`, and `use` must call nothing of the engine's, since
|
|
65
|
+
* the engine's memory may move when it is asked for more.
|
|
66
|
+
*/
|
|
67
|
+
withDrawnPage?<T>(index: number, scale: number, part: PagePart | undefined, use: (raster: PdfRaster) => T): T;
|
|
68
|
+
close(): void | Promise<void>;
|
|
69
|
+
};
|
|
70
|
+
/** NOTE: thrown by an engine that was stopped, or that stopped by itself, before it answered. Its message says why in words a reader can be shown. */
|
|
71
|
+
export declare class EngineStopped extends Error {
|
|
72
|
+
}
|
|
73
|
+
/** NOTE: opens a PDF's bytes; it throws when they cannot be opened, as with a password it was not given. */
|
|
74
|
+
export type PdfEngine = (data: Uint8Array) => Promise<OpenPdf>;
|
|
75
|
+
/** NOTE: one word as an engine found it, before it is given a place in a page's text. */
|
|
76
|
+
export type WordDraft = {
|
|
77
|
+
text: string;
|
|
78
|
+
box: Box;
|
|
79
|
+
edges?: number[];
|
|
80
|
+
confidence?: number;
|
|
81
|
+
};
|
|
82
|
+
/** NOTE: reads the words on an encoded picture, as lines of words boxed in the picture's pixels. */
|
|
83
|
+
export type OcrEngine = {
|
|
84
|
+
readonly name: string;
|
|
85
|
+
recognize(image: Uint8Array): Promise<WordDraft[][]>;
|
|
86
|
+
close(): Promise<void>;
|
|
87
|
+
};
|
|
88
|
+
/** NOTE: turns a file the reader cannot open, such as a Word file, into a PDF it can. None ships with the library. */
|
|
89
|
+
export type Converter = {
|
|
90
|
+
accepts(bytes: Uint8Array, name: string): boolean;
|
|
91
|
+
toPdf(bytes: Uint8Array, name: string): Promise<Uint8Array>;
|
|
92
|
+
};
|
package/reader/seams.js
ADDED
package/reader/seen.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Word } from "../core/index.js";
|
|
2
|
+
import type { PdfPageText, PdfRaster } from "./seams.js";
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: for each word of a text layer, why a reader cannot see it, or undefined when they can. It is judged by looking:
|
|
5
|
+
* the page is drawn, and a word with nothing to tell apart inside its box is unseen, whatever made it so (white print,
|
|
6
|
+
* print marked not to be painted, something painted over it, a place off the page).
|
|
7
|
+
* CONSTRAINT: only a word with a letter or a digit in it can be hidden. Real files carry bullets and separators
|
|
8
|
+
* that show nothing, by design or for want of a glyph, and counting those would bury the words that matter.
|
|
9
|
+
* TRADEOFF: a word hidden behind a photograph is taken for seen, since the photograph gives its box something to show.
|
|
10
|
+
*/
|
|
11
|
+
export declare function hiddenWords(words: ReadonlyArray<Word>, page: {
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
}, raster: PdfRaster): Array<Word["hidden"]>;
|
|
15
|
+
/** NOTE: true when nearly all of a page's characters are marked not to be painted, which is how an OCR writes its words over a scan. */
|
|
16
|
+
export declare function isUnpaintedLayer(page: PdfPageText): boolean;
|
package/reader/seen.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { isSeparatorCode } from "./layer-words.js";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: how far apart, out of 255, two points inside a word's box must be in red, green or blue for the word to count
|
|
4
|
+
* as seen. Pale grey print on white is about 50 apart; white on white, or nothing painted at all, under 10. Colour
|
|
5
|
+
* counts as well as lightness, since yellow print on white is as light as the page and still plain to see.
|
|
6
|
+
*/
|
|
7
|
+
const SEEN_CONTRAST = 32;
|
|
8
|
+
/** NOTE: a word with a letter or a digit in it and a box lower than this, in page units, is print of about 3 points or less, which nobody reads. */
|
|
9
|
+
const TINY_HEIGHT = 2;
|
|
10
|
+
/** NOTE: the share of a page's characters that must be marked not to be painted for its text to be an OCR's, laid over a picture of the page. */
|
|
11
|
+
const UNPAINTED_SHARE = 0.9;
|
|
12
|
+
const LETTER_OR_DIGIT = /[\p{L}\p{N}]/u;
|
|
13
|
+
/**
|
|
14
|
+
* NOTE: how far past a word's box, in pixels of the drawn page, the look reaches. A letter that fills its whole box,
|
|
15
|
+
* as a plain capital I does, has nothing to tell apart inside it and is seen only against the page around it.
|
|
16
|
+
*/
|
|
17
|
+
const SURROUND = 1;
|
|
18
|
+
/** NOTE: true when the drawn page shows something inside the box: any two points of it differ enough to be told apart. */
|
|
19
|
+
function hasInk(raster, left, top, right, bottom) {
|
|
20
|
+
const most = [0, 0, 0];
|
|
21
|
+
const least = [255, 255, 255];
|
|
22
|
+
for (let y = top; y < bottom; y += 1) {
|
|
23
|
+
for (let x = left; x < right; x += 1) {
|
|
24
|
+
const at = (y * raster.width + x) * 4;
|
|
25
|
+
for (let channel = 0; channel < 3; channel += 1) {
|
|
26
|
+
const value = raster.rgba[at + channel] ?? 0;
|
|
27
|
+
if (value > (most[channel] ?? 0))
|
|
28
|
+
most[channel] = value;
|
|
29
|
+
if (value < (least[channel] ?? 255))
|
|
30
|
+
least[channel] = value;
|
|
31
|
+
if ((most[channel] ?? 0) - (least[channel] ?? 255) >= SEEN_CONTRAST)
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* NOTE: for each word of a text layer, why a reader cannot see it, or undefined when they can. It is judged by looking:
|
|
40
|
+
* the page is drawn, and a word with nothing to tell apart inside its box is unseen, whatever made it so (white print,
|
|
41
|
+
* print marked not to be painted, something painted over it, a place off the page).
|
|
42
|
+
* CONSTRAINT: only a word with a letter or a digit in it can be hidden. Real files carry bullets and separators
|
|
43
|
+
* that show nothing, by design or for want of a glyph, and counting those would bury the words that matter.
|
|
44
|
+
* TRADEOFF: a word hidden behind a photograph is taken for seen, since the photograph gives its box something to show.
|
|
45
|
+
*/
|
|
46
|
+
export function hiddenWords(words, page, raster) {
|
|
47
|
+
const scale = raster.width / page.width;
|
|
48
|
+
return words.map((word) => {
|
|
49
|
+
if (!LETTER_OR_DIGIT.test(word.text))
|
|
50
|
+
return undefined;
|
|
51
|
+
if (word.box.height < TINY_HEIGHT)
|
|
52
|
+
return "tiny";
|
|
53
|
+
const left = Math.floor(word.box.x * scale);
|
|
54
|
+
const top = Math.floor(word.box.y * scale);
|
|
55
|
+
const right = Math.ceil((word.box.x + word.box.width) * scale);
|
|
56
|
+
const bottom = Math.ceil((word.box.y + word.box.height) * scale);
|
|
57
|
+
const isOnPage = right > 0 && bottom > 0 && left < raster.width && top < raster.height;
|
|
58
|
+
const isSeen = isOnPage &&
|
|
59
|
+
hasInk(raster, Math.max(0, left - SURROUND), Math.max(0, top - SURROUND), Math.min(raster.width, right + SURROUND), Math.min(raster.height, bottom + SURROUND));
|
|
60
|
+
return isSeen ? undefined : "unseen";
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/** NOTE: true when nearly all of a page's characters are marked not to be painted, which is how an OCR writes its words over a scan. */
|
|
64
|
+
export function isUnpaintedLayer(page) {
|
|
65
|
+
const chars = page.chars.filter((char) => !isSeparatorCode(char.code));
|
|
66
|
+
return chars.length > 0 && chars.filter((char) => char.isUnpainted).length >= chars.length * UNPAINTED_SHARE;
|
|
67
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Word } from "../core/index.js";
|
|
2
|
+
import type { PdfPageText } from "./seams.js";
|
|
3
|
+
export type Trust = {
|
|
4
|
+
isTrusted: true;
|
|
5
|
+
} | {
|
|
6
|
+
isTrusted: false;
|
|
7
|
+
reason: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function trustLayer(page: PdfPageText, words: ReadonlyArray<Word>): Trust;
|
package/reader/trust.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isSeparatorCode } from "./layer-words.js";
|
|
2
|
+
/** CONSTRAINT: every threshold here is a first guess, to be tuned against real files. */
|
|
3
|
+
const MIN_LAYER_CHARS = 8;
|
|
4
|
+
const BAD_CHAR_SHARE = 0.05;
|
|
5
|
+
const MIN_BAD_CHARS = 3;
|
|
6
|
+
const MIXED_WORD_SHARE = 0.02;
|
|
7
|
+
const MIN_MIXED_WORDS = 2;
|
|
8
|
+
const LETTER = /\p{L}/u;
|
|
9
|
+
const DIGIT = /\p{N}/u;
|
|
10
|
+
const LINK_OR_ADDRESS = /:\/\/|^www\.|@/i;
|
|
11
|
+
const EDGE_PUNCTUATION = /^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu;
|
|
12
|
+
/** NOTE: symbols a wrong font map prints where a letter belongs, as in "Ma<e" or "Jor=an". */
|
|
13
|
+
const LETTER_STAND_INS = /[<>=\\|^~`{}[\]"]/;
|
|
14
|
+
function isPrivateOrUnknown(code) {
|
|
15
|
+
return code === 0xfffd || (code >= 0xe000 && code <= 0xf8ff);
|
|
16
|
+
}
|
|
17
|
+
function isMixedWord(text) {
|
|
18
|
+
if (!LETTER.test(text) || DIGIT.test(text) || LINK_OR_ADDRESS.test(text))
|
|
19
|
+
return false;
|
|
20
|
+
return LETTER_STAND_INS.test(text.replace(EDGE_PUNCTUATION, ""));
|
|
21
|
+
}
|
|
22
|
+
export function trustLayer(page, words) {
|
|
23
|
+
const visible = page.chars.filter((char) => !isSeparatorCode(char.code));
|
|
24
|
+
if (visible.length < MIN_LAYER_CHARS)
|
|
25
|
+
return { isTrusted: false, reason: "no text layer" };
|
|
26
|
+
const bad = visible.filter((char) => char.hasMapError || isPrivateOrUnknown(char.code)).length;
|
|
27
|
+
if (bad >= MIN_BAD_CHARS && bad >= visible.length * BAD_CHAR_SHARE)
|
|
28
|
+
return { isTrusted: false, reason: "garbled text layer" };
|
|
29
|
+
const lettered = words.filter((word) => LETTER.test(word.text));
|
|
30
|
+
const mixed = lettered.filter((word) => isMixedWord(word.text)).length;
|
|
31
|
+
if (mixed >= MIN_MIXED_WORDS && mixed >= lettered.length * MIXED_WORD_SHARE)
|
|
32
|
+
return { isTrusted: false, reason: "garbled text layer" };
|
|
33
|
+
return { isTrusted: true };
|
|
34
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Attachment, Place, Placement } from "./types.js";
|
|
2
|
+
import type { Part, View } from "./view.js";
|
|
3
|
+
/** NOTE: owns the caller's own elements held at a place on a page: over it, scaling with the page, or beside it at their own size. */
|
|
4
|
+
export type Attachments = Part & {
|
|
5
|
+
/** NOTE: puts the caller's element at a place on a page and keeps it there; the element's content and behaviour stay the caller's. */
|
|
6
|
+
attach(node: HTMLElement, place: Place, options?: {
|
|
7
|
+
placement?: Placement;
|
|
8
|
+
gap?: number;
|
|
9
|
+
}): Attachment;
|
|
10
|
+
};
|
|
11
|
+
export declare function createAttachments(view: View): Attachments;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { clamp, placeOver } from "./dom.js";
|
|
2
|
+
import { pageLayer } from "./page-layers.js";
|
|
3
|
+
const PANE_MARGIN = 4;
|
|
4
|
+
const DEFAULT_GAP = 6;
|
|
5
|
+
export function createAttachments(view) {
|
|
6
|
+
const floats = new Set();
|
|
7
|
+
const own = new Set();
|
|
8
|
+
/** NOTE: beside its place at its own size; it flips to the other side when its side would leave the pane. */
|
|
9
|
+
const placeFloat = (float) => {
|
|
10
|
+
const at = view.inContent(float.place.page, float.place.box);
|
|
11
|
+
if (!at)
|
|
12
|
+
return;
|
|
13
|
+
const size = { width: float.element.offsetWidth, height: float.element.offsetHeight };
|
|
14
|
+
const pane = view.paneBox();
|
|
15
|
+
const fitsAbove = at.y - float.gap - size.height >= pane.y;
|
|
16
|
+
const fitsBelow = at.y + at.height + float.gap + size.height <= pane.y + pane.height;
|
|
17
|
+
let placement = float.placement;
|
|
18
|
+
if (placement === "above" && !fitsAbove && fitsBelow)
|
|
19
|
+
placement = "below";
|
|
20
|
+
else if (placement === "below" && !fitsBelow && fitsAbove)
|
|
21
|
+
placement = "above";
|
|
22
|
+
let x = at.x + at.width / 2 - size.width / 2;
|
|
23
|
+
let y = at.y + at.height / 2 - size.height / 2;
|
|
24
|
+
if (placement === "above")
|
|
25
|
+
y = at.y - float.gap - size.height;
|
|
26
|
+
else if (placement === "below")
|
|
27
|
+
y = at.y + at.height + float.gap;
|
|
28
|
+
else if (placement === "left")
|
|
29
|
+
x = at.x - float.gap - size.width;
|
|
30
|
+
else
|
|
31
|
+
x = at.x + at.width + float.gap;
|
|
32
|
+
if (placement === "above" || placement === "below") {
|
|
33
|
+
const least = pane.x + PANE_MARGIN;
|
|
34
|
+
x = clamp(x, least, Math.max(least, pane.x + pane.width - size.width - PANE_MARGIN));
|
|
35
|
+
}
|
|
36
|
+
float.element.style.transform = `translate(${Math.round(x)}px, ${Math.round(y)}px)`;
|
|
37
|
+
float.element.dataset.placement = placement;
|
|
38
|
+
};
|
|
39
|
+
const floatObserver = new ResizeObserver((entries) => {
|
|
40
|
+
for (const entry of entries) {
|
|
41
|
+
for (const float of floats)
|
|
42
|
+
if (float.element === entry.target)
|
|
43
|
+
placeFloat(float);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const unwatch = view.onChange(() => {
|
|
47
|
+
for (const float of floats)
|
|
48
|
+
placeFloat(float);
|
|
49
|
+
});
|
|
50
|
+
const letGo = (float) => {
|
|
51
|
+
floats.delete(float);
|
|
52
|
+
floatObserver.unobserve(float.element);
|
|
53
|
+
float.element.classList.remove("pm-float");
|
|
54
|
+
float.element.style.transform = "";
|
|
55
|
+
float.element.remove();
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
attach(node, place, options) {
|
|
59
|
+
const placement = options?.placement ?? "over";
|
|
60
|
+
if (placement === "over") {
|
|
61
|
+
const put = (to) => {
|
|
62
|
+
const page = view.pages[to.page];
|
|
63
|
+
if (!page)
|
|
64
|
+
return;
|
|
65
|
+
placeOver(node, to.box, page.size);
|
|
66
|
+
const over = pageLayer(page, "attached");
|
|
67
|
+
own.add(over);
|
|
68
|
+
over.append(node);
|
|
69
|
+
};
|
|
70
|
+
node.classList.add("pm-attached");
|
|
71
|
+
put(place);
|
|
72
|
+
return {
|
|
73
|
+
move: put,
|
|
74
|
+
remove: () => {
|
|
75
|
+
node.classList.remove("pm-attached");
|
|
76
|
+
node.remove();
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const float = { element: node, place, placement, gap: options?.gap ?? DEFAULT_GAP };
|
|
81
|
+
node.classList.add("pm-float");
|
|
82
|
+
view.floats.append(node);
|
|
83
|
+
floats.add(float);
|
|
84
|
+
floatObserver.observe(node);
|
|
85
|
+
placeFloat(float);
|
|
86
|
+
return {
|
|
87
|
+
move: (to) => {
|
|
88
|
+
float.place = to;
|
|
89
|
+
placeFloat(float);
|
|
90
|
+
},
|
|
91
|
+
remove: () => letGo(float),
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
destroy() {
|
|
95
|
+
unwatch();
|
|
96
|
+
for (const float of [...floats])
|
|
97
|
+
letGo(float);
|
|
98
|
+
floatObserver.disconnect();
|
|
99
|
+
for (const over of own)
|
|
100
|
+
over.remove();
|
|
101
|
+
own.clear();
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|
package/viewer/dom.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Box } from "../core/index.js";
|
|
2
|
+
export declare function element(className: string): HTMLDivElement;
|
|
3
|
+
export declare function clamp(value: number, low: number, high: number): number;
|
|
4
|
+
export declare function unionOf(rects: ReadonlyArray<Box>): Box | null;
|
|
5
|
+
/** NOTE: a box placed over a page by percentages of the page's own size, so it holds its place at any zoom. */
|
|
6
|
+
export declare function placeOver(node: HTMLElement, box: Box, page: {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
}): void;
|
|
10
|
+
/**
|
|
11
|
+
* NOTE: how a move the viewer makes itself is shown: gliding, or at once for a reader whose system asks for less
|
|
12
|
+
* motion. It is asked each time, since a reader can change the setting while a document is open.
|
|
13
|
+
*/
|
|
14
|
+
export declare function glide(): ScrollBehavior;
|
package/viewer/dom.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export function element(className) {
|
|
2
|
+
const node = document.createElement("div");
|
|
3
|
+
node.className = className;
|
|
4
|
+
return node;
|
|
5
|
+
}
|
|
6
|
+
export function clamp(value, low, high) {
|
|
7
|
+
return Math.min(high, Math.max(low, value));
|
|
8
|
+
}
|
|
9
|
+
export function unionOf(rects) {
|
|
10
|
+
const first = rects[0];
|
|
11
|
+
if (!first)
|
|
12
|
+
return null;
|
|
13
|
+
let left = first.x;
|
|
14
|
+
let top = first.y;
|
|
15
|
+
let right = first.x + first.width;
|
|
16
|
+
let bottom = first.y + first.height;
|
|
17
|
+
for (const rect of rects) {
|
|
18
|
+
left = Math.min(left, rect.x);
|
|
19
|
+
top = Math.min(top, rect.y);
|
|
20
|
+
right = Math.max(right, rect.x + rect.width);
|
|
21
|
+
bottom = Math.max(bottom, rect.y + rect.height);
|
|
22
|
+
}
|
|
23
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
24
|
+
}
|
|
25
|
+
/** NOTE: a box placed over a page by percentages of the page's own size, so it holds its place at any zoom. */
|
|
26
|
+
export function placeOver(node, box, page) {
|
|
27
|
+
node.style.left = `${(box.x / page.width) * 100}%`;
|
|
28
|
+
node.style.top = `${(box.y / page.height) * 100}%`;
|
|
29
|
+
node.style.width = `${(box.width / page.width) * 100}%`;
|
|
30
|
+
node.style.height = `${(box.height / page.height) * 100}%`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* NOTE: how a move the viewer makes itself is shown: gliding, or at once for a reader whose system asks for less
|
|
34
|
+
* motion. It is asked each time, since a reader can change the setting while a document is open.
|
|
35
|
+
*/
|
|
36
|
+
export function glide() {
|
|
37
|
+
return window.matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth";
|
|
38
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: the events of one thing: who listens to each, and telling them. Each event has a name and the values its
|
|
3
|
+
* listeners are called with, so a listener is checked against the event it asks for.
|
|
4
|
+
* INVARIANT: listeners hear an event in the order they asked. One taken away while an event is being told still
|
|
5
|
+
* hears that one, so no listener's leaving changes what another hears.
|
|
6
|
+
*/
|
|
7
|
+
export type Events<Told extends {
|
|
8
|
+
[Name in keyof Told]: unknown[];
|
|
9
|
+
}> = {
|
|
10
|
+
/** NOTE: calls `listener` each time the event is told, until the function it returns is called. */
|
|
11
|
+
on<Name extends keyof Told>(name: Name, listener: (...values: Told[Name]) => void): () => void;
|
|
12
|
+
emit<Name extends keyof Told>(name: Name, ...values: Told[Name]): void;
|
|
13
|
+
clear(): void;
|
|
14
|
+
};
|
|
15
|
+
export declare function createEvents<Told extends {
|
|
16
|
+
[Name in keyof Told]: unknown[];
|
|
17
|
+
}>(): Events<Told>;
|
package/viewer/events.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function createEvents() {
|
|
2
|
+
const asked = new Map();
|
|
3
|
+
return {
|
|
4
|
+
/** NOTE: each asking is kept as its own entry, so the same function asked for twice hears twice and stops one at a time. */
|
|
5
|
+
on(name, listener) {
|
|
6
|
+
const asking = { listener: listener };
|
|
7
|
+
asked.set(name, [...(asked.get(name) ?? []), asking]);
|
|
8
|
+
return () => {
|
|
9
|
+
asked.set(name, (asked.get(name) ?? []).filter((each) => each !== asking));
|
|
10
|
+
};
|
|
11
|
+
},
|
|
12
|
+
emit(name, ...values) {
|
|
13
|
+
for (const { listener } of asked.get(name) ?? [])
|
|
14
|
+
listener(...values);
|
|
15
|
+
},
|
|
16
|
+
clear() {
|
|
17
|
+
asked.clear();
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { createViewer } from "./viewer.js";
|
|
2
|
+
export { viewOver } from "./view-over.js";
|
|
3
|
+
export { createAttachments } from "./attachments.js";
|
|
4
|
+
export { createLinks } from "./links.js";
|
|
5
|
+
export { createMarks } from "./marks.js";
|
|
6
|
+
export { createPictures } from "./pictures.js";
|
|
7
|
+
export { createPointer } from "./pointer.js";
|
|
8
|
+
export { createText } from "./text.js";
|
|
9
|
+
export type { Attachments } from "./attachments.js";
|
|
10
|
+
export type { Links } from "./links.js";
|
|
11
|
+
export type { MarkOptions, Marks } from "./marks.js";
|
|
12
|
+
export type { PictureOptions, Pictures } from "./pictures.js";
|
|
13
|
+
export type { Pointer, PointerOptions } from "./pointer.js";
|
|
14
|
+
export type { SelectableText } from "./text.js";
|
|
15
|
+
export type { Attachment, PagePoint, Place, Placement, ScreenBox, Viewer, ViewerDrag, ViewerEvents, ViewerFit, ViewerLink, ViewerMark, ViewerOptions, ViewerPage, ViewerRotation, ViewerSpread, ViewerTextLine, ViewMove } from "./types.js";
|
|
16
|
+
export type { Part, View, ViewChange, ViewPage, ViewShift } from "./view.js";
|
|
17
|
+
export type { FrameOptions, FramePage } from "./view-over.js";
|
package/viewer/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { createViewer } from "./viewer.js";
|
|
2
|
+
export { viewOver } from "./view-over.js";
|
|
3
|
+
export { createAttachments } from "./attachments.js";
|
|
4
|
+
export { createLinks } from "./links.js";
|
|
5
|
+
export { createMarks } from "./marks.js";
|
|
6
|
+
export { createPictures } from "./pictures.js";
|
|
7
|
+
export { createPointer } from "./pointer.js";
|
|
8
|
+
export { createText } from "./text.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ViewerLink } from "./types.js";
|
|
2
|
+
import type { Part, View } from "./view.js";
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: owns the links over the pages. Each is an anchor of the page it lies on, so the browser itself opens it, a
|
|
5
|
+
* keyboard reaches it, and a screen reader announces it.
|
|
6
|
+
* CONSTRAINT: an address is checked here as well as where the reading was made, since the links come from the caller
|
|
7
|
+
* and the caller may have them from anywhere. One that fails is not drawn.
|
|
8
|
+
*/
|
|
9
|
+
export type Links = Part & {
|
|
10
|
+
/**
|
|
11
|
+
* NOTE: lays links over the pages. One with an address opens it in a new tab, and only when the address is a web,
|
|
12
|
+
* mail or telephone address; one to a page goes there.
|
|
13
|
+
*/
|
|
14
|
+
setLinks(links: ReadonlyArray<ViewerLink>): void;
|
|
15
|
+
};
|
|
16
|
+
export declare function createLinks(view: View): Links;
|
package/viewer/links.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { safeLinkUrl } from "../core/index.js";
|
|
2
|
+
import { placeOver } from "./dom.js";
|
|
3
|
+
import { pageLayer } from "./page-layers.js";
|
|
4
|
+
export function createLinks(view) {
|
|
5
|
+
const hosts = new Map();
|
|
6
|
+
const hostOf = (index) => {
|
|
7
|
+
const page = view.pages[index];
|
|
8
|
+
if (!page)
|
|
9
|
+
return null;
|
|
10
|
+
let host = hosts.get(index);
|
|
11
|
+
if (!host) {
|
|
12
|
+
host = pageLayer(page, "links");
|
|
13
|
+
hosts.set(index, host);
|
|
14
|
+
}
|
|
15
|
+
return host;
|
|
16
|
+
};
|
|
17
|
+
return {
|
|
18
|
+
setLinks(links) {
|
|
19
|
+
for (const host of hosts.values())
|
|
20
|
+
host.replaceChildren();
|
|
21
|
+
for (const link of links) {
|
|
22
|
+
const page = view.pages[link.page];
|
|
23
|
+
const host = hostOf(link.page);
|
|
24
|
+
if (!page || !host)
|
|
25
|
+
continue;
|
|
26
|
+
const anchor = document.createElement("a");
|
|
27
|
+
anchor.className = "pm-link";
|
|
28
|
+
anchor.draggable = false;
|
|
29
|
+
if ("url" in link) {
|
|
30
|
+
const url = safeLinkUrl(link.url);
|
|
31
|
+
if (!url)
|
|
32
|
+
continue;
|
|
33
|
+
anchor.href = url;
|
|
34
|
+
anchor.target = "_blank";
|
|
35
|
+
anchor.rel = "noopener noreferrer nofollow";
|
|
36
|
+
anchor.setAttribute("aria-label", link.label ?? url);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const to = link.toPage;
|
|
40
|
+
if (!view.pages[to])
|
|
41
|
+
continue;
|
|
42
|
+
anchor.href = `#page-${to + 1}`;
|
|
43
|
+
anchor.setAttribute("aria-label", link.label ?? `Page ${to + 1}`);
|
|
44
|
+
anchor.addEventListener("click", (event) => {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
view.goToPage(to);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
placeOver(anchor, link.box, page.size);
|
|
50
|
+
host.append(anchor);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
destroy() {
|
|
54
|
+
for (const host of hosts.values())
|
|
55
|
+
host.remove();
|
|
56
|
+
hosts.clear();
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { PagePoint, ViewerMark } from "./types.js";
|
|
2
|
+
import type { Part, View } from "./view.js";
|
|
3
|
+
/** NOTE: owns the marks drawn over the pages, kept in named layers so one can change without the rest. */
|
|
4
|
+
export type Marks = Part & {
|
|
5
|
+
/** NOTE: replaces the marks of one named layer. */
|
|
6
|
+
setLayer(layer: string, marks: ReadonlyArray<ViewerMark>): void;
|
|
7
|
+
/** NOTE: several marks may be in focus at once, as the pieces of one change are; `reveal` brings the first into view. */
|
|
8
|
+
focusMark(id: string | ReadonlyArray<string> | null, options?: {
|
|
9
|
+
reveal?: boolean;
|
|
10
|
+
}): void;
|
|
11
|
+
/** NOTE: the marks a point lies on, each as it was set. */
|
|
12
|
+
marksAt(point: PagePoint): ViewerMark[];
|
|
13
|
+
};
|
|
14
|
+
export type MarkOptions = {
|
|
15
|
+
/** NOTE: a mark pressed with the keyboard, told as a pointer's press on it would be: the middle of the mark, and the marks there. */
|
|
16
|
+
onPress?: (point: PagePoint, marks: ReadonlyArray<ViewerMark>) => void;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* NOTE: a mark with a label is a button a keyboard reaches. All of them are one stop for the Tab key, however many
|
|
20
|
+
* there are: Tab comes to one mark, the arrow keys and Home and End step between them in reading order, and Enter
|
|
21
|
+
* or the space bar presses the one in hand. Stepping brings a mark into view and changes nothing else, so which
|
|
22
|
+
* mark the caller has in focus stays the caller's to say, as it is under a pointer.
|
|
23
|
+
*/
|
|
24
|
+
export declare function createMarks(view: View, options?: MarkOptions): Marks;
|