-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyPdfContentStreamProcessor.java
More file actions
1051 lines (918 loc) · 41.9 KB
/
Copy pathMyPdfContentStreamProcessor.java
File metadata and controls
1051 lines (918 loc) · 41.9 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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Modified version of: com.itextpdf.text.pdf.parser.PdfContentStreamProcessor
* with some private fields changed to protected and protected push/pop methods added,
* to support sub-class org.t3as.pdf.RedactionStreamProcessor.
* Modifications Copyright (c) 2014 NICTA
*
* $Id: PdfContentStreamProcessor.java 6134 2013-12-23 13:15:14Z blowagie $
*
* This file is part of the iText (R) project.
* Copyright (c) 1998-2014 iText Group NV
* Authors: Kevin Day, Bruno Lowagie, Paulo Soares, et al.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation with the addition of the
* following permission added to Section 15 as permitted in Section 7(a):
* FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
* ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
* OF THIRD PARTY RIGHTS
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA, 02110-1301 USA, or download the license from the following URL:
* http://itextpdf.com/terms-of-use/
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License.
*
* In accordance with Section 7(b) of the GNU Affero General Public License,
* a covered work must retain the producer line in every PDF that is created
* or manipulated using iText.
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial activities involving the iText software without
* disclosing the source code of your own applications.
* These activities include: offering paid services to customers as an ASP,
* serving PDFs on the fly in a web application, shipping iText with a closed
* source product.
*
* For more information, please contact iText Software Corp. at this
* address: sales@itextpdf.com
*/
package com.itextpdf.text.pdf.parser;
//in this package to get access to package protected items
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.error_messages.MessageLocalization;
import com.itextpdf.text.io.RandomAccessSourceFactory;
import com.itextpdf.text.pdf.CMYKColor;
import com.itextpdf.text.pdf.CMapAwareDocumentFont;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PRIndirectReference;
import com.itextpdf.text.pdf.PRTokeniser;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfContentParser;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfIndirectReference;
import com.itextpdf.text.pdf.PdfLiteral;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfStream;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
/**
* Processor for a PDF content Stream.
* @since 2.1.4
*/
public class MyPdfContentStreamProcessor {
public interface MyContentOperator {
/**
* Invokes a content operator.
* @param processor the processor that is dealing with the PDF content
* @param operator the literal PDF syntax of the operator
* @param operands the operands that come with the operator
* @throws Exception any exception can be thrown - it will be re-packaged into a runtime exception and re-thrown by the {@link PdfContentStreamProcessor}
*/
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) throws Exception;
}
public interface MyXObjectDoHandler {
public void handleXObject(MyPdfContentStreamProcessor processor, PdfStream stream, PdfIndirectReference ref);
}
/**
* Default operator
* @since 5.0.1
*/
public static final String DEFAULTOPERATOR = "DefaultOperator";
/** A map with all supported operators operators (PDF syntax). */
final private Map<String, MyContentOperator> operators;
/** Resources for the content stream. */
private ResourceDictionary resources;
/** Stack keeping track of the graphics state. */
private final Stack<GraphicsState> gsStack = new Stack<GraphicsState>();
/** Text matrix. */
private Matrix textMatrix;
/** Text line matrix. */
private Matrix textLineMatrix;
/** Listener that will be notified of render events */
final private RenderListener renderListener;
/** A map with all supported XObject handlers */
final private Map<PdfName, MyXObjectDoHandler> xobjectDoHandlers;
/**
* The font cache.
* @since 5.0.6
*/
/** */
final private Map<Integer,CMapAwareDocumentFont> cachedFonts = new HashMap<Integer, CMapAwareDocumentFont>();
/**
* A stack containing marked content info.
* @since 5.0.2
*/
private final Stack<MarkedContentInfo> markedContentStack = new Stack<MarkedContentInfo>();
private PdfName fontResourceName = null;
/**
* Creates a new PDF Content Stream Processor that will send it's output to the
* designated render listener.
*
* @param renderListener the {@link RenderListener} that will receive rendering notifications
*/
public MyPdfContentStreamProcessor(RenderListener renderListener) {
this.renderListener = renderListener;
operators = new HashMap<String, MyContentOperator>();
populateOperators();
xobjectDoHandlers = new HashMap<PdfName, MyXObjectDoHandler>();
populateXObjectDoHandlers();
reset();
}
private void setFontResourceName(PdfName fontResourceName) {
this.fontResourceName = fontResourceName;
}
/**
* @return resource name for the current font set by the most recent SetTextFont (Tf) command.
*/
protected PdfName getFontResourceName() {
return this.fontResourceName;
}
private void populateXObjectDoHandlers(){
registerXObjectDoHandler(PdfName.DEFAULT, new IgnoreXObjectDoHandler());
registerXObjectDoHandler(PdfName.FORM, new FormXObjectDoHandler());
registerXObjectDoHandler(PdfName.IMAGE, new ImageXObjectDoHandler());
}
/**
* Registers a Do handler that will be called when Do for the provided XObject subtype is encountered during content processing.
* <br>
* If you register a handler, it is a very good idea to pass the call on to the existing registered handler (returned by this call), otherwise you
* may inadvertently change the internal behavior of the processor.
* @param xobjectSubType the XObject subtype this handler will process, or PdfName.DEFAULT for a catch-all handler
* @param handler the handler that will receive notification when the Do operator for the specified subtype is encountered
* @return the existing registered handler, if any
* @since 5.0.1
*/
public MyXObjectDoHandler registerXObjectDoHandler(PdfName xobjectSubType, MyXObjectDoHandler handler){
return xobjectDoHandlers.put(xobjectSubType, handler);
}
/**
* Gets the font pointed to by the indirect reference. The font may have been cached.
* @param ind the indirect reference ponting to the font
* @return the font
* @since 5.0.6
*/
private CMapAwareDocumentFont getFont(PRIndirectReference ind) {
Integer n = Integer.valueOf(ind.getNumber());
CMapAwareDocumentFont font = cachedFonts.get(n);
if (font == null) {
font = new CMapAwareDocumentFont(ind);
cachedFonts.put(n, font);
}
return font;
}
private CMapAwareDocumentFont getFont(PdfDictionary fontResource) {
return new CMapAwareDocumentFont(fontResource);
}
/**
* Loads all the supported graphics and text state operators in a map.
*/
private void populateOperators(){
registerContentOperator(DEFAULTOPERATOR, new IgnoreOperatorContentOperator());
registerContentOperator("q", new PushGraphicsState());
registerContentOperator("Q", new PopGraphicsState());
registerContentOperator("g", new SetGrayFill());
registerContentOperator("G", new SetGrayStroke());
registerContentOperator("rg", new SetRGBFill());
registerContentOperator("RG", new SetRGBStroke());
registerContentOperator("k", new SetCMYKFill());
registerContentOperator("K", new SetCMYKStroke());
registerContentOperator("cs", new SetColorSpaceFill());
registerContentOperator("CS", new SetColorSpaceStroke());
registerContentOperator("sc", new SetColorFill());
registerContentOperator("SC", new SetColorStroke());
registerContentOperator("scn", new SetColorFill());
registerContentOperator("SCN", new SetColorStroke());
registerContentOperator("cm", new ModifyCurrentTransformationMatrix());
registerContentOperator("gs", new ProcessGraphicsStateResource());
SetTextCharacterSpacing tcOperator = new SetTextCharacterSpacing();
registerContentOperator("Tc", tcOperator);
SetTextWordSpacing twOperator = new SetTextWordSpacing();
registerContentOperator("Tw", twOperator);
registerContentOperator("Tz", new SetTextHorizontalScaling());
SetTextLeading tlOperator = new SetTextLeading();
registerContentOperator("TL", tlOperator);
registerContentOperator("Tf", new SetTextFont());
registerContentOperator("Tr", new SetTextRenderMode());
registerContentOperator("Ts", new SetTextRise());
registerContentOperator("BT", new BeginText());
registerContentOperator("ET", new EndText());
registerContentOperator("BMC", new BeginMarkedContent());
registerContentOperator("BDC", new BeginMarkedContentDictionary());
registerContentOperator("EMC", new EndMarkedContent());
TextMoveStartNextLine tdOperator = new TextMoveStartNextLine();
registerContentOperator("Td", tdOperator);
registerContentOperator("TD", new TextMoveStartNextLineWithLeading(tdOperator, tlOperator));
registerContentOperator("Tm", new TextSetTextMatrix());
TextMoveNextLine tstarOperator = new TextMoveNextLine(tdOperator);
registerContentOperator("T*", tstarOperator);
ShowText tjOperator = new ShowText();
registerContentOperator("Tj", tjOperator);
MoveNextLineAndShowText tickOperator = new MoveNextLineAndShowText(tstarOperator, tjOperator);
registerContentOperator("'", tickOperator);
registerContentOperator("\"", new MoveNextLineAndShowTextWithSpacing(twOperator, tcOperator, tickOperator));
registerContentOperator("TJ", new ShowTextArray());
registerContentOperator("Do", new Do());
}
/**
* Registers a content operator that will be called when the specified operator string is encountered during content processing.
* <br>
* If you register an operator, it is a very good idea to pass the call on to the existing registered operator (returned by this call), otherwise you
* may inadvertently change the internal behavior of the processor.
* @param operatorString the operator id, or DEFAULTOPERATOR for a catch-all operator
* @param operator the operator that will receive notification when the operator is encountered
* @return the existing registered operator, if any
* @since 2.1.7
*/
public MyContentOperator registerContentOperator(String operatorString, MyContentOperator operator){
return operators.put(operatorString, operator);
}
/**
* Resets the graphics state stack, matrices and resources.
*/
public void reset(){
gsStack.removeAllElements();
gsStack.add(new GraphicsState());
textMatrix = null;
textLineMatrix = null;
resources = new ResourceDictionary();
fontResourceName = null;
}
protected void push(PdfDictionary r) {
resources.push(r);
}
protected void pop() {
resources.pop();
}
/**
* Returns the current graphics state.
* @return the graphics state
*/
protected GraphicsState gs(){
return gsStack.peek();
}
/**
* Invokes an operator.
* @param operator the PDF Syntax of the operator
* @param operands a list with operands
*/
protected void invokeOperator(PdfLiteral operator, ArrayList<PdfObject> operands) throws Exception{
MyContentOperator op = operators.get(operator.toString());
if (op == null)
op = operators.get(DEFAULTOPERATOR);
op.invoke(this, operator, operands);
}
/**
* Add to the marked content stack
* @param tag the tag of the marked content
* @param dict the PdfDictionary associated with the marked content
* @since 5.0.2
*/
private void beginMarkedContent(PdfName tag, PdfDictionary dict) {
markedContentStack.push(new MarkedContentInfo(tag, dict));
}
/**
* Remove the latest marked content from the stack. Keeps track of the BMC, BDC and EMC operators.
* @since 5.0.2
*/
private void endMarkedContent() {
markedContentStack.pop();
}
/**
* Decodes a PdfString (which will contain glyph ids encoded in the font's encoding)
* based on the active font, and determine the unicode equivalent
* @param in the String that needs to be encoded
* @return the encoded String
* @since 2.1.7
*/
private String decode(PdfString in){
byte[] bytes = in.getBytes();
return gs().font.decode(bytes, 0, bytes.length);
}
/**
* Used to trigger beginTextBlock on the renderListener
*/
private void beginText(){
renderListener.beginTextBlock();
}
/**
* Used to trigger endTextBlock on the renderListener
*/
private void endText(){
renderListener.endTextBlock();
}
/**
* Displays text.
* @param string the text to display
*/
private void displayPdfString(PdfString string){
String unicode = decode(string);
TextRenderInfo renderInfo = new TextRenderInfo(unicode, gs(), textMatrix, markedContentStack);
renderListener.renderText(renderInfo);
textMatrix = new Matrix(renderInfo.getUnscaledWidth(), 0).multiply(textMatrix);
}
/**
* Displays an XObject using the registered handler for this XObject's subtype
* @param xobjectName the name of the XObject to retrieve from the resource dictionary
*/
private void displayXObject(PdfName xobjectName) throws IOException {
PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
PdfObject xobject = xobjects.getDirectObject(xobjectName);
PdfStream xobjectStream = (PdfStream)xobject;
PdfName subType = xobjectStream.getAsName(PdfName.SUBTYPE);
if (xobject.isStream()){
MyXObjectDoHandler handler = xobjectDoHandlers.get(subType);
if (handler == null)
handler = xobjectDoHandlers.get(PdfName.DEFAULT);
handler.handleXObject(this, xobjectStream, xobjects.getAsIndirectObject(xobjectName));
} else {
throw new IllegalStateException(MessageLocalization.getComposedMessage("XObject.1.is.not.a.stream", xobjectName));
}
}
/**
* Adjusts the text matrix for the specified adjustment value (see TJ operator in the PDF spec for information)
* @param tj the text adjustment
*/
private void applyTextAdjust(float tj){
float adjustBy = -tj/1000f * gs().fontSize * gs().horizontalScaling;
textMatrix = new Matrix(adjustBy, 0).multiply(textMatrix);
}
/**
* Processes PDF syntax.
* <b>Note:</b> If you re-use a given {@link MyPdfContentStreamProcessor}, you must call {@link MyPdfContentStreamProcessor#reset()}
* @param contentBytes the bytes of a content stream
* @param resources the resources that come with the content stream
*/
public void processContent(byte[] contentBytes, PdfDictionary resources){
this.resources.push(resources);
try {
PRTokeniser tokeniser = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().createSource(contentBytes)));
PdfContentParser ps = new PdfContentParser(tokeniser);
ArrayList<PdfObject> operands = new ArrayList<PdfObject>();
while (ps.parse(operands).size() > 0){
PdfLiteral operator = (PdfLiteral)operands.get(operands.size()-1);
if ("BI".equals(operator.toString())){
// we don't call invokeOperator for embedded images - this is one area of the PDF spec that is particularly nasty and inconsistent
PdfDictionary colorSpaceDic = resources != null ? resources.getAsDict(PdfName.COLORSPACE) : null;
handleInlineImage(InlineImageUtils.parseInlineImage(ps, colorSpaceDic), colorSpaceDic);
} else {
invokeOperator(operator, operands);
}
}
}
catch (Exception e) {
throw new ExceptionConverter(e);
}
this.resources.pop();
}
/**
* Callback when an inline image is found. This requires special handling because inline images don't follow the standard operator syntax
* @param info the inline image
* @param colorSpaceDic the color space for the inline immage
*/
protected void handleInlineImage(InlineImageInfo info, PdfDictionary colorSpaceDic){
ImageRenderInfo renderInfo = ImageRenderInfo.createForEmbeddedImage(gs().ctm, info, colorSpaceDic);
renderListener.renderImage(renderInfo);
}
/**
* A resource dictionary that allows stack-like behavior to support resource dictionary inheritance
*/
private static class ResourceDictionary extends PdfDictionary {
private final List<PdfDictionary> resourcesStack = new ArrayList<PdfDictionary>();
public ResourceDictionary() {
}
public void push(PdfDictionary resources){
resourcesStack.add(resources);
}
public void pop(){
resourcesStack.remove(resourcesStack.size()-1);
}
@Override
public PdfObject getDirectObject(PdfName key) {
for (int i = resourcesStack.size() - 1; i >= 0; i--){
PdfDictionary subResource = resourcesStack.get(i);
if (subResource != null){
PdfObject obj = subResource.getDirectObject(key);
if (obj != null) return obj;
}
}
return super.getDirectObject(key); // shouldn't be necessary, but just in case we've done something crazy
}
}
/**
* A content operator implementation (unregistered).
*/
private static class IgnoreOperatorContentOperator implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands){
// ignore the operator
}
}
/**
* A content operator implementation (TJ).
*/
private static class ShowTextArray implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfArray array = (PdfArray)operands.get(0);
for (Iterator<PdfObject> i = array.listIterator(); i.hasNext(); ) {
PdfObject entryObj = i.next();
if (entryObj instanceof PdfString){
processor.displayPdfString((PdfString)entryObj);
} else {
float tj = ((PdfNumber)entryObj).floatValue();
processor.applyTextAdjust(tj);
}
}
}
}
/**
* A content operator implementation (").
*/
private static class MoveNextLineAndShowTextWithSpacing implements MyContentOperator{
private final SetTextWordSpacing setTextWordSpacing;
private final SetTextCharacterSpacing setTextCharacterSpacing;
private final MoveNextLineAndShowText moveNextLineAndShowText;
public MoveNextLineAndShowTextWithSpacing(SetTextWordSpacing setTextWordSpacing, SetTextCharacterSpacing setTextCharacterSpacing, MoveNextLineAndShowText moveNextLineAndShowText) {
this.setTextWordSpacing = setTextWordSpacing;
this.setTextCharacterSpacing = setTextCharacterSpacing;
this.moveNextLineAndShowText = moveNextLineAndShowText;
}
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfNumber aw = (PdfNumber)operands.get(0);
PdfNumber ac = (PdfNumber)operands.get(1);
PdfString string = (PdfString)operands.get(2);
ArrayList<PdfObject> twOperands = new ArrayList<PdfObject>(1);
twOperands.add(0, aw);
setTextWordSpacing.invoke(processor, null, twOperands);
ArrayList<PdfObject> tcOperands = new ArrayList<PdfObject>(1);
tcOperands.add(0, ac);
setTextCharacterSpacing.invoke(processor, null, tcOperands);
ArrayList<PdfObject> tickOperands = new ArrayList<PdfObject>(1);
tickOperands.add(0, string);
moveNextLineAndShowText.invoke(processor, null, tickOperands);
}
}
/**
* A content operator implementation (').
*/
private static class MoveNextLineAndShowText implements MyContentOperator{
private final TextMoveNextLine textMoveNextLine;
private final ShowText showText;
public MoveNextLineAndShowText(TextMoveNextLine textMoveNextLine, ShowText showText) {
this.textMoveNextLine = textMoveNextLine;
this.showText = showText;
}
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
textMoveNextLine.invoke(processor, null, new ArrayList<PdfObject>(0));
showText.invoke(processor, null, operands);
}
}
/**
* A content operator implementation (Tj).
*/
private static class ShowText implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfString string = (PdfString)operands.get(0);
processor.displayPdfString(string);
}
}
/**
* A content operator implementation (T*).
*/
private static class TextMoveNextLine implements MyContentOperator{
private final TextMoveStartNextLine moveStartNextLine;
public TextMoveNextLine(TextMoveStartNextLine moveStartNextLine){
this.moveStartNextLine = moveStartNextLine;
}
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
ArrayList<PdfObject> tdoperands = new ArrayList<PdfObject>(2);
tdoperands.add(0, new PdfNumber(0));
tdoperands.add(1, new PdfNumber(-processor.gs().leading));
moveStartNextLine.invoke(processor, null, tdoperands);
}
}
/**
* A content operator implementation (Tm).
*/
private static class TextSetTextMatrix implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
float a = ((PdfNumber)operands.get(0)).floatValue();
float b = ((PdfNumber)operands.get(1)).floatValue();
float c = ((PdfNumber)operands.get(2)).floatValue();
float d = ((PdfNumber)operands.get(3)).floatValue();
float e = ((PdfNumber)operands.get(4)).floatValue();
float f = ((PdfNumber)operands.get(5)).floatValue();
processor.textLineMatrix = new Matrix(a, b, c, d, e, f);
processor.textMatrix = processor.textLineMatrix;
}
}
/**
* A content operator implementation (TD).
*/
private static class TextMoveStartNextLineWithLeading implements MyContentOperator{
private final TextMoveStartNextLine moveStartNextLine;
private final SetTextLeading setTextLeading;
public TextMoveStartNextLineWithLeading(TextMoveStartNextLine moveStartNextLine, SetTextLeading setTextLeading){
this.moveStartNextLine = moveStartNextLine;
this.setTextLeading = setTextLeading;
}
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
float ty = ((PdfNumber)operands.get(1)).floatValue();
ArrayList<PdfObject> tlOperands = new ArrayList<PdfObject>(1);
tlOperands.add(0, new PdfNumber(-ty));
setTextLeading.invoke(processor, null, tlOperands);
moveStartNextLine.invoke(processor, null, operands);
}
}
/**
* A content operator implementation (Td).
*/
private static class TextMoveStartNextLine implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
float tx = ((PdfNumber)operands.get(0)).floatValue();
float ty = ((PdfNumber)operands.get(1)).floatValue();
Matrix translationMatrix = new Matrix(tx, ty);
processor.textMatrix = translationMatrix.multiply(processor.textLineMatrix);
processor.textLineMatrix = processor.textMatrix;
}
}
/**
* A content operator implementation (Tf).
*/
private static class SetTextFont implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfName fontResourceName = (PdfName)operands.get(0);
float size = ((PdfNumber)operands.get(1)).floatValue();
PdfDictionary fontsDictionary = processor.resources.getAsDict(PdfName.FONT);
CMapAwareDocumentFont font;
PdfObject fontObject = fontsDictionary.get(fontResourceName);
if (fontObject instanceof PdfDictionary)
font = processor.getFont((PdfDictionary)fontObject);
else
font = processor.getFont((PRIndirectReference)fontObject);
processor.gs().font = font;
processor.gs().fontSize = size;
processor.setFontResourceName(fontResourceName);
}
}
/**
* A content operator implementation (Tr).
*/
private static class SetTextRenderMode implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfNumber render = (PdfNumber)operands.get(0);
processor.gs().renderMode = render.intValue();
}
}
/**
* A content operator implementation (Ts).
*/
private static class SetTextRise implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfNumber rise = (PdfNumber)operands.get(0);
processor.gs().rise = rise.floatValue();
}
}
/**
* A content operator implementation (TL).
*/
private static class SetTextLeading implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfNumber leading = (PdfNumber)operands.get(0);
processor.gs().leading = leading.floatValue();
}
}
/**
* A content operator implementation (Tz).
*/
private static class SetTextHorizontalScaling implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfNumber scale = (PdfNumber)operands.get(0);
processor.gs().horizontalScaling = scale.floatValue()/100f;
}
}
/**
* A content operator implementation (Tc).
*/
private static class SetTextCharacterSpacing implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfNumber charSpace = (PdfNumber)operands.get(0);
processor.gs().characterSpacing = charSpace.floatValue();
}
}
/**
* A content operator implementation (Tw).
*/
private static class SetTextWordSpacing implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfNumber wordSpace = (PdfNumber)operands.get(0);
processor.gs().wordSpacing = wordSpace.floatValue();
}
}
/**
* A content operator implementation (gs).
*/
private static class ProcessGraphicsStateResource implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfName dictionaryName = (PdfName)operands.get(0);
PdfDictionary extGState = processor.resources.getAsDict(PdfName.EXTGSTATE);
if (extGState == null)
throw new IllegalArgumentException(MessageLocalization.getComposedMessage("resources.do.not.contain.extgstate.entry.unable.to.process.operator.1", operator));
PdfDictionary gsDic = extGState.getAsDict(dictionaryName);
if (gsDic == null)
throw new IllegalArgumentException(MessageLocalization.getComposedMessage("1.is.an.unknown.graphics.state.dictionary", dictionaryName));
// at this point, all we care about is the FONT entry in the GS dictionary
PdfArray fontParameter = gsDic.getAsArray(PdfName.FONT);
if (fontParameter != null){
CMapAwareDocumentFont font = processor.getFont((PRIndirectReference)fontParameter.getPdfObject(0));
float size = fontParameter.getAsNumber(1).floatValue();
processor.gs().font = font;
processor.gs().fontSize = size;
}
}
}
/**
* A content operator implementation (q).
*/
private static class PushGraphicsState implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
GraphicsState gs = processor.gsStack.peek();
GraphicsState copy = new GraphicsState(gs);
processor.gsStack.push(copy);
}
}
/**
* A content operator implementation (cm).
*/
private static class ModifyCurrentTransformationMatrix implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
float a = ((PdfNumber)operands.get(0)).floatValue();
float b = ((PdfNumber)operands.get(1)).floatValue();
float c = ((PdfNumber)operands.get(2)).floatValue();
float d = ((PdfNumber)operands.get(3)).floatValue();
float e = ((PdfNumber)operands.get(4)).floatValue();
float f = ((PdfNumber)operands.get(5)).floatValue();
Matrix matrix = new Matrix(a, b, c, d, e, f);
GraphicsState gs = processor.gsStack.peek();
gs.ctm = matrix.multiply(gs.ctm);
}
}
/**
* Gets a color based on a list of operands.
*/
private static BaseColor getColor(PdfName colorSpace, List<PdfObject> operands) {
if (PdfName.DEVICEGRAY.equals(colorSpace)) {
return getColor(1, operands);
}
if (PdfName.DEVICERGB.equals(colorSpace)) {
return getColor(3, operands);
}
if (PdfName.DEVICECMYK.equals(colorSpace)) {
return getColor(4, operands);
}
return null;
}
/**
* Gets a color based on a list of operands.
*/
private static BaseColor getColor(int nOperands, List<PdfObject> operands) {
float[] c = new float[nOperands];
for (int i = 0; i < nOperands; i++) {
c[i] = ((PdfNumber)operands.get(i)).floatValue();
}
switch (nOperands) {
case 1:
return new GrayColor(c[0]);
case 3:
return new BaseColor(c[0], c[1], c[2]);
case 4:
return new CMYKColor(c[0], c[1], c[2], c[3]);
}
return null;
}
/**
* A content operator implementation (g).
*/
private static class SetGrayFill implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gs().fillColor = getColor(1, operands);
}
}
/**
* A content operator implementation (G).
*/
private static class SetGrayStroke implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gs().strokeColor = getColor(1, operands);
}
}
/**
* A content operator implementation (rg).
*/
private static class SetRGBFill implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gs().fillColor = getColor(3, operands);
}
}
/**
* A content operator implementation (RG).
*/
private static class SetRGBStroke implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gs().strokeColor = getColor(3, operands);
}
}
/**
* A content operator implementation (rg).
*/
private static class SetCMYKFill implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gs().fillColor = getColor(4, operands);
}
}
/**
* A content operator implementation (RG).
*/
private static class SetCMYKStroke implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gs().strokeColor = getColor(4, operands);
}
}
/**
* A content operator implementation (CS).
*/
private static class SetColorSpaceFill implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gs().colorSpaceFill = (PdfName)operands.get(0);
}
}
/**
* A content operator implementation (cs).
*/
private static class SetColorSpaceStroke implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gs().colorSpaceStroke = (PdfName)operands.get(0);
}
}
/**
* A content operator implementation (sc / scn).
*/
private static class SetColorFill implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gs().fillColor = getColor(processor.gs().colorSpaceFill, operands);
}
}
/**
* A content operator implementation (SC / SCN).
*/
private static class SetColorStroke implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gs().strokeColor = getColor(processor.gs().colorSpaceStroke, operands);
}
}
/**
* A content operator implementation (Q).
*/
private static class PopGraphicsState implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.gsStack.pop();
}
}
/**
* A content operator implementation (BT).
*/
private static class BeginText implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.textMatrix = new Matrix();
processor.textLineMatrix = processor.textMatrix;
processor.beginText();
}
}
/**
* A content operator implementation (ET).
*/
private static class EndText implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
processor.textMatrix = null;
processor.textLineMatrix = null;
processor.endText();
}
}
/**
* A content operator implementation (BMC).
* @since 5.0.2
*/
private static class BeginMarkedContent implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor,
PdfLiteral operator, ArrayList<PdfObject> operands)
throws Exception {
processor.beginMarkedContent((PdfName)operands.get(0), new PdfDictionary());
}
}
/**
* A content operator implementation (BDC).
* @since 5.0.2
*/
private static class BeginMarkedContentDictionary implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor,
PdfLiteral operator, ArrayList<PdfObject> operands)
throws Exception {
PdfObject properties = operands.get(1);
processor.beginMarkedContent((PdfName)operands.get(0), getPropertiesDictionary(properties, processor.resources));
}
private PdfDictionary getPropertiesDictionary(PdfObject operand1, ResourceDictionary resources){
if (operand1.isDictionary())
return (PdfDictionary)operand1;
PdfName dictionaryName = ((PdfName)operand1);
return resources.getAsDict(dictionaryName);
}
}
/**
* A content operator implementation (BMC).
* @since 5.0.2
*/
private static class EndMarkedContent implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor,
PdfLiteral operator, ArrayList<PdfObject> operands)
throws Exception {
processor.endMarkedContent();
}
}
/**
* A content operator implementation (Do).
*/
private static class Do implements MyContentOperator{
public void invoke(MyPdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) throws IOException {
PdfName xobjectName = (PdfName)operands.get(0);
processor.displayXObject(xobjectName);
}
}
/**
* An XObject subtype handler for FORM
*/
private static class FormXObjectDoHandler implements MyXObjectDoHandler{
public void handleXObject(MyPdfContentStreamProcessor processor, PdfStream stream, PdfIndirectReference ref) {
final PdfDictionary resources = stream.getAsDict(PdfName.RESOURCES);
// we read the content bytes up here so if it fails we don't leave the graphics state stack corrupted
// this is probably not necessary (if we fail on this, probably the entire content stream processing
// operation should be rejected