StatefulOperatorHandler.java
22.8 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.cos.CosDictionary
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.fontset.PDFFontSet
* com.adobe.internal.pdftoolkit.core.types.ASArray
* com.adobe.internal.pdftoolkit.core.types.ASCoordinate
* com.adobe.internal.pdftoolkit.core.types.ASMatrix
* com.adobe.internal.pdftoolkit.core.types.ASName
* com.adobe.internal.pdftoolkit.core.types.ASString
*/
package com.adobe.internal.pdftoolkit.pdf.content;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.fontset.PDFFontSet;
import com.adobe.internal.pdftoolkit.core.types.ASArray;
import com.adobe.internal.pdftoolkit.core.types.ASCoordinate;
import com.adobe.internal.pdftoolkit.core.types.ASMatrix;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.pdf.content.Content;
import com.adobe.internal.pdftoolkit.pdf.content.ContentStack;
import com.adobe.internal.pdftoolkit.pdf.content.Instruction;
import com.adobe.internal.pdftoolkit.pdf.content.NoopOperatorHandler;
import com.adobe.internal.pdftoolkit.pdf.content.XObjectProcessingFunction;
import com.adobe.internal.pdftoolkit.pdf.content.XObjectsCycleDetector;
import com.adobe.internal.pdftoolkit.pdf.content.processor.ActualText;
import com.adobe.internal.pdftoolkit.pdf.content.processor.GState;
import com.adobe.internal.pdftoolkit.pdf.content.processor.GStateStack;
import com.adobe.internal.pdftoolkit.pdf.content.processor.Path;
import com.adobe.internal.pdftoolkit.pdf.content.processor.StatefulContentStreamProcessor;
import com.adobe.internal.pdftoolkit.pdf.content.processor.TextObject;
import com.adobe.internal.pdftoolkit.pdf.content.processor.TextState;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFResources;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFDashPattern;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFExtGState;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFLineCap;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFLineJoin;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFRenderingIntent;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFFont;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFFontMap;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObject;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectForm;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectMap;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import java.util.HashMap;
public abstract class StatefulOperatorHandler
extends NoopOperatorHandler {
protected ContentStack contentStack = new ContentStack();
protected GStateStack gStateStack = new GStateStack<GState>(new GState());
protected Path currentPath;
protected TextObject currentTextObj;
protected PDFFontSet fontSet;
protected boolean isTD = false;
protected HashMap<PDFResources, HashMap<String, Integer>> fontCache = new HashMap();
protected HashMap<Integer, PDFFont> cosToFontMap = new HashMap();
protected PDFPage currentPage;
private boolean ignoreErrors;
protected XObjectsCycleDetector xobjectsCycleDetector;
private ActualText actualText;
public StatefulOperatorHandler() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
}
protected StatefulOperatorHandler(PDFPage page) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this();
this.currentPage = page;
}
protected PDFResources getResources() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.contentStack.peek().getResources();
}
public ContentStack getContentStack() {
return this.contentStack;
}
public GState getGState() {
return this.gStateStack.peek();
}
public void pushGState() {
GState gState = new GState((GState)this.gStateStack.peek());
this.gStateStack.push(gState);
}
public GState popGState() {
return this.gStateStack.pop();
}
public void w(double lineWidth, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.getGState().setLineWidth(lineWidth);
}
public void J(PDFLineCap lineCap, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.getGState().setLineCap(lineCap);
}
public void j(PDFLineJoin lineJoin, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.getGState().setLineJoin(lineJoin);
}
public void M(double miterLimit, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.getGState().setMiterLimit(miterLimit);
}
public void d(PDFDashPattern dashPattern, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.getGState().setDashPattern(dashPattern);
}
public void ri(PDFRenderingIntent intent, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.getGState().setRenderingIntent(intent);
}
public void i(double flatness, Instruction instruction) throws PDFIOException {
this.getGState().setFlatness(flatness);
}
public void gs(ASName extGStateName, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFResources resources = this.getResources();
PDFExtGState extGState = resources.getExtGState(extGStateName);
this.getGState().loadExtGState(extGState);
}
public void q(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.pushGState();
}
public void Q(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.popGState();
}
public void cm(double a, double b, double c, double d, double e, double f, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ASMatrix ctm = new ASMatrix(a, b, c, d, e, f);
GState gState = this.getGState();
gState.setCTM(ctm.concat(gState.getCTM()));
}
public void m(double x, double y, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.moveTo(new ASCoordinate(x, y));
}
public void l(double x, double y, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.lineTo(new ASCoordinate(x, y));
}
public void c(double x1, double y1, double x2, double y2, double x3, double y3, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.curveTo(new ASCoordinate(x1, y1), new ASCoordinate(x2, y2), new ASCoordinate(x3, y3));
}
public void v(double x2, double y2, double x3, double y3, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.curveToV(new ASCoordinate(x2, y2), new ASCoordinate(x3, y3));
}
public void y(double x1, double y1, double x3, double y3, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.curveToV(new ASCoordinate(x1, y1), new ASCoordinate(x3, y3));
}
public void h(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.close();
}
public void re(double x, double y, double width, double height, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.moveTo(new ASCoordinate(x, y));
this.currentPath.lineTo(new ASCoordinate(x + width, y));
this.currentPath.lineTo(new ASCoordinate(x + width, y + height));
this.currentPath.lineTo(new ASCoordinate(x, y + height));
this.currentPath.close();
}
protected void createNewPath() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.currentPath = new Path();
this.currentPath.setWindingRule(0);
}
protected void endPath() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.currentPath = null;
}
public void S(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.endPath();
}
public void s(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.close();
this.endPath();
}
public void f(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath != null) {
this.currentPath.close();
this.endPath();
}
}
public void fStar(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.close();
this.endPath();
}
public void B(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.endPath();
}
public void BStar(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.endPath();
}
public void b(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.close();
this.endPath();
}
public void bStar(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.close();
this.endPath();
}
public void n(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.setShow(false);
this.endPath();
}
public void W(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.close();
this.currentPath.setWindingRule(1);
this.getGState().addPathToClip(this.currentPath);
}
public void WStar(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentPath == null) {
this.createNewPath();
}
this.currentPath.close();
this.currentPath.setWindingRule(0);
this.getGState().addPathToClip(this.currentPath);
}
public void Tc(double charSpacing, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
this.getTextState().setCharSpacing(charSpacing);
}
public void Tw(double wordSpacing, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
this.getTextState().setWordSpacing(wordSpacing);
}
public void Tz(double scaling, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
this.getTextState().setHorizScaling(scaling);
}
public void TL(double leading, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
this.getTextState().setLeading(leading);
}
public void Tf(ASName fontName, double fontSize, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
String asFontName;
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
TextState textState = this.getTextState();
textState.setFontName(fontName);
textState.setFontSize(fontSize);
PDFResources resources = this.getResources();
HashMap nameToObjectNumMap = this.fontCache.get(resources);
if (nameToObjectNumMap == null) {
nameToObjectNumMap = new HashMap();
this.fontCache.put(resources, nameToObjectNumMap);
}
if (!nameToObjectNumMap.containsKey(asFontName = fontName.asString())) {
PDFFontMap pageFontMap;
PDFResources pageRes;
CosDictionary obj = null;
PDFFontMap pdfFontMap = null;
if (resources != null) {
pdfFontMap = PDFFontMap.getInstance(resources.getDictionaryCosObjectValue(ASName.k_Font));
}
if (pdfFontMap != null) {
obj = pdfFontMap.getDictionaryDictionaryValue(fontName);
}
if (obj == null && (pageRes = this.currentPage.getResources()) != null && (pageFontMap = PDFFontMap.getInstance(pageRes.getDictionaryCosObjectValue(ASName.k_Font))) != null) {
pdfFontMap = pageFontMap;
obj = pageFontMap.getDictionaryDictionaryValue(fontName);
}
if (obj == null) {
if (this.currentTextObj != null) {
this.currentTextObj.setSkipThisTextObject(true);
}
return;
}
Integer objNum = obj.getObjNum();
PDFFont font = this.cosToFontMap.get(objNum);
if (font == null) {
font = pdfFontMap.get(fontName);
this.cosToFontMap.put(objNum, font);
}
font.setFontSet(this.fontSet);
nameToObjectNumMap.put(asFontName, objNum);
textState.setFont(font);
} else {
Integer objNum = (Integer)nameToObjectNumMap.get(asFontName);
PDFFont font = this.cosToFontMap.get(objNum);
textState.setFont(font);
}
}
public void Tr(int renderMode, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
this.getTextState().setRenderingMode(renderMode);
}
public void Ts(double textRise, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
this.getTextState().setRise(textRise);
}
public void BT(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.currentTextObj = new TextObject(this.currentPage.getPDFDocument(), this.ignoreErrors);
}
public void ET(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
}
public void Td(double tx, double ty, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
TextObject textObj = this.getCurrentTextObject();
ASMatrix tlm = textObj.getTextLineMatrix();
tlm = tlm.preMultiply(tx, ty);
textObj.setTextLineMatrix(tlm);
textObj.setTextMatrix(tlm);
}
public void TD(double tx, double ty, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
this.isTD = true;
this.TL(- ty, null);
this.Td(tx, ty, null);
}
public void Tm(double a, double b, double c, double d, double e, double f, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && !this.currentTextObj.skipThisTextObject()) {
ASMatrix tm;
ASMatrix tlm = tm = new ASMatrix(a, b, c, d, e, f);
TextObject textObj = this.getCurrentTextObject();
textObj.setTextMatrix(tm);
textObj.setTextLineMatrix(tlm);
}
}
public void TStar(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
double leading = this.getTextState().getLeading();
this.Td(0.0, - leading, null);
}
public void Tj(ASString string, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.currentTextObj.addTextRun(string, this.getGState(), this.actualText);
}
public void SingleQuote(ASString string, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
this.TStar(null);
this.Tj(string, null);
}
public void DoubleQuote(double wordSpacing, double charSpacing, ASString string, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
this.Tw(wordSpacing, null);
this.Tc(charSpacing, null);
this.currentTextObj.addTextRun(string, this.getGState(), this.actualText);
}
public void TJ(ASArray tjArray, Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.currentTextObj != null && this.currentTextObj.skipThisTextObject()) {
return;
}
this.currentTextObj.addTextRun(tjArray, this.getGState(), this.actualText);
}
public TextState getTextState() {
return this.getGState().getTextState();
}
public TextObject getCurrentTextObject() {
return this.currentTextObj;
}
public void BI(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
}
public void EI(Instruction instruction) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void processXObject(ASName name, Instruction instruction, XObjectProcessingFunction function) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFXObject xObject = null;
PDFResources xobjectsResources = this.getResources();
PDFResources pageResources = this.currentPage.getResources();
if (xobjectsResources == null) {
if (pageResources != null) {
xObject = pageResources.getXObjectMap().get(name);
}
} else {
PDFXObjectMap pdfxObjectMap;
PDFXObjectMap xObjectMap = xobjectsResources.getXObjectMap();
if ((xObjectMap == null || (xObject = xObjectMap.get(name)) == null) && pageResources != null && (pdfxObjectMap = pageResources.getXObjectMap()) != null) {
xObject = pdfxObjectMap.get(name);
}
}
if (xObject != null) {
this.q(null);
if (this.xobjectsCycleDetector != null) {
if (this.xobjectsCycleDetector.cycleDetected(xObject)) {
throw new PDFInvalidDocumentException("Cyclic dependency found while processing xobjects.");
}
this.xobjectsCycleDetector.push(xObject);
}
try {
if (function != null) {
function.process(xObject);
} else if (xObject instanceof PDFXObjectForm) {
PDFXObjectForm xObjectForm = (PDFXObjectForm)xObject;
StatefulContentStreamProcessor streamProcessor = new StatefulContentStreamProcessor(this);
streamProcessor.process(Content.newInstance(xObjectForm));
}
}
finally {
if (this.xobjectsCycleDetector != null) {
this.xobjectsCycleDetector.pop();
}
}
this.Q(null);
}
}
public void setIgnoreErrors(boolean ignoreErrors) {
this.ignoreErrors = ignoreErrors;
}
}