1 47 package com.lowagie.text.pdf; 48 49 import java.awt.Font ; 50 import java.io.File ; 51 import java.util.HashMap ; 52 53 import com.lowagie.text.ExceptionConverter; 54 57 58 public class DefaultFontMapper implements FontMapper { 59 60 62 public static class BaseFontParameters { 63 65 public String fontName; 66 68 public String encoding; 69 71 public boolean embedded; 72 74 public boolean cached; 75 77 public byte ttfAfm[]; 78 80 public byte pfb[]; 81 82 85 public BaseFontParameters(String fontName) { 86 this.fontName = fontName; 87 encoding = BaseFont.CP1252; 88 embedded = BaseFont.EMBEDDED; 89 cached = BaseFont.CACHED; 90 } 91 } 92 93 95 private HashMap aliases = new HashMap (); 96 98 private HashMap mapper = new HashMap (); 99 105 106 public BaseFont awtToPdf(Font font) { 107 try { 108 BaseFontParameters p = getBaseFontParameters(font.getFontName()); 109 if (p != null) 110 return BaseFont.createFont(p.fontName, p.encoding, p.embedded, p.cached, p.ttfAfm, p.pfb); 111 String fontKey = null; 112 String logicalName = font.getName(); 113 114 if (logicalName.equalsIgnoreCase("DialogInput") || logicalName.equalsIgnoreCase("Monospaced") || logicalName.equalsIgnoreCase("Courier")) { 115 116 if (font.isItalic()) { 117 if (font.isBold()) { 118 fontKey = BaseFont.COURIER_BOLDOBLIQUE; 119 120 } else { 121 fontKey = BaseFont.COURIER_OBLIQUE; 122 } 123 124 } else { 125 if (font.isBold()) { 126 fontKey = BaseFont.COURIER_BOLD; 127 128 } else { 129 fontKey = BaseFont.COURIER; 130 } 131 } 132 133 } else if (logicalName.equalsIgnoreCase("Serif") || logicalName.equalsIgnoreCase("TimesRoman")) { 134 135 if (font.isItalic()) { 136 if (font.isBold()) { 137 fontKey = BaseFont.TIMES_BOLDITALIC; 138 139 } else { 140 fontKey = BaseFont.TIMES_ITALIC; 141 } 142 143 } else { 144 if (font.isBold()) { 145 fontKey = BaseFont.TIMES_BOLD; 146 147 } else { 148 fontKey = BaseFont.TIMES_ROMAN; 149 } 150 } 151 152 } else { 154 if (font.isItalic()) { 155 if (font.isBold()) { 156 fontKey = BaseFont.HELVETICA_BOLDOBLIQUE; 157 158 } else { 159 fontKey = BaseFont.HELVETICA_OBLIQUE; 160 } 161 162 } else { 163 if (font.isBold()) { 164 fontKey = BaseFont.HELVETICA_BOLD; 165 } else { 166 fontKey = BaseFont.HELVETICA; 167 } 168 } 169 } 170 return BaseFont.createFont(fontKey, BaseFont.CP1252, false); 171 } 172 catch (Exception e) { 173 throw new ExceptionConverter(e); 174 } 175 } 176 177 184 185 public Font pdfToAwt(BaseFont font, int size) { 186 String names[][] = font.getFullFontName(); 187 if (names.length == 1) 188 return new Font (names[0][3], 0, size); 189 String name10 = null; 190 String name3x = null; 191 for (int k = 0; k < names.length; ++k) { 192 String name[] = names[k]; 193 if (name[0].equals("1") && name[1].equals("0")) 194 name10 = name[3]; 195 else if (name[2].equals("1033")) { 196 name3x = name[3]; 197 break; 198 } 199 } 200 String finalName = name3x; 201 if (finalName == null) 202 finalName = name10; 203 if (finalName == null) 204 finalName = names[0][3]; 205 return new Font (finalName, 0, size); 206 } 207 208 212 public void putName(String awtName, BaseFontParameters parameters) { 213 mapper.put(awtName, parameters); 214 } 215 216 220 public void putAlias(String alias, String awtName) { 221 aliases.put(alias, awtName); 222 } 223 224 228 public BaseFontParameters getBaseFontParameters(String name) { 229 String alias = (String )aliases.get(name); 230 if (alias == null) 231 return (BaseFontParameters)mapper.get(name); 232 BaseFontParameters p = (BaseFontParameters)mapper.get(alias); 233 if (p == null) 234 return (BaseFontParameters)mapper.get(name); 235 else 236 return p; 237 } 238 239 244 public void insertNames(Object allNames[], String path) { 245 String names[][] = (String [][])allNames[2]; 246 String main = null; 247 for (int k = 0; k < names.length; ++k) { 248 String name[] = names[k]; 249 if (name[2].equals("1033")) { 250 main = name[3]; 251 break; 252 } 253 } 254 if (main == null) 255 main = names[0][3]; 256 BaseFontParameters p = new BaseFontParameters(path); 257 mapper.put(main, p); 258 for (int k = 0; k < names.length; ++k) { 259 aliases.put(names[k][3], main); 260 } 261 aliases.put(allNames[0], main); 262 } 263 264 271 public int insertDirectory(String dir) { 272 File file = new File (dir); 273 if (!file.exists() || !file.isDirectory()) 274 return 0; 275 File files[] = file.listFiles(); 276 if (files == null) 277 return 0; 278 int count = 0; 279 for (int k = 0; k < files.length; ++k) { 280 file = files[k]; 281 String name = file.getPath().toLowerCase(); 282 try { 283 if (name.endsWith(".ttf") || name.endsWith(".otf") || name.endsWith(".afm")) { 284 Object allNames[] = BaseFont.getAllFontNames(file.getPath(), BaseFont.CP1252, null); 285 insertNames(allNames, file.getPath()); 286 ++count; 287 } 288 else if (name.endsWith(".ttc")) { 289 String ttcs[] = BaseFont.enumerateTTCNames(file.getPath()); 290 for (int j = 0; j < ttcs.length; ++j) { 291 String nt = file.getPath() + "," + j; 292 Object allNames[] = BaseFont.getAllFontNames(nt, BaseFont.CP1252, null); 293 insertNames(allNames, nt); 294 } 295 ++count; 296 } 297 } 298 catch (Exception e) { 299 } 300 } 301 return count; 302 } 303 304 public HashMap getMapper() { 305 return mapper; 306 } 307 308 public HashMap getAliases() { 309 return aliases; 310 } 311 } 312 | Popular Tags |