pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1865254 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java
Date Thu, 15 Aug 2019 19:02:33 GMT
Author: tilman
Date: Thu Aug 15 19:02:33 2019
New Revision: 1865254

URL: http://svn.apache.org/viewvc?rev=1865254&view=rev
Log:
PDFBOX-3353: try to get font from default resources

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java?rev=1865254&r1=1865253&r2=1865254&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java
Thu Aug 15 19:02:33 2019
@@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.fontbox.util.Charsets;
 import org.apache.pdfbox.contentstream.operator.Operator;
+import org.apache.pdfbox.contentstream.operator.OperatorName;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSName;
@@ -30,6 +31,7 @@ import org.apache.pdfbox.cos.COSObject;
 import org.apache.pdfbox.pdfparser.PDFStreamParser;
 import org.apache.pdfbox.pdmodel.PDAppearanceContentStream;
 import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDResources;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.font.PDFont;
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
@@ -54,6 +56,9 @@ public class PDFreeTextAppearanceHandler
     private static final Pattern COLOR_PATTERN =
             Pattern.compile(".*color\\:\\s*\\#([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]).*");
 
+    private float fontSize = 10;
+    private COSName fontName = COSName.HELV;
+
     public PDFreeTextAppearanceHandler(PDAnnotation annotation)
     {
         super(annotation);
@@ -233,7 +238,21 @@ public class PDFreeTextAppearanceHandler
             float clipWidth = width - ab.width * 4;
             float clipHeight = rotation == 90 || rotation == 270 ? 
                                 borderBox.getWidth() - ab.width * 4 : borderBox.getHeight()
- ab.width * 4;
-            float fontSize = extractFontSize(annotation);
+            extractFontDetails(annotation);
+            if (document != null && document.getDocumentCatalog().getAcroForm() !=
null)
+            {
+                // Try to get font from AcroForm default resources
+                // Sample file: https://gitlab.freedesktop.org/poppler/poppler/issues/6
+                PDResources defaultResources = document.getDocumentCatalog().getAcroForm().getDefaultResources();
+                if (defaultResources != null)
+                {
+                    PDFont defaultResourcesFont = defaultResources.getFont(fontName);
+                    if (defaultResourcesFont != null)
+                    {
+                        font = defaultResourcesFont;
+                    }
+                }
+            }
 
             // value used by Adobe, no idea where it comes from, actual font bbox max y is
0.931
             // gathered by creating an annotation with width 0.
@@ -366,7 +385,9 @@ public class PDFreeTextAppearanceHandler
                 {
                     Operator op = (Operator) token;
                     String name = op.getName();
-                    if ("g".equals(name) || "rg".equals(name) || "k".equals(name))
+                    if (OperatorName.NON_STROKING_GRAY.equals(name) ||
+                        OperatorName.NON_STROKING_RGB.equals(name) ||
+                        OperatorName.NON_STROKING_CMYK.equals(name))
                     {
                         graphicOp = op;
                         colors = arguments;
@@ -382,13 +403,13 @@ public class PDFreeTextAppearanceHandler
             {
                 switch (graphicOp.getName())
                 {
-                    case "g":
+                    case OperatorName.NON_STROKING_GRAY:
                         strokingColor = new PDColor(colors, PDDeviceGray.INSTANCE);
                         break;
-                    case "rg":
+                    case OperatorName.NON_STROKING_RGB:
                         strokingColor = new PDColor(colors, PDDeviceRGB.INSTANCE);
                         break;
-                    case "k":
+                    case OperatorName.NON_STROKING_CMYK:
                         strokingColor = new PDColor(colors, PDDeviceCMYK.INSTANCE);
                         break;
                     default:
@@ -403,15 +424,19 @@ public class PDFreeTextAppearanceHandler
         return strokingColor;
     }
 
-    //TODO extractNonStrokingColor and extractFontSize
-    // might somehow be replaced with PDDefaultAppearanceString,
-    // which is quite similar.
-    private float extractFontSize(PDAnnotationFreeText annotation)
+    //TODO extractNonStrokingColor and extractFontDetails
+    // might somehow be replaced with PDDefaultAppearanceString, which is quite similar.
+    private void extractFontDetails(PDAnnotationFreeText annotation)
     {
         String defaultAppearance = annotation.getDefaultAppearance();
+        if (defaultAppearance == null && document != null &&
+            document.getDocumentCatalog().getAcroForm() != null)
+        {
+            defaultAppearance = document.getDocumentCatalog().getAcroForm().getDefaultAppearance();
+        }
         if (defaultAppearance == null)
         {
-            return 10;
+            return;
         }
 
         try
@@ -430,7 +455,7 @@ public class PDFreeTextAppearanceHandler
                 {
                     Operator op = (Operator) token;
                     String name = op.getName();
-                    if ("Tf".equals(name))
+                    if (OperatorName.SET_FONT_AND_SIZE.equals(name))
                     {
                         fontArguments = arguments;
                     }
@@ -443,18 +468,22 @@ public class PDFreeTextAppearanceHandler
             }
             if (fontArguments.size() >= 2)
             {
-                COSBase base = fontArguments.get(1);
+                COSBase base = fontArguments.get(0);
+                if (base instanceof COSName)
+                {
+                    fontName = (COSName) base;
+                }
+                base = fontArguments.get(1);
                 if (base instanceof COSNumber)
                 {
-                    return ((COSNumber) base).floatValue();
+                    fontSize = ((COSNumber) base).floatValue();
                 }
             }
         }
         catch (IOException ex)
         {
-            LOG.warn("Problem parsing /DA, will use default 10", ex);
+            LOG.warn("Problem parsing /DA, will use default 'Helv 10'", ex);
         }
-        return 10;
     }
 
     @Override



Mime
View raw message