001: /*
002: * $Id: TTFTest.java,v 1.2 2007/12/20 18:33:32 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 test;
023:
024: /*
025: * TTFTest.java
026: *
027: * Created on August 9, 2003, 5:57 PM
028: */
029:
030: import com.sun.pdfview.font.ttf.*;
031:
032: import java.util.*;
033: import java.io.*;
034: import java.awt.*;
035: import java.awt.event.*;
036: import javax.swing.*;
037: import java.awt.font.*;
038:
039: /**
040: *
041: * @author jon
042: */
043: public class TTFTest {
044: /**
045: * @param args the command line arguments
046: */
047: public static void main(String[] args) {
048: if (args.length < 1) {
049: System.out.println("Usage: ");
050: System.out
051: .println(" TTFTest <input file> <output file>");
052: System.exit(-1);
053: }
054:
055: try {
056: RandomAccessFile raf = new RandomAccessFile(args[0], "r");
057:
058: int size = (int) raf.length();
059: byte[] data = new byte[size];
060:
061: raf.readFully(data);
062:
063: TrueTypeFont ttf = TrueTypeFont.parseFont(data);
064:
065: MaxpTable maxp = (MaxpTable) ttf.getTable("maxp");
066: int nglyphs = maxp.getNumGlyphs();
067:
068: if (ttf.getTable("cmap") == null) {
069:
070: byte[] map = new byte[256];
071: for (int i = 0; i < map.length; i++) {
072: if (i < nglyphs) {
073: map[i] = (byte) (i);
074: } else {
075: map[i] = 0;
076: }
077: }
078:
079: CMapFormat0 newMap = (CMapFormat0) CMap.createMap(
080: (short) 0, (short) 0);
081: newMap.setMap(map);
082:
083: CMapFormat4 newMap4 = (CMapFormat4) CMap.createMap(
084: (short) 4, (short) 0);
085: newMap4.addSegment((short) 1, (short) (nglyphs + 0),
086: (short) 0);
087:
088: CmapTable cmap = (CmapTable) TrueTypeTable.createTable(
089: ttf, "cmap");
090: cmap.addCMap((short) 1, (short) 0, newMap);
091: cmap.addCMap((short) 3, (short) 1, newMap4);
092:
093: ttf.addTable("cmap", cmap);
094: }
095:
096: if (ttf.getTable("name") == null) {
097: NameTable name = (NameTable) TrueTypeTable.createTable(
098: ttf, "name");
099:
100: short platID = NameTable.PLATFORMID_MACINTOSH;
101: short encID = NameTable.ENCODINGID_MAC_ROMAN;
102: short langID = NameTable.LANGUAGEID_MAC_ENGLISH;
103:
104: /* name.addRecord(platID, encID, langID,
105: NameTable.NAMEID_COPYRIGHT,
106: "Copyright (c) 2000 Bigelow & Holmes Inc. Pat. Des 289,421.");
107: name.addRecord(platID, encID, langID,
108: NameTable.NAMEID_FAMILY,
109: "Lucida Bright");
110: name.addRecord(platID, encID, langID,
111: NameTable.NAMEID_SUBFAMILY,
112: "Regular");
113: name.addRecord(platID, encID, langID,
114: NameTable.NAMEID_SUBFAMILY_UNIQUE,
115: "Lucida Bright Regular: B&H:2000");
116: name.addRecord(platID, encID, langID,
117: NameTable.NAMEID_FULL_NAME,
118: "Lucida Bright Regular");
119: name.addRecord(platID, encID, langID,
120: NameTable.NAMEID_VERSION,
121: "January 28, 2000; 1.10 (JAVA)");
122: name.addRecord(platID, encID, langID,
123: NameTable.NAMEID_POSTSCRIPT_NAME,
124: "LucidaBright");
125: name.addRecord(platID, encID, langID,
126: NameTable.NAMEID_TRADEMARK,
127: "Lucida is a registered trademark of Bigelow & Holmes Inc.");
128: */
129:
130: platID = NameTable.PLATFORMID_MICROSOFT;
131: encID = 1;
132: langID = 1033;
133:
134: name
135: .addRecord(platID, encID, langID,
136: NameTable.NAMEID_COPYRIGHT,
137: "Copyright (c) 2000 Bigelow & Holmes Inc. Pat. Des 289,421.");
138: name.addRecord(platID, encID, langID,
139: NameTable.NAMEID_FAMILY, "Lucida Bright");
140: name.addRecord(platID, encID, langID,
141: NameTable.NAMEID_SUBFAMILY, "Regular");
142: name.addRecord(platID, encID, langID,
143: NameTable.NAMEID_SUBFAMILY_UNIQUE,
144: "Lucida Bright Regular: B&H:2000");
145: name.addRecord(platID, encID, langID,
146: NameTable.NAMEID_FULL_NAME,
147: "Lucida Bright Regular");
148: name.addRecord(platID, encID, langID,
149: NameTable.NAMEID_VERSION,
150: "January 28, 2000; 1.10 (JAVA)");
151: name.addRecord(platID, encID, langID,
152: NameTable.NAMEID_POSTSCRIPT_NAME,
153: "LucidaBright");
154: name
155: .addRecord(platID, encID, langID,
156: NameTable.NAMEID_TRADEMARK,
157: "Lucida is a registered trademark of Bigelow & Holmes Inc.");
158:
159: ttf.addTable("name", name);
160: }
161:
162: ttf.getTable("head");
163: ttf.getTable("hhea");
164:
165: System.out.println(ttf);
166:
167: if (args.length == 2) {
168: FileOutputStream fis = new FileOutputStream(args[1]);
169: fis.write(ttf.writeFont());
170: fis.close();
171: }
172:
173: InputStream fontStream = new ByteArrayInputStream(ttf
174: .writeFont());
175:
176: Font f = Font.createFont(Font.TRUETYPE_FONT, fontStream);
177:
178: System.out.println("Attributes of font " + f.getFontName()
179: + "(" + f.getNumGlyphs() + "):");
180: Map m = f.getAttributes();
181: for (Iterator i = m.keySet().iterator(); i.hasNext();) {
182: Object key = i.next();
183: Object value = m.get(key);
184:
185: System.out.println(key + " = " + value);
186: }
187: System.out.println();
188:
189: Font f2 = f.deriveFont((float) 18.0);
190:
191: JFrame jf = new JFrame();
192: jf.addWindowListener(new WindowAdapter() {
193: public void windowClosing(WindowEvent we) {
194: System.exit(1);
195: }
196: });
197:
198: JPanel thePanel = new JPanel();
199: thePanel.setLayout(new GridLayout(0, 32));
200:
201: for (int i = 0; i < nglyphs; i++) {
202: JLabel lbl = new JLabel(Integer.toHexString(i) + ": ");
203:
204: JLabel val = new JLabel(String.valueOf((char) i));
205: val.setFont(f2);
206:
207: thePanel.add(lbl);
208: thePanel.add(val);
209: }
210:
211: JScrollPane jsp = new JScrollPane(thePanel);
212:
213: jf.getContentPane().add(jsp);
214:
215: jf.pack();
216: jf.show();
217: } catch (Exception e) {
218: e.printStackTrace();
219: }
220:
221: }
222:
223: }
|