You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2021/07/10 11:02:17 UTC

svn commit: r1891431 - in /pdfbox/branches/2.0/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java

Author: lehmi
Date: Sat Jul 10 11:02:16 2021
New Revision: 1891431

URL: http://svn.apache.org/viewvc?rev=1891431&view=rev
Log:
PDFBOX-5236: allow general graphics state operators within text mode

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java
    pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java?rev=1891431&r1=1891430&r2=1891431&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java Sat Jul 10 11:02:16 2021
@@ -1253,14 +1253,9 @@ abstract class PDAbstractContentStream i
      *
      * @param lineWidth The width which is used for drawing.
      * @throws IOException If the content stream could not be written
-     * @throws IllegalStateException If the method was called within a text block.
      */
     public void setLineWidth(float lineWidth) throws IOException
     {
-        if (inTextMode)
-        {
-            throw new IllegalStateException("Error: setLineWidth is not allowed within a text block.");
-        }
         writeOperand(lineWidth);
         writeOperator(OperatorName.SET_LINE_WIDTH);
     }
@@ -1270,15 +1265,10 @@ abstract class PDAbstractContentStream i
      *
      * @param lineJoinStyle 0 for miter join, 1 for round join, and 2 for bevel join.
      * @throws IOException If the content stream could not be written.
