KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: FontFactory.java 2366 2006-09-14 23:10:58Z 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.util.Properties JavaDoc;
55 import java.util.Set JavaDoc;
56
57 import com.lowagie.text.pdf.BaseFont;
58
59 /**
60  * If you are using True Type fonts, you can declare the paths of the different ttf- and ttc-files
61  * to this static class first and then create fonts in your code using one of the static getFont-method
62  * without having to enter a path as parameter.
63  *
64  * @author Bruno Lowagie
65  */

66
67 public class FontFactory {
68     
69 /** This is a possible value of a base 14 type 1 font */
70     public static final String JavaDoc COURIER = BaseFont.COURIER;
71     
72 /** This is a possible value of a base 14 type 1 font */
73     public static final String JavaDoc COURIER_BOLD = BaseFont.COURIER_BOLD;
74     
75 /** This is a possible value of a base 14 type 1 font */
76     public static final String JavaDoc COURIER_OBLIQUE = BaseFont.COURIER_OBLIQUE;
77     
78 /** This is a possible value of a base 14 type 1 font */
79     public static final String JavaDoc COURIER_BOLDOBLIQUE = BaseFont.COURIER_BOLDOBLIQUE;
80     
81 /** This is a possible value of a base 14 type 1 font */
82     public static final String JavaDoc HELVETICA = BaseFont.HELVETICA;
83     
84 /** This is a possible value of a base 14 type 1 font */
85     public static final String JavaDoc HELVETICA_BOLD = BaseFont.HELVETICA_BOLD;
86     
87 /** This is a possible value of a base 14 type 1 font */
88     public static final String JavaDoc HELVETICA_OBLIQUE = BaseFont.HELVETICA_OBLIQUE;
89     
90 /** This is a possible value of a base 14 type 1 font */
91     public static final String JavaDoc HELVETICA_BOLDOBLIQUE = BaseFont.HELVETICA_BOLDOBLIQUE;
92     
93 /** This is a possible value of a base 14 type 1 font */
94     public static final String JavaDoc SYMBOL = BaseFont.SYMBOL;
95     
96 /** This is a possible value of a base 14 type 1 font */
97     public static final String JavaDoc TIMES = "Times";
98     
99 /** This is a possible value of a base 14 type 1 font */
100     public static final String JavaDoc TIMES_ROMAN = BaseFont.TIMES_ROMAN;
101     
102 /** This is a possible value of a base 14 type 1 font */
103     public static final String JavaDoc TIMES_BOLD = BaseFont.TIMES_BOLD;
104     
105 /** This is a possible value of a base 14 type 1 font */
106     public static final String JavaDoc TIMES_ITALIC = BaseFont.TIMES_ITALIC;
107     
108 /** This is a possible value of a base 14 type 1 font */
109     public static final String JavaDoc TIMES_BOLDITALIC = BaseFont.TIMES_BOLDITALIC;
110     
111 /** This is a possible value of a base 14 type 1 font */
112     public static final String JavaDoc ZAPFDINGBATS = BaseFont.ZAPFDINGBATS;
113     
114     private static FontFactoryImp fontImp = new FontFactoryImp();
115     
116 /** This is the default encoding to use. */
117     public static String JavaDoc defaultEncoding = BaseFont.WINANSI;
118     
119 /** This is the default value of the <VAR>embedded</VAR> variable. */
120     public static boolean defaultEmbedding = BaseFont.NOT_EMBEDDED;
121     
122 /** Creates new FontFactory */
123     private FontFactory() {
124     }
125     
126 /**
127  * Constructs a <CODE>Font</CODE>-object.
128  *
129  * @param fontname the name of the font
130  * @param encoding the encoding of the font
131  * @param embedded true if the font is to be embedded in the PDF
132  * @param size the size of this font
133  * @param style the style of this font
134  * @param color the <CODE>Color</CODE> of this font.
135  * @return the Font constructed based on the parameters
136  */

137     
138     public static Font getFont(String JavaDoc fontname, String JavaDoc encoding, boolean embedded, float size, int style, Color JavaDoc color) {
139         return fontImp.getFont(fontname, encoding, embedded, size, style, color);
140     }
141     
142 /**
143  * Constructs a <CODE>Font</CODE>-object.
144  *
145  * @param fontname the name of the font
146  * @param encoding the encoding of the font
147  * @param embedded true if the font is to be embedded in the PDF
148  * @param size the size of this font
149  * @param style the style of this font
150  * @param color the <CODE>Color</CODE> of this font.
151  * @param cached true if the font comes from the cache or is added to
152  * the cache if new, false if the font is always created new
153  * @return the Font constructed based on the parameters
154  */

155     
156     public static Font getFont(String JavaDoc fontname, String JavaDoc encoding, boolean embedded, float size, int style, Color JavaDoc color, boolean cached) {
157         return fontImp.getFont(fontname, encoding, embedded, size, style, color, cached);
158     }
159     
160 /**
161  * Constructs a <CODE>Font</CODE>-object.
162  *
163  * @param attributes the attributes of a <CODE>Font</CODE> object.
164  * @return the Font constructed based on the attributes
165  */

166     
167     public static Font getFont(Properties JavaDoc attributes) {
168         fontImp.defaultEmbedding = defaultEmbedding;
169         fontImp.defaultEncoding = defaultEncoding;
170         return fontImp.getFont(attributes);
171     }
172     
173 /**
174  * Constructs a <CODE>Font</CODE>-object.
175  *
176  * @param fontname the name of the font
177  * @param encoding the encoding of the font
178  * @param embedded true if the font is to be embedded in the PDF
179  * @param size the size of this font
180  * @param style the style of this font
181  * @return the Font constructed based on the parameters
182  */

183     
184     public static Font getFont(String JavaDoc fontname, String JavaDoc encoding, boolean embedded, float size, int style) {
185         return getFont(fontname, encoding, embedded, size, style, null);
186     }
187     
188 /**
189  * Constructs a <CODE>Font</CODE>-object.
190  *
191  * @param fontname the name of the font
192  * @param encoding the encoding of the font
193  * @param embedded true if the font is to be embedded in the PDF
194  * @param size the size of this font
195  * @return the Font constructed based on the parameters
196  */

197     
198     public static Font getFont(String JavaDoc fontname, String JavaDoc encoding, boolean embedded, float size) {
199         return getFont(fontname, encoding, embedded, size, Font.UNDEFINED, null);
200     }
201     
202 /**
203  * Constructs a <CODE>Font</CODE>-object.
204  *
205  * @param fontname the name of the font
206  * @param encoding the encoding of the font
207  * @param embedded true if the font is to be embedded in the PDF
208  * @return the Font constructed based on the parameters
209  */

210     
211     public static Font getFont(String JavaDoc fontname, String JavaDoc encoding, boolean embedded) {
212         return getFont(fontname, encoding, embedded, Font.UNDEFINED, Font.UNDEFINED, null);
213     }
214     
215 /**
216  * Constructs a <CODE>Font</CODE>-object.
217  *
218  * @param fontname the name of the font
219  * @param encoding the encoding of the font
220  * @param size the size of this font
221  * @param style the style of this font
222  * @param color the <CODE>Color</CODE> of this font.
223  * @return the Font constructed based on the parameters
224  */

225     
226     public static Font getFont(String JavaDoc fontname, String JavaDoc encoding, float size, int style, Color JavaDoc color) {
227         return getFont(fontname, encoding, defaultEmbedding, size, style, color);
228     }
229     
230 /**
231  * Constructs a <CODE>Font</CODE>-object.
232  *
233  * @param fontname the name of the font
234  * @param encoding the encoding of the font
235  * @param size the size of this font
236  * @param style the style of this font
237  * @return the Font constructed based on the parameters
238  */

239     
240     public static Font getFont(String JavaDoc fontname, String JavaDoc encoding, float size, int style) {
241         return getFont(fontname, encoding, defaultEmbedding, size, style, null);
242     }
243     
244 /**
245  * Constructs a <CODE>Font</CODE>-object.
246  *
247  * @param fontname the name of the font
248  * @param encoding the encoding of the font
249  * @param size the size of this font
250  * @return the Font constructed based on the parameters
251  */

252     
253     public static Font getFont(String JavaDoc fontname, String JavaDoc encoding, float size) {
254         return getFont(fontname, encoding, defaultEmbedding, size, Font.UNDEFINED, null);
255     }
256     
257 /**
258  * Constructs a <CODE>Font</CODE>-object.
259  *
260  * @param fontname the name of the font
261  * @param encoding the encoding of the font
262  * @return the Font constructed based on the parameters
263  */

264     
265     public static Font getFont(String JavaDoc fontname, String JavaDoc encoding) {
266         return getFont(fontname, encoding, defaultEmbedding, Font.UNDEFINED, Font.UNDEFINED, null);
267     }
268     
269 /**
270  * Constructs a <CODE>Font</CODE>-object.
271  *
272  * @param fontname the name of the font
273  * @param size the size of this font
274  * @param style the style of this font
275  * @param color the <CODE>Color</CODE> of this font.
276  * @return the Font constructed based on the parameters
277  */

278     
279     public static Font getFont(String JavaDoc fontname, float size, int style, Color JavaDoc color) {
280         return getFont(fontname, defaultEncoding, defaultEmbedding, size, style, color);
281     }
282     
283 /**
284  * Constructs a <CODE>Font</CODE>-object.
285  *
286  * @param fontname the name of the font
287  * @param size the size of this font
288  * @param style the style of this font
289  * @return the Font constructed based on the parameters
290  */

291     
292     public static Font getFont(String JavaDoc fontname, float size, int style) {
293         return getFont(fontname, defaultEncoding, defaultEmbedding, size, style, null);
294     }
295     
296 /**
297  * Constructs a <CODE>Font</CODE>-object.
298  *
299  * @param fontname the name of the font
300  * @param size the size of this font
301  * @return the Font constructed based on the parameters
302  */

303     
304     public static Font getFont(String JavaDoc fontname, float size) {
305         return getFont(fontname, defaultEncoding, defaultEmbedding, size, Font.UNDEFINED, null);
306     }
307     
308 /**
309  * Constructs a <CODE>Font</CODE>-object.
310  *
311  * @param fontname the name of the font
312  * @return the Font constructed based on the parameters
313  */

314     
315     public static Font getFont(String JavaDoc fontname) {
316         return getFont(fontname, defaultEncoding, defaultEmbedding, Font.UNDEFINED, Font.UNDEFINED, null);
317     }
318     
319     /**
320      * Register a font by giving explicitly the font family and name.
321      * @param familyName the font family
322      * @param fullName the font name
323      * @param path the font path
324      */

325     public void registerFamily(String JavaDoc familyName, String JavaDoc fullName, String JavaDoc path) {
326         fontImp.registerFamily(familyName, fullName, path);
327     }
328     
329 /**
330  * Register a ttf- or a ttc-file.
331  *
332  * @param path the path to a ttf- or ttc-file
333  */

334     
335     public static void register(String JavaDoc path) {
336         register(path, null);
337     }
338     
339 /**
340  * Register a font file and use an alias for the font contained in it.
341  *
342  * @param path the path to a font file
343  * @param alias the alias you want to use for the font
344  */

345     
346     public static void register(String JavaDoc path, String JavaDoc alias) {
347         fontImp.register(path, alias);
348     }
349
350     /** Register all the fonts in a directory.
351      * @param dir the directory
352      * @return the number of fonts registered
353      */

354     public static int registerDirectory(String JavaDoc dir) {
355         return fontImp.registerDirectory(dir);
356     }
357
358     /** Register fonts in some probable directories. It usually works in Windows,
359      * Linux and Solaris.
360      * @return the number of fonts registered
361      */

362     public static int registerDirectories() {
363         return fontImp.registerDirectories();
364     }
365
366 /**
367  * Gets a set of registered fontnames.
368  * @return a set of registered fonts
369  */

370     
371     public static Set JavaDoc getRegisteredFonts() {
372         return fontImp.getRegisteredFonts();
373     }
374     
375 /**
376  * Gets a set of registered fontnames.
377  * @return a set of registered font families
378  */

379     
380     public static Set JavaDoc getRegisteredFamilies() {
381         return fontImp.getRegisteredFamilies();
382     }
383     
384 /**
385  * Gets a set of registered fontnames.
386  * @param fontname of a font that may or may not be registered
387  * @return true if a given font is registered
388  */

389     
390     public static boolean contains(String JavaDoc fontname) {
391         return fontImp.isRegistered(fontname);
392     }
393     
394 /**
395  * Checks if a certain font is registered.
396  *
397  * @param fontname the name of the font that has to be checked.
398  * @return true if the font is found
399  */

400     
401     public static boolean isRegistered(String JavaDoc fontname) {
402         return fontImp.isRegistered(fontname);
403     }
404     
405     /**
406      * Gets the font factory implementation.
407      * @return the font factory implementation
408      */

409     public static FontFactoryImp getFontImp() {
410         return fontImp;
411     }
412     
413     /**
414      * Sets the font factory implementation.
415      * @param fontImp the font factory implementation
416      */

417     public static void setFontImp(FontFactoryImp fontImp) {
418         if (fontImp == null)
419             throw new NullPointerException JavaDoc("FontFactoryImp cannot be null.");
420         FontFactory.fontImp = fontImp;
421     }
422 }
Popular Tags