@u1f992/pdfdiff 0.0.1 → 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.
Files changed (71) hide show
  1. package/.github/workflows/gh-pages.yml +60 -0
  2. package/.github/workflows/publish.yml +34 -0
  3. package/.vscode/extensions.json +3 -0
  4. package/.vscode/settings.json +20 -0
  5. package/LICENSE +674 -0
  6. package/README.md +24 -45
  7. package/dist/browser.d.ts +2 -0
  8. package/dist/browser.d.ts.map +1 -0
  9. package/dist/browser.js +3690 -0
  10. package/dist/browser.js.map +1 -0
  11. package/dist/cli.d.ts +3 -0
  12. package/dist/cli.d.ts.map +1 -0
  13. package/dist/cli.js +39856 -0
  14. package/dist/cli.js.map +1 -0
  15. package/dist/coi-serviceworker.min.js +2 -0
  16. package/dist/diff.d.ts +15 -0
  17. package/dist/diff.d.ts.map +1 -0
  18. package/dist/image.d.ts +13 -0
  19. package/dist/image.d.ts.map +1 -0
  20. package/dist/index.d.ts +27 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.html +72 -0
  23. package/dist/index.js +3554 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/index.test.d.ts +2 -0
  26. package/dist/index.test.d.ts.map +1 -0
  27. package/dist/iterable.d.ts +2 -0
  28. package/dist/iterable.d.ts.map +1 -0
  29. package/dist/iterable.test.d.ts +2 -0
  30. package/dist/iterable.test.d.ts.map +1 -0
  31. package/dist/jimp.d.ts +6 -0
  32. package/dist/jimp.d.ts.map +1 -0
  33. package/dist/mupdf-wasm.wasm +0 -0
  34. package/dist/pdf.d.ts +5 -0
  35. package/dist/pdf.d.ts.map +1 -0
  36. package/dist/rgba-color.d.ts +4 -0
  37. package/dist/rgba-color.d.ts.map +1 -0
  38. package/dist/rgba-color.test.d.ts +2 -0
  39. package/dist/rgba-color.test.d.ts.map +1 -0
  40. package/dist/style.css +19 -0
  41. package/dist/worker.d.ts +42 -0
  42. package/dist/worker.d.ts.map +1 -0
  43. package/dist/worker.js +3515 -0
  44. package/dist/worker.js.map +1 -0
  45. package/package.json +45 -7
  46. package/prettier.config.js +3 -0
  47. package/prototyping/README.md +1 -0
  48. package/prototyping/flat-map-concurrency.js +218 -0
  49. package/prototyping/worker.js +10 -0
  50. package/rollup.config.js +108 -0
  51. package/src/browser.ts +196 -0
  52. package/src/cli.ts +195 -0
  53. package/src/diff.ts +85 -0
  54. package/src/image.ts +150 -0
  55. package/src/index.html +72 -0
  56. package/src/index.test.ts +97 -0
  57. package/src/index.ts +275 -0
  58. package/src/iterable.test.ts +40 -0
  59. package/src/iterable.ts +24 -0
  60. package/src/jimp.ts +15 -0
  61. package/src/pdf.ts +74 -0
  62. package/src/rgba-color.test.ts +43 -0
  63. package/src/rgba-color.ts +63 -0
  64. package/src/style.css +19 -0
  65. package/src/worker.ts +159 -0
  66. package/test/a.pdf +0 -0
  67. package/test/b.pdf +0 -0
  68. package/test/base.xcf +0 -0
  69. package/test/expected.png +0 -0
  70. package/test/mask.pdf +0 -0
  71. package/tsconfig.json +50 -0
package/src/worker.ts ADDED
@@ -0,0 +1,159 @@
1
+ /*
2
+ * Copyright (C) 2025 Koutaro Mukai
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify
5
+ * it under the terms of the GNU General Public License as published by
6
+ * the Free Software Foundation, either version 3 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import * as mupdf from "mupdf";
19
+
20
+ import { drawDifference, type Pallet } from "./diff.ts";
21
+ import {
22
+ composeLayers,
23
+ createEmptyImage,
24
+ type AlignStrategy,
25
+ } from "./image.ts";
26
+ import type { JimpInstance } from "./jimp.ts";
27
+ import { pageToImage } from "./pdf.ts";
28
+
29
+ export type InitMessage = {
30
+ type: "init";
31
+ aBytes: Uint8Array;
32
+ bBytes: Uint8Array;
33
+ maskBytes: Uint8Array | null;
34
+ dpi: number;
35
+ alpha: boolean;
36
+ pallet: Pallet;
37
+ align: AlignStrategy;
38
+ };
39
+
40
+ export type PageMessage = {
41
+ type: "page";
42
+ index: number;
43
+ };
44
+
45
+ export type ReadyMessage = {
46
+ type: "ready";
47
+ };
48
+
49
+ export type PageResultMessage = {
50
+ type: "pageResult";
51
+ index: number;
52
+ a: { width: number; height: number; data: ArrayBuffer };
53
+ b: { width: number; height: number; data: ArrayBuffer };
54
+ diff: { width: number; height: number; data: ArrayBuffer };
55
+ addition: [number, number][];
56
+ deletion: [number, number][];
57
+ modification: [number, number][];
58
+ };
59
+
60
+ let pdfA: mupdf.Document;
61
+ let pdfB: mupdf.Document;
62
+ let pdfMask: mupdf.Document;
63
+ let opts: {
64
+ dpi: number;
65
+ alpha: boolean;
66
+ pallet: Pallet;
67
+ align: AlignStrategy;
68
+ };
69
+
70
+ function toTransferable(
71
+ src: Buffer | Uint8Array | Uint8ClampedArray | number[],
72
+ ): ArrayBuffer {
73
+ const view =
74
+ src instanceof Uint8Array || src instanceof Uint8ClampedArray
75
+ ? src
76
+ : Uint8Array.from(src as ArrayLike<number>);
77
+ const out = new ArrayBuffer(view.byteLength);
78
+ new Uint8Array(out).set(view);
79
+ return out;
80
+ }
81
+
82
+ async function processPage(index: number): Promise<PageResultMessage> {
83
+ const [pageA, pageB, pageMask] = (await Promise.all([
84
+ index < pdfA.countPages()
85
+ ? pageToImage(pdfA.loadPage(index), opts.dpi, opts.alpha)
86
+ : createEmptyImage(1, 1),
87
+ index < pdfB.countPages()
88
+ ? pageToImage(pdfB.loadPage(index), opts.dpi, opts.alpha)
89
+ : createEmptyImage(1, 1),
90
+ index < pdfMask.countPages()
91
+ ? pageToImage(pdfMask.loadPage(index), opts.dpi, opts.alpha)
92
+ : createEmptyImage(1, 1),
93
+ ])) as [JimpInstance, JimpInstance, JimpInstance];
94
+
95
+ const {
96
+ diff: diffLayer,
97
+ addition,
98
+ deletion,
99
+ modification,
100
+ } = drawDifference(pageA, pageB, pageMask, opts.pallet, opts.align);
101
+ const diff = composeLayers(pageA.width, pageA.height, [
102
+ [pageA, 0.2],
103
+ [pageB, 0.2],
104
+ [diffLayer, 1],
105
+ ]);
106
+
107
+ return {
108
+ type: "pageResult",
109
+ index,
110
+ a: {
111
+ width: pageA.width,
112
+ height: pageA.height,
113
+ data: toTransferable(pageA.bitmap.data),
114
+ },
115
+ b: {
116
+ width: pageB.width,
117
+ height: pageB.height,
118
+ data: toTransferable(pageB.bitmap.data),
119
+ },
120
+ diff: {
121
+ width: diff.width,
122
+ height: diff.height,
123
+ data: toTransferable(diff.bitmap.data),
124
+ },
125
+ addition,
126
+ deletion,
127
+ modification,
128
+ };
129
+ }
130
+
131
+ self.addEventListener(
132
+ "message",
133
+ async (e: MessageEvent<InitMessage | PageMessage>) => {
134
+ const msg = e.data;
135
+ if (msg.type === "init") {
136
+ pdfA = mupdf.PDFDocument.openDocument(msg.aBytes, "application/pdf");
137
+ pdfB = mupdf.PDFDocument.openDocument(msg.bBytes, "application/pdf");
138
+ pdfMask = msg.maskBytes
139
+ ? mupdf.PDFDocument.openDocument(msg.maskBytes, "application/pdf")
140
+ : new mupdf.PDFDocument();
141
+ opts = {
142
+ dpi: msg.dpi,
143
+ alpha: msg.alpha,
144
+ pallet: msg.pallet,
145
+ align: msg.align,
146
+ };
147
+ if (pdfA.countPages() > 0) pdfA.loadPage(0).destroy();
148
+ const ready: ReadyMessage = { type: "ready" };
149
+ self.postMessage(ready);
150
+ } else if (msg.type === "page") {
151
+ const result = await processPage(msg.index);
152
+ self.postMessage(result, [
153
+ result.a.data,
154
+ result.b.data,
155
+ result.diff.data,
156
+ ]);
157
+ }
158
+ },
159
+ );
package/test/a.pdf ADDED
Binary file
package/test/b.pdf ADDED
Binary file
package/test/base.xcf ADDED
Binary file
Binary file
package/test/mask.pdf ADDED
Binary file
package/tsconfig.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ // Visit https://aka.ms/tsconfig to read more about this file
3
+ "compilerOptions": {
4
+ // File Layout
5
+ "rootDir": "./src",
6
+ "outDir": "./dist",
7
+
8
+ // Environment Settings
9
+ // See also https://aka.ms/tsconfig/module
10
+ "module": "esnext",
11
+ "target": "esnext",
12
+ // "types": [],
13
+ // For nodejs:
14
+ "lib": ["esnext", "WebWorker", "DOM"],
15
+ "types": ["node"],
16
+ // and npm install -D @types/node
17
+
18
+ // Other Outputs
19
+ "sourceMap": true,
20
+ "declaration": true,
21
+ "declarationMap": true,
22
+
23
+ // Stricter Typechecking Options
24
+ "noUncheckedIndexedAccess": true,
25
+ "exactOptionalPropertyTypes": true,
26
+
27
+ // Style Options
28
+ // "noImplicitReturns": true,
29
+ // "noImplicitOverride": true,
30
+ // "noUnusedLocals": true,
31
+ // "noUnusedParameters": true,
32
+ // "noFallthroughCasesInSwitch": true,
33
+ // "noPropertyAccessFromIndexSignature": true,
34
+
35
+ // Recommended Options
36
+ "strict": true,
37
+ "jsx": "react-jsx",
38
+ "verbatimModuleSyntax": true,
39
+ "isolatedModules": true,
40
+ "noUncheckedSideEffectImports": true,
41
+ "moduleDetection": "force",
42
+ "skipLibCheck": true,
43
+
44
+ // With Rollup
45
+ "moduleResolution": "bundler",
46
+ "allowImportingTsExtensions": true,
47
+ "rewriteRelativeImportExtensions": true,
48
+ "noEmit": true
49
+ }
50
+ }