KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > ttf > TTFParser


1 /**
2  * Copyright (c) 2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.ttf;
32
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39
40 /**
41  * A true type font file parser.
42  *
43  * @author Ben Litchfield (ben@csh.rit.edu)
44  * @version $Revision: 1.2 $
45  */

46 public class TTFParser
47 {
48     /**
49      * Parse a file and get a true type font.
50      * @param ttfFile The TTF file.
51      * @return A true type font.
52      * @throws IOException If there is an error parsing the true type font.
53      */

54     public TrueTypeFont parseTTF( String JavaDoc ttfFile ) throws IOException JavaDoc
55     {
56         RAFDataStream raf = new RAFDataStream( ttfFile, "r" );
57         return parseTTF( raf );
58     }
59     
60     /**
61      * Parse a file and get a true type font.
62      * @param ttfFile The TTF file.
63      * @return A true type font.
64      * @throws IOException If there is an error parsing the true type font.
65      */

66     public TrueTypeFont parseTTF( File JavaDoc ttfFile ) throws IOException JavaDoc
67     {
68         RAFDataStream raf = new RAFDataStream( ttfFile, "r" );
69         return parseTTF( raf );
70     }
71     
72     /**
73      * Parse a file and get a true type font.
74      * @param raf The TTF file.
75      * @return A true type font.
76      * @throws IOException If there is an error parsing the true type font.
77      */

78     public TrueTypeFont parseTTF( TTFDataStream raf ) throws IOException JavaDoc
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 JavaDoc initialized = new ArrayList JavaDoc();
92         //need to initialize a couple tables in a certain order
93
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 JavaDoc 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 JavaDoc
133     {
134         TTFTable retval = null;
135         String JavaDoc 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             //unknown table type but read it anyway.
191
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