KickJava   Java API By Example, From Geeks To Geeks.

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


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

47 package com.lowagie.text.pdf;
48
49 import java.awt.Font JavaDoc;
50 import java.io.File JavaDoc;
51 import java.util.HashMap JavaDoc;
52
53 import com.lowagie.text.ExceptionConverter;
54 /** Default class to map awt fonts to BaseFont.
55  * @author Paulo Soares (psoares@consiste.pt)
56  */

57
58 public class DefaultFontMapper implements FontMapper {
59     
60     /** A representation of BaseFont parameters.
61      */

62     public static class BaseFontParameters {
63         /** The font name.
64          */

65         public String JavaDoc fontName;
66         /** The encoding for that font.
67          */

68         public String JavaDoc encoding;
69         /** The embedding for that font.
70          */

71         public boolean embedded;
72         /** Whether the font is cached of not.
73          */

74         public boolean cached;
75         /** The font bytes for ttf and afm.
76          */

77         public byte ttfAfm[];
78         /** The font bytes for pfb.
79          */

80         public byte pfb[];
81         
82         /** Constructs default BaseFont parameters.
83          * @param fontName the font name or location
84          */

85         public BaseFontParameters(String JavaDoc fontName) {
86             this.fontName = fontName;
87             encoding = BaseFont.CP1252;
88             embedded = BaseFont.EMBEDDED;
89             cached = BaseFont.CACHED;
90         }
91     }
92     
93     /** Maps aliases to names.
94      */

95     private HashMap JavaDoc aliases = new HashMap JavaDoc();
96     /** Maps names to BaseFont parameters.
97      */

98     private HashMap JavaDoc mapper = new HashMap JavaDoc();
99     /**
100      * Returns a BaseFont which can be used to represent the given AWT Font
101      *
102      * @param font the font to be converted
103      * @return a BaseFont which has similar properties to the provided Font
104      */

105     
106     public BaseFont awtToPdf(Font JavaDoc font) {
107         try {
108             BaseFontParameters p = getBaseFontParameters(font.getFontName());
109             if (p != null)
110                 return BaseFont.createFont(p.fontName, p.encoding, p.embedded, p.cached, p.ttfAfm, p.pfb);
111             String JavaDoc fontKey = null;
112             String JavaDoc logicalName = font.getName();
113
114             if (logicalName.equalsIgnoreCase("DialogInput") || logicalName.equalsIgnoreCase("Monospaced") || logicalName.equalsIgnoreCase("Courier")) {
115
116                 if (font.isItalic()) {
117                     if (font.isBold()) {
118                         fontKey = BaseFont.COURIER_BOLDOBLIQUE;
119
120                     } else {
121                         fontKey = BaseFont.COURIER_OBLIQUE;
122                     }
123
124                 } else {
125                     if (font.isBold()) {
126                         fontKey = BaseFont.COURIER_BOLD;
127
128                     } else {
129                         fontKey = BaseFont.COURIER;
130                     }
131                 }
132
133             } else if (logicalName.equalsIgnoreCase("Serif") || logicalName.equalsIgnoreCase("TimesRoman")) {
134
135                 if (font.isItalic()) {
136                     if (font.isBold()) {
137                         fontKey = BaseFont.TIMES_BOLDITALIC;
138
139                     } else {
140                         fontKey = BaseFont.TIMES_ITALIC;
141                     }
142
143                 } else {
144                     if (font.isBold()) {
145                         fontKey = BaseFont.TIMES_BOLD;
146
147                     } else {
148                         fontKey = BaseFont.TIMES_ROMAN;
149                     }
150                 }
151
152             } else { // default, this catches Dialog and SansSerif
153

154                 if (font.isItalic()) {
155                     if (font.isBold()) {
156                         fontKey = BaseFont.HELVETICA_BOLDOBLIQUE;
157
158                     } else {
159                         fontKey = BaseFont.HELVETICA_OBLIQUE;
160                     }
161
162                 } else {
163                     if (font.isBold()) {
164                         fontKey = BaseFont.HELVETICA_BOLD;
165                     } else {
166                         fontKey = BaseFont.HELVETICA;
167                     }
168                 }
169             }
170             return BaseFont.createFont(fontKey, BaseFont.CP1252, false);
171         }
172         catch (Exception JavaDoc e) {
173             throw new ExceptionConverter(e);
174         }
175     }
176     
177     /**
178      * Returns an AWT Font which can be used to represent the given BaseFont
179      *
180      * @param font the font to be converted
181      * @param size the desired point size of the resulting font
182      * @return a Font which has similar properties to the provided BaseFont
183      */

184     
185     public Font JavaDoc pdfToAwt(BaseFont font, int size) {
186         String JavaDoc names[][] = font.getFullFontName();
187         if (names.length == 1)
188             return new Font JavaDoc(names[0][3], 0, size);
189         String JavaDoc name10 = null;
190         String JavaDoc name3x = null;
191         for (int k = 0; k < names.length; ++k) {
192             String JavaDoc name[] = names[k];
193             if (name[0].equals("1") && name[1].equals("0"))
194                 name10 = name[3];
195             else if (name[2].equals("1033")) {
196                 name3x = name[3];
197                 break;
198             }
199         }
200         String JavaDoc finalName = name3x;
201         if (finalName == null)
202             finalName = name10;
203         if (finalName == null)
204             finalName = names[0][3];
205         return new Font JavaDoc(finalName, 0, size);
206     }
207     
208     /** Maps a name to a BaseFont parameter.
209      * @param awtName the name
210      * @param parameters the BaseFont parameter
211      */

212     public void putName(String JavaDoc awtName, BaseFontParameters parameters) {
213         mapper.put(awtName, parameters);
214     }
215     
216     /** Maps an alias to a name.
217      * @param alias the alias
218      * @param awtName the name
219      */

220     public void putAlias(String JavaDoc alias, String JavaDoc awtName) {
221         aliases.put(alias, awtName);
222     }
223     
224     /** Looks for a BaseFont parameter associated with a name.
225      * @param name the name
226      * @return the BaseFont parameter or <CODE>null</CODE> if not found.
227      */

228     public BaseFontParameters getBaseFontParameters(String JavaDoc name) {
229         String JavaDoc alias = (String JavaDoc)aliases.get(name);
230         if (alias == null)
231             return (BaseFontParameters)mapper.get(name);
232         BaseFontParameters p = (BaseFontParameters)mapper.get(alias);
233         if (p == null)
234             return (BaseFontParameters)mapper.get(name);
235         else
236             return p;
237     }
238     
239     /**
240      * Inserts the names in this map.
241      * @param allNames the returned value of calling {@link BaseFont#getAllFontNames(String, String, byte[])}
242      * @param path the full path to the font
243      */

244     public void insertNames(Object JavaDoc allNames[], String JavaDoc path) {
245         String JavaDoc names[][] = (String JavaDoc[][])allNames[2];
246         String JavaDoc main = null;
247         for (int k = 0; k < names.length; ++k) {
248             String JavaDoc name[] = names[k];
249             if (name[2].equals("1033")) {
250                 main = name[3];
251                 break;
252             }
253         }
254         if (main == null)
255             main = names[0][3];
256         BaseFontParameters p = new BaseFontParameters(path);
257         mapper.put(main, p);
258         for (int k = 0; k < names.length; ++k) {
259             aliases.put(names[k][3], main);
260         }
261         aliases.put(allNames[0], main);
262     }
263     
264     /** Inserts all the fonts recognized by iText in the
265      * <CODE>directory</CODE> into the map. The encoding
266      * will be <CODE>BaseFont.CP1252</CODE> but can be
267      * changed later.
268      * @param dir the directory to scan
269      * @return the number of files processed
270      */

271     public int insertDirectory(String JavaDoc dir) {
272         File JavaDoc file = new File JavaDoc(dir);
273         if (!file.exists() || !file.isDirectory())
274             return 0;
275         File JavaDoc files[] = file.listFiles();
276         if (files == null)
277             return 0;
278         int count = 0;
279         for (int k = 0; k < files.length; ++k) {
280             file = files[k];
281             String JavaDoc name = file.getPath().toLowerCase();
282             try {
283                 if (name.endsWith(".ttf") || name.endsWith(".otf") || name.endsWith(".afm")) {
284                     Object JavaDoc allNames[] = BaseFont.getAllFontNames(file.getPath(), BaseFont.CP1252, null);
285                     insertNames(allNames, file.getPath());
286                     ++count;
287                 }
288                 else if (name.endsWith(".ttc")) {
289                     String JavaDoc ttcs[] = BaseFont.enumerateTTCNames(file.getPath());
290                     for (int j = 0; j < ttcs.length; ++j) {
291                         String JavaDoc nt = file.getPath() + "," + j;
292                         Object JavaDoc allNames[] = BaseFont.getAllFontNames(nt, BaseFont.CP1252, null);
293                         insertNames(allNames, nt);
294                     }
295                     ++count;
296                 }
297             }
298             catch (Exception JavaDoc e) {
299             }
300         }
301         return count;
302     }
303     
304     public HashMap JavaDoc getMapper() {
305         return mapper;
306     }
307     
308     public HashMap JavaDoc getAliases() {
309         return aliases;
310     }
311 }
312
Popular Tags