You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2015/05/17 20:40:59 UTC

svn commit: r1679890 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: pdfwriter/ContentStreamWriter.java pdmodel/interactive/form/AppearanceGeneratorHelper.java

Author: jahewson
Date: Sun May 17 18:40:59 2015
New Revision: 1679890

URL: http://svn.apache.org/r1679890
Log:
PDFBOX-2333: Combine two methods with overlapping functionality

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/ContentStreamWriter.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/ContentStreamWriter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/ContentStreamWriter.java?rev=1679890&r1=1679889&r2=1679890&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/ContentStreamWriter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/ContentStreamWriter.java Sun May 17 18:40:59 2015
@@ -18,10 +18,9 @@ package org.apache.pdfbox.pdfwriter;
 
 import java.io.IOException;
 import java.io.OutputStream;
-
 import java.util.List;
 import java.util.Map;
-
+import org.apache.pdfbox.contentstream.operator.Operator;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSBoolean;
@@ -30,7 +29,6 @@ import org.apache.pdfbox.cos.COSFloat;
 import org.apache.pdfbox.cos.COSInteger;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSString;
-import org.apache.pdfbox.contentstream.operator.Operator;
 import org.apache.pdfbox.util.Charsets;
 
 /**
@@ -62,23 +60,54 @@ public class ContentStreamWriter
     }
 
     /**
+     * Writes a single operand token.
+     *
+     * @param base The operand to write to the stream.
+     * @throws IOException If there is an error writing to the stream.
+     */
+    public void writeToken(COSBase base) throws IOException
+    {
+        writeObject(base);
+    }
+
+    /**
+     *  Writes a single operator token.
+     *
+     * @param op The operator to write to the stream.
+     * @throws IOException If there is an error writing to the stream.
+     */
+    public void writeToken(Operator op) throws IOException
+    {
+        writeObject(op);
+    }
+
+    /**
+     * Writes a series of tokens followed by a new line.
+     * 
+     * @param tokens The tokens to write to the stream.
+     * @throws IOException If there is an error writing to the stream.
+     */
+    public void writeTokens(Object... tokens) throws IOException
+    {
+        for (Object token : tokens)
+        {
+            writeObject(token);
+        }
+        output.write("\n".getBytes(Charsets.US_ASCII));
+    }
+
+    /**
      * This will write out the list of tokens to the stream.
      *
      * @param tokens The tokens to write to the stream.
-     * @param start The start index into the list of tokens.
-     * @param end The end index into the list of tokens.
      * @throws IOException If there is an error writing to the stream.
      */
-    public void writeTokens( List tokens, int start, int end ) throws IOException
+    public void writeTokens( List tokens ) throws IOException
     {
-        for( int i=start; i<end; i++ )
+        for (Object token : tokens)
         {
-            Object o = tokens.get( i );
-            writeObject( o );
-            //write a space between each object.
-            output.write( 32 );
+            writeObject(token);
         }
-        output.flush();
     }
 
     private void writeObject( Object o ) throws IOException
@@ -86,22 +115,27 @@ public class ContentStreamWriter
         if( o instanceof COSString )
         {
             COSWriter.writeString((COSString)o, output);
+            output.write( SPACE );
         }
         else if( o instanceof COSFloat )
         {
             ((COSFloat)o).writePDF( output );
+            output.write( SPACE );
         }
         else if( o instanceof COSInteger )
         {
             ((COSInteger)o).writePDF( output );
+            output.write( SPACE );
         }
         else if( o instanceof COSBoolean )
         {
             ((COSBoolean)o).writePDF( output );
+            output.write( SPACE );
         }
         else if( o instanceof COSName )
         {
             ((COSName)o).writePDF( output );
+            output.write( SPACE );
         }
         else if( o instanceof COSArray )
         {
@@ -165,15 +199,4 @@ public class ContentStreamWriter
             throw new IOException( "Error:Unknown type in content stream:" + o );
         }
     }
-
-    /**
-     * This will write out the list of tokens to the stream.
-     *
-     * @param tokens The tokens to write to the stream.
-     * @throws IOException If there is an error writing to the stream.
-     */
-    public void writeTokens( List tokens ) throws IOException
-    {
-        writeTokens( tokens, 0, tokens.size() );
-    }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java?rev=1679890&r1=1679889&r2=1679890&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Sun May 17 18:40:59 2015
@@ -50,6 +50,7 @@ class AppearanceGeneratorHelper
     private static final Log LOG = LogFactory.getLog(AppearanceGeneratorHelper.class);
     private static final float GLYPH_TO_PDF_SCALE = 1000f;
     private static final Operator BMC = Operator.getOperator("BMC");
