Author: tilman
Date: Sun Oct 17 18:08:38 2021
New Revision: 1894328

URL: http://svn.apache.org/viewvc?rev=1894328&view=rev
Log:
PDFBOX-4892: optimize, as suggested by valerybokov

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=1894328&r1=1894327&r2=1894328&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 Oct 17 18:08:38 2021
@@ -41,6 +41,7 @@ import org.apache.pdfbox.pdmodel.font.PD
 import org.apache.pdfbox.pdmodel.font.PDType3Font;
 import org.apache.pdfbox.pdmodel.font.PDVectorFont;
 import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
+import org.apache.pdfbox.pdmodel.interactive.action.PDAction;
 import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
 import 
org.apache.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
@@ -242,6 +243,8 @@ class AppearanceGeneratorHelper
                 appearanceDict.setNormalAppearance(appearanceStream);
                 // TODO support appearances other than "normal"
             }
+            PDAppearanceCharacteristicsDictionary appearanceCharacteristics =
+                    widget.getAppearanceCharacteristics();
                 
             /*
              * Adobe Acrobat always recreates the complete appearance stream 
if there is an appearance characteristics
@@ -249,9 +252,9 @@ class AppearanceGeneratorHelper
              * stream from the entries.
              * 
              */
-            if (widget.getAppearanceCharacteristics() != null || 
appearanceStream.getContentStream().getLength() == 0)
+            if (appearanceCharacteristics != null || 
appearanceStream.getContentStream().getLength() == 0)
             {
-                initializeAppearanceContent(widget, appearanceStream);
+                initializeAppearanceContent(widget, appearanceCharacteristics, 
appearanceStream);
             }
                 
             setAppearanceContent(widget, appearanceStream);
@@ -267,19 +270,20 @@ class AppearanceGeneratorHelper
         // format the field value for the appearance if there is scripting 
support and the field
         // has a format event
         PDFormFieldAdditionalActions actions = field.getActions();
-
-        if (actions != null && actions.getF() != null)
+        if (actions == null)
+        {
+            return apValue;
+        }
+        PDAction actionF = actions.getF();
+        if (actionF != null)
         {
             if (field.getAcroForm().getScriptingHandler() != null)
             {
                 ScriptingHandler scriptingHandler = 
field.getAcroForm().getScriptingHandler();
-                return scriptingHandler.format((PDActionJavaScript) 
field.getActions().getF(), apValue);
-            }
-            else
-            {
-                LOG.info("Field contains a formatting action but no 
ScriptingHandler has been supplied - formatted value might be incorrect");
-                return apValue;
+                return scriptingHandler.format((PDActionJavaScript) actionF, 
apValue);
             }
+            LOG.info("Field contains a formatting action but no 
ScriptingHandler " +
+                     "has been supplied - formatted value might be incorrect");
         }
         return apValue;
     }
@@ -351,17 +355,18 @@ class AppearanceGeneratorHelper
      * around the widget
      * 
      * @param widget the field widget
+     * @param appearanceCharacteristics the appearance characteristics 
dictionary from the widget or
+     * null
      * @param appearanceStream the appearance stream to be used
      * @throws IOException in case we can't write to the appearance stream
      */
-    private void initializeAppearanceContent(PDAnnotationWidget widget, 
PDAppearanceStream appearanceStream) throws IOException
+    private void initializeAppearanceContent(PDAnnotationWidget widget,
+            PDAppearanceCharacteristicsDictionary appearanceCharacteristics,
+            PDAppearanceStream appearanceStream) throws IOException
     {
         try (ByteArrayOutputStream output = new ByteArrayOutputStream();
-                PDAppearanceContentStream contents = new 
PDAppearanceContentStream(appearanceStream, output))
+             PDAppearanceContentStream contents = new 
PDAppearanceContentStream(appearanceStream, output))
         {
-
-            PDAppearanceCharacteristicsDictionary appearanceCharacteristics = 
widget.getAppearanceCharacteristics();
-            
             // TODO: support more entries like patterns, etc.
             if (appearanceCharacteristics != null)
             {
@@ -548,7 +553,7 @@ class AppearanceGeneratorHelper
             }
             else
             {
-                // Adobe shows the text 'shiftet up' in case the caps don't 
fit into the clipping area
+                // Adobe shows the text 'shifted up' in case the caps don't 
fit into the clipping area
                 if (fontCapAtSize > clipRect.getHeight())
                 {
                     y = clipRect.getLowerLeftY() + -fontDescentAtSize;


Reply via email to