You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2016/05/31 06:27:39 UTC

svn commit: r1746232 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: contentstream/PDAbstractContentStream.java pdmodel/PDPageContentStream.java

Author: msahyoun
Date: Tue May 31 06:27:39 2016
New Revision: 1746232

URL: http://svn.apache.org/viewvc?rev=1746232&view=rev
Log:
PDFBOX-3353: use shared methods for deprecated commands; share NumberFormat 

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java?rev=1746232&r1=1746231&r2=1746232&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/contentstream/PDAbstractContentStream.java Tue May 31 06:27:39 2016
@@ -105,6 +105,8 @@ public abstract class PDAbstractContentS
      */
     public PDAbstractContentStream() throws IOException
     {
+        formatDecimal.setMaximumFractionDigits(4);
+        formatDecimal.setGroupingUsed(false);
     }
     
     /**
@@ -137,6 +139,22 @@ public abstract class PDAbstractContentS
         formatDecimal.setGroupingUsed(false);
     }
     
+    /**
+     * Sets the maximum number of digits allowed for fractional numbers.
+     * 
+     * @see NumberFormat#setMaximumFractionDigits(int)
+     * @param fractionDigitsNumber
+     */
+    protected void setMaximumFractionDigits(int fractionDigitsNumber)
+    {
+        formatDecimal.setMaximumFractionDigits(fractionDigitsNumber);
+    }
+    
+    public OutputStream getOutput()
+    {
+        return this.output;
+    }
+
     public void setOutput(OutputStream outputStream)
     {
         this.output = outputStream;
@@ -152,11 +170,6 @@ public abstract class PDAbstractContentS
         this.resources = resources;
     }
     
-    public OutputStream getOutput()
-    {
-        return this.output;
-    }
-    
     public Stack<PDColorSpace> getStrokingColorSpaceStack()
     {
         return strokingColorSpaceStack;
@@ -1339,6 +1352,15 @@ public abstract class PDAbstractContentS
     }
 
     /**
+     * Writes a double to the content stream.
+     */
+    protected void writeOperand(double data) throws IOException
+    {
+        write(formatDecimal.format(data));
+        output.write(' ');
+    }
+
+    /**
      * Writes a real real to the content stream.
      */
     protected void writeOperand(float real) throws IOException
@@ -1383,6 +1405,14 @@ public abstract class PDAbstractContentS
     }
 
     /**
+     * Writes a byte[] to the content stream.
+     */
+    protected void write(byte[] data) throws IOException
+    {
+        output.write(data);
+    }
+    
+    /**
      * Writes a string to the content stream as ASCII.
      */
     protected void writeLine() throws IOException

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java?rev=1746232&r1=1746231&r2=1746232&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java Tue May 31 06:27:39 2016
@@ -21,8 +21,6 @@ import java.awt.geom.PathIterator;
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.text.NumberFormat;
-import java.util.Locale;
 import java.util.Stack;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -44,7 +42,6 @@ import org.apache.pdfbox.pdmodel.graphic
 import org.apache.pdfbox.pdmodel.graphics.image.PDInlineImage;
 import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
-import org.apache.pdfbox.util.Charsets;
 import org.apache.pdfbox.util.Matrix;
 
 /**
@@ -93,9 +90,6 @@ public final class PDPageContentStream e
     private final Stack<PDColorSpace> nonStrokingColorSpaceStack = new Stack<PDColorSpace>();
     private final Stack<PDColorSpace> strokingColorSpaceStack = new Stack<PDColorSpace>();
 
-    // number format
-    private final NumberFormat formatDecimal = NumberFormat.getNumberInstance(Locale.US);
-
     /**
      * Create a new PDPage content stream.
      *
@@ -257,8 +251,7 @@ public final class PDPageContentStream e
         }
         setResources(resources);
         // configure NumberFormat
-        formatDecimal.setMaximumFractionDigits(10);
-        formatDecimal.setGroupingUsed(false);
+        setMaximumFractionDigits(10);
     }
 
     /**
@@ -288,9 +281,6 @@ public final class PDPageContentStream e
         this.document = doc;
         
         setResources(appearance.getResources());
-        
-        formatDecimal.setMaximumFractionDigits(4);
-        formatDecimal.setGroupingUsed(false);
     }
 
     /**
@@ -365,6 +355,7 @@ public final class PDPageContentStream e
             }
         }
 
+        
         COSWriter.writeString(font.encode(text), getOutput());
         write(" ");
 
@@ -1074,7 +1065,7 @@ public final class PDPageContentStream e
     @Deprecated
     public void appendRawCommands(String commands) throws IOException
     {
-        getOutput().write(commands.getBytes(Charsets.US_ASCII));
+        write(commands);
     }
 
     /**
@@ -1087,7 +1078,7 @@ public final class PDPageContentStream e
     @Deprecated
     public void appendRawCommands(byte[] commands) throws IOException
     {
-        getOutput().write(commands);
+        write(commands);
     }
 
     /**
@@ -1100,7 +1091,7 @@ public final class PDPageContentStream e
     @Deprecated
     public void appendRawCommands(int data) throws IOException
     {
-        getOutput().write(data);
+        writeOperand(data);
     }
 
     /**
@@ -1113,7 +1104,7 @@ public final class PDPageContentStream e
     @Deprecated
     public void appendRawCommands(double data) throws IOException
     {
-        getOutput().write(formatDecimal.format(data).getBytes(Charsets.US_ASCII));
+        writeOperand(data);
     }
 
     /**
@@ -1126,7 +1117,7 @@ public final class PDPageContentStream e
     @Deprecated
     public void appendRawCommands(float data) throws IOException
     {
-        getOutput().write(formatDecimal.format(data).getBytes(Charsets.US_ASCII));
+        writeOperand(data);
     }
 
     /**
@@ -1139,7 +1130,7 @@ public final class PDPageContentStream e
     @Deprecated
     public void appendCOSName(COSName name) throws IOException
     {
-        name.writePDF(getOutput());
+        writeOperand(name);
     }
     
     /**
@@ -1148,20 +1139,10 @@ public final class PDPageContentStream e
      * @param state The extended graphics state.
      * @throws IOException If the content stream could not be written.
      */
+    @Override
     public void setGraphicsStateParameters(PDExtendedGraphicsState state) throws IOException
     {
         writeOperand(getResources().add(state));
         writeOperator("gs");
     }
-
-    /**
-     * Close the content stream.  This must be called when you are done with this object.
-     *
-     * @throws IOException If the underlying stream has a problem being written to.
-     */
-    @Override
-    public void close() throws IOException
-    {
-        getOutput().close();
-    }
 }