Skip to content

Commit

Permalink
Fix #450 : avoid creating new graphic state for stroke colors, if not…
Browse files Browse the repository at this point in the history
… needed
  • Loading branch information
douglas-six authored and asturio committed Nov 10, 2020
1 parent 0abe991 commit df4427b
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ void restore(final GraphicState restore) {
/** The list were we save/restore the layer depth */
protected List<Integer> layerDepth;

private int lastFillAlpha = 1;
private int lastStrokeAlpha = 1;

static {
abrev.put(PdfName.BITSPERCOMPONENT, "/BPC ");
abrev.put(PdfName.COLORSPACE, "/CS ");
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit df4427b

Please sign in to comment.