@scoriiu/fenshot 0.1.6 → 0.1.8
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/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/package.json +18 -6
|
@@ -0,0 +1,130 @@
|
|
|
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
|
+
/**
|
|
59
|
+
* Who is to move in a diagram, when the book says so in the words beside it ("Black to play"): the nearest such
|
|
60
|
+
* words under the board, or over it. Null when the page has no text (a scan) or says nothing.
|
|
61
|
+
* `box` is the diagram in fractions of the page.
|
|
62
|
+
*/
|
|
63
|
+
export async function turnNear(doc, n, box) {
|
|
64
|
+
const page = await doc.getPage(n);
|
|
65
|
+
const view = page.getViewport({ scale: 1 });
|
|
66
|
+
const side = box.x1 - box.x0, height = box.y1 - box.y0;
|
|
67
|
+
const near = [];
|
|
68
|
+
for (const item of (await page.getTextContent()).items) {
|
|
69
|
+
if (!("str" in item) || !item.str.trim())
|
|
70
|
+
continue;
|
|
71
|
+
const [x, y] = view.convertToViewportPoint(item.transform[4], item.transform[5]);
|
|
72
|
+
const fx = x / view.width, fy = y / view.height;
|
|
73
|
+
if (fx < box.x0 - side * 0.6 || fx > box.x1 + side * 0.2)
|
|
74
|
+
continue;
|
|
75
|
+
const gap = fy > box.y1 ? fy - box.y1 : fy < box.y0 ? (box.y0 - fy) * 1.5 : null;
|
|
76
|
+
if (gap !== null && gap < height * 0.7)
|
|
77
|
+
near.push({ gap, text: item.str });
|
|
78
|
+
}
|
|
79
|
+
near.sort((a, b) => a.gap - b.gap);
|
|
80
|
+
for (const { text } of near) {
|
|
81
|
+
const line = text.toLowerCase().normalize("NFC").trim();
|
|
82
|
+
if (BLACK_MOVES.test(line))
|
|
83
|
+
return "b";
|
|
84
|
+
if (WHITE_MOVES.test(line))
|
|
85
|
+
return "w";
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
/** The book's first page as a small picture, for its card in the library. */
|
|
90
|
+
export async function coverOf(doc) {
|
|
91
|
+
try {
|
|
92
|
+
const canvas = document.createElement("canvas");
|
|
93
|
+
await (await drawPage(doc, 1, 192, canvas)).promise;
|
|
94
|
+
return canvas.toDataURL("image/jpeg", 0.72);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Which side a diagram is printed from, when the page carries the board's coordinates as text: "black" when the
|
|
102
|
+
* rank numbers beside it run 1 to 8 downwards or the file letters under it run h to a, "white" the other way,
|
|
103
|
+
* null when the page says nothing (the coordinates are part of a picture, or there are none).
|
|
104
|
+
* `box` is the diagram in fractions of the page.
|
|
105
|
+
*/
|
|
106
|
+
export async function sideBelow(doc, n, box) {
|
|
107
|
+
const page = await doc.getPage(n);
|
|
108
|
+
const view = page.getViewport({ scale: 1 });
|
|
109
|
+
const side = box.x1 - box.x0, height = box.y1 - box.y0;
|
|
110
|
+
const at = new Map();
|
|
111
|
+
for (const item of (await page.getTextContent()).items) {
|
|
112
|
+
if (!("str" in item))
|
|
113
|
+
continue;
|
|
114
|
+
const mark = item.str.trim().toLowerCase();
|
|
115
|
+
if (!/^[a-h1-8]$/.test(mark))
|
|
116
|
+
continue;
|
|
117
|
+
const [x, y] = view.convertToViewportPoint(item.transform[4], item.transform[5]);
|
|
118
|
+
const fx = x / view.width, fy = y / view.height;
|
|
119
|
+
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);
|
|
120
|
+
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);
|
|
121
|
+
if (/[1-8]/.test(mark) ? beside : under)
|
|
122
|
+
at.set(mark, { fx, fy });
|
|
123
|
+
}
|
|
124
|
+
const one = at.get("1"), eight = at.get("8"), a = at.get("a"), h = at.get("h");
|
|
125
|
+
if (one && eight && Math.abs(one.fy - eight.fy) > height * 0.5)
|
|
126
|
+
return one.fy > eight.fy ? "white" : "black";
|
|
127
|
+
if (a && h && Math.abs(a.fx - h.fx) > side * 0.5)
|
|
128
|
+
return a.fx < h.fx ? "white" : "black";
|
|
129
|
+
return null;
|
|
130
|
+
}
|