pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jahew...@apache.org
Subject svn commit: r1606956 - in /pdfbox/trunk: fontbox/src/main/java/org/apache/fontbox/util/FontManager.java pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java
Date Tue, 01 Jul 2014 00:40:14 GMT
Author: jahewson
Date: Tue Jul  1 00:40:14 2014
New Revision: 1606956

URL: http://svn.apache.org/r1606956
Log:
PDFBOX-2169: NPE in PDTrueTypeFont.makeFontDescriptor

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

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/util/FontManager.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/util/FontManager.java?rev=1606956&r1=1606955&r2=1606956&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/util/FontManager.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/util/FontManager.java Tue Jul  1
00:40:14 2014
@@ -50,6 +50,25 @@ public class FontManager
     // HashMap with all known true type fonts
     private static HashMap<String, String> fontMappingTTF = new HashMap<String,
String>();
 
+    // fallback font
+    private static TrueTypeFont standardFont;
+
+    static
+    {
+        try
+        {
+            standardFont = findTTFont("Arial");
+            if (standardFont == null)
+            {
+                throw new IOException("Could not load TTF fallback font");
+            }
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
     private FontManager()
     {
     }
@@ -318,23 +337,33 @@ public class FontManager
      * Search for a true type font for the given font name.
      * 
      * @param fontname the given font name
-     * @return the mapped true type font
+     * @return the mapped true type font, or null if none could be found
      * @throws IOException if something went wrong
      */
     public static TrueTypeFont findTTFont(String fontname) throws IOException
     {
-        String ttffontname = findTTFontname(fontname);
+        String ttfFontName = findTTFontname(fontname);
         TrueTypeFont ttfFont = null;
-        if (ttffontname != null)
+        if (ttfFontName != null)
         {
             TTFParser ttfParser = new TTFParser();
-            InputStream fontStream = ResourceLoader.loadResource(ttffontname);
+            InputStream fontStream = ResourceLoader.loadResource(ttfFontName);
             if (fontStream == null)
             {
-                throw new IOException("Can't load external font: " + ttffontname);
+                throw new IOException("Can't load external font: " + ttfFontName);
             }
             ttfFont = ttfParser.parseTTF(fontStream);
         }
         return ttfFont;
     }
+
+    /**
+     * Get the standard font from the environment.
+     *
+     * @return standard font
+     */
+    public static TrueTypeFont getStandardFont()
+    {
+        return standardFont;
+    }
 }

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=1606956&r1=1606955&r2=1606956&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 Tue
Jul  1 00:40:14 2014
@@ -289,6 +289,15 @@ public class PDTrueTypeFont extends PDFo
             }
         }
 
+        if (this.getFontEncoding() == null)
+        {
+            // todo: calling this.getFontEncoding() doesn't work if the font is loaded
+            //       from the local system, because it relies on the FontDescriptor!
+            //       We make do for now by returning an incomplete descriptor pending further
+            //       refactoring of PDFont#determineEncoding().
+            return fd;
+        }
+
         Map<Integer, String> codeToName = this.getFontEncoding().getCodeToNameMap();
 
         int firstChar = Collections.min(codeToName.keySet());
@@ -372,6 +381,10 @@ public class PDTrueTypeFont extends PDFo
                 // check if there is a font mapping for an external font file
                 ttf = FontManager.findTTFont(getBaseFont());
             }
+            if (ttf == null)
+            {
+                ttf = FontManager.getStandardFont();
+            }
         }
         return ttf;
     }



Mime
View raw message