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/01/15 21:48:18 UTC

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

Author: jukka
Date: Fri Jan 15 20:48:18 2010
New Revision: 899810

URL: http://svn.apache.org/viewvc?rev=899810&view=rev
Log:
PDFBOX-604: Various text extraction performance improvements

Memorize font type information to speed up PDFont.encode()

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

Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java?rev=899810&r1=899809&r2=899810&view=diff
==============================================================================
--- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java (original)
+++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java Fri Jan 15 20:48:18 2010
@@ -368,11 +368,7 @@
     public String encode( byte[] c, int offset, int length ) throws IOException
     {
         String retval = null;
-        COSName fontSubtype = (COSName)font.getDictionaryObject( COSName.SUBTYPE );
-        String fontSubtypeName = fontSubtype.getName();
-        if( fontSubtypeName.equals( "Type0" ) ||
-            fontSubtypeName.equals( "Type1" ) ||
-            fontSubtypeName.equals( "TrueType" ))
+        if( isTypeFont() )
         {
             if( cmap == null )
             {
@@ -392,8 +388,7 @@
                         COSStream encodingStream = (COSStream)encoding;
                         parseCmap( null, encodingStream.getUnfilteredStream(), null );
                     }
-                    else if( fontSubtypeName.equals( "Type0" ) &&
-                             encoding instanceof COSName )
+                    else if( isType0Font() && encoding instanceof COSName )
                     {
                         COSName encodingName = (COSName)encoding;
                         cmap = cmapObjects.get( encodingName );
@@ -423,8 +418,7 @@
                     {
                         COSDictionary fontDescriptor =
                             (COSDictionary)font.getDictionaryObject( COSName.FONT_DESC );
-                        if( fontSubtypeName.equals( "TrueType" ) &&
-                            fontDescriptor != null &&
+                        if( isTrueTypeFont() && fontDescriptor != null &&
                             (fontDescriptor.getDictionaryObject( COSName.FONT_FILE )!= null ||
                              fontDescriptor.getDictionaryObject( COSName.FONT_FILE2 ) != null ||
                              fontDescriptor.getDictionaryObject( COSName.FONT_FILE3 ) != null ) )
@@ -604,6 +598,12 @@
         return font.getNameAsString( COSName.TYPE );
     }
 
+    // Memorized values to avoid repeated dictionary lookups
+    private String subtype = null;
+    private boolean type0Font;
+    private boolean trueTypeFont;
+    private boolean typeFont;
+
     /**
      * This will get the subtype of font, Type1, Type3, ...
      *
@@ -611,7 +611,28 @@
      */
     public String getSubType()
     {
-        return font.getNameAsString( COSName.SUBTYPE );
+        if (subtype == null) {
+            subtype = font.getNameAsString( COSName.SUBTYPE );
+            type0Font = "Type0".equals(subtype);
+            trueTypeFont = "TrueType".equals(subtype);
+            typeFont = type0Font || "Type1".equals(subtype) || trueTypeFont;
+        }
+        return subtype;
+    }
+
+    private boolean isType0Font() {
+        getSubType();
+        return type0Font;
+    }
+
+    private boolean isTrueTypeFont() {
+        getSubType();
+        return trueTypeFont;
+    }
+
+    private boolean isTypeFont() {
+        getSubType();
+        return typeFont;
     }
 
     /**