Skip to content

Commit

Permalink
PDFBOX-663: ensure that the FontDescriptor is not null for external T…
Browse files Browse the repository at this point in the history
…rueType fonts

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@927000 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Villu Ruusmann committed Mar 24, 2010
1 parent e6802f9 commit 7ecdb2f
Showing 1 changed file with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ public PDTrueTypeFont()
*
* @param fontDictionary The font dictionary according to the PDF specification.
*/
public PDTrueTypeFont( COSDictionary fontDictionary )
public PDTrueTypeFont( COSDictionary fontDictionary ) throws IOException
{
super( fontDictionary );
ensureFontDescriptor();
}

/**
Expand Down Expand Up @@ -146,14 +147,50 @@ public static PDTrueTypeFont loadTTF( PDDocument doc, File file ) throws IOExcep
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++ )
Expand Down Expand Up @@ -317,8 +354,6 @@ else if( nr.getNameId() == NameRecord.NAME_FONT_FAMILY_NAME )
ttf.close();
}
}

return retval;
}

/**
Expand Down Expand Up @@ -386,6 +421,16 @@ public void drawString( String string, Graphics g, float fontSize,
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
Expand Down

0 comments on commit 7ecdb2f

Please sign in to comment.