1 31 package org.pdfbox.ttf; 32 33 import java.io.File ; 34 import java.io.IOException ; 35 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 40 46 public class TTFParser 47 { 48 54 public TrueTypeFont parseTTF( String ttfFile ) throws IOException 55 { 56 RAFDataStream raf = new RAFDataStream( ttfFile, "r" ); 57 return parseTTF( raf ); 58 } 59 60 66 public TrueTypeFont parseTTF( File ttfFile ) throws IOException 67 { 68 RAFDataStream raf = new RAFDataStream( ttfFile, "r" ); 69 return parseTTF( raf ); 70 } 71 72 78 public TrueTypeFont parseTTF( TTFDataStream raf ) throws IOException 79 { 80 TrueTypeFont font = new TrueTypeFont( raf ); 81 font.setVersion( raf.read32Fixed() ); 82 int numberOfTables = raf.readUnsignedShort(); 83 int searchRange = raf.readUnsignedShort(); 84 int entrySelector = raf.readUnsignedShort(); 85 int rangeShift = raf.readUnsignedShort(); 86 for( int i=0; i<numberOfTables; i++ ) 87 { 88 TTFTable table = readTableDirectory( raf ); 89 font.addTable( table ); 90 } 91 List initialized = new ArrayList (); 92 HeaderTable head = font.getHeader(); 94 raf.seek( head.getOffset() ); 95 head.initData( font, raf ); 96 initialized.add( head ); 97 98 99 HorizontalHeaderTable hh = font.getHorizontalHeader(); 100 raf.seek( hh.getOffset() ); 101 hh.initData( font, raf ); 102 initialized.add( hh ); 103 104 MaximumProfileTable maxp = font.getMaximumProfile(); 105 raf.seek( maxp.getOffset() ); 106 maxp.initData( font, raf ); 107 initialized.add( maxp ); 108 109 PostScriptTable post = font.getPostScript(); 110 raf.seek( post.getOffset() ); 111 post.initData( font, raf ); 112 initialized.add( post ); 113 114 IndexToLocationTable loc = font.getIndexToLocation(); 115 raf.seek( loc.getOffset() ); 116 loc.initData( font, raf ); 117 initialized.add( loc ); 118 119 Iterator iter = font.getTables().iterator(); 120 while( iter.hasNext() ) 121 { 122 TTFTable table = (TTFTable)iter.next(); 123 if( !initialized.contains( table ) ) 124 { 125 raf.seek( table.getOffset() ); 126 table.initData( font, raf ); 127 } 128 } 129 return font; 130 } 131 132 private TTFTable readTableDirectory( TTFDataStream raf ) throws IOException 133 { 134 TTFTable retval = null; 135 String tag = raf.readString( 4 ); 136 if( tag.equals( CMAPTable.TAG ) ) 137 { 138 retval = new CMAPTable(); 139 } 140 else if( tag.equals( GlyphTable.TAG ) ) 141 { 142 retval = new GlyphTable(); 143 } 144 else if( tag.equals( HeaderTable.TAG ) ) 145 { 146 retval = new HeaderTable(); 147 } 148 else if( tag.equals( HorizontalHeaderTable.TAG ) ) 149 { 150 retval = new HorizontalHeaderTable(); 151 } 152 else if( tag.equals( HorizontalMetricsTable.TAG ) ) 153 { 154 retval = new HorizontalMetricsTable(); 155 } 156 else if( tag.equals( IndexToLocationTable.TAG ) ) 157 { 158 retval = new IndexToLocationTable(); 159 } 160 else if( tag.equals( MaximumProfileTable.TAG ) ) 161 { 162 retval = new MaximumProfileTable(); 163 } 164 else if( tag.equals( NamingTable.TAG ) ) 165 { 166 retval = new NamingTable(); 167 } 168 else if( tag.equals( OS2WindowsMetricsTable.TAG ) ) 169 { 170 retval = new OS2WindowsMetricsTable(); 171 } 172 else if( tag.equals( PostScriptTable.TAG ) ) 173 { 174 retval = new PostScriptTable(); 175 } 176 else if( tag.equals( GlyphTable.TAG ) ) 177 { 178 retval = new GlyphTable(); 179 } 180 else if( tag.equals( GlyphTable.TAG ) ) 181 { 182 retval = new GlyphTable(); 183 } 184 else if( tag.equals( DigitalSignatureTable.TAG ) ) 185 { 186 retval = new DigitalSignatureTable(); 187 } 188 else 189 { 190 retval = new TTFTable(); 192 } 193 retval.setTag( tag ); 194 retval.setCheckSum( raf.readUnsignedInt() ); 195 retval.setOffset( raf.readUnsignedInt() ); 196 retval.setLength( raf.readUnsignedInt() ); 197 return retval; 198 } 199 } | Popular Tags |