You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by vf...@apache.org on 2010/03/24 11:15:19 UTC

svn commit: r927000 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java

Author: vfed
Date: Wed Mar 24 10:15:18 2010
New Revision: 927000

URL: http://svn.apache.org/viewvc?rev=927000&view=rev
Log:
PDFBOX-663: ensure that the FontDescriptor is not null for external TrueType fonts

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java?rev=927000&r1=926999&r2=927000&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java Wed Mar 24 10:15:18 2010
@@ -111,9 +111,10 @@ public class PDTrueTypeFont extends PDSi
      *
      * @param fontDictionary The font dictionary according to the PDF specification.
      */
-    public PDTrueTypeFont( COSDictionary fontDictionary )
+    public PDTrueTypeFont( COSDictionary fontDictionary ) throws IOException
     {
         super( fontDictionary );
+        ensureFontDescriptor();
     }
 
     /**
@@ -146,14 +147,50 @@ public class PDTrueTypeFont extends PDSi
         fontStream.addCompression();
         fd.setFontFile2( fontStream );
         retval.setFontDescriptor( fd );
+        InputStream ttfData = new FileInputStream(file);
+        try
+        {
+            loadDescriptorDictionary(retval, fd, ttfData);
+        }
+        finally
+        {
+            ttfData.close();
+        }
         //only support winansi encoding right now, should really
         //just use Identity-H with unicode mapping
         retval.setEncoding( new WinAnsiEncoding() );
+        return retval;
+    }
+
+    private void ensureFontDescriptor() throws IOException
+    {
+        PDFontDescriptorDictionary fd = (PDFontDescriptorDictionary)getFontDescriptor();
+        if( fd == null )
+        {
+            fd = new PDFontDescriptorDictionary();
+            setFontDescriptor(fd);
+            InputStream ttfData = getExternalTTFData();
+            if( ttfData != null )
+            {
+                try
+                {
+                    loadDescriptorDictionary(this, fd, ttfData);
+                }
+                finally
+                {
+                    ttfData.close();
+                }
+            }
+        }
+    }
+
+    private static void loadDescriptorDictionary(PDTrueTypeFont retval, PDFontDescriptorDictionary fd, InputStream ttfData) throws IOException
+    {
         TrueTypeFont ttf = null;
         try
         {
             TTFParser parser = new TTFParser();
-            ttf = parser.parseTTF( file );
+            ttf = parser.parseTTF( ttfData );
             NamingTable naming = ttf.getNaming();
             List records = naming.getNameRecords();
             for( int i=0; i<records.size(); i++ )
@@ -317,8 +354,6 @@ public class PDTrueTypeFont extends PDSi
                 ttf.close();
             }
         }
-
-        return retval;
     }
 
     /**
@@ -386,6 +421,16 @@ public class PDTrueTypeFont extends PDSi
         writeFont(g2d, at, awtFont, fontSize, x, y, string);
     }
 
+    private InputStream getExternalTTFData() throws IOException
+    {
+        String ttfResource = externalFonts.getProperty( UNKNOWN_FONT );
+        String baseFont = getBaseFont();
+        if( baseFont != null && externalFonts.containsKey(baseFont) )
+        {
+            ttfResource = externalFonts.getProperty( baseFont );
+        }
+        return (ttfResource != null ? ResourceLoader.loadResource(ttfResource) : null);
+    }
 
     /**
      * Permit to load an external TTF Font program file