001: /*
002: * $Id: TrueTypeTable.java,v 1.2 2007/12/20 18:33:30 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package com.sun.pdfview.font.ttf;
023:
024: import java.nio.ByteBuffer;
025:
026: /**
027: * The base class for TrueType tables. Specific tables can extend this
028: * to add more functionality
029: */
030: public class TrueTypeTable {
031: /**
032: * Well known tables
033: */
034: public static final int CMAP_TABLE = 0x636d6170;
035: public static final int GLYF_TABLE = 0x676c7966;
036: public static final int HEAD_TABLE = 0x68656164;
037: public static final int HHEA_TABLE = 0x68686561;
038: public static final int HMTX_TABLE = 0x686d7478;
039: public static final int MAXP_TABLE = 0x6d617870;
040: public static final int NAME_TABLE = 0x6e616d65;
041: public static final int POST_TABLE = 0x706f7374;
042: public static final int LOCA_TABLE = 0x6c6f6361;
043:
044: /**
045: * This table's tag
046: */
047: private int tag;
048:
049: /**
050: * The data in this table, in ByteBuffer form
051: */
052: private ByteBuffer data;
053:
054: /**
055: * Creates a new instance of TrueTypeTable.
056: *
057: * This method is protected. Use the <code>getTable()</code> methods
058: * to get new instances.
059: *
060: * @param tag the tag for this table
061: */
062: protected TrueTypeTable(int tag) {
063: this .tag = tag;
064: }
065:
066: /**
067: * Get a new instance of an empty table by tag string
068: *
069: * @param ttf the font that contains this table
070: * @param tagString the tag for this table, as a 4 character string
071: * (e.g. head or cmap)
072: */
073: public static TrueTypeTable createTable(TrueTypeFont ttf,
074: String tagString) {
075: return createTable(ttf, tagString, null);
076: }
077:
078: /**
079: * Get a new instance of a table with provided data
080: *
081: * @param ttf the font that contains this table
082: * @param tagString the tag for this table, as a 4 character string
083: * (e.g. head or cmap)
084: * @param data the table data
085: */
086: public static TrueTypeTable createTable(TrueTypeFont ttf,
087: String tagString, ByteBuffer data) {
088: TrueTypeTable outTable = null;
089:
090: int tag = stringToTag(tagString);
091:
092: switch (tag) {
093: case CMAP_TABLE: // cmap table
094: outTable = new CmapTable();
095: break;
096: case GLYF_TABLE:
097: outTable = new GlyfTable(ttf);
098: break;
099: case HEAD_TABLE: // head table
100: outTable = new HeadTable();
101: break;
102: case HHEA_TABLE: // hhea table
103: outTable = new HheaTable();
104: break;
105: case HMTX_TABLE:
106: outTable = new HmtxTable(ttf);
107: break;
108: case LOCA_TABLE:
109: outTable = new LocaTable(ttf);
110: break;
111: case MAXP_TABLE: // maxp table
112: outTable = new MaxpTable();
113: break;
114: case NAME_TABLE: // name table
115: outTable = new NameTable();
116: break;
117: case POST_TABLE: // post table
118: outTable = new PostTable();
119: break;
120: default:
121: outTable = new TrueTypeTable(tag);
122: break;
123: }
124:
125: if (data != null) {
126: outTable.setData(data);
127: }
128:
129: return outTable;
130: }
131:
132: /**
133: * Get the table's tag
134: */
135: public int getTag() {
136: return tag;
137: }
138:
139: /**
140: * Get the data in the table
141: */
142: public ByteBuffer getData() {
143: return data;
144: }
145:
146: /**
147: * Set the data in the table
148: */
149: public void setData(ByteBuffer data) {
150: this .data = data;
151: }
152:
153: /**
154: * Get the size of the table, in bytes
155: */
156: public int getLength() {
157: return getData().remaining();
158: }
159:
160: /**
161: * Get the tag as a string
162: */
163: public static String tagToString(int tag) {
164: char[] c = new char[4];
165: c[0] = (char) (0xff & (tag >> 24));
166: c[1] = (char) (0xff & (tag >> 16));
167: c[2] = (char) (0xff & (tag >> 8));
168: c[3] = (char) (0xff & (tag));
169:
170: return new String(c);
171: }
172:
173: /**
174: * Turn a string into a tag
175: */
176: public static int stringToTag(String tag) {
177: char[] c = tag.toCharArray();
178:
179: if (c.length != 4) {
180: throw new IllegalArgumentException("Bad tag length: " + tag);
181: }
182:
183: return c[0] << 24 | c[1] << 16 | c[2] << 8 | c[3];
184: }
185:
186: /**
187: * Put into a nice string
188: */
189: public String toString() {
190: String out = " " + tagToString(getTag())
191: + " Table. Data is: ";
192: if (getData() == null) {
193: out += "not set";
194: } else {
195: out += "set";
196: }
197: return out;
198: }
199: }
|