1 50 51 package com.lowagie.text; 52 53 import java.awt.Color ; 54 import java.io.File ; 55 import java.io.IOException ; 56 import java.util.ArrayList ; 57 import java.util.Enumeration ; 58 import java.util.Hashtable ; 59 import java.util.Iterator ; 60 import java.util.Properties ; 61 import java.util.Set ; 62 63 import com.lowagie.text.html.Markup; 64 import com.lowagie.text.pdf.BaseFont; 65 66 73 74 public class FontFactoryImp { 75 76 77 private Properties trueTypeFonts = new Properties (); 78 79 private static String [] TTFamilyOrder = { 80 "3", "1", "1033", 81 "3", "0", "1033", 82 "1", "0", "0", 83 "0", "3", "0" 84 }; 85 86 87 private Hashtable fontFamilies = new Hashtable (); 88 89 90 public String defaultEncoding = BaseFont.WINANSI; 91 92 93 public boolean defaultEmbedding = BaseFont.NOT_EMBEDDED; 94 95 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 tmp; 113 tmp = new ArrayList (); 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 (); 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 (); 126 tmp.add(FontFactory.SYMBOL); 127 fontFamilies.put(FontFactory.SYMBOL.toLowerCase(), tmp); 128 tmp = new ArrayList (); 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 (); 136 tmp.add(FontFactory.ZAPFDINGBATS); 137 fontFamilies.put(FontFactory.ZAPFDINGBATS.toLowerCase(), tmp); 138 } 139 140 151 public Font getFont(String fontname, String encoding, boolean embedded, float size, int style, Color color) { 152 return getFont(fontname, encoding, embedded, size, style, color, true); 153 } 154 155 156 157 170 public Font getFont(String fontname, String encoding, boolean embedded, float size, int style, Color color, boolean cached) { 171 if (fontname == null) return new Font(Font.UNDEFINED, size, style, color); 172 String lowercasefontname = fontname.toLowerCase(); 173 ArrayList tmp = (ArrayList ) fontFamilies.get(lowercasefontname); 174 if (tmp != null) { 175 int s = style == Font.UNDEFINED ? Font.NORMAL : style; 177 int fs = Font.NORMAL; 178 boolean found = false; 179 for (Iterator i = tmp.iterator(); i.hasNext(); ) { 180 String f = (String ) i.next(); 181 String 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 basefont = BaseFont.createFont(fontname, encoding, embedded, cached, null, null, true); 200 } 201 catch(DocumentException de) { 202 } 203 if (basefont == null) { 204 fontname = trueTypeFonts.getProperty(fontname.toLowerCase()); 206 if (fontname == null) return new Font(Font.UNDEFINED, size, style, color); 208 basefont = BaseFont.createFont(fontname, encoding, embedded, cached, null, null); 210 } 211 } 212 catch(DocumentException de) { 213 throw new ExceptionConverter(de); 215 } 216 catch(IOException ioe) { 217 return new Font(Font.UNDEFINED, size, style, color); 219 } 220 catch(NullPointerException npe) { 221 return new Font(Font.UNDEFINED, size, style, color); 223 } 224 return new Font(basefont, size, style, color); 225 } 226 227 228 234 235 public Font getFont(Properties attributes) { 236 String fontname = null; 237 String encoding = defaultEncoding; 238 boolean embedded = defaultEmbedding; 239 float size = Font.UNDEFINED; 240 int style = Font.NORMAL; 241 Color color = null; 242 String value = attributes.getProperty(Markup.HTML_ATTR_STYLE); 243 if (value != null && value.length() > 0) { 244 Properties 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 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 e = styleAttributes.keys(); e.hasMoreElements();) { 276 Object 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 r = attributes.getProperty(ElementTags.RED); 300 String g = attributes.getProperty(ElementTags.GREEN); 301 String 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 (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 330 331 public Font getFont(String fontname, String encoding, boolean embedded, float size, int style) { 332 return getFont(fontname, encoding, embedded, size, style, null); 333 } 334 335 344 345 public Font getFont(String fontname, String encoding, boolean embedded, float size) { 346 return getFont(fontname, encoding, embedded, size, Font.UNDEFINED, null); 347 } 348 349 357 358 public Font getFont(String fontname, String encoding, boolean embedded) { 359 return getFont(fontname, encoding, embedded, Font.UNDEFINED, Font.UNDEFINED, null); 360 } 361 362 372 373 public Font getFont(String fontname, String encoding, float size, int style, Color color) { 374 return getFont(fontname, encoding, defaultEmbedding, size, style, color); 375 } 376 377 386 387 public Font getFont(String fontname, String encoding, float size, int style) { 388 return getFont(fontname, encoding, defaultEmbedding, size, style, null); 389 } 390 391 399 400 public Font getFont(String fontname, String encoding, float size) { 401 return getFont(fontname, encoding, defaultEmbedding, size, Font.UNDEFINED, null); 402 } 403 404 411 412 public Font getFont(String fontname, String encoding) { 413 return getFont(fontname, encoding, defaultEmbedding, Font.UNDEFINED, Font.UNDEFINED, null); 414 } 415 416 425 426 public Font getFont(String fontname, float size, int style, Color color) { 427 return getFont(fontname, defaultEncoding, defaultEmbedding, size, style, color); 428 } 429 430 438 439 public Font getFont(String fontname, float size, int style) { 440 return getFont(fontname, defaultEncoding, defaultEmbedding, size, style, null); 441 } 442 443 450 451 public Font getFont(String fontname, float size) { 452 return getFont(fontname, defaultEncoding, defaultEmbedding, size, Font.UNDEFINED, null); 453 } 454 455 461 462 public Font getFont(String fontname) { 463 return getFont(fontname, defaultEncoding, defaultEmbedding, Font.UNDEFINED, Font.UNDEFINED, null); 464 } 465 466 472 public void registerFamily(String familyName, String fullName, String path) { 473 if (path != null) 474 trueTypeFonts.setProperty(fullName, path); 475 ArrayList tmp = (ArrayList ) fontFamilies.get(familyName); 476 if (tmp == null) { 477 tmp = new ArrayList (); 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 )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 501 502 public void register(String path) { 503 register(path, null); 504 } 505 506 512 513 public void register(String path, String alias) { 514 try { 515 if (path.toLowerCase().endsWith(".ttf") || path.toLowerCase().endsWith(".otf") || path.toLowerCase().indexOf(".ttc,") > 0) { 516 Object allNames[] = BaseFont.getAllFontNames(path, BaseFont.WINANSI, null); 517 trueTypeFonts.setProperty(((String )allNames[0]).toLowerCase(), path); 518 if (alias != null) { 519 trueTypeFonts.setProperty(alias.toLowerCase(), path); 520 } 521 String [][] names = (String [][])allNames[2]; for (int i = 0; i < names.length; i++) { 524 trueTypeFonts.setProperty(names[i][3].toLowerCase(), path); 525 } 526 String fullName = null; 527 String familyName = null; 528 names = (String [][])allNames[1]; 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 lastName = ""; 540 names = (String [][])allNames[2]; 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 [] 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 fullName = (bf.getFullFontName()[0][3]).toLowerCase(); 566 String familyName = (bf.getFamilyFontName()[0][3]).toLowerCase(); 567 String 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 throw new ExceptionConverter(de); 576 } 577 catch(IOException ioe) { 578 throw new ExceptionConverter(ioe); 579 } 580 } 581 582 586 public int registerDirectory(String dir) { 587 int count = 0; 588 try { 589 File file = new File (dir); 590 if (!file.exists() || !file.isDirectory()) 591 return 0; 592 String 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 (dir, files[k]); 598 String 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 e) { 605 } 607 } 608 } 609 catch (Exception e) { 610 } 612 return count; 613 } 614 615 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 638 639 public Set getRegisteredFonts() { 640 return Utilities.getKeySet(trueTypeFonts); 641 } 642 643 647 648 public Set getRegisteredFamilies() { 649 return Utilities.getKeySet(fontFamilies); 650 } 651 652 658 public boolean isRegistered(String fontname) { 659 return trueTypeFonts.containsKey(fontname.toLowerCase()); 660 } 661 } | Popular Tags |