+    private static final Operator EMC = Operator.getOperator("EMC");
     
     private final PDVariableText field;
     private final DefaultAppearanceHandler defaultAppearanceHandler;
@@ -108,18 +109,9 @@ class AppearanceGeneratorHelper
                     appearanceDict.setNormalAppearance(appearanceStream);
                     // TODO support appearances other than "normal"
                 }
-
-                List<Object> tokens = tokenize(appearanceStream);
+                
                 PDFont font = getFontAndUpdateResources(appearanceStream);
-
-                if (!tokens.contains(BMC))
-                {
-                    createAppearanceContent(tokens, widget, font, appearanceStream);
-                }
-                else
-                {
-                    updateAppearanceContent(tokens, widget, font, appearanceStream);
-                }
+                setAppearanceContent(appearanceStream, widget, font);
             }
         }
     }
@@ -237,52 +229,46 @@ class AppearanceGeneratorHelper
         }
         return null;
     }
-    
-    /**
-     * Create new content. 
-     */
-    private void createAppearanceContent(List<Object> tokens, PDAnnotationWidget widget, 
-            PDFont pdFont, PDAppearanceStream appearanceStream)
-            throws IOException
-    {
-        ByteArrayOutputStream output = new ByteArrayOutputStream();
-        // BJL 9/25/2004 Must prepend existing stream
-        // because it might have operators to draw things like
-        // rectangles and such
-        ContentStreamWriter writer = new ContentStreamWriter(output);
-        writer.writeTokens(tokens);
-        output.write("/Tx BMC\n".getBytes("ISO-8859-1"));
-        PDRectangle boundingBox = resolveBoundingBox(widget, appearanceStream);
-        insertGeneratedAppearance(boundingBox, output, pdFont, tokens, appearanceStream);
-        output.write("EMC".getBytes("ISO-8859-1"));
-        output.close();
-        writeToStream(output.toByteArray(), appearanceStream);
-    }
 
     /**
-     * Update existing content.
+     * Constructs and sets new contents for given appearance stream.
      */
-    private void updateAppearanceContent(List<Object> tokens, PDAnnotationWidget widget, 
-            PDFont pdFont, PDAppearanceStream appearanceStream)
-            throws IOException
+    private void setAppearanceContent(PDAppearanceStream appearanceStream,
+                                      PDAnnotationWidget widget, PDFont font) throws IOException
     {
         ByteArrayOutputStream output = new ByteArrayOutputStream();
         ContentStreamWriter writer = new ContentStreamWriter(output);
-        PDRectangle boundingBox = resolveBoundingBox(widget, appearanceStream);
 
+        List<Object> tokens = tokenize(appearanceStream);
         int bmcIndex = tokens.indexOf(Operator.getOperator("BMC"));
-        int emcIndex = tokens.indexOf(Operator.getOperator("EMC"));
-
-        writer.writeTokens(tokens, 0, bmcIndex + 1);
-
-        output.write("\n".getBytes("ISO-8859-1"));
+        if (bmcIndex == -1)
+        {
+            // append to existing stream
+            writer.writeTokens(tokens);
+            writer.writeTokens(COSName.TX, BMC);
+        }
+        else
+        {
+            // prepend content before BMC
+            writer.writeTokens(tokens.subList(0, bmcIndex + 1));
+        }
         
-        insertGeneratedAppearance(boundingBox, output, pdFont, tokens, appearanceStream);
-
-        if (emcIndex != -1)
+        // insert field contents
+        PDRectangle boundingBox = resolveBoundingBox(widget, appearanceStream);
+        insertGeneratedAppearance(boundingBox, output, font, tokens, appearanceStream);
+        
+        int emcIndex = tokens.indexOf(Operator.getOperator("EMC"));
+        if (emcIndex == -1)
         {
-            writer.writeTokens(tokens, emcIndex, tokens.size());
+            // append EMC
+            writer.writeTokens(EMC);
         }
+        else
+        {
+            // append contents after EMC
+            writer.writeTokens(tokens.subList(emcIndex, tokens.size()));
+        }
+
         output.close();
         writeToStream(output.toByteArray(), appearanceStream);
     }