1 /*
2 * $Id: FontFactory.java 3373 2008-05-12 16:21:24Z xlv $
3 *
4 * Copyright 2002 by Bruno Lowagie.
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * (the "License"); you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the License.
13 *
14 * The Original Code is 'iText, a free JAVA-PDF library'.
15 *
16 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18 * All Rights Reserved.
19 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21 *
22 * Contributor(s): all the names of the contributors are added in the source code
23 * where applicable.
24 *
25 * Alternatively, the contents of this file may be used under the terms of the
26 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27 * provisions of LGPL are applicable instead of those above. If you wish to
28 * allow use of your version of this file only under the terms of the LGPL
29 * License and not to allow others to use your version of this file under
30 * the MPL, indicate your decision by deleting the provisions above and
31 * replace them with the notice and other provisions required by the LGPL.
32 * If you do not delete the provisions above, a recipient may use your version
33 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34 *
35 * This library is free software; you can redistribute it and/or modify it
36 * under the terms of the MPL as stated above or under the terms of the GNU
37 * Library General Public License as published by the Free Software Foundation;
38 * either version 2 of the License, or any later version.
39 *
40 * This library is distributed in the hope that it will be useful, but WITHOUT
41 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43 * details.
44 *
45 * If you didn't download this code from the following link, you should check if
46 * you aren't using an obsolete version:
47 * http://www.lowagie.com/iText/
48 */
49
50 package com.lowagie.text;
51
52 import java.awt.Color;
53 import java.util.Properties;
54 import java.util.Set;
55
56 import com.lowagie.text.pdf.BaseFont;
57
58 /**
59 * If you are using True Type fonts, you can declare the paths of the different ttf- and ttc-files
60 * to this static class first and then create fonts in your code using one of the static getFont-method
61 * without having to enter a path as parameter.
62 *
63 * @author Bruno Lowagie
64 */
65
66 public final class FontFactory {
67
68 /** This is a possible value of a base 14 type 1 font */
69 public static final String COURIER = BaseFont.COURIER;
70
71 /** This is a possible value of a base 14 type 1 font */
72 public static final String COURIER_BOLD = BaseFont.COURIER_BOLD;
73
74 /** This is a possible value of a base 14 type 1 font */
75 public static final String COURIER_OBLIQUE = BaseFont.COURIER_OBLIQUE;
76
77 /** This is a possible value of a base 14 type 1 font */
78 public static final String COURIER_BOLDOBLIQUE = BaseFont.COURIER_BOLDOBLIQUE;
79
80 /** This is a possible value of a base 14 type 1 font */
81 public static final String HELVETICA = BaseFont.HELVETICA;
82
83 /** This is a possible value of a base 14 type 1 font */
84 public static final String HELVETICA_BOLD = BaseFont.HELVETICA_BOLD;
85
86 /** This is a possible value of a base 14 type 1 font */
87 public static final String HELVETICA_OBLIQUE = BaseFont.HELVETICA_OBLIQUE;
88
89 /** This is a possible value of a base 14 type 1 font */
90 public static final String HELVETICA_BOLDOBLIQUE = BaseFont.HELVETICA_BOLDOBLIQUE;
91
92 /** This is a possible value of a base 14 type 1 font */
93 public static final String SYMBOL = BaseFont.SYMBOL;
94
95 /** This is a possible value of a base 14 type 1 font */
96 public static final String TIMES = "Times";
97
98 /** This is a possible value of a base 14 type 1 font */
99 public static final String TIMES_ROMAN = BaseFont.TIMES_ROMAN;
100
101 /** This is a possible value of a base 14 type 1 font */
102 public static final String TIMES_BOLD = BaseFont.TIMES_BOLD;
103
104 /** This is a possible value of a base 14 type 1 font */
105 public static final String TIMES_ITALIC = BaseFont.TIMES_ITALIC;
106
107 /** This is a possible value of a base 14 type 1 font */
108 public static final String TIMES_BOLDITALIC = BaseFont.TIMES_BOLDITALIC;
109
110 /** This is a possible value of a base 14 type 1 font */
111 public static final String ZAPFDINGBATS = BaseFont.ZAPFDINGBATS;
112
113 private static FontFactoryImp fontImp = new FontFactoryImp();
114
115 /** This is the default encoding to use. */
116 public static String defaultEncoding = BaseFont.WINANSI;
117
118 /** This is the default value of the <VAR>embedded</VAR> variable. */
119 public static boolean defaultEmbedding = BaseFont.NOT_EMBEDDED;
120
121 /** Creates new FontFactory */
122 private FontFactory() {
123 }
124
125 /**
126 * Constructs a <CODE>Font</CODE>-object.
127 *
128 * @param fontname the name of the font
129 * @param encoding the encoding of the font
130 * @param embedded true if the font is to be embedded in the PDF
131 * @param size the size of this font
132 * @param style the style of this font
133 * @param color the <CODE>Color</CODE> of this font.
134 * @return the Font constructed based on the parameters
135 */
136
137 public static Font getFont(String fontname, String encoding, boolean embedded, float size, int style, Color color) {
138 return fontImp.getFont(fontname, encoding, embedded, size, style, color);
139 }
140
141 /**
142 * Constructs a <CODE>Font</CODE>-object.
143 *
144 * @param fontname the name of the font
145 * @param encoding the encoding of the font
146 * @param embedded true if the font is to be embedded in the PDF
147 * @param size the size of this font
148 * @param style the style of this font
149 * @param color the <CODE>Color</CODE> of this font.
150 * @param cached true if the font comes from the cache or is added to
151 * the cache if new, false if the font is always created new
152 * @return the Font constructed based on the parameters
153 */
154
155 public static Font getFont(String fontname, String encoding, boolean embedded, float size, int style, Color color, boolean cached) {
156 return fontImp.getFont(fontname, encoding, embedded, size, style, color, cached);
157 }
158
159 /**
160 * Constructs a <CODE>Font</CODE>-object.
161 *
162 * @param attributes the attributes of a <CODE>Font</CODE> object.
163 * @return the Font constructed based on the attributes
164 */
165
166 public static Font getFont(Properties attributes) {
167 fontImp.defaultEmbedding = defaultEmbedding;
168 fontImp.defaultEncoding = defaultEncoding;
169 return fontImp.getFont(attributes);
170 }
171
172 /**
173 * Constructs a <CODE>Font</CODE>-object.
174 *
175 * @param fontname the name of the font
176 * @param encoding the encoding of the font
177 * @param embedded true if the font is to be embedded in the PDF
178 * @param size the size of this font
179 * @param style the style of this font
180 * @return the Font constructed based on the parameters
181 */
182
183 public static Font getFont(String fontname, String encoding, boolean embedded, float size, int style) {
184 return getFont(fontname, encoding, embedded, size, style, null);
185 }
186
187 /**
188 * Constructs a <CODE>Font</CODE>-object.
189 *
190 * @param fontname the name of the font
191 * @param encoding the encoding of the font
192 * @param embedded true if the font is to be embedded in the PDF
193 * @param size the size of this font
194 * @return the Font constructed based on the parameters
195 */
196
197 public static Font getFont(String fontname, String encoding, boolean embedded, float size) {
198 return getFont(fontname, encoding, embedded, size, Font.UNDEFINED, null);
199 }
200
201 /**
202 * Constructs a <CODE>Font</CODE>-object.
203 *
204 * @param fontname the name of the font
205 * @param encoding the encoding of the font
206 * @param embedded true if the font is to be embedded in the PDF
207 * @return the Font constructed based on the parameters
208 */
209
210 public static Font getFont(String fontname, String encoding, boolean embedded) {
211 return getFont(fontname, encoding, embedded, Font.UNDEFINED, Font.UNDEFINED, null);
212 }
213
214 /**
215 * Constructs a <CODE>Font</CODE>-object.
216 *
217 * @param fontname the name of the font
218 * @param encoding the encoding of the font
219 * @param size the size of this font
220 * @param style the style of this font
221 * @param color the <CODE>Color</CODE> of this font.
222 * @return the Font constructed based on the parameters
223 */
224
225 public static Font getFont(String fontname, String encoding, float size, int style, Color color) {
226 return getFont(fontname, encoding, defaultEmbedding, size, style, color);
227 }
228
229 /**
230 * Constructs a <CODE>Font</CODE>-object.
231 *
232 * @param fontname the name of the font
233 * @param encoding the encoding of the font
234 * @param size the size of this font
235 * @param style the style of this font
236 * @return the Font constructed based on the parameters
237 */
238
239 public static Font getFont(String fontname, String encoding, float size, int style) {
240 return getFont(fontname, encoding, defaultEmbedding, size, style, null);
241 }
242
243 /**
244 * Constructs a <CODE>Font</CODE>-object.
245 *
246 * @param fontname the name of the font
247 * @param encoding the encoding of the font
248 * @param size the size of this font
249 * @return the Font constructed based on the parameters
250 */
251
252 public static Font getFont(String fontname, String encoding, float size) {
253 return getFont(fontname, encoding, defaultEmbedding, size, Font.UNDEFINED, null);
254 }
255
256 /**
257 * Constructs a <CODE>Font</CODE>-object.
258 *
259 * @param fontname the name of the font
260 * @param encoding the encoding of the font
261 * @return the Font constructed based on the parameters
262 */
263
264 public static Font getFont(String fontname, String encoding) {
265 return getFont(fontname, encoding, defaultEmbedding, Font.UNDEFINED, Font.UNDEFINED, null);
266 }
267
268 /**
269 * Constructs a <CODE>Font</CODE>-object.
270 *
271 * @param fontname the name of the font
272 * @param size the size of this font
273 * @param style the style of this font
274 * @param color the <CODE>Color</CODE> of this font.
275 * @return the Font constructed based on the parameters
276 */
277
278 public static Font getFont(String fontname, float size, int style, Color color) {
279 return getFont(fontname, defaultEncoding, defaultEmbedding, size, style, color);
280 }
281
282 /**
283 * Constructs a <CODE>Font</CODE>-object.
284 *
285 * @param fontname the name of the font
286 * @param size the size of this font
287 * @param color the <CODE>Color</CODE> of this font.
288 * @return the Font constructed based on the parameters
289 * @since 2.1.0
290 */
291
292 public static Font getFont(String fontname, float size, Color color) {
293 return getFont(fontname, defaultEncoding, defaultEmbedding, size, Font.UNDEFINED, color);
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 * @param style the style of this font
302 * @return the Font constructed based on the parameters
303 */
304
305 public static Font getFont(String fontname, float size, int style) {
306 return getFont(fontname, defaultEncoding, defaultEmbedding, size, style, null);
307 }
308
309 /**
310 * Constructs a <CODE>Font</CODE>-object.
311 *
312 * @param fontname the name of the font
313 * @param size the size of this font
314 * @return the Font constructed based on the parameters
315 */
316
317 public static Font getFont(String fontname, float size) {
318 return getFont(fontname, defaultEncoding, defaultEmbedding, size, Font.UNDEFINED, null);
319 }
320
321 /**
322 * Constructs a <CODE>Font</CODE>-object.
323 *
324 * @param fontname the name of the font
325 * @return the Font constructed based on the parameters
326 */
327
328 public static Font getFont(String fontname) {
329 return getFont(fontname, defaultEncoding, defaultEmbedding, Font.UNDEFINED, Font.UNDEFINED, null);
330 }
331
332 /**
333 * Register a font by giving explicitly the font family and name.
334 * @param familyName the font family
335 * @param fullName the font name
336 * @param path the font path
337 */
338 public void registerFamily(String familyName, String fullName, String path) {
339 fontImp.registerFamily(familyName, fullName, path);
340 }
341
342 /**
343 * Register a ttf- or a ttc-file.
344 *
345 * @param path the path to a ttf- or ttc-file
346 */
347
348 public static void register(String path) {
349 register(path, null);
350 }
351
352 /**
353 * Register a font file and use an alias for the font contained in it.
354 *
355 * @param path the path to a font file
356 * @param alias the alias you want to use for the font
357 */
358
359 public static void register(String path, String alias) {
360 fontImp.register(path, alias);
361 }
362
363 /** Register all the fonts in a directory.
364 * @param dir the directory
365 * @return the number of fonts registered
366 */
367 public static int registerDirectory(String dir) {
368 return fontImp.registerDirectory(dir);
369 }
370
371 /**
372 * Register all the fonts in a directory and possibly its subdirectories.
373 * @param dir the directory
374 * @param scanSubdirectories recursively scan subdirectories if <code>true</true>
375 * @return the number of fonts registered
376 * @since 2.1.2
377 */
378 public static int registerDirectory(String dir, boolean scanSubdirectories) {
379 return fontImp.registerDirectory(dir, scanSubdirectories);
380 }
381
382 /** Register fonts in some probable directories. It usually works in Windows,
383 * Linux and Solaris.
384 * @return the number of fonts registered
385 */
386 public static int registerDirectories() {
387 return fontImp.registerDirectories();
388 }
389
390 /**
391 * Gets a set of registered fontnames.
392 * @return a set of registered fonts
393 */
394
395 public static Set getRegisteredFonts() {
396 return fontImp.getRegisteredFonts();
397 }
398
399 /**
400 * Gets a set of registered fontnames.
401 * @return a set of registered font families
402 */
403
404 public static Set getRegisteredFamilies() {
405 return fontImp.getRegisteredFamilies();
406 }
407
408 /**
409 * Gets a set of registered fontnames.
410 * @param fontname of a font that may or may not be registered
411 * @return true if a given font is registered
412 */
413
414 public static boolean contains(String fontname) {
415 return fontImp.isRegistered(fontname);
416 }
417
418 /**
419 * Checks if a certain font is registered.
420 *
421 * @param fontname the name of the font that has to be checked.
422 * @return true if the font is found
423 */
424
425 public static boolean isRegistered(String fontname) {
426 return fontImp.isRegistered(fontname);
427 }
428
429 /**
430 * Gets the font factory implementation.
431 * @return the font factory implementation
432 */
433 public static FontFactoryImp getFontImp() {
434 return fontImp;
435 }
436
437 /**
438 * Sets the font factory implementation.
439 * @param fontImp the font factory implementation
440 */
441 public static void setFontImp(FontFactoryImp fontImp) {
442 if (fontImp == null)
443 throw new NullPointerException("FontFactoryImp cannot be null.");
444 FontFactory.fontImp = fontImp;
445 }
446 }