You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2015/11/08 14:45:43 UTC

svn commit: r1713228 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font: PDType3CharProc.java PDType3Font.java

Author: tilman
Date: Sun Nov  8 13:45:43 2015
New Revision: 1713228

URL: http://svn.apache.org/viewvc?rev=1713228&view=rev
Log:
PDFBOX-3076: getBoundingBox from charprocs when dictionary FontBBox is (0 0 0 0) if possible

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3CharProc.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3CharProc.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3CharProc.java?rev=1713228&r1=1713227&r2=1713228&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3CharProc.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3CharProc.java Sun Nov  8 13:45:43 2015
@@ -84,6 +84,55 @@ public final class PDType3CharProc imple
         return font.getFontBBox();
     }
 
+    /**
+     * Calculate the bounding box of this glyph. This will work only if the first operator in the
+     * stream is d1.
+     *
+     * @return the bounding box of this glyph, or null if the first operator is not d1.
+     * @throws IOException If an io error occurs while parsing the stream.
+     */
+    public PDRectangle getGlyphBBox() throws IOException
+    {
+        List<COSBase> arguments = new ArrayList<COSBase>();
+        PDFStreamParser parser = new PDFStreamParser(this);
+        Object token = parser.parseNextToken();
+        while (token != null)
+        {
+            if (token instanceof COSObject)
+            {
+                arguments.add(((COSObject) token).getObject());
+            }
+            else if (token instanceof Operator)
+            {
+                if (((Operator) token).getName().equals("d1") && arguments.size() == 6)
+                {
+                    for (int i = 0; i < 6; ++i)
+                    {
+                        if (!(arguments.get(i) instanceof COSNumber))
+                        {
+                            return null;
+                        }
+                    }
+                    return new PDRectangle(
+                            ((COSNumber) arguments.get(2)).floatValue(),
+                            ((COSNumber) arguments.get(3)).floatValue(),
+                            ((COSNumber) arguments.get(4)).floatValue() - ((COSNumber) arguments.get(2)).floatValue(),
+                            ((COSNumber) arguments.get(5)).floatValue() - ((COSNumber) arguments.get(3)).floatValue());
+                }
+                else
+                {
+                    return null;
+                }
+            }
+            else
+            {
+                arguments.add((COSBase) token);
+            }
+            token = parser.parseNextToken();
+        }
+        return null;
+    }
+
     @Override
     public Matrix getMatrix()
     {

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java?rev=1713228&r1=1713227&r2=1713228&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java Sun Nov  8 13:45:43 2015
@@ -22,6 +22,7 @@ import java.io.InputStream;
 import org.apache.fontbox.FontBoxFont;
 import org.apache.fontbox.util.BoundingBox;
 import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSStream;
@@ -43,6 +44,7 @@ public class PDType3Font extends PDSimpl
     private PDResources resources;
     private COSDictionary charProcs;
     private Matrix fontMatrix;
+    private BoundingBox fontBBox;
 
     /**
      * Constructor.
@@ -241,7 +243,7 @@ public class PDType3Font extends PDSimpl
     }
 
     /**
-     * This will get the fonts bounding box.
+     * This will get the fonts bounding box from its dictionary.
      *
      * @return The fonts bounding box.
      */
@@ -259,11 +261,42 @@ public class PDType3Font extends PDSimpl
     @Override
     public BoundingBox getBoundingBox()
     {
+        if (fontBBox != null)
+        {
+            return fontBBox;
+        }
         PDRectangle rect = getFontBBox();
-        return new BoundingBox(rect.getLowerLeftX(), rect.getLowerLeftY(),
-                               rect.getUpperRightX(), rect.getUpperRightY());
+        if (rect.getLowerLeftX() == 0 && rect.getLowerLeftY() == 0
+                && rect.getUpperRightX() == 0 && rect.getUpperRightY() == 0)
+        {
+            // Plan B: get the max bounding box of the glyphs
+            COSDictionary cp = getCharProcs();
+            for (COSName name : cp.keySet())
+            {
+                COSBase base = cp.getDictionaryObject(name);
+                if (base instanceof COSStream)
+                {
+                    PDType3CharProc charProc = new PDType3CharProc(this, (COSStream) base);
+                    try
+                    {
+                        PDRectangle glyphBBox = charProc.getGlyphBBox();
+                        rect.setLowerLeftX(Math.min(rect.getLowerLeftX(), glyphBBox.getLowerLeftX()));
+                        rect.setLowerLeftY(Math.min(rect.getLowerLeftY(), glyphBBox.getLowerLeftY()));
+                        rect.setUpperRightX(Math.max(rect.getUpperRightX(), glyphBBox.getUpperRightX()));
+                        rect.setUpperRightY(Math.max(rect.getUpperRightY(), glyphBBox.getUpperRightY()));
+                    }
+                    catch (IOException ex)
+                    {
+                        // ignore
+                    }
+                }
+            }
+        }
+        fontBBox = new BoundingBox(rect.getLowerLeftX(), rect.getLowerLeftY(),
+                rect.getUpperRightX(), rect.getUpperRightY());
+        return fontBBox;
     }
-    
+
     /**
      * Returns the dictionary containing all streams to be used to render the glyphs.
      *