001/* 002 * $Id: FontFactory.java 4847 2011-05-05 19:46:13Z redlab_b $ 003 * 004 * This file is part of the iText (R) project. 005 * Copyright (c) 1998-2011 1T3XT BVBA 006 * Authors: Bruno Lowagie, Paulo Soares, et al. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Affero General Public License version 3 010 * as published by the Free Software Foundation with the addition of the 011 * following permission added to Section 15 as permitted in Section 7(a): 012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT, 013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 014 * 015 * This program is distributed in the hope that it will be useful, but 016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 017 * or FITNESS FOR A PARTICULAR PURPOSE. 018 * See the GNU Affero General Public License for more details. 019 * You should have received a copy of the GNU Affero General Public License 020 * along with this program; if not, see http://www.gnu.org/licenses or write to 021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 022 * Boston, MA, 02110-1301 USA, or download the license from the following URL: 023 * http://itextpdf.com/terms-of-use/ 024 * 025 * The interactive user interfaces in modified source and object code versions 026 * of this program must display Appropriate Legal Notices, as required under 027 * Section 5 of the GNU Affero General Public License. 028 * 029 * In accordance with Section 7(b) of the GNU Affero General Public License, 030 * a covered work must retain the producer line in every PDF that is created 031 * or manipulated using iText. 032 * 033 * You can be released from the requirements of the license by purchasing 034 * a commercial license. Buying such a license is mandatory as soon as you 035 * develop commercial activities involving the iText software without 036 * disclosing the source code of your own applications. 037 * These activities include: offering paid services to customers as an ASP, 038 * serving PDFs on the fly in a web application, shipping iText with a closed 039 * source product. 040 * 041 * For more information, please contact iText Software Corp. at this 042 * address: sales@itextpdf.com 043 */ 044package com.itextpdf.text; 045 046import java.util.Set; 047 048import com.itextpdf.text.error_messages.MessageLocalization; 049import com.itextpdf.text.pdf.BaseFont; 050 051/** 052 * If you are using True Type fonts, you can declare the paths of the different ttf- and ttc-files 053 * to this static class first and then create fonts in your code using one of the static getFont-method 054 * without having to enter a path as parameter. 055 * 056 * @author Bruno Lowagie 057 */ 058 059public final class FontFactory { 060 061/** This is a possible value of a base 14 type 1 font */ 062 public static final String COURIER = BaseFont.COURIER; 063 064/** This is a possible value of a base 14 type 1 font */ 065 public static final String COURIER_BOLD = BaseFont.COURIER_BOLD; 066 067/** This is a possible value of a base 14 type 1 font */ 068 public static final String COURIER_OBLIQUE = BaseFont.COURIER_OBLIQUE; 069 070/** This is a possible value of a base 14 type 1 font */ 071 public static final String COURIER_BOLDOBLIQUE = BaseFont.COURIER_BOLDOBLIQUE; 072 073/** This is a possible value of a base 14 type 1 font */ 074 public static final String HELVETICA = BaseFont.HELVETICA; 075 076/** This is a possible value of a base 14 type 1 font */ 077 public static final String HELVETICA_BOLD = BaseFont.HELVETICA_BOLD; 078 079/** This is a possible value of a base 14 type 1 font */ 080 public static final String HELVETICA_OBLIQUE = BaseFont.HELVETICA_OBLIQUE; 081 082/** This is a possible value of a base 14 type 1 font */ 083 public static final String HELVETICA_BOLDOBLIQUE = BaseFont.HELVETICA_BOLDOBLIQUE; 084 085/** This is a possible value of a base 14 type 1 font */ 086 public static final String SYMBOL = BaseFont.SYMBOL; 087 088/** This is a possible value of a base 14 type 1 font */ 089 public static final String TIMES = "Times"; 090 091/** This is a possible value of a base 14 type 1 font */ 092 public static final String TIMES_ROMAN = BaseFont.TIMES_ROMAN; 093 094/** This is a possible value of a base 14 type 1 font */ 095 public static final String TIMES_BOLD = BaseFont.TIMES_BOLD; 096 097/** This is a possible value of a base 14 type 1 font */ 098 public static final String TIMES_ITALIC = BaseFont.TIMES_ITALIC; 099 100/** This is a possible value of a base 14 type 1 font */ 101 public static final String TIMES_BOLDITALIC = BaseFont.TIMES_BOLDITALIC; 102 103/** This is a possible value of a base 14 type 1 font */ 104 public static final String ZAPFDINGBATS = BaseFont.ZAPFDINGBATS; 105 106 private static FontFactoryImp fontImp = new FontFactoryImp(); 107 108/** This is the default encoding to use. */ 109 public static String defaultEncoding = BaseFont.WINANSI; 110 111/** This is the default value of the <VAR>embedded</VAR> variable. */ 112 public static boolean defaultEmbedding = BaseFont.NOT_EMBEDDED; 113 114/** Creates new FontFactory */ 115 private FontFactory() { 116 } 117 118/** 119 * Constructs a <CODE>Font</CODE>-object. 120 * 121 * @param fontname the name of the font 122 * @param encoding the encoding of the font 123 * @param embedded true if the font is to be embedded in the PDF 124 * @param size the size of this font 125 * @param style the style of this font 126 * @param color the <CODE>BaseColor</CODE> of this font. 127 * @return the Font constructed based on the parameters 128 */ 129 130 public static Font getFont(final String fontname, final String encoding, final boolean embedded, final float size, final int style, final BaseColor color) { 131 return fontImp.getFont(fontname, encoding, embedded, size, style, color); 132 } 133 134/** 135 * Constructs a <CODE>Font</CODE>-object. 136 * 137 * @param fontname the name of the font 138 * @param encoding the encoding of the font 139 * @param embedded true if the font is to be embedded in the PDF 140 * @param size the size of this font 141 * @param style the style of this font 142 * @param color the <CODE>BaseColor</CODE> of this font. 143 * @param cached true if the font comes from the cache or is added to 144 * the cache if new, false if the font is always created new 145 * @return the Font constructed based on the parameters 146 */ 147 148 public static Font getFont(final String fontname, final String encoding, final boolean embedded, final float size, final int style, final BaseColor color, final boolean cached) { 149 return fontImp.getFont(fontname, encoding, embedded, size, style, color, cached); 150 } 151 152/** 153 * Constructs a <CODE>Font</CODE>-object. 154 * 155 * @param fontname the name of the font 156 * @param encoding the encoding of the font 157 * @param embedded true if the font is to be embedded in the PDF 158 * @param size the size of this font 159 * @param style the style of this font 160 * @return the Font constructed based on the parameters 161 */ 162 163 public static Font getFont(final String fontname, final String encoding, final boolean embedded, final float size, final int style) { 164 return getFont(fontname, encoding, embedded, size, style, null); 165 } 166 167/** 168 * Constructs a <CODE>Font</CODE>-object. 169 * 170 * @param fontname the name of the font 171 * @param encoding the encoding of the font 172 * @param embedded true if the font is to be embedded in the PDF 173 * @param size the size of this font 174 * @return the Font constructed based on the parameters 175 */ 176 177 public static Font getFont(final String fontname, final String encoding, final boolean embedded, final float size) { 178 return getFont(fontname, encoding, embedded, size, Font.UNDEFINED, null); 179 } 180 181/** 182 * Constructs a <CODE>Font</CODE>-object. 183 * 184 * @param fontname the name of the font 185 * @param encoding the encoding of the font 186 * @param embedded true if the font is to be embedded in the PDF 187 * @return the Font constructed based on the parameters 188 */ 189 190 public static Font getFont(final String fontname, final String encoding, final boolean embedded) { 191 return getFont(fontname, encoding, embedded, Font.UNDEFINED, Font.UNDEFINED, null); 192 } 193 194/** 195 * Constructs a <CODE>Font</CODE>-object. 196 * 197 * @param fontname the name of the font 198 * @param encoding the encoding of the font 199 * @param size the size of this font 200 * @param style the style of this font 201 * @param color the <CODE>BaseColor</CODE> of this font. 202 * @return the Font constructed based on the parameters 203 */ 204 205 public static Font getFont(final String fontname, final String encoding, final float size, final int style, final BaseColor color) { 206 return getFont(fontname, encoding, defaultEmbedding, size, style, color); 207 } 208 209/** 210 * Constructs a <CODE>Font</CODE>-object. 211 * 212 * @param fontname the name of the font 213 * @param encoding the encoding of the font 214 * @param size the size of this font 215 * @param style the style of this font 216 * @return the Font constructed based on the parameters 217 */ 218 219 public static Font getFont(final String fontname, final String encoding, final float size, final int style) { 220 return getFont(fontname, encoding, defaultEmbedding, size, style, null); 221 } 222 223/** 224 * Constructs a <CODE>Font</CODE>-object. 225 * 226 * @param fontname the name of the font 227 * @param encoding the encoding of the font 228 * @param size the size of this font 229 * @return the Font constructed based on the parameters 230 */ 231 232 public static Font getFont(final String fontname, final String encoding, final float size) { 233 return getFont(fontname, encoding, defaultEmbedding, size, Font.UNDEFINED, null); 234 } 235 236/** 237 * Constructs a <CODE>Font</CODE>-object. 238 * 239 * @param fontname the name of the font 240 * @param encoding the encoding of the font 241 * @return the Font constructed based on the parameters 242 */ 243 244 public static Font getFont(final String fontname, final String encoding) { 245 return getFont(fontname, encoding, defaultEmbedding, Font.UNDEFINED, Font.UNDEFINED, null); 246 } 247 248/** 249 * Constructs a <CODE>Font</CODE>-object. 250 * 251 * @param fontname the name of the font 252 * @param size the size of this font 253 * @param style the style of this font 254 * @param color the <CODE>BaseColor</CODE> of this font. 255 * @return the Font constructed based on the parameters 256 */ 257 258 public static Font getFont(final String fontname, final float size, final int style, final BaseColor color) { 259 return getFont(fontname, defaultEncoding, defaultEmbedding, size, style, color); 260 } 261 262/** 263 * Constructs a <CODE>Font</CODE>-object. 264 * 265 * @param fontname the name of the font 266 * @param size the size of this font 267 * @param color the <CODE>BaseColor</CODE> of this font. 268 * @return the Font constructed based on the parameters 269 * @since 2.1.0 270 */ 271 272 public static Font getFont(final String fontname, final float size, final BaseColor color) { 273 return getFont(fontname, defaultEncoding, defaultEmbedding, size, Font.UNDEFINED, color); 274 } 275 276/** 277 * Constructs a <CODE>Font</CODE>-object. 278 * 279 * @param fontname the name of the font 280 * @param size the size of this font 281 * @param style the style of this font 282 * @return the Font constructed based on the parameters 283 */ 284 285 public static Font getFont(final String fontname, final float size, final int style) { 286 return getFont(fontname, defaultEncoding, defaultEmbedding, size, style, null); 287 } 288 289/** 290 * Constructs a <CODE>Font</CODE>-object. 291 * 292 * @param fontname the name of the font 293 * @param size the size of this font 294 * @return the Font constructed based on the parameters 295 */ 296 297 public static Font getFont(final String fontname, final float size) { 298 return getFont(fontname, defaultEncoding, defaultEmbedding, size, Font.UNDEFINED, null); 299 } 300 301/** 302 * Constructs a <CODE>Font</CODE>-object. 303 * 304 * @param fontname the name of the font 305 * @return the Font constructed based on the parameters 306 */ 307 308 public static Font getFont(final String fontname) { 309 return getFont(fontname, defaultEncoding, defaultEmbedding, Font.UNDEFINED, Font.UNDEFINED, null); 310 } 311 312 /** 313 * Register a font by giving explicitly the font family and name. 314 * @param familyName the font family 315 * @param fullName the font name 316 * @param path the font path 317 */ 318 public static void registerFamily(final String familyName, final String fullName, final String path) { 319 fontImp.registerFamily(familyName, fullName, path); 320 } 321 322/** 323 * Register a ttf- or a ttc-file. 324 * 325 * @param path the path to a ttf- or ttc-file 326 */ 327 328 public static void register(final String path) { 329 register(path, null); 330 } 331 332/** 333 * Register a font file and use an alias for the font contained in it. 334 * 335 * @param path the path to a font file 336 * @param alias the alias you want to use for the font 337 */ 338 339 public static void register(final String path, final String alias) { 340 fontImp.register(path, alias); 341 } 342 343 /** Register all the fonts in a directory. 344 * @param dir the directory 345 * @return the number of fonts registered 346 */ 347 public static int registerDirectory(final String dir) { 348 return fontImp.registerDirectory(dir); 349 } 350 351 /** 352 * Register all the fonts in a directory and possibly its subdirectories. 353 * @param dir the directory 354 * @param scanSubdirectories recursively scan subdirectories if <code>true</true> 355 * @return the number of fonts registered 356 * @since 2.1.2 357 */ 358 public static int registerDirectory(final String dir, final boolean scanSubdirectories) { 359 return fontImp.registerDirectory(dir, scanSubdirectories); 360 } 361 362 /** Register fonts in some probable directories. It usually works in Windows, 363 * Linux and Solaris. 364 * @return the number of fonts registered 365 */ 366 public static int registerDirectories() { 367 return fontImp.registerDirectories(); 368 } 369 370/** 371 * Gets a set of registered fontnames. 372 * @return a set of registered fonts 373 */ 374 375 public static Set<String> getRegisteredFonts() { 376 return fontImp.getRegisteredFonts(); 377 } 378 379/** 380 * Gets a set of registered fontnames. 381 * @return a set of registered font families 382 */ 383 384 public static Set<String> getRegisteredFamilies() { 385 return fontImp.getRegisteredFamilies(); 386 } 387 388/** 389 * Gets a set of registered fontnames. 390 * @param fontname of a font that may or may not be registered 391 * @return true if a given font is registered 392 */ 393 394 public static boolean contains(final String fontname) { 395 return fontImp.isRegistered(fontname); 396 } 397 398/** 399 * Checks if a certain font is registered. 400 * 401 * @param fontname the name of the font that has to be checked. 402 * @return true if the font is found 403 */ 404 405 public static boolean isRegistered(final String fontname) { 406 return fontImp.isRegistered(fontname); 407 } 408 409 /** 410 * Gets the font factory implementation. 411 * @return the font factory implementation 412 */ 413 public static FontFactoryImp getFontImp() { 414 return fontImp; 415 } 416 417 /** 418 * Sets the font factory implementation. 419 * @param fontImp the font factory implementation 420 */ 421 public static void setFontImp(final FontFactoryImp fontImp) { 422 if (fontImp == null) 423 throw new NullPointerException(MessageLocalization.getComposedMessage("fontfactoryimp.cannot.be.null")); 424 FontFactory.fontImp = fontImp; 425 } 426}