-     * @throws IllegalStateException If the method was called within a text block.
      * @throws IllegalArgumentException If the parameter is not a valid line join style.
      */
     public void setLineJoinStyle(int lineJoinStyle) throws IOException
     {
-        if (inTextMode)
-        {
-            throw new IllegalStateException("Error: setLineJoinStyle is not allowed within a text block.");
-        }
         if (lineJoinStyle >= 0 && lineJoinStyle <= 2)
         {
             writeOperand(lineJoinStyle);
@@ -1295,15 +1285,10 @@ abstract class PDAbstractContentStream i
      *
      * @param lineCapStyle 0 for butt cap, 1 for round cap, and 2 for projecting square cap.
      * @throws IOException If the content stream could not be written.
-     * @throws IllegalStateException If the method was called within a text block.
      * @throws IllegalArgumentException If the parameter is not a valid line cap style.
      */
     public void setLineCapStyle(int lineCapStyle) throws IOException
     {
-        if (inTextMode)
-        {
-            throw new IllegalStateException("Error: setLineCapStyle is not allowed within a text block.");
-        }
         if (lineCapStyle >= 0 && lineCapStyle <= 2)
         {
             writeOperand(lineCapStyle);
@@ -1321,14 +1306,9 @@ abstract class PDAbstractContentStream i
      * @param pattern The pattern array
      * @param phase The phase of the pattern
      * @throws IOException If the content stream could not be written.
-     * @throws IllegalStateException If the method was called within a text block.
      */
     public void setLineDashPattern(float[] pattern, float phase) throws IOException
     {
-        if (inTextMode)
-        {
-            throw new IllegalStateException("Error: setLineDashPattern is not allowed within a text block.");
-        }
         write("[");
         for (float value : pattern)
         {
@@ -1344,13 +1324,10 @@ abstract class PDAbstractContentStream i
      *
      * @param miterLimit the new miter limit.
      * @throws IOException If the content stream could not be written.
+     * @throws IllegalArgumentException If the parameter is <= 0.
      */
     public void setMiterLimit(float miterLimit) throws IOException
     {
-        if (inTextMode)
-        {
-            throw new IllegalStateException("Error: setMiterLimit is not allowed within a text block.");
-        }
         if (miterLimit <= 0.0)
         {
             throw new IllegalArgumentException("A miter limit <= 0 is invalid and will not render in Acrobat Reader");

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java?rev=1891431&r1=1891430&r2=1891431&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java Sat Jul 10 11:02:16 2021
@@ -2166,14 +2166,9 @@ public final class PDPageContentStream i
      *
      * @param lineWidth The width which is used for drawing.
      * @throws IOException If the content stream could not be written
-     * @throws IllegalStateException If the method was called within a text block.
      */
     public void setLineWidth(float lineWidth) throws IOException
     {
-        if (inTextMode)
-        {
-            throw new IllegalStateException("Error: setLineWidth is not allowed within a text block.");
-        }
         writeOperand(lineWidth);
         writeOperator(OperatorName.SET_LINE_WIDTH);
     }
@@ -2183,15 +2178,10 @@ public final class PDPageContentStream i
      *
      * @param lineJoinStyle 0 for miter join, 1 for round join, and 2 for bevel join.
      * @throws IOException If the content stream could not be written.
-     * @throws IllegalStateException If the method was called within a text block.
      * @throws IllegalArgumentException If the parameter is not a valid line join style.
      */
     public void setLineJoinStyle(int lineJoinStyle) throws IOException
     {
-        if (inTextMode)
-        {
-            throw new IllegalStateException("Error: setLineJoinStyle is not allowed within a text block.");
-        }
         if (lineJoinStyle >= 0 && lineJoinStyle <= 2)
         {
             writeOperand(lineJoinStyle);
@@ -2208,15 +2198,10 @@ public final class PDPageContentStream i
      *
      * @param lineCapStyle 0 for butt cap, 1 for round cap, and 2 for projecting square cap.
      * @throws IOException If the content stream could not be written.
-     * @throws IllegalStateException If the method was called within a text block.
      * @throws IllegalArgumentException If the parameter is not a valid line cap style.
      */
     public void setLineCapStyle(int lineCapStyle) throws IOException
     {
-        if (inTextMode)
-        {
-            throw new IllegalStateException("Error: setLineCapStyle is not allowed within a text block.");
-        }
         if (lineCapStyle >= 0 && lineCapStyle <= 2)
         {
             writeOperand(lineCapStyle);
@@ -2234,14 +2219,9 @@ public final class PDPageContentStream i
      * @param pattern The pattern array
      * @param phase The phase of the pattern
      * @throws IOException If the content stream could not be written.
-     * @throws IllegalStateException If the method was called within a text block.
      */
     public void setLineDashPattern(float[] pattern, float phase) throws IOException
     {
-        if (inTextMode)
-        {
-            throw new IllegalStateException("Error: setLineDashPattern is not allowed within a text block.");
-        }
         write("[");
         for (float value : pattern)
         {
@@ -2257,13 +2237,10 @@ public final class PDPageContentStream i
      *
      * @param miterLimit the new miter limit.
      * @throws IOException If the content stream could not be written.
+     * @throws IllegalArgumentException If the parameter is <= 0.
      */
     public void setMiterLimit(float miterLimit) throws IOException
     {
-        if (inTextMode)
-        {
-            throw new IllegalStateException("Error: setMiterLimit is not allowed within a text block.");
-        }
         if (miterLimit <= 0.0)
         {
             throw new IllegalArgumentException("A miter limit <= 0 is invalid and will not render in Acrobat Reader");

Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java?rev=1891431&r1=1891430&r2=1891431&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/TestPDPageContentStream.java Sat Jul 10 11:02:16 2021
@@ -25,6 +25,7 @@ import org.apache.pdfbox.contentstream.o
 import org.apache.pdfbox.cos.COSNumber;
 import org.apache.pdfbox.pdfparser.PDFStreamParser;
 import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
+import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
 
 /**
  * @author Yegor Kozlov
@@ -178,4 +179,34 @@ public class TestPDPageContentStream ext
         contentStream.close();
         doc.close();
     }
+
+    /**
+     * Check that general graphics state operators are allowed in text mode.
+     * 
+     * @throws IOException
+     */
+    public void testGeneralGraphicStateOperatorTextMode() throws IOException
+    {
+        PDDocument doc = new PDDocument();
+        PDPage page = new PDPage();
+        doc.addPage(page);
+        PDPageContentStream contentStream = new PDPageContentStream(doc, page);
+        contentStream.beginText();
+        // J
+        contentStream.setLineCapStyle(0);
+        // j
+        contentStream.setLineJoinStyle(0);
+        // w
+        contentStream.setLineWidth(10f);
+        // d
+        contentStream.setLineDashPattern(new float[] { 2, 1 }, 0f);
+        // M
+        contentStream.setMiterLimit(1.0f);
+        // gs
+        contentStream.setGraphicsStateParameters(new PDExtendedGraphicsState());
+        // ri, i are not supported with a specific setter
+        contentStream.endText();
+        contentStream.close();
+    }
+
 }