Skip to content

Commit dc82f37

Browse files
Remove redundant optimization logic for canvas pdf/a checks
1 parent ace2435 commit dc82f37

File tree

3 files changed

+20
-60
lines changed

3 files changed

+20
-60
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,10 +1071,10 @@ public void addOutputIntent(PdfOutputIntent outputIntent) {
10711071
public void checkIsoConformance(Object obj, IsoKey key) {
10721072
}
10731073

1074-
public void checkIsoConformance(Object obj, IsoKey key, PdfResources resources, int gStateIndex) {
1074+
public void checkIsoConformance(Object obj, IsoKey key, PdfResources resources) {
10751075
}
10761076

1077-
public void checkShowTextIsoConformance(Object gState, PdfResources resources, int gStateIndex) {
1077+
public void checkShowTextIsoConformance(Object gState, PdfResources resources) {
10781078
}
10791079

10801080
public void addFileAttachment(String description, byte[] fileStore, String fileDisplay, PdfName mimeType, PdfDictionary fileParameter, PdfName afRelationshipValue) {

kernel/src/main/java/com/itextpdf/kernel/pdf/canvas/PdfCanvas.java

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,6 @@ public class PdfCanvas {
156156
*/
157157
protected List<Integer> layerDepth;
158158

159-
/**
160-
* Used to identify if gState has changed since last pdf/a check.
161-
*/
162-
private int gStateIndex = 0;
163-
164159
/**
165160
* Creates PdfCanvas from content stream of page, form XObject, pattern etc.
166161
*
@@ -299,7 +294,6 @@ public PdfCanvas saveState() {
299294
* @return current canvas.
300295
*/
301296
public PdfCanvas restoreState() {
302-
++gStateIndex;
303297
document.checkIsoConformance('Q', IsoKey.CANVAS_STACK);
304298
currentGs = gsStack.pop();
305299
contentStream.getOutputStream().writeBytes(Q);
@@ -389,7 +383,6 @@ public PdfCanvas endVariableText() {
389383
* @return current canvas.
390384
*/
391385
public PdfCanvas setFontAndSize(PdfFont font, float size) {
392-
++gStateIndex;
393386
if (size < 0.0001f && size > -0.0001f)
394387
throw new PdfException(PdfException.FontSizeTooSmall, size);
395388
currentGs.setFontSize(size);
@@ -430,7 +423,6 @@ public PdfCanvas moveText(float x, float y) {
430423
* @return current canvas.
431424
*/
432425
public PdfCanvas setLeading(final float leading) {
433-
++gStateIndex;
434426
currentGs.setLeading(leading);
435427
contentStream.getOutputStream()
436428
.writeFloat(leading)
@@ -450,7 +442,6 @@ public PdfCanvas setLeading(final float leading) {
450442
* @return current canvas.
451443
*/
452444
public PdfCanvas moveTextWithLeading(final float x, final float y) {
453-
++gStateIndex;
454445
currentGs.setLeading(-y);
455446
contentStream.getOutputStream()
456447
.writeFloat(x)
@@ -479,7 +470,7 @@ public PdfCanvas newlineText() {
479470
* @return current canvas.
480471
*/
481472
public PdfCanvas newlineShowText(final String text) {
482-
document.checkShowTextIsoConformance(currentGs, resources, gStateIndex);
473+
document.checkShowTextIsoConformance(currentGs, resources);
483474
showTextInt(text);
484475
contentStream.getOutputStream()
485476
.writeByte((byte) '\'')
@@ -496,7 +487,7 @@ public PdfCanvas newlineShowText(final String text) {
496487
* @return current canvas.
497488
*/
498489
public PdfCanvas newlineShowText(final float wordSpacing, final float charSpacing, final String text) {
499-
document.checkShowTextIsoConformance(currentGs, resources, gStateIndex);
490+
document.checkShowTextIsoConformance(currentGs, resources);
500491
contentStream.getOutputStream()
501492
.writeFloat(wordSpacing)
502493
.writeSpace()
@@ -509,7 +500,6 @@ public PdfCanvas newlineShowText(final float wordSpacing, final float charSpacin
509500
// (cfr PDF reference v1.6, table 5.6)
510501
currentGs.setCharSpacing(charSpacing);
511502
currentGs.setWordSpacing(wordSpacing);
512-
++gStateIndex;
513503
return this;
514504
}
515505

@@ -520,7 +510,6 @@ public PdfCanvas newlineShowText(final float wordSpacing, final float charSpacin
520510
* @return current canvas.
521511
*/
522512
public PdfCanvas setTextRenderingMode(int textRenderingMode) {
523-
++gStateIndex;
524513
currentGs.setTextRenderingMode(textRenderingMode);
525514
contentStream.getOutputStream()
526515
.writeInteger(textRenderingMode).writeSpace()
@@ -537,7 +526,6 @@ public PdfCanvas setTextRenderingMode(int textRenderingMode) {
537526
* @return current canvas.
538527
*/
539528
public PdfCanvas setTextRise(float textRise) {
540-
++gStateIndex;
541529
currentGs.setTextRise(textRise);
542530
contentStream.getOutputStream()
543531
.writeFloat(textRise).writeSpace()
@@ -552,7 +540,6 @@ public PdfCanvas setTextRise(float textRise) {
552540
* @return current canvas.
553541
*/
554542
public PdfCanvas setWordSpacing(float wordSpacing) {
555-
++gStateIndex;
556543
currentGs.setWordSpacing(wordSpacing);
557544
contentStream.getOutputStream()
558545
.writeFloat(wordSpacing).writeSpace()
@@ -567,7 +554,6 @@ public PdfCanvas setWordSpacing(float wordSpacing) {
567554
* @return current canvas.
568555
*/
569556
public PdfCanvas setCharacterSpacing(float charSpacing) {
570-
++gStateIndex;
571557
currentGs.setCharSpacing(charSpacing);
572558
contentStream.getOutputStream()
573559
.writeFloat(charSpacing).writeSpace()
@@ -582,7 +568,6 @@ public PdfCanvas setCharacterSpacing(float charSpacing) {
582568
* @return current canvas.
583569
*/
584570
public PdfCanvas setHorizontalScaling(float scale) {
585-
++gStateIndex;
586571
currentGs.setHorizontalScaling(scale);
587572
contentStream.getOutputStream()
588573
.writeFloat(scale)
@@ -637,7 +622,7 @@ public PdfCanvas setTextMatrix(float x, float y) {
637622
* @return current canvas.
638623
*/
639624
public PdfCanvas showText(String text) {
640-
document.checkShowTextIsoConformance(currentGs, resources, gStateIndex);
625+
document.checkShowTextIsoConformance(currentGs, resources);
641626
showTextInt(text);
642627
contentStream.getOutputStream().writeBytes(Tj);
643628
return this;
@@ -650,7 +635,7 @@ public PdfCanvas showText(String text) {
650635
* @return current canvas.
651636
*/
652637
public PdfCanvas showText(GlyphLine text) {
653-
document.checkShowTextIsoConformance(currentGs, resources, gStateIndex);
638+
document.checkShowTextIsoConformance(currentGs, resources);
654639
PdfFont font;
655640
if ((font = currentGs.getFont()) == null) {
656641
throw new PdfException(PdfException.FontAndSizeMustBeSetBeforeWritingAnyText, currentGs);
@@ -758,7 +743,7 @@ private float getSubrangeWidth(GlyphLine text, int from, int to) {
758743
public PdfCanvas showText(PdfArray textArray) {
759744
if (currentGs.getFont() == null)
760745
throw new PdfException(PdfException.FontAndSizeMustBeSetBeforeWritingAnyText, currentGs);
761-
document.checkShowTextIsoConformance(currentGs, resources, gStateIndex);
746+
document.checkShowTextIsoConformance(currentGs, resources);
762747
contentStream.getOutputStream().writeBytes(PdfOutputStream.getIsoBytes("["));
763748
for (PdfObject obj : textArray) {
764749
if (obj.isString()) {
@@ -1117,7 +1102,7 @@ public PdfCanvas closePath() {
11171102
* @return current canvas.
11181103
*/
11191104
public PdfCanvas closePathEoFillStroke() {
1120-
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL_STROKE, resources, gStateIndex);
1105+
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL_STROKE, resources);
11211106
contentStream.getOutputStream().writeBytes(bStar);
11221107
return this;
11231108
}
@@ -1128,7 +1113,7 @@ public PdfCanvas closePathEoFillStroke() {
11281113
* @return current canvas.
11291114
*/
11301115
public PdfCanvas closePathFillStroke() {
1131-
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL_STROKE, resources, gStateIndex);
1116+
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL_STROKE, resources);
11321117
contentStream.getOutputStream().writeBytes(b);
11331118
return this;
11341119
}
@@ -1149,7 +1134,7 @@ public PdfCanvas newPath() {
11491134
* @return current canvas.
11501135
*/
11511136
public PdfCanvas stroke() {
1152-
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_STROKE, resources, gStateIndex);
1137+
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_STROKE, resources);
11531138
contentStream.getOutputStream().writeBytes(S);
11541139
return this;
11551140
}
@@ -1192,7 +1177,7 @@ public PdfCanvas closePathStroke() {
11921177
* @return current canvas.
11931178
*/
11941179
public PdfCanvas fill() {
1195-
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL, resources, gStateIndex);
1180+
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL, resources);
11961181
contentStream.getOutputStream().writeBytes(f);
11971182
return this;
11981183
}
@@ -1203,7 +1188,7 @@ public PdfCanvas fill() {
12031188
* @return current canvas.
12041189
*/
12051190
public PdfCanvas fillStroke() {
1206-
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL_STROKE, resources, gStateIndex);
1191+
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL_STROKE, resources);
12071192
contentStream.getOutputStream().writeBytes(B);
12081193
return this;
12091194
}
@@ -1214,7 +1199,7 @@ public PdfCanvas fillStroke() {
12141199
* @return current canvas.
12151200
*/
12161201
public PdfCanvas eoFill() {
1217-
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL, resources, gStateIndex);
1202+
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL, resources);
12181203
contentStream.getOutputStream().writeBytes(fStar);
12191204
return this;
12201205
}
@@ -1225,7 +1210,7 @@ public PdfCanvas eoFill() {
12251210
* @return current canvas.
12261211
*/
12271212
public PdfCanvas eoFillStroke() {
1228-
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL_STROKE, resources, gStateIndex);
1213+
document.checkIsoConformance(currentGs, IsoKey.DRAWMODE_FILL_STROKE, resources);
12291214
contentStream.getOutputStream().writeBytes(BStar);
12301215
return this;
12311216
}
@@ -1240,7 +1225,6 @@ public PdfCanvas setLineWidth(float lineWidth) {
12401225
if (currentGs.getLineWidth() == lineWidth) {
12411226
return this;
12421227
}
1243-
++gStateIndex;
12441228
currentGs.setLineWidth(lineWidth);
12451229
contentStream.getOutputStream()
12461230
.writeFloat(lineWidth).writeSpace()
@@ -1259,7 +1243,6 @@ public PdfCanvas setLineWidth(float lineWidth) {
12591243
public PdfCanvas setLineCapStyle(int lineCapStyle) {
12601244
if (currentGs.getLineCapStyle() == lineCapStyle)
12611245
return this;
1262-
++gStateIndex;
12631246
currentGs.setLineCapStyle(lineCapStyle);
12641247
contentStream.getOutputStream()
12651248
.writeInteger(lineCapStyle).writeSpace()
@@ -1278,7 +1261,6 @@ public PdfCanvas setLineCapStyle(int lineCapStyle) {
12781261
public PdfCanvas setLineJoinStyle(int lineJoinStyle) {
12791262
if (currentGs.getLineJoinStyle() == lineJoinStyle)
12801263
return this;
1281-
++gStateIndex;
12821264
currentGs.setLineJoinStyle(lineJoinStyle);
12831265
contentStream.getOutputStream()
12841266
.writeInteger(lineJoinStyle).writeSpace()
@@ -1296,7 +1278,6 @@ public PdfCanvas setLineJoinStyle(int lineJoinStyle) {
12961278
public PdfCanvas setMiterLimit(float miterLimit) {
12971279
if (currentGs.getMiterLimit() == miterLimit)
12981280
return this;
1299-
++gStateIndex;
13001281
currentGs.setMiterLimit(miterLimit);
13011282
contentStream.getOutputStream()
13021283
.writeFloat(miterLimit).writeSpace()
@@ -1316,7 +1297,6 @@ public PdfCanvas setMiterLimit(float miterLimit) {
13161297
* @return current canvas.
13171298
*/
13181299
public PdfCanvas setLineDash(final float phase) {
1319-
++gStateIndex;
13201300
currentGs.setDashPattern(getDashPatternArray(phase));
13211301
contentStream.getOutputStream().writeByte((byte) '[').writeByte((byte) ']').writeSpace()
13221302
.writeFloat(phase).writeSpace()
@@ -1337,7 +1317,6 @@ public PdfCanvas setLineDash(final float phase) {
13371317
* @return current canvas.
13381318
*/
13391319
public PdfCanvas setLineDash(final float unitsOn, final float phase) {
1340-
++gStateIndex;
13411320
currentGs.setDashPattern(getDashPatternArray(new float[]{unitsOn}, phase));
13421321
contentStream.getOutputStream().writeByte((byte) '[').writeFloat(unitsOn).writeByte((byte) ']').writeSpace()
13431322
.writeFloat(phase).writeSpace()
@@ -1360,7 +1339,6 @@ public PdfCanvas setLineDash(final float unitsOn, final float phase) {
13601339
* @return current canvas.
13611340
*/
13621341
public PdfCanvas setLineDash(final float unitsOn, final float unitsOff, final float phase) {
1363-
++gStateIndex;
13641342
currentGs.setDashPattern(getDashPatternArray(new float[]{unitsOn, unitsOff}, phase));
13651343
contentStream.getOutputStream().writeByte((byte) '[').writeFloat(unitsOn).writeSpace()
13661344
.writeFloat(unitsOff).writeByte((byte) ']').writeSpace()
@@ -1382,7 +1360,6 @@ public PdfCanvas setLineDash(final float unitsOn, final float unitsOff, final fl
13821360
* @return current canvas.
13831361
*/
13841362
public final PdfCanvas setLineDash(final float[] array, final float phase) {
1385-
++gStateIndex;
13861363
currentGs.setDashPattern(getDashPatternArray(array, phase));
13871364
PdfOutputStream out = contentStream.getOutputStream();
13881365
out.writeByte((byte) '[');
@@ -1406,7 +1383,6 @@ public PdfCanvas setRenderingIntent(PdfName renderingIntent) {
14061383
document.checkIsoConformance(renderingIntent, IsoKey.RENDERING_INTENT);
14071384
if (renderingIntent.equals(currentGs.getRenderingIntent()))
14081385
return this;
1409-
++gStateIndex;
14101386
currentGs.setRenderingIntent(renderingIntent);
14111387
contentStream.getOutputStream()
14121388
.write(renderingIntent).writeSpace()
@@ -1426,7 +1402,6 @@ public PdfCanvas setRenderingIntent(PdfName renderingIntent) {
14261402
public PdfCanvas setFlatnessTolerance(float flatnessTolerance) {
14271403
if (currentGs.getFlatnessTolerance() == flatnessTolerance)
14281404
return this;
1429-
++gStateIndex;
14301405
currentGs.setFlatnessTolerance(flatnessTolerance);
14311406
contentStream.getOutputStream()
14321407
.writeFloat(flatnessTolerance).writeSpace()
@@ -1753,7 +1728,7 @@ public PdfXObject addImage(Image image, float a, float b, float c, float d, floa
17531728
* @return created Image XObject or null in case of in-line image (asInline = true).
17541729
*/
17551730
public PdfXObject addImage(Image image, float a, float b, float c, float d, float e, float f, boolean asInline) {
1756-
document.checkIsoConformance(currentGs, IsoKey.GRAPHIC_STATE_ONLY, null, gStateIndex);
1731+
document.checkIsoConformance(currentGs, IsoKey.GRAPHIC_STATE_ONLY, null);
17571732
if (image.getOriginalType() == Image.WMF) {
17581733
WmfImageHelper wmf = new WmfImageHelper(image);
17591734
// TODO add matrix parameters
@@ -1968,7 +1943,6 @@ public PdfCanvas addXObject(PdfXObject xObject, float x, float y, float height,
19681943
* @return current canvas.
19691944
*/
19701945
public PdfCanvas setExtGState(PdfExtGState extGState) {
1971-
++gStateIndex;
19721946
if (!extGState.isFlushed())
19731947
currentGs.updateFromExtGState(extGState);
19741948
PdfName name = resources.addExtGState(extGState);
@@ -2126,7 +2100,7 @@ public PdfStream getContentStream() {
21262100
* @on error
21272101
*/
21282102
protected void addInlineImage(PdfImageXObject imageXObject, float a, float b, float c, float d, float e, float f) {
2129-
document.checkIsoConformance(imageXObject.getPdfObject(), IsoKey.INLINE_IMAGE, resources, gStateIndex);
2103+
document.checkIsoConformance(imageXObject.getPdfObject(), IsoKey.INLINE_IMAGE, resources);
21302104
saveState();
21312105
concatMatrix(a, b, c, d, e, f);
21322106
PdfOutputStream os = contentStream.getOutputStream();
@@ -2315,7 +2289,6 @@ private PdfCanvas addImage(PdfImageXObject image, float x, float y, float height
23152289
}
23162290

23172291
private void updateGStateColorFields(boolean fill, Color newColor) {
2318-
++gStateIndex;
23192292
if (fill) {
23202293
currentGs.setFillColor(newColor);
23212294
PdfObject colorSpaceObject = newColor.getColorSpace().getPdfObject();

0 commit comments

Comments
 (0)