@arbidocs/react 0.4.70 → 0.4.71
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 +6 -4
- package/dist/ProductionDialog-BRrZ5Lj5.d.cts +84 -0
- package/dist/ProductionDialog-DUC9BXWS.d.ts +84 -0
- package/dist/annotations/freeze/index.cjs +45 -1100
- package/dist/annotations/freeze/index.cjs.map +1 -1
- package/dist/annotations/freeze/index.d.cts +2 -85
- package/dist/annotations/freeze/index.d.ts +2 -85
- package/dist/annotations/freeze/index.js +4 -1084
- package/dist/annotations/freeze/index.js.map +1 -1
- package/dist/annotations/index.d.cts +3 -51
- package/dist/annotations/index.d.ts +3 -51
- package/dist/artifacts/index.cjs +9 -9
- package/dist/artifacts/index.js +2 -2
- package/dist/bundle-BnCSbHy2.d.cts +85 -0
- package/dist/bundle-BnCSbHy2.d.ts +85 -0
- package/dist/bundle-N2JIJ2T6.js +4 -0
- package/dist/bundle-N2JIJ2T6.js.map +1 -0
- package/dist/bundle-T6BEVYHE.cjs +13 -0
- package/dist/bundle-T6BEVYHE.cjs.map +1 -0
- package/dist/{chunk-SYPUFZGA.js → chunk-6ERJUIMQ.js} +3 -3
- package/dist/{chunk-SYPUFZGA.js.map → chunk-6ERJUIMQ.js.map} +1 -1
- package/dist/{chunk-7CL7QHT6.cjs → chunk-6TFD5OS5.cjs} +25 -25
- package/dist/{chunk-7CL7QHT6.cjs.map → chunk-6TFD5OS5.cjs.map} +1 -1
- package/dist/{chunk-ZMTLGO3K.cjs → chunk-IIT7DMQL.cjs} +6 -2
- package/dist/chunk-IIT7DMQL.cjs.map +1 -0
- package/dist/chunk-QJXFRP5R.cjs +1103 -0
- package/dist/chunk-QJXFRP5R.cjs.map +1 -0
- package/dist/{chunk-J4MX2W4H.js → chunk-WBYNKFUO.js} +6 -3
- package/dist/chunk-WBYNKFUO.js.map +1 -0
- package/dist/chunk-ZJMUNGOY.js +1088 -0
- package/dist/chunk-ZJMUNGOY.js.map +1 -0
- package/dist/document-tools/index.cjs +223 -8
- package/dist/document-tools/index.cjs.map +1 -1
- package/dist/document-tools/index.d.cts +139 -9
- package/dist/document-tools/index.d.ts +139 -9
- package/dist/document-tools/index.js +207 -9
- package/dist/document-tools/index.js.map +1 -1
- package/dist/index.cjs +153 -149
- package/dist/index.d.cts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +6 -6
- package/dist/ui/index.cjs +9 -9
- package/dist/ui/index.js +1 -1
- package/dist/viewers/index.cjs +2 -2
- package/dist/viewers/index.js +1 -1
- package/package.json +3 -3
- package/dist/chunk-J4MX2W4H.js.map +0 -1
- package/dist/chunk-ZMTLGO3K.cjs.map +0 -1
- package/dist/store-CqkkD7Ho.d.cts +0 -35
- package/dist/store-D1nhbkgk.d.ts +0 -35
|
@@ -1,727 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { createPdfJsRasterizer, browserCanvasFactory, freezePdf } from '../../chunk-ZJMUNGOY.js';
|
|
2
|
+
export { DEFAULT_DPI, FreezeError, MAX_PAGE_PIXELS, batesLabel, browserCanvasFactory, buildBundle, burnMarks, createPdfJsRasterizer, freezePdf, readProvenance } from '../../chunk-ZJMUNGOY.js';
|
|
3
|
+
import { isEffectiveRedaction } from '../../chunk-COWZGCL5.js';
|
|
3
4
|
import { pdfjs } from 'react-pdf';
|
|
4
|
-
import qrcode from 'qrcode-generator';
|
|
5
5
|
|
|
6
|
-
// src/annotations/freeze/coords.ts
|
|
7
|
-
function normaliseRotation(angle) {
|
|
8
|
-
const a = (Math.round(angle / 90) * 90 % 360 + 360) % 360;
|
|
9
|
-
return a;
|
|
10
|
-
}
|
|
11
|
-
function displayedSize(g) {
|
|
12
|
-
return g.rotation === 90 || g.rotation === 270 ? { width: g.height, height: g.width } : { width: g.width, height: g.height };
|
|
13
|
-
}
|
|
14
|
-
function displayedToUser(g, dx, dy) {
|
|
15
|
-
switch (g.rotation) {
|
|
16
|
-
case 0:
|
|
17
|
-
return { x: g.x + dx, y: g.y + g.height - dy };
|
|
18
|
-
case 90:
|
|
19
|
-
return { x: g.x + dy, y: g.y + dx };
|
|
20
|
-
case 180:
|
|
21
|
-
return { x: g.x + g.width - dx, y: g.y + dy };
|
|
22
|
-
case 270:
|
|
23
|
-
return { x: g.x + g.width - dy, y: g.y + g.height - dx };
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function normToUserRect(g, r) {
|
|
27
|
-
const size = displayedSize(g);
|
|
28
|
-
const a = displayedToUser(g, r.x * size.width, r.y * size.height);
|
|
29
|
-
const b = displayedToUser(g, (r.x + r.w) * size.width, (r.y + r.h) * size.height);
|
|
30
|
-
return {
|
|
31
|
-
x: Math.min(a.x, b.x),
|
|
32
|
-
y: Math.min(a.y, b.y),
|
|
33
|
-
width: Math.abs(b.x - a.x),
|
|
34
|
-
height: Math.abs(b.y - a.y)
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// src/annotations/freeze/hash.ts
|
|
39
|
-
async function sha256Hex(bytes) {
|
|
40
|
-
const copy = new Uint8Array(bytes.byteLength);
|
|
41
|
-
copy.set(bytes);
|
|
42
|
-
const digest = await globalThis.crypto.subtle.digest("SHA-256", copy.buffer);
|
|
43
|
-
return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
44
|
-
}
|
|
45
|
-
function canonicalJson(value) {
|
|
46
|
-
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
|
|
47
|
-
if (value && typeof value === "object") {
|
|
48
|
-
const entries = Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
49
|
-
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalJson(v)}`).join(",")}}`;
|
|
50
|
-
}
|
|
51
|
-
return JSON.stringify(value);
|
|
52
|
-
}
|
|
53
|
-
function layerFingerprint(layer) {
|
|
54
|
-
return {
|
|
55
|
-
...layer,
|
|
56
|
-
annotations: layer.annotations.map((a) => {
|
|
57
|
-
return a.kind === "redaction" ? { ...a, text: void 0 } : a;
|
|
58
|
-
})
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
var hashLayer = (layer) => sha256Hex(new TextEncoder().encode(canonicalJson(layerFingerprint(layer))));
|
|
62
|
-
|
|
63
|
-
// src/annotations/freeze/types.ts
|
|
64
|
-
var ENCRYPTED_MESSAGE = "This PDF is password-protected, so a copy cannot be frozen from it. Remove the protection first.";
|
|
65
|
-
var FreezeError = class extends Error {
|
|
66
|
-
code;
|
|
67
|
-
constructor(code, message) {
|
|
68
|
-
super(message);
|
|
69
|
-
this.name = "FreezeError";
|
|
70
|
-
this.code = code;
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
// src/annotations/freeze/vector.ts
|
|
75
|
-
var HIGHLIGHT_OPACITY = 0.7;
|
|
76
|
-
var MARKER_SIZE_PT = 12;
|
|
77
|
-
var MARKER_COLOR = "#ffd43b";
|
|
78
|
-
var MARKER_BORDER = "#7a5c00";
|
|
79
|
-
var CAP_HEIGHT = 0.72;
|
|
80
|
-
function hexToRgb(hex) {
|
|
81
|
-
const n = parseInt(hex.slice(1), 16);
|
|
82
|
-
return rgb((n >> 16 & 255) / 255, (n >> 8 & 255) / 255, (n & 255) / 255);
|
|
83
|
-
}
|
|
84
|
-
function pageGeometry(page) {
|
|
85
|
-
const media = page.getMediaBox();
|
|
86
|
-
const crop = page.getCropBox();
|
|
87
|
-
const x1 = Math.max(media.x, crop.x);
|
|
88
|
-
const y1 = Math.max(media.y, crop.y);
|
|
89
|
-
const x2 = Math.min(media.x + media.width, crop.x + crop.width);
|
|
90
|
-
const y2 = Math.min(media.y + media.height, crop.y + crop.height);
|
|
91
|
-
const usable = x2 > x1 && y2 > y1;
|
|
92
|
-
return {
|
|
93
|
-
x: usable ? x1 : media.x,
|
|
94
|
-
y: usable ? y1 : media.y,
|
|
95
|
-
width: usable ? x2 - x1 : media.width,
|
|
96
|
-
height: usable ? y2 - y1 : media.height,
|
|
97
|
-
rotation: normaliseRotation(page.getRotation().angle)
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
function numberComments(marks) {
|
|
101
|
-
const comments = marks.filter((a) => a.kind === "comment").sort(
|
|
102
|
-
(a, b) => a.page - b.page || a.rect.y - b.rect.y || a.rect.x - b.rect.x || (a.id < b.id ? -1 : 1)
|
|
103
|
-
);
|
|
104
|
-
return new Map(comments.map((c, i) => [c.id, i + 1]));
|
|
105
|
-
}
|
|
106
|
-
var TextSanitiser = class {
|
|
107
|
-
supported;
|
|
108
|
-
replaced = /* @__PURE__ */ new Set();
|
|
109
|
-
constructor(font) {
|
|
110
|
-
this.supported = new Set(font.getCharacterSet());
|
|
111
|
-
}
|
|
112
|
-
clean(text) {
|
|
113
|
-
let out = "";
|
|
114
|
-
for (const ch of text) {
|
|
115
|
-
const cp = ch.codePointAt(0);
|
|
116
|
-
if (this.supported.has(cp)) out += ch;
|
|
117
|
-
else {
|
|
118
|
-
out += "?";
|
|
119
|
-
this.replaced.add(ch);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return out;
|
|
123
|
-
}
|
|
124
|
-
supports(text) {
|
|
125
|
-
for (const ch of text) if (!this.supported.has(ch.codePointAt(0))) return false;
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
async function createVectorContext(doc, marks, comments) {
|
|
130
|
-
const regular = await doc.embedFont(StandardFonts.Helvetica);
|
|
131
|
-
const bold = await doc.embedFont(StandardFonts.HelveticaBold);
|
|
132
|
-
return {
|
|
133
|
-
doc,
|
|
134
|
-
regular,
|
|
135
|
-
bold,
|
|
136
|
-
numbers: numberComments(marks),
|
|
137
|
-
comments,
|
|
138
|
-
sanitiser: new TextSanitiser(regular)
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
function drawHighlight(page, g, a) {
|
|
142
|
-
for (const r of a.rects) {
|
|
143
|
-
const u = normToUserRect(g, r);
|
|
144
|
-
page.drawRectangle({
|
|
145
|
-
x: u.x,
|
|
146
|
-
y: u.y,
|
|
147
|
-
width: u.width,
|
|
148
|
-
height: u.height,
|
|
149
|
-
color: hexToRgb(a.color),
|
|
150
|
-
opacity: HIGHLIGHT_OPACITY,
|
|
151
|
-
blendMode: BlendMode.Multiply
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
function drawBox(page, g, a) {
|
|
156
|
-
const u = normToUserRect(g, a.rect);
|
|
157
|
-
page.drawRectangle({
|
|
158
|
-
x: u.x,
|
|
159
|
-
y: u.y,
|
|
160
|
-
width: u.width,
|
|
161
|
-
height: u.height,
|
|
162
|
-
borderColor: hexToRgb(a.color),
|
|
163
|
-
borderWidth: a.strokeWidth,
|
|
164
|
-
borderOpacity: 1,
|
|
165
|
-
opacity: 0
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
function fitFontSize(font, text, widthPt, heightPt) {
|
|
169
|
-
const probe = font.widthOfTextAtSize(text || " ", 100);
|
|
170
|
-
const byWidth = probe > 0 ? widthPt * 0.9 * 100 / probe : heightPt;
|
|
171
|
-
return Math.max(1, Math.min(heightPt * 0.6, byWidth));
|
|
172
|
-
}
|
|
173
|
-
function drawCentredText(page, g, font, text, box, size, color) {
|
|
174
|
-
const textWidth = font.widthOfTextAtSize(text, size);
|
|
175
|
-
const dx = box.x + (box.w - textWidth) / 2;
|
|
176
|
-
const dy = box.y + box.h / 2 + size * CAP_HEIGHT / 2;
|
|
177
|
-
const origin = displayedToUser(g, dx, dy);
|
|
178
|
-
page.drawText(text, { x: origin.x, y: origin.y, size, font, color, rotate: degrees(g.rotation) });
|
|
179
|
-
}
|
|
180
|
-
function drawStamp(page, g, a, ctx) {
|
|
181
|
-
if (!ctx.sanitiser.supports(a.label)) {
|
|
182
|
-
throw new FreezeError(
|
|
183
|
-
"unsupported-text",
|
|
184
|
-
`Stamp "${a.label}" uses characters a vector stamp cannot draw; freeze as an image instead`
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
const u = normToUserRect(g, a.rect);
|
|
188
|
-
const color = hexToRgb(a.color);
|
|
189
|
-
page.drawRectangle({
|
|
190
|
-
x: u.x,
|
|
191
|
-
y: u.y,
|
|
192
|
-
width: u.width,
|
|
193
|
-
height: u.height,
|
|
194
|
-
borderColor: color,
|
|
195
|
-
borderWidth: 2,
|
|
196
|
-
opacity: 0,
|
|
197
|
-
borderOpacity: 1
|
|
198
|
-
});
|
|
199
|
-
const size = displayedSize(g);
|
|
200
|
-
const box = {
|
|
201
|
-
x: a.rect.x * size.width,
|
|
202
|
-
y: a.rect.y * size.height,
|
|
203
|
-
w: a.rect.w * size.width,
|
|
204
|
-
h: a.rect.h * size.height
|
|
205
|
-
};
|
|
206
|
-
const fontSize = fitFontSize(ctx.bold, a.label, box.w, box.h);
|
|
207
|
-
drawCentredText(page, g, ctx.bold, a.label, box, fontSize, color);
|
|
208
|
-
}
|
|
209
|
-
function drawCommentMarker(page, g, a, ctx) {
|
|
210
|
-
const n = ctx.numbers.get(a.id);
|
|
211
|
-
if (n === void 0) return;
|
|
212
|
-
const size = displayedSize(g);
|
|
213
|
-
const box = {
|
|
214
|
-
x: Math.min(a.rect.x * size.width, size.width - MARKER_SIZE_PT),
|
|
215
|
-
y: Math.min(a.rect.y * size.height, size.height - MARKER_SIZE_PT),
|
|
216
|
-
w: MARKER_SIZE_PT,
|
|
217
|
-
h: MARKER_SIZE_PT
|
|
218
|
-
};
|
|
219
|
-
const u = normToUserRect(g, {
|
|
220
|
-
x: box.x / size.width,
|
|
221
|
-
y: box.y / size.height,
|
|
222
|
-
w: box.w / size.width,
|
|
223
|
-
h: box.h / size.height
|
|
224
|
-
});
|
|
225
|
-
page.drawRectangle({
|
|
226
|
-
x: u.x,
|
|
227
|
-
y: u.y,
|
|
228
|
-
width: u.width,
|
|
229
|
-
height: u.height,
|
|
230
|
-
color: hexToRgb(MARKER_COLOR),
|
|
231
|
-
borderColor: hexToRgb(MARKER_BORDER),
|
|
232
|
-
borderWidth: 0.75
|
|
233
|
-
});
|
|
234
|
-
drawCentredText(page, g, ctx.bold, String(n), box, 7, hexToRgb(MARKER_BORDER));
|
|
235
|
-
}
|
|
236
|
-
function drawVectorMark(page, a, ctx) {
|
|
237
|
-
const g = pageGeometry(page);
|
|
238
|
-
switch (a.kind) {
|
|
239
|
-
case "highlight":
|
|
240
|
-
return drawHighlight(page, g, a);
|
|
241
|
-
case "box":
|
|
242
|
-
return drawBox(page, g, a);
|
|
243
|
-
case "stamp":
|
|
244
|
-
return drawStamp(page, g, a, ctx);
|
|
245
|
-
case "comment":
|
|
246
|
-
return ctx.comments === "omit" ? void 0 : drawCommentMarker(page, g, a, ctx);
|
|
247
|
-
case "redaction":
|
|
248
|
-
throw new Error("redactions are never drawn as vectors");
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
function wrap(font, text, size, maxWidth) {
|
|
252
|
-
const lines = [];
|
|
253
|
-
for (const paragraph of text.split(/\r?\n/)) {
|
|
254
|
-
let line = "";
|
|
255
|
-
for (const word of paragraph.split(/\s+/).filter(Boolean)) {
|
|
256
|
-
const candidate = line ? `${line} ${word}` : word;
|
|
257
|
-
if (font.widthOfTextAtSize(candidate, size) <= maxWidth || !line) line = candidate;
|
|
258
|
-
else {
|
|
259
|
-
lines.push(line);
|
|
260
|
-
line = word;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
lines.push(line);
|
|
264
|
-
}
|
|
265
|
-
return lines;
|
|
266
|
-
}
|
|
267
|
-
function appendCommentAppendix(ctx, comments, pageSize2) {
|
|
268
|
-
if (comments.length === 0) return;
|
|
269
|
-
const { doc, regular, bold, sanitiser } = ctx;
|
|
270
|
-
const margin = 54;
|
|
271
|
-
const lineHeight = 13;
|
|
272
|
-
const maxWidth = pageSize2[0] - margin * 2;
|
|
273
|
-
let page = doc.addPage(pageSize2);
|
|
274
|
-
let y = pageSize2[1] - margin;
|
|
275
|
-
const ensure = (needed) => {
|
|
276
|
-
if (y - needed < margin) {
|
|
277
|
-
page = doc.addPage(pageSize2);
|
|
278
|
-
y = pageSize2[1] - margin;
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
const line = (text, font, size, indent = 0) => {
|
|
282
|
-
ensure(lineHeight);
|
|
283
|
-
page.drawText(sanitiser.clean(text), {
|
|
284
|
-
x: margin + indent,
|
|
285
|
-
y: y - size,
|
|
286
|
-
size,
|
|
287
|
-
font,
|
|
288
|
-
color: rgb(0.1, 0.1, 0.1)
|
|
289
|
-
});
|
|
290
|
-
y -= lineHeight;
|
|
291
|
-
};
|
|
292
|
-
line("Comments", bold, 14);
|
|
293
|
-
y -= 6;
|
|
294
|
-
const ordered = [...comments].sort(
|
|
295
|
-
(a, b) => (ctx.numbers.get(a.id) ?? 0) - (ctx.numbers.get(b.id) ?? 0)
|
|
296
|
-
);
|
|
297
|
-
for (const c of ordered) {
|
|
298
|
-
ensure(lineHeight * 3);
|
|
299
|
-
line(
|
|
300
|
-
`[${ctx.numbers.get(c.id)}] Page ${c.page} - ${c.author}${c.resolved ? " (resolved)" : ""}`,
|
|
301
|
-
bold,
|
|
302
|
-
10
|
|
303
|
-
);
|
|
304
|
-
for (const l of wrap(regular, sanitiser.clean(c.body), 10, maxWidth - 12))
|
|
305
|
-
line(l, regular, 10, 12);
|
|
306
|
-
for (const r of c.replies) {
|
|
307
|
-
line(`${r.author}:`, bold, 9, 24);
|
|
308
|
-
for (const l of wrap(regular, sanitiser.clean(r.body), 9, maxWidth - 36))
|
|
309
|
-
line(l, regular, 9, 36);
|
|
310
|
-
}
|
|
311
|
-
y -= 8;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// src/annotations/freeze/raster.ts
|
|
316
|
-
var MAX_PAGE_PIXELS = 36e6;
|
|
317
|
-
var MARKER_FILL = "#ffd43b";
|
|
318
|
-
var MARKER_INK = "#7a5c00";
|
|
319
|
-
function browserCanvasFactory() {
|
|
320
|
-
return (width, height) => {
|
|
321
|
-
if (typeof OffscreenCanvas !== "undefined") {
|
|
322
|
-
const canvas2 = new OffscreenCanvas(width, height);
|
|
323
|
-
const ctx2 = canvas2.getContext("2d");
|
|
324
|
-
return {
|
|
325
|
-
canvas: canvas2,
|
|
326
|
-
ctx: ctx2,
|
|
327
|
-
async encode(format, quality) {
|
|
328
|
-
const blob = await canvas2.convertToBlob({ type: `image/${format}`, quality });
|
|
329
|
-
return new Uint8Array(await blob.arrayBuffer());
|
|
330
|
-
},
|
|
331
|
-
release() {
|
|
332
|
-
canvas2.width = 0;
|
|
333
|
-
canvas2.height = 0;
|
|
334
|
-
}
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
|
-
const canvas = document.createElement("canvas");
|
|
338
|
-
canvas.width = width;
|
|
339
|
-
canvas.height = height;
|
|
340
|
-
const ctx = canvas.getContext("2d");
|
|
341
|
-
return {
|
|
342
|
-
canvas,
|
|
343
|
-
ctx,
|
|
344
|
-
encode(format, quality) {
|
|
345
|
-
return new Promise((resolve, reject) => {
|
|
346
|
-
canvas.toBlob(
|
|
347
|
-
(blob) => {
|
|
348
|
-
if (!blob) return reject(new Error("canvas could not be encoded"));
|
|
349
|
-
void blob.arrayBuffer().then((buf) => resolve(new Uint8Array(buf)), reject);
|
|
350
|
-
},
|
|
351
|
-
`image/${format}`,
|
|
352
|
-
quality
|
|
353
|
-
);
|
|
354
|
-
});
|
|
355
|
-
},
|
|
356
|
-
release() {
|
|
357
|
-
canvas.width = 0;
|
|
358
|
-
canvas.height = 0;
|
|
359
|
-
}
|
|
360
|
-
};
|
|
361
|
-
};
|
|
362
|
-
}
|
|
363
|
-
async function createPdfJsRasterizer(pdfjs2, data, canvases, options = {}) {
|
|
364
|
-
const task = pdfjs2.getDocument({
|
|
365
|
-
data: data.slice(),
|
|
366
|
-
password: options.password,
|
|
367
|
-
...options.documentOptions
|
|
368
|
-
});
|
|
369
|
-
let doc;
|
|
370
|
-
try {
|
|
371
|
-
doc = await task.promise;
|
|
372
|
-
} catch (error) {
|
|
373
|
-
await task.destroy().catch(() => {
|
|
374
|
-
});
|
|
375
|
-
if (error?.name === "PasswordException") {
|
|
376
|
-
throw new FreezeError("encrypted", ENCRYPTED_MESSAGE);
|
|
377
|
-
}
|
|
378
|
-
throw error;
|
|
379
|
-
}
|
|
380
|
-
return {
|
|
381
|
-
numPages: doc.numPages,
|
|
382
|
-
async renderPage(pageNumber, dpi) {
|
|
383
|
-
const page = await doc.getPage(pageNumber);
|
|
384
|
-
try {
|
|
385
|
-
const base = page.getViewport({ scale: 1 });
|
|
386
|
-
let scale = dpi / 72;
|
|
387
|
-
const pixels = base.width * scale * (base.height * scale);
|
|
388
|
-
if (pixels > MAX_PAGE_PIXELS) scale *= Math.sqrt(MAX_PAGE_PIXELS / pixels);
|
|
389
|
-
const viewport = page.getViewport({ scale });
|
|
390
|
-
const widthPx = Math.max(1, Math.ceil(viewport.width));
|
|
391
|
-
const heightPx = Math.max(1, Math.ceil(viewport.height));
|
|
392
|
-
const handle = canvases(widthPx, heightPx);
|
|
393
|
-
handle.ctx.fillStyle = "#ffffff";
|
|
394
|
-
handle.ctx.fillRect(0, 0, widthPx, heightPx);
|
|
395
|
-
await page.render({ canvasContext: handle.ctx, canvas: handle.canvas, viewport }).promise;
|
|
396
|
-
return {
|
|
397
|
-
widthPt: base.width,
|
|
398
|
-
heightPt: base.height,
|
|
399
|
-
widthPx,
|
|
400
|
-
heightPx,
|
|
401
|
-
dpi: scale * 72,
|
|
402
|
-
ctx: handle.ctx,
|
|
403
|
-
encode: handle.encode,
|
|
404
|
-
release: handle.release
|
|
405
|
-
};
|
|
406
|
-
} finally {
|
|
407
|
-
page.cleanup();
|
|
408
|
-
}
|
|
409
|
-
},
|
|
410
|
-
destroy: () => task.destroy()
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
function burnMarks(page, marks, options) {
|
|
414
|
-
const { ctx } = page;
|
|
415
|
-
const w = page.widthPx;
|
|
416
|
-
const h = page.heightPx;
|
|
417
|
-
const pxPerPt = page.widthPx / page.widthPt;
|
|
418
|
-
ctx.save();
|
|
419
|
-
for (const a of marks) {
|
|
420
|
-
if (a.kind === "highlight") {
|
|
421
|
-
ctx.globalCompositeOperation = "multiply";
|
|
422
|
-
ctx.globalAlpha = HIGHLIGHT_OPACITY;
|
|
423
|
-
ctx.fillStyle = a.color;
|
|
424
|
-
for (const r of a.rects) ctx.fillRect(r.x * w, r.y * h, r.w * w, r.h * h);
|
|
425
|
-
ctx.globalCompositeOperation = "source-over";
|
|
426
|
-
ctx.globalAlpha = 1;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
for (const a of marks) {
|
|
430
|
-
if (a.kind === "box") {
|
|
431
|
-
ctx.strokeStyle = a.color;
|
|
432
|
-
ctx.lineWidth = a.strokeWidth * pxPerPt;
|
|
433
|
-
ctx.strokeRect(a.rect.x * w, a.rect.y * h, a.rect.w * w, a.rect.h * h);
|
|
434
|
-
} else if (a.kind === "stamp") {
|
|
435
|
-
drawStampOnCanvas(
|
|
436
|
-
ctx,
|
|
437
|
-
a.label,
|
|
438
|
-
a.color,
|
|
439
|
-
{ x: a.rect.x * w, y: a.rect.y * h, w: a.rect.w * w, h: a.rect.h * h },
|
|
440
|
-
pxPerPt
|
|
441
|
-
);
|
|
442
|
-
} else if (a.kind === "comment" && options.comments !== "omit") {
|
|
443
|
-
drawMarkerOnCanvas(ctx, a, options.numbers.get(a.id), w, h, pxPerPt);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
for (const a of marks) {
|
|
447
|
-
if (!isEffectiveRedaction(a)) continue;
|
|
448
|
-
const pad = options.paddingPt * pxPerPt;
|
|
449
|
-
a.rects.forEach((r, i) => {
|
|
450
|
-
const x0 = Math.max(0, Math.floor(r.x * w - pad));
|
|
451
|
-
const y0 = Math.max(0, Math.floor(r.y * h - pad));
|
|
452
|
-
const x1 = Math.min(w, Math.ceil((r.x + r.w) * w + pad));
|
|
453
|
-
const y1 = Math.min(h, Math.ceil((r.y + r.h) * h + pad));
|
|
454
|
-
ctx.fillStyle = "#000000";
|
|
455
|
-
ctx.fillRect(x0, y0, x1 - x0, y1 - y0);
|
|
456
|
-
if (options.labels && i === 0 && a.reason)
|
|
457
|
-
drawLabel(ctx, a.reason, { x: x0, y: y0, w: x1 - x0, h: y1 - y0 });
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
|
-
ctx.restore();
|
|
461
|
-
}
|
|
462
|
-
function drawStampOnCanvas(ctx, label, color, box, pxPerPt) {
|
|
463
|
-
ctx.strokeStyle = color;
|
|
464
|
-
ctx.lineWidth = 2 * pxPerPt;
|
|
465
|
-
ctx.strokeRect(box.x, box.y, box.w, box.h);
|
|
466
|
-
ctx.fillStyle = color;
|
|
467
|
-
ctx.textAlign = "center";
|
|
468
|
-
ctx.textBaseline = "middle";
|
|
469
|
-
ctx.font = "bold 100px Helvetica, Arial, sans-serif";
|
|
470
|
-
const probe = ctx.measureText(label).width;
|
|
471
|
-
const size = Math.max(1, Math.min(box.h * 0.6, probe > 0 ? box.w * 0.9 * 100 / probe : box.h));
|
|
472
|
-
ctx.font = `bold ${size}px Helvetica, Arial, sans-serif`;
|
|
473
|
-
ctx.fillText(label, box.x + box.w / 2, box.y + box.h / 2);
|
|
474
|
-
}
|
|
475
|
-
function drawMarkerOnCanvas(ctx, a, n, w, h, pxPerPt) {
|
|
476
|
-
if (n === void 0) return;
|
|
477
|
-
const size = MARKER_SIZE_PT * pxPerPt;
|
|
478
|
-
const x = Math.min(a.rect.x * w, w - size);
|
|
479
|
-
const y = Math.min(a.rect.y * h, h - size);
|
|
480
|
-
ctx.fillStyle = MARKER_FILL;
|
|
481
|
-
ctx.fillRect(x, y, size, size);
|
|
482
|
-
ctx.strokeStyle = MARKER_INK;
|
|
483
|
-
ctx.lineWidth = 0.75 * pxPerPt;
|
|
484
|
-
ctx.strokeRect(x, y, size, size);
|
|
485
|
-
ctx.fillStyle = MARKER_INK;
|
|
486
|
-
ctx.textAlign = "center";
|
|
487
|
-
ctx.textBaseline = "middle";
|
|
488
|
-
ctx.font = `bold ${7 * pxPerPt}px Helvetica, Arial, sans-serif`;
|
|
489
|
-
ctx.fillText(String(n), x + size / 2, y + size / 2);
|
|
490
|
-
}
|
|
491
|
-
function drawLabel(ctx, text, box) {
|
|
492
|
-
const size = Math.min(box.h * 0.7, 14);
|
|
493
|
-
if (size < 6) return;
|
|
494
|
-
ctx.font = `${size}px Helvetica, Arial, sans-serif`;
|
|
495
|
-
const width = ctx.measureText(text).width;
|
|
496
|
-
if (width > box.w - 4) return;
|
|
497
|
-
ctx.fillStyle = "#ffffff";
|
|
498
|
-
ctx.textAlign = "center";
|
|
499
|
-
ctx.textBaseline = "middle";
|
|
500
|
-
ctx.fillText(text, box.x + box.w / 2, box.y + box.h / 2);
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
// src/annotations/freeze/freeze.ts
|
|
504
|
-
var DEFAULT_DPI = 200;
|
|
505
|
-
var DEFAULT_JPEG_QUALITY = 0.92;
|
|
506
|
-
var DEFAULT_REDACTION_PADDING_PT = 1.5;
|
|
507
|
-
var MIN_DPI = 72;
|
|
508
|
-
var MAX_DPI = 600;
|
|
509
|
-
var FALLBACK_PAGE = [595.28, 841.89];
|
|
510
|
-
var PROVENANCE_KEY = "ArbiProvenance";
|
|
511
|
-
var KINDS = ["highlight", "box", "comment", "stamp", "redaction"];
|
|
512
|
-
async function freezePdf(original, rawLayer, options) {
|
|
513
|
-
const layer = parseLayer(rawLayer);
|
|
514
|
-
const now = options.now ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
515
|
-
const comments = options.comments ?? "omit";
|
|
516
|
-
const dpi = options.dpi ?? DEFAULT_DPI;
|
|
517
|
-
if (!(dpi >= MIN_DPI && dpi <= MAX_DPI)) {
|
|
518
|
-
throw new FreezeError("invalid-option", `dpi must be between ${MIN_DPI} and ${MAX_DPI}`);
|
|
519
|
-
}
|
|
520
|
-
const redactions = layer.annotations.filter(isEffectiveRedaction);
|
|
521
|
-
const pending = redactions.filter((r) => r.status === "pending");
|
|
522
|
-
if (pending.length > 0 && (options.pendingRedactions ?? "error") === "error") {
|
|
523
|
-
throw new FreezeError(
|
|
524
|
-
"pending-redactions",
|
|
525
|
-
`${pending.length} redaction mark(s) have not been approved or rejected`
|
|
526
|
-
);
|
|
527
|
-
}
|
|
528
|
-
const marks = layer.annotations.filter((a) => a.kind !== "redaction" || isEffectiveRedaction(a));
|
|
529
|
-
const modeApplied = redactions.length > 0 ? "image" : options.mode;
|
|
530
|
-
const [originalSha256, layerSha256] = await Promise.all([sha256Hex(original), hashLayer(layer)]);
|
|
531
|
-
const warnings = [];
|
|
532
|
-
if (redactions.length > 0 && comments !== "omit" && marks.some((a) => a.kind === "comment")) {
|
|
533
|
-
warnings.push(
|
|
534
|
-
"Comments are included in a document with redactions and may repeat redacted text"
|
|
535
|
-
);
|
|
536
|
-
}
|
|
537
|
-
let built;
|
|
538
|
-
if (modeApplied === "image") {
|
|
539
|
-
built = await freezeAsImage(marks, options, comments, dpi, now, warnings);
|
|
540
|
-
} else {
|
|
541
|
-
built = await freezeAsVector(original, marks, comments, now, warnings);
|
|
542
|
-
}
|
|
543
|
-
const provenance = {
|
|
544
|
-
schema: 1,
|
|
545
|
-
derivedFrom: layer.documentId,
|
|
546
|
-
originalSha256,
|
|
547
|
-
layerSha256,
|
|
548
|
-
modeRequested: options.mode,
|
|
549
|
-
modeApplied,
|
|
550
|
-
...modeApplied !== options.mode ? { modeChangedBecause: "redactions-present" } : {},
|
|
551
|
-
frozenAt: now,
|
|
552
|
-
frozenBy: options.author,
|
|
553
|
-
pages: built.pages,
|
|
554
|
-
...built.dpi === void 0 ? {} : { dpi: built.dpi },
|
|
555
|
-
counts: countKinds(marks),
|
|
556
|
-
redactions: summariseRedactions(redactions)
|
|
557
|
-
};
|
|
558
|
-
setProvenance(built.doc, provenance);
|
|
559
|
-
return { bytes: await built.doc.save(), provenance, warnings };
|
|
560
|
-
}
|
|
561
|
-
function countKinds(marks) {
|
|
562
|
-
const counts = Object.fromEntries(KINDS.map((k) => [k, 0]));
|
|
563
|
-
for (const a of marks) counts[a.kind]++;
|
|
564
|
-
return counts;
|
|
565
|
-
}
|
|
566
|
-
function summariseRedactions(redactions) {
|
|
567
|
-
const reasons = {};
|
|
568
|
-
const pages = /* @__PURE__ */ new Set();
|
|
569
|
-
for (const r of redactions) {
|
|
570
|
-
if (r.kind !== "redaction") continue;
|
|
571
|
-
pages.add(r.page);
|
|
572
|
-
reasons[r.reason] = (reasons[r.reason] ?? 0) + 1;
|
|
573
|
-
}
|
|
574
|
-
return { count: redactions.length, pages: [...pages].sort((a, b) => a - b), reasons };
|
|
575
|
-
}
|
|
576
|
-
function checkPages(marks, pages) {
|
|
577
|
-
for (const a of marks) {
|
|
578
|
-
if (a.page > pages) {
|
|
579
|
-
throw new FreezeError(
|
|
580
|
-
"page-out-of-range",
|
|
581
|
-
`mark ${a.id} is on page ${a.page}; the document has ${pages}`
|
|
582
|
-
);
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
function checkAborted(signal) {
|
|
587
|
-
if (signal?.aborted) throw new DOMException("Freeze aborted", "AbortError");
|
|
588
|
-
}
|
|
589
|
-
async function loadPdfDocument(bytes) {
|
|
590
|
-
try {
|
|
591
|
-
return await PDFDocument.load(bytes);
|
|
592
|
-
} catch (error) {
|
|
593
|
-
if (error instanceof EncryptedPDFError || /is encrypted/i.test(error?.message ?? "")) {
|
|
594
|
-
throw new FreezeError("encrypted", ENCRYPTED_MESSAGE);
|
|
595
|
-
}
|
|
596
|
-
throw new FreezeError("invalid-pdf", `The PDF could not be read: ${error.message}`);
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
async function freezeAsVector(original, marks, comments, now, warnings) {
|
|
600
|
-
const doc = await loadPdfDocument(original);
|
|
601
|
-
const pages = doc.getPages();
|
|
602
|
-
checkPages(marks, pages.length);
|
|
603
|
-
const ctx = await createVectorContext(doc, marks, comments);
|
|
604
|
-
for (const a of marks) drawVectorMark(pages[a.page - 1], a, ctx);
|
|
605
|
-
if (comments === "appendix") {
|
|
606
|
-
const first = pages[0] ? displayedSize(pageGeometry(pages[0])) : void 0;
|
|
607
|
-
const size = first ? [first.width, first.height] : FALLBACK_PAGE;
|
|
608
|
-
appendCommentAppendix(
|
|
609
|
-
ctx,
|
|
610
|
-
marks.filter((a) => a.kind === "comment"),
|
|
611
|
-
size
|
|
612
|
-
);
|
|
613
|
-
}
|
|
614
|
-
reportReplaced(ctx.sanitiser, warnings);
|
|
615
|
-
doc.setProducer("ARBI");
|
|
616
|
-
doc.setModificationDate(new Date(now));
|
|
617
|
-
return { doc, pages: pages.length };
|
|
618
|
-
}
|
|
619
|
-
async function freezeAsImage(marks, options, comments, dpi, now, warnings) {
|
|
620
|
-
const rasterizer = options.rasterizer;
|
|
621
|
-
if (!rasterizer) {
|
|
622
|
-
throw new FreezeError("rasterizer-required", "Image freeze needs a page rasterizer");
|
|
623
|
-
}
|
|
624
|
-
try {
|
|
625
|
-
checkPages(marks, rasterizer.numPages);
|
|
626
|
-
const out = await PDFDocument.create();
|
|
627
|
-
out.setProducer("ARBI");
|
|
628
|
-
out.setCreator("ARBI");
|
|
629
|
-
out.setCreationDate(new Date(now));
|
|
630
|
-
out.setModificationDate(new Date(now));
|
|
631
|
-
const numbers = numberComments(marks);
|
|
632
|
-
const format = options.imageFormat ?? "jpeg";
|
|
633
|
-
const quality = options.jpegQuality ?? DEFAULT_JPEG_QUALITY;
|
|
634
|
-
const burn = {
|
|
635
|
-
paddingPt: options.redactionPaddingPt ?? DEFAULT_REDACTION_PADDING_PT,
|
|
636
|
-
labels: options.redactionLabels ?? true,
|
|
637
|
-
comments,
|
|
638
|
-
numbers
|
|
639
|
-
};
|
|
640
|
-
const ocrFont = options.ocr ? await out.embedFont(StandardFonts.Helvetica) : void 0;
|
|
641
|
-
const ocrSanitiser = ocrFont ? new TextSanitiser(ocrFont) : void 0;
|
|
642
|
-
let lowestDpi = dpi;
|
|
643
|
-
let firstSize;
|
|
644
|
-
for (let n = 1; n <= rasterizer.numPages; n++) {
|
|
645
|
-
checkAborted(options.signal);
|
|
646
|
-
const page = await rasterizer.renderPage(n, dpi);
|
|
647
|
-
try {
|
|
648
|
-
burnMarks(
|
|
649
|
-
page,
|
|
650
|
-
marks.filter((a) => a.page === n),
|
|
651
|
-
burn
|
|
652
|
-
);
|
|
653
|
-
const bytes = await page.encode(format, quality);
|
|
654
|
-
const image = format === "png" ? await out.embedPng(bytes) : await out.embedJpg(bytes);
|
|
655
|
-
const outPage = out.addPage([page.widthPt, page.heightPt]);
|
|
656
|
-
outPage.drawImage(image, { x: 0, y: 0, width: page.widthPt, height: page.heightPt });
|
|
657
|
-
lowestDpi = Math.min(lowestDpi, page.dpi);
|
|
658
|
-
firstSize ??= [page.widthPt, page.heightPt];
|
|
659
|
-
if (options.ocr && ocrFont && ocrSanitiser) {
|
|
660
|
-
const words = await options.ocr({
|
|
661
|
-
pageNumber: n,
|
|
662
|
-
bytes,
|
|
663
|
-
format,
|
|
664
|
-
widthPx: page.widthPx,
|
|
665
|
-
heightPx: page.heightPx
|
|
666
|
-
});
|
|
667
|
-
drawInvisibleText(outPage, words, page.widthPt, page.heightPt, ocrFont, ocrSanitiser);
|
|
668
|
-
}
|
|
669
|
-
} finally {
|
|
670
|
-
page.release();
|
|
671
|
-
}
|
|
672
|
-
options.onProgress?.(n, rasterizer.numPages);
|
|
673
|
-
}
|
|
674
|
-
if (comments === "appendix") {
|
|
675
|
-
const ctx = await createVectorContext(out, marks, comments);
|
|
676
|
-
appendCommentAppendix(
|
|
677
|
-
ctx,
|
|
678
|
-
marks.filter((a) => a.kind === "comment"),
|
|
679
|
-
firstSize ?? FALLBACK_PAGE
|
|
680
|
-
);
|
|
681
|
-
reportReplaced(ctx.sanitiser, warnings);
|
|
682
|
-
}
|
|
683
|
-
return { doc: out, pages: rasterizer.numPages, dpi: Math.round(lowestDpi) };
|
|
684
|
-
} finally {
|
|
685
|
-
await rasterizer.destroy();
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
function drawInvisibleText(page, words, widthPt, heightPt, font, sanitiser) {
|
|
689
|
-
for (const w of words) {
|
|
690
|
-
if (!w.text || !sanitiser.supports(w.text)) continue;
|
|
691
|
-
const size = Math.max(1, w.h * heightPt * 0.8);
|
|
692
|
-
page.drawText(w.text, {
|
|
693
|
-
x: w.x * widthPt,
|
|
694
|
-
y: heightPt - (w.y + w.h) * heightPt,
|
|
695
|
-
size,
|
|
696
|
-
font,
|
|
697
|
-
color: rgb(0, 0, 0),
|
|
698
|
-
opacity: 0
|
|
699
|
-
});
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
function reportReplaced(sanitiser, warnings) {
|
|
703
|
-
if (sanitiser.replaced.size > 0) {
|
|
704
|
-
warnings.push(
|
|
705
|
-
`${sanitiser.replaced.size} character(s) in comments cannot be drawn by the built-in font and were replaced with "?"`
|
|
706
|
-
);
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
function setProvenance(doc, provenance) {
|
|
710
|
-
const info = doc.context.lookup(doc.context.trailerInfo.Info);
|
|
711
|
-
if (!info || !("set" in info)) throw new Error("document has no info dictionary");
|
|
712
|
-
info.set(
|
|
713
|
-
PDFName.of(PROVENANCE_KEY),
|
|
714
|
-
PDFHexString.fromText(JSON.stringify(provenance))
|
|
715
|
-
);
|
|
716
|
-
}
|
|
717
|
-
async function readProvenance(bytes) {
|
|
718
|
-
const doc = await PDFDocument.load(bytes);
|
|
719
|
-
const info = doc.context.lookup(doc.context.trailerInfo.Info);
|
|
720
|
-
if (!info || !("get" in info)) return null;
|
|
721
|
-
const value = info.get(PDFName.of(PROVENANCE_KEY));
|
|
722
|
-
if (!(value instanceof PDFHexString)) return null;
|
|
723
|
-
return JSON.parse(value.decodeText());
|
|
724
|
-
}
|
|
725
6
|
async function freezePdfInBrowser(bytes, layer, { assets, password, ...options }) {
|
|
726
7
|
const needsRaster = options.mode === "image" || layer.annotations.some(isEffectiveRedaction);
|
|
727
8
|
const rasterizer = needsRaster ? await createPdfJsRasterizer(pdfjs, bytes, browserCanvasFactory(), {
|
|
@@ -735,368 +16,7 @@ async function freezePdfInBrowser(bytes, layer, { assets, password, ...options }
|
|
|
735
16
|
}) : void 0;
|
|
736
17
|
return freezePdf(bytes, layer, { ...options, rasterizer });
|
|
737
18
|
}
|
|
738
|
-
var EDGE_MARGIN_PT = 18;
|
|
739
|
-
var BATES_FONT_SIZE_PT = 9;
|
|
740
|
-
var QR_SIZE_PT = 56;
|
|
741
|
-
var QR_QUIET_MODULES = 2;
|
|
742
|
-
var PLATE_PADDING_PT = 2;
|
|
743
|
-
function batesLabel(options, index) {
|
|
744
|
-
const digits = options.digits ?? 6;
|
|
745
|
-
const number = String(Math.floor((options.start ?? 1) + index)).padStart(digits, "0");
|
|
746
|
-
return `${options.prefix ?? ""}${number}`;
|
|
747
|
-
}
|
|
748
|
-
function validateBates(options) {
|
|
749
|
-
const { start = 1, digits = 6 } = options;
|
|
750
|
-
if (!Number.isInteger(start) || start < 0) {
|
|
751
|
-
throw new FreezeError("invalid-option", "Bates start must be a whole number, 0 or more");
|
|
752
|
-
}
|
|
753
|
-
if (!Number.isInteger(digits) || digits < 1 || digits > 12) {
|
|
754
|
-
throw new FreezeError("invalid-option", "Bates digits must be a whole number from 1 to 12");
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
function plateOrigin(g, corner, w, h) {
|
|
758
|
-
const size = displayedSize(g);
|
|
759
|
-
return {
|
|
760
|
-
x: corner.endsWith("left") ? EDGE_MARGIN_PT : size.width - EDGE_MARGIN_PT - w,
|
|
761
|
-
y: corner.startsWith("top") ? EDGE_MARGIN_PT : size.height - EDGE_MARGIN_PT - h
|
|
762
|
-
};
|
|
763
|
-
}
|
|
764
|
-
function drawPlate(page, g, box) {
|
|
765
|
-
const size = displayedSize(g);
|
|
766
|
-
const u = normToUserRect(g, {
|
|
767
|
-
x: box.x / size.width,
|
|
768
|
-
y: box.y / size.height,
|
|
769
|
-
w: box.w / size.width,
|
|
770
|
-
h: box.h / size.height
|
|
771
|
-
});
|
|
772
|
-
page.drawRectangle({ x: u.x, y: u.y, width: u.width, height: u.height, color: rgb(1, 1, 1) });
|
|
773
|
-
}
|
|
774
|
-
function drawBatesLabel(page, label, font, corner) {
|
|
775
|
-
const g = pageGeometry(page);
|
|
776
|
-
const textWidth = font.widthOfTextAtSize(label, BATES_FONT_SIZE_PT);
|
|
777
|
-
const w = textWidth + PLATE_PADDING_PT * 2;
|
|
778
|
-
const h = BATES_FONT_SIZE_PT + PLATE_PADDING_PT * 2;
|
|
779
|
-
const { x, y } = plateOrigin(g, corner, w, h);
|
|
780
|
-
drawPlate(page, g, { x, y, w, h });
|
|
781
|
-
const origin = displayedToUser(
|
|
782
|
-
g,
|
|
783
|
-
x + PLATE_PADDING_PT,
|
|
784
|
-
y + PLATE_PADDING_PT + BATES_FONT_SIZE_PT * 0.8
|
|
785
|
-
);
|
|
786
|
-
page.drawText(label, {
|
|
787
|
-
x: origin.x,
|
|
788
|
-
y: origin.y,
|
|
789
|
-
size: BATES_FONT_SIZE_PT,
|
|
790
|
-
font,
|
|
791
|
-
color: rgb(0, 0, 0),
|
|
792
|
-
rotate: degrees(g.rotation)
|
|
793
|
-
});
|
|
794
|
-
}
|
|
795
|
-
function buildQr(text) {
|
|
796
|
-
try {
|
|
797
|
-
const qr = qrcode(0, "M");
|
|
798
|
-
qr.addData(text, "Byte");
|
|
799
|
-
qr.make();
|
|
800
|
-
return { modules: qr.getModuleCount(), isDark: (r, c) => qr.isDark(r, c) };
|
|
801
|
-
} catch (error) {
|
|
802
|
-
throw new FreezeError(
|
|
803
|
-
"invalid-option",
|
|
804
|
-
`The QR link could not be encoded: ${error.message}`
|
|
805
|
-
);
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
function drawQr(page, qr, corner) {
|
|
809
|
-
const g = pageGeometry(page);
|
|
810
|
-
const total = qr.modules + QR_QUIET_MODULES * 2;
|
|
811
|
-
const module = QR_SIZE_PT / total;
|
|
812
|
-
const { x, y } = plateOrigin(g, corner, QR_SIZE_PT, QR_SIZE_PT);
|
|
813
|
-
drawPlate(page, g, { x, y, w: QR_SIZE_PT, h: QR_SIZE_PT });
|
|
814
|
-
const size = displayedSize(g);
|
|
815
|
-
for (let row = 0; row < qr.modules; row++) {
|
|
816
|
-
let col = 0;
|
|
817
|
-
while (col < qr.modules) {
|
|
818
|
-
if (!qr.isDark(row, col)) {
|
|
819
|
-
col++;
|
|
820
|
-
continue;
|
|
821
|
-
}
|
|
822
|
-
const startCol = col;
|
|
823
|
-
while (col < qr.modules && qr.isDark(row, col)) col++;
|
|
824
|
-
const u = normToUserRect(g, {
|
|
825
|
-
x: (x + (QR_QUIET_MODULES + startCol) * module) / size.width,
|
|
826
|
-
y: (y + (QR_QUIET_MODULES + row) * module) / size.height,
|
|
827
|
-
w: (col - startCol) * module / size.width,
|
|
828
|
-
h: module / size.height
|
|
829
|
-
});
|
|
830
|
-
page.drawRectangle({
|
|
831
|
-
x: u.x,
|
|
832
|
-
y: u.y,
|
|
833
|
-
width: u.width,
|
|
834
|
-
height: u.height,
|
|
835
|
-
color: rgb(0, 0, 0)
|
|
836
|
-
});
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
var A4 = [595.28, 841.89];
|
|
841
|
-
var MARGIN = 54;
|
|
842
|
-
var LINE = 13;
|
|
843
|
-
var TEXT = rgb(0.1, 0.1, 0.1);
|
|
844
|
-
var MUTED = rgb(0.4, 0.4, 0.4);
|
|
845
|
-
async function insertIndexPages(doc, entries, options) {
|
|
846
|
-
const regular = await doc.embedFont(StandardFonts.Helvetica);
|
|
847
|
-
const bold = await doc.embedFont(StandardFonts.HelveticaBold);
|
|
848
|
-
const sanitiser = new TextSanitiser(regular);
|
|
849
|
-
const [width, height] = options.size ?? A4;
|
|
850
|
-
const columns = {
|
|
851
|
-
number: MARGIN,
|
|
852
|
-
name: MARGIN + 28,
|
|
853
|
-
range: width - MARGIN - 190,
|
|
854
|
-
pages: width - MARGIN - 30
|
|
855
|
-
};
|
|
856
|
-
const nameWidth = columns.range - columns.name - 12;
|
|
857
|
-
let inserted = 0;
|
|
858
|
-
let page;
|
|
859
|
-
let y = 0;
|
|
860
|
-
const newPage = () => {
|
|
861
|
-
page = doc.insertPage(inserted++, [width, height]);
|
|
862
|
-
y = height - MARGIN;
|
|
863
|
-
};
|
|
864
|
-
const text = (value, x, font, size, color = TEXT) => page.drawText(sanitiser.clean(value), { x, y: y - size, size, font, color });
|
|
865
|
-
const header = () => {
|
|
866
|
-
text("#", columns.number, bold, 9, MUTED);
|
|
867
|
-
text("Document", columns.name, bold, 9, MUTED);
|
|
868
|
-
text("Bates range", columns.range, bold, 9, MUTED);
|
|
869
|
-
text("Pages", columns.pages, bold, 9, MUTED);
|
|
870
|
-
y -= LINE + 4;
|
|
871
|
-
};
|
|
872
|
-
newPage();
|
|
873
|
-
text("Production index", MARGIN, bold, 16);
|
|
874
|
-
y -= 24;
|
|
875
|
-
const first = entries[0]?.firstLabel;
|
|
876
|
-
const last = entries[entries.length - 1]?.lastLabel;
|
|
877
|
-
const total = entries.reduce((n, e) => n + e.pages, 0);
|
|
878
|
-
const summary = [
|
|
879
|
-
`${entries.length} document${entries.length === 1 ? "" : "s"}, ${total} page${total === 1 ? "" : "s"}`,
|
|
880
|
-
first && last ? `${first} to ${last}` : "",
|
|
881
|
-
`produced ${options.producedAt.slice(0, 10)}`,
|
|
882
|
-
options.producedBy ? `by ${options.producedBy}` : ""
|
|
883
|
-
].filter(Boolean).join(", ");
|
|
884
|
-
text(summary, MARGIN, regular, 10, MUTED);
|
|
885
|
-
y -= LINE + 10;
|
|
886
|
-
header();
|
|
887
|
-
const rangeWidth = columns.pages - columns.range - 8;
|
|
888
|
-
entries.forEach((entry, i) => {
|
|
889
|
-
const lines = wrap(regular, sanitiser.clean(entry.name), 10, nameWidth);
|
|
890
|
-
const single = entry.firstLabel === entry.lastLabel ? entry.firstLabel : `${entry.firstLabel} - ${entry.lastLabel}`;
|
|
891
|
-
const range = regular.widthOfTextAtSize(sanitiser.clean(single), 10) <= rangeWidth ? [single] : [`${entry.firstLabel} -`, entry.lastLabel];
|
|
892
|
-
const rows = Math.max(lines.length, range.length);
|
|
893
|
-
if (y - rows * LINE < MARGIN) {
|
|
894
|
-
newPage();
|
|
895
|
-
header();
|
|
896
|
-
}
|
|
897
|
-
const top = y;
|
|
898
|
-
text(String(i + 1), columns.number, regular, 10);
|
|
899
|
-
text(String(entry.pages), columns.pages, regular, 10);
|
|
900
|
-
range.forEach((line, n) => {
|
|
901
|
-
y = top - n * LINE;
|
|
902
|
-
text(line, columns.range, regular, 10);
|
|
903
|
-
});
|
|
904
|
-
lines.forEach((line, n) => {
|
|
905
|
-
y = top - n * LINE;
|
|
906
|
-
text(line, columns.name, regular, 10);
|
|
907
|
-
});
|
|
908
|
-
y = top - rows * LINE - 4;
|
|
909
|
-
});
|
|
910
|
-
return inserted;
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
// src/annotations/freeze/bundle.ts
|
|
914
|
-
var BUNDLE_KEY = "ArbiBundle";
|
|
915
|
-
var DEFAULT_QR_CORNER = "bottom-left";
|
|
916
|
-
var DEFAULT_BATES_CORNER = "bottom-right";
|
|
917
|
-
function checkAborted2(signal) {
|
|
918
|
-
if (signal?.aborted) throw new DOMException("Bundle aborted", "AbortError");
|
|
919
|
-
}
|
|
920
|
-
function outputName(name, used) {
|
|
921
|
-
const base = (name.replace(/\.pdf$/i, "").replace(/[\\/:*?"<>|]+/g, "_").trim() || "document") + ".pdf";
|
|
922
|
-
let candidate = base;
|
|
923
|
-
for (let n = 2; used.has(candidate); n++) candidate = base.replace(/\.pdf$/, `-${n}.pdf`);
|
|
924
|
-
used.add(candidate);
|
|
925
|
-
return candidate;
|
|
926
|
-
}
|
|
927
|
-
function validate(items, options) {
|
|
928
|
-
if (items.length === 0)
|
|
929
|
-
throw new FreezeError("invalid-option", "A bundle needs at least one document");
|
|
930
|
-
validateBates(options.bates);
|
|
931
|
-
if (options.qr) {
|
|
932
|
-
const missing = items.find((i) => !i.qrUrl);
|
|
933
|
-
if (missing) {
|
|
934
|
-
throw new FreezeError("invalid-option", `"${missing.name}" has no link for its QR code`);
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
const corner = options.bates.corner ?? DEFAULT_BATES_CORNER;
|
|
938
|
-
if (options.qr && (options.qrCorner ?? DEFAULT_QR_CORNER) === corner) {
|
|
939
|
-
throw new FreezeError(
|
|
940
|
-
"invalid-option",
|
|
941
|
-
"The Bates label and the QR code need different corners"
|
|
942
|
-
);
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
async function stampPages(doc, items, firstIndex, options) {
|
|
946
|
-
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
947
|
-
const sanitiser = new TextSanitiser(font);
|
|
948
|
-
const label = batesLabel(options.bates, firstIndex);
|
|
949
|
-
if (!sanitiser.supports(label)) {
|
|
950
|
-
throw new FreezeError(
|
|
951
|
-
"unsupported-text",
|
|
952
|
-
`Bates prefix "${options.bates.prefix}" uses characters the built-in font cannot draw`
|
|
953
|
-
);
|
|
954
|
-
}
|
|
955
|
-
const pages = doc.getPages();
|
|
956
|
-
const corner = options.bates.corner ?? DEFAULT_BATES_CORNER;
|
|
957
|
-
let at = 0;
|
|
958
|
-
for (const item of items) {
|
|
959
|
-
const qr = options.qr && item.qrUrl ? buildQr(item.qrUrl) : void 0;
|
|
960
|
-
for (let p = 0; p < item.pages; p++, at++) {
|
|
961
|
-
const page = pages[at];
|
|
962
|
-
drawBatesLabel(page, batesLabel(options.bates, firstIndex + at), font, corner);
|
|
963
|
-
if (qr) drawQr(page, qr, options.qrCorner ?? DEFAULT_QR_CORNER);
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
function setBundleRecord(doc, record) {
|
|
968
|
-
const info = doc.context.lookup(doc.context.trailerInfo.Info);
|
|
969
|
-
if (!info || !("set" in info)) return;
|
|
970
|
-
info.set(
|
|
971
|
-
PDFName.of(BUNDLE_KEY),
|
|
972
|
-
PDFHexString.fromText(JSON.stringify(record))
|
|
973
|
-
);
|
|
974
|
-
}
|
|
975
|
-
async function buildBundle(items, options) {
|
|
976
|
-
validate(items, options);
|
|
977
|
-
const now = options.now ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
978
|
-
const used = /* @__PURE__ */ new Set();
|
|
979
|
-
const files = [];
|
|
980
|
-
const index = [];
|
|
981
|
-
const loaded = [];
|
|
982
|
-
for (const item of items) {
|
|
983
|
-
checkAborted2(options.signal);
|
|
984
|
-
const doc = await loadPdfDocument(item.bytes);
|
|
985
|
-
loaded.push({ item, doc, pages: doc.getPageCount() });
|
|
986
|
-
}
|
|
987
|
-
if (options.layout === "merged") {
|
|
988
|
-
const out = await PDFDocument.create();
|
|
989
|
-
out.setProducer("ARBI");
|
|
990
|
-
out.setCreator("ARBI");
|
|
991
|
-
out.setCreationDate(new Date(now));
|
|
992
|
-
out.setModificationDate(new Date(now));
|
|
993
|
-
const provenance = {};
|
|
994
|
-
for (const { item, doc } of loaded) {
|
|
995
|
-
checkAborted2(options.signal);
|
|
996
|
-
for (const page of await out.copyPages(doc, doc.getPageIndices())) out.addPage(page);
|
|
997
|
-
provenance[item.documentId] = await readProvenance(item.bytes).catch(() => null);
|
|
998
|
-
}
|
|
999
|
-
await stampPages(
|
|
1000
|
-
out,
|
|
1001
|
-
loaded.map(({ item, pages }) => ({ pages, qrUrl: item.qrUrl })),
|
|
1002
|
-
0,
|
|
1003
|
-
options
|
|
1004
|
-
);
|
|
1005
|
-
const file = outputName(options.mergedName ?? "production.pdf", used);
|
|
1006
|
-
let first2 = 0;
|
|
1007
|
-
for (const { item, pages } of loaded) {
|
|
1008
|
-
index.push({
|
|
1009
|
-
documentId: item.documentId,
|
|
1010
|
-
name: item.name,
|
|
1011
|
-
file,
|
|
1012
|
-
pages,
|
|
1013
|
-
firstLabel: batesLabel(options.bates, first2),
|
|
1014
|
-
lastLabel: batesLabel(options.bates, first2 + pages - 1)
|
|
1015
|
-
});
|
|
1016
|
-
first2 += pages;
|
|
1017
|
-
}
|
|
1018
|
-
const indexPages2 = options.indexPage ? await insertIndexPages(out, index, {
|
|
1019
|
-
size: pageSize(loaded[0].doc),
|
|
1020
|
-
producedAt: now,
|
|
1021
|
-
producedBy: options.author
|
|
1022
|
-
}) : 0;
|
|
1023
|
-
setBundleRecord(out, {
|
|
1024
|
-
schema: 1,
|
|
1025
|
-
producedAt: now,
|
|
1026
|
-
...indexPages2 ? { indexPages: indexPages2 } : {},
|
|
1027
|
-
...options.author ? { producedBy: options.author } : {},
|
|
1028
|
-
layout: "merged",
|
|
1029
|
-
items: index.map((e) => ({
|
|
1030
|
-
documentId: e.documentId,
|
|
1031
|
-
name: e.name,
|
|
1032
|
-
pages: e.pages,
|
|
1033
|
-
firstLabel: e.firstLabel,
|
|
1034
|
-
lastLabel: e.lastLabel,
|
|
1035
|
-
frozen: provenance[e.documentId] ?? null
|
|
1036
|
-
}))
|
|
1037
|
-
});
|
|
1038
|
-
const total = first2;
|
|
1039
|
-
files.push({
|
|
1040
|
-
name: file,
|
|
1041
|
-
bytes: await out.save(),
|
|
1042
|
-
pages: total + indexPages2,
|
|
1043
|
-
firstLabel: batesLabel(options.bates, 0),
|
|
1044
|
-
lastLabel: batesLabel(options.bates, total - 1)
|
|
1045
|
-
});
|
|
1046
|
-
options.onProgress?.(loaded.length, loaded.length);
|
|
1047
|
-
return { files, index, indexPages: indexPages2 };
|
|
1048
|
-
}
|
|
1049
|
-
let first = 0;
|
|
1050
|
-
for (const [n, { item, doc, pages }] of loaded.entries()) {
|
|
1051
|
-
checkAborted2(options.signal);
|
|
1052
|
-
await stampPages(doc, [{ pages, qrUrl: item.qrUrl }], first, options);
|
|
1053
|
-
doc.setModificationDate(new Date(now));
|
|
1054
|
-
const file = outputName(item.name, used);
|
|
1055
|
-
const entry = {
|
|
1056
|
-
documentId: item.documentId,
|
|
1057
|
-
name: item.name,
|
|
1058
|
-
file,
|
|
1059
|
-
pages,
|
|
1060
|
-
firstLabel: batesLabel(options.bates, first),
|
|
1061
|
-
lastLabel: batesLabel(options.bates, first + pages - 1)
|
|
1062
|
-
};
|
|
1063
|
-
index.push(entry);
|
|
1064
|
-
files.push({
|
|
1065
|
-
name: file,
|
|
1066
|
-
bytes: await doc.save(),
|
|
1067
|
-
pages,
|
|
1068
|
-
firstLabel: entry.firstLabel,
|
|
1069
|
-
lastLabel: entry.lastLabel
|
|
1070
|
-
});
|
|
1071
|
-
first += pages;
|
|
1072
|
-
options.onProgress?.(n + 1, loaded.length);
|
|
1073
|
-
}
|
|
1074
|
-
if (!options.indexPage) return { files, index, indexPages: 0 };
|
|
1075
|
-
const cover = await PDFDocument.create();
|
|
1076
|
-
cover.setProducer("ARBI");
|
|
1077
|
-
cover.setCreator("ARBI");
|
|
1078
|
-
cover.setCreationDate(new Date(now));
|
|
1079
|
-
const indexPages = await insertIndexPages(cover, index, {
|
|
1080
|
-
size: pageSize(loaded[0].doc),
|
|
1081
|
-
producedAt: now,
|
|
1082
|
-
producedBy: options.author
|
|
1083
|
-
});
|
|
1084
|
-
files.unshift({
|
|
1085
|
-
name: outputName("index.pdf", used),
|
|
1086
|
-
bytes: await cover.save(),
|
|
1087
|
-
pages: indexPages,
|
|
1088
|
-
firstLabel: "",
|
|
1089
|
-
lastLabel: ""
|
|
1090
|
-
});
|
|
1091
|
-
return { files, index, indexPages };
|
|
1092
|
-
}
|
|
1093
|
-
function pageSize(doc) {
|
|
1094
|
-
const page = doc.getPage(0);
|
|
1095
|
-
const { width, height } = page.getSize();
|
|
1096
|
-
const turned = page.getRotation().angle % 180 !== 0;
|
|
1097
|
-
return turned ? [height, width] : [width, height];
|
|
1098
|
-
}
|
|
1099
19
|
|
|
1100
|
-
export {
|
|
20
|
+
export { freezePdfInBrowser };
|
|
1101
21
|
//# sourceMappingURL=index.js.map
|
|
1102
22
|
//# sourceMappingURL=index.js.map
|