@kibitzo/chess-book-reader 0.0.0-stage → 0.2.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/README.md +36 -2
- package/dist/Library.d.ts +11 -0
- package/dist/Library.js +74 -0
- package/dist/PageView.d.ts +29 -0
- package/dist/PageView.js +80 -0
- package/dist/ReadApp.d.ts +9 -0
- package/dist/ReadApp.js +126 -0
- package/dist/Reader.d.ts +8 -0
- package/dist/Reader.js +117 -0
- package/dist/ReaderBar.d.ts +32 -0
- package/dist/ReaderBar.js +36 -0
- package/dist/SideGrip.d.ts +12 -0
- package/dist/SideGrip.js +66 -0
- package/dist/find.d.ts +64 -0
- package/dist/find.js +275 -0
- package/dist/host.d.ts +97 -0
- package/dist/host.js +66 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/pdf.d.ts +54 -0
- package/dist/pdf.js +131 -0
- package/dist/protocol.d.ts +64 -0
- package/dist/protocol.js +7 -0
- package/dist/reader.css +1084 -0
- package/dist/search.d.ts +18 -0
- package/dist/search.js +45 -0
- package/dist/store.d.ts +63 -0
- package/dist/store.js +103 -0
- package/dist/useDiagrams.d.ts +25 -0
- package/dist/useDiagrams.js +208 -0
- package/dist/usePages.d.ts +18 -0
- package/dist/usePages.js +148 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +78 -0
- package/package.json +59 -4
package/dist/pdf.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
let lib = null;
|
|
2
|
+
function pdfjs() {
|
|
3
|
+
lib ??= import("pdfjs-dist").then((m) => {
|
|
4
|
+
m.GlobalWorkerOptions.workerSrc = config.workerSrc;
|
|
5
|
+
return m;
|
|
6
|
+
});
|
|
7
|
+
return lib;
|
|
8
|
+
}
|
|
9
|
+
/** Where pdf.js's worker and the files it fetches as a book needs them are (the site says: ReaderHost.pdf). */
|
|
10
|
+
let config = { workerSrc: "", assets: "/pdfjs/" };
|
|
11
|
+
export function configurePdf(next) {
|
|
12
|
+
config = { ...next, assets: next.assets.endsWith("/") ? next.assets : `${next.assets}/` };
|
|
13
|
+
}
|
|
14
|
+
const fetched = () => ({ wasmUrl: `${config.assets}wasm/`, cMapUrl: `${config.assets}cmaps/`, cMapPacked: true, standardFontDataUrl: `${config.assets}standard_fonts/`, iccUrl: `${config.assets}iccs/` });
|
|
15
|
+
export async function openPdf(file) {
|
|
16
|
+
const { getDocument } = await pdfjs();
|
|
17
|
+
return getDocument({ data: await file.arrayBuffer(), ...fetched() }).promise;
|
|
18
|
+
}
|
|
19
|
+
/** A page's height over its width. */
|
|
20
|
+
export async function ratioOf(doc, n) {
|
|
21
|
+
const v = (await doc.getPage(n)).getViewport({ scale: 1 });
|
|
22
|
+
return v.height / v.width;
|
|
23
|
+
}
|
|
24
|
+
/** Draw page `n` on `canvas`, `width` pixels wide. The returned task can be cancelled when the page scrolls away. */
|
|
25
|
+
export async function drawPage(doc, n, width, canvas) {
|
|
26
|
+
const page = await doc.getPage(n);
|
|
27
|
+
const base = page.getViewport({ scale: 1 });
|
|
28
|
+
const viewport = page.getViewport({ scale: width / base.width });
|
|
29
|
+
canvas.width = Math.round(viewport.width);
|
|
30
|
+
canvas.height = Math.round(viewport.height);
|
|
31
|
+
return page.render({ canvas, viewport });
|
|
32
|
+
}
|
|
33
|
+
/** The book's own list of chapters, each with the page it opens on. Empty when the PDF has none. */
|
|
34
|
+
export async function chaptersOf(doc) {
|
|
35
|
+
const out = [];
|
|
36
|
+
const walk = async (items, depth) => {
|
|
37
|
+
for (const item of items) {
|
|
38
|
+
try {
|
|
39
|
+
const dest = typeof item.dest === "string" ? await doc.getDestination(item.dest) : item.dest;
|
|
40
|
+
const ref = Array.isArray(dest) ? dest[0] : null;
|
|
41
|
+
if (ref && typeof ref === "object")
|
|
42
|
+
out.push({ title: item.title.trim(), page: (await doc.getPageIndex(ref)) + 1, depth });
|
|
43
|
+
}
|
|
44
|
+
catch { /* a chapter that points nowhere is left out */ }
|
|
45
|
+
if (item.items?.length && depth < 1)
|
|
46
|
+
await walk(item.items, depth + 1);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const outline = (await doc.getOutline());
|
|
50
|
+
if (outline)
|
|
51
|
+
await walk(outline, 0);
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
/** "Black to play" as a caption ends its line: "forcing Black to play 6…Kd8" in the author's prose is not one. */
|
|
55
|
+
const caption = (words) => new RegExp(`(${words})[\\s.!:]*$`);
|
|
56
|
+
const BLACK_MOVES = caption("black to (move|play)|black'?s move|schwarz am zug|schwarz zieht|trait aux noirs|les noirs jouent|juegan (las )?negras|negras juegan|mueven (las )?negras|negrul (muta|la mutare)|mut[aă] negrul|il nero muove|muove il nero|tocca al nero|zwart aan zet|pretas jogam|jogam as pretas|ход ч[её]рных|ч[её]рные начинают");
|
|
57
|
+
const WHITE_MOVES = caption("white to (move|play)|white'?s move|wei(ss|ß) am zug|wei(ss|ß) zieht|trait aux blancs|les blancs jouent|juegan (las )?blancas|blancas juegan|mueven (las )?blancas|albul (muta|la mutare)|mut[aă] albul|il bianco muove|muove il bianco|tocca al bianco|wit aan zet|brancas jogam|jogam as brancas|ход белых|белые начинают");
|
|
58
|
+
/** A page's text, read once for all of its diagrams. Empty for a scan. */
|
|
59
|
+
export async function wordsOf(doc, n) {
|
|
60
|
+
const page = await doc.getPage(n);
|
|
61
|
+
const view = page.getViewport({ scale: 1 });
|
|
62
|
+
const out = [];
|
|
63
|
+
for (const item of (await page.getTextContent()).items) {
|
|
64
|
+
if (!("str" in item) || !item.str.trim())
|
|
65
|
+
continue;
|
|
66
|
+
const [x, y] = view.convertToViewportPoint(item.transform[4], item.transform[5]);
|
|
67
|
+
out.push({ text: item.str, fx: x / view.width, fy: y / view.height });
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Who is to move in a diagram, when the book says so in the words beside it ("Black to play"): the nearest such
|
|
73
|
+
* words under the board, or over it. Null when the page has no text (a scan) or says nothing.
|
|
74
|
+
* `box` is the diagram in fractions of the page.
|
|
75
|
+
*/
|
|
76
|
+
export function turnNear(words, box) {
|
|
77
|
+
const side = box.x1 - box.x0, height = box.y1 - box.y0;
|
|
78
|
+
const near = [];
|
|
79
|
+
for (const { text, fx, fy } of words) {
|
|
80
|
+
if (fx < box.x0 - side * 0.6 || fx > box.x1 + side * 0.2)
|
|
81
|
+
continue;
|
|
82
|
+
const gap = fy > box.y1 ? fy - box.y1 : fy < box.y0 ? (box.y0 - fy) * 1.5 : null;
|
|
83
|
+
if (gap !== null && gap < height * 0.7)
|
|
84
|
+
near.push({ gap, text });
|
|
85
|
+
}
|
|
86
|
+
near.sort((a, b) => a.gap - b.gap);
|
|
87
|
+
for (const { text } of near) {
|
|
88
|
+
const line = text.toLowerCase().normalize("NFC").trim();
|
|
89
|
+
if (BLACK_MOVES.test(line))
|
|
90
|
+
return "b";
|
|
91
|
+
if (WHITE_MOVES.test(line))
|
|
92
|
+
return "w";
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
/** The book's first page as a small picture, for its card in the library. */
|
|
97
|
+
export async function coverOf(doc) {
|
|
98
|
+
try {
|
|
99
|
+
const canvas = document.createElement("canvas");
|
|
100
|
+
await (await drawPage(doc, 1, 192, canvas)).promise;
|
|
101
|
+
return canvas.toDataURL("image/jpeg", 0.72);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Which side a diagram is printed from, when the page carries the board's coordinates as text: "black" when the
|
|
109
|
+
* rank numbers beside it run 1 to 8 downwards or the file letters under it run h to a, "white" the other way,
|
|
110
|
+
* null when the page says nothing (the coordinates are part of a picture, or there are none).
|
|
111
|
+
* `box` is the diagram in fractions of the page.
|
|
112
|
+
*/
|
|
113
|
+
export function sideBelow(words, box) {
|
|
114
|
+
const side = box.x1 - box.x0, height = box.y1 - box.y0;
|
|
115
|
+
const at = new Map();
|
|
116
|
+
for (const { text, fx, fy } of words) {
|
|
117
|
+
const mark = text.trim().toLowerCase();
|
|
118
|
+
if (!/^[a-h1-8]$/.test(mark))
|
|
119
|
+
continue;
|
|
120
|
+
const beside = fy > box.y0 - height * 0.05 && fy < box.y1 + height * 0.1 && (Math.abs(fx - box.x0) < side * 0.18 || Math.abs(fx - box.x1) < side * 0.18);
|
|
121
|
+
const under = fx > box.x0 - side * 0.05 && fx < box.x1 + side * 0.05 && (Math.abs(fy - box.y1) < height * 0.18 || Math.abs(fy - box.y0) < height * 0.18);
|
|
122
|
+
if (/[1-8]/.test(mark) ? beside : under)
|
|
123
|
+
at.set(mark, { fx, fy });
|
|
124
|
+
}
|
|
125
|
+
const one = at.get("1"), eight = at.get("8"), a = at.get("a"), h = at.get("h");
|
|
126
|
+
if (one && eight && Math.abs(one.fy - eight.fy) > height * 0.5)
|
|
127
|
+
return one.fy > eight.fy ? "white" : "black";
|
|
128
|
+
if (a && h && Math.abs(a.fx - h.fx) > side * 0.5)
|
|
129
|
+
return a.fx < h.fx ? "white" : "black";
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Diagram } from "./store";
|
|
2
|
+
/**
|
|
3
|
+
* What the page and the search worker say to each other (search.ts on the page, worker.ts in the worker). The
|
|
4
|
+
* page draws a book's page into a picture and hands it over; everything after that (finding the boards, the
|
|
5
|
+
* model, the marks beside a board) happens in the worker, so the page's own thread stays free for the reader.
|
|
6
|
+
*/
|
|
7
|
+
/** Raised when the finder finds more than it did: pages it searched before are then searched again. */
|
|
8
|
+
export declare const FINDER = 5;
|
|
9
|
+
export type Box = {
|
|
10
|
+
x0: number;
|
|
11
|
+
y0: number;
|
|
12
|
+
x1: number;
|
|
13
|
+
y1: number;
|
|
14
|
+
};
|
|
15
|
+
/** Where the model and its runtime are, as absolute addresses (a worker cannot resolve the page's relative ones). */
|
|
16
|
+
export interface Recognition {
|
|
17
|
+
modelUrl: string;
|
|
18
|
+
wasmPaths: string | {
|
|
19
|
+
mjs: string;
|
|
20
|
+
wasm: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export type Ask = {
|
|
24
|
+
type: "init";
|
|
25
|
+
recognition: Recognition;
|
|
26
|
+
}
|
|
27
|
+
/** Every diagram of a page. */
|
|
28
|
+
| {
|
|
29
|
+
type: "page";
|
|
30
|
+
id: number;
|
|
31
|
+
bitmap: ImageBitmap;
|
|
32
|
+
}
|
|
33
|
+
/** The diagram at a point of a page (fractions). */
|
|
34
|
+
| {
|
|
35
|
+
type: "at";
|
|
36
|
+
id: number;
|
|
37
|
+
bitmap: ImageBitmap;
|
|
38
|
+
fx: number;
|
|
39
|
+
fy: number;
|
|
40
|
+
}
|
|
41
|
+
/** The diagram in a box the reader drew (fractions). */
|
|
42
|
+
| {
|
|
43
|
+
type: "box";
|
|
44
|
+
id: number;
|
|
45
|
+
bitmap: ImageBitmap;
|
|
46
|
+
box: Box;
|
|
47
|
+
};
|
|
48
|
+
export type Tell = {
|
|
49
|
+
type: "found";
|
|
50
|
+
id: number;
|
|
51
|
+
diagram: Diagram;
|
|
52
|
+
} | {
|
|
53
|
+
type: "page";
|
|
54
|
+
id: number;
|
|
55
|
+
diagrams: Diagram[];
|
|
56
|
+
} | {
|
|
57
|
+
type: "read";
|
|
58
|
+
id: number;
|
|
59
|
+
diagram: Diagram | null;
|
|
60
|
+
} | {
|
|
61
|
+
type: "error";
|
|
62
|
+
id: number;
|
|
63
|
+
message: string;
|
|
64
|
+
};
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the page and the search worker say to each other (search.ts on the page, worker.ts in the worker). The
|
|
3
|
+
* page draws a book's page into a picture and hands it over; everything after that (finding the boards, the
|
|
4
|
+
* model, the marks beside a board) happens in the worker, so the page's own thread stays free for the reader.
|
|
5
|
+
*/
|
|
6
|
+
/** Raised when the finder finds more than it did: pages it searched before are then searched again. */
|
|
7
|
+
export const FINDER = 5;
|