001: /*
002: * $Id: FontFactoryImp.java 2838 2007-06-13 12:53:11Z blowagie $
003: * $Name$
004: *
005: * Copyright 2002 by Bruno Lowagie.
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text;
052:
053: import java.awt.Color;
054: import java.io.File;
055: import java.io.IOException;
056: import java.util.ArrayList;
057: import java.util.Enumeration;
058: import java.util.Hashtable;
059: import java.util.Iterator;
060: import java.util.Properties;
061: import java.util.Set;
062:
063: import com.lowagie.text.html.Markup;
064: import com.lowagie.text.pdf.BaseFont;
065:
066: /**
067: * If you are using True Type fonts, you can declare the paths of the different ttf- and ttc-files
068: * to this class first and then create fonts in your code using one of the getFont method
069: * without having to enter a path as parameter.
070: *
071: * @author Bruno Lowagie
072: */
073:
074: public class FontFactoryImp {
075:
076: /** This is a map of postscriptfontnames of True Type fonts and the path of their ttf- or ttc-file. */
077: private Properties trueTypeFonts = new Properties();
078:
079: private static String[] TTFamilyOrder = { "3", "1", "1033", "3",
080: "0", "1033", "1", "0", "0", "0", "3", "0" };
081:
082: /** This is a map of fontfamilies. */
083: private Hashtable fontFamilies = new Hashtable();
084:
085: /** This is the default encoding to use. */
086: public String defaultEncoding = BaseFont.WINANSI;
087:
088: /** This is the default value of the <VAR>embedded</VAR> variable. */
089: public boolean defaultEmbedding = BaseFont.NOT_EMBEDDED;
090:
091: /** Creates new FontFactory */
092: public FontFactoryImp() {
093: trueTypeFonts.setProperty(FontFactory.COURIER.toLowerCase(),
094: FontFactory.COURIER);
095: trueTypeFonts.setProperty(FontFactory.COURIER_BOLD
096: .toLowerCase(), FontFactory.COURIER_BOLD);
097: trueTypeFonts.setProperty(FontFactory.COURIER_OBLIQUE
098: .toLowerCase(), FontFactory.COURIER_OBLIQUE);
099: trueTypeFonts.setProperty(FontFactory.COURIER_BOLDOBLIQUE
100: .toLowerCase(), FontFactory.COURIER_BOLDOBLIQUE);
101: trueTypeFonts.setProperty(FontFactory.HELVETICA.toLowerCase(),
102: FontFactory.HELVETICA);
103: trueTypeFonts.setProperty(FontFactory.HELVETICA_BOLD
104: .toLowerCase(), FontFactory.HELVETICA_BOLD);
105: trueTypeFonts.setProperty(FontFactory.HELVETICA_OBLIQUE
106: .toLowerCase(), FontFactory.HELVETICA_OBLIQUE);
107: trueTypeFonts.setProperty(FontFactory.HELVETICA_BOLDOBLIQUE
108: .toLowerCase(), FontFactory.HELVETICA_BOLDOBLIQUE);
109: trueTypeFonts.setProperty(FontFactory.SYMBOL.toLowerCase(),
110: FontFactory.SYMBOL);
111: trueTypeFonts.setProperty(
112: FontFactory.TIMES_ROMAN.toLowerCase(),
113: FontFactory.TIMES_ROMAN);
114: trueTypeFonts.setProperty(FontFactory.TIMES_BOLD.toLowerCase(),
115: FontFactory.TIMES_BOLD);
116: trueTypeFonts.setProperty(FontFactory.TIMES_ITALIC
117: .toLowerCase(), FontFactory.TIMES_ITALIC);
118: trueTypeFonts.setProperty(FontFactory.TIMES_BOLDITALIC
119: .toLowerCase(), FontFactory.TIMES_BOLDITALIC);
120: trueTypeFonts.setProperty(FontFactory.ZAPFDINGBATS
121: .toLowerCase(), FontFactory.ZAPFDINGBATS);
122:
123: ArrayList tmp;
124: tmp = new ArrayList();
125: tmp.add(FontFactory.COURIER);
126: tmp.add(FontFactory.COURIER_BOLD);
127: tmp.add(FontFactory.COURIER_OBLIQUE);
128: tmp.add(FontFactory.COURIER_BOLDOBLIQUE);
129: fontFamilies.put(FontFactory.COURIER.toLowerCase(), tmp);
130: tmp = new ArrayList();
131: tmp.add(FontFactory.HELVETICA);
132: tmp.add(FontFactory.HELVETICA_BOLD);
133: tmp.add(FontFactory.HELVETICA_OBLIQUE);
134: tmp.add(FontFactory.HELVETICA_BOLDOBLIQUE);
135: fontFamilies.put(FontFactory.HELVETICA.toLowerCase(), tmp);
136: tmp = new ArrayList();
137: tmp.add(FontFactory.SYMBOL);
138: fontFamilies.put(FontFactory.SYMBOL.toLowerCase(), tmp);
139: tmp = new ArrayList();
140: tmp.add(FontFactory.TIMES_ROMAN);
141: tmp.add(FontFactory.TIMES_BOLD);
142: tmp.add(FontFactory.TIMES_ITALIC);
143: tmp.add(FontFactory.TIMES_BOLDITALIC);
144: fontFamilies.put(FontFactory.TIMES.toLowerCase(), tmp);
145: fontFamilies.put(FontFactory.TIMES_ROMAN.toLowerCase(), tmp);
146: tmp = new ArrayList();
147: tmp.add(FontFactory.ZAPFDINGBATS);
148: fontFamilies.put(FontFactory.ZAPFDINGBATS.toLowerCase(), tmp);
149: }
150:
151: /**
152: * Constructs a <CODE>Font</CODE>-object.
153: *
154: * @param fontname the name of the font
155: * @param encoding the encoding of the font
156: * @param embedded true if the font is to be embedded in the PDF
157: * @param size the size of this font
158: * @param style the style of this font
159: * @param color the <CODE>Color</CODE> of this font.
160: * @return the Font constructed based on the parameters
161: */
162: public Font getFont(String fontname, String encoding,
163: boolean embedded, float size, int style, Color color) {
164: return getFont(fontname, encoding, embedded, size, style,
165: color, true);
166: }
167:
168: /**
169: * Constructs a <CODE>Font</CODE>-object.
170: *
171: * @param fontname the name of the font
172: * @param encoding the encoding of the font
173: * @param embedded true if the font is to be embedded in the PDF
174: * @param size the size of this font
175: * @param style the style of this font
176: * @param color the <CODE>Color</CODE> of this font.
177: * @param cached true if the font comes from the cache or is added to
178: * the cache if new, false if the font is always created new
179: * @return the Font constructed based on the parameters
180: */
181: public Font getFont(String fontname, String encoding,
182: boolean embedded, float size, int style, Color color,
183: boolean cached) {
184: if (fontname == null)
185: return new Font(Font.UNDEFINED, size, style, color);
186: String lowercasefontname = fontname.toLowerCase();
187: ArrayList tmp = (ArrayList) fontFamilies.get(lowercasefontname);
188: if (tmp != null) {
189: // some bugs were fixed here by Daniel Marczisovszky
190: int s = style == Font.UNDEFINED ? Font.NORMAL : style;
191: int fs = Font.NORMAL;
192: boolean found = false;
193: for (Iterator i = tmp.iterator(); i.hasNext();) {
194: String f = (String) i.next();
195: String lcf = f.toLowerCase();
196: fs = Font.NORMAL;
197: if (lcf.toLowerCase().indexOf("bold") != -1)
198: fs |= Font.BOLD;
199: if (lcf.toLowerCase().indexOf("italic") != -1
200: || lcf.toLowerCase().indexOf("oblique") != -1)
201: fs |= Font.ITALIC;
202: if ((s & Font.BOLDITALIC) == fs) {
203: fontname = f;
204: found = true;
205: break;
206: }
207: }
208: if (style != Font.UNDEFINED && found) {
209: style &= ~fs;
210: }
211: }
212: BaseFont basefont = null;
213: try {
214: try {
215: // the font is a type 1 font or CJK font
216: basefont = BaseFont.createFont(fontname, encoding,
217: embedded, cached, null, null, true);
218: } catch (DocumentException de) {
219: }
220: if (basefont == null) {
221: // the font is a true type font or an unknown font
222: fontname = trueTypeFonts.getProperty(fontname
223: .toLowerCase());
224: // the font is not registered as truetype font
225: if (fontname == null)
226: return new Font(Font.UNDEFINED, size, style, color);
227: // the font is registered as truetype font
228: basefont = BaseFont.createFont(fontname, encoding,
229: embedded, cached, null, null);
230: }
231: } catch (DocumentException de) {
232: // this shouldn't happen
233: throw new ExceptionConverter(de);
234: } catch (IOException ioe) {
235: // the font is registered as a true type font, but the path was wrong
236: return new Font(Font.UNDEFINED, size, style, color);
237: } catch (NullPointerException npe) {
238: // null was entered as fontname and/or encoding
239: return new Font(Font.UNDEFINED, size, style, color);
240: }
241: return new Font(basefont, size, style, color);
242: }
243:
244: /**
245: * Constructs a <CODE>Font</CODE>-object.
246: *
247: * @param attributes the attributes of a <CODE>Font</CODE> object.
248: * @return the Font constructed based on the attributes
249: */
250:
251: public Font getFont(Properties attributes) {
252: String fontname = null;
253: String encoding = defaultEncoding;
254: boolean embedded = defaultEmbedding;
255: float size = Font.UNDEFINED;
256: int style = Font.NORMAL;
257: Color color = null;
258: String value = attributes.getProperty(Markup.HTML_ATTR_STYLE);
259: if (value != null && value.length() > 0) {
260: Properties styleAttributes = Markup.parseAttributes(value);
261: if (styleAttributes.isEmpty()) {
262: attributes.put(Markup.HTML_ATTR_STYLE, value);
263: } else {
264: fontname = styleAttributes
265: .getProperty(Markup.CSS_KEY_FONTFAMILY);
266: if (fontname != null) {
267: String tmp;
268: while (fontname.indexOf(',') != -1) {
269: tmp = fontname.substring(0, fontname
270: .indexOf(','));
271: if (isRegistered(tmp)) {
272: fontname = tmp;
273: } else {
274: fontname = fontname.substring(fontname
275: .indexOf(',') + 1);
276: }
277: }
278: }
279: if ((value = styleAttributes
280: .getProperty(Markup.CSS_KEY_FONTSIZE)) != null) {
281: size = Markup.parseLength(value);
282: }
283: if ((value = styleAttributes
284: .getProperty(Markup.CSS_KEY_FONTWEIGHT)) != null) {
285: style |= Font.getStyleValue(value);
286: }
287: if ((value = styleAttributes
288: .getProperty(Markup.CSS_KEY_FONTSTYLE)) != null) {
289: style |= Font.getStyleValue(value);
290: }
291: if ((value = styleAttributes
292: .getProperty(Markup.CSS_KEY_COLOR)) != null) {
293: color = Markup.decodeColor(value);
294: }
295: attributes.putAll(styleAttributes);
296: for (Enumeration e = styleAttributes.keys(); e
297: .hasMoreElements();) {
298: Object o = e.nextElement();
299: attributes.put(o, styleAttributes.get(o));
300: }
301: }
302: }
303: if ((value = attributes.getProperty(ElementTags.ENCODING)) != null) {
304: encoding = value;
305: }
306: if ("true".equals(attributes.getProperty(ElementTags.EMBEDDED))) {
307: embedded = true;
308: }
309: if ((value = attributes.getProperty(ElementTags.FONT)) != null) {
310: fontname = value;
311: }
312: if ((value = attributes.getProperty(ElementTags.SIZE)) != null) {
313: size = Markup.parseLength(value);
314: }
315: if ((value = attributes.getProperty(Markup.HTML_ATTR_STYLE)) != null) {
316: style |= Font.getStyleValue(value);
317: }
318: if ((value = attributes.getProperty(ElementTags.STYLE)) != null) {
319: style |= Font.getStyleValue(value);
320: }
321: String r = attributes.getProperty(ElementTags.RED);
322: String g = attributes.getProperty(ElementTags.GREEN);
323: String b = attributes.getProperty(ElementTags.BLUE);
324: if (r != null || g != null || b != null) {
325: int red = 0;
326: int green = 0;
327: int blue = 0;
328: if (r != null)
329: red = Integer.parseInt(r);
330: if (g != null)
331: green = Integer.parseInt(g);
332: if (b != null)
333: blue = Integer.parseInt(b);
334: color = new Color(red, green, blue);
335: } else if ((value = attributes.getProperty(ElementTags.COLOR)) != null) {
336: color = Markup.decodeColor(value);
337: }
338: if (fontname == null) {
339: return getFont(null, encoding, embedded, size, style, color);
340: }
341: return getFont(fontname, encoding, embedded, size, style, color);
342: }
343:
344: /**
345: * Constructs a <CODE>Font</CODE>-object.
346: *
347: * @param fontname the name of the font
348: * @param encoding the encoding of the font
349: * @param embedded true if the font is to be embedded in the PDF
350: * @param size the size of this font
351: * @param style the style of this font
352: * @return the Font constructed based on the parameters
353: */
354:
355: public Font getFont(String fontname, String encoding,
356: boolean embedded, float size, int style) {
357: return getFont(fontname, encoding, embedded, size, style, null);
358: }
359:
360: /**
361: * Constructs a <CODE>Font</CODE>-object.
362: *
363: * @param fontname the name of the font
364: * @param encoding the encoding of the font
365: * @param embedded true if the font is to be embedded in the PDF
366: * @param size the size of this font
367: * @return the Font constructed based on the parameters
368: */
369:
370: public Font getFont(String fontname, String encoding,
371: boolean embedded, float size) {
372: return getFont(fontname, encoding, embedded, size,
373: Font.UNDEFINED, null);
374: }
375:
376: /**
377: * Constructs a <CODE>Font</CODE>-object.
378: *
379: * @param fontname the name of the font
380: * @param encoding the encoding of the font
381: * @param embedded true if the font is to be embedded in the PDF
382: * @return the Font constructed based on the parameters
383: */
384:
385: public Font getFont(String fontname, String encoding,
386: boolean embedded) {
387: return getFont(fontname, encoding, embedded, Font.UNDEFINED,
388: Font.UNDEFINED, 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: * @param style the style of this font
398: * @param color the <CODE>Color</CODE> of this font.
399: * @return the Font constructed based on the parameters
400: */
401:
402: public Font getFont(String fontname, String encoding, float size,
403: int style, Color color) {
404: return getFont(fontname, encoding, defaultEmbedding, size,
405: style, color);
406: }
407:
408: /**
409: * Constructs a <CODE>Font</CODE>-object.
410: *
411: * @param fontname the name of the font
412: * @param encoding the encoding of the font
413: * @param size the size of this font
414: * @param style the style of this font
415: * @return the Font constructed based on the parameters
416: */
417:
418: public Font getFont(String fontname, String encoding, float size,
419: int style) {
420: return getFont(fontname, encoding, defaultEmbedding, size,
421: style, null);
422: }
423:
424: /**
425: * Constructs a <CODE>Font</CODE>-object.
426: *
427: * @param fontname the name of the font
428: * @param encoding the encoding of the font
429: * @param size the size of this font
430: * @return the Font constructed based on the parameters
431: */
432:
433: public Font getFont(String fontname, String encoding, float size) {
434: return getFont(fontname, encoding, defaultEmbedding, size,
435: Font.UNDEFINED, null);
436: }
437:
438: /**
439: * Constructs a <CODE>Font</CODE>-object.
440: *
441: * @param fontname the name of the font
442: * @param encoding the encoding of the font
443: * @return the Font constructed based on the parameters
444: */
445:
446: public Font getFont(String fontname, String encoding) {
447: return getFont(fontname, encoding, defaultEmbedding,
448: Font.UNDEFINED, Font.UNDEFINED, null);
449: }
450:
451: /**
452: * Constructs a <CODE>Font</CODE>-object.
453: *
454: * @param fontname the name of the font
455: * @param size the size of this font
456: * @param style the style of this font
457: * @param color the <CODE>Color</CODE> of this font.
458: * @return the Font constructed based on the parameters
459: */
460:
461: public Font getFont(String fontname, float size, int style,
462: Color color) {
463: return getFont(fontname, defaultEncoding, defaultEmbedding,
464: size, style, color);
465: }
466:
467: /**
468: * Constructs a <CODE>Font</CODE>-object.
469: *
470: * @param fontname the name of the font
471: * @param size the size of this font
472: * @param style the style of this font
473: * @return the Font constructed based on the parameters
474: */
475:
476: public Font getFont(String fontname, float size, int style) {
477: return getFont(fontname, defaultEncoding, defaultEmbedding,
478: size, style, null);
479: }
480:
481: /**
482: * Constructs a <CODE>Font</CODE>-object.
483: *
484: * @param fontname the name of the font
485: * @param size the size of this font
486: * @return the Font constructed based on the parameters
487: */
488:
489: public Font getFont(String fontname, float size) {
490: return getFont(fontname, defaultEncoding, defaultEmbedding,
491: size, Font.UNDEFINED, null);
492: }
493:
494: /**
495: * Constructs a <CODE>Font</CODE>-object.
496: *
497: * @param fontname the name of the font
498: * @return the Font constructed based on the parameters
499: */
500:
501: public Font getFont(String fontname) {
502: return getFont(fontname, defaultEncoding, defaultEmbedding,
503: Font.UNDEFINED, Font.UNDEFINED, null);
504: }
505:
506: /**
507: * Register a font by giving explicitly the font family and name.
508: * @param familyName the font family
509: * @param fullName the font name
510: * @param path the font path
511: */
512: public void registerFamily(String familyName, String fullName,
513: String path) {
514: if (path != null)
515: trueTypeFonts.setProperty(fullName, path);
516: ArrayList tmp = (ArrayList) fontFamilies.get(familyName);
517: if (tmp == null) {
518: tmp = new ArrayList();
519: tmp.add(fullName);
520: fontFamilies.put(familyName, tmp);
521: } else {
522: int fullNameLength = fullName.length();
523: boolean inserted = false;
524: for (int j = 0; j < tmp.size(); ++j) {
525: if (((String) tmp.get(j)).length() >= fullNameLength) {
526: tmp.add(j, fullName);
527: inserted = true;
528: break;
529: }
530: }
531: if (!inserted)
532: tmp.add(fullName);
533: }
534: }
535:
536: /**
537: * Register a ttf- or a ttc-file.
538: *
539: * @param path the path to a ttf- or ttc-file
540: */
541:
542: public void register(String path) {
543: register(path, null);
544: }
545:
546: /**
547: * Register a font file and use an alias for the font contained in it.
548: *
549: * @param path the path to a font file
550: * @param alias the alias you want to use for the font
551: */
552:
553: public void register(String path, String alias) {
554: try {
555: if (path.toLowerCase().endsWith(".ttf")
556: || path.toLowerCase().endsWith(".otf")
557: || path.toLowerCase().indexOf(".ttc,") > 0) {
558: Object allNames[] = BaseFont.getAllFontNames(path,
559: BaseFont.WINANSI, null);
560: trueTypeFonts.setProperty(((String) allNames[0])
561: .toLowerCase(), path);
562: if (alias != null) {
563: trueTypeFonts
564: .setProperty(alias.toLowerCase(), path);
565: }
566: // register all the font names with all the locales
567: String[][] names = (String[][]) allNames[2]; //full name
568: for (int i = 0; i < names.length; i++) {
569: trueTypeFonts.setProperty(
570: names[i][3].toLowerCase(), path);
571: }
572: String fullName = null;
573: String familyName = null;
574: names = (String[][]) allNames[1]; //family name
575: for (int k = 0; k < TTFamilyOrder.length; k += 3) {
576: for (int i = 0; i < names.length; i++) {
577: if (TTFamilyOrder[k].equals(names[i][0])
578: && TTFamilyOrder[k + 1]
579: .equals(names[i][1])
580: && TTFamilyOrder[k + 2]
581: .equals(names[i][2])) {
582: familyName = names[i][3].toLowerCase();
583: k = TTFamilyOrder.length;
584: break;
585: }
586: }
587: }
588: if (familyName != null) {
589: String lastName = "";
590: names = (String[][]) allNames[2]; //full name
591: for (int i = 0; i < names.length; i++) {
592: for (int k = 0; k < TTFamilyOrder.length; k += 3) {
593: if (TTFamilyOrder[k].equals(names[i][0])
594: && TTFamilyOrder[k + 1]
595: .equals(names[i][1])
596: && TTFamilyOrder[k + 2]
597: .equals(names[i][2])) {
598: fullName = names[i][3];
599: if (fullName.equals(lastName))
600: continue;
601: lastName = fullName;
602: registerFamily(familyName, fullName,
603: null);
604: break;
605: }
606: }
607: }
608: }
609: } else if (path.toLowerCase().endsWith(".ttc")) {
610: if (alias != null)
611: System.err
612: .println("class FontFactory: You can't define an alias for a true type collection.");
613: String[] names = BaseFont.enumerateTTCNames(path);
614: for (int i = 0; i < names.length; i++) {
615: register(path + "," + i);
616: }
617: } else if (path.toLowerCase().endsWith(".afm")
618: || path.toLowerCase().endsWith(".pfm")) {
619: BaseFont bf = BaseFont.createFont(path,
620: BaseFont.CP1252, false);
621: String fullName = (bf.getFullFontName()[0][3])
622: .toLowerCase();
623: String familyName = (bf.getFamilyFontName()[0][3])
624: .toLowerCase();
625: String psName = bf.getPostscriptFontName()
626: .toLowerCase();
627: registerFamily(familyName, fullName, null);
628: trueTypeFonts.setProperty(psName, path);
629: trueTypeFonts.setProperty(fullName, path);
630: }
631: } catch (DocumentException de) {
632: // this shouldn't happen
633: throw new ExceptionConverter(de);
634: } catch (IOException ioe) {
635: throw new ExceptionConverter(ioe);
636: }
637: }
638:
639: /** Register all the fonts in a directory.
640: * @param dir the directory
641: * @return the number of fonts registered
642: */
643: public int registerDirectory(String dir) {
644: int count = 0;
645: try {
646: File file = new File(dir);
647: if (!file.exists() || !file.isDirectory())
648: return 0;
649: String files[] = file.list();
650: if (files == null)
651: return 0;
652: for (int k = 0; k < files.length; ++k) {
653: try {
654: file = new File(dir, files[k]);
655: String name = file.getPath().toLowerCase();
656: if (name.endsWith(".ttf") || name.endsWith(".otf")
657: || name.endsWith(".afm")
658: || name.endsWith(".pfm")
659: || name.endsWith(".ttc")) {
660: register(file.getPath(), null);
661: ++count;
662: }
663: } catch (Exception e) {
664: //empty on purpose
665: }
666: }
667: } catch (Exception e) {
668: //empty on purpose
669: }
670: return count;
671: }
672:
673: /** Register fonts in some probable directories. It usually works in Windows,
674: * Linux and Solaris.
675: * @return the number of fonts registered
676: */
677: public int registerDirectories() {
678: int count = 0;
679: count += registerDirectory("c:/windows/fonts");
680: count += registerDirectory("c:/winnt/fonts");
681: count += registerDirectory("d:/windows/fonts");
682: count += registerDirectory("d:/winnt/fonts");
683: count += registerDirectory("/usr/X/lib/X11/fonts/TrueType");
684: count += registerDirectory("/usr/openwin/lib/X11/fonts/TrueType");
685: count += registerDirectory("/usr/share/fonts/default/TrueType");
686: count += registerDirectory("/usr/X11R6/lib/X11/fonts/ttf");
687: count += registerDirectory("/Library/Fonts");
688: count += registerDirectory("/System/Library/Fonts");
689: return count;
690: }
691:
692: /**
693: * Gets a set of registered fontnames.
694: * @return a set of registered fonts
695: */
696:
697: public Set getRegisteredFonts() {
698: return Utilities.getKeySet(trueTypeFonts);
699: }
700:
701: /**
702: * Gets a set of registered fontnames.
703: * @return a set of registered font families
704: */
705:
706: public Set getRegisteredFamilies() {
707: return Utilities.getKeySet(fontFamilies);
708: }
709:
710: /**
711: * Checks if a certain font is registered.
712: *
713: * @param fontname the name of the font that has to be checked.
714: * @return true if the font is found
715: */
716: public boolean isRegistered(String fontname) {
717: return trueTypeFonts.containsKey(fontname.toLowerCase());
718: }
719: }
|