-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredact-engine.js
More file actions
300 lines (291 loc) · 9.37 KB
/
Copy pathredact-engine.js
File metadata and controls
300 lines (291 loc) · 9.37 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
// Minimal PDF content-stream tokenizer and text-run remover.
//
// Redaction here means two things, applied together per rect:
// 1. Any Tj/TJ/'/'' text-show operator whose conservatively-estimated
// bounding box overlaps the rect is dropped from the content stream
// entirely — the underlying text bytes no longer exist in the file.
// 2. A solid black rectangle is drawn over the same area, so non-text
// content (images, vector art, scanned pages) is visually covered too.
//
// The width estimate for (2) uses a fixed 0.65×fontSize-per-character
// average. That's a deliberate overestimate for most fonts, so redaction
// errs toward removing slightly more text than the rect strictly covers
// rather than leaving a partial word behind.
//
// Known limits (surfaced in the UI, not hidden):
// - Only axis-aligned (non-rotated) text is handled correctly, since bbox
// estimation assumes a roughly horizontal text matrix.
// - Only true text removal for text drawn with Tj/TJ. Image/vector content
// is covered visually, not removed from the file.
function tokenize(text) {
const tokens = [];
let i = 0;
const n = text.length;
while (i < n) {
const c = text[i];
if (/\s/.test(c)) { i++; continue; }
if (c === "%") { while (i < n && text[i] !== "\n") i++; continue; }
if (c === "(") {
let depth = 1, start = i; i++;
while (i < n && depth > 0) {
if (text[i] === "\\") { i += 2; continue; }
if (text[i] === "(") depth++;
else if (text[i] === ")") depth--;
i++;
}
tokens.push({ type: "string", raw: text.slice(start, i) });
continue;
}
if (c === "<" && text[i + 1] === "<") {
let depth = 1, start = i; i += 2;
while (i < n && depth > 0) {
if (text[i] === "<" && text[i + 1] === "<") { depth++; i += 2; continue; }
if (text[i] === ">" && text[i + 1] === ">") { depth--; i += 2; continue; }
i++;
}
tokens.push({ type: "dict", raw: text.slice(start, i) });
continue;
}
if (c === "<") {
let start = i; i++;
while (i < n && text[i] !== ">") i++;
i++;
tokens.push({ type: "hexstring", raw: text.slice(start, i) });
continue;
}
if (c === "[") {
let depth = 1, start = i; i++;
while (i < n && depth > 0) {
if (text[i] === "[") depth++;
else if (text[i] === "]") depth--;
else if (text[i] === "(") {
let d2 = 1; i++;
while (i < n && d2 > 0) {
if (text[i] === "\\") { i += 2; continue; }
if (text[i] === "(") d2++;
else if (text[i] === ")") d2--;
i++;
}
continue;
}
i++;
}
tokens.push({ type: "array", raw: text.slice(start, i) });
continue;
}
if (c === "/") {
let start = i; i++;
while (i < n && !/[\s/[\]<>()]/.test(text[i])) i++;
tokens.push({ type: "name", raw: text.slice(start, i) });
continue;
}
if (/[-+.\d]/.test(c)) {
let start = i; i++;
while (i < n && /[-+.\d]/.test(text[i])) i++;
tokens.push({ type: "num", raw: text.slice(start, i), value: parseFloat(text.slice(start, i)) });
continue;
}
let start = i;
while (i < n && !/[\s/[\]<>()]/.test(text[i])) i++;
if (i === start) { i++; continue; }
const rawOp = text.slice(start, i);
tokens.push({ type: "op", raw: rawOp });
if (rawOp === "ID") {
// Inline image: exactly one whitespace byte separates ID from the raw
// (often binary) image data, which runs until whitespace+"EI"+delimiter.
// That data is arbitrary bytes, not PDF syntax, so it must be captured
// verbatim rather than re-tokenized (binary can contain "(", ")", etc.
// that would otherwise desync the parser for the rest of the stream).
const dataStart = i + 1;
let j = dataStart;
while (j < n) {
if (
/\s/.test(text[j]) &&
text[j + 1] === "E" &&
text[j + 2] === "I" &&
(j + 3 >= n || /[\s/[\]<>()]/.test(text[j + 3]))
) {
break;
}
j++;
}
tokens.push({ type: "raw", raw: text.slice(dataStart, j) });
tokens.push({ type: "op", raw: "EI" });
i = j + 3;
}
}
return tokens;
}
function mul(m1, m2) {
const [a1, b1, c1, d1, e1, f1] = m1;
const [a2, b2, c2, d2, e2, f2] = m2;
return [
a1 * a2 + b1 * c2,
a1 * b2 + b1 * d2,
c1 * a2 + d1 * c2,
c1 * b2 + d1 * d2,
e1 * a2 + f1 * c2 + e2,
e1 * b2 + f1 * d2 + f2,
];
}
function applyToPoint(m, x, y) {
return [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]];
}
function decodeStringLength(raw) {
if (raw.startsWith("(")) {
const inner = raw.slice(1, -1);
let count = 0;
for (let i = 0; i < inner.length; i++) {
if (inner[i] === "\\") { i++; count++; continue; }
count++;
}
return count;
}
if (raw.startsWith("<")) {
const inner = raw.slice(1, -1).replace(/\s/g, "");
return Math.ceil(inner.length / 2);
}
return 0;
}
function rectsOverlap(a, b) {
return a[0] < b[2] && a[2] > b[0] && a[1] < b[3] && a[3] > b[1];
}
/**
* Rewrites a page's content stream, dropping text-show operators whose
* estimated bounding box overlaps any rect in `rects` (PDF user-space
* points, [x0, y0, x1, y1], origin bottom-left).
* Returns { text, removedCount }.
*/
export function redactContentStream(source, rects) {
const tokens = tokenize(source);
const out = [];
// Per the PDF spec, the CTM plus the text-state parameters (font, font
// size, leading, etc.) are all part of the graphics state saved/restored
// by q/Q -- only Tm/Tlm are excluded (they're reset by BT instead). So
// fontSize/leading must be stacked alongside ctm, or a font-size change
// made inside a q...Q block would leak past the matching Q and corrupt
// the bbox estimate for text drawn after it.
let gsStack = [];
let ctm = [1, 0, 0, 1, 0, 0];
let tm = [1, 0, 0, 1, 0, 0];
let tlm = [1, 0, 0, 1, 0, 0];
let fontSize = 12;
let leading = 0;
let pending = [];
let removedCount = 0;
function textBBox(charCount) {
const combined = mul(tm, ctm);
const [x0, y0] = applyToPoint(combined, 0, -fontSize * 0.25);
const widthEstimate = charCount * fontSize * 0.65;
const [x1, y1] = applyToPoint(combined, widthEstimate, fontSize * 0.95);
return [Math.min(x0, x1), Math.min(y0, y1), Math.max(x0, x1), Math.max(y0, y1)];
}
function overlapsAny(bbox) {
return rects.some((r) => rectsOverlap(bbox, r));
}
for (const tok of tokens) {
if (tok.type !== "op") {
pending.push(tok);
continue;
}
const op = tok.raw;
switch (op) {
case "q":
gsStack.push({ ctm, fontSize, leading });
break;
case "Q": {
const restored = gsStack.pop();
if (restored) {
ctm = restored.ctm;
fontSize = restored.fontSize;
leading = restored.leading;
} else {
ctm = [1, 0, 0, 1, 0, 0];
}
break;
}
case "cm": {
const nums = pending.filter((t) => t.type === "num").map((t) => t.value);
if (nums.length === 6) ctm = mul(nums, ctm);
break;
}
case "BT":
tm = [1, 0, 0, 1, 0, 0];
tlm = [1, 0, 0, 1, 0, 0];
break;
case "Tf": {
const nums = pending.filter((t) => t.type === "num").map((t) => t.value);
if (nums.length >= 1) fontSize = nums[nums.length - 1];
break;
}
case "TL": {
const nums = pending.filter((t) => t.type === "num").map((t) => t.value);
if (nums.length) leading = nums[0];
break;
}
case "Td":
case "TD": {
const nums = pending.filter((t) => t.type === "num").map((t) => t.value);
if (nums.length === 2) {
if (op === "TD") leading = -nums[1];
tlm = mul([1, 0, 0, 1, nums[0], nums[1]], tlm);
tm = tlm;
}
break;
}
case "Tm": {
const nums = pending.filter((t) => t.type === "num").map((t) => t.value);
if (nums.length === 6) {
tm = nums;
tlm = nums;
}
break;
}
case "T*": {
tlm = mul([1, 0, 0, 1, 0, -leading], tlm);
tm = tlm;
break;
}
case "Tj":
case "'":
case "\"": {
if (op !== "Tj") {
tlm = mul([1, 0, 0, 1, 0, -leading], tlm);
tm = tlm;
}
const strTok = [...pending].reverse().find((t) => t.type === "string" || t.type === "hexstring");
const len = strTok ? decodeStringLength(strTok.raw) : 0;
const bbox = textBBox(len);
if (overlapsAny(bbox)) {
removedCount++;
pending = [];
continue;
}
break;
}
case "TJ": {
const arrTok = pending.find((t) => t.type === "array");
let len = 0;
if (arrTok) {
const inner = tokenize(arrTok.raw.slice(1, -1));
for (const it of inner) {
if (it.type === "string" || it.type === "hexstring") len += decodeStringLength(it.raw);
}
}
const bbox = textBBox(len);
if (overlapsAny(bbox)) {
removedCount++;
pending = [];
continue;
}
break;
}
default:
break;
}
out.push(...pending, tok);
pending = [];
}
out.push(...pending);
return { text: out.map((t) => t.raw).join(" "), removedCount };
}