@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/react/hooks.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { BoxSelection, HiddenWords, LineHover, linkViewers, NO_VIEWER, ParagraphComparison, Search, viewerState, WordComparison, WordSelection } from "../behaviours/index.js";
|
|
2
|
+
import { NOT_OPEN, openDocument } from "../document/index.js";
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* NOTE: every hook here only binds a behaviour from `pagemark-behaviours` to React's state. None holds logic of its
|
|
6
|
+
* own, so another framework gets the same behaviour by writing the same few lines of binding.
|
|
7
|
+
* NOTE: a behaviour is made while rendering and never closed. That is safe because a behaviour holds on to nothing
|
|
8
|
+
* until it is listened to, and lets go when the last listener leaves.
|
|
9
|
+
*/
|
|
10
|
+
const NO_SUBSCRIPTION = () => () => undefined;
|
|
11
|
+
export function useReadable(readable, fallback) {
|
|
12
|
+
const get = readable ? readable.get : () => fallback;
|
|
13
|
+
return useSyncExternalStore(readable ? readable.subscribe : NO_SUBSCRIPTION, get, get);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* NOTE: an open document made for `key` and closed, with its source, when the key changes or the component goes. The
|
|
17
|
+
* key names what is open, as a document's id does, so a caller keeps no identity of its own. The document is null
|
|
18
|
+
* until it is made, and for a null key.
|
|
19
|
+
*/
|
|
20
|
+
export function useDocument(key, source, options = {}) {
|
|
21
|
+
const { all, batch, pollMs } = options;
|
|
22
|
+
const [document, setDocument] = useState(null);
|
|
23
|
+
const make = useRef(source);
|
|
24
|
+
make.current = source;
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (key === null)
|
|
27
|
+
return undefined;
|
|
28
|
+
const opened = openDocument(make.current(), { all, batch, pollMs });
|
|
29
|
+
setDocument(opened);
|
|
30
|
+
return () => {
|
|
31
|
+
opened.close();
|
|
32
|
+
setDocument(null);
|
|
33
|
+
};
|
|
34
|
+
}, [key, all, batch, pollMs]);
|
|
35
|
+
return document;
|
|
36
|
+
}
|
|
37
|
+
/** NOTE: how far a document has come: its status, its pages' sizes, and the pages with whatever words have arrived. */
|
|
38
|
+
export function useDocumentState(document) {
|
|
39
|
+
return useReadable(document, NOT_OPEN);
|
|
40
|
+
}
|
|
41
|
+
function useBaseBehaviour(Type, document, inputs) {
|
|
42
|
+
const behaviour = useMemo(() => new Type(document), [Type, document]);
|
|
43
|
+
const stateFor = useCallback(() => behaviour.stateFor(inputs), [behaviour, inputs]);
|
|
44
|
+
const state = useSyncExternalStore(behaviour.subscribe, stateFor, stateFor);
|
|
45
|
+
useEffect(() => behaviour.set(inputs), [behaviour, inputs]);
|
|
46
|
+
return [state, behaviour];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* NOTE: looks for `quote` in a document: the places it occurs, their marks, the one in focus, and the commands that
|
|
50
|
+
* step through them. A new document starts a new search.
|
|
51
|
+
*/
|
|
52
|
+
export function useSearch(document, quote) {
|
|
53
|
+
const [state, search] = useBaseBehaviour(Search, document, quote);
|
|
54
|
+
return useMemo(() => ({ ...state, next: search.next, previous: search.previous, focus: search.focus }), [state, search]);
|
|
55
|
+
}
|
|
56
|
+
/** NOTE: the line under the pointer, described, with its mark. Hand `onPointer` to the viewer. */
|
|
57
|
+
export function useLineHover(document) {
|
|
58
|
+
const [state, hover] = useBaseBehaviour(LineHover, document);
|
|
59
|
+
return useMemo(() => ({ ...state, onPointer: hover.onPointer }), [state, hover]);
|
|
60
|
+
}
|
|
61
|
+
/** NOTE: the words a drag runs over, in reading order, with their text and marks. Hand `onDrag` to the viewer. */
|
|
62
|
+
export function useWordSelection(document) {
|
|
63
|
+
const [state, selection] = useBaseBehaviour(WordSelection, document);
|
|
64
|
+
return useMemo(() => ({ ...state, onDrag: selection.onDrag, clear: selection.clear }), [state, selection]);
|
|
65
|
+
}
|
|
66
|
+
/** NOTE: a box a reader drags, and the words inside it. Hand `onDrag` to the viewer. */
|
|
67
|
+
export function useBoxSelection(document) {
|
|
68
|
+
const [state, box] = useBaseBehaviour(BoxSelection, document);
|
|
69
|
+
return useMemo(() => ({ ...state, onDrag: box.onDrag, clear: box.clear }), [state, box]);
|
|
70
|
+
}
|
|
71
|
+
/** NOTE: the words a reader cannot see, in runs, with a mark for each and their count. */
|
|
72
|
+
export function useHiddenWords(document) {
|
|
73
|
+
return useBaseBehaviour(HiddenWords, document)[0];
|
|
74
|
+
}
|
|
75
|
+
/** NOTE: binds a comparison of two documents as the base hook binds a behaviour of one. A new document on either side starts a new comparison. */
|
|
76
|
+
function useBaseComparison(Type, ...[before, after]) {
|
|
77
|
+
const comparison = useMemo(() => new Type(before, after), [Type, before, after]);
|
|
78
|
+
const state = useReadable(comparison);
|
|
79
|
+
return useMemo(() => ({ ...state, next: comparison.next, previous: comparison.previous, focus: comparison.focus }), [state, comparison]);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* NOTE: what changed between two versions, told as runs of words: the changes, each side's marks, the change in
|
|
83
|
+
* hand and the ids of its marks on each side, and the commands that step through the changes.
|
|
84
|
+
* NOTE: it fetches every word of both versions itself, and `isPartial` says while it waits, so a caller neither
|
|
85
|
+
* opens a version whole nor watches for it to be complete.
|
|
86
|
+
*/
|
|
87
|
+
export function useComparison(...versions) {
|
|
88
|
+
return useBaseComparison(WordComparison, ...versions);
|
|
89
|
+
}
|
|
90
|
+
/** NOTE: what changed between two versions, told by paragraph, as `useComparison` tells it by words. */
|
|
91
|
+
export function useParagraphComparison(...versions) {
|
|
92
|
+
return useBaseComparison(ParagraphComparison, ...versions);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* NOTE: what a bar over a viewer needs, in one object: the viewer's zoom, what its zoom is held to, how far its
|
|
96
|
+
* pages are turned, how many stand side by side, the page in view and how many pages there are, and the commands that change them. A bar of your own shows the first and binds
|
|
97
|
+
* its buttons to the second, and holds nothing itself.
|
|
98
|
+
* NOTE: with no viewer yet, the state is at rest and the commands do nothing.
|
|
99
|
+
* NOTE: the commands keep their identity while the state changes, and take nothing a button's press would hand
|
|
100
|
+
* them, so one can be given to a button as it is.
|
|
101
|
+
*/
|
|
102
|
+
export function useViewerControls(viewer) {
|
|
103
|
+
const readable = useMemo(() => (viewer ? viewerState(viewer) : null), [viewer]);
|
|
104
|
+
const state = useReadable(readable, NO_VIEWER);
|
|
105
|
+
const commands = useMemo(() => ({
|
|
106
|
+
zoomIn: () => viewer?.zoomIn(),
|
|
107
|
+
zoomOut: () => viewer?.zoomOut(),
|
|
108
|
+
zoomTo: (zoom) => viewer?.zoomTo(zoom),
|
|
109
|
+
fitWidth: () => viewer?.fitWidth(),
|
|
110
|
+
fitPage: () => viewer?.fitPage(),
|
|
111
|
+
rotateLeft: () => viewer?.rotateLeft(),
|
|
112
|
+
rotateRight: () => viewer?.rotateRight(),
|
|
113
|
+
setSpread: (spread) => viewer?.setSpread(spread),
|
|
114
|
+
goToPage: (page) => viewer?.goToPage(page),
|
|
115
|
+
}), [viewer]);
|
|
116
|
+
return useMemo(() => ({ ...state, ...commands }), [state, commands]);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* NOTE: keeps viewers side by side in step: a zoom in one is the same zoom in the others, and a reader's scroll in
|
|
120
|
+
* one moves the others as far, each keeping its own place. `scroll: false` links the zoom alone.
|
|
121
|
+
* CONSTRAINT: the list holds the same number of viewers on every render; one that is not built yet is null.
|
|
122
|
+
*/
|
|
123
|
+
export function useLinkedViewers(viewers, options = {}) {
|
|
124
|
+
const { scroll } = options;
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
const built = viewers.filter((viewer) => viewer !== null);
|
|
127
|
+
return built.length > 1 ? linkViewers(built, { scroll }) : undefined;
|
|
128
|
+
}, [...viewers, scroll]);
|
|
129
|
+
}
|
package/react/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { useBoxSelection, useComparison, useDocument, useDocumentState, useHiddenWords, useLineHover, useLinkedViewers, useParagraphComparison, useReadable, useSearch, useViewerControls, useWordSelection, } from "./hooks.js";
|
|
2
|
+
export { PagemarkAttachment, PagemarkViewer } from "./viewer.js";
|
|
3
|
+
export type { PagemarkAttachmentProps, PagemarkViewerProps } from "./viewer.js";
|
package/react/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { useBoxSelection, useComparison, useDocument, useDocumentState, useHiddenWords, useLineHover, useLinkedViewers, useParagraphComparison, useReadable, useSearch, useViewerControls, useWordSelection, } from "./hooks.js";
|
|
2
|
+
export { PagemarkAttachment, PagemarkViewer } from "./viewer.js";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Box } from "../core/index.js";
|
|
2
|
+
import { type PagemarkDocument } from "../document/index.js";
|
|
3
|
+
import { type ReactNode } from "react";
|
|
4
|
+
import { type Placement, type Viewer, type ViewerEvents, type ViewerLink, type ViewerMark, type ViewerOptions, type ViewerTextLine } from "../viewer/index.js";
|
|
5
|
+
/**
|
|
6
|
+
* NOTE: a viewer is given an open `document`, or `pages` and `imageUrl` of the caller's own. A document brings its
|
|
7
|
+
* pages' sizes, its pictures, the text a reader selects and its links, and is told when the screen is idle.
|
|
8
|
+
* Anything given beside it replaces what it would bring: `text={null}` shows no text to select, as for a tool that
|
|
9
|
+
* drags a box over words.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* NOTE: a prop for each of the viewer's events, named `on` and the event: `onZoom`, `onPress`, `onView`. Each is
|
|
13
|
+
* called with what the event tells. They are made from the viewer's own list of events, so the two cannot differ.
|
|
14
|
+
*/
|
|
15
|
+
export type PagemarkViewerEvents = {
|
|
16
|
+
[Name in keyof ViewerEvents as `on${Capitalize<Name>}`]?: (...values: ViewerEvents[Name]) => void;
|
|
17
|
+
};
|
|
18
|
+
export type PagemarkViewerProps = Omit<ViewerOptions, "pages" | "imageUrl"> & PagemarkViewerEvents & {
|
|
19
|
+
document?: PagemarkDocument | null;
|
|
20
|
+
pages?: ViewerOptions["pages"];
|
|
21
|
+
imageUrl?: ViewerOptions["imageUrl"];
|
|
22
|
+
marks?: ReadonlyArray<ViewerMark>;
|
|
23
|
+
/** NOTE: further named layers of marks, each replaced on its own when its list changes. */
|
|
24
|
+
layers?: Readonly<Record<string, ReadonlyArray<ViewerMark>>>;
|
|
25
|
+
/** NOTE: the mark, or the marks, drawn in the focus colour; the first is scrolled to the middle of the pane. */
|
|
26
|
+
focusedMarkId?: string | ReadonlyArray<string> | null;
|
|
27
|
+
/** NOTE: the lines of each page a reader can select and copy. Left out, they are the document's; with no document, the pages have no text to select. */
|
|
28
|
+
text?: ReadonlyArray<ReadonlyArray<ViewerTextLine> | undefined> | null;
|
|
29
|
+
/** NOTE: the links laid over the pages. Left out, they are the document's. */
|
|
30
|
+
links?: ReadonlyArray<ViewerLink>;
|
|
31
|
+
onViewer?: (viewer: Viewer | null) => void;
|
|
32
|
+
className?: string;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* CONSTRAINT: `pages`, `imageUrl` and `tileUrl`, when given, must keep their identity between renders, since a new
|
|
36
|
+
* one builds the viewer again. A document's own keep theirs.
|
|
37
|
+
*/
|
|
38
|
+
export declare function PagemarkViewer(props: PagemarkViewerProps): import("react").JSX.Element;
|
|
39
|
+
export type PagemarkAttachmentProps = {
|
|
40
|
+
viewer: Viewer | null;
|
|
41
|
+
page: number;
|
|
42
|
+
box: Box;
|
|
43
|
+
placement?: Placement;
|
|
44
|
+
gap?: number;
|
|
45
|
+
children: ReactNode;
|
|
46
|
+
};
|
|
47
|
+
/** NOTE: renders its children at a place on a page; what they are and how they behave is up to the caller. */
|
|
48
|
+
export declare function PagemarkAttachment({ viewer, page, box, placement, gap, children }: PagemarkAttachmentProps): import("react").ReactPortal;
|
package/react/viewer.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { textLines, viewerLinks } from "../behaviours/index.js";
|
|
3
|
+
import { NOT_OPEN } from "../document/index.js";
|
|
4
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
5
|
+
import { createPortal } from "react-dom";
|
|
6
|
+
import { createViewer } from "../viewer/index.js";
|
|
7
|
+
import { useReadable } from "./hooks.js";
|
|
8
|
+
const NO_LINKS = [];
|
|
9
|
+
/**
|
|
10
|
+
* CONSTRAINT: `pages`, `imageUrl` and `tileUrl`, when given, must keep their identity between renders, since a new
|
|
11
|
+
* one builds the viewer again. A document's own keep theirs.
|
|
12
|
+
*/
|
|
13
|
+
export function PagemarkViewer(props) {
|
|
14
|
+
const { document, scales, label, fit, marks, layers, focusedMarkId, onViewer, className, pages: ownPages, imageUrl: ownImageUrl, tileUrl: ownTileUrl, text: ownText, links: ownLinks, ...events } = props;
|
|
15
|
+
const read = useReadable(document ?? null, NOT_OPEN);
|
|
16
|
+
const pages = ownPages ?? read.sizes;
|
|
17
|
+
const imageUrl = ownImageUrl ?? document?.pageImage;
|
|
18
|
+
const tileUrl = ownTileUrl ?? document?.tile;
|
|
19
|
+
const brought = useMemo(() => (document ? { text: textLines(read.pages), links: viewerLinks(read.pages) } : null), [document, read.pages]);
|
|
20
|
+
const text = ownText === undefined ? (brought?.text ?? null) : ownText;
|
|
21
|
+
const links = ownLinks ?? brought?.links ?? NO_LINKS;
|
|
22
|
+
const host = useRef(null);
|
|
23
|
+
const viewer = useRef(null);
|
|
24
|
+
const drawnLayers = useRef(new Map());
|
|
25
|
+
const told = {
|
|
26
|
+
...events,
|
|
27
|
+
onViewer,
|
|
28
|
+
releaseImage: events.releaseImage ?? document?.release,
|
|
29
|
+
/** NOTE: the document fetches the words of the pages near the pane, so the text a reader selects is there when they arrive. */
|
|
30
|
+
onPagesNear: (pages) => {
|
|
31
|
+
document?.near(pages);
|
|
32
|
+
events.onPagesNear?.(pages);
|
|
33
|
+
},
|
|
34
|
+
onIdle: () => {
|
|
35
|
+
document?.whenIdle();
|
|
36
|
+
events.onIdle?.();
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
const handlers = useRef(told);
|
|
40
|
+
handlers.current = told;
|
|
41
|
+
/** NOTE: the fit a viewer opens with; a later one does not build the viewer again, since the reader may have zoomed since. */
|
|
42
|
+
const opensWith = useRef(fit);
|
|
43
|
+
opensWith.current = fit;
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (!host.current || !imageUrl || pages.length === 0)
|
|
46
|
+
return undefined;
|
|
47
|
+
const created = createViewer(host.current, { pages, imageUrl, tileUrl, scales, fit: opensWith.current, releaseImage: (url) => handlers.current.releaseImage?.(url) });
|
|
48
|
+
created.on("zoom", (zoom) => handlers.current.onZoom?.(zoom));
|
|
49
|
+
created.on("pageInView", (page) => handlers.current.onPageInView?.(page));
|
|
50
|
+
created.on("pointer", (point) => handlers.current.onPointer?.(point));
|
|
51
|
+
created.on("pointerMarks", (under, point) => handlers.current.onPointerMarks?.(under, point));
|
|
52
|
+
created.on("press", (point, under) => handlers.current.onPress?.(point, under));
|
|
53
|
+
created.on("drag", (drag) => handlers.current.onDrag?.(drag));
|
|
54
|
+
created.on("view", (move) => handlers.current.onView?.(move));
|
|
55
|
+
created.on("idle", () => handlers.current.onIdle?.());
|
|
56
|
+
created.on("failures", (pages) => handlers.current.onFailures?.(pages));
|
|
57
|
+
created.on("pagesNear", (pages) => handlers.current.onPagesNear?.(pages));
|
|
58
|
+
/** NOTE: an event tells of a change, so where the viewer starts is told to these two props here. */
|
|
59
|
+
handlers.current.onZoom?.(created.zoom);
|
|
60
|
+
handlers.current.onPageInView?.(created.pageInView);
|
|
61
|
+
viewer.current = created;
|
|
62
|
+
drawnLayers.current = new Map();
|
|
63
|
+
handlers.current.onViewer?.(created);
|
|
64
|
+
return () => {
|
|
65
|
+
created.destroy();
|
|
66
|
+
viewer.current = null;
|
|
67
|
+
handlers.current.onViewer?.(null);
|
|
68
|
+
};
|
|
69
|
+
}, [pages, imageUrl, tileUrl, scales]);
|
|
70
|
+
/** NOTE: a name is set on the pane and not given when the viewer is made, so a new name does not build the viewer again. */
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
const pane = viewer.current?.pane;
|
|
73
|
+
if (!pane || label === undefined)
|
|
74
|
+
return undefined;
|
|
75
|
+
pane.setAttribute("role", "group");
|
|
76
|
+
pane.setAttribute("aria-label", label);
|
|
77
|
+
return () => {
|
|
78
|
+
pane.removeAttribute("role");
|
|
79
|
+
pane.removeAttribute("aria-label");
|
|
80
|
+
};
|
|
81
|
+
}, [label, pages, imageUrl, tileUrl, scales]);
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
viewer.current?.setMarks(marks ?? []);
|
|
84
|
+
}, [marks, pages, imageUrl, tileUrl, scales]);
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
const current = viewer.current;
|
|
87
|
+
if (!current)
|
|
88
|
+
return;
|
|
89
|
+
const wanted = layers ?? {};
|
|
90
|
+
for (const name of drawnLayers.current.keys()) {
|
|
91
|
+
if (!(name in wanted)) {
|
|
92
|
+
current.setLayer(name, []);
|
|
93
|
+
drawnLayers.current.delete(name);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
for (const [name, list] of Object.entries(wanted)) {
|
|
97
|
+
if (drawnLayers.current.get(name) === list)
|
|
98
|
+
continue;
|
|
99
|
+
current.setLayer(name, list);
|
|
100
|
+
drawnLayers.current.set(name, list);
|
|
101
|
+
}
|
|
102
|
+
}, [layers, pages, imageUrl, tileUrl, scales]);
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
viewer.current?.setText(text ?? null);
|
|
105
|
+
}, [text, pages, imageUrl, tileUrl, scales]);
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
viewer.current?.setLinks(links ?? []);
|
|
108
|
+
}, [links, pages, imageUrl, tileUrl, scales]);
|
|
109
|
+
/** NOTE: a list of ids is a new list on every render, so the focus follows what the ids are and not which list holds them. */
|
|
110
|
+
const focusKey = typeof focusedMarkId === "string" ? focusedMarkId : (focusedMarkId ?? []).join("\n");
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
viewer.current?.focusMark(focusKey === "" ? null : focusKey.split("\n"), { reveal: true });
|
|
113
|
+
}, [focusKey, marks, pages, imageUrl, tileUrl, scales]);
|
|
114
|
+
return _jsx("div", { ref: host, className: className });
|
|
115
|
+
}
|
|
116
|
+
/** NOTE: renders its children at a place on a page; what they are and how they behave is up to the caller. */
|
|
117
|
+
export function PagemarkAttachment({ viewer, page, box, placement, gap, children }) {
|
|
118
|
+
const [host] = useState(() => document.createElement("div"));
|
|
119
|
+
const { x, y, width, height } = box;
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (!viewer)
|
|
122
|
+
return undefined;
|
|
123
|
+
const attachment = viewer.attach(host, { page, box: { x, y, width, height } }, { placement, gap });
|
|
124
|
+
return () => attachment.remove();
|
|
125
|
+
}, [viewer, host, page, x, y, width, height, placement, gap]);
|
|
126
|
+
return createPortal(children, host);
|
|
127
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { PageReading } from "../core/index.js";
|
|
2
|
+
import type { WordDraft } from "./seams.js";
|
|
3
|
+
/** NOTE: a page's text, words and lines from its words grouped line by line, in reading order. */
|
|
4
|
+
export declare function assemble(groups: ReadonlyArray<ReadonlyArray<WordDraft>>): Pick<PageReading, "text" | "words" | "lines">;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
function union(a, b) {
|
|
2
|
+
const x = Math.min(a.x, b.x);
|
|
3
|
+
const y = Math.min(a.y, b.y);
|
|
4
|
+
return { x, y, width: Math.max(a.x + a.width, b.x + b.width) - x, height: Math.max(a.y + a.height, b.y + b.height) - y };
|
|
5
|
+
}
|
|
6
|
+
/** NOTE: a page's text, words and lines from its words grouped line by line, in reading order. */
|
|
7
|
+
export function assemble(groups) {
|
|
8
|
+
const words = [];
|
|
9
|
+
const lines = [];
|
|
10
|
+
let text = "";
|
|
11
|
+
for (const group of groups) {
|
|
12
|
+
const first = group[0];
|
|
13
|
+
if (!first)
|
|
14
|
+
continue;
|
|
15
|
+
if (lines.length > 0)
|
|
16
|
+
text += "\n";
|
|
17
|
+
const line = { box: first.box, firstWord: words.length, lastWord: words.length };
|
|
18
|
+
group.forEach((draft, index) => {
|
|
19
|
+
if (index > 0)
|
|
20
|
+
text += " ";
|
|
21
|
+
line.box = union(line.box, draft.box);
|
|
22
|
+
line.lastWord = words.length;
|
|
23
|
+
words.push({ ...draft, line: lines.length, start: text.length, end: text.length + draft.text.length });
|
|
24
|
+
text += draft.text;
|
|
25
|
+
});
|
|
26
|
+
lines.push(line);
|
|
27
|
+
}
|
|
28
|
+
return { text, words, lines };
|
|
29
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { readLayerPage } from "./layer-page.js";
|
|
2
|
+
export type { LayerPage } from "./layer-page.js";
|
|
3
|
+
export { layerWords } from "./layer-words.js";
|
|
4
|
+
export { estimateEdges, settleEdges } from "./letter-edges.js";
|
|
5
|
+
export { hiddenWords, isUnpaintedLayer } from "./seen.js";
|
|
6
|
+
export { EngineStopped } from "./seams.js";
|
|
7
|
+
export type { Converter, OcrEngine, OpenPdf, PagePart, PdfChar, PdfEngine, PdfLink, PdfPageText, PdfRaster, WordDraft } from "./seams.js";
|
package/reader/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { PageLink, PageReading } from "../core/index.js";
|
|
2
|
+
import type { OpenPdf, PdfPageText } from "./seams.js";
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: what a page's own text layer gives. Trusted, it is a finished reading, hidden words flagged, and whether it
|
|
5
|
+
* is an earlier OCR's layer over a scan. Not trusted, it is the reason, and the page is left for OCR or left unread.
|
|
6
|
+
* `text` is what the engine read, for a caller that goes on to OCR the page. `links` are the page's links either way,
|
|
7
|
+
* since they lie over the page whatever its words are read from.
|
|
8
|
+
*/
|
|
9
|
+
export type LayerPage = {
|
|
10
|
+
isTrusted: true;
|
|
11
|
+
reading: PageReading;
|
|
12
|
+
isScanLayer: boolean;
|
|
13
|
+
text: PdfPageText;
|
|
14
|
+
links: PageLink[];
|
|
15
|
+
} | {
|
|
16
|
+
isTrusted: false;
|
|
17
|
+
reason: string;
|
|
18
|
+
size: {
|
|
19
|
+
index: number;
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
};
|
|
23
|
+
text: PdfPageText;
|
|
24
|
+
links: PageLink[];
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* NOTE: reads one page from its text layer alone, the same way wherever it runs: on a server before OCR is considered,
|
|
28
|
+
* and in a browser where there is no OCR to consider. It needs nothing but an open PDF.
|
|
29
|
+
*/
|
|
30
|
+
export declare function readLayerPage(pdf: OpenPdf, index: number): Promise<LayerPage>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { layerWords } from "./layer-words.js";
|
|
2
|
+
import { pageLinks } from "./links.js";
|
|
3
|
+
import { hiddenWords, isUnpaintedLayer } from "./seen.js";
|
|
4
|
+
import { trustLayer } from "./trust.js";
|
|
5
|
+
/** NOTE: how large a trusted page is drawn to look for words nobody can see: enough for fine print to leave a mark, and quick to draw. */
|
|
6
|
+
const SEEN_SCALE = 2;
|
|
7
|
+
/** NOTE: the share of an unpainted layer's words that must be seen in the picture for it to be an earlier OCR's reading of a scan, and not words hidden on a page. */
|
|
8
|
+
const SCAN_SEEN_SHARE = 0.5;
|
|
9
|
+
function round(value) {
|
|
10
|
+
return Math.round(value * 100) / 100;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* NOTE: reads one page from its text layer alone, the same way wherever it runs: on a server before OCR is considered,
|
|
14
|
+
* and in a browser where there is no OCR to consider. It needs nothing but an open PDF.
|
|
15
|
+
*/
|
|
16
|
+
export async function readLayerPage(pdf, index) {
|
|
17
|
+
const text = await pdf.readPage(index);
|
|
18
|
+
const size = { index, width: round(text.width), height: round(text.height) };
|
|
19
|
+
const layer = layerWords(text);
|
|
20
|
+
const trust = trustLayer(text, layer.words);
|
|
21
|
+
const links = pageLinks(text, pdf.pageCount);
|
|
22
|
+
if (!trust.isTrusted)
|
|
23
|
+
return { isTrusted: false, reason: trust.reason, size, text, links };
|
|
24
|
+
/** NOTE: looked at where the engine drew it when the engine lends it, so no copy of the page is made only to be thrown away. */
|
|
25
|
+
const hidden = pdf.withDrawnPage
|
|
26
|
+
? pdf.withDrawnPage(index, SEEN_SCALE, undefined, (raster) => hiddenWords(layer.words, text, raster))
|
|
27
|
+
: hiddenWords(layer.words, text, await pdf.drawPage(index, SEEN_SCALE));
|
|
28
|
+
const hiddenCount = hidden.filter(Boolean).length;
|
|
29
|
+
const isScanLayer = isUnpaintedLayer(text) && layer.words.length - hiddenCount >= layer.words.length * SCAN_SEEN_SHARE;
|
|
30
|
+
const reading = {
|
|
31
|
+
...size,
|
|
32
|
+
source: "layer",
|
|
33
|
+
...(isScanLayer ? { sourceReason: "written by an earlier OCR" } : {}),
|
|
34
|
+
...(hiddenCount > 0 ? { hiddenWords: hiddenCount } : {}),
|
|
35
|
+
...(links.length > 0 ? { links } : {}),
|
|
36
|
+
...layer,
|
|
37
|
+
words: layer.words.map((word, order) => (hidden[order] ? { ...word, hidden: hidden[order] } : word)),
|
|
38
|
+
};
|
|
39
|
+
return { isTrusted: true, reading, isScanLayer, text, links };
|
|
40
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { assemble } from "./assemble.js";
|
|
2
|
+
const LINE_FEED = 0x0a;
|
|
3
|
+
const CARRIAGE_RETURN = 0x0d;
|
|
4
|
+
function isLineBreak(code) {
|
|
5
|
+
return code === LINE_FEED || code === CARRIAGE_RETURN;
|
|
6
|
+
}
|
|
7
|
+
function isSeparator(code) {
|
|
8
|
+
return (code <= 0x20 ||
|
|
9
|
+
code === 0xa0 ||
|
|
10
|
+
code === 0x3000 ||
|
|
11
|
+
code === 0xfeff ||
|
|
12
|
+
code === 0xfffe ||
|
|
13
|
+
code === 0xffff ||
|
|
14
|
+
(code >= 0x2000 && code <= 0x200b));
|
|
15
|
+
}
|
|
16
|
+
function hasArea(char) {
|
|
17
|
+
return char.right > char.left && char.bottom > char.top;
|
|
18
|
+
}
|
|
19
|
+
function round(value) {
|
|
20
|
+
return Math.round(value * 100) / 100;
|
|
21
|
+
}
|
|
22
|
+
function union(a, b) {
|
|
23
|
+
const x = Math.min(a.x, b.x);
|
|
24
|
+
const y = Math.min(a.y, b.y);
|
|
25
|
+
return { x, y, width: Math.max(a.x + a.width, b.x + b.width) - x, height: Math.max(a.y + a.height, b.y + b.height) - y };
|
|
26
|
+
}
|
|
27
|
+
function wordBox(chars) {
|
|
28
|
+
const drawn = chars.filter(hasArea);
|
|
29
|
+
if (drawn.length === 0)
|
|
30
|
+
return null;
|
|
31
|
+
const left = Math.min(...drawn.map((char) => char.left));
|
|
32
|
+
const top = Math.min(...drawn.map((char) => char.top));
|
|
33
|
+
const right = Math.max(...drawn.map((char) => char.right));
|
|
34
|
+
const bottom = Math.max(...drawn.map((char) => char.bottom));
|
|
35
|
+
return { x: round(left), y: round(top), width: round(right - left), height: round(bottom - top) };
|
|
36
|
+
}
|
|
37
|
+
/** NOTE: left out when the characters do not advance left to right, as in turned or right-to-left text. */
|
|
38
|
+
function wordEdges(chars, box) {
|
|
39
|
+
const edges = [box.x];
|
|
40
|
+
for (let index = 1; index < chars.length; index += 1) {
|
|
41
|
+
const previous = chars[index - 1];
|
|
42
|
+
const char = chars[index];
|
|
43
|
+
const last = edges[edges.length - 1] ?? box.x;
|
|
44
|
+
if (!previous || !char || !hasArea(char)) {
|
|
45
|
+
edges.push(last);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const edge = hasArea(previous) ? (previous.right + char.left) / 2 : char.left;
|
|
49
|
+
if (edge < last - 0.5)
|
|
50
|
+
return undefined;
|
|
51
|
+
edges.push(round(Math.max(edge, last)));
|
|
52
|
+
}
|
|
53
|
+
edges.push(round(box.x + box.width));
|
|
54
|
+
return edges;
|
|
55
|
+
}
|
|
56
|
+
function isOnLine(line, word) {
|
|
57
|
+
const overlap = Math.min(line.y + line.height, word.y + word.height) - Math.max(line.y, word.y);
|
|
58
|
+
const isLevel = overlap > 0.4 * Math.min(line.height, word.height);
|
|
59
|
+
const continues = word.x > line.x + line.width - 2 * line.height;
|
|
60
|
+
return isLevel && continues;
|
|
61
|
+
}
|
|
62
|
+
export function isSeparatorCode(code) {
|
|
63
|
+
return isSeparator(code);
|
|
64
|
+
}
|
|
65
|
+
export function layerWords(page) {
|
|
66
|
+
const groups = [];
|
|
67
|
+
let lineBox = null;
|
|
68
|
+
let pending = [];
|
|
69
|
+
let breaksLine = false;
|
|
70
|
+
const endWord = () => {
|
|
71
|
+
if (pending.length === 0)
|
|
72
|
+
return;
|
|
73
|
+
const chars = pending;
|
|
74
|
+
pending = [];
|
|
75
|
+
const box = wordBox(chars);
|
|
76
|
+
if (!box)
|
|
77
|
+
return;
|
|
78
|
+
const draft = { text: String.fromCodePoint(...chars.map((char) => char.code)), box, edges: wordEdges(chars, box) };
|
|
79
|
+
const current = groups[groups.length - 1];
|
|
80
|
+
if (!current || !lineBox || breaksLine || !isOnLine(lineBox, box)) {
|
|
81
|
+
groups.push([draft]);
|
|
82
|
+
lineBox = box;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
current.push(draft);
|
|
86
|
+
lineBox = union(lineBox, box);
|
|
87
|
+
}
|
|
88
|
+
breaksLine = false;
|
|
89
|
+
};
|
|
90
|
+
for (const char of page.chars) {
|
|
91
|
+
if (isSeparator(char.code)) {
|
|
92
|
+
endWord();
|
|
93
|
+
if (isLineBreak(char.code))
|
|
94
|
+
breaksLine = true;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
pending.push(char);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
endWord();
|
|
101
|
+
return assemble(groups);
|
|
102
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Box } from "../core/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: where each letter of a word starts and ends when only the word's box is known: one boundary more than the
|
|
4
|
+
* word has letters, from the box's left edge to its right, each letter given room by how wide it usually is.
|
|
5
|
+
*/
|
|
6
|
+
export declare function estimateEdges(text: string, box: Box): number[];
|
|
7
|
+
/**
|
|
8
|
+
* NOTE: an OCR engine's own letter boundaries, each kept where it is near the estimate and replaced by the estimate
|
|
9
|
+
* where it is not. Measured against the true boundaries of two scanned files, Tesseract's are closer than the
|
|
10
|
+
* estimate on average but more than a letter off about one time in ten, most often between a word's last letter and
|
|
11
|
+
* the punctuation after it; this keeps what it gets right and drops what it gets wrong.
|
|
12
|
+
*/
|
|
13
|
+
export declare function settleEdges(text: string, box: Box, measured: ReadonlyArray<number> | undefined): number[];
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: how wide a letter usually is beside an ordinary small one, in coarse groups that hold for serif and sans-serif
|
|
3
|
+
* print alike. Punctuation is its own group, narrower than the slim letters: counted with them, a full stop is given
|
|
4
|
+
* too much room and the boundary before it lands inside the word's last letter.
|
|
5
|
+
*/
|
|
6
|
+
const PUNCTUATION = new Set(".,;:'!|`\"");
|
|
7
|
+
const SLIM = new Set("iljtfI1()[]-");
|
|
8
|
+
const WIDE = new Set("mwMW@%&");
|
|
9
|
+
const PUNCTUATION_WIDTH = 0.45;
|
|
10
|
+
const SLIM_WIDTH = 0.55;
|
|
11
|
+
const WIDE_WIDTH = 1.5;
|
|
12
|
+
const CAPITAL_WIDTH = 1.3;
|
|
13
|
+
const CAPITAL = /\p{Lu}/u;
|
|
14
|
+
/** NOTE: how far from the estimate, in average letters of the word, a measured boundary may be and still be believed. */
|
|
15
|
+
const BELIEVED_WITHIN = 0.5;
|
|
16
|
+
function widthOf(letter) {
|
|
17
|
+
if (PUNCTUATION.has(letter))
|
|
18
|
+
return PUNCTUATION_WIDTH;
|
|
19
|
+
if (SLIM.has(letter))
|
|
20
|
+
return SLIM_WIDTH;
|
|
21
|
+
if (WIDE.has(letter))
|
|
22
|
+
return WIDE_WIDTH;
|
|
23
|
+
return CAPITAL.test(letter) ? CAPITAL_WIDTH : 1;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* NOTE: where each letter of a word starts and ends when only the word's box is known: one boundary more than the
|
|
27
|
+
* word has letters, from the box's left edge to its right, each letter given room by how wide it usually is.
|
|
28
|
+
*/
|
|
29
|
+
export function estimateEdges(text, box) {
|
|
30
|
+
const letters = [...text];
|
|
31
|
+
const total = letters.reduce((sum, letter) => sum + widthOf(letter), 0);
|
|
32
|
+
const edges = [box.x];
|
|
33
|
+
let used = 0;
|
|
34
|
+
for (const letter of letters) {
|
|
35
|
+
used += widthOf(letter);
|
|
36
|
+
edges.push(box.x + (box.width * used) / total);
|
|
37
|
+
}
|
|
38
|
+
if (letters.length > 0)
|
|
39
|
+
edges[letters.length] = box.x + box.width;
|
|
40
|
+
return edges;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* NOTE: an OCR engine's own letter boundaries, each kept where it is near the estimate and replaced by the estimate
|
|
44
|
+
* where it is not. Measured against the true boundaries of two scanned files, Tesseract's are closer than the
|
|
45
|
+
* estimate on average but more than a letter off about one time in ten, most often between a word's last letter and
|
|
46
|
+
* the punctuation after it; this keeps what it gets right and drops what it gets wrong.
|
|
47
|
+
*/
|
|
48
|
+
export function settleEdges(text, box, measured) {
|
|
49
|
+
const estimate = estimateEdges(text, box);
|
|
50
|
+
if (!measured || measured.length !== estimate.length)
|
|
51
|
+
return estimate;
|
|
52
|
+
const last = estimate.length - 1;
|
|
53
|
+
const believed = (BELIEVED_WITHIN * box.width) / Math.max(1, last);
|
|
54
|
+
return estimate.map((guess, index) => {
|
|
55
|
+
const edge = measured[index];
|
|
56
|
+
if (index === 0 || index === last || edge === undefined)
|
|
57
|
+
return guess;
|
|
58
|
+
return Math.abs(edge - guess) > believed ? guess : edge;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type PageLink } from "../core/index.js";
|
|
2
|
+
import type { PdfPageText } from "./seams.js";
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: the links of a page as a reading keeps them. One is left out when it has no area to press, when its address
|
|
5
|
+
* is not safe to follow, or when the page it leads to is not in the file.
|
|
6
|
+
*/
|
|
7
|
+
export declare function pageLinks(text: PdfPageText, pageCount: number): PageLink[];
|
package/reader/links.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { safeLinkUrl } from "../core/index.js";
|
|
2
|
+
function round(value) {
|
|
3
|
+
return Math.round(value * 100) / 100;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* NOTE: the links of a page as a reading keeps them. One is left out when it has no area to press, when its address
|
|
7
|
+
* is not safe to follow, or when the page it leads to is not in the file.
|
|
8
|
+
*/
|
|
9
|
+
export function pageLinks(text, pageCount) {
|
|
10
|
+
const links = [];
|
|
11
|
+
for (const link of text.links ?? []) {
|
|
12
|
+
const box = { x: round(link.left), y: round(link.top), width: round(link.right - link.left), height: round(link.bottom - link.top) };
|
|
13
|
+
if (box.width <= 0 || box.height <= 0)
|
|
14
|
+
continue;
|
|
15
|
+
const url = link.url === undefined ? null : safeLinkUrl(link.url);
|
|
16
|
+
if (url)
|
|
17
|
+
links.push({ box, url });
|
|
18
|
+
else if (link.url === undefined && link.page !== undefined && Number.isInteger(link.page) && link.page >= 0 && link.page < pageCount)
|
|
19
|
+
links.push({ box, page: link.page });
|
|
20
|
+
}
|
|
21
|
+
return links;
|
|
22
|
+
}
|