From a2f5e3b7d801a767a7d0ac3190069a8eca7b90d4 Mon Sep 17 00:00:00 2001 From: SIX Date: Tue, 10 Nov 2020 21:52:06 +0100 Subject: [PATCH] Fix #450 : avoid creating new graphic state for stroke colors, if not needed --- .../com/lowagie/text/pdf/PdfContentByte.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java b/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java index 4ee8088cb..4ecb4af89 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java @@ -219,6 +219,9 @@ void restore(final GraphicState restore) { /** The list were we save/restore the layer depth */ protected List layerDepth; + private int lastFillAlpha = 1; + private int lastStrokeAlpha = 1; + static { abrev.put(PdfName.BITSPERCOMPONENT, "/BPC "); abrev.put(PdfName.COLORSPACE, "/CS "); @@ -1393,8 +1396,7 @@ public void endText() { public void saveState() { content.append("q").append_i(separator); if (state != null) { - GraphicState gstate = new GraphicState(state); - stateList.add(gstate); + stateList.add(new GraphicState(state)); } } @@ -2302,10 +2304,12 @@ public void setColorStroke(Color color) { } private void saveColorStroke(ExtendedColor extendedColor) { - PdfGState gState = new PdfGState(); - gState.setStrokeOpacity(extendedColor.getAlpha() / MAX_INT_COLOR_VALUE); - setGState(gState); - + if (lastStrokeAlpha != extendedColor.getAlpha()) { + PdfGState gState = new PdfGState(); + gState.setStrokeOpacity(extendedColor.getAlpha() / MAX_INT_COLOR_VALUE); + setGState(gState); + lastStrokeAlpha = extendedColor.getAlpha(); + } if (state != null) state.colorStroke = extendedColor; } @@ -2347,13 +2351,12 @@ public void setColorFill(Color color) { } } - private int lastAlpha = 1; private void saveColorFill(ExtendedColor extendedColor) { - if (lastAlpha != extendedColor.getAlpha()) { + if (lastFillAlpha != extendedColor.getAlpha()) { PdfGState gState = new PdfGState(); gState.setFillOpacity(extendedColor.getAlpha() / MAX_INT_COLOR_VALUE); setGState(gState); - lastAlpha = extendedColor.getAlpha(); + lastFillAlpha = extendedColor.getAlpha(); } if (state != null) state.colorFill = extendedColor; }