KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > font > Font


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.svggen.font;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.RandomAccessFile JavaDoc;
23
24 import org.apache.batik.svggen.font.table.CmapTable;
25 import org.apache.batik.svggen.font.table.GlyfTable;
26 import org.apache.batik.svggen.font.table.HeadTable;
27 import org.apache.batik.svggen.font.table.HheaTable;
28 import org.apache.batik.svggen.font.table.HmtxTable;
29 import org.apache.batik.svggen.font.table.LocaTable;
30 import org.apache.batik.svggen.font.table.MaxpTable;
31 import org.apache.batik.svggen.font.table.NameTable;
32 import org.apache.batik.svggen.font.table.Os2Table;
33 import org.apache.batik.svggen.font.table.PostTable;
34 import org.apache.batik.svggen.font.table.Table;
35 import org.apache.batik.svggen.font.table.TableDirectory;
36 import org.apache.batik.svggen.font.table.TableFactory;
37
38 /**
39  * The TrueType font.
40  * @version $Id: Font.java,v 1.6 2004/08/18 07:15:18 vhardy Exp $
41  * @author <a HREF="mailto:david@steadystate.co.uk">David Schweinsberg</a>
42  */

43 public class Font {
44
45     private String JavaDoc path;
46 // private Interpreter interp = null;
47
// private Parser parser = null;
48
private TableDirectory tableDirectory = null;
49     private Table[] tables;
50     private Os2Table os2;
51     private CmapTable cmap;
52     private GlyfTable glyf;
53     private HeadTable head;
54     private HheaTable hhea;
55     private HmtxTable hmtx;
56     private LocaTable loca;
57     private MaxpTable maxp;
58     private NameTable name;
59     private PostTable post;
60
61     /**
62      * Constructor
63      */

64     public Font() {
65     }
66
67     public Table getTable(int tableType) {
68         for (int i = 0; i < tables.length; i++) {
69             if ((tables[i] != null) && (tables[i].getType() == tableType)) {
70                 return tables[i];
71             }
72         }
73         return null;
74     }
75
76     public Os2Table getOS2Table() {
77         return os2;
78     }
79     
80     public CmapTable getCmapTable() {
81         return cmap;
82     }
83     
84     public HeadTable getHeadTable() {
85         return head;
86     }
87     
88     public HheaTable getHheaTable() {
89         return hhea;
90     }
91     
92     public HmtxTable getHmtxTable() {
93         return hmtx;
94     }
95     
96     public LocaTable getLocaTable() {
97         return loca;
98     }
99     
100     public MaxpTable getMaxpTable() {
101         return maxp;
102     }
103
104     public NameTable getNameTable() {
105         return name;
106     }
107
108     public PostTable getPostTable() {
109         return post;
110     }
111
112     public int getAscent() {
113         return hhea.getAscender();
114     }
115
116     public int getDescent() {
117         return hhea.getDescender();
118     }
119
120     public int getNumGlyphs() {
121         return maxp.getNumGlyphs();
122     }
123
124     public Glyph getGlyph(int i) {
125         return (glyf.getDescription(i) != null)
126             ? new Glyph(
127                 glyf.getDescription(i),
128                 hmtx.getLeftSideBearing(i),
129                 hmtx.getAdvanceWidth(i))
130             : null;
131     }
132
133     public String JavaDoc getPath() {
134         return path;
135     }
136
137     public TableDirectory getTableDirectory() {
138         return tableDirectory;
139     }
140
141     /**
142      * @param pathName Path to the TTF font file
143      */

144     protected void read(String JavaDoc pathName) {
145         path = pathName;
146         File JavaDoc f = new File JavaDoc(pathName);
147
148         if (!f.exists()) {
149             // TODO: Throw TTException
150
return;
151         }
152
153         try {
154             RandomAccessFile JavaDoc raf = new RandomAccessFile JavaDoc(f, "r");
155             tableDirectory = new TableDirectory(raf);
156             tables = new Table[tableDirectory.getNumTables()];
157
158             // Load each of the tables
159
for (int i = 0; i < tableDirectory.getNumTables(); i++) {
160                 tables[i] = TableFactory.create
161                     (tableDirectory.getEntry(i), raf);
162             }
163             raf.close();
164
165             // Get references to commonly used tables
166
os2 = (Os2Table) getTable(Table.OS_2);
167             cmap = (CmapTable) getTable(Table.cmap);
168             glyf = (GlyfTable) getTable(Table.glyf);
169             head = (HeadTable) getTable(Table.head);
170             hhea = (HheaTable) getTable(Table.hhea);
171             hmtx = (HmtxTable) getTable(Table.hmtx);
172             loca = (LocaTable) getTable(Table.loca);
173             maxp = (MaxpTable) getTable(Table.maxp);
174             name = (NameTable) getTable(Table.name);
175             post = (PostTable) getTable(Table.post);
176
177             // Initialize the tables that require it
178
hmtx.init(hhea.getNumberOfHMetrics(),
179                       maxp.getNumGlyphs() - hhea.getNumberOfHMetrics());
180             loca.init(maxp.getNumGlyphs(), head.getIndexToLocFormat() == 0);
181             glyf.init(maxp.getNumGlyphs(), loca);
182         } catch (IOException JavaDoc e) {
183             e.printStackTrace();
184         }
185     }
186     
187     public static Font create() {
188         return new Font();
189     }
190     
191     /**
192      * @param pathName Path to the TTF font file
193      */

194     public static Font create(String JavaDoc pathName) {
195         Font f = new Font();
196         f.read(pathName);
197         return f;
198     }
199 }
200
Popular Tags