KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > FontFactoryImp


1 /*
2  * $Id: FontFactoryImp.java 2745 2007-05-09 22:52:57Z xlv $
3  * $Name$
4  *
5  * Copyright 2002 by Bruno Lowagie.
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;
52
53 import java.awt.Color JavaDoc;
54 import java.io.File JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.Enumeration JavaDoc;
58 import java.util.Hashtable JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.Properties JavaDoc;
61 import java.util.Set JavaDoc;
62
63 import com.lowagie.text.html.Markup;
64 import com.lowagie.text.pdf.BaseFont;
65
66 /**
67  * If you are using True Type fonts, you can declare the paths of the different ttf- and ttc-files
68  * to this class first and then create fonts in your code using one of the getFont method
69  * without having to enter a path as parameter.
70  *
71  * @author Bruno Lowagie
72  */

73
74 public class FontFactoryImp {
75         
76 /** This is a map of postscriptfontnames of True Type fonts and the path of their ttf- or ttc-file. */
77     private Properties JavaDoc trueTypeFonts = new Properties JavaDoc();
78     
79     private static String JavaDoc[] TTFamilyOrder = {
80         "3", "1", "1033",
81         "3", "0", "1033",
82         "1", "0", "0",
83         "0", "3", "0"
84     };
85
86 /** This is a map of fontfamilies. */
87     private Hashtable JavaDoc fontFamilies = new Hashtable JavaDoc();
88     
89 /** This is the default encoding to use. */
90     public String JavaDoc defaultEncoding = BaseFont.WINANSI;
91     
92 /** This is the default value of the <VAR>embedded</VAR> variable. */
93     public boolean defaultEmbedding = BaseFont.NOT_EMBEDDED;
94     
95 /** Creates new FontFactory */
96     public FontFactoryImp() {
97         trueTypeFonts.setProperty(FontFactory.COURIER.toLowerCase(), FontFactory.COURIER);
98         trueTypeFonts.setProperty(FontFactory.COURIER_BOLD.toLowerCase(), FontFactory.COURIER_BOLD);
99         trueTypeFonts.setProperty(FontFactory.COURIER_OBLIQUE.toLowerCase(), FontFactory.COURIER_OBLIQUE);
100         trueTypeFonts.setProperty(FontFactory.COURIER_BOLDOBLIQUE.toLowerCase(), FontFactory.COURIER_BOLDOBLIQUE);
101         trueTypeFonts.setProperty(FontFactory.HELVETICA.toLowerCase(), FontFactory.HELVETICA);
102         trueTypeFonts.setProperty(FontFactory.HELVETICA_BOLD.toLowerCase(), FontFactory.HELVETICA_BOLD);
103         trueTypeFonts.setProperty(FontFactory.HELVETICA_OBLIQUE.toLowerCase(), FontFactory.HELVETICA_OBLIQUE);
104         trueTypeFonts.setProperty(FontFactory.HELVETICA_BOLDOBLIQUE.toLowerCase(), FontFactory.HELVETICA_BOLDOBLIQUE);
105         trueTypeFonts.setProperty(FontFactory.SYMBOL.toLowerCase(), FontFactory.SYMBOL);
106         trueTypeFonts.setProperty(FontFactory.TIMES_ROMAN.toLowerCase(), FontFactory.TIMES_ROMAN);
107         trueTypeFonts.setProperty(FontFactory.TIMES_BOLD.toLowerCase(), FontFactory.TIMES_BOLD);
108         trueTypeFonts.setProperty(FontFactory.TIMES_ITALIC.toLowerCase(), FontFactory.TIMES_ITALIC);
109         trueTypeFonts.setProperty(FontFactory.TIMES_BOLDITALIC.toLowerCase(), FontFactory.TIMES_BOLDITALIC);
110         trueTypeFonts.setProperty(FontFactory.ZAPFDINGBATS.toLowerCase(), FontFactory.ZAPFDINGBATS);
111
112         ArrayList JavaDoc tmp;
113         tmp = new ArrayList JavaDoc();
114         tmp.add(FontFactory.COURIER);
115         tmp.add(FontFactory.COURIER_BOLD);
116         tmp.add(FontFactory.COURIER_OBLIQUE);
117         tmp.add(FontFactory.COURIER_BOLDOBLIQUE);
118         fontFamilies.put(FontFactory.COURIER.toLowerCase(), tmp);
119         tmp = new ArrayList JavaDoc();
120         tmp.add(FontFactory.HELVETICA);
121         tmp.add(FontFactory.HELVETICA_BOLD);
122         tmp.add(FontFactory.HELVETICA_OBLIQUE);
123         tmp.add(FontFactory.HELVETICA_BOLDOBLIQUE);
124         fontFamilies.put(FontFactory.HELVETICA.toLowerCase(), tmp);
125         tmp = new ArrayList JavaDoc();
126         tmp.add(FontFactory.SYMBOL);
127         fontFamilies.put(FontFactory.SYMBOL.toLowerCase(), tmp);
128         tmp = new ArrayList JavaDoc();
129         tmp.add(FontFactory.TIMES_ROMAN);
130         tmp.add(FontFactory.TIMES_BOLD);
131         tmp.add(FontFactory.TIMES_ITALIC);
132         tmp.add(FontFactory.TIMES_BOLDITALIC);
133         fontFamilies.put(FontFactory.TIMES.toLowerCase(), tmp);
134         fontFamilies.put(FontFactory.TIMES_ROMAN.toLowerCase(), tmp);
135         tmp = new ArrayList JavaDoc();
136         tmp.add(FontFactory.ZAPFDINGBATS);
137         fontFamilies.put(FontFactory.ZAPFDINGBATS.toLowerCase(), tmp);
138     }
139     
140     /**
141      * Constructs a <CODE>Font</CODE>-object.
142      *
143      * @param fontname the name of the font
144      * @param encoding the encoding of the font
145      * @param embedded true if the font is to be embedded in the PDF
146      * @param size the size of this font
147      * @param style the style of this font
148      * @param color the <CODE>Color</CODE> of this font.
149      * @return the Font constructed based on the parameters
150      */

151     public Font getFont(String JavaDoc fontname, String JavaDoc encoding, boolean embedded, float size, int style, Color JavaDoc color) {
152         return getFont(fontname, encoding, embedded, size, style, color, true);
153     }
154     
155     
156     
157     /**
158      * Constructs a <CODE>Font</CODE>-object.
159      *
160      * @param fontname the name of the font
161      * @param encoding the encoding of the font
162      * @param embedded true if the font is to be embedded in the PDF
163      * @param size the size of this font
164      * @param style the style of this font
165      * @param color the <CODE>Color</CODE> of this font.
166      * @param cached true if the font comes from the cache or is added to
167      * the cache if new, false if the font is always created new
168      * @return the Font constructed based on the parameters
169      */

170     public Font getFont(String JavaDoc fontname, String JavaDoc encoding, boolean embedded, float size, int style, Color JavaDoc color, boolean cached) {
171         if (fontname == null) return new Font(Font.UNDEFINED, size, style, color);
172         String JavaDoc lowercasefontname = fontname.toLowerCase();
173         ArrayList JavaDoc tmp = (ArrayList JavaDoc) fontFamilies.get(lowercasefontname);
174         if (tmp != null) {
175             // some bugs were fixed here by Daniel Marczisovszky
176
int s = style == Font.UNDEFINED ? Font.NORMAL : style;
177             int fs = Font.NORMAL;
178             boolean found = false;
179             for (Iterator JavaDoc i = tmp.iterator(); i.hasNext(); ) {
180                 String JavaDoc f = (String JavaDoc) i.next();
181                 String JavaDoc lcf = f.toLowerCase();
182                 fs = Font.NORMAL;
183                 if (lcf.toLowerCase().indexOf("bold") != -1) fs |= Font.BOLD;
184                 if (lcf.toLowerCase().indexOf("italic") != -1 || lcf.toLowerCase().indexOf("oblique") != -1) fs |= Font.ITALIC;
185                 if ((s & Font.BOLDITALIC) == fs) {
186                     fontname = f;
187                     found = true;
188                     break;
189                 }
190             }
191             if (style != Font.UNDEFINED && found) {
192                 style &= ~fs;
193             }
194         }
195         BaseFont basefont = null;
196         try {
197             try {
198                 // the font is a type 1 font or CJK font
199
basefont = BaseFont.createFont(fontname, encoding, embedded, cached, null, null, true);
200             }
201             catch(DocumentException de) {
202             }
203             if (basefont == null) {
204                 // the font is a true type font or an unknown font
205
fontname = trueTypeFonts.getProperty(fontname.toLowerCase());
206                 // the font is not registered as truetype font
207
if (fontname == null) return new Font(Font.UNDEFINED, size, style, color);
208                 // the font is registered as truetype font
209
basefont = BaseFont.createFont(fontname, encoding, embedded, cached, null, null);
210             }
211         }
212         catch(DocumentException de) {
213             // this shouldn't happen
214
throw new ExceptionConverter(de);
215         }
216         catch(IOException JavaDoc ioe) {
217             // the font is registered as a true type font, but the path was wrong
218
return new Font(Font.UNDEFINED, size, style, color);
219         }
220         catch(NullPointerException JavaDoc npe) {
221             // null was entered as fontname and/or encoding
222
return new Font(Font.UNDEFINED, size, style, color);
223         }
224         return new Font(basefont, size, style, color);
225     }
226     
227     
228 /**
229  * Constructs a <CODE>Font</CODE>-object.
230  *
231  * @param attributes the attributes of a <CODE>Font</CODE> object.
232  * @return the Font constructed based on the attributes
233  */

234     
235     public Font getFont(Properties JavaDoc attributes) {
236         String JavaDoc fontname = null;
237         String JavaDoc encoding = defaultEncoding;
238         boolean embedded = defaultEmbedding;
239         float size = Font.UNDEFINED;
240         int style = Font.NORMAL;
241         Color JavaDoc color = null;
242         String JavaDoc value = attributes.getProperty(Markup.HTML_ATTR_STYLE);
243         if (value != null && value.length() > 0) {
244             Properties JavaDoc styleAttributes = Markup.parseAttributes(value);
245             if (styleAttributes.isEmpty()) {
246                 attributes.put(Markup.HTML_ATTR_STYLE, value);
247             }
248             else {
249                 fontname = styleAttributes.getProperty(Markup.CSS_KEY_FONTFAMILY);
250                 if (fontname != null) {
251                     String JavaDoc tmp;
252                     while (fontname.indexOf(',') != -1) {
253                         tmp = fontname.substring(0, fontname.indexOf(','));
254                         if (isRegistered(tmp)) {
255                             fontname = tmp;
256                         }
257                         else {
258                             fontname = fontname.substring(fontname.indexOf(',') + 1);
259                         }
260                     }
261                 }
262                 if ((value = styleAttributes.getProperty(Markup.CSS_KEY_FONTSIZE)) != null) {
263                     size = Markup.parseLength(value);
264                 }
265                 if ((value = styleAttributes.getProperty(Markup.CSS_KEY_FONTWEIGHT)) != null) {
266                     style |= Font.getStyleValue(value);
267                 }
268                 if ((value = styleAttributes.getProperty(Markup.CSS_KEY_FONTSTYLE)) != null) {
269                     style |= Font.getStyleValue(value);
270                 }
271                 if ((value = styleAttributes.getProperty(Markup.CSS_KEY_COLOR)) != null) {
272                     color = Markup.decodeColor(value);
273                 }
274                 attributes.putAll(styleAttributes);
275                 for (Enumeration JavaDoc e = styleAttributes.keys(); e.hasMoreElements();) {
276                     Object JavaDoc o = e.nextElement();
277                     attributes.put(o, styleAttributes.get(o));
278                 }
279             }
280         }
281         if ((value = attributes.getProperty(ElementTags.ENCODING)) != null) {
282             encoding = value;
283         }
284         if ("true".equals(attributes.getProperty(ElementTags.EMBEDDED))) {
285             embedded = true;
286         }
287         if ((value = attributes.getProperty(ElementTags.FONT)) != null) {
288             fontname = value;
289         }
290         if ((value = attributes.getProperty(ElementTags.SIZE)) != null) {
291             size = Float.parseFloat(value + "f");
292         }
293         if ((value = attributes.getProperty(Markup.HTML_ATTR_STYLE)) != null) {
294             style |= Font.getStyleValue(value);
295         }
296         if ((value = attributes.getProperty(ElementTags.STYLE)) != null) {
297             style |= Font.getStyleValue(value);
298         }
299         String JavaDoc r = attributes.getProperty(ElementTags.RED);
300         String JavaDoc g = attributes.getProperty(ElementTags.GREEN);
301         String JavaDoc b = attributes.getProperty(ElementTags.BLUE);
302         if (r != null || g != null || b != null) {
303             int red = 0;
304             int green = 0;
305             int blue = 0;
306             if (r != null) red = Integer.parseInt(r);
307             if (g != null) green = Integer.parseInt(g);
308             if (b != null) blue = Integer.parseInt(b);
309             color = new Color JavaDoc(red, green, blue);
310         }
311         else if ((value = attributes.getProperty(ElementTags.COLOR)) != null) {
312             color = Markup.decodeColor(value);
313         }
314         if (fontname == null) {
315             return getFont(null, encoding, embedded, size, style, color);
316         }
317         return getFont(fontname, encoding, embedded, size, style, color);
318     }
319     
320 /**
321  * Constructs a <CODE>Font</CODE>-object.
322  *
323  * @param fontname the name of the font
324  * @param encoding the encoding of the font
325  * @param embedded true if the font is to be embedded in the PDF
326  * @param size the size of this font
327  * @param style the style of this font
328  * @return the Font constructed based on the parameters
329  */

330     
331     public Font getFont(String JavaDoc fontname, String JavaDoc encoding, boolean embedded, float size, int style) {
332         return getFont(fontname, encoding, embedded, size, style, null);
333     }
334     
335 /**
336  * Constructs a <CODE>Font</CODE>-object.
337  *
338  * @param fontname the name of the font
339  * @param encoding the encoding of the font
340  * @param embedded true if the font is to be embedded in the PDF
341  * @param size the size of this font
342  * @return the Font constructed based on the parameters
343  */

344     
345     public Font getFont(String JavaDoc fontname, String JavaDoc encoding, boolean embedded, float size) {
346         return getFont(fontname, encoding, embedded, size, Font.UNDEFINED, null);
347     }
348     
349 /**
350  * Constructs a <CODE>Font</CODE>-object.
351  *
352  * @param fontname the name of the font
353  * @param encoding the encoding of the font
354  * @param embedded true if the font is to be embedded in the PDF
355  * @return the Font constructed based on the parameters
356  */

357     
358     public Font getFont(String JavaDoc fontname, String JavaDoc encoding, boolean embedded) {
359         return getFont(fontname, encoding, embedded, Font.UNDEFINED, Font.UNDEFINED, null);
360     }
361     
362 /**
363  * Constructs a <CODE>Font</CODE>-object.
364  *
365  * @param fontname the name of the font
366  * @param encoding the encoding of the font
367  * @param size the size of this font
368  * @param style the style of this font
369  * @param color the <CODE>Color</CODE> of this font.
370  * @return the Font constructed based on the parameters
371  */

372     
373     public Font getFont(String JavaDoc fontname, String JavaDoc encoding, float size, int style, Color JavaDoc color) {
374         return getFont(fontname, encoding, defaultEmbedding, size, style, color);
375     }
376     
377 /**
378  * Constructs a <CODE>Font</CODE>-object.
379  *
380  * @param fontname the name of the font
381  * @param encoding the encoding of the font
382  * @param size the size of this font
383  * @param style the style of this font
384  * @return the Font constructed based on the parameters
385  */

386     
387     public Font getFont(String JavaDoc fontname, String JavaDoc encoding, float size, int style) {
388         return getFont(fontname, encoding, defaultEmbedding, size, style, null);
389     }
390     
391 /**
392  * Constructs a <CODE>Font</CODE>-object.
393  *
394  * @param fontname the name of the font
395  * @param encoding the encoding of the font
396  * @param size the size of this font
397  * @return the Font constructed based on the parameters
398  */

399     
400     public Font getFont(String JavaDoc fontname, String JavaDoc encoding, float size) {
401         return getFont(fontname, encoding, defaultEmbedding, size, Font.UNDEFINED, null);
402     }
403     
404 /**
405  * Constructs a <CODE>Font</CODE>-object.
406  *
407  * @param fontname the name of the font
408  * @param encoding the encoding of the font
409  * @return the Font constructed based on the parameters
410  */

411     
412     public Font getFont(String JavaDoc fontname, String JavaDoc encoding) {
413         return getFont(fontname, encoding, defaultEmbedding, Font.UNDEFINED, Font.UNDEFINED, null);
414     }
415     
416 /**
417  * Constructs a <CODE>Font</CODE>-object.
418  *
419  * @param fontname the name of the font
420  * @param size the size of this font
421  * @param style the style of this font
422  * @param color the <CODE>Color</CODE> of this font.
423  * @return the Font constructed based on the parameters
424  */

425     
426     public Font getFont(String JavaDoc fontname, float size, int style, Color JavaDoc color) {
427         return getFont(fontname, defaultEncoding, defaultEmbedding, size, style, color);
428     }
429     
430 /**
431  * Constructs a <CODE>Font</CODE>-object.
432  *
433  * @param fontname the name of the font
434  * @param size the size of this font
435  * @param style the style of this font
436  * @return the Font constructed based on the parameters
437  */

438     
439     public Font getFont(String JavaDoc fontname, float size, int style) {
440         return getFont(fontname, defaultEncoding, defaultEmbedding, size, style, null);
441     }
442     
443 /**
444  * Constructs a <CODE>Font</CODE>-object.
445  *
446  * @param fontname the name of the font
447  * @param size the size of this font
448  * @return the Font constructed based on the parameters
449  */

450     
451     public Font getFont(String JavaDoc fontname, float size) {
452         return getFont(fontname, defaultEncoding, defaultEmbedding, size, Font.UNDEFINED, null);
453     }
454     
455 /**
456  * Constructs a <CODE>Font</CODE>-object.
457  *
458  * @param fontname the name of the font
459  * @return the Font constructed based on the parameters
460  */

461     
462     public Font getFont(String JavaDoc fontname) {
463         return getFont(fontname, defaultEncoding, defaultEmbedding, Font.UNDEFINED, Font.UNDEFINED, null);
464     }
465     
466     /**
467      * Register a font by giving explicitly the font family and name.
468      * @param familyName the font family
469      * @param fullName the font name
470      * @param path the font path
471      */

472     public void registerFamily(String JavaDoc familyName, String JavaDoc fullName, String JavaDoc path) {
473         if (path != null)
474             trueTypeFonts.setProperty(fullName, path);
475         ArrayList JavaDoc tmp = (ArrayList JavaDoc) fontFamilies.get(familyName);
476         if (tmp == null) {
477             tmp = new ArrayList JavaDoc();
478             tmp.add(fullName);
479             fontFamilies.put(familyName, tmp);
480         }
481         else {
482             int fullNameLength = fullName.length();
483             boolean inserted = false;
484             for (int j = 0; j < tmp.size(); ++j) {
485                 if (((String JavaDoc)tmp.get(j)).length() >= fullNameLength) {
486                     tmp.add(j, fullName);
487                     inserted = true;
488                     break;
489                 }
490             }
491             if (!inserted)
492                 tmp.add(fullName);
493         }
494     }
495     
496 /**
497  * Register a ttf- or a ttc-file.
498  *
499  * @param path the path to a ttf- or ttc-file
500  */

501     
502     public void register(String JavaDoc path) {
503         register(path, null);
504     }
505     
506 /**
507  * Register a font file and use an alias for the font contained in it.
508  *
509  * @param path the path to a font file
510  * @param alias the alias you want to use for the font
511  */

512     
513     public void register(String JavaDoc path, String JavaDoc alias) {
514         try {
515             if (path.toLowerCase().endsWith(".ttf") || path.toLowerCase().endsWith(".otf") || path.toLowerCase().indexOf(".ttc,") > 0) {
516                 Object JavaDoc allNames[] = BaseFont.getAllFontNames(path, BaseFont.WINANSI, null);
517                 trueTypeFonts.setProperty(((String JavaDoc)allNames[0]).toLowerCase(), path);
518                 if (alias != null) {
519                     trueTypeFonts.setProperty(alias.toLowerCase(), path);
520                 }
521                 // register all the font names with all the locales
522
String JavaDoc[][] names = (String JavaDoc[][])allNames[2]; //full name
523
for (int i = 0; i < names.length; i++) {
524                     trueTypeFonts.setProperty(names[i][3].toLowerCase(), path);
525                 }
526                 String JavaDoc fullName = null;
527                 String JavaDoc familyName = null;
528                 names = (String JavaDoc[][])allNames[1]; //family name
529
for (int k = 0; k < TTFamilyOrder.length; k += 3) {
530                     for (int i = 0; i < names.length; i++) {
531                         if (TTFamilyOrder[k].equals(names[i][0]) && TTFamilyOrder[k + 1].equals(names[i][1]) && TTFamilyOrder[k + 2].equals(names[i][2])) {
532                             familyName = names[i][3].toLowerCase();
533                             k = TTFamilyOrder.length;
534                             break;
535                         }
536                     }
537                 }
538                 if (familyName != null) {
539                     String JavaDoc lastName = "";
540                     names = (String JavaDoc[][])allNames[2]; //full name
541
for (int i = 0; i < names.length; i++) {
542                         for (int k = 0; k < TTFamilyOrder.length; k += 3) {
543                             if (TTFamilyOrder[k].equals(names[i][0]) && TTFamilyOrder[k + 1].equals(names[i][1]) && TTFamilyOrder[k + 2].equals(names[i][2])) {
544                                 fullName = names[i][3];
545                                 if (fullName.equals(lastName))
546                                     continue;
547                                 lastName = fullName;
548                                 registerFamily(familyName, fullName, null);
549                                 break;
550                             }
551                         }
552                     }
553                 }
554             }
555             else if (path.toLowerCase().endsWith(".ttc")) {
556                 if (alias != null)
557                     System.err.println("class FontFactory: You can't define an alias for a true type collection.");
558                 String JavaDoc[] names = BaseFont.enumerateTTCNames(path);
559                 for (int i = 0; i < names.length; i++) {
560                     register(path + "," + i);
561                 }
562             }
563             else if (path.toLowerCase().endsWith(".afm") || path.toLowerCase().endsWith(".pfm")) {
564                 BaseFont bf = BaseFont.createFont(path, BaseFont.CP1252, false);
565                 String JavaDoc fullName = (bf.getFullFontName()[0][3]).toLowerCase();
566                 String JavaDoc familyName = (bf.getFamilyFontName()[0][3]).toLowerCase();
567                 String JavaDoc psName = bf.getPostscriptFontName().toLowerCase();
568                 registerFamily(familyName, fullName, null);
569                 trueTypeFonts.setProperty(psName, path);
570                 trueTypeFonts.setProperty(fullName, path);
571             }
572         }
573         catch(DocumentException de) {
574             // this shouldn't happen
575
throw new ExceptionConverter(de);
576         }
577         catch(IOException JavaDoc ioe) {
578             throw new ExceptionConverter(ioe);
579         }
580     }
581
582     /** Register all the fonts in a directory.
583      * @param dir the directory
584      * @return the number of fonts registered
585      */

586     public int registerDirectory(String JavaDoc dir) {
587         int count = 0;
588         try {
589             File JavaDoc file = new File JavaDoc(dir);
590             if (!file.exists() || !file.isDirectory())
591                 return 0;
592             String JavaDoc files[] = file.list();
593             if (files == null)
594                 return 0;
595             for (int k = 0; k < files.length; ++k) {
596                 try {
597                     file = new File JavaDoc(dir, files[k]);
598                     String JavaDoc name = file.getPath().toLowerCase();
599                     if (name.endsWith(".ttf") || name.endsWith(".otf") || name.endsWith(".afm") || name.endsWith(".pfm") || name.endsWith(".ttc")) {
600                         register(file.getPath(), null);
601                         ++count;
602                     }
603                 }
604                 catch (Exception JavaDoc e) {
605                     //empty on purpose
606
}
607             }
608         }
609         catch (Exception JavaDoc e) {
610             //empty on purpose
611
}
612         return count;
613     }
614
615     /** Register fonts in some probable directories. It usually works in Windows,
616      * Linux and Solaris.
617      * @return the number of fonts registered
618      */

619     public int registerDirectories() {
620         int count = 0;
621         count += registerDirectory("c:/windows/fonts");
622         count += registerDirectory("c:/winnt/fonts");
623         count += registerDirectory("d:/windows/fonts");
624         count += registerDirectory("d:/winnt/fonts");
625         count += registerDirectory("/usr/X/lib/X11/fonts/TrueType");
626         count += registerDirectory("/usr/openwin/lib/X11/fonts/TrueType");
627         count += registerDirectory("/usr/share/fonts/default/TrueType");
628         count += registerDirectory("/usr/X11R6/lib/X11/fonts/ttf");
629         count += registerDirectory("/Library/Fonts");
630         count += registerDirectory("/System/Library/Fonts");
631         return count;
632     }
633
634 /**
635  * Gets a set of registered fontnames.
636  * @return a set of registered fonts
637  */

638     
639     public Set JavaDoc getRegisteredFonts() {
640         return Utilities.getKeySet(trueTypeFonts);
641     }
642     
643 /**
644  * Gets a set of registered fontnames.
645  * @return a set of registered font families
646  */

647     
648     public Set JavaDoc getRegisteredFamilies() {
649         return Utilities.getKeySet(fontFamilies);
650     }
651     
652 /**
653  * Checks if a certain font is registered.
654  *
655  * @param fontname the name of the font that has to be checked.
656  * @return true if the font is found
657  */

658     public boolean isRegistered(String JavaDoc fontname) {
659         return trueTypeFonts.containsKey(fontname.toLowerCase());
660     }
661 }
Popular Tags