forge-terminal 0.1.0 → 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 +305 -24
- package/dist/chunks/adm-zip-SQKLKBDP.js +3 -0
- package/dist/chunks/available-2VX7K7W6.js +3 -0
- package/dist/chunks/boot-ODZZHFUI.js +3 -0
- package/dist/chunks/cfb-V6M2PYOD.js +13 -0
- package/dist/chunks/chunk-2WYHI73Y.js +3 -0
- package/dist/chunks/chunk-4YHKTP7I.js +3 -0
- package/dist/chunks/chunk-5PYTOHLZ.js +3 -0
- package/dist/chunks/chunk-7YQR2MVH.js +3 -0
- package/dist/chunks/chunk-DRBA23IQ.js +3 -0
- package/dist/chunks/chunk-EOYBXOCV.js +4 -0
- package/dist/chunks/chunk-FQV57RMH.js +3 -0
- package/dist/chunks/chunk-IMKI5F4I.js +4 -0
- package/dist/chunks/chunk-J7FLFHQJ.js +3 -0
- package/dist/chunks/chunk-L2WK7CJG.js +3 -0
- package/dist/chunks/chunk-LFE3WRU4.js +3 -0
- package/dist/chunks/chunk-MHFY7HF6.js +10 -0
- package/dist/chunks/chunk-PBIS2ONZ.js +3 -0
- package/dist/chunks/chunk-QVACE4IE.js +3 -0
- package/dist/chunks/chunk-TMUK2DD3.js +3 -0
- package/dist/chunks/chunk-UNVHKJW4.js +41 -0
- package/dist/chunks/chunk-V4GHQGQM.js +4 -0
- package/dist/chunks/chunk-VB665HTV.js +3 -0
- package/dist/chunks/chunk-YHK5VE5R.js +3 -0
- package/dist/chunks/colour-QV2C3R7R.js +3 -0
- package/dist/chunks/config-command-UXJY623L.js +3 -0
- package/dist/chunks/dist-J2V6ITHG.js +29 -0
- package/dist/chunks/es-T2U7NBLD.js +3 -0
- package/dist/chunks/execute-4WJCUM7J.js +3 -0
- package/dist/chunks/extensions-command-6WZV2AES.js +3 -0
- package/dist/chunks/launch-2OI4ZLBQ.js +66 -0
- package/dist/chunks/lib-TPEJ5CJJ.js +46 -0
- package/dist/chunks/lock-R6GZXCCW.js +3 -0
- package/dist/chunks/manager-H52ENJOH.js +4 -0
- package/dist/chunks/paths-XY2345OS.js +3 -0
- package/dist/chunks/pdf-compress-IMRFBLZF.js +3 -0
- package/dist/chunks/react-devtools-core-UM4FS6CU.js +3 -0
- package/dist/chunks/run-N3F4EM7E.js +4 -0
- package/dist/chunks/server-5RWSVTOM.js +73 -0
- package/dist/chunks/theme-GW7CQPUO.js +3 -0
- package/dist/chunks/word-OGQ2KZAJ.js +24 -0
- package/dist/index.js +67 -95
- package/dist/third-party-licenses.txt +2856 -0
- package/package.json +28 -21
- package/skills/forge/SKILL.md +128 -0
- package/dist/cli/args.js +0 -278
- package/dist/cli/args.js.map +0 -1
- package/dist/cli/config-command.js +0 -55
- package/dist/cli/config-command.js.map +0 -1
- package/dist/cli/execute.js +0 -377
- package/dist/cli/execute.js.map +0 -1
- package/dist/cli/report.js +0 -123
- package/dist/cli/report.js.map +0 -1
- package/dist/cli/spinner.js +0 -60
- package/dist/cli/spinner.js.map +0 -1
- package/dist/cli/stdin.js +0 -47
- package/dist/cli/stdin.js.map +0 -1
- package/dist/config/preferences.js +0 -143
- package/dist/config/preferences.js.map +0 -1
- package/dist/core/actions/compress.js +0 -157
- package/dist/core/actions/compress.js.map +0 -1
- package/dist/core/actions/convert.js +0 -270
- package/dist/core/actions/convert.js.map +0 -1
- package/dist/core/actions/extract.js +0 -94
- package/dist/core/actions/extract.js.map +0 -1
- package/dist/core/actions/index.js +0 -23
- package/dist/core/actions/index.js.map +0 -1
- package/dist/core/actions/merge.js +0 -16
- package/dist/core/actions/merge.js.map +0 -1
- package/dist/core/actions/rotate.js +0 -36
- package/dist/core/actions/rotate.js.map +0 -1
- package/dist/core/actions/split.js +0 -65
- package/dist/core/actions/split.js.map +0 -1
- package/dist/core/atomic.js +0 -61
- package/dist/core/atomic.js.map +0 -1
- package/dist/core/capabilities.js +0 -59
- package/dist/core/capabilities.js.map +0 -1
- package/dist/core/compress.js +0 -76
- package/dist/core/compress.js.map +0 -1
- package/dist/core/describe.js +0 -49
- package/dist/core/describe.js.map +0 -1
- package/dist/core/errors.js +0 -469
- package/dist/core/errors.js.map +0 -1
- package/dist/core/execute-jobs.js +0 -122
- package/dist/core/execute-jobs.js.map +0 -1
- package/dist/core/formats.js +0 -108
- package/dist/core/formats.js.map +0 -1
- package/dist/core/order.js +0 -44
- package/dist/core/order.js.map +0 -1
- package/dist/core/output-path.js +0 -176
- package/dist/core/output-path.js.map +0 -1
- package/dist/core/pages.js +0 -107
- package/dist/core/pages.js.map +0 -1
- package/dist/core/pdf-compress.js +0 -206
- package/dist/core/pdf-compress.js.map +0 -1
- package/dist/core/plan.js +0 -42
- package/dist/core/plan.js.map +0 -1
- package/dist/core/resolve.js +0 -115
- package/dist/core/resolve.js.map +0 -1
- package/dist/core/run.js +0 -66
- package/dist/core/run.js.map +0 -1
- package/dist/core/suggest.js +0 -50
- package/dist/core/suggest.js.map +0 -1
- package/dist/core/types.js +0 -2
- package/dist/core/types.js.map +0 -1
- package/dist/core/units.js +0 -94
- package/dist/core/units.js.map +0 -1
- package/dist/core/write-safety.js +0 -128
- package/dist/core/write-safety.js.map +0 -1
- package/dist/engines/heic.js +0 -166
- package/dist/engines/heic.js.map +0 -1
- package/dist/engines/image.js +0 -342
- package/dist/engines/image.js.map +0 -1
- package/dist/engines/pdf.js +0 -444
- package/dist/engines/pdf.js.map +0 -1
- package/dist/engines/pdfium.js +0 -161
- package/dist/engines/pdfium.js.map +0 -1
- package/dist/engines/registry.js +0 -65
- package/dist/engines/registry.js.map +0 -1
- package/dist/engines/types.js +0 -2
- package/dist/engines/types.js.map +0 -1
- package/dist/engines/word.js +0 -385
- package/dist/engines/word.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/shell/App.js +0 -1972
- package/dist/shell/App.js.map +0 -1
- package/dist/shell/ClickTargets.js +0 -183
- package/dist/shell/ClickTargets.js.map +0 -1
- package/dist/shell/ThemeContext.js +0 -21
- package/dist/shell/ThemeContext.js.map +0 -1
- package/dist/shell/blocks.js +0 -160
- package/dist/shell/blocks.js.map +0 -1
- package/dist/shell/clipboard.js +0 -64
- package/dist/shell/clipboard.js.map +0 -1
- package/dist/shell/commands.js +0 -51
- package/dist/shell/commands.js.map +0 -1
- package/dist/shell/components/Banner.js +0 -73
- package/dist/shell/components/Banner.js.map +0 -1
- package/dist/shell/components/CommandPalette.js +0 -37
- package/dist/shell/components/CommandPalette.js.map +0 -1
- package/dist/shell/components/FileCard.js +0 -59
- package/dist/shell/components/FileCard.js.map +0 -1
- package/dist/shell/components/HintBar.js +0 -18
- package/dist/shell/components/HintBar.js.map +0 -1
- package/dist/shell/components/Hints.js +0 -21
- package/dist/shell/components/Hints.js.map +0 -1
- package/dist/shell/components/MergeList.js +0 -268
- package/dist/shell/components/MergeList.js.map +0 -1
- package/dist/shell/components/ModeHeader.js +0 -21
- package/dist/shell/components/ModeHeader.js.map +0 -1
- package/dist/shell/components/PageGrid.js +0 -324
- package/dist/shell/components/PageGrid.js.map +0 -1
- package/dist/shell/components/PathInput.js +0 -147
- package/dist/shell/components/PathInput.js.map +0 -1
- package/dist/shell/components/Progress.js +0 -43
- package/dist/shell/components/Progress.js.map +0 -1
- package/dist/shell/components/Prompt.js +0 -667
- package/dist/shell/components/Prompt.js.map +0 -1
- package/dist/shell/components/ResultLinks.js +0 -26
- package/dist/shell/components/ResultLinks.js.map +0 -1
- package/dist/shell/components/Select.js +0 -152
- package/dist/shell/components/Select.js.map +0 -1
- package/dist/shell/components/Slider.js +0 -48
- package/dist/shell/components/Slider.js.map +0 -1
- package/dist/shell/components/StagedFiles.js +0 -92
- package/dist/shell/components/StagedFiles.js.map +0 -1
- package/dist/shell/components/ThemePicker.js +0 -18
- package/dist/shell/components/ThemePicker.js.map +0 -1
- package/dist/shell/components/UnitRow.js +0 -30
- package/dist/shell/components/UnitRow.js.map +0 -1
- package/dist/shell/flows/pdf.js +0 -424
- package/dist/shell/flows/pdf.js.map +0 -1
- package/dist/shell/frame-geometry.js +0 -56
- package/dist/shell/frame-geometry.js.map +0 -1
- package/dist/shell/hyperlink.js +0 -27
- package/dist/shell/hyperlink.js.map +0 -1
- package/dist/shell/intro.js +0 -101
- package/dist/shell/intro.js.map +0 -1
- package/dist/shell/launch.js +0 -75
- package/dist/shell/launch.js.map +0 -1
- package/dist/shell/mark.js +0 -134
- package/dist/shell/mark.js.map +0 -1
- package/dist/shell/mouse.js +0 -209
- package/dist/shell/mouse.js.map +0 -1
- package/dist/shell/redraw.js +0 -36
- package/dist/shell/redraw.js.map +0 -1
- package/dist/shell/reveal.js +0 -24
- package/dist/shell/reveal.js.map +0 -1
- package/dist/shell/stage.js +0 -34
- package/dist/shell/stage.js.map +0 -1
- package/dist/shell/terminal-write.js +0 -16
- package/dist/shell/terminal-write.js.map +0 -1
- package/dist/shell/theme.js +0 -178
- package/dist/shell/theme.js.map +0 -1
- package/dist/shell/useFrameOrigin.js +0 -175
- package/dist/shell/useFrameOrigin.js.map +0 -1
- package/dist/shell/useKeys.js +0 -28
- package/dist/shell/useKeys.js.map +0 -1
- package/dist/shell/useMouse.js +0 -150
- package/dist/shell/useMouse.js.map +0 -1
- package/dist/shell/useMouseRouting.js +0 -69
- package/dist/shell/useMouseRouting.js.map +0 -1
- package/dist/shell/width.js +0 -62
- package/dist/shell/width.js.map +0 -1
- package/dist/utils/display-path.js +0 -31
- package/dist/utils/display-path.js.map +0 -1
- package/dist/utils/unescape-path.js +0 -68
- package/dist/utils/unescape-path.js.map +0 -1
package/dist/core/formats.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
export const FORMATS = {
|
|
2
|
-
jpeg: {
|
|
3
|
-
id: 'jpeg',
|
|
4
|
-
label: 'JPEG',
|
|
5
|
-
extensions: ['.jpg', '.jpeg'],
|
|
6
|
-
hasAlpha: false,
|
|
7
|
-
animatable: false,
|
|
8
|
-
lossy: true,
|
|
9
|
-
hint: 'universal',
|
|
10
|
-
defaultQuality: 82,
|
|
11
|
-
},
|
|
12
|
-
png: {
|
|
13
|
-
id: 'png',
|
|
14
|
-
label: 'PNG',
|
|
15
|
-
extensions: ['.png'],
|
|
16
|
-
hasAlpha: true,
|
|
17
|
-
animatable: false,
|
|
18
|
-
lossy: false,
|
|
19
|
-
hint: 'lossless',
|
|
20
|
-
},
|
|
21
|
-
webp: {
|
|
22
|
-
id: 'webp',
|
|
23
|
-
label: 'WebP',
|
|
24
|
-
extensions: ['.webp'],
|
|
25
|
-
hasAlpha: true,
|
|
26
|
-
animatable: true,
|
|
27
|
-
lossy: true,
|
|
28
|
-
hint: 'smaller, modern',
|
|
29
|
-
defaultQuality: 80,
|
|
30
|
-
},
|
|
31
|
-
avif: {
|
|
32
|
-
id: 'avif',
|
|
33
|
-
label: 'AVIF',
|
|
34
|
-
extensions: ['.avif'],
|
|
35
|
-
hasAlpha: true,
|
|
36
|
-
animatable: true,
|
|
37
|
-
lossy: true,
|
|
38
|
-
hint: 'smallest',
|
|
39
|
-
defaultQuality: 50,
|
|
40
|
-
},
|
|
41
|
-
heic: {
|
|
42
|
-
id: 'heic',
|
|
43
|
-
label: 'HEIC',
|
|
44
|
-
extensions: ['.heic', '.heif'],
|
|
45
|
-
hasAlpha: true,
|
|
46
|
-
animatable: false,
|
|
47
|
-
lossy: true,
|
|
48
|
-
hint: 'Apple photos',
|
|
49
|
-
},
|
|
50
|
-
gif: {
|
|
51
|
-
id: 'gif',
|
|
52
|
-
label: 'GIF',
|
|
53
|
-
extensions: ['.gif'],
|
|
54
|
-
hasAlpha: true,
|
|
55
|
-
animatable: true,
|
|
56
|
-
lossy: false,
|
|
57
|
-
hint: 'animation',
|
|
58
|
-
},
|
|
59
|
-
tiff: {
|
|
60
|
-
id: 'tiff',
|
|
61
|
-
label: 'TIFF',
|
|
62
|
-
extensions: ['.tif', '.tiff'],
|
|
63
|
-
hasAlpha: true,
|
|
64
|
-
animatable: false,
|
|
65
|
-
lossy: false,
|
|
66
|
-
hint: 'archival',
|
|
67
|
-
},
|
|
68
|
-
pdf: {
|
|
69
|
-
id: 'pdf',
|
|
70
|
-
label: 'PDF',
|
|
71
|
-
extensions: ['.pdf'],
|
|
72
|
-
hasAlpha: false,
|
|
73
|
-
animatable: false,
|
|
74
|
-
// A PDF container is not lossy. /compress supplies its own quality
|
|
75
|
-
// control; /convert must not show a quality slider for this target.
|
|
76
|
-
lossy: false,
|
|
77
|
-
hint: 'document',
|
|
78
|
-
},
|
|
79
|
-
docx: {
|
|
80
|
-
id: 'docx',
|
|
81
|
-
label: 'DOCX',
|
|
82
|
-
extensions: ['.docx'],
|
|
83
|
-
hasAlpha: false,
|
|
84
|
-
animatable: false,
|
|
85
|
-
lossy: false,
|
|
86
|
-
hint: 'Word document',
|
|
87
|
-
},
|
|
88
|
-
doc: {
|
|
89
|
-
id: 'doc',
|
|
90
|
-
label: 'DOC',
|
|
91
|
-
extensions: ['.doc'],
|
|
92
|
-
hasAlpha: false,
|
|
93
|
-
animatable: false,
|
|
94
|
-
lossy: false,
|
|
95
|
-
hint: 'legacy Word',
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
export const ALL_FORMAT_IDS = Object.keys(FORMATS);
|
|
99
|
-
export function formatById(id) {
|
|
100
|
-
return FORMATS[id.toLowerCase()];
|
|
101
|
-
}
|
|
102
|
-
export function primaryExtension(id) {
|
|
103
|
-
const ext = FORMATS[id].extensions[0];
|
|
104
|
-
if (!ext)
|
|
105
|
-
throw new Error(`format ${id} declares no extensions`);
|
|
106
|
-
return ext;
|
|
107
|
-
}
|
|
108
|
-
//# sourceMappingURL=formats.js.map
|
package/dist/core/formats.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formats.js","sourceRoot":"","sources":["../../src/core/formats.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,OAAO,GAAiC;IACnD,IAAI,EAAE;QACJ,EAAE,EAAE,MAAM;QACV,KAAK,EAAE,MAAM;QACb,UAAU,EAAE,CAAC,MAAM,EAA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ChE,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
package/dist/core/order.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { basename } from 'node:path';
|
|
2
|
-
const CYCLE = ['dropped', 'name', 'newest', 'oldest'];
|
|
3
|
-
export function nextSortMode(mode) {
|
|
4
|
-
return CYCLE[(CYCLE.indexOf(mode) + 1) % CYCLE.length];
|
|
5
|
-
}
|
|
6
|
-
/**
|
|
7
|
-
* Move one item to a new position, clamping at both ends.
|
|
8
|
-
*
|
|
9
|
-
* Clamping rather than wrapping: a row dragged past the top should stop
|
|
10
|
-
* there, not reappear at the bottom, which is what every list in every other
|
|
11
|
-
* application does.
|
|
12
|
-
*/
|
|
13
|
-
export function moveItem(list, from, to) {
|
|
14
|
-
const target = Math.min(Math.max(to, 0), list.length - 1);
|
|
15
|
-
if (from === target || from < 0 || from >= list.length)
|
|
16
|
-
return [...list];
|
|
17
|
-
const next = [...list];
|
|
18
|
-
const [item] = next.splice(from, 1);
|
|
19
|
-
if (item === undefined)
|
|
20
|
-
return [...list];
|
|
21
|
-
next.splice(target, 0, item);
|
|
22
|
-
return next;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Order for merge.
|
|
26
|
-
*
|
|
27
|
-
* Hand-reordering thirty scans is not a thing anyone should do, and a glob
|
|
28
|
-
* already arrives in name order — this makes that explicit and reversible.
|
|
29
|
-
* `dropped` is identity, which is what makes the cycle safe to spin through.
|
|
30
|
-
*/
|
|
31
|
-
export function sortSources(sources, mode, mtimes) {
|
|
32
|
-
const time = (s) => mtimes.get(s.path) ?? 0;
|
|
33
|
-
switch (mode) {
|
|
34
|
-
case 'dropped':
|
|
35
|
-
return [...sources];
|
|
36
|
-
case 'name':
|
|
37
|
-
return [...sources].sort((a, b) => basename(a.path).localeCompare(basename(b.path)));
|
|
38
|
-
case 'newest':
|
|
39
|
-
return [...sources].sort((a, b) => time(b) - time(a));
|
|
40
|
-
case 'oldest':
|
|
41
|
-
return [...sources].sort((a, b) => time(a) - time(b));
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
//# sourceMappingURL=order.js.map
|
package/dist/core/order.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"order.js","sourceRoot":"","sources":["../../src/core/order.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAKpC,MAAM,KAAK,GAAe,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAA;AAEjE,MAAM,UAAU,YAAY,CAAC,IAAc;IACzC,OAAO,KAAK,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,MAAM,CAAa,CAAA;AACpE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAI,IAAS,EAAE,IAAY,EAAE,EAAU;IAC7D,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACzD,IAAI,IAAI,KAAK,MAAM,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM;QAAE,OAAO,CAAC,GAAG,IAAI,CAAC,CAAA;IACxE,MAAM,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC,CAAA;IACtB,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IACnC,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,CAAC,GAAG,IAAI,CAAC,CAAA;IACxC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;IAC5B,OAAO,IAAI,CAAA;AACb,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CACzB,OAAqB,EACrB,IAAc,EACd,MAA2B;IAE3B,MAAM,IAAI,GAAG,CAAC,CAAa,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACvD,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,SAAS;YACZ,OAAO,CAAC,GAAG,OAAO,CAAC,CAAA;QACrB,KAAK,MAAM;YACT,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACtF,KAAK,QAAQ;YACX,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;QACvD,KAAK,QAAQ;YACX,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;IACzD,CAAC;AACH,CAAC"}
|
package/dist/core/output-path.js
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import { existsSync, statSync } from 'node:fs';
|
|
2
|
-
import { basename, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
3
|
-
import { primaryExtension } from './formats.js';
|
|
4
|
-
/**
|
|
5
|
-
* A trailing separator always means directory. An existing directory on disk
|
|
6
|
-
* also means directory. Anything else with no extension is treated as a
|
|
7
|
-
* directory too, because `-o dist` is far more often a folder than a file.
|
|
8
|
-
*/
|
|
9
|
-
export function looksLikeDirectory(p) {
|
|
10
|
-
if (p.endsWith('/') || p.endsWith(sep))
|
|
11
|
-
return true;
|
|
12
|
-
if (existsSync(p) && statSync(p).isDirectory())
|
|
13
|
-
return true;
|
|
14
|
-
return extname(p) === '';
|
|
15
|
-
}
|
|
16
|
-
function swapExtension(path, target) {
|
|
17
|
-
const name = basename(path);
|
|
18
|
-
const ext = extname(name);
|
|
19
|
-
const stem = ext ? name.slice(0, -ext.length) : name;
|
|
20
|
-
return stem + primaryExtension(target);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Where inside an output directory a source's files belong.
|
|
24
|
-
*
|
|
25
|
-
* With `--recursive`, `resolveInputs` records the root each source was found
|
|
26
|
-
* under, and the tree below that root is recreated inside the destination —
|
|
27
|
-
* `in/sub/a.png` with `-o out` writes `out/sub/a.jpg`, not `out/a.jpg`, so a
|
|
28
|
-
* recursive scan of a nested library does not collapse into one flat folder
|
|
29
|
-
* (and, with it, into a pile of `output-collision` refusals).
|
|
30
|
-
*
|
|
31
|
-
* Shared by `resolveOutputPath` and `rasterOutputPaths` rather than written
|
|
32
|
-
* out in each: a document source that flattened while an image beside it
|
|
33
|
-
* nested was the same flag meaning two different things depending on what
|
|
34
|
-
* was dropped on it.
|
|
35
|
-
*
|
|
36
|
-
* No root, or a source that lies outside the root it was given, keeps the
|
|
37
|
-
* destination as-is — there is no meaningful tree to recreate.
|
|
38
|
-
*/
|
|
39
|
-
function relativeToRoot(destination, sourceDir, sourceRoot) {
|
|
40
|
-
if (!sourceRoot)
|
|
41
|
-
return destination;
|
|
42
|
-
const rel = relative(resolve(sourceRoot), sourceDir);
|
|
43
|
-
if (!rel || rel.startsWith('..'))
|
|
44
|
-
return destination;
|
|
45
|
-
return join(destination, rel);
|
|
46
|
-
}
|
|
47
|
-
export function resolveOutputPath(req) {
|
|
48
|
-
const source = resolve(req.sourcePath);
|
|
49
|
-
const filename = swapExtension(source, req.target);
|
|
50
|
-
if (!req.output)
|
|
51
|
-
return join(resolve(source, '..'), filename);
|
|
52
|
-
const output = isAbsolute(req.output) ? req.output : resolve(req.output);
|
|
53
|
-
if (!looksLikeDirectory(req.output))
|
|
54
|
-
return resolve(output);
|
|
55
|
-
return join(relativeToRoot(output, resolve(source, '..'), req.sourceRoot), filename);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* The name spec §8's "keep both" choice writes to: ` (1)` before the
|
|
59
|
-
* extension — `photo (1).webp`, never `photo.webp (1)`, so the file still
|
|
60
|
-
* opens in the right application. The number counts up until it finds a name
|
|
61
|
-
* nothing occupies, because keeping both twice must not overwrite the copy
|
|
62
|
-
* kept the first time.
|
|
63
|
-
*
|
|
64
|
-
* Pure with respect to conversion, like the rest of this module: it decides a
|
|
65
|
-
* name, it does not create anything. Nothing here closes the race between the
|
|
66
|
-
* check and a later write — the atomic rename in `engines/image.ts` is what
|
|
67
|
-
* bounds the damage if some other process claims the name in between.
|
|
68
|
-
*/
|
|
69
|
-
export function uniqueOutputPath(path) {
|
|
70
|
-
if (!existsSync(path))
|
|
71
|
-
return path;
|
|
72
|
-
const dir = resolve(path, '..');
|
|
73
|
-
const name = basename(path);
|
|
74
|
-
const ext = extname(name);
|
|
75
|
-
const stem = ext ? name.slice(0, -ext.length) : name;
|
|
76
|
-
for (let n = 1; n < 10_000; n++) {
|
|
77
|
-
const candidate = join(dir, `${stem} (${n})${ext}`);
|
|
78
|
-
if (!existsSync(candidate))
|
|
79
|
-
return candidate;
|
|
80
|
-
}
|
|
81
|
-
// Unreachable short of 10,000 copies of one file in one folder. Throwing
|
|
82
|
-
// beats returning `path`, which the caller would dutifully overwrite.
|
|
83
|
-
throw new Error(`no free name beside ${name}`);
|
|
84
|
-
}
|
|
85
|
-
function stemAndExt(path) {
|
|
86
|
-
const dir = resolve(path, '..');
|
|
87
|
-
const name = basename(path);
|
|
88
|
-
const ext = extname(name);
|
|
89
|
-
return { dir, stem: ext ? name.slice(0, -ext.length) : name, ext: ext || '.pdf' };
|
|
90
|
-
}
|
|
91
|
-
/** `report.pdf` + `trimmed` → `report-trimmed.pdf`, beside the source. */
|
|
92
|
-
export function suffixedOutputPath(sourcePath, suffix) {
|
|
93
|
-
const { dir, stem, ext } = stemAndExt(resolve(sourcePath));
|
|
94
|
-
return join(dir, `${stem}-${suffix}${ext}`);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Numbered outputs for a split.
|
|
98
|
-
*
|
|
99
|
-
* Zero-padded to the width of the count, so `-01`…`-12` sorts the way anyone
|
|
100
|
-
* looking at the folder expects. Unpadded numbers put `-10` before `-2`.
|
|
101
|
-
*/
|
|
102
|
-
export function splitOutputPaths(sourcePath, count) {
|
|
103
|
-
const { dir, stem, ext } = stemAndExt(resolve(sourcePath));
|
|
104
|
-
const width = String(count).length;
|
|
105
|
-
return Array.from({ length: count }, (_, i) => join(dir, `${stem}-${String(i + 1).padStart(width, '0')}${ext}`));
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Outputs for rasterising selected pages of a document to images — one file
|
|
109
|
-
* per page in `pages`, in the order given (`core/actions/convert.ts` and the
|
|
110
|
-
* CLI's execute path both hand this an already-`normalisePages`d list).
|
|
111
|
-
*
|
|
112
|
-
* Named by the page's own 1-based number, not its position in the
|
|
113
|
-
* selection — rendering pages 2-3 of a document writes `doc-2.jpg` and
|
|
114
|
-
* `doc-3.jpg`, never `doc-1.jpg`/`doc-2.jpg`, so a filename on disk always
|
|
115
|
-
* says which page it holds. `splitOutputPaths`-style zero-padding, but against
|
|
116
|
-
* the widest page *number* being written rather than the document's whole page
|
|
117
|
-
* count — a 2-page selection out of a 300-page document gets `-2`/`-3`, not
|
|
118
|
-
* `-002`/`-003`.
|
|
119
|
-
*
|
|
120
|
-
* Padding follows the largest page number, NOT how many pages were selected.
|
|
121
|
-
* Those differ whenever the selection is sparse, and using the count writes
|
|
122
|
-
* names that sort wrongly: pages 2 and 10 are two pages, so a count-derived
|
|
123
|
-
* width of 1 gives `doc-2.jpg` and `doc-10.jpg`, which `ls` and Finder order
|
|
124
|
-
* as `doc-10, doc-2`. That is the same defect `splitOutputPaths` above is
|
|
125
|
-
* careful to avoid, and the page picker makes sparse selections easy to reach.
|
|
126
|
-
*
|
|
127
|
-
* `sourceRoot` recreates the source tree inside `destination` exactly as
|
|
128
|
-
* `resolveOutputPath` does for a one-output conversion (`relativeToRoot`),
|
|
129
|
-
* so `--recursive -o out` nests a rasterised PDF beside the images it was
|
|
130
|
-
* scanned with instead of flattening it to the top of the destination.
|
|
131
|
-
*/
|
|
132
|
-
export function rasterOutputPaths(sourcePath, pages, target, destination, sourceRoot) {
|
|
133
|
-
const { stem } = stemAndExt(resolve(sourcePath));
|
|
134
|
-
const dir = destination
|
|
135
|
-
? relativeToRoot(resolve(destination), resolve(sourcePath, '..'), sourceRoot)
|
|
136
|
-
: resolve(sourcePath, '..');
|
|
137
|
-
const ext = primaryExtension(target);
|
|
138
|
-
const width = String(Math.max(1, ...pages.map((p) => p + 1))).length;
|
|
139
|
-
const names = pages.map((p) => join(dir, `${stem}-${String(p + 1).padStart(width, '0')}${ext}`));
|
|
140
|
-
const [first, ...rest] = names;
|
|
141
|
-
if (first === undefined) {
|
|
142
|
-
throw new Error('rasterOutputPaths requires at least one selected page');
|
|
143
|
-
}
|
|
144
|
-
return [first, ...rest];
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Outputs for an extract.
|
|
148
|
-
*
|
|
149
|
-
* Separate files are named by 1-based page number rather than sequence, so
|
|
150
|
-
* the name says where the page came from — `report-p12.pdf` is page 12, not
|
|
151
|
-
* the twelfth file.
|
|
152
|
-
*/
|
|
153
|
-
export function extractOutputPaths(sourcePath, pages, separate) {
|
|
154
|
-
if (!separate)
|
|
155
|
-
return [suffixedOutputPath(sourcePath, 'extract')];
|
|
156
|
-
const { dir, stem, ext } = stemAndExt(resolve(sourcePath));
|
|
157
|
-
return pages.map((p) => join(dir, `${stem}-p${p + 1}${ext}`));
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* The output for a merge.
|
|
161
|
-
*
|
|
162
|
-
* Every other operation derives its name from its one source; merge has none.
|
|
163
|
-
* The common parent folder is the best available answer — `~/invoices/*.pdf`
|
|
164
|
-
* becomes `invoices-merged.pdf` in that folder. When the inputs span folders
|
|
165
|
-
* there is no shared name worth using, so it falls back to the first file.
|
|
166
|
-
*/
|
|
167
|
-
export function mergeOutputPath(sourcePaths) {
|
|
168
|
-
const first = resolve(sourcePaths[0] ?? 'merged.pdf');
|
|
169
|
-
const dirs = new Set(sourcePaths.map((p) => resolve(p, '..')));
|
|
170
|
-
if (dirs.size === 1) {
|
|
171
|
-
const dir = resolve(first, '..');
|
|
172
|
-
return join(dir, `${basename(dir)}-merged.pdf`);
|
|
173
|
-
}
|
|
174
|
-
return suffixedOutputPath(first, 'merged');
|
|
175
|
-
}
|
|
176
|
-
//# sourceMappingURL=output-path.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"output-path.js","sourceRoot":"","sources":["../../src/core/output-path.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAC9C,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAA;AACvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAY/C;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,CAAS;IAC1C,IAAI,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,IAAI,CAAA;IACnD,IAAI,UAAU,CAAC,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE;QAAE,OAAO,IAAI,CAAA;IAC3D,OAAO,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE,CAAA;AAC1B,CAAC;AAED,SAAS,aAAa,CAAC,IAAY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AAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,GAAG,EAAE,CAAC,CAAC,CAAA;AAC/D,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAAC,WAAqB;IACnD,MAAM,KAAK,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,YAAY,CAAC,CAAA;IACrD,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAA;IAC9D,IAAI,IAAI,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QACpB,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;QAChC,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAC,GAAG,CAAC,aAAa,CAAC,CAAA;IACjD,CAAC;IACD,OAAO,kBAAkB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;AAC5C,CAAC"}
|
package/dist/core/pages.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { invalidPageRange } from './errors.js';
|
|
2
|
-
/**
|
|
3
|
-
* Parse a user-typed page selection into 0-based page indices.
|
|
4
|
-
*
|
|
5
|
-
* The grammar is comma-separated terms, each `N`, `N-M`, or `N-` meaning "to
|
|
6
|
-
* the end". Input is 1-based because that is what page numbers are to
|
|
7
|
-
* everyone who is not a programmer; the output is 0-based because that is
|
|
8
|
-
* what pdf-lib wants. That conversion happens here and nowhere else.
|
|
9
|
-
*
|
|
10
|
-
* Out-of-range pages are an error rather than a silent clamp: someone who
|
|
11
|
-
* types `1-100` on a 10-page document has misunderstood something, and
|
|
12
|
-
* quietly giving them 10 pages hides it.
|
|
13
|
-
*/
|
|
14
|
-
export function parseRanges(input, pageCount) {
|
|
15
|
-
const terms = input
|
|
16
|
-
.split(',')
|
|
17
|
-
.map((t) => t.trim())
|
|
18
|
-
.filter((t) => t !== '');
|
|
19
|
-
if (terms.length === 0) {
|
|
20
|
-
throw invalidPageRange(input, 'That selects no pages.', pageCount);
|
|
21
|
-
}
|
|
22
|
-
const pages = new Set();
|
|
23
|
-
const inBounds = (n, term) => {
|
|
24
|
-
if (!Number.isInteger(n) || n < 1 || n > pageCount) {
|
|
25
|
-
throw invalidPageRange(term, `"${term}" is outside 1 and ${pageCount}.`, pageCount);
|
|
26
|
-
}
|
|
27
|
-
return n;
|
|
28
|
-
};
|
|
29
|
-
for (const term of terms) {
|
|
30
|
-
const span = term.match(/^(\d+)\s*-\s*(\d*)$/);
|
|
31
|
-
if (span?.[1] !== undefined) {
|
|
32
|
-
const from = inBounds(Number(span[1]), term);
|
|
33
|
-
const to = span[2] === '' ? pageCount : inBounds(Number(span[2]), term);
|
|
34
|
-
if (to < from) {
|
|
35
|
-
throw invalidPageRange(term, `"${term}" ends before it starts.`, pageCount);
|
|
36
|
-
}
|
|
37
|
-
for (let p = from; p <= to; p++)
|
|
38
|
-
pages.add(p - 1);
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
if (!/^\d+$/.test(term)) {
|
|
42
|
-
throw invalidPageRange(term, `"${term}" is not a page number.`, pageCount);
|
|
43
|
-
}
|
|
44
|
-
pages.add(inBounds(Number(term), term) - 1);
|
|
45
|
-
}
|
|
46
|
-
return [...pages].sort((a, b) => a - b);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* The single ordering an explicit page selection is used in: unique, ascending.
|
|
50
|
-
*
|
|
51
|
-
* Extract names one output file per selected page (`extractOutputPaths`) and
|
|
52
|
-
* the PDF engine copies those pages itself. Both derive from this, because a
|
|
53
|
-
* selection can arrive in any order — the shell's page grid appends in the
|
|
54
|
-
* order pages were pressed — and two independent orderings meant a file named
|
|
55
|
-
* `doc-p5.pdf` could be handed the document's second page. One function, one
|
|
56
|
-
* order, so the name and the content cannot disagree.
|
|
57
|
-
*
|
|
58
|
-
* Deduped as well as sorted: two outputs named for one page would collide,
|
|
59
|
-
* and `write-safety.ts` refuses a job whose own outputs alias each other.
|
|
60
|
-
*/
|
|
61
|
-
export function normalisePages(pages) {
|
|
62
|
-
return [...new Set(pages)].sort((a, b) => a - b);
|
|
63
|
-
}
|
|
64
|
-
/** The inverse of `parseRanges`, for showing a selection back to the user. */
|
|
65
|
-
export function formatRanges(pages) {
|
|
66
|
-
if (pages.length === 0)
|
|
67
|
-
return '';
|
|
68
|
-
const sorted = [...new Set(pages)].sort((a, b) => a - b);
|
|
69
|
-
const parts = [];
|
|
70
|
-
let start = sorted[0];
|
|
71
|
-
let prev = start;
|
|
72
|
-
for (const page of sorted.slice(1)) {
|
|
73
|
-
if (page === prev + 1) {
|
|
74
|
-
prev = page;
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
parts.push(start === prev ? `${start + 1}` : `${start + 1}-${prev + 1}`);
|
|
78
|
-
start = page;
|
|
79
|
-
prev = page;
|
|
80
|
-
}
|
|
81
|
-
parts.push(start === prev ? `${start + 1}` : `${start + 1}-${prev + 1}`);
|
|
82
|
-
return parts.join(', ');
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Cut points and contiguous ranges are the same data seen two ways, which is
|
|
86
|
-
* what lets the grid and the typed range editor edit one selection.
|
|
87
|
-
*
|
|
88
|
-
* `cuts` holds the 0-based index of each page *after which* the document is
|
|
89
|
-
* cut. The result always partitions the document: every page lands in exactly
|
|
90
|
-
* one range, which is what makes split a partition rather than a selection.
|
|
91
|
-
*/
|
|
92
|
-
export function cutsToRanges(cuts, pageCount) {
|
|
93
|
-
const sorted = [...new Set(cuts)].sort((a, b) => a - b).filter((c) => c >= 0 && c < pageCount - 1);
|
|
94
|
-
const ranges = [];
|
|
95
|
-
let from = 0;
|
|
96
|
-
for (const cut of sorted) {
|
|
97
|
-
ranges.push({ from, to: cut });
|
|
98
|
-
from = cut + 1;
|
|
99
|
-
}
|
|
100
|
-
ranges.push({ from, to: pageCount - 1 });
|
|
101
|
-
return ranges;
|
|
102
|
-
}
|
|
103
|
-
/** The cut points implied by a list of contiguous ranges. */
|
|
104
|
-
export function rangesToCuts(ranges) {
|
|
105
|
-
return ranges.slice(0, -1).map((r) => r.to);
|
|
106
|
-
}
|
|
107
|
-
//# sourceMappingURL=pages.js.map
|
package/dist/core/pages.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pages.js","sourceRoot":"","sources":["../../src/core/pages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAQ9C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,SAAiB;IAC1D,MAAM,KAAK,GAAG,KAAK;SAChB,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;SACpB,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAA;IAE1B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,gBAAgB,CAAC,KAAK,EAAE,wBAAwB,EAAE,SAAS,CAAC,CAAA;IACpE,CAAC;IAED,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAA;IAC/B,MAAM,QAAQ,GAAG,CAAC,CAAS,EAAE,IAAY,EAAE,EAAE;QAC3C,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,SAAS,EAAE,CAAC;YACnD,MAAM,gBAAgB,CAAC,IAAI,EAAE,IAAI,IAAI,sBAAsB,SAAS,GAAG,EAAE,SAAS,CAAC,CAAA;QACrF,CAAC;QACD,OAAO,CAAC,CAAA;IAC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KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACzB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,IAAc,EAAE,SAAiB;IAC5D,MAAM,MAAM,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,SAAS,GAAG,CAAC,CAAC,CAAA;IAClG,MAAM,MAAM,GAAgB,EAAE,CAAA;IAC9B,IAAI,IAAI,GAAG,CAAC,CAAA;IACZ,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;QACzB,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,CAAA;QAC9B,IAAI,GAAG,GAAG,GAAG,CAAC,CAAA;IAChB,CAAC;IACD,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,SAAS,GAAG,CAAC,EAAE,CAAC,CAAA;IACxC,OAAO,MAAM,CAAA;AACf,CAAC;AAED,6DAA6D;AAC7D,MAAM,UAAU,YAAY,CAAC,MAAmB;IAC9C,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;AAC7C,CAAC"}
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import { PDFDocument, PDFName, PDFNumber, PDFRawStream } from 'pdf-lib';
|
|
2
|
-
import sharp from 'sharp';
|
|
3
|
-
/**
|
|
4
|
-
* Shrinking a PDF by re-encoding — and optionally shrinking — the images
|
|
5
|
-
* inside it.
|
|
6
|
-
*
|
|
7
|
-
* A PDF is a container, not an image format. Its own structure is lossless,
|
|
8
|
-
* which is why `/compress` used to refuse one outright — but a scan or a photo
|
|
9
|
-
* brochure is mostly JPEG data wrapped in that container, and re-encoding
|
|
10
|
-
* those streams at a lower quality is exactly the trade `/compress` offers
|
|
11
|
-
* everywhere else.
|
|
12
|
-
*
|
|
13
|
-
* Two levers, and the second is far the larger. Measured on a 3-page 300 dpi
|
|
14
|
-
* A4 scan of 5.7 MB: re-encoding alone at q40 reaches 1065 KB, while also
|
|
15
|
-
* reducing to 150 dpi reaches 111 KB — about ten times more saving. They are
|
|
16
|
-
* separate knobs because they cost different things: quality costs fine
|
|
17
|
-
* detail, resolution costs the ability to zoom and to print well.
|
|
18
|
-
*
|
|
19
|
-
* This module holds no engine or UI concerns: bytes in, bytes out, plus counts
|
|
20
|
-
* the caller can report. That keeps it usable by the quality path and by the
|
|
21
|
-
* target-size search, which calls it once per attempt.
|
|
22
|
-
*/
|
|
23
|
-
/**
|
|
24
|
-
* `/DCTDecode` is a JPEG stream stored verbatim, so Sharp can decode it
|
|
25
|
-
* straight out of the PDF and re-encode it.
|
|
26
|
-
*
|
|
27
|
-
* `/FlateDecode` is raw pixel data under zlib. Reconstructing an image from
|
|
28
|
-
* it means handling colour space (DeviceRGB, DeviceGray, ICCBased, indexed
|
|
29
|
-
* palettes), bits-per-component and decode arrays — getting any of those
|
|
30
|
-
* wrong writes a corrupt page that still opens, which is worse than declining.
|
|
31
|
-
* Those images are left exactly as they are and counted as skipped.
|
|
32
|
-
*/
|
|
33
|
-
const RECOMPRESSABLE = '/DCTDecode';
|
|
34
|
-
/**
|
|
35
|
-
* Every image XObject, with the width of the page that references it.
|
|
36
|
-
*
|
|
37
|
-
* The page width stands in for the width the image is drawn at. That is exact
|
|
38
|
-
* for a scan, where one image covers the page, and a scan is the case this
|
|
39
|
-
* feature exists for. Reading the true drawn size would mean parsing each
|
|
40
|
-
* content stream's transformation matrix; the pay-off is a better figure for
|
|
41
|
-
* collages, and the cost is a content-stream parser this does not need.
|
|
42
|
-
*/
|
|
43
|
-
function imageRefs(doc) {
|
|
44
|
-
const drawnWidth = new Map();
|
|
45
|
-
for (const page of doc.getPages()) {
|
|
46
|
-
const resources = page.node.Resources();
|
|
47
|
-
const xobjects = resources?.lookup(PDFName.of('XObject'));
|
|
48
|
-
for (const key of xobjects?.keys?.() ?? []) {
|
|
49
|
-
const entry = xobjects?.get?.(key);
|
|
50
|
-
if (entry !== undefined)
|
|
51
|
-
drawnWidth.set(String(entry), page.getSize().width);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
const out = [];
|
|
55
|
-
for (const [ref, obj] of doc.context.enumerateIndirectObjects()) {
|
|
56
|
-
if (!(obj instanceof PDFRawStream))
|
|
57
|
-
continue;
|
|
58
|
-
if (obj.dict.get(PDFName.of('Subtype'))?.toString() !== '/Image')
|
|
59
|
-
continue;
|
|
60
|
-
const width = drawnWidth.get(String(ref));
|
|
61
|
-
out.push({
|
|
62
|
-
ref,
|
|
63
|
-
stream: obj,
|
|
64
|
-
filter: String(obj.dict.get(PDFName.of('Filter'))),
|
|
65
|
-
...(width === undefined ? {} : { drawnWidthPt: width }),
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
return out;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* A numeric dictionary entry, or `undefined`.
|
|
72
|
-
*
|
|
73
|
-
* `dict.get()` is typed as the `PDFObject` base, which carries no `asNumber`.
|
|
74
|
-
* Narrowing through `PDFNumber` keeps that honest instead of casting: an entry
|
|
75
|
-
* that is an indirect reference, or absent, reads as unknown rather than as a
|
|
76
|
-
* number that happens to be `NaN`.
|
|
77
|
-
*/
|
|
78
|
-
function numberEntry(stream, key) {
|
|
79
|
-
const value = stream.dict.get(PDFName.of(key));
|
|
80
|
-
return value instanceof PDFNumber ? value.asNumber() : undefined;
|
|
81
|
-
}
|
|
82
|
-
/** Pixels per inch an image is actually shown at. */
|
|
83
|
-
function effectiveDpi(pixelWidth, drawnWidthPt) {
|
|
84
|
-
return pixelWidth / (drawnWidthPt / 72);
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* What a PDF offers before anything is re-encoded.
|
|
88
|
-
*
|
|
89
|
-
* Separating this from the work itself is what lets `/compress` refuse a
|
|
90
|
-
* text-only PDF up front, and distinguish that from a PDF whose images are
|
|
91
|
-
* simply a kind Forge cannot re-encode — two different answers a user
|
|
92
|
-
* deserves to be told apart.
|
|
93
|
-
*/
|
|
94
|
-
export function surveyDocument(doc) {
|
|
95
|
-
let compressible = 0;
|
|
96
|
-
let skipped = 0;
|
|
97
|
-
let maxDpi;
|
|
98
|
-
for (const image of imageRefs(doc)) {
|
|
99
|
-
if (image.filter !== RECOMPRESSABLE) {
|
|
100
|
-
skipped++;
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
compressible++;
|
|
104
|
-
const pixels = numberEntry(image.stream, 'Width');
|
|
105
|
-
if (pixels !== undefined && image.drawnWidthPt !== undefined) {
|
|
106
|
-
const dpi = effectiveDpi(pixels, image.drawnWidthPt);
|
|
107
|
-
if (maxDpi === undefined || dpi > maxDpi)
|
|
108
|
-
maxDpi = dpi;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return { compressible, skipped, ...(maxDpi === undefined ? {} : { maxDpi: Math.round(maxDpi) }) };
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* The same survey from raw bytes.
|
|
115
|
-
*
|
|
116
|
-
* `engines/pdf.ts`'s probe uses {@link surveyDocument} on the document it has
|
|
117
|
-
* already parsed instead — probing happens on every drop, and parsing a large
|
|
118
|
-
* PDF twice to answer one question is a cost the user feels as lag.
|
|
119
|
-
*/
|
|
120
|
-
export async function surveyPdfImages(bytes) {
|
|
121
|
-
return surveyDocument(await PDFDocument.load(bytes, { ignoreEncryption: true }));
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Re-encodes every JPEG image in `bytes`, optionally reducing resolution, and
|
|
125
|
-
* returns the rebuilt document.
|
|
126
|
-
*
|
|
127
|
-
* Always loads from the original bytes rather than mutating a shared document.
|
|
128
|
-
* The target-size search calls this once per attempt, and a compounding
|
|
129
|
-
* version would make the same settings yield a different size depending on
|
|
130
|
-
* what ran before — the search would then converge on a number that cannot be
|
|
131
|
-
* reproduced.
|
|
132
|
-
*
|
|
133
|
-
* An image is only replaced when the result is actually smaller. Raising the
|
|
134
|
-
* quality above what a stream already carries makes it bigger, and
|
|
135
|
-
* "compressing" a file into a larger one is a promise broken.
|
|
136
|
-
*/
|
|
137
|
-
export async function compressPdf(bytes, options) {
|
|
138
|
-
const doc = await PDFDocument.load(bytes, { ignoreEncryption: true });
|
|
139
|
-
let recompressed = 0;
|
|
140
|
-
let skipped = 0;
|
|
141
|
-
let fromDpi;
|
|
142
|
-
let toDpi;
|
|
143
|
-
for (const { ref, stream, filter, drawnWidthPt } of imageRefs(doc)) {
|
|
144
|
-
if (filter !== RECOMPRESSABLE) {
|
|
145
|
-
skipped++;
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
const source = Buffer.from(stream.contents);
|
|
149
|
-
const pixelWidth = numberEntry(stream, 'Width');
|
|
150
|
-
// Only downsample when the resolution is both known and above the cap.
|
|
151
|
-
let targetPixels;
|
|
152
|
-
if (options.dpi !== undefined && pixelWidth !== undefined && drawnWidthPt !== undefined) {
|
|
153
|
-
const current = effectiveDpi(pixelWidth, drawnWidthPt);
|
|
154
|
-
if (current > options.dpi) {
|
|
155
|
-
targetPixels = Math.max(1, Math.round((drawnWidthPt / 72) * options.dpi));
|
|
156
|
-
if (fromDpi === undefined || current > fromDpi)
|
|
157
|
-
fromDpi = Math.round(current);
|
|
158
|
-
toDpi = options.dpi;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
let encoded;
|
|
162
|
-
let encodedWidth;
|
|
163
|
-
let encodedHeight;
|
|
164
|
-
try {
|
|
165
|
-
const pipeline = sharp(source);
|
|
166
|
-
const resized = targetPixels === undefined
|
|
167
|
-
? pipeline
|
|
168
|
-
: pipeline.resize({ width: targetPixels, withoutEnlargement: true });
|
|
169
|
-
const { data, info } = await resized
|
|
170
|
-
.jpeg({ quality: options.quality, mozjpeg: true })
|
|
171
|
-
.toBuffer({ resolveWithObject: true });
|
|
172
|
-
encoded = data;
|
|
173
|
-
encodedWidth = info.width;
|
|
174
|
-
encodedHeight = info.height;
|
|
175
|
-
}
|
|
176
|
-
catch {
|
|
177
|
-
// A stream Sharp cannot read — a JPEG variant it declines, most
|
|
178
|
-
// likely CMYK or a damaged scan. Left untouched rather than dropped.
|
|
179
|
-
skipped++;
|
|
180
|
-
continue;
|
|
181
|
-
}
|
|
182
|
-
if (encoded.byteLength >= source.byteLength) {
|
|
183
|
-
skipped++;
|
|
184
|
-
continue;
|
|
185
|
-
}
|
|
186
|
-
// A whole replacement object rather than writing through `contents`,
|
|
187
|
-
// which pdf-lib declares read-only. Reusing the original dictionary keeps
|
|
188
|
-
// ColorSpace and BitsPerComponent exactly as they were; only the pixel
|
|
189
|
-
// data, its length, and — when resized — the dimensions change.
|
|
190
|
-
stream.dict.set(PDFName.of('Length'), doc.context.obj(encoded.byteLength));
|
|
191
|
-
if (targetPixels !== undefined && encodedWidth !== undefined && encodedHeight !== undefined) {
|
|
192
|
-
stream.dict.set(PDFName.of('Width'), doc.context.obj(encodedWidth));
|
|
193
|
-
stream.dict.set(PDFName.of('Height'), doc.context.obj(encodedHeight));
|
|
194
|
-
}
|
|
195
|
-
doc.context.assign(ref, PDFRawStream.of(stream.dict, encoded));
|
|
196
|
-
recompressed++;
|
|
197
|
-
}
|
|
198
|
-
return {
|
|
199
|
-
bytes: await doc.save(),
|
|
200
|
-
recompressed,
|
|
201
|
-
skipped,
|
|
202
|
-
...(fromDpi === undefined ? {} : { fromDpi }),
|
|
203
|
-
...(toDpi === undefined ? {} : { toDpi }),
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
//# sourceMappingURL=pdf-compress.js.map
|