1 51 package org.apache.fop.layout; 52 53 import org.apache.fop.apps.FOPException; 54 import org.apache.fop.messaging.MessageHandler; 55 56 import java.util.HashMap ; 57 58 public class FontInfo { 59 HashMap usedFonts; 60 HashMap triplets; HashMap fonts; 63 public FontInfo() { 64 this.triplets = new HashMap (); 65 this.fonts = new HashMap (); 66 this.usedFonts = new HashMap (); 67 } 68 69 public void addFontProperties(String name, String family, String style, 70 String weight) { 71 75 76 String key = createFontKey(family, style, weight); 77 this.triplets.put(key, name); 78 } 79 80 public void addMetrics(String name, FontMetric metrics) { 81 83 this.fonts.put(name, metrics); 84 } 85 86 public String fontLookup(String family, String style, 87 String weight) throws FOPException { 88 return fontLookup(createFontKey(family, style, weight)); 89 } 90 91 public String fontLookup(String key) throws FOPException { 92 93 String f = (String )this.triplets.get(key); 94 if (f == null) { 95 int i = key.indexOf(','); 96 String s = "any" + key.substring(i); 97 f = (String )this.triplets.get(s); 98 if (f == null) { 99 f = (String )this.triplets.get("any,normal,normal"); 100 if (f == null) { 101 throw new FOPException("no default font defined by OutputConverter"); 102 } 103 MessageHandler.errorln("defaulted font to any,normal,normal"); 104 } 105 MessageHandler.errorln("unknown font " + key 106 + " so defaulted font to any"); 107 } 108 109 usedFonts.put(f, fonts.get(f)); 110 return f; 111 } 112 113 public boolean hasFont(String family, String style, String weight) { 114 String key = createFontKey(family, style, weight); 115 return this.triplets.get(key) != null; 116 } 117 118 public boolean hasFont(String key) { 119 return this.triplets.get(key) != null; 120 } 121 122 125 public static String createFontKey(String family, String style, 126 String weight) { 127 int i; 128 129 try { 130 i = Integer.parseInt(weight); 131 } catch (NumberFormatException e) { 132 i = 0; 133 } 134 135 if (i > 600) 136 weight = "bold"; 137 else if (i > 0) 138 weight = "normal"; 139 140 return family + "," + style + "," + weight; 141 } 142 143 public HashMap getFonts() { 144 return this.fonts; 145 } 146 147 public HashMap getUsedFonts() { 148 return this.usedFonts; 149 } 150 151 public FontMetric getMetricsFor(String fontName) throws FOPException { 152 usedFonts.put(fontName, fonts.get(fontName)); 153 return (FontMetric)fonts.get(fontName); 154 } 155 156 } 157 | Popular Tags |