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 2015/03/13 19:29:29 UTC

svn commit: r1666536 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java

Author: msahyoun
Date: Fri Mar 13 18:29:29 2015
New Revision: 1666536

URL: http://svn.apache.org/r1666536
Log:
PDFBOX-1402 correct autosize for multiline text fields

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

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=1666536&r1=1666535&r2=1666536&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 Fri Mar 13 18:29:29 2015
@@ -280,9 +280,12 @@ class AppearanceGeneratorHelper
         
         // start the text output
         composer.beginText();
+        
+        // calculate the fontSize 
+        fontSize = calculateFontSize(font, contentEdge, tokens);
+        
         if (!defaultAppearanceHandler.getTokens().isEmpty())
         {
-            fontSize = calculateFontSize(font, boundingBox, tokens);
             defaultAppearanceHandler.setFontSize(fontSize);
             ContentStreamWriter daWriter = new ContentStreamWriter(output);
             daWriter.writeTokens(defaultAppearanceHandler.getTokens());
@@ -470,31 +473,31 @@ class AppearanceGeneratorHelper
      *
      * @throws IOException If there is an error getting the font height.
      */
-    private float calculateFontSize(PDFont pdFont, PDRectangle boundingBox, List<Object> tokens) throws IOException
+    private float calculateFontSize(PDFont pdFont, PDRectangle contentEdge, List<Object> tokens) throws IOException
     {
-        float fontSize = 0;
+        // default font size is 12 in Acrobat
+        float fontSize = 12f;
+        
         if (!defaultAppearanceHandler.getTokens().isEmpty())
         {
             fontSize = defaultAppearanceHandler.getFontSize();
         }
 
-        float widthBasedFontSize = Float.MAX_VALUE;
-
-        // TODO review the calculation as this seems to not reflect how Acrobat calculates the font size
-        if (parent instanceof PDTextField && ((PDTextField) parent).doNotScroll())
+        // if the font size is 0 the size depends on the content
+        if (fontSize == 0 && !isMultiLine())
         {
-            // if we don't scroll then we will shrink the font to fit into the text area.
             float widthAtFontSize1 = pdFont.getStringWidth(value) / GLYPH_TO_PDF_SCALE;
-            float availableWidth = getAvailableWidth(boundingBox, getLineWidth(tokens));
-            widthBasedFontSize = availableWidth / widthAtFontSize1;
+            float widthBasedFontSize = contentEdge.getWidth() / widthAtFontSize1;
+            float height = pdFont.getFontDescriptor().getFontBoundingBox().getHeight() / GLYPH_TO_PDF_SCALE;
+            fontSize = Math.min(contentEdge.getHeight() / height, widthBasedFontSize);
         }
+        
+        // restore to default size for multiline text
         if (fontSize == 0)
         {
-            float lineWidth = getLineWidth(tokens);
-            float height = pdFont.getFontDescriptor().getFontBoundingBox().getHeight() / GLYPH_TO_PDF_SCALE;
-            float availHeight = getAvailableHeight(boundingBox, lineWidth);
-            fontSize = Math.min(availHeight / height, widthBasedFontSize);
+            fontSize = 12f;
         }
+
         return fontSize;
     }
 
@@ -582,26 +585,6 @@ class AppearanceGeneratorHelper
     }
     
     /**
-     * calculates the available width of the box.
-     * 
-     * @return the calculated available width of the box
-     */
-    private float getAvailableWidth(PDRectangle boundingBox, float lineWidth)
-    {
-        return boundingBox.getWidth() - 2 * lineWidth;
-    }
-
-    /**
-     * calculates the available height of the box.
-     * 
-     * @return the calculated available height of the box
-     */
-    private float getAvailableHeight(PDRectangle boundingBox, float lineWidth)
-    {
-        return boundingBox.getHeight() - 2 * lineWidth;
-    }
-    
-    /**
      * Get the capHeight for a font.
      * @throws IOException in case the font information can not be retrieved.
      */