pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jahew...@apache.org
Subject svn commit: r1677712 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel: PDPageContentStream.java interactive/form/AppearanceGeneratorHelper.java interactive/form/AppearancePrimitivesComposer.java interactive/form/PlainTextFormatter.java
Date Tue, 05 May 2015 00:14:36 GMT
Author: jahewson
Date: Tue May  5 00:14:35 2015
New Revision: 1677712

URL: http://svn.apache.org/r1677712
Log:
PDFBOX-2333: Replace AppearancePrimitivesComposer with PDPageContentStream

Removed:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearancePrimitivesComposer.java
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PlainTextFormatter.java

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=1677712&r1=1677711&r2=1677712&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  5 00:14:35 2015
@@ -53,6 +53,7 @@ import org.apache.pdfbox.pdmodel.graphic
 import org.apache.pdfbox.pdmodel.graphics.image.PDInlineImage;
 import org.apache.pdfbox.pdmodel.graphics.shading.PDShading;
 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;
 
@@ -211,6 +212,24 @@ public final class PDPageContentStream i
     }
 
     /**
+     * Create a new appearance stream. Note that this is not actually a "page" content stream.
+     *
+     * @param doc The document the page is part of.
+     * @param appearance The appearance stream to write to.
+     * @throws IOException If there is an error writing to the page contents.
+     */
+    public PDPageContentStream(PDDocument doc, PDAppearanceStream appearance) throws IOException
+    {
+        this.document = doc;
+        
+        output = appearance.getPDStream().createOutputStream();
+        this.resources = appearance.getResources();
+        
+        formatDecimal.setMaximumFractionDigits(10);
+        formatDecimal.setGroupingUsed(false);
+    }
+
+    /**
      * Begin some text operations.
      *
      * @throws IOException If there is an error writing to the stream or if you attempt to

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=1677712&r1=1677711&r2=1677712&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
Tue May  5 00:14:35 2015
@@ -30,6 +30,7 @@ import org.apache.pdfbox.cos.COSNumber;
 import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.pdfparser.PDFStreamParser;
 import org.apache.pdfbox.pdfwriter.ContentStreamWriter;
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
 import org.apache.pdfbox.pdmodel.PDResources;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
@@ -225,7 +226,7 @@ class AppearanceGeneratorHelper
         writer.writeTokens(tokens);
         output.write("/Tx BMC\n".getBytes("ISO-8859-1"));
         PDRectangle boundingBox = resolveBoundingBox(widget, appearanceStream);
-        insertGeneratedAppearance(boundingBox, output, pdFont, tokens);
+        insertGeneratedAppearance(boundingBox, output, pdFont, tokens, appearanceStream);
         output.write("EMC".getBytes("ISO-8859-1"));
         output.close();
         writeToStream(output.toByteArray(), appearanceStream);
@@ -249,7 +250,7 @@ class AppearanceGeneratorHelper
 
         output.write("\n".getBytes("ISO-8859-1"));
         
-        insertGeneratedAppearance(boundingBox, output, pdFont, tokens);
+        insertGeneratedAppearance(boundingBox, output, pdFont, tokens, appearanceStream);
 
         if (emcIndex != -1)
         {
@@ -263,11 +264,11 @@ class AppearanceGeneratorHelper
      * Generate and insert text content and clipping around it.   
      */
     private void insertGeneratedAppearance(PDRectangle boundingBox, OutputStream output,
-            PDFont font, List<Object> tokens)
+            PDFont font, List<Object> tokens, PDAppearanceStream appearanceStream)
             throws IOException
     {
-        AppearancePrimitivesComposer composer = new AppearancePrimitivesComposer(output);
-        float fontSize = 0.0f;
+        PDPageContentStream contents = new PDPageContentStream(acroForm.getDocument(),
+                                                               appearanceStream);
 
         // Acrobat calculates the left and right padding dependent on the offset of the border
edge
         // This calculation works for forms having been generated by Acrobat.
@@ -277,14 +278,15 @@ class AppearanceGeneratorHelper
         PDRectangle contentEdge = applyPadding(paddingEdge,Math.max(1f, lineWidth));
         
         // add a clipping path to avoid overlapping with the border
-        composer.addRect(paddingEdge);
-        composer.clip();
+        contents.addRect(paddingEdge.getLowerLeftX(), paddingEdge.getLowerLeftY(),
+                         paddingEdge.getWidth(), paddingEdge.getHeight());
+        contents.clip();
         
         // start the text output
-        composer.beginText();
+        contents.beginText();
         
         // calculate the fontSize 
-        fontSize = calculateFontSize(font, contentEdge);
+        float fontSize = calculateFontSize(font, contentEdge);
         
         if (!defaultAppearanceHandler.getTokens().isEmpty())
         {
@@ -303,8 +305,9 @@ class AppearanceGeneratorHelper
         {
             // calculation of the horizontal offset from where the text will be printed
             leftOffset = calculateHorizontalOffset(contentEdge, font, fontSize);
-            composer.newLineAtOffset(leftOffset, verticalOffset);
-            composer.showText(value, font);
+            contents.newLineAtOffset(leftOffset, verticalOffset);
+            contents.setFont(font, fontSize);
+            contents.showText(value);
         }
         else
         {
@@ -314,11 +317,11 @@ class AppearanceGeneratorHelper
             appearanceStyle.setFont(font);
             appearanceStyle.setFontSize(fontSize);
             
-            //Adobe Acrobat uses the font's bounding box for the leading between the lines
+            // Adobe Acrobat uses the font's bounding box for the leading between the lines
             appearanceStyle.setLeading(font.getBoundingBox().getHeight() / GLYPH_TO_PDF_SCALE
* fontSize);
             
             PlainTextFormatter formatter = new PlainTextFormatter
-                                                .Builder(composer)
+                                                .Builder(contents)
                                                     .style(appearanceStyle)
                                                     .text(textContent)
                                                     .width(contentEdge.getWidth())
@@ -329,7 +332,8 @@ class AppearanceGeneratorHelper
             formatter.format();
 
         }
-        composer.endText();
+        contents.endText();
+        contents.close();
     }
 
     /*

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PlainTextFormatter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PlainTextFormatter.java?rev=1677712&r1=1677711&r2=1677712&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PlainTextFormatter.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PlainTextFormatter.java
Tue May  5 00:14:35 2015
@@ -19,6 +19,7 @@ package org.apache.pdfbox.pdmodel.intera
 import java.io.IOException;
 import java.util.List;
 
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
 import org.apache.pdfbox.pdmodel.font.PDFont;
 import org.apache.pdfbox.pdmodel.interactive.form.PlainText.Line;
 import org.apache.pdfbox.pdmodel.interactive.form.PlainText.Paragraph;
@@ -68,7 +69,7 @@ class PlainTextFormatter
     private final boolean wrapLines;
     private final float width;
     
-    private final AppearancePrimitivesComposer composer;
+    private final PDPageContentStream contents;
     private final PlainText textContent;
     private final TextAlign textAlignment;
     
@@ -79,7 +80,7 @@ class PlainTextFormatter
     {
 
         // required parameters
-        private AppearancePrimitivesComposer composer;
+        private PDPageContentStream contents;
 
         // optional parameters
         private AppearanceStyle appearanceStyle;
@@ -93,9 +94,9 @@ class PlainTextFormatter
         private float horizontalOffset = 0f;
         private float verticalOffset = 0f;
         
-        public Builder(AppearancePrimitivesComposer composer)
+        public Builder(PDPageContentStream contents)
         {
-            this.composer = composer;
+            this.contents = contents;
         }
 
         Builder style(AppearanceStyle appearanceStyle)
@@ -153,7 +154,7 @@ class PlainTextFormatter
         appearanceStyle = builder.appearanceStyle;
         wrapLines = builder.wrapLines;
         width = builder.width;
-        composer = builder.composer;
+        contents = builder.contents;
         textContent = builder.textContent;
         textAlignment = builder.textAlignment;
         horizontalOffset = builder.horizontalOffset;
@@ -169,6 +170,8 @@ class PlainTextFormatter
     {
         if (textContent != null && !textContent.getParagraphs().isEmpty())
         {
+            contents.setFont(appearanceStyle.getFont(), appearanceStyle.getFontSize());
+            
             for (Paragraph paragraph : textContent.getParagraphs())
             {
                 if (wrapLines)
@@ -182,7 +185,7 @@ class PlainTextFormatter
                 }
                 else
                 {
-                    composer.showText(paragraph.getText(), appearanceStyle.getFont());
+                    contents.showText(paragraph.getText());
                 }
             }
         }
@@ -230,7 +233,7 @@ class PlainTextFormatter
             
             if (lines.indexOf(line) == 0)
             {
-                composer.newLineAtOffset(offset, verticalOffset);
+                contents.newLineAtOffset(offset, verticalOffset);
                 // reset the initial verticalOffset
                 verticalOffset = 0f;
                 horizontalOffset = 0f;
@@ -239,18 +242,19 @@ class PlainTextFormatter
             {
                 // keep the last position
                 verticalOffset = verticalOffset - appearanceStyle.getLeading();
-                composer.newLineAtOffset(offset, -appearanceStyle.getLeading());
+                contents.newLineAtOffset(offset, -appearanceStyle.getLeading());
             }
             lastPos = startOffset; 
 
             List<Word> words = line.getWords();
+            contents.setFont(appearanceStyle.getFont(), appearanceStyle.getFontSize());
             for (Word word : words)
             {
-                composer.showText(word.getText(), font);
+                contents.showText(word.getText());
                 wordWidth = (Float) word.getAttributes().getIterator().getAttribute(TextAttribute.WIDTH);
                 if (words.indexOf(word) != words.size() -1)
                 {
-                    composer.newLineAtOffset(wordWidth + interWordSpacing, 0f);
+                    contents.newLineAtOffset(wordWidth + interWordSpacing, 0f);
                     lastPos = lastPos + wordWidth + interWordSpacing;
                 }
             }



Mime
View raw message