@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/dist/worker.js ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * The search for diagrams, in a worker of its own (founder 2026-12-05: searching a book made the page stutter,
3
+ * 172 freezes of over 0.2 s on a book of 196 pages, because the board finder and the model ran on the thread that
4
+ * also scrolls the page). A site starts it with a file of one line, `import "@kibitzo/chess-book-reader/worker"`,
5
+ * so that its own bundler builds the worker. One job at a time, in the order they arrive: the page decides the
6
+ * order (useDiagrams).
7
+ */
8
+ import { extractTiles, probsToPlacement, recognizeGray, rgbaToGray } from "@scoriiu/fenshot";
9
+ import { findDiagrams, readAt, readBox } from "./find";
10
+ const scope = self;
11
+ /** The recognizer looks at a picture no larger than this (as @scoriiu/fenshot's own recognize does). */
12
+ const MAX_DIM = 1600;
13
+ let model = null;
14
+ let recognition = null;
15
+ function load({ modelUrl, wasmPaths }) {
16
+ return (model ??= (async () => {
17
+ // The plain wasm build, as the recognizer itself loads it.
18
+ const ort = (await import("onnxruntime-web/wasm"));
19
+ ort.env.wasm.wasmPaths = wasmPaths;
20
+ return { ort, session: await ort.InferenceSession.create(modelUrl, { executionProviders: ["wasm"] }) };
21
+ })());
22
+ }
23
+ const frame = new OffscreenCanvas(1, 1);
24
+ const scanner = {
25
+ async recognize(source) {
26
+ // A model that did not load (the network) is asked for again by the next page.
27
+ if (!model && recognition)
28
+ void load(recognition).catch(() => { model = null; });
29
+ if (!model)
30
+ throw new Error("the search worker was not started");
31
+ const k = Math.min(1, MAX_DIM / Math.max(source.width, source.height));
32
+ const width = Math.round(source.width * k), height = Math.round(source.height * k);
33
+ frame.width = width;
34
+ frame.height = height;
35
+ const pen = frame.getContext("2d", { willReadFrequently: true });
36
+ if (!pen)
37
+ return null;
38
+ pen.drawImage(source, 0, 0, width, height);
39
+ const gray = rgbaToGray(pen.getImageData(0, 0, width, height).data, width, height);
40
+ const { ort, session } = await model;
41
+ const scan = await recognizeGray(gray, async (corners) => {
42
+ const out = await session.run({ tiles: new ort.Tensor("float32", extractTiles(gray, corners), [64, 1024]) });
43
+ return probsToPlacement(out["probs"].data);
44
+ });
45
+ return scan && { ...scan, frame: { width, height } };
46
+ },
47
+ };
48
+ /** The picture as a canvas the search may draw over and read pixels from. */
49
+ function canvasOf(bitmap) {
50
+ const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
51
+ canvas.getContext("2d", { willReadFrequently: true })?.drawImage(bitmap, 0, 0);
52
+ bitmap.close();
53
+ return canvas;
54
+ }
55
+ async function answer(ask) {
56
+ if (ask.type === "init") {
57
+ recognition = ask.recognition;
58
+ void load(recognition).catch(() => { model = null; });
59
+ return;
60
+ }
61
+ const page = canvasOf(ask.bitmap);
62
+ if (ask.type === "page") {
63
+ const diagrams = await findDiagrams(scanner, page, (diagram) => scope.postMessage({ type: "found", id: ask.id, diagram }));
64
+ scope.postMessage({ type: "page", id: ask.id, diagrams });
65
+ }
66
+ else {
67
+ const diagram = ask.type === "at" ? await readAt(scanner, page, ask.fx, ask.fy) : await readBox(scanner, page, ask.box);
68
+ scope.postMessage({ type: "read", id: ask.id, diagram });
69
+ }
70
+ }
71
+ let line = Promise.resolve();
72
+ scope.onmessage = (e) => {
73
+ const ask = e.data;
74
+ line = line.then(() => answer(ask)).catch((err) => {
75
+ if (ask.type !== "init")
76
+ scope.postMessage({ type: "error", id: ask.id, message: String(err) });
77
+ });
78
+ };
package/package.json CHANGED
@@ -1,6 +1,61 @@
1
1
  {
2
2
  "name": "@kibitzo/chess-book-reader",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "A chess-book reader for the browser: open a PDF, every diagram is found and read, a click puts it on your board beside the page. React, with the board, words and look given by the site.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./reader.css": "./dist/reader.css",
14
+ "./worker": {
15
+ "types": "./dist/worker.d.ts",
16
+ "import": "./dist/worker.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "sideEffects": [
23
+ "*.css",
24
+ "./dist/worker.js"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsc -p tsconfig.build.json && cp src/reader.css dist/reader.css",
28
+ "prepublishOnly": "npm run build"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "peerDependencies": {
34
+ "@scoriiu/fenshot": ">=0.1.6",
35
+ "onnxruntime-web": ">=1.17.0",
36
+ "pdfjs-dist": ">=5",
37
+ "react": ">=18"
38
+ },
39
+ "devDependencies": {
40
+ "@types/react": "^19.0.0",
41
+ "pdfjs-dist": "6.5.438",
42
+ "typescript": "^5.6.0",
43
+ "onnxruntime-web": "1.26.0"
44
+ },
45
+ "keywords": [
46
+ "chess",
47
+ "pdf",
48
+ "chess book",
49
+ "diagram",
50
+ "fen",
51
+ "react"
52
+ ],
53
+ "license": "MIT",
54
+ "repository": {
55
+ "type": "git",
56
+ "url": "git+https://github.com/scoriiu/fenshot.git",
57
+ "directory": "packages/reader"
58
+ },
59
+ "author": "Kibitzo (https://kibitzo.com)",
60
+ "homepage": "https://fenshot.com/read/"
61
+ }