@scoriiu/fenshot 0.1.5 → 0.1.7
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/README.md +25 -2
- package/dist/detect.d.ts +24 -0
- package/dist/detect.js +167 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/reader/Library.d.ts +11 -0
- package/dist/reader/Library.js +74 -0
- package/dist/reader/PageView.d.ts +35 -0
- package/dist/reader/PageView.js +80 -0
- package/dist/reader/ReadApp.d.ts +9 -0
- package/dist/reader/ReadApp.js +122 -0
- package/dist/reader/Reader.d.ts +8 -0
- package/dist/reader/Reader.js +117 -0
- package/dist/reader/ReaderBar.d.ts +30 -0
- package/dist/reader/ReaderBar.js +36 -0
- package/dist/reader/SideGrip.d.ts +12 -0
- package/dist/reader/SideGrip.js +66 -0
- package/dist/reader/find.d.ts +42 -0
- package/dist/reader/find.js +279 -0
- package/dist/reader/host.d.ts +90 -0
- package/dist/reader/host.js +66 -0
- package/dist/reader/index.d.ts +3 -0
- package/dist/reader/index.js +3 -0
- package/dist/reader/pdf.d.ts +46 -0
- package/dist/reader/pdf.js +130 -0
- package/dist/reader/reader.css +1084 -0
- package/dist/reader/store.d.ts +63 -0
- package/dist/reader/store.js +103 -0
- package/dist/reader/useDiagrams.d.ts +18 -0
- package/dist/reader/useDiagrams.js +146 -0
- package/dist/reader/usePages.d.ts +18 -0
- package/dist/reader/usePages.js +148 -0
- package/dist/recognize.d.ts +8 -0
- package/dist/recognize.js +59 -20
- package/package.json +29 -7
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { useReaderHost, useT, useTrack } from "./host";
|
|
4
|
+
import { PageView } from "./PageView";
|
|
5
|
+
import { ReaderBar } from "./ReaderBar";
|
|
6
|
+
import { SideGrip, storedSide } from "./SideGrip";
|
|
7
|
+
import { same, useDiagrams } from "./useDiagrams";
|
|
8
|
+
import { usePages } from "./usePages";
|
|
9
|
+
/**
|
|
10
|
+
* A book open: its pages in one column (usePages), every diagram of it found in the background (useDiagrams), and
|
|
11
|
+
* a board beside the pages for the diagram the reader opened: with a click on it, with the bar's "next diagram",
|
|
12
|
+
* or with a double-click where the search found none.
|
|
13
|
+
*/
|
|
14
|
+
const EMPTY = "8/8/8/8/8/8/8/8";
|
|
15
|
+
/** The diagram to go to from where the reader is: from the open one, else from the page he is reading. */
|
|
16
|
+
function stepFrom(all, index, page, step) {
|
|
17
|
+
// Round the book: before the first is the last, after the last the first.
|
|
18
|
+
if (index >= 0)
|
|
19
|
+
return all[(index + step + all.length) % all.length];
|
|
20
|
+
if (step > 0)
|
|
21
|
+
return all.find((s) => s.page >= page) ?? all[0];
|
|
22
|
+
const before = all.filter((s) => s.page <= page);
|
|
23
|
+
return before.filter((s) => s.page < page).at(-1) ?? before.at(-1) ?? all.at(-1);
|
|
24
|
+
}
|
|
25
|
+
export function Reader({ book, doc, onLibrary, onProgress }) {
|
|
26
|
+
const t = useT();
|
|
27
|
+
const count = useTrack();
|
|
28
|
+
const { BoardPanel } = useReaderHost();
|
|
29
|
+
const view = usePages(doc, book.page);
|
|
30
|
+
const { page, goToDiagram } = view;
|
|
31
|
+
const { found, all, searched, readPoint, readIn } = useDiagrams(doc, book.id, page, view.pane);
|
|
32
|
+
/** The reader is drawing a box around a diagram that was not found. */
|
|
33
|
+
const [selecting, setSelecting] = useState(false);
|
|
34
|
+
const [sideW, setSideW] = useState(storedSide);
|
|
35
|
+
const [picked, setPicked] = useState(null);
|
|
36
|
+
const [busy, setBusy] = useState(null);
|
|
37
|
+
/** A diagram that could not be read, and where: the reader may set it up by hand. */
|
|
38
|
+
const [unread, setUnread] = useState(null);
|
|
39
|
+
useEffect(() => { const id = setTimeout(() => onProgress(page), 500); return () => clearTimeout(id); }, [page, onProgress]);
|
|
40
|
+
useEffect(() => { if (!unread)
|
|
41
|
+
return; const id = setTimeout(() => setUnread(null), 7000); return () => clearTimeout(id); }, [unread]);
|
|
42
|
+
const open = useCallback((n, diagram) => {
|
|
43
|
+
setPicked({ page: n, diagram });
|
|
44
|
+
count("read-pick", { sure: diagram.min >= 0.7 });
|
|
45
|
+
}, []);
|
|
46
|
+
// Read through a ref: a page is not drawn again each time another page's diagrams are found.
|
|
47
|
+
const foundNow = useRef(found);
|
|
48
|
+
foundNow.current = found;
|
|
49
|
+
const onPoint = useCallback((p) => {
|
|
50
|
+
const known = (foundNow.current.get(p.page) ?? []).find((d) => p.fx >= d.x0 && p.fx <= d.x1 && p.fy >= d.y0 && p.fy <= d.y1);
|
|
51
|
+
if (known)
|
|
52
|
+
return open(p.page, known);
|
|
53
|
+
setBusy(p);
|
|
54
|
+
void readPoint(p).then((diagram) => {
|
|
55
|
+
if (diagram)
|
|
56
|
+
open(p.page, diagram);
|
|
57
|
+
else {
|
|
58
|
+
setUnread(p);
|
|
59
|
+
count("read-nodiagram");
|
|
60
|
+
}
|
|
61
|
+
}).catch(() => setUnread(p)).finally(() => setBusy(null));
|
|
62
|
+
}, [open, readPoint]);
|
|
63
|
+
const onBox = useCallback((n, box) => {
|
|
64
|
+
const middle = { page: n, fx: (box.x0 + box.x1) / 2, fy: (box.y0 + box.y1) / 2 };
|
|
65
|
+
setSelecting(false);
|
|
66
|
+
setBusy(middle);
|
|
67
|
+
void readIn(n, box).then((diagram) => {
|
|
68
|
+
if (diagram)
|
|
69
|
+
open(n, diagram);
|
|
70
|
+
else
|
|
71
|
+
setUnread(middle);
|
|
72
|
+
count("read-select", { read: diagram !== null });
|
|
73
|
+
}).catch(() => setUnread(middle)).finally(() => setBusy(null));
|
|
74
|
+
}, [open, readIn]);
|
|
75
|
+
const byHand = () => {
|
|
76
|
+
if (!unread)
|
|
77
|
+
return;
|
|
78
|
+
setPicked({ page: unread.page, byHand: true, diagram: { x0: unread.fx, y0: unread.fy, x1: unread.fx, y1: unread.fy, placement: EMPTY, flipped: false, min: 1, mean: 1 } });
|
|
79
|
+
setUnread(null);
|
|
80
|
+
count("read-byhand");
|
|
81
|
+
};
|
|
82
|
+
const index = picked && !picked.byHand ? all.findIndex((s) => s.page === picked.page && same(s.diagram, picked.diagram)) : -1;
|
|
83
|
+
const step = useCallback((by) => {
|
|
84
|
+
const to = stepFrom(all, index, page, by);
|
|
85
|
+
if (!to)
|
|
86
|
+
return;
|
|
87
|
+
goToDiagram(to.page, to.diagram);
|
|
88
|
+
setPicked({ page: to.page, diagram: to.diagram });
|
|
89
|
+
count("read-step");
|
|
90
|
+
}, [all, index, page, goToDiagram]);
|
|
91
|
+
const { zoomBy } = view;
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
const key = (e) => {
|
|
94
|
+
if (e.defaultPrevented || e.metaKey || e.ctrlKey || e.altKey || e.target instanceof HTMLInputElement)
|
|
95
|
+
return;
|
|
96
|
+
if (e.key === "Escape") {
|
|
97
|
+
if (selecting)
|
|
98
|
+
setSelecting(false);
|
|
99
|
+
else
|
|
100
|
+
setPicked(null);
|
|
101
|
+
}
|
|
102
|
+
else if (e.key === "+" || e.key === "=")
|
|
103
|
+
zoomBy(1);
|
|
104
|
+
else if (e.key === "-")
|
|
105
|
+
zoomBy(-1);
|
|
106
|
+
else if (e.key === "0")
|
|
107
|
+
zoomBy(0);
|
|
108
|
+
else if (e.key === "ArrowRight")
|
|
109
|
+
step(1);
|
|
110
|
+
else if (e.key === "ArrowLeft")
|
|
111
|
+
step(-1);
|
|
112
|
+
};
|
|
113
|
+
window.addEventListener("keydown", key);
|
|
114
|
+
return () => window.removeEventListener("keydown", key);
|
|
115
|
+
}, [zoomBy, step, selecting]);
|
|
116
|
+
return (_jsxs("div", { className: `rd ${picked ? "rd-has-board" : ""}`, style: { "--rd-side": `${sideW}px` }, "data-testid": "reader", children: [_jsx(ReaderBar, { doc: doc, name: book.name, page: page, onLibrary: onLibrary, onPage: view.goTo, nav: { total: all.length, index, searched, pages: view.pages, onStep: step }, select: { on: selecting, toggle: () => setSelecting(!selecting) }, zoom: { percent: view.zoom, by: zoomBy, canOut: view.canZoomOut, canIn: view.canZoomIn } }), _jsxs("div", { className: "rd-body", children: [_jsx("div", { className: `rd-pages ${selecting ? "rd-selecting" : ""}`, ref: view.pane, tabIndex: 0, "data-testid": "read-pages", children: view.laidOut && Array.from({ length: view.pages }, (_, i) => i + 1).map((n) => (_jsx(PageView, { doc: doc, n: n, width: view.width, ratio0: view.ratio0, live: view.near.has(n), found: found.get(n), picked: picked?.page === n && !picked.byHand ? picked.diagram : null, busy: busy?.page === n ? busy : null, selecting: selecting, onPoint: onPoint, onDiagram: open, onBox: onBox, holder: view.holder }, n))) }), _jsx(SideGrip, { width: sideW, label: t("readResize"), onWidth: setSideW }), picked ? (_jsx("aside", { className: "rd-side", "data-testid": "read-board-panel", children: _jsx(BoardPanel, { picked: picked, onClose: () => setPicked(null) }) })) : (_jsxs("aside", { className: "rd-side rd-side-empty", children: [_jsxs("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": true, children: [_jsx("rect", { x: "3.5", y: "3.5", width: "17", height: "17", rx: "2" }), _jsx("path", { d: "M3.5 12h17M12 3.5v17" })] }), _jsx("p", { className: "rd-hint", children: t("readHint") }), _jsx("p", { className: "rd-hint-sub", children: t("readHintSub") })] }))] }), selecting && _jsx("div", { className: "rd-toast", role: "status", "data-testid": "read-select-hint", children: _jsx("span", { children: t("readSelectHint") }) }), unread && (_jsxs("div", { className: "rd-toast", role: "status", "data-testid": "read-toast", children: [_jsx("span", { children: t("readNoDiagram") }), _jsx("button", { type: "button", className: "rd-btn rd-btn-quiet", onClick: byHand, "data-testid": "read-byhand", children: t("readByHand") })] }))] }));
|
|
117
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Doc } from "./pdf";
|
|
2
|
+
export interface DiagramNavProps {
|
|
3
|
+
/** How many diagrams were found so far, and which of them is open (0-based, -1 for none). */
|
|
4
|
+
total: number;
|
|
5
|
+
index: number;
|
|
6
|
+
/** Pages searched, of the book's pages: the count is still growing while they differ. */
|
|
7
|
+
searched: number;
|
|
8
|
+
pages: number;
|
|
9
|
+
onStep(step: -1 | 1): void;
|
|
10
|
+
}
|
|
11
|
+
export declare function ReaderBar({ doc, name, page, nav, select, zoom, onLibrary, onPage }: {
|
|
12
|
+
doc: Doc;
|
|
13
|
+
name: string;
|
|
14
|
+
page: number;
|
|
15
|
+
nav: DiagramNavProps;
|
|
16
|
+
/** The reader draws a box around a diagram that was not found. */
|
|
17
|
+
select: {
|
|
18
|
+
on: boolean;
|
|
19
|
+
toggle(): void;
|
|
20
|
+
};
|
|
21
|
+
/** The size of the pages in percent; a step of 0 is back to 100%. */
|
|
22
|
+
zoom: {
|
|
23
|
+
percent: number;
|
|
24
|
+
by(step: number): void;
|
|
25
|
+
canOut: boolean;
|
|
26
|
+
canIn: boolean;
|
|
27
|
+
};
|
|
28
|
+
onLibrary(): void;
|
|
29
|
+
onPage(n: number): void;
|
|
30
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { useT } from "./host";
|
|
4
|
+
import { chaptersOf } from "./pdf";
|
|
5
|
+
/**
|
|
6
|
+
* The bar over an open book: back to the library, the book's name, its contents, its diagrams (how many, which
|
|
7
|
+
* one is open, the one before and the next), the page, and the size of the pages.
|
|
8
|
+
*/
|
|
9
|
+
const Icon = ({ d, width = 2 }) => (_jsx("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: width, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": true, children: _jsx("path", { d: d }) }));
|
|
10
|
+
function Contents({ doc, onPage }) {
|
|
11
|
+
const t = useT();
|
|
12
|
+
const [chapters, setChapters] = useState([]);
|
|
13
|
+
const [open, setOpen] = useState(false);
|
|
14
|
+
useEffect(() => { void chaptersOf(doc).then(setChapters); }, [doc]);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!open)
|
|
17
|
+
return;
|
|
18
|
+
const key = (e) => { if (e.key === "Escape")
|
|
19
|
+
setOpen(false); };
|
|
20
|
+
window.addEventListener("keydown", key);
|
|
21
|
+
return () => window.removeEventListener("keydown", key);
|
|
22
|
+
}, [open]);
|
|
23
|
+
if (!chapters.length)
|
|
24
|
+
return null;
|
|
25
|
+
return (_jsxs("div", { className: "rd-contents", children: [_jsxs("button", { type: "button", className: "rd-chip", onClick: () => setOpen(!open), "aria-expanded": open, "aria-label": t("readContents"), "data-testid": "read-contents-open", children: [_jsx(Icon, { d: "M8 6h12M8 12h12M8 18h12M4 6h.01M4 12h.01M4 18h.01" }), _jsx("span", { children: t("readContents") })] }), open && (_jsx("ul", { className: "rd-contents-list", "data-testid": "read-contents", children: chapters.map((c, i) => (_jsx("li", { children: _jsxs("button", { type: "button", style: { paddingInlineStart: 12 + c.depth * 14 }, onClick: () => { onPage(c.page); setOpen(false); }, children: [_jsx("span", { children: c.title }), _jsx("span", { className: "rd-contents-page", children: c.page })] }) }, i))) }))] }));
|
|
26
|
+
}
|
|
27
|
+
function DiagramNav({ total, index, searched, pages, onStep }) {
|
|
28
|
+
const t = useT();
|
|
29
|
+
const finding = searched < pages;
|
|
30
|
+
const about = finding ? t("readFinding", { done: searched, pages }) : t("readDiagrams", { n: total });
|
|
31
|
+
return (_jsxs("div", { className: `rd-nav ${finding ? "finding" : ""}`, title: about, "data-testid": "read-nav", children: [_jsx("button", { type: "button", className: "rd-icon", onClick: () => onStep(-1), disabled: !total, "aria-label": t("readDiagramPrev"), "data-testid": "read-nav-prev", children: _jsx(Icon, { d: "M15 6l-6 6 6 6" }) }), _jsxs("span", { className: "rd-nav-count", style: { minWidth: `calc(${String(total).length * 2 + 3.4}ch + 21px)` }, "aria-label": about, "data-testid": "read-nav-count", children: [_jsxs("svg", { viewBox: "0 0 24 24", "aria-hidden": true, children: [_jsx("rect", { x: "3.5", y: "3.5", width: "17", height: "17", rx: "2.5", fill: "none", stroke: "currentColor", strokeWidth: "1.8" }), _jsx("path", { d: "M3.5 3.5h8.5v8.5H3.5zM12 12h8.5v8.5H12z", fill: "currentColor", opacity: "0.55" })] }), index >= 0 ? _jsxs(_Fragment, { children: [_jsx("b", { children: index + 1 }), _jsxs("i", { children: ["/ ", total] })] }) : _jsx("b", { children: total })] }), _jsx("button", { type: "button", className: "rd-icon", onClick: () => onStep(1), disabled: !total, "aria-label": t("readDiagramNext"), "data-testid": "read-nav-next", children: _jsx(Icon, { d: "M9 6l6 6-6 6" }) }), finding && _jsx("span", { className: "rd-nav-progress", "aria-hidden": true, children: _jsx("span", { style: { width: `${(searched / pages) * 100}%` } }) })] }));
|
|
32
|
+
}
|
|
33
|
+
export function ReaderBar({ doc, name, page, nav, select, zoom, onLibrary, onPage }) {
|
|
34
|
+
const t = useT();
|
|
35
|
+
return (_jsxs("header", { className: "rd-bar", children: [_jsxs("button", { type: "button", className: "rd-back", onClick: onLibrary, "data-testid": "read-library", children: [_jsx(Icon, { d: "M15 6l-6 6 6 6" }), _jsx("span", { children: t("readLibrary") })] }), _jsx("span", { className: "rd-title", title: name, children: name }), _jsx(Contents, { doc: doc, onPage: onPage }), _jsx(DiagramNav, { ...nav }), _jsxs("button", { type: "button", className: `rd-chip rd-select ${select.on ? "on" : ""}`, onClick: select.toggle, "aria-pressed": select.on, title: t("readSelectTitle"), "data-testid": "read-select", children: [_jsx(Icon, { d: "M4 8V5a1 1 0 0 1 1-1h3M16 4h3a1 1 0 0 1 1 1v3M20 16v3a1 1 0 0 1-1 1h-3M8 20H5a1 1 0 0 1-1-1v-3M12 9v6M9 12h6" }), _jsx("span", { children: select.on ? t("readCancel") : t("readSelect") })] }), _jsxs("label", { className: "rd-pagebox", title: t("readGoToPage"), children: [_jsx("input", { type: "number", min: 1, max: doc.numPages, value: page, "aria-label": t("readGoToPage"), onChange: (e) => onPage(Number(e.target.value)), "data-testid": "read-page" }), _jsxs("span", { children: ["/ ", doc.numPages] })] }), _jsxs("div", { className: "rd-zoom", children: [_jsx("button", { type: "button", className: "rd-icon", onClick: () => zoom.by(-1), disabled: !zoom.canOut, "aria-label": t("readZoomOut"), title: t("readZoomOut"), "data-testid": "read-zoom-out", children: _jsx(Icon, { d: "M5 12h14", width: 2.2 }) }), _jsxs("button", { type: "button", className: "rd-zoom-now", onClick: () => zoom.by(0), disabled: zoom.percent === 100, title: t("readZoomReset"), "aria-label": t("readZoomReset"), "data-testid": "read-zoom", children: [zoom.percent, "%"] }), _jsx("button", { type: "button", className: "rd-icon", onClick: () => zoom.by(1), disabled: !zoom.canIn, "aria-label": t("readZoomIn"), title: t("readZoomIn"), "data-testid": "read-zoom-in", children: _jsx(Icon, { d: "M12 5v14M5 12h14", width: 2.2 }) })] })] }));
|
|
36
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The edge between the book and the board: drag it to give the board more room or less, press the arrow keys for
|
|
3
|
+
* small steps, double-click for the width it came with. The width is kept on this device. While the edge is
|
|
4
|
+
* dragged only a CSS variable on the reader changes (no render); the reader learns the new width when it is let go.
|
|
5
|
+
*/
|
|
6
|
+
export declare const SIDE_DEFAULT = 480;
|
|
7
|
+
export declare function storedSide(): number;
|
|
8
|
+
export declare function SideGrip({ width, label, onWidth }: {
|
|
9
|
+
width: number;
|
|
10
|
+
label: string;
|
|
11
|
+
onWidth(width: number): void;
|
|
12
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useRef } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* The edge between the book and the board: drag it to give the board more room or less, press the arrow keys for
|
|
5
|
+
* small steps, double-click for the width it came with. The width is kept on this device. While the edge is
|
|
6
|
+
* dragged only a CSS variable on the reader changes (no render); the reader learns the new width when it is let go.
|
|
7
|
+
*/
|
|
8
|
+
export const SIDE_DEFAULT = 480;
|
|
9
|
+
const SIDE_MIN = 340, STEP = 24, KEY = "fenshot-read-side";
|
|
10
|
+
const widest = () => Math.max(SIDE_MIN, Math.round(window.innerWidth * 0.62));
|
|
11
|
+
const held = (w) => Math.round(Math.min(widest(), Math.max(SIDE_MIN, w)));
|
|
12
|
+
export function storedSide() {
|
|
13
|
+
try {
|
|
14
|
+
const kept = Number(localStorage.getItem(KEY));
|
|
15
|
+
return kept > 0 ? held(kept) : SIDE_DEFAULT;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return SIDE_DEFAULT;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function SideGrip({ width, label, onWidth }) {
|
|
22
|
+
const now = useRef(width);
|
|
23
|
+
const settle = (w) => {
|
|
24
|
+
const next = held(w);
|
|
25
|
+
try {
|
|
26
|
+
localStorage.setItem(KEY, String(next));
|
|
27
|
+
}
|
|
28
|
+
catch { /* a private window: the width lasts the visit */ }
|
|
29
|
+
onWidth(next);
|
|
30
|
+
};
|
|
31
|
+
const press = (down) => {
|
|
32
|
+
if (down.button > 0)
|
|
33
|
+
return;
|
|
34
|
+
const grip = down.currentTarget, reader = grip.closest(".rd");
|
|
35
|
+
if (!reader)
|
|
36
|
+
return;
|
|
37
|
+
down.preventDefault();
|
|
38
|
+
grip.setPointerCapture(down.pointerId);
|
|
39
|
+
const rtl = getComputedStyle(reader).direction === "rtl";
|
|
40
|
+
reader.classList.add("rd-resizing");
|
|
41
|
+
const move = (e) => {
|
|
42
|
+
now.current = held(rtl ? e.clientX : window.innerWidth - e.clientX);
|
|
43
|
+
reader.style.setProperty("--rd-side", `${now.current}px`);
|
|
44
|
+
};
|
|
45
|
+
const up = () => {
|
|
46
|
+
grip.removeEventListener("pointermove", move);
|
|
47
|
+
grip.removeEventListener("pointerup", up);
|
|
48
|
+
grip.removeEventListener("pointercancel", up);
|
|
49
|
+
reader.classList.remove("rd-resizing");
|
|
50
|
+
settle(now.current);
|
|
51
|
+
};
|
|
52
|
+
now.current = width;
|
|
53
|
+
grip.addEventListener("pointermove", move);
|
|
54
|
+
grip.addEventListener("pointerup", up);
|
|
55
|
+
grip.addEventListener("pointercancel", up);
|
|
56
|
+
};
|
|
57
|
+
const key = (e) => {
|
|
58
|
+
const rtl = getComputedStyle(e.currentTarget).direction === "rtl";
|
|
59
|
+
const wider = rtl ? "ArrowRight" : "ArrowLeft", narrower = rtl ? "ArrowLeft" : "ArrowRight";
|
|
60
|
+
if (e.key !== wider && e.key !== narrower)
|
|
61
|
+
return;
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
settle(width + (e.key === wider ? STEP : -STEP));
|
|
64
|
+
};
|
|
65
|
+
return (_jsx("div", { className: "rd-grip", role: "separator", "aria-orientation": "vertical", "aria-label": label, "aria-valuenow": width, "aria-valuemin": SIDE_MIN, "aria-valuemax": widest(), tabIndex: 0, onPointerDown: press, onKeyDown: key, onDoubleClick: () => settle(SIDE_DEFAULT), "data-testid": "read-grip" }));
|
|
66
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Recognizer } from "../recognize";
|
|
2
|
+
import type { Diagram } from "./store";
|
|
3
|
+
type Rect = [x: number, y: number, w: number, h: number];
|
|
4
|
+
/** A read worth showing as a diagram without being asked: every square at least this sure. */
|
|
5
|
+
export declare const SURE = 0.6;
|
|
6
|
+
/** Raised when the finder finds more than it did: pages it searched before are then searched again. */
|
|
7
|
+
export declare const FINDER = 5;
|
|
8
|
+
/**
|
|
9
|
+
* Black to move, as many books print it: a filled mark (a triangle, a square, a disc) beside the board's right
|
|
10
|
+
* edge, level with its bottom rank or its top one. A hollow mark, or none, says nothing here. The mark is told
|
|
11
|
+
* from the rank numbers some books print on that side by its shape: about as wide as tall, and filled.
|
|
12
|
+
* Seen 2026-12-05 in a book of exercises: 18 of its 44 diagrams carry the filled mark, and each of them has a
|
|
13
|
+
* mate in one for Black. The box is the board in the canvas's pixels.
|
|
14
|
+
*/
|
|
15
|
+
export declare function blackMarked(page: HTMLCanvasElement, x0: number, y0: number, x1: number, y1: number): boolean;
|
|
16
|
+
/** A board in a book is square and takes a real share of the page's width. */
|
|
17
|
+
export declare function plausible(x0: number, y0: number, x1: number, y1: number, pageW: number): boolean;
|
|
18
|
+
/** The windows to read around a click, nearest first. */
|
|
19
|
+
export declare function windowsAt(pageW: number, pageH: number, cx: number, cy: number): Rect[];
|
|
20
|
+
/** The diagram at a point of the page (fractions), or null when no board is found there. */
|
|
21
|
+
export declare function readAt(recognizer: Recognizer, page: HTMLCanvasElement, fx: number, fy: number): Promise<Diagram | null>;
|
|
22
|
+
/**
|
|
23
|
+
* The diagram in a box the reader drew on the page (fractions of the page), for one neither the search nor a
|
|
24
|
+
* double-click found: the box with a little room around it, then the box itself. Null when it cannot be read.
|
|
25
|
+
*/
|
|
26
|
+
export declare function readBox(recognizer: Recognizer, page: HTMLCanvasElement, box: {
|
|
27
|
+
x0: number;
|
|
28
|
+
y0: number;
|
|
29
|
+
x1: number;
|
|
30
|
+
y1: number;
|
|
31
|
+
}): Promise<Diagram | null>;
|
|
32
|
+
/**
|
|
33
|
+
* Every diagram of a page that reads surely, told one by one as it is found (`onFound`). A look asks first whether
|
|
34
|
+
* anything looks like a board (boardOn); only then the model reads. What was looked at is covered and the page is
|
|
35
|
+
* looked at again. On a page of several diagrams the look at the whole page can stop seeing the rest once some are
|
|
36
|
+
* covered (measured 2026-12-05: 2 of a page's 4, a book of exercises in two columns), so the places where the
|
|
37
|
+
* found ones leave a gap in their rows and columns are then looked at one by one (gapsIn). Looking at every part
|
|
38
|
+
* of every such page instead found the same diagrams and took four times as long (325 s against 83 s on a book
|
|
39
|
+
* of 187 pages). The page's canvas is drawn over: pass a copy.
|
|
40
|
+
*/
|
|
41
|
+
export declare function findDiagrams(recognizer: Recognizer, page: HTMLCanvasElement, onFound?: (d: Diagram) => void): Promise<Diagram[]>;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diagrams on a page of a book. Two ways in:
|
|
3
|
+
* - findDiagrams reads a whole page in the background: the board finder is run, what it found is covered, and it
|
|
4
|
+
* is run again, so a page with several diagrams gives them all;
|
|
5
|
+
* - readAt reads the diagram under a double-click, for a diagram the background pass missed: a window around
|
|
6
|
+
* the click, then wider ones, until a board is found that the click is on.
|
|
7
|
+
* Measured 2026-12-05 on a modern typeset book (41 pages): a board found on 37, every one read with all squares
|
|
8
|
+
* at 75% or more. An old scanned printing reads far worse; such a read opens in the editor to be checked.
|
|
9
|
+
*/
|
|
10
|
+
import { boardness, findBlurredBoardCandidates, findChessboardCorners, snapCorners } from "../detect";
|
|
11
|
+
import { rgbaToGray } from "../tiles";
|
|
12
|
+
/** A read worth showing as a diagram without being asked: every square at least this sure. */
|
|
13
|
+
export const SURE = 0.6;
|
|
14
|
+
/** Raised when the finder finds more than it did: pages it searched before are then searched again. */
|
|
15
|
+
export const FINDER = 5;
|
|
16
|
+
/** A page of exercises holds six or eight diagrams, sometimes twelve. Each look finds one. */
|
|
17
|
+
const MAX_LOOKS = 24;
|
|
18
|
+
/**
|
|
19
|
+
* A read worth opening at all. On a print the model does not know (an old scan with hatched squares) it answers
|
|
20
|
+
* with scattered pieces at 15% to 40% on its worst square, also where there is only text: showing that as the
|
|
21
|
+
* book's position would be a lie. Between this and SURE the board opens with a word of caution.
|
|
22
|
+
*/
|
|
23
|
+
const READABLE_MIN = 0.45, READABLE_MEAN = 0.85;
|
|
24
|
+
const readable = (r) => r.minConfidence >= READABLE_MIN && r.meanConfidence >= READABLE_MEAN && r.placement !== "8/8/8/8/8/8/8/8";
|
|
25
|
+
/**
|
|
26
|
+
* Black to move, as many books print it: a filled mark (a triangle, a square, a disc) beside the board's right
|
|
27
|
+
* edge, level with its bottom rank or its top one. A hollow mark, or none, says nothing here. The mark is told
|
|
28
|
+
* from the rank numbers some books print on that side by its shape: about as wide as tall, and filled.
|
|
29
|
+
* Seen 2026-12-05 in a book of exercises: 18 of its 44 diagrams carry the filled mark, and each of them has a
|
|
30
|
+
* mate in one for Black. The box is the board in the canvas's pixels.
|
|
31
|
+
*/
|
|
32
|
+
export function blackMarked(page, x0, y0, x1, y1) {
|
|
33
|
+
const side = x1 - x0, pen = page.getContext("2d", { willReadFrequently: true });
|
|
34
|
+
const left = Math.round(x1 + side * 0.01), width = Math.min(page.width - left, Math.round(side * 0.2));
|
|
35
|
+
if (!pen || width < 6)
|
|
36
|
+
return false;
|
|
37
|
+
const filledMark = (top, height) => {
|
|
38
|
+
const y = Math.max(0, Math.round(top)), hgt = Math.min(page.height - y, Math.round(height));
|
|
39
|
+
if (hgt < 6)
|
|
40
|
+
return false;
|
|
41
|
+
const px = pen.getImageData(left, y, width, hgt).data;
|
|
42
|
+
const dark = (i) => px[i * 4] * 0.3 + px[i * 4 + 1] * 0.59 + px[i * 4 + 2] * 0.11 < 110;
|
|
43
|
+
const seen = new Uint8Array(width * hgt);
|
|
44
|
+
for (let start = 0; start < width * hgt; start++) {
|
|
45
|
+
if (seen[start] || !dark(start))
|
|
46
|
+
continue;
|
|
47
|
+
// One blot of ink: its box and how much of the box it fills.
|
|
48
|
+
let minX = width, maxX = 0, minY = hgt, maxY = 0, ink = 0;
|
|
49
|
+
const todo = [start];
|
|
50
|
+
seen[start] = 1;
|
|
51
|
+
while (todo.length) {
|
|
52
|
+
const i = todo.pop(), cx = i % width, cy = (i - cx) / width;
|
|
53
|
+
ink++;
|
|
54
|
+
minX = Math.min(minX, cx);
|
|
55
|
+
maxX = Math.max(maxX, cx);
|
|
56
|
+
minY = Math.min(minY, cy);
|
|
57
|
+
maxY = Math.max(maxY, cy);
|
|
58
|
+
for (const [nx, ny] of [[cx - 1, cy], [cx + 1, cy], [cx, cy - 1], [cx, cy + 1]]) {
|
|
59
|
+
const j = ny * width + nx;
|
|
60
|
+
if (nx < 0 || ny < 0 || nx >= width || ny >= hgt || seen[j] || !dark(j))
|
|
61
|
+
continue;
|
|
62
|
+
seen[j] = 1;
|
|
63
|
+
todo.push(j);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const bw = maxX - minX + 1, bh = maxY - minY + 1;
|
|
67
|
+
const sized = bw >= side * 0.03 && bh >= side * 0.03 && bw <= side * 0.14 && bh <= side * 0.14;
|
|
68
|
+
if (sized && bw / bh > 0.62 && bw / bh < 1.6 && ink / (bw * bh) >= 0.42)
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
};
|
|
73
|
+
return filledMark(y1 - side * 0.16, side * 0.2) || filledMark(y0 - side * 0.04, side * 0.2);
|
|
74
|
+
}
|
|
75
|
+
/** A board in a book is square and takes a real share of the page's width. */
|
|
76
|
+
export function plausible(x0, y0, x1, y1, pageW) {
|
|
77
|
+
const w = x1 - x0, h = y1 - y0;
|
|
78
|
+
return w > pageW * 0.12 && w < pageW * 0.85 && Math.abs(w - h) < w * 0.12;
|
|
79
|
+
}
|
|
80
|
+
/** The windows to read around a click, nearest first. */
|
|
81
|
+
export function windowsAt(pageW, pageH, cx, cy) {
|
|
82
|
+
const square = (share) => {
|
|
83
|
+
const side = Math.min(pageW * share, pageH);
|
|
84
|
+
const x = Math.min(Math.max(0, cx - side / 2), pageW - side), y = Math.min(Math.max(0, cy - side / 2), pageH - side);
|
|
85
|
+
return [Math.round(x), Math.round(y), Math.round(side), Math.round(side)];
|
|
86
|
+
};
|
|
87
|
+
const band = Math.min(pageH, pageW * 1.2);
|
|
88
|
+
const top = Math.min(Math.max(0, cy - band / 2), pageH - band);
|
|
89
|
+
return [square(0.5), square(0.8), [0, Math.round(top), pageW, Math.round(band)], [0, 0, pageW, pageH]];
|
|
90
|
+
}
|
|
91
|
+
/** The board's corners in the pixels of what was read: the recognizer answers in its own, smaller frame. */
|
|
92
|
+
function inPixels(r, width) {
|
|
93
|
+
const k = r.frame ? width / r.frame.width : 1;
|
|
94
|
+
return { x0: r.corners.x0 * k, y0: r.corners.y0 * k, x1: r.corners.x1 * k, y1: r.corners.y1 * k };
|
|
95
|
+
}
|
|
96
|
+
async function read(recognizer, source, [x, y, w, h], pageW, pageH) {
|
|
97
|
+
const bitmap = await createImageBitmap(source, x, y, w, h);
|
|
98
|
+
const r = await recognizer.recognize(bitmap);
|
|
99
|
+
bitmap.close();
|
|
100
|
+
if (!r || !readable(r))
|
|
101
|
+
return null;
|
|
102
|
+
const { x0, y0, x1, y1 } = inPixels(r, w);
|
|
103
|
+
if (!plausible(x0, y0, x1, y1, pageW))
|
|
104
|
+
return null;
|
|
105
|
+
const d = { x0: (x + x0) / pageW, y0: (y + y0) / pageH, x1: (x + x1) / pageW, y1: (y + y1) / pageH, placement: r.placement, flipped: false, min: r.minConfidence, mean: r.meanConfidence };
|
|
106
|
+
return blackMarked(source, x + x0, y + y0, x + x1, y + y1) ? { ...d, turn: "b" } : d;
|
|
107
|
+
}
|
|
108
|
+
const holds = (d, fx, fy) => {
|
|
109
|
+
const padX = (d.x1 - d.x0) * 0.06, padY = (d.y1 - d.y0) * 0.06;
|
|
110
|
+
return fx >= d.x0 - padX && fx <= d.x1 + padX && fy >= d.y0 - padY && fy <= d.y1 + padY;
|
|
111
|
+
};
|
|
112
|
+
/** The diagram at a point of the page (fractions), or null when no board is found there. */
|
|
113
|
+
export async function readAt(recognizer, page, fx, fy) {
|
|
114
|
+
const { width: w, height: h } = page;
|
|
115
|
+
let best = null;
|
|
116
|
+
for (const rect of windowsAt(w, h, fx * w, fy * h)) {
|
|
117
|
+
const d = await read(recognizer, page, rect, w, h);
|
|
118
|
+
if (!d || !holds(d, fx, fy))
|
|
119
|
+
continue;
|
|
120
|
+
if (d.min >= SURE)
|
|
121
|
+
return d;
|
|
122
|
+
if (!best || d.mean > best.mean)
|
|
123
|
+
best = d;
|
|
124
|
+
}
|
|
125
|
+
return best;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The diagram in a box the reader drew on the page (fractions of the page), for one neither the search nor a
|
|
129
|
+
* double-click found: the box with a little room around it, then the box itself. Null when it cannot be read.
|
|
130
|
+
*/
|
|
131
|
+
export async function readBox(recognizer, page, box) {
|
|
132
|
+
const { width: w, height: h } = page;
|
|
133
|
+
const x0 = box.x0 * w, y0 = box.y0 * h, bw = (box.x1 - box.x0) * w, bh = (box.y1 - box.y0) * h;
|
|
134
|
+
for (const room of [0.08, 0]) {
|
|
135
|
+
const left = Math.max(0, Math.round(x0 - bw * room)), top = Math.max(0, Math.round(y0 - bh * room));
|
|
136
|
+
const right = Math.min(w, Math.round(x0 + bw * (1 + room))), bottom = Math.min(h, Math.round(y0 + bh * (1 + room)));
|
|
137
|
+
const d = await read(recognizer, page, [left, top, right - left, bottom - top], w, h);
|
|
138
|
+
if (d)
|
|
139
|
+
return d;
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
/** A square that looks like a board this much (see `boardness`) is read; below it the page has no more boards. */
|
|
144
|
+
const BOARDLIKE = 0.6;
|
|
145
|
+
/** The page in greys, at the size the recognizer looks at, and that size over the page's. */
|
|
146
|
+
function greysOf(page, scratch) {
|
|
147
|
+
const k = Math.min(1, 1600 / Math.max(page.width, page.height));
|
|
148
|
+
scratch.width = Math.round(page.width * k);
|
|
149
|
+
scratch.height = Math.round(page.height * k);
|
|
150
|
+
const pen = scratch.getContext("2d", { willReadFrequently: true });
|
|
151
|
+
if (!pen)
|
|
152
|
+
return null;
|
|
153
|
+
pen.drawImage(page, 0, 0, scratch.width, scratch.height);
|
|
154
|
+
return { greys: rgbaToGray(pen.getImageData(0, 0, scratch.width, scratch.height).data, scratch.width, scratch.height), k };
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The most board-like square on a page, in the page's pixels, or null when nothing on it looks like a board.
|
|
158
|
+
* This is the cheap question, asked before the model is: measured 2026-12-05 on three books, every diagram the
|
|
159
|
+
* model then read surely scored 0.97 or more here (53 of 53), and no stretch of text scored over 0.29 (74 of
|
|
160
|
+
* 74). A page of text costs about 150 ms this way; asking the model about it cost 400 ms, and three to eight
|
|
161
|
+
* seconds when it was asked again and again.
|
|
162
|
+
*/
|
|
163
|
+
function boardOn(page, scratch, pageW = page.width) {
|
|
164
|
+
const seen = greysOf(page, scratch);
|
|
165
|
+
if (!seen)
|
|
166
|
+
return null;
|
|
167
|
+
const { greys, k } = seen;
|
|
168
|
+
const raw = findChessboardCorners(greys);
|
|
169
|
+
let best = null, score = BOARDLIKE;
|
|
170
|
+
for (const c of [...(raw ? [raw, snapCorners(greys, raw)] : []), ...findBlurredBoardCandidates(greys, [])]) {
|
|
171
|
+
if (!plausible(c.x0 / k, c.y0 / k, c.x1 / k, c.y1 / k, pageW))
|
|
172
|
+
continue;
|
|
173
|
+
const b = boardness(greys, c);
|
|
174
|
+
if (b >= score) {
|
|
175
|
+
best = c;
|
|
176
|
+
score = b;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return best && { x0: best.x0 / k, y0: best.y0 / k, x1: best.x1 / k, y1: best.y1 / k };
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* One look at a region of the page (the whole page, or a window of it): is a board there, and if so what does the
|
|
183
|
+
* model read. Answers the box to cover before the next look (null when nothing board-like is left) and the
|
|
184
|
+
* diagram when it read surely. Boxes are in the page's pixels.
|
|
185
|
+
*/
|
|
186
|
+
async function lookIn(recognizer, page, [x, y, w, h], scratch, crop) {
|
|
187
|
+
const whole = x === 0 && y === 0 && w === page.width && h === page.height;
|
|
188
|
+
const region = whole ? page : crop;
|
|
189
|
+
if (!whole) {
|
|
190
|
+
crop.width = w;
|
|
191
|
+
crop.height = h;
|
|
192
|
+
crop.getContext("2d")?.drawImage(page, x, y, w, h, 0, 0, w, h);
|
|
193
|
+
}
|
|
194
|
+
const box = boardOn(region, scratch, page.width);
|
|
195
|
+
if (!box)
|
|
196
|
+
return null;
|
|
197
|
+
const bitmap = await createImageBitmap(region);
|
|
198
|
+
const r = await recognizer.recognize(bitmap);
|
|
199
|
+
bitmap.close();
|
|
200
|
+
const shift = (b) => ({ x0: b.x0 + x, y0: b.y0 + y, x1: b.x1 + x, y1: b.y1 + y });
|
|
201
|
+
const read = r && shift(inPixels(r, w));
|
|
202
|
+
if (!r || !read || !plausible(read.x0, read.y0, read.x1, read.y1, page.width) || r.minConfidence < SURE)
|
|
203
|
+
return { cover: shift(box), diagram: null };
|
|
204
|
+
const { width: pw, height: ph } = page;
|
|
205
|
+
const d = { x0: read.x0 / pw, y0: read.y0 / ph, x1: read.x1 / pw, y1: read.y1 / ph, placement: r.placement, flipped: false, min: r.minConfidence, mean: r.meanConfidence };
|
|
206
|
+
if (blackMarked(page, read.x0, read.y0, read.x1, read.y1))
|
|
207
|
+
d.turn = "b";
|
|
208
|
+
return { cover: read, diagram: d };
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Where more diagrams are likely, from those already found: a book sets its diagrams in rows and columns, all of
|
|
212
|
+
* one size, so every crossing of a found column with a found row that holds no diagram yet is a place to look.
|
|
213
|
+
* A page with one diagram, or with all of its grid found, gives none: the common case costs nothing.
|
|
214
|
+
*/
|
|
215
|
+
function gapsIn(found, w, h) {
|
|
216
|
+
if (found.length < 2)
|
|
217
|
+
return [];
|
|
218
|
+
const side = found.map((d) => (d.x1 - d.x0) * w).sort((a, b) => a - b)[found.length >> 1], near = side * 0.25;
|
|
219
|
+
const lines = (values) => values.sort((a, b) => a - b).filter((v, i, all) => i === 0 || v - all[i - 1] > near);
|
|
220
|
+
const out = [];
|
|
221
|
+
for (const top of lines(found.map((d) => d.y0 * h))) {
|
|
222
|
+
for (const left of lines(found.map((d) => d.x0 * w))) {
|
|
223
|
+
if (found.some((d) => Math.abs(d.x0 * w - left) <= near && Math.abs(d.y0 * h - top) <= near))
|
|
224
|
+
continue;
|
|
225
|
+
const room = side * 0.18, x = Math.max(0, Math.round(left - room)), y = Math.max(0, Math.round(top - room));
|
|
226
|
+
out.push([x, y, Math.min(w - x, Math.round(side + 2 * room)), Math.min(h - y, Math.round(side + 2 * room))]);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Every diagram of a page that reads surely, told one by one as it is found (`onFound`). A look asks first whether
|
|
233
|
+
* anything looks like a board (boardOn); only then the model reads. What was looked at is covered and the page is
|
|
234
|
+
* looked at again. On a page of several diagrams the look at the whole page can stop seeing the rest once some are
|
|
235
|
+
* covered (measured 2026-12-05: 2 of a page's 4, a book of exercises in two columns), so the places where the
|
|
236
|
+
* found ones leave a gap in their rows and columns are then looked at one by one (gapsIn). Looking at every part
|
|
237
|
+
* of every such page instead found the same diagrams and took four times as long (325 s against 83 s on a book
|
|
238
|
+
* of 187 pages). The page's canvas is drawn over: pass a copy.
|
|
239
|
+
*/
|
|
240
|
+
export async function findDiagrams(recognizer, page, onFound) {
|
|
241
|
+
const { width: w, height: h } = page;
|
|
242
|
+
const pen = page.getContext("2d");
|
|
243
|
+
if (!pen)
|
|
244
|
+
return [];
|
|
245
|
+
const scratch = document.createElement("canvas"), crop = document.createElement("canvas");
|
|
246
|
+
const found = [];
|
|
247
|
+
let looks = 0;
|
|
248
|
+
const search = async (rect, tries) => {
|
|
249
|
+
for (let i = 0; i < tries && looks < MAX_LOOKS; i++, looks++) {
|
|
250
|
+
const seen = await lookIn(recognizer, page, rect, scratch, crop);
|
|
251
|
+
if (!seen)
|
|
252
|
+
return;
|
|
253
|
+
const d = seen.diagram;
|
|
254
|
+
if (d && !found.some((f) => overlap(f, d) > 0.5)) {
|
|
255
|
+
found.push(d);
|
|
256
|
+
onFound?.(d);
|
|
257
|
+
}
|
|
258
|
+
// Cover what was read, or the board that could not be, and look again for another.
|
|
259
|
+
const { cover } = seen, pad = (cover.x1 - cover.x0) * 0.05;
|
|
260
|
+
pen.fillStyle = "#fff";
|
|
261
|
+
pen.fillRect(cover.x0 - pad, cover.y0 - pad, cover.x1 - cover.x0 + 2 * pad, cover.y1 - cover.y0 + 2 * pad);
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
await search([0, 0, w, h], MAX_LOOKS);
|
|
265
|
+
// A gap that is filled can open another (a third row found through its second column), so ask again, a few times.
|
|
266
|
+
for (let round = 0; round < 3; round++) {
|
|
267
|
+
const before = found.length;
|
|
268
|
+
for (const rect of gapsIn(found, w, h))
|
|
269
|
+
await search(rect, 1);
|
|
270
|
+
if (found.length === before)
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
return found.sort((a, b) => a.y0 - b.y0 || a.x0 - b.x0);
|
|
274
|
+
}
|
|
275
|
+
/** How much of the smaller of two diagrams the other covers, 0..1. */
|
|
276
|
+
function overlap(a, b) {
|
|
277
|
+
const x = Math.max(0, Math.min(a.x1, b.x1) - Math.max(a.x0, b.x0)), y = Math.max(0, Math.min(a.y1, b.y1) - Math.max(a.y0, b.y0));
|
|
278
|
+
return (x * y) / Math.min((a.x1 - a.x0) * (a.y1 - a.y0), (b.x1 - b.x0) * (b.y1 - b.y0));
|
|
279
|
+
}
|