KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > FontDetails


1 /*
2  * $Id: FontDetails.java 2366 2006-09-14 23:10:58Z xlv $
3  * $Name$
4  *
5  * Copyright 2001, 2002 by Paulo Soares.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.io.UnsupportedEncodingException JavaDoc;
54 import java.util.HashMap JavaDoc;
55
56 import com.lowagie.text.ExceptionConverter;
57 /** Each font in the document will have an instance of this class
58  * where the characters used will be represented.
59  *
60  * @author Paulo Soares (psoares@consiste.pt)
61  */

62 class FontDetails {
63     
64     /** The indirect reference to this font
65      */

66     PdfIndirectReference indirectReference;
67     /** The font name that appears in the document body stream
68      */

69     PdfName fontName;
70     /** The font
71      */

72     BaseFont baseFont;
73     /** The font if its an instance of <CODE>TrueTypeFontUnicode</CODE>
74      */

75     TrueTypeFontUnicode ttu;
76
77     CJKFont cjkFont;
78     /** The array used with single byte encodings
79      */

80     byte shortTag[];
81     /** The map used with double byte encodings. The key is Integer(glyph) and the
82      * value is int[]{glyph, width, Unicode code}
83      */

84     HashMap JavaDoc longTag;
85     
86     IntHashtable cjkTag;
87     /** The font type
88      */

89     int fontType;
90     /** <CODE>true</CODE> if the font is symbolic
91      */

92     boolean symbolic;
93     /** Indicates if all the glyphs and widths for that particular
94      * encoding should be included in the document.
95      */

96     protected boolean subset = true;
97     /** Each font used in a document has an instance of this class.
98      * This class stores the characters used in the document and other
99      * specifics unique to the current working document.
100      * @param fontName the font name
101      * @param indirectReference the indirect reference to the font
102      * @param baseFont the <CODE>BaseFont</CODE>
103      */

104     FontDetails(PdfName fontName, PdfIndirectReference indirectReference, BaseFont baseFont) {
105         this.fontName = fontName;
106         this.indirectReference = indirectReference;
107         this.baseFont = baseFont;
108         fontType = baseFont.getFontType();
109         switch (fontType) {
110             case BaseFont.FONT_TYPE_T1:
111             case BaseFont.FONT_TYPE_TT:
112                 shortTag = new byte[256];
113                 break;
114             case BaseFont.FONT_TYPE_CJK:
115                 cjkTag = new IntHashtable();
116                 cjkFont = (CJKFont)baseFont;
117                 break;
118             case BaseFont.FONT_TYPE_TTUNI:
119                 longTag = new HashMap JavaDoc();
120                 ttu = (TrueTypeFontUnicode)baseFont;
121                 symbolic = baseFont.isFontSpecific();
122                 break;
123         }
124     }
125     
126     /** Gets the indirect reference to this font.
127      * @return the indirect reference to this font
128      */

129     PdfIndirectReference getIndirectReference() {
130         return indirectReference;
131     }
132     
133     /** Gets the font name as it appears in the document body.
134      * @return the font name
135      */

136     PdfName getFontName() {
137         return fontName;
138     }
139     
140     /** Gets the <CODE>BaseFont</CODE> of this font.
141      * @return the <CODE>BaseFont</CODE> of this font
142      */

143     BaseFont getBaseFont() {
144         return baseFont;
145     }
146     
147     /** Converts the text into bytes to be placed in the document.
148      * The conversion is done according to the font and the encoding and the characters
149      * used are stored.
150      * @param text the text to convert
151      * @return the conversion
152      */

153     byte[] convertToBytes(String JavaDoc text) {
154         byte b[] = null;
155         switch (fontType) {
156             case BaseFont.FONT_TYPE_T3:
157                 return baseFont.convertToBytes(text);
158             case BaseFont.FONT_TYPE_T1:
159             case BaseFont.FONT_TYPE_TT: {
160                 b = baseFont.convertToBytes(text);
161                 int len = b.length;
162                 for (int k = 0; k < len; ++k)
163                     shortTag[((int)b[k]) & 0xff] = 1;
164                 break;
165             }
166             case BaseFont.FONT_TYPE_CJK: {
167                 int len = text.length();
168                 for (int k = 0; k < len; ++k)
169                     cjkTag.put(cjkFont.getCidCode(text.charAt(k)), 0);
170                 b = baseFont.convertToBytes(text);
171                 break;
172             }
173             case BaseFont.FONT_TYPE_DOCUMENT: {
174                 b = baseFont.convertToBytes(text);
175                 break;
176             }
177             case BaseFont.FONT_TYPE_TTUNI: {
178                 try {
179                     int len = text.length();
180                     int metrics[] = null;
181                     char glyph[] = new char[len];
182                     int i = 0;
183                     if (symbolic) {
184                         b = PdfEncodings.convertToBytes(text, "symboltt");
185                         len = b.length;
186                         for (int k = 0; k < len; ++k) {
187                             metrics = ttu.getMetricsTT(b[k] & 0xff);
188                             if (metrics == null)
189                                 continue;
190                             longTag.put(new Integer JavaDoc(metrics[0]), new int[]{metrics[0], metrics[1], ttu.getUnicodeDifferences(b[k] & 0xff)});
191                             glyph[i++] = (char)metrics[0];
192                         }
193                     }
194                     else {
195                         for (int k = 0; k < len; ++k) {
196                             char c = text.charAt(k);
197                             metrics = ttu.getMetricsTT(c);
198                             if (metrics == null)
199                                 continue;
200                             int m0 = metrics[0];
201                             Integer JavaDoc gl = new Integer JavaDoc(m0);
202                             if (!longTag.containsKey(gl))
203                                 longTag.put(gl, new int[]{m0, metrics[1], c});
204                             glyph[i++] = (char)m0;
205                         }
206                     }
207                     String JavaDoc s = new String JavaDoc(glyph, 0, i);
208                     b = s.getBytes(CJKFont.CJK_ENCODING);
209                 }
210                 catch (UnsupportedEncodingException JavaDoc e) {
211                     throw new ExceptionConverter(e);
212                 }
213                 break;
214             }
215         }
216         return b;
217     }
218     
219     /** Writes the font definition to the document.
220      * @param writer the <CODE>PdfWriter</CODE> of this document
221      */

222     void writeFont(PdfWriter writer) {
223         try {
224             switch (fontType) {
225                 case BaseFont.FONT_TYPE_T3:
226                     baseFont.writeFont(writer, indirectReference, null);
227                     break;
228                 case BaseFont.FONT_TYPE_T1:
229                 case BaseFont.FONT_TYPE_TT: {
230                     int firstChar;
231                     int lastChar;
232                     for (firstChar = 0; firstChar < 256; ++firstChar) {
233                         if (shortTag[firstChar] != 0)
234                             break;
235                     }
236                     for (lastChar = 255; lastChar >= firstChar; --lastChar) {
237                         if (shortTag[lastChar] != 0)
238                             break;
239                     }
240                     if (firstChar > 255) {
241                         firstChar = 255;
242                         lastChar = 255;
243                     }
244                     baseFont.writeFont(writer, indirectReference, new Object JavaDoc[]{new Integer JavaDoc(firstChar), new Integer JavaDoc(lastChar), shortTag, new Boolean JavaDoc(subset)});
245                     break;
246                 }
247                 case BaseFont.FONT_TYPE_CJK:
248                     baseFont.writeFont(writer, indirectReference, new Object JavaDoc[]{cjkTag});
249                     break;
250                 case BaseFont.FONT_TYPE_TTUNI:
251                     baseFont.writeFont(writer, indirectReference, new Object JavaDoc[]{longTag, new Boolean JavaDoc(subset)});
252                     break;
253             }
254         }
255         catch(Exception JavaDoc e) {
256             throw new ExceptionConverter(e);
257         }
258     }
259     
260     /** Indicates if all the glyphs and widths for that particular
261      * encoding should be included in the document.
262      * @return <CODE>false</CODE> to include all the glyphs and widths.
263      */

264     public boolean isSubset() {
265         return subset;
266     }
267     
268     /** Indicates if all the glyphs and widths for that particular
269      * encoding should be included in the document. Set to <CODE>false</CODE>
270      * to include all.
271      * @param subset new value of property subset
272      */

273     public void setSubset(boolean subset) {
274         this.subset = subset;
275     }
276 }
277
Popular Tags