You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ju...@apache.org on 2010/07/01 12:41:10 UTC

svn commit: r959587 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java

Author: jukka
Date: Thu Jul  1 10:41:10 2010
New Revision: 959587

URL: http://svn.apache.org/viewvc?rev=959587&view=rev
Log:
PDFBOX-765: Performance regression in PDFBox 1.2.0

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java?rev=959587&r1=959586&r2=959587&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java Thu Jul  1 10:41:10 2010
@@ -87,8 +87,51 @@ public abstract class PDFont implements 
     private static Map<COSName, CMap> cmapObjects =
         Collections.synchronizedMap( new HashMap<COSName, CMap>() );
 
-    private static Map<String, FontMetric> afmObjects =
-        Collections.synchronizedMap( new HashMap<String, FontMetric>() );
+    /**
+     * The static map of the default Adobe font metrics.
+     */
+    private static final Map<String, FontMetric> afmObjects =
+        Collections.unmodifiableMap( getAdobeFontMetrics() );
+
+    private static Map<String, FontMetric> getAdobeFontMetrics() {
+        Map<String, FontMetric> metrics = new HashMap<String, FontMetric>();
+        addAdobeFontMetric( metrics, "Courier-Bold" );
+        addAdobeFontMetric( metrics, "Courier-BoldOblique" );
+        addAdobeFontMetric( metrics, "Courier" );
+        addAdobeFontMetric( metrics, "Courier-Oblique" );
+        addAdobeFontMetric( metrics, "Helvetica" );
+        addAdobeFontMetric( metrics, "Helvetica-Bold" );
+        addAdobeFontMetric( metrics, "Helvetica-BoldOblique" );
+        addAdobeFontMetric( metrics, "Helvetica-Oblique" );
+        addAdobeFontMetric( metrics, "Symbol" );
+        addAdobeFontMetric( metrics, "Times-Bold" );
+        addAdobeFontMetric( metrics, "Times-BoldItalic" );
+        addAdobeFontMetric( metrics, "Times-Italic" );
+        addAdobeFontMetric( metrics, "Times-Roman" );
+        addAdobeFontMetric( metrics, "ZapfDingbats" );
+        return metrics;
+    }
+
+    private static void addAdobeFontMetric(
+            Map<String, FontMetric> metrics, String name ) {
+        try {
+            String resource =
+                "org/apache/pdfbox/resources/afm/" + name + ".afm";
+            InputStream afmStream = ResourceLoader.loadResource( resource );
+            if( afmStream != null )
+            {
+                try {
+                    AFMParser parser = new AFMParser( afmStream );
+                    parser.parse();
+                    metrics.put( name, parser.getResult() );
+                } finally {
+                    afmStream.close();
+                }
+            }
+        } catch (Exception e) {
+            // ignore
+        }
+    }
 
     /**
      * This will be set if the font has a toUnicode stream.
@@ -103,12 +146,11 @@ public abstract class PDFont implements 
      * SPECIAL NOTE: The font calculations are currently in COSObject, which
      * is where they will reside until PDFont is mature enough to take them over.
      * PDFont is the appropriate place for them and not in COSObject but we need font
-     * calculations for text extractaion.  THIS METHOD WILL BE MOVED OR REMOVED
+     * calculations for text extraction.  THIS METHOD WILL BE MOVED OR REMOVED
      * TO ANOTHER LOCATION IN A FUTURE VERSION OF PDFBOX.
      */
     public static void clearResources()
     {
-        afmObjects.clear();
         cmapObjects.clear();
     }
 
@@ -298,19 +340,6 @@ public abstract class PDFont implements 
             if( name != null )
             {
                 afm = afmObjects.get( name );
-                if( afm == null )
-                {
-                    String resource =
-                        "org/apache/pdfbox/resources/afm/" + name + ".afm";
-                    InputStream afmStream = ResourceLoader.loadResource( resource );
-                    if( afmStream != null )
-                    {
-                        AFMParser parser = new AFMParser( afmStream );
-                        parser.parse();
-                        afm = parser.getResult();
-                        afmObjects.put( name, afm );
-                    }
-                }
             }
         }
         return afm;