-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamEditor.cs
More file actions
602 lines (577 loc) · 19.5 KB
/
StreamEditor.cs
File metadata and controls
602 lines (577 loc) · 19.5 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// ===========================================================================
// ©2013-2024 WebSupergoo. All rights reserved.
//
// This source code is for use exclusively with the ABCpdf product with
// which it is distributed, under the terms of the license for that
// product. Details can be found at
//
// http://www.websupergoo.com/
//
// This copyright notice must not be deleted and must be reproduced alongside
// any sections of code extracted from this module.
// ===========================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Diagnostics;
using System.Security.Cryptography;
using SD = System.Drawing;
using SD2 = System.Drawing.Drawing2D;
using WebSupergoo.ABCpdf13;
using WebSupergoo.ABCpdf13.Atoms;
using WebSupergoo.ABCpdf13.Objects;
using WebSupergoo.ABCpdf13.Operations;
namespace WebSupergoo.PDFSurgeon {
public class StreamEditor {
private byte[] _liveData;
private static readonly HashSet<string> _pathMake = new HashSet<string>(new string[] { "m", "l", "c", "v", "y", "h", "re", "W", "W*" });
private static readonly HashSet<string> _pathPaint = new HashSet<string>(new string[] { "S", "s", "f", "F", "f*", "B", "B*", "b", "b*", "n" });
private StreamEditor() { }
public StreamEditor(ObjectExtractor obj) {
Extractor = obj;
}
public ObjectExtractor Extractor { get; private set; }
public StreamObject Stream { get { return Extractor.Object as StreamObject; } }
public StreamObject ContentStream { get; private set; }
public IndirectObject ResourceOwner { get; private set; }
public string OriginalText { get; set; }
public string LiveText { get; set; }
public string CurrentText { get { return Extractor.GetValue(Extractor.Doc); } set { Extractor.SetValue(Extractor.Doc, value); } }
public void Load() {
StreamObject so = Stream;
ContentStream = (so != null) && (!so.Compressed) && ((so is FormXObject) || (Extractor.Context.PageContents.Contains(so.ID))) ? so : null;
ResourceOwner = ContentStream as FormXObject;
if (ResourceOwner == null) {
int id = 0;
if (Extractor.Context.PageContentIdToPageId.TryGetValue(Extractor.Object.ID, out id))
ResourceOwner = Extractor.Doc.ObjectSoup[id];
}
OriginalText = LiveText = CurrentText;
}
public StringBuilder GetPartialContentStream(double lo, double hi) {
// get low and high values
if (lo > hi) {
double t = lo;
lo = hi;
hi = t;
}
// get stream data
if ((_liveData == null) && (ContentStream != null))
_liveData = ContentStream.GetData();
if (_liveData == null)
return null;
// analyse content stream
ArrayAtom array = ArrayAtom.FromContentStream(_liveData);
IList<Tuple<string, int>> items = OpAtom.Find(array);
Stack<int> saveRestoreStack = new Stack<int>();
Stack<int> markedContentStack = new Stack<int>();
List<Tuple<int, int>> pairs = new List<Tuple<int, int>>();
int beginText = -1, beginPath = -1;
for (int i = 0; i < items.Count; i++) {
var pair = items[i];
string op = ((OpAtom)array[pair.Item2]).Text;
if (op == "q")
saveRestoreStack.Push(i);
else if (op == "Q")
pairs.Add(new Tuple<int, int>(saveRestoreStack.Pop(), i));
if (op == "BMC" || op == "BDC")
markedContentStack.Push(i);
else if (op == "EMC") {
// Marked content start and end can be interwoven with other content start and end
// so we only put this sequence in as a pair if there is nothing inside it.
if (i == markedContentStack.Pop() + 1)
pairs.Add(new Tuple<int, int>(i - 1, i));
}
else if (op == "BT")
beginText = i;
else if (op == "ET") {
if (beginText >= 0)
pairs.Add(new Tuple<int, int>(beginText, i));
beginText = -1;
}
else if (_pathMake.Contains(op)) {
if (beginPath < 0)
beginPath = i;
}
else if (_pathPaint.Contains(op)) {
if (beginPath >= 0)
pairs.Add(new Tuple<int, int>(beginPath, i));
beginPath = -1;
}
}
//Debug.Assert(saveRestoreStack.Count == 0);
//Debug.Assert(markedContentStack.Count == 0);
// build set of sections which will be discarded
int loIndex = (int)(items.Count * lo);
int hiIndex = (int)(items.Count * hi);
Dictionary<int, int> discardPairs = new Dictionary<int, int>();
foreach (var pair in pairs) {
if (pair.Item2 < loIndex)
discardPairs[pair.Item1] = pair.Item2;
else if (pair.Item1 > hiIndex)
discardPairs[pair.Item1] = pair.Item2;
}
// build contents
int index = 0;
StringBuilder sb = new StringBuilder();
bool writeCommand = true;
for (int i = 0; i < items.Count; i++) {
int endIndex = 0;
if (discardPairs.TryGetValue(i, out endIndex)) {
writeCommand = false;
i = endIndex;
}
var pair = items[i];
if (writeCommand)
AppendCommand(sb, array, index, pair.Item2, false);
writeCommand = true;
index = pair.Item2 + 1;
}
return sb;
}
public string FormatContentStream() {
if (ContentStream == null)
return null;
return FormatContentStream(ContentStream);
}
private static string FormatContentStream(StreamObject stream) {
bool composite = false;
ArrayAtom array = ArrayAtom.FromContentStream(stream.GetData());
int indent = 0;
HashSet<string> indentPlus = new HashSet<string>(new string[] { "q", "BT" });
HashSet<string> indentMinus = new HashSet<string>(new string[] { "Q", "ET" });
IList<Tuple<string, int>> items = OpAtom.Find(array);
int index = 0;
StringBuilder code = new StringBuilder();
foreach (var pair in items) {
string op = ((OpAtom)array[pair.Item2]).Text;
// add indent to code
if (indentMinus.Contains(op))
indent--;
for (int i = 0; i < indent; i++)
code.Append(" ");
AppendCommand(code, array, index, pair.Item2, composite);
if (indentPlus.Contains(op))
indent++;
index = pair.Item2 + 1;
}
// write out any atoms that are left over
for (int i = index; i < array.Count; i++) {
code.Append(" ");
code.Append(array[i].ToString());
}
// update dict atom at start
DictAtom dict = (DictAtom)stream.Atom.Clone();
Atom.SetItem(dict, "Length", new NumAtom(code.Length));
StringBuilder result = new StringBuilder();
result.AppendLine(dict.ToString());
result.AppendLine("stream");
result.AppendLine(code.ToString());
result.AppendLine("endstream");
return result.ToString();
}
private static void AppendCommand(StringBuilder sb, ArrayAtom array, int startIndex, int endIndex, bool multibyte) {
for (int i = startIndex; i <= endIndex; i++) {
if (i != startIndex)
sb.Append(" ");
Atom item = array[i];
// We write arrays out individually so that we can override default cr lf behavior.
// We write strings out with checking so that we can use a hex encoding if appropriate.
// NB With ToString format overrides we can eliminate this function.
ArrayAtom itemArray = item as ArrayAtom;
StringAtom itemString = item as StringAtom;
if (itemArray != null) {
int n = itemArray.Count;
sb.Append("[");
for (int j = 0; j < n; j++) {
sb.Append(itemArray[j].ToString());
if (j != n - 1)
sb.Append(" ");
}
sb.Append("]");
}
else if (itemString != null) {
var data = itemString.Data;
int hexCount = 0;
for (int j = 0; j < data.Length; j++) {
if (data[j] < 32 || data[j] > 127)
hexCount++;
}
if (hexCount > data.Length / 3) {
sb.Append('<');
foreach (var b in data)
sb.Append(b.ToString("X2"));
sb.Append('>');
}
else {
sb.Append(item.ToString());
}
}
else {
sb.Append(item.ToString());
}
}
sb.AppendLine();
}
}
public class GraphicsScanner : ContentStreamScanner {
[DebuggerDisplay("Bounds = {Bounds} Text = \"{Text}\"")]
public class Fixture {
public Fixture(SD2.GraphicsPath path, int offset, int length, byte[] data) { PdfPath = path; Offset = offset; Length = length; Data = data; }
public SD2.GraphicsPath PdfPath { get; set; }
public SD2.GraphicsPath Path { get; set; }
public SD.Region Region { get; set; }
public double Area { get; set; }
public int Offset { get; set; }
public int Length { get; set; }
public byte[] Data { get; set; }
public int OwnerID { get; set; }
public string Text {
get {
int length = Math.Min(Data.Length - Offset, Length);
if (Offset < 0 || length <= 0)
return "";
return ASCIIEncoding.ASCII.GetString(Data, Offset, length).TrimEnd();
}
}
public string Context {
get {
return ASCIIEncoding.ASCII.GetString(Data, 0, Data.Length);
}
}
public RectangleF Bounds {
get {
return Path != null ? Path.GetBounds() : new RectangleF();
}
}
}
public class Graphics {
public List<Fixture> All { get; set; } = new List<Fixture>();
public Dictionary<int, List<Fixture>> Container { get; set; } = new Dictionary<int, List<Fixture>>();
public void Transform(float top, float dpi) {
var xform = new SD2.Matrix();
xform.Scale(1, -1, SD2.MatrixOrder.Append);
xform.Translate(0, (float)top, SD2.MatrixOrder.Append);
float scale = dpi / 72.0f;
xform.Scale(scale, scale, SD2.MatrixOrder.Append);
foreach (var part in All) {
part.Path = (SD2.GraphicsPath)part.PdfPath.Clone();
part.Path.Transform(xform);
try {
for (int i = 0; i < 10; i++) {
part.Area = 0;
part.Region = new Region(part.Path);
foreach (var rect in part.Region.GetRegionScans(new SD2.Matrix()))
part.Area += Math.Abs(rect.Width * rect.Height);
if (part.Area > 1)
break;
// Widen throws out of memory if path is a point
part.Path.Widen(new Pen(Color.Black, 1));
}
}
catch {
}
}
}
}
private static readonly HashSet<string> _ops;
static GraphicsScanner() {
List<string> state = new List<string>();
state.AddRange(ContentStreamScanner.OperatorCategories.PathPainting);
state.AddRange(ContentStreamScanner.OperatorCategories.PathConstruction);
state.AddRange(ContentStreamScanner.OperatorCategories.ClippingPaths);
state.AddRange(ContentStreamScanner.OperatorCategories.SpecialGraphicsState);
state.AddRange(ContentStreamScanner.OperatorCategories.TextObjects);
state.AddRange(ContentStreamScanner.OperatorCategories.TextState);
state.AddRange(ContentStreamScanner.OperatorCategories.TextPositioning);
state.AddRange(ContentStreamScanner.OperatorCategories.TextShowing);
state.AddRange(ContentStreamScanner.OperatorCategories.XObjects);
state.Add("Meta:InlineImage");
_ops = new HashSet<string>(state);
}
private IndirectObject _owner = null;
private byte[] _data = null;
private SD.PointF _point = new PointF();
private SD2.GraphicsPath _path = null;
private Graphics _results = null;
private int _start = 0, _pos = 0;
public GraphicsScanner(IndirectObject owner) : base() {
if (owner == null)
throw new NullReferenceException(nameof(owner));
_owner = owner;
Operators = _ops;
DoWidths = true;
DoShowText = true;
DoUnicode = false;
}
public Graphics GetResults() {
Process();
return _results;
}
public void Process() {
var contentIDs = new List<int>();
var contentEnds = new List<int>();
if (_owner is FormXObject) {
FormXObject xo = (FormXObject)_owner;
_data = new byte[xo.CopyDecompressedData()];
xo.CopyDecompressedData(_data);
contentIDs.Add(xo.ID);
contentEnds.Add(_data.Length);
}
else if (_owner is Page) {
Page page = (Page)_owner;
int offset = 0;
foreach (var so in page.GetLayers()) {
int length = so.CopyDecompressedData();
contentIDs.Add(so.ID);
contentEnds.Add(offset + length);
offset += length + 1;
}
_data = page.GetContentData();
}
else
throw new Exception("Object is not a content stream type.");
_point = new PointF();
_path = null;
_results = new Graphics();
_results.All = new List<Fixture>();
// get data
IList<int> offsets = null;
ArrayAtom array = ArrayAtom.FromContentStream(_data, out offsets);
offsets.Add(_data.Length);
Debug.Assert(offsets.Count == array.Count + 1);
// process
_start = _pos = 0;
Process(_owner, array);
// convert ArrayAtom based offsets to byte based offsets
foreach (var part in _results.All) {
Debug.Assert(part.Length >= 0);
if (part.Offset + part.Length < offsets.Count) {
int p1 = offsets[part.Offset];
int p2 = offsets[part.Offset + part.Length];
Debug.Assert(p2 - p1 >= 0);
part.Offset = p1;
part.Length = p2 - p1;
}
else {
part.Offset = 0;
part.Length = 0;
}
}
// identify original containers
if (true) {
List<Fixture> list = null;
int item = -1, last = -1, id = -1;
foreach (var part in _results.All) {
while (part.Offset >= last) {
item++;
last = contentEnds[item];
list = new List<Fixture>();
id = contentIDs[item];
_results.Container[id] = list;
}
part.OwnerID = id;
list.Add(part);
}
}
// add in annotations
if (_owner is Page) {
Page page = (Page)_owner;
foreach (var annot in page.GetAnnotations()) {
var path = MakeRectPath(annot.Rect);
var desc = $"Annotation ID {annot.ID}";
var data = ASCIIEncoding.ASCII.GetBytes(desc);
var fixture = new Fixture(path, 0, data.Length, data);
_results.All.Add(fixture);
var list = new List<Fixture>();
list.Add(fixture);
_results.Container[annot.ID] = list;
}
}
}
public override void ProcessItem(IndirectObject owner, ArrayAtom contents, string op, int pos) {
_pos = pos;
try {
base.ProcessItem(owner, contents, op, pos);
}
catch (Exception ex) {
#if FALSE
Debug.Assert(false, "Unexpected exception. Probable corrupt stream.");
#endif
}
switch (op) {
// path construction
case "m":
if (true) {
if (_path == null) {
_start = pos - 2;
_path = new SD2.GraphicsPath();
}
_path.StartFigure();
_point = GetPointF(contents, pos, 0, 1);
}
break;
case "l":
if (true) {
if (_path == null) {
_start = pos - 2;
_path = new SD2.GraphicsPath();
}
PointF point = GetPointF(contents, pos, 0, 1);
_path.AddLine(_point, point);
_point = point;
}
break;
case "c":
if (true) {
if (_path == null) {
_start = pos - 6;
_path = new SD2.GraphicsPath();
}
PointF control1 = GetPointF(contents, pos, 0, 3);
PointF control2 = GetPointF(contents, pos, 1, 3);
PointF point = GetPointF(contents, pos, 2, 3);
_path.AddBezier(_point, control1, control2, point);
_point = point;
}
break;
case "v":
if (true) {
if (_path == null) {
_start = pos - 4;
_path = new SD2.GraphicsPath();
}
PointF control = GetPointF(contents, pos, 0, 2);
PointF point = GetPointF(contents, pos, 1, 2);
_path.AddBezier(_point, _point, control, point);
_point = point;
}
break;
case "y":
if (true) {
if (_path == null) {
_start = pos - 4;
_path = new SD2.GraphicsPath();
}
PointF control = GetPointF(contents, pos, 0, 2);
PointF point = GetPointF(contents, pos, 1, 2);
_path.AddBezier(_point, control, point, point);
_point = point;
}
break;
case "h":
if (true) {
if (_path == null) {
_start = pos;
_path = new SD2.GraphicsPath();
}
_path.CloseFigure();
}
break;
case "re":
if (true) {
if (_path == null) {
_start = pos - 4;
_path = new SD2.GraphicsPath();
}
_path.StartFigure();
_point = GetPointF(contents, pos, 0, 2);
PointF size = GetPointF(contents, pos, 1, 2);
RectangleF rectF = new RectangleF(_point.X, _point.Y, size.X, size.Y);
_path.AddLine(rectF.X, rectF.Y, rectF.X + rectF.Width, rectF.Y);
_path.AddLine(rectF.X + rectF.Width, rectF.Y, rectF.X + rectF.Width, rectF.Y + rectF.Height);
_path.AddLine(rectF.X + rectF.Width, rectF.Y + rectF.Height, rectF.X, rectF.Y + rectF.Height);
_path.CloseFigure();
}
break;
// path drawing
case "S":
case "s":
case "f":
case "F":
case "f*":
case "B":
case "B*":
case "b":
case "b*":
case "n":
if (_path != null) {
_path.Transform(State.CTM.Matrix);
_results.All.Add(new Fixture(_path, _start, pos - _start + 1, _data));
_start = pos;
_path = null;
_point = new PointF();
}
else {
#if FALSE
var snip = new ArrayAtom();
for (int i = pos - 20; i < pos + 20 && i >= 0; i++)
snip.Add(contents[i].Clone());
string snippet = snip.ToString();
Debug.Assert(false, "Degenerate path detected.");
#endif
}
break;
// xobject
case "Do":
if (true) {
var pair = Resources.GetResource(_owner, ResourceType.XObject, GetName(contents, pos));
FormXObject formX = pair != null ? pair.Object as FormXObject : null;
XRect rect = formX != null ? formX.BBox : XRect.FromSides(0, 0, 1, 1);
RectangleF rectF = new RectangleF((float)rect.Left, (float)rect.Top, (float)rect.Width, (float)rect.Height);
var path = MakeRectPath(rect);
if (formX != null && formX.Matrix != null)
path.Transform(formX.Matrix.Matrix);
path.Transform(State.CTM.Matrix);
_results.All.Add(new Fixture(path, pos - 1, 2, _data));
}
break;
// other
case "Meta:InlineImage":
if (true) {
XRect rect = XRect.FromSides(0, 0, 1, 1);
var path = new SD2.GraphicsPath();
path.AddLine((float)rect.Left, (float)rect.Bottom, (float)rect.Right, (float)rect.Bottom);
path.AddLine((float)rect.Right, (float)rect.Bottom, (float)rect.Right, (float)rect.Top);
path.AddLine((float)rect.Right, (float)rect.Top, (float)rect.Left, (float)rect.Top);
path.CloseFigure();
path.Transform(State.CTM.Matrix);
_results.All.Add(new Fixture(path, pos, 1, _data));
}
break;
}
}
public override void ShowText(List<int> codes, List<int> widths, List<double> advances, double advanceTotal, StringBuilder text, List<string> strings, bool vertical) {
advanceTotal /= State.TextFontSize;
var rect = !vertical ? XRect.FromSides(0, 0, advanceTotal, 1) : XRect.FromSides(0, 0, 1, advanceTotal);
var trm = Text.GetTextRenderingMatrix(State); // includes font size and CTM
var corners = trm.TransformPoints(rect.GetCorners());
var points = new PointF[corners.Length];
for (int i = 0; i < corners.Length; i++)
points[i] = new PointF((float)corners[i].X, (float)corners[i].Y);
var path = new SD2.GraphicsPath();
path.AddLines(points);
path.CloseFigure();
_results.All.Add(new Fixture(path, _pos - 1, 2, _data));
base.ShowText(codes, widths, advances, advanceTotal, text, strings, vertical);
}
private static string GetName(ArrayAtom array, int pos) {
return array[pos - 1, (Enum)null];
}
private static PointF GetPointF(ArrayAtom array, int pos, int index, int count) {
pos = pos - (count * 2) + (index * 2);
return new PointF((float)array[pos, (double)0], (float)array[pos + 1, (double)0]);
}
private SD2.GraphicsPath MakeRectPath(XRect rect) {
var path = new SD2.GraphicsPath();
path.AddLine((float)rect.Left, (float)rect.Bottom, (float)rect.Right, (float)rect.Bottom);
path.AddLine((float)rect.Right, (float)rect.Bottom, (float)rect.Right, (float)rect.Top);
path.AddLine((float)rect.Right, (float)rect.Top, (float)rect.Left, (float)rect.Top);
path.CloseFigure();
return path;
}
}
}