1 17 18 19 20 package org.apache.fop.fonts; 21 22 import java.util.Map ; 23 24 28 public class Font { 29 30 31 public static final FontTriplet DEFAULT_FONT = new FontTriplet("any", "normal", 400); 32 33 public static final int NORMAL = 400; 34 35 public static final int BOLD = 700; 36 37 private String fontName; 38 private FontTriplet triplet; 39 private int fontSize; 40 41 44 46 private FontMetrics metric; 47 48 55 public Font(String key, FontTriplet triplet, FontMetrics met, int fontSize) { 56 this.fontName = key; 57 this.triplet = triplet; 58 this.metric = met; 59 this.fontSize = fontSize; 60 } 61 62 66 public int getAscender() { 67 return metric.getAscender(fontSize) / 1000; 68 } 69 70 74 public int getCapHeight() { 75 return metric.getCapHeight(fontSize) / 1000; 76 } 77 78 82 public int getDescender() { 83 return metric.getDescender(fontSize) / 1000; 84 } 85 86 90 public String getFontName() { 91 return fontName; 92 } 93 94 95 public FontTriplet getFontTriplet() { 96 return this.triplet; 97 } 98 99 103 public int getFontSize() { 104 return fontSize; 105 } 106 107 111 public int getXHeight() { 112 return metric.getXHeight(fontSize) / 1000; 113 } 114 115 116 public boolean hasKerning() { 117 return metric.hasKerningInfo(); 118 } 119 120 124 public Map getKerning() { 125 if (metric.hasKerningInfo()) { 126 return metric.getKerningInfo(); 127 } else { 128 return java.util.Collections.EMPTY_MAP; 129 } 130 } 131 132 138 public int getKernValue(char ch1, char ch2) { 139 Map kernPair = (Map )getKerning().get(new Integer (ch1)); 140 if (kernPair != null) { 141 Integer width = (Integer )kernPair.get(new Integer (ch2)); 142 if (width != null) { 143 return width.intValue(); 144 } 145 } 146 return 0; 147 } 148 149 154 public int getWidth(int charnum) { 155 return (metric.getWidth(charnum, fontSize) / 1000); 157 } 158 159 165 public char mapChar(char c) { 166 167 if (metric instanceof org.apache.fop.fonts.Typeface) { 168 return ((org.apache.fop.fonts.Typeface)metric).mapChar(c); 169 } 170 171 char d = CodePointMapping.getMapping("WinAnsiEncoding").mapChar(c); 173 if (d != 0) { 174 c = d; 175 } else { 176 c = '#'; 177 } 178 179 return c; 180 } 181 182 187 public boolean hasChar(char c) { 188 if (metric instanceof org.apache.fop.fonts.Typeface) { 189 return ((org.apache.fop.fonts.Typeface)metric).hasChar(c); 190 } else { 191 return (CodePointMapping.getMapping("WinAnsiEncoding").mapChar(c) > 0); 193 } 194 } 195 196 199 public String toString() { 200 StringBuffer sbuf = new StringBuffer (); 201 sbuf.append('('); 202 205 sbuf.append(fontName); 206 sbuf.append(','); 207 sbuf.append(fontSize); 208 213 sbuf.append(')'); 214 return sbuf.toString(); 215 } 216 217 225 public int getCharWidth(char c) { 226 int width; 227 228 if ((c == '\n') || (c == '\r') || (c == '\t') || (c == '\u00A0')) { 229 width = getCharWidth(' '); 230 } else { 231 if (hasChar(c)) { 232 width = getWidth(mapChar(c)); 233 } else { 234 width = -1; 235 } 236 if (width <= 0) { 237 int em = getFontSize(); int en = em / 2; 242 if (c == ' ') { 243 width = em; 244 } else if (c == '\u2000') { 245 width = en; 246 } else if (c == '\u2001') { 247 width = em; 248 } else if (c == '\u2002') { 249 width = em / 2; 250 } else if (c == '\u2003') { 251 width = getFontSize(); 252 } else if (c == '\u2004') { 253 width = em / 3; 254 } else if (c == '\u2005') { 255 width = em / 4; 256 } else if (c == '\u2006') { 257 width = em / 6; 258 } else if (c == '\u2007') { 259 width = getCharWidth('0'); 260 } else if (c == '\u2008') { 261 width = getCharWidth('.'); 262 } else if (c == '\u2009') { 263 width = em / 5; 264 } else if (c == '\u200A') { 265 width = em / 10; 266 } else if (c == '\u200B') { 267 width = 0; 268 } else if (c == '\u202F') { 269 width = getCharWidth(' ') / 2; 270 } else if (c == '\u3000') { 271 width = getCharWidth(' ') * 2; 272 } else { 273 width = getWidth(mapChar(c)); 275 } 276 } 277 } 278 279 return width; 280 } 281 282 287 public int getWordWidth(String word) { 288 if (word == null) { 289 return 0; 290 } 291 int wordLength = word.length(); 292 int width = 0; 293 char[] characters = new char[wordLength]; 294 word.getChars(0, wordLength, characters, 0); 295 for (int i = 0; i < wordLength; i++) { 296 width += getCharWidth(characters[i]); 297 } 298 return width; 299 } 300 301 } 302 303 304 | Popular Tags |