Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·197 lines (180 loc) · 7.49 KB
/
Copy pathcli.js
File metadata and controls
executable file
·197 lines (180 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env node
/**
* TileWright CLI — tile a PDF pattern from the terminal.
*
* Exists for the designer with forty patterns to re-tile, and for anyone who
* wants this in a build script. Same engine as the browser app.
*/
'use strict';
var fs = require('fs');
var path = require('path');
var tiler = require('./src/tiler.js');
var units = require('./src/units.js');
var VERSION = tiler.VERSION;
var HELP = [
'',
' TileWright ' + VERSION + ' — layer-preserving PDF pattern tiler',
'',
' Usage',
' tilewright <input.pdf> [options]',
'',
' Options',
' -o, --out <file> output path (default: <input>.tiled.pdf)',
' -p, --paper <size> A0..A5, Letter, Legal, Tabloid, or WxH',
' (default: A4)',
' --landscape rotate the output paper',
' -v, --overlap <n> glue margin (default: 15)',
' -m, --margin <n> printer margin (default: 0)',
' -u, --unit <u> mm | cm | in | pt (default: mm)',
' -s, --scale <n> scale the pattern (default: 1)',
' --page <n> source page, 1-based (default: 1)',
' --labels <scheme> letterRow | numberRow | numeric',
' --no-calibration omit the print-scale check page',
' --no-map omit the assembly map page',
' --no-marks omit trim lines, shading and match marks',
' --title <text> document title',
' -i, --info report the file and exit, tiling nothing',
' -q, --quiet only print the output path',
' -h, --help this text',
' --version print version',
'',
' Examples',
' tilewright dress-a0.pdf',
' tilewright dress-a0.pdf -p Letter -v 0.5 -u in -o dress-letter.pdf',
' tilewright dress-a0.pdf --info',
' for f in patterns/*.pdf; do tilewright "$f" -p A4; done',
'',
].join('\n');
function parseArgs(argv) {
var o = { _: [] };
for (var i = 0; i < argv.length; i++) {
var a = argv[i];
var next = function () {
var v = argv[++i];
if (v === undefined) throw new Error('Missing value after ' + a);
return v;
};
switch (a) {
case '-o': case '--out': o.out = next(); break;
case '-p': case '--paper': o.paper = next(); break;
case '--landscape': o.landscape = true; break;
case '-v': case '--overlap': o.overlap = Number(next()); break;
case '-m': case '--margin': o.margin = Number(next()); break;
case '-u': case '--unit': o.unit = next(); break;
case '-s': case '--scale': o.scale = Number(next()); break;
case '--page': o.page = Number(next()); break;
case '--labels': o.labels = next(); break;
case '--no-calibration': o.calibration = false; break;
case '--no-map': o.map = false; break;
case '--no-marks': o.marks = false; break;
case '--title': o.title = next(); break;
case '-i': case '--info': o.info = true; break;
case '-q': case '--quiet': o.quiet = true; break;
case '-h': case '--help': o.help = true; break;
case '--version': o.version = true; break;
default:
if (a[0] === '-') throw new Error('Unknown option: ' + a);
o._.push(a);
}
}
return o;
}
/** "300x400" or "300x400mm" -> a custom paper spec. */
function parsePaper(str, unit) {
if (!str) return 'A4';
var m = /^(\d+(?:\.\d+)?)\s*[xX*]\s*(\d+(?:\.\d+)?)\s*([a-z]*)$/.exec(str.trim());
if (m) return { w: Number(m[1]), h: Number(m[2]), unit: m[3] || unit || 'mm' };
return str;
}
function fmtBytes(n) {
if (n < 1024) return n + ' B';
if (n < 1024 * 1024) return (n / 1024).toFixed(1) + ' kB';
return (n / 1024 / 1024).toFixed(2) + ' MB';
}
async function main() {
var args;
try { args = parseArgs(process.argv.slice(2)); }
catch (e) { console.error('\n ' + e.message + '\n Try --help.\n'); process.exit(2); }
if (args.version) { console.log(VERSION); return; }
if (args.help) { console.log(HELP); return; }
if (args._.length === 0) { console.log(HELP); process.exit(1); }
var input = args._[0];
if (!fs.existsSync(input)) {
console.error('\n No such file: ' + input + '\n');
process.exit(2);
}
var bytes;
try { bytes = fs.readFileSync(input); }
catch (e) { console.error('\n Could not read ' + input + ': ' + e.message + '\n'); process.exit(2); }
var unit = args.unit || 'mm';
// ---- --info ------------------------------------------------------------
if (args.info) {
var info;
try { info = await tiler.inspect(bytes); }
catch (e) { console.error('\n ' + e.message + '\n'); process.exit(1); }
console.log('');
console.log(' ' + path.basename(input));
console.log(' ' + info.pageCount + ' page' + (info.pageCount === 1 ? '' : 's'));
info.pages.forEach(function (p) {
console.log(' page ' + (p.index + 1) + ': ' +
units.fmt(p.width, unit) + ' x ' + units.fmt(p.height, unit) +
(p.rotation ? ' (rotated ' + p.rotation + ')' : ''));
});
if (info.layerCount) {
console.log(' ' + info.layerCount + ' layer' + (info.layerCount === 1 ? '' : 's') + ':');
info.layers.forEach(function (n) { console.log(' - ' + n); });
} else {
console.log(' no PDF layers');
}
console.log('');
return;
}
// ---- tile --------------------------------------------------------------
var opts = {
paper: parsePaper(args.paper, unit),
orientation: args.landscape ? 'landscape' : 'portrait',
overlap: args.overlap === undefined ? 15 : args.overlap,
margins: args.margin === undefined ? 0 : args.margin,
unit: unit,
scale: args.scale === undefined ? 1 : args.scale,
pageIndex: args.page === undefined ? 0 : args.page - 1,
labelScheme: args.labels,
calibration: args.calibration !== false,
assemblyMap: args.map !== false,
title: args.title || path.basename(input, path.extname(input)),
};
if (args.marks === false) {
opts.showTrim = false; opts.showOverlap = false; opts.showMatchMarks = false;
}
var result;
var t0 = Date.now();
try { result = await tiler.tile(bytes, opts); }
catch (e) { console.error('\n ' + e.message + '\n'); process.exit(1); }
var out = args.out || input.replace(/\.pdf$/i, '') + '.tiled.pdf';
try { fs.writeFileSync(out, result.bytes); }
catch (e) { console.error('\n Could not write ' + out + ': ' + e.message + '\n'); process.exit(2); }
if (args.quiet) { console.log(out); return; }
var g = result.grid;
console.log('');
console.log(' ' + path.basename(input) + ' -> ' + path.basename(out));
console.log(' ' + units.fmt(g.scaledSource.w, unit) + ' x ' + units.fmt(g.scaledSource.h, unit) +
' tiled to ' + g.cols + ' x ' + g.rows + ' = ' + result.emitted + ' sheet' +
(result.emitted === 1 ? '' : 's') +
(result.skipped ? ' (' + result.skipped + ' blank skipped)' : ''));
console.log(' overlap ' + units.fmt(g.overlap, unit) +
' scale ' + (g.scale * 100).toFixed(1) + '%' +
' ' + fmtBytes(result.bytes.length) +
' ' + (Date.now() - t0) + 'ms');
if (result.layers.restored) {
console.log(' layers preserved: ' + result.layers.names.filter(Boolean).join(', '));
} else {
console.log(' no PDF layers in source');
}
result.warnings.forEach(function (w) { console.log(' ! ' + w); });
console.log('');
}
main().catch(function (e) {
console.error('\n Unexpected error: ' + (e && e.message) + '\n');
if (process.env.TILEWRIGHT_DEBUG) console.error(e.stack);
process.exit(1);
});