You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2015/12/16 18:34:30 UTC

svn commit: r1720397 - in /pdfbox/trunk: fontbox/src/main/java/org/apache/fontbox/afm/AFMParser.java pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java

Author: lehmi
Date: Wed Dec 16 17:34:30 2015
New Revision: 1720397

URL: http://svn.apache.org/viewvc?rev=1720397&view=rev
Log:
PDFBOX-3131: reduce the amount of data when parsing AFM files as PDFBox uses char metrics only

Modified:
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/afm/AFMParser.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/afm/AFMParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/afm/AFMParser.java?rev=1720397&r1=1720396&r2=1720397&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/afm/AFMParser.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/afm/AFMParser.java Wed Dec 16 17:34:30 2015
@@ -16,8 +16,6 @@
  */
 package org.apache.fontbox.afm;
 
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.StringTokenizer;
@@ -290,35 +288,6 @@ public class AFMParser
     private final InputStream input;
 
     /**
-     * A method to test parsing of all AFM documents in the resources
-     * directory.
-     *
-     * @param args Ignored.
-     *
-     * @throws IOException If there is an error parsing one of the documents.
-     */
-    public static void main( String[] args ) throws IOException
-    {
-        java.io.File afmDir = new java.io.File( "Resources/afm" );
-        java.io.File[] files = afmDir.listFiles();
-        if (files != null)
-        {
-            for (File file : files)
-            {
-                if (file.getPath().toUpperCase().endsWith(".AFM"))
-                {
-                    long start = System.currentTimeMillis();
-                    FileInputStream input = new FileInputStream(file);
-                    AFMParser parser = new AFMParser(input);
-                    parser.parse();
-                    long stop = System.currentTimeMillis();
-                    System.out.println("Parsing:" + file.getPath() + " " + (stop - start));
-                }
-            }
-        }
-    }
-
-    /**
      * Constructor.
      *
      * @param in The input stream to read the AFM document from.
@@ -329,7 +298,7 @@ public class AFMParser
     }
 
     /**
-     * This will parse the AFM document.  This will close the Input stream
+     * This will parse the AFM document. The input stream is closed
      * when the parsing is finished.
      *
      * @return the parsed FontMetric
@@ -338,17 +307,30 @@ public class AFMParser
      */
     public FontMetrics parse() throws IOException
     {
-        return parseFontMetric();
+        return parseFontMetric(false);
     }
 
     /**
+     * This will parse the AFM document. The input stream is closed
+     * when the parsing is finished.
+     *
+     * @param reducedDataset parse a reduced subset of data if set to true
+     * @return the parsed FontMetric
+     * 
+     * @throws IOException If there is an IO error reading the document.
+     */
+    public FontMetrics parse(boolean reducedDataset) throws IOException
+    {
+        return parseFontMetric(reducedDataset);
+    }
+    /**
      * This will parse a font metrics item.
      *
      * @return The parse font metrics item.
      *
      * @throws IOException If there is an error reading the AFM file.
      */
-    private FontMetrics parseFontMetric() throws IOException
+    private FontMetrics parseFontMetric(boolean reducedDataset) throws IOException
     {
         FontMetrics fontMetrics = new FontMetrics();
         String startFontMetrics = readString();
@@ -359,6 +341,7 @@ public class AFMParser
         }
         fontMetrics.setAFMVersion( readFloat() );
         String nextCommand;
+        boolean charMetricsRead = false;
         while( !END_FONT_METRICS.equals( (nextCommand = readString() ) ) )
         {
             if( FONT_NAME.equals( nextCommand ) )
@@ -494,8 +477,9 @@ public class AFMParser
                     throw new IOException( "Error: Expected '" + END_CHAR_METRICS + "' actual '" +
                                                 end + "'" );
                 }
+                charMetricsRead = true;
             }
-            else if( START_COMPOSITES.equals( nextCommand ) )
+            else if( !reducedDataset && START_COMPOSITES.equals( nextCommand ) )
             {
                 int count = readInt();
                 for( int i=0; i<count; i++ )
@@ -510,12 +494,16 @@ public class AFMParser
                                                 end + "'" );
                 }
             }
-            else if( START_KERN_DATA.equals( nextCommand ) )
+            else if( !reducedDataset && START_KERN_DATA.equals( nextCommand ) )
             {
                 parseKernData( fontMetrics );
             }
             else
             {
+                if (reducedDataset && charMetricsRead)
+                {
+                    break;
+                }
                 throw new IOException( "Unknown AFM key '" + nextCommand + "'" );
             }
         }
@@ -983,7 +971,7 @@ public class AFMParser
     private String readLine() throws IOException
     {
         //First skip the whitespace
-        StringBuilder buf = new StringBuilder();
+        StringBuilder buf = new StringBuilder(60);
         int nextByte = input.read();
         while( isWhitespace( nextByte ) )
         {
@@ -1010,7 +998,7 @@ public class AFMParser
     private String readString() throws IOException
     {
         //First skip the whitespace
-        StringBuilder buf = new StringBuilder();
+        StringBuilder buf = new StringBuilder(24);
         int nextByte = input.read();
         while( isWhitespace( nextByte ) )
         {

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java?rev=1720397&r1=1720396&r2=1720397&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/Standard14Fonts.java Wed Dec 16 17:34:30 2015
@@ -116,7 +116,7 @@ final class Standard14Fonts
             try
             {
                 AFMParser parser = new AFMParser(afmStream);
-                FontMetrics metric = parser.parse();
+                FontMetrics metric = parser.parse(true);
                 STANDARD14_AFM_MAP.put(fontName, metric);
             }
             finally