001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.svggen.font;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.RandomAccessFile;
024:
025: import org.apache.batik.svggen.font.table.CmapTable;
026: import org.apache.batik.svggen.font.table.GlyfTable;
027: import org.apache.batik.svggen.font.table.HeadTable;
028: import org.apache.batik.svggen.font.table.HheaTable;
029: import org.apache.batik.svggen.font.table.HmtxTable;
030: import org.apache.batik.svggen.font.table.LocaTable;
031: import org.apache.batik.svggen.font.table.MaxpTable;
032: import org.apache.batik.svggen.font.table.NameTable;
033: import org.apache.batik.svggen.font.table.Os2Table;
034: import org.apache.batik.svggen.font.table.PostTable;
035: import org.apache.batik.svggen.font.table.Table;
036: import org.apache.batik.svggen.font.table.TableDirectory;
037: import org.apache.batik.svggen.font.table.TableFactory;
038:
039: /**
040: * The TrueType font.
041: * @version $Id: Font.java 475477 2006-11-15 22:44:28Z cam $
042: * @author <a href="mailto:david@steadystate.co.uk">David Schweinsberg</a>
043: */
044: public class Font {
045:
046: private String path;
047: // private Interpreter interp = null;
048: // private Parser parser = null;
049: private TableDirectory tableDirectory = null;
050: private Table[] tables;
051: private Os2Table os2;
052: private CmapTable cmap;
053: private GlyfTable glyf;
054: private HeadTable head;
055: private HheaTable hhea;
056: private HmtxTable hmtx;
057: private LocaTable loca;
058: private MaxpTable maxp;
059: private NameTable name;
060: private PostTable post;
061:
062: /**
063: * Constructor
064: */
065: public Font() {
066: }
067:
068: public Table getTable(int tableType) {
069: for (int i = 0; i < tables.length; i++) {
070: if ((tables[i] != null)
071: && (tables[i].getType() == tableType)) {
072: return tables[i];
073: }
074: }
075: return null;
076: }
077:
078: public Os2Table getOS2Table() {
079: return os2;
080: }
081:
082: public CmapTable getCmapTable() {
083: return cmap;
084: }
085:
086: public HeadTable getHeadTable() {
087: return head;
088: }
089:
090: public HheaTable getHheaTable() {
091: return hhea;
092: }
093:
094: public HmtxTable getHmtxTable() {
095: return hmtx;
096: }
097:
098: public LocaTable getLocaTable() {
099: return loca;
100: }
101:
102: public MaxpTable getMaxpTable() {
103: return maxp;
104: }
105:
106: public NameTable getNameTable() {
107: return name;
108: }
109:
110: public PostTable getPostTable() {
111: return post;
112: }
113:
114: public int getAscent() {
115: return hhea.getAscender();
116: }
117:
118: public int getDescent() {
119: return hhea.getDescender();
120: }
121:
122: public int getNumGlyphs() {
123: return maxp.getNumGlyphs();
124: }
125:
126: public Glyph getGlyph(int i) {
127: return (glyf.getDescription(i) != null) ? new Glyph(glyf
128: .getDescription(i), hmtx.getLeftSideBearing(i), hmtx
129: .getAdvanceWidth(i)) : null;
130: }
131:
132: public String getPath() {
133: return path;
134: }
135:
136: public TableDirectory getTableDirectory() {
137: return tableDirectory;
138: }
139:
140: /**
141: * @param pathName Path to the TTF font file
142: */
143: protected void read(String pathName) {
144: path = pathName;
145: File f = new File(pathName);
146:
147: if (!f.exists()) {
148: // TODO: Throw TTException
149: return;
150: }
151:
152: try {
153: RandomAccessFile raf = new RandomAccessFile(f, "r");
154: tableDirectory = new TableDirectory(raf);
155: tables = new Table[tableDirectory.getNumTables()];
156:
157: // Load each of the tables
158: for (int i = 0; i < tableDirectory.getNumTables(); i++) {
159: tables[i] = TableFactory.create(tableDirectory
160: .getEntry(i), raf);
161: }
162: raf.close();
163:
164: // Get references to commonly used tables
165: os2 = (Os2Table) getTable(Table.OS_2);
166: cmap = (CmapTable) getTable(Table.cmap);
167: glyf = (GlyfTable) getTable(Table.glyf);
168: head = (HeadTable) getTable(Table.head);
169: hhea = (HheaTable) getTable(Table.hhea);
170: hmtx = (HmtxTable) getTable(Table.hmtx);
171: loca = (LocaTable) getTable(Table.loca);
172: maxp = (MaxpTable) getTable(Table.maxp);
173: name = (NameTable) getTable(Table.name);
174: post = (PostTable) getTable(Table.post);
175:
176: // Initialize the tables that require it
177: hmtx.init(hhea.getNumberOfHMetrics(), maxp.getNumGlyphs()
178: - hhea.getNumberOfHMetrics());
179: loca.init(maxp.getNumGlyphs(),
180: head.getIndexToLocFormat() == 0);
181: glyf.init(maxp.getNumGlyphs(), loca);
182: } catch (IOException 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 pathName) {
195: Font f = new Font();
196: f.read(pathName);
197: return f;
198: }
199: }
|