1 17 18 19 20 package org.apache.fop.fonts; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 33 42 public class FontInfo { 43 44 45 protected static Log log = LogFactory.getLog(FontInfo.class); 46 47 48 private Map usedFonts; 49 50 51 private Map triplets; 52 53 54 private Map fonts; 55 56 59 private Collection loggedFontKeys; 60 61 62 private Map fontInstanceCache = new java.util.HashMap (); 63 64 67 public FontInfo() { 68 this.triplets = new java.util.HashMap (); 69 this.fonts = new java.util.HashMap (); 70 this.usedFonts = new java.util.HashMap (); 71 } 72 73 78 public boolean isSetupValid() { 79 return triplets.containsKey(Font.DEFAULT_FONT); 80 } 81 82 89 public void addFontProperties(String name, String family, String style, int weight) { 90 addFontProperties(name, createFontKey(family, style, weight)); 91 } 92 93 98 public void addFontProperties(String name, FontTriplet triplet) { 99 103 this.triplets.put(triplet, name); 104 } 105 106 111 public void addMetrics(String name, FontMetrics metrics) { 112 114 this.fonts.put(name, metrics); 115 } 116 117 131 private FontTriplet fontLookup(String family, String style, 132 int weight, boolean substFont) { 133 FontTriplet startKey = createFontKey(family, style, weight); 134 FontTriplet key = startKey; 135 String f = getInternalFontKey(key); 137 if (f == null) { 138 key = findAdjustWeight(family, style, weight); 140 f = getInternalFontKey(key); 141 142 if (!substFont && f == null) { 143 return null; 144 } 145 146 if (f == null) { 149 key = createFontKey(family, "normal", weight); 150 f = getInternalFontKey(key); 151 } 152 153 if (f == null) { 155 key = createFontKey(family, "normal", 400); 156 f = getInternalFontKey(key); 157 } 158 159 if (f == null) { 161 key = createFontKey("any", style, weight); 162 f = getInternalFontKey(key); 163 } 164 165 if (f == null) { 167 key = Font.DEFAULT_FONT; 168 f = getInternalFontKey(key); 169 } 170 } 171 172 if (f != null) { 173 if (key != startKey) { 174 notifyFontReplacement(startKey, key); 175 } 176 return key; 177 } else { 178 return null; 179 } 180 } 181 182 186 public void useFont(String internalName) { 187 usedFonts.put(internalName, fonts.get(internalName)); 188 } 189 190 196 public Font getFontInstance(FontTriplet triplet, int fontSize) { 197 Map sizes = (Map )fontInstanceCache.get(triplet); 198 if (sizes == null) { 199 sizes = new java.util.HashMap (); 200 fontInstanceCache.put(triplet, sizes); 201 } 202 Integer size = new Integer (fontSize); 203 Font font = (Font)sizes.get(size); 204 if (font == null) { 205 String fname = getInternalFontKey(triplet); 206 useFont(fname); 207 FontMetrics metrics = getMetricsFor(fname); 208 font = new Font(fname, triplet, metrics, fontSize); 209 sizes.put(size, font); 210 } 211 return font; 212 } 213 214 226 public FontTriplet fontLookup(String family, String style, 227 int weight) { 228 return fontLookup(family, style, weight, true); 229 } 230 231 243 public FontTriplet fontLookup(String [] family, String style, 244 int weight) { 245 for (int i = 0; i < family.length; i++) { 246 FontTriplet triplet = fontLookup(family[i], style, weight, (i >= family.length - 1)); 247 if (triplet != null) { 248 return triplet; 249 } 250 } 251 throw new IllegalStateException ("fontLookup must return a key on the last call"); 252 } 253 254 private void notifyFontReplacement(FontTriplet replacedKey, FontTriplet newKey) { 255 if (loggedFontKeys == null) { 256 loggedFontKeys = new java.util.HashSet (); 257 } 258 if (!loggedFontKeys.contains(replacedKey)) { 259 loggedFontKeys.add(replacedKey); 260 log.warn("Font '" + replacedKey + "' not found. " 261 + "Substituting with '" + newKey + "'."); 262 } 263 } 264 265 273 public FontTriplet findAdjustWeight(String family, String style, 274 int weight) { 275 FontTriplet key = null; 276 String f = null; 277 int newWeight = weight; 278 if (newWeight < 400) { 279 while (f == null && newWeight > 0) { 280 newWeight -= 100; 281 key = createFontKey(family, style, newWeight); 282 f = getInternalFontKey(key); 283 } 284 } else if (newWeight == 500) { 285 key = createFontKey(family, style, 400); 286 f = getInternalFontKey(key); 287 } else if (newWeight > 500) { 288 while (f == null && newWeight < 1000) { 289 newWeight += 100; 290 key = createFontKey(family, style, newWeight); 291 f = getInternalFontKey(key); 292 } 293 newWeight = weight; 294 while (f == null && newWeight > 400) { 295 newWeight -= 100; 296 key = createFontKey(family, style, newWeight); 297 f = getInternalFontKey(key); 298 } 299 } 300 if (f == null && weight != 400) { 301 key = createFontKey(family, style, 400); 302 f = getInternalFontKey(key); 303 } 304 305 if (f != null) { 306 return key; 307 } else { 308 return null; 309 } 310 } 311 312 319 public boolean hasFont(String family, String style, int weight) { 320 FontTriplet key = createFontKey(family, style, weight); 321 return this.triplets.containsKey(key); 322 } 323 324 329 public String getInternalFontKey(FontTriplet triplet) { 330 return (String )triplets.get(triplet); 331 } 332 333 340 public static FontTriplet createFontKey(String family, String style, 341 int weight) { 342 return new FontTriplet(family, style, weight); 343 } 344 345 349 public Map getFonts() { 350 return java.util.Collections.unmodifiableMap(this.fonts); 351 } 352 353 359 public Map getUsedFonts() { 360 return this.usedFonts; 361 } 362 363 368 public FontMetrics getMetricsFor(String fontName) { 369 FontMetrics metrics = (FontMetrics)fonts.get(fontName); 370 usedFonts.put(fontName, metrics); 371 return metrics; 372 } 373 374 381 public FontTriplet getTripletFor(String fontName) { 382 List foundTriplets = new ArrayList (); 383 for (Iterator iter = triplets.entrySet().iterator(); iter.hasNext();) { 384 Map.Entry tripletEntry = (Map.Entry ) iter.next(); 385 if (fontName.equals(((String )tripletEntry.getValue()))) { 386 foundTriplets.add(tripletEntry.getKey()); 387 } 388 } 389 if (foundTriplets.size() > 0) { 390 Collections.sort(foundTriplets); 391 return (FontTriplet)foundTriplets.get(0); 392 } 393 return null; 394 } 395 396 404 public String getFontStyleFor(String fontName) { 405 FontTriplet triplet = getTripletFor(fontName); 406 if (triplet != null) { 407 return triplet.getStyle(); 408 } else { 409 return ""; 410 } 411 } 412 413 421 public int getFontWeightFor(String fontName) { 422 FontTriplet triplet = getTripletFor(fontName); 423 if (triplet != null) { 424 return triplet.getWeight(); 425 } else { 426 return 0; 427 } 428 } 429 430 } 431 432 433 434 | Popular Tags |