@u1f992/pdfdiff 0.0.1 → 0.1.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 (67) 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 +3621 -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 +39804 -0
  14. package/dist/cli.js.map +1 -0
  15. package/dist/diff.d.ts +15 -0
  16. package/dist/diff.d.ts.map +1 -0
  17. package/dist/image.d.ts +13 -0
  18. package/dist/image.d.ts.map +1 -0
  19. package/dist/index.d.ts +26 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.html +67 -0
  22. package/dist/index.js +3493 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/iterable.d.ts +2 -0
  25. package/dist/iterable.d.ts.map +1 -0
  26. package/dist/iterable.test.d.ts +2 -0
  27. package/dist/iterable.test.d.ts.map +1 -0
  28. package/dist/jimp.d.ts +6 -0
  29. package/dist/jimp.d.ts.map +1 -0
  30. package/dist/mupdf-wasm.wasm +0 -0
  31. package/dist/pdf.d.ts +377 -0
  32. package/dist/pdf.d.ts.map +1 -0
  33. package/dist/rgba-color.d.ts +4 -0
  34. package/dist/rgba-color.d.ts.map +1 -0
  35. package/dist/rgba-color.test.d.ts +2 -0
  36. package/dist/rgba-color.test.d.ts.map +1 -0
  37. package/dist/style.css +19 -0
  38. package/dist/worker.d.ts +2 -0
  39. package/dist/worker.d.ts.map +1 -0
  40. package/dist/worker.js +380 -0
  41. package/dist/worker.js.map +1 -0
  42. package/package.json +44 -7
  43. package/prettier.config.js +3 -0
  44. package/prototyping/README.md +1 -0
  45. package/prototyping/flat-map-concurrency.js +218 -0
  46. package/prototyping/worker.js +10 -0
  47. package/rollup.config.js +121 -0
  48. package/src/browser.ts +184 -0
  49. package/src/cli.ts +175 -0
  50. package/src/diff.ts +70 -0
  51. package/src/image.ts +128 -0
  52. package/src/index.html +67 -0
  53. package/src/index.ts +186 -0
  54. package/src/iterable.test.ts +40 -0
  55. package/src/iterable.ts +24 -0
  56. package/src/jimp.ts +14 -0
  57. package/src/pdf.ts +42 -0
  58. package/src/rgba-color.test.ts +43 -0
  59. package/src/rgba-color.ts +63 -0
  60. package/src/style.css +19 -0
  61. package/src/worker.ts +62 -0
  62. package/test/a.pdf +0 -0
  63. package/test/b.pdf +0 -0
  64. package/test/base.xcf +0 -0
  65. package/test/expected.png +0 -0
  66. package/test/mask.pdf +0 -0
  67. package/tsconfig.json +50 -0
package/src/jimp.ts ADDED
@@ -0,0 +1,14 @@
1
+ import * as jimp from "jimp";
2
+
3
+ export type JimpInstance = Pick<
4
+ jimp.JimpInstance,
5
+ | "width"
6
+ | "height"
7
+ | "getPixelColor"
8
+ | "setPixelColor"
9
+ | "resize"
10
+ | "composite"
11
+ > & {
12
+ getBuffer: (mime: "image/png") => ReturnType<jimp.JimpInstance["getBuffer"]>;
13
+ getBase64: (mime: "image/png") => ReturnType<jimp.JimpInstance["getBase64"]>;
14
+ };
package/src/pdf.ts ADDED
@@ -0,0 +1,42 @@
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 jimp from "jimp";
19
+ import * as mupdf from "mupdf";
20
+
21
+ export function* loadPages(pdf: mupdf.Document) {
22
+ for (let i = 0; i < pdf.countPages(); i++) {
23
+ yield pdf.loadPage(i);
24
+ }
25
+ }
26
+
27
+ export async function pageToImage(
28
+ page: mupdf.Page,
29
+ dpi: number,
30
+ alpha: boolean,
31
+ ) {
32
+ const zoom = dpi / 72;
33
+ const pixmap = page.toPixmap(
34
+ [zoom, 0, 0, zoom, 0, 0],
35
+ mupdf.ColorSpace.DeviceRGB,
36
+ alpha,
37
+ );
38
+ const ret = await jimp.Jimp.fromBuffer(new Uint8Array(pixmap.asPNG()).buffer);
39
+ pixmap.destroy();
40
+ page.destroy();
41
+ return ret;
42
+ }
@@ -0,0 +1,43 @@
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 assert from "assert";
19
+ import test from "node:test";
20
+
21
+ import { parseHex, formatHex } from "./rgba-color.ts";
22
+
23
+ test("parseHex", async (ctx) => {
24
+ await ctx.test("#rgb", () => {
25
+ assert.deepStrictEqual(parseHex("#fed"), [0xff, 0xee, 0xdd, 0xff]);
26
+ });
27
+ await ctx.test("#rrggbb", () => {
28
+ assert.deepStrictEqual(parseHex("#fffefd"), [0xff, 0xfe, 0xfd, 0xff]);
29
+ });
30
+ await ctx.test("#rgba", () => {
31
+ assert.deepStrictEqual(parseHex("#fedc"), [0xff, 0xee, 0xdd, 0xcc]);
32
+ });
33
+ await ctx.test("#rrggbbaa", () => {
34
+ assert.deepStrictEqual(parseHex("#fffefdfc"), [0xff, 0xfe, 0xfd, 0xfc]);
35
+ });
36
+ await ctx.test("invalid", () => {
37
+ assert.deepStrictEqual(parseHex("foobar"), null);
38
+ });
39
+ });
40
+
41
+ test("formatHex", () => {
42
+ assert.deepStrictEqual(formatHex([0xff, 0xfe, 0xfd, 0xfc]), "#fffefdfc");
43
+ });
@@ -0,0 +1,63 @@
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
+ export type RGBAColor = [number, number, number, number];
19
+
20
+ export const parseHex = (hex: string) => {
21
+ if (/^#([0-9a-fA-F]{3})$/.test(hex)) {
22
+ return [
23
+ parseInt(hex[1]! + hex[1]!, 16),
24
+ parseInt(hex[2]! + hex[2]!, 16),
25
+ parseInt(hex[3]! + hex[3]!, 16),
26
+ 255,
27
+ ] as RGBAColor;
28
+ }
29
+ if (/^#([0-9a-fA-F]{4})$/.test(hex)) {
30
+ return [
31
+ parseInt(hex[1]! + hex[1]!, 16),
32
+ parseInt(hex[2]! + hex[2]!, 16),
33
+ parseInt(hex[3]! + hex[3]!, 16),
34
+ parseInt(hex[4]! + hex[4]!, 16),
35
+ ] as RGBAColor;
36
+ }
37
+ if (/^#([0-9a-fA-F]{6})$/.test(hex)) {
38
+ return [
39
+ parseInt(hex.slice(1, 3), 16),
40
+ parseInt(hex.slice(3, 5), 16),
41
+ parseInt(hex.slice(5, 7), 16),
42
+ 255,
43
+ ] as RGBAColor;
44
+ }
45
+ if (/^#([0-9a-fA-F]{8})$/.test(hex)) {
46
+ return [
47
+ parseInt(hex.slice(1, 3), 16),
48
+ parseInt(hex.slice(3, 5), 16),
49
+ parseInt(hex.slice(5, 7), 16),
50
+ parseInt(hex.slice(7, 9), 16),
51
+ ] as RGBAColor;
52
+ }
53
+ return null;
54
+ };
55
+
56
+ export const formatHex = ([r, g, b, a]: RGBAColor) =>
57
+ "#" +
58
+ [r, g, b, a]
59
+ .map((v) => {
60
+ const hex = v.toString(16);
61
+ return hex.length === 1 ? "0" + hex : hex;
62
+ })
63
+ .join("");
package/src/style.css ADDED
@@ -0,0 +1,19 @@
1
+ .summary-content {
2
+ display: inline-flex;
3
+ align-items: baseline;
4
+ gap: 20px;
5
+ }
6
+
7
+ .checkerboard-bg {
8
+ background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAAAAABWESUoAAAAHUlEQVQ4y2P4gQYeoAGGUQUjSgG6ALqGUQUjSgEAdjWwLh+tpFgAAAAASUVORK5CYII=");
9
+ }
10
+
11
+ .diff-table {
12
+ border: 1px solid black;
13
+ border-collapse: collapse;
14
+ }
15
+
16
+ .diff-table th,
17
+ .diff-table td {
18
+ border: 1px solid black;
19
+ }
package/src/worker.ts ADDED
@@ -0,0 +1,62 @@
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 jimp from "jimp";
19
+
20
+ import { drawDifference, type Pallet } from "./diff.js";
21
+ import { composeLayers, type AlignStrategy } from "./image.js";
22
+ import type { JimpInstance } from "./jimp.js";
23
+
24
+ self.addEventListener(
25
+ "message",
26
+ async (
27
+ e: MessageEvent<{
28
+ bufA: ArrayBuffer;
29
+ bufB: ArrayBuffer;
30
+ bufMask: ArrayBuffer;
31
+ pallet: Pallet;
32
+ align: AlignStrategy;
33
+ }>,
34
+ ) => {
35
+ const { bufA, bufB, bufMask, pallet, align } = e.data;
36
+ const a = (await jimp.Jimp.fromBuffer(bufA)) as JimpInstance;
37
+ const b = (await jimp.Jimp.fromBuffer(bufB)) as JimpInstance;
38
+ const mask = (await jimp.Jimp.fromBuffer(bufMask)) as JimpInstance;
39
+ const {
40
+ diff: diffLayer,
41
+ addition,
42
+ deletion,
43
+ modification,
44
+ } = drawDifference(a, b, mask, pallet, align);
45
+ const diff = composeLayers(a.width, a.height, [
46
+ [a, 0.2],
47
+ [b, 0.2],
48
+ [diffLayer, 1],
49
+ ]);
50
+ const bufDiff = new Uint8Array(await diff.getBuffer(jimp.JimpMime.png))
51
+ .buffer;
52
+ self.postMessage(
53
+ {
54
+ bufDiff,
55
+ addition,
56
+ deletion,
57
+ modification,
58
+ },
59
+ [bufDiff],
60
+ );
61
+ },
62
+ );
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
+ }