io.js
10.1 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
"use strict";
// const { basename, extname } = require("path");
import { basename, extname } from "../../path-browserify/index.js";
//
// Mime type <-> File extension mappings
//
class Format {
constructor() {
let isWeb = (() => typeof global == "undefined")(),
png = "image/png",
jpg = "image/jpeg",
jpeg = "image/jpeg",
webp = "image/webp",
pdf = "application/pdf",
svg = "image/svg+xml";
Object.assign(this, {
toMime: this.toMime.bind(this),
fromMime: this.fromMime.bind(this),
expected: isWeb
? `"png", "jpg", or "webp"`
: `"png", "jpg", "pdf", or "svg"`,
formats: isWeb ? { png, jpg, jpeg, webp } : { png, jpg, jpeg, pdf, svg },
mimes: isWeb
? { [png]: "png", [jpg]: "jpg", [webp]: "webp" }
: { [png]: "png", [jpg]: "jpg", [pdf]: "pdf", [svg]: "svg" }
});
}
toMime(ext) {
return this.formats[(ext || "").replace(/^\./, "").toLowerCase()];
}
fromMime(mime) {
return this.mimes[mime];
}
}
//
// Validation of the options dict shared by the Canvas saveAs, toBuffer, and toDataURL methods
//
function options(
pages,
{
filename = "",
extension = "",
format,
page,
quality,
matte,
density,
outline,
archive
} = {}
) {
var { fromMime, toMime, expected } = new Format(),
archive = archive || "canvas",
ext = format || extension.replace(/@\d+x$/i, "") || extname(filename),
format = fromMime(toMime(ext) || ext),
mime = toMime(format),
pp = pages.length;
if (!ext)
throw new Error(
`Cannot determine image format (use a filename extension or 'format' argument)`
);
if (!format)
throw new Error(`Unsupported file format "${ext}" (expected ${expected})`);
if (!pp)
throw new RangeError(
`Canvas has no associated contexts (try calling getContext or newPage first)`
);
let padding,
isSequence,
pattern = filename.replace(/{(\d*)}/g, (_, width) => {
isSequence = true;
width = parseInt(width, 10);
padding = isFinite(width) ? width : isFinite(padding) ? padding : -1;
return "{}";
});
// allow negative indexing if a specific page is specified
let idx = page > 0 ? page - 1 : page < 0 ? pp + page : undefined;
if ((isFinite(idx) && idx < 0) || idx >= pp)
throw new RangeError(
pp == 1
? `Canvas only has a ‘page 1’ (${idx} is out of bounds)`
: `Canvas has pages 1–${pp} (${idx} is out of bounds)`
);
pages = isFinite(idx)
? [pages[idx]]
: isSequence || format == "pdf"
? pages
: pages.slice(-1); // default to the 'current' context
if (quality === undefined) {
quality = 0.92;
} else {
if (
typeof quality != "number" ||
!isFinite(quality) ||
quality < 0 ||
quality > 1
) {
throw new TypeError(
"The quality option must be an number in the 0.0–1.0 range"
);
}
}
if (density === undefined) {
let m = (extension || basename(filename, ext)).match(/@(\d+)x$/i);
density = m ? parseInt(m[1], 10) : 1;
} else if (
typeof density != "number" ||
!Number.isInteger(density) ||
density < 1
) {
throw new TypeError("The density option must be a non-negative integer");
}
if (outline === undefined) {
outline = true;
} else if (format == "svg") {
outline = !!outline;
}
return {
filename,
pattern,
format,
mime,
pages,
padding,
quality,
matte,
density,
outline,
archive
};
}
//
// Zip (pace Phil Katz & q.v. https://github.com/jimmywarting/StreamSaver.js)
//
class Crc32 {
static for(data) {
return new Crc32().append(data).get();
}
constructor() {
this.crc = -1;
}
get() {
return ~this.crc;
}
append(data) {
var crc = this.crc | 0,
table = this.table;
for (var offset = 0, len = data.length | 0; offset < len; offset++) {
crc = (crc >>> 8) ^ table[(crc ^ data[offset]) & 0xff];
}
this.crc = crc;
return this;
}
}
Crc32.prototype.table = (() => {
var i,
j,
t,
table = [];
for (i = 0; i < 256; i++) {
t = i;
for (j = 0; j < 8; j++) {
t = t & 1 ? (t >>> 1) ^ 0xedb88320 : t >>> 1;
}
table[i] = t;
}
return table;
})();
function calloc(size) {
let array = new Uint8Array(size),
view = new DataView(array.buffer),
buf = {
array,
view,
size,
set8(at, to) {
view.setUint8(at, to);
return buf;
},
set16(at, to) {
view.setUint16(at, to, true);
return buf;
},
set32(at, to) {
view.setUint32(at, to, true);
return buf;
},
bytes(at, to) {
array.set(to, at);
return buf;
}
};
return buf;
}
// const TextEncoder=require('util').TextEncoder
class Zip {
constructor(directory) {
let now = new Date();
Object.assign(this, {
directory,
offset: 0,
files: [],
time:
(((now.getHours() << 6) | now.getMinutes()) << 5) |
(now.getSeconds() / 2),
date:
((((now.getFullYear() - 1980) << 4) | (now.getMonth() + 1)) << 5) |
now.getDate()
});
this.add(directory);
}
async add(filename, blob) {
let folder = !blob,
name = Zip.encoder.encode(`${this.directory}/${folder ? "" : filename}`),
data = new Uint8Array(folder ? 0 : await blob.arrayBuffer()),
preamble = 30 + name.length,
descriptor = preamble + data.length,
postamble = 16,
{ offset } = this;
let header = calloc(26)
.set32(0, 0x08080014) // zip version
.set16(6, this.time) // time
.set16(8, this.date) // date
.set32(10, Crc32.for(data)) // checksum
.set32(14, data.length) // compressed size (w/ zero compression)
.set32(18, data.length) // un-compressed size
.set16(22, name.length); // filename length (utf8 bytes)
offset += preamble;
let payload = calloc(preamble + data.length + postamble)
.set32(0, 0x04034b50) // local header signature
.bytes(4, header.array) // ...header fields...
.bytes(30, name) // filename
.bytes(preamble, data); // blob bytes
offset += data.length;
payload
.set32(descriptor, 0x08074b50) // signature
.bytes(descriptor + 4, header.array.slice(10, 22)); // length & filemame
offset += postamble;
this.files.push({ offset, folder, name, header, payload });
this.offset = offset;
}
toBuffer() {
// central directory record
let length = this.files.reduce(
(len, { name }) => 46 + name.length + len,
0
),
cdr = calloc(length + 22),
index = 0;
for (var { offset, name, header, folder } of this.files) {
cdr
.set32(index, 0x02014b50) // archive file signature
.set16(index + 4, 0x0014) // version
.bytes(index + 6, header.array) // ...header fields...
.set8(index + 38, folder ? 0x10 : 0) // is_dir flag
.set32(index + 42, offset) // file offset
.bytes(index + 46, name); // filename
index += 46 + name.length;
}
cdr
.set32(index, 0x06054b50) // signature
.set16(index + 8, this.files.length) // № files per-segment
.set16(index + 10, this.files.length) // № files this segment
.set32(index + 12, length) // central directory length
.set32(index + 16, this.offset); // file-offset of directory
// concatenated zipfile data
let output = new Uint8Array(this.offset + cdr.size),
cursor = 0;
for (var { payload } of this.files) {
output.set(payload.array, cursor);
cursor += payload.size;
}
output.set(cdr.array, cursor);
return output;
}
get blob() {
return new Blob([this.toBuffer()], { type: "application/zip" });
}
}
Zip.encoder = new TextEncoder();
//
// Browser helpers for converting canvas elements to blobs/buffers/files/zips
//
const asBlob = (canvas, mime, quality, matte) => {
if (matte) {
let { width, height } = canvas,
comp = Object.assign(document.createElement("canvas"), { width, height }),
ctx = comp.getContext("2d");
ctx.fillStyle = matte;
ctx.fillRect(0, 0, width, height);
ctx.drawImage(canvas, 0, 0);
canvas = comp;
}
return new Promise((res, rej) => canvas.toBlob(res, mime, quality));
};
const asBuffer = (...args) => asBlob(...args).then(b => b.arrayBuffer());
const asDownload = async (canvas, mime, quality, matte, filename) => {
_download(filename, await asBlob(canvas, mime, quality, matte));
};
const asZipDownload = async (
pages,
mime,
quality,
matte,
archive,
pattern,
padding
) => {
let filenames = i =>
pattern.replace("{}", String(i + 1).padStart(padding, "0")),
folder = basename(archive, ".zip") || "archive",
zip = new Zip(folder);
await Promise.all(
pages.map(async (page, i) => {
let filename = filenames(i); // serialize filename(s) before awaiting
await zip.add(filename, await asBlob(page, mime, quality, matte));
})
);
_download(`${folder}.zip`, zip.blob);
};
const _download = (filename, blob) => {
const href = window.URL.createObjectURL(blob),
link = document.createElement("a");
link.style.display = "none";
link.href = href;
link.setAttribute("download", filename);
if (typeof link.download === "undefined") {
link.setAttribute("target", "_blank");
}
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
setTimeout(() => window.URL.revokeObjectURL(href), 100);
};
const atScale = (pages, density, matte) =>
pages.map(page => {
if (density == 1 && !matte) return page.canvas;
let scaled = document.createElement("canvas"),
ctx = scaled.getContext("2d"),
src = page.canvas ? page.canvas : page;
scaled.width = src.width * density;
scaled.height = src.height * density;
if (matte) {
ctx.fillStyle = matte;
ctx.fillRect(0, 0, scaled.width, scaled.height);
}
ctx.scale(density, density);
ctx.drawImage(src, 0, 0);
return scaled;
});
const obj = { asBuffer, asDownload, asZipDownload, atScale, options };
export default obj;
// module.exports = { asBuffer, asDownload, asZipDownload, atScale, options };