From a52799eb61b4bfd89005665238c7a6796988b35f Mon Sep 17 00:00:00 2001 From: xxx <1138069338@qq.com> Date: Mon, 20 Apr 2026 02:23:49 +1000 Subject: [PATCH] introduce private method to remove clones --- .../pdmodel/PDAbstractContentStream.java | 119 +++++------------- 1 file changed, 31 insertions(+), 88 deletions(-) diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java index d57ab7699a4..3500f88f54f 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java @@ -417,10 +417,7 @@ public void drawImage(PDImageXObject image, float x, float y) throws IOException */ public void drawImage(PDImageXObject image, float x, float y, float width, float height) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: drawImage is not allowed within a text block."); - } + assertNotInTextMode("drawImage"); saveGraphicsState(); @@ -444,10 +441,7 @@ public void drawImage(PDImageXObject image, float x, float y, float width, float */ public void drawImage(PDImageXObject image, Matrix matrix) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: drawImage is not allowed within a text block."); - } + assertNotInTextMode("drawImage"); saveGraphicsState(); @@ -488,10 +482,7 @@ public void drawImage(PDInlineImage inlineImage, float x, float y) throws IOExce */ public void drawImage(PDInlineImage inlineImage, float x, float y, float width, float height) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: drawImage is not allowed within a text block."); - } + assertNotInTextMode("drawImage"); saveGraphicsState(); transform(new Matrix(width, 0, 0, height, x, y)); @@ -553,10 +544,7 @@ public void drawImage(PDInlineImage inlineImage, float x, float y, float width, */ public void drawForm(PDFormXObject form) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: drawForm is not allowed within a text block."); - } + assertNotInTextMode("drawForm"); writeOperand(resources.add(form)); writeOperator(OperatorName.DRAW_OBJECT); @@ -899,10 +887,7 @@ public void setNonStrokingColor(float g) throws IOException */ public void addRect(float x, float y, float width, float height) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: addRect is not allowed within a text block."); - } + assertNotInTextMode("addRect"); writeOperand(x); writeOperand(y); writeOperand(width); @@ -925,10 +910,7 @@ public void addRect(float x, float y, float width, float height) throws IOExcept */ public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: curveTo is not allowed within a text block."); - } + assertNotInTextMode("curveTo"); writeOperand(x1); writeOperand(y1); writeOperand(x2); @@ -951,10 +933,7 @@ public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) */ public void curveTo2(float x2, float y2, float x3, float y3) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: curveTo2 is not allowed within a text block."); - } + assertNotInTextMode("curveTo2"); writeOperand(x2); writeOperand(y2); writeOperand(x3); @@ -975,10 +954,7 @@ public void curveTo2(float x2, float y2, float x3, float y3) throws IOException */ public void curveTo1(float x1, float y1, float x3, float y3) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: curveTo1 is not allowed within a text block."); - } + assertNotInTextMode("curveTo1"); writeOperand(x1); writeOperand(y1); writeOperand(x3); @@ -996,10 +972,7 @@ public void curveTo1(float x1, float y1, float x3, float y3) throws IOException */ public void moveTo(float x, float y) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: moveTo is not allowed within a text block."); - } + assertNotInTextMode("moveTo"); writeOperand(x); writeOperand(y); writeOperator(OperatorName.MOVE_TO); @@ -1015,10 +988,7 @@ public void moveTo(float x, float y) throws IOException */ public void lineTo(float x, float y) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: lineTo is not allowed within a text block."); - } + assertNotInTextMode("lineTo"); writeOperand(x); writeOperand(y); writeOperator(OperatorName.LINE_TO); @@ -1032,10 +1002,7 @@ public void lineTo(float x, float y) throws IOException */ public void stroke() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: stroke is not allowed within a text block."); - } + assertNotInTextMode("stroke"); writeOperator(OperatorName.STROKE_PATH); } @@ -1047,10 +1014,7 @@ public void stroke() throws IOException */ public void closeAndStroke() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: closeAndStroke is not allowed within a text block."); - } + assertNotInTextMode("closeAndStroke"); writeOperator(OperatorName.CLOSE_AND_STROKE); } @@ -1062,10 +1026,7 @@ public void closeAndStroke() throws IOException */ public void fill() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: fill is not allowed within a text block."); - } + assertNotInTextMode("fill"); writeOperator(OperatorName.FILL_NON_ZERO); } @@ -1077,10 +1038,7 @@ public void fill() throws IOException */ public void fillEvenOdd() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: fillEvenOdd is not allowed within a text block."); - } + assertNotInTextMode("fillEvenOdd"); writeOperator(OperatorName.FILL_EVEN_ODD); } @@ -1094,10 +1052,7 @@ public void fillEvenOdd() throws IOException */ public void fillAndStroke() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: fillAndStroke is not allowed within a text block."); - } + assertNotInTextMode("fillAndStroke"); writeOperator(OperatorName.FILL_NON_ZERO_AND_STROKE); } @@ -1111,10 +1066,7 @@ public void fillAndStroke() throws IOException */ public void fillAndStrokeEvenOdd() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: fillAndStrokeEvenOdd is not allowed within a text block."); - } + assertNotInTextMode("fillAndStrokeEvenOdd"); writeOperator(OperatorName.FILL_EVEN_ODD_AND_STROKE); } @@ -1128,10 +1080,7 @@ public void fillAndStrokeEvenOdd() throws IOException */ public void closeAndFillAndStroke() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: closeAndFillAndStroke is not allowed within a text block."); - } + assertNotInTextMode("closeAndFillAndStroke"); writeOperator(OperatorName.CLOSE_FILL_NON_ZERO_AND_STROKE); } @@ -1145,10 +1094,7 @@ public void closeAndFillAndStroke() throws IOException */ public void closeAndFillAndStrokeEvenOdd() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: closeAndFillAndStrokeEvenOdd is not allowed within a text block."); - } + assertNotInTextMode("closeAndFillAndStrokeEvenOdd"); writeOperator(OperatorName.CLOSE_FILL_EVEN_ODD_AND_STROKE); } @@ -1161,10 +1107,7 @@ public void closeAndFillAndStrokeEvenOdd() throws IOException */ public void shadingFill(PDShading shading) throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: shadingFill is not allowed within a text block."); - } + assertNotInTextMode("shadingFill"); writeOperand(resources.add(shading)); writeOperator(OperatorName.SHADING_FILL); @@ -1178,10 +1121,7 @@ public void shadingFill(PDShading shading) throws IOException */ public void closePath() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: closePath is not allowed within a text block."); - } + assertNotInTextMode("closePath"); writeOperator(OperatorName.CLOSE_PATH); } @@ -1193,10 +1133,7 @@ public void closePath() throws IOException */ public void clip() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: clip is not allowed within a text block."); - } + assertNotInTextMode("clip"); writeOperator(OperatorName.CLIP_NON_ZERO); // end path without filling or stroking @@ -1211,10 +1148,7 @@ public void clip() throws IOException */ public void clipEvenOdd() throws IOException { - if (inTextMode) - { - throw new IllegalStateException("Error: clipEvenOdd is not allowed within a text block."); - } + assertNotInTextMode("clipEvenOdd"); writeOperator(OperatorName.CLIP_EVEN_ODD); // end path without filling or stroking @@ -1569,6 +1503,15 @@ private boolean isOutsideOneInterval(double val) return val < 0 || val > 1; } + private void assertNotInTextMode(String operation) + { + if (inTextMode) + { + throw new IllegalStateException( + "Error: " + operation + " is not allowed within a text block."); + } + } + protected void setStrokingColorSpaceStack(PDColorSpace colorSpace) { if (strokingColorSpaceStack.isEmpty())