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 2020/12/31 12:32:41 UTC

svn commit: r1884985 - /pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateCheckBox.java

Author: tilman
Date: Thu Dec 31 12:32:41 2020
New Revision: 1884985

URL: http://svn.apache.org/viewvc?rev=1884985&view=rev
Log:
PDFBOX-5063: use AFM resource if the ZapfDingbats font isn't available; closes #103

Modified:
    pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateCheckBox.java

Modified: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateCheckBox.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateCheckBox.java?rev=1884985&r1=1884984&r2=1884985&view=diff
==============================================================================
--- pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateCheckBox.java (original)
+++ pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateCheckBox.java Thu Dec 31 12:32:41 2020
@@ -18,6 +18,10 @@ package org.apache.pdfbox.examples.inter
 
 import java.awt.geom.Rectangle2D;
 import java.io.IOException;
+import org.apache.fontbox.afm.AFMParser;
+import org.apache.fontbox.afm.CharMetric;
+import org.apache.fontbox.afm.FontMetrics;
+import org.apache.fontbox.util.BoundingBox;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.PDDocument;
@@ -157,6 +161,28 @@ public class CreateCheckBox
             String name = PDType1Font.ZAPF_DINGBATS.codeToName(normalCaption.codePointAt(0));
             String unicode = PDType1Font.ZAPF_DINGBATS.getGlyphList().toUnicode(name);
             Rectangle2D bounds = PDType1Font.ZAPF_DINGBATS.getPath(name).getBounds2D();
+            if (bounds.isEmpty())
+            {
+                // ZapfDingbats font missing, let's use AFM resources instead.
+                // You can remove this code block if you know that you have the font.
+                AFMParser parser = new AFMParser(PDType1Font.class.getResourceAsStream(
+                        "/org/apache/pdfbox/resources/afm/ZapfDingbats.afm"));
+                FontMetrics metric = parser.parse();
+                for (CharMetric cm : metric.getCharMetrics())
+                {
+                    if (normalCaption.codePointAt(0) == cm.getCharacterCode())
+                    {
+                        BoundingBox bb = cm.getBoundingBox();
+                        bounds = new Rectangle2D.Float(bb.getLowerLeftX(), bb.getLowerLeftY(), 
+                                                       bb.getWidth(), bb.getHeight());
+                        unicode = PDType1Font.ZAPF_DINGBATS.getGlyphList().toUnicode(cm.getName());
+                    }
+                }
+            }
+            if (bounds.isEmpty())
+            {
+                throw new IOException("Bounds rectangle for chosen glyph is empty");
+            }
             float size = (float) Math.min(bounds.getWidth(), bounds.getHeight()) / 1000;
             // assume that checkmark has square size
             // the calculations approximate what Adobe is doing, i.e. put the glyph in the middle