1 18 package org.apache.batik.gvt.font; 19 20 import java.awt.GraphicsEnvironment ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.StringTokenizer ; 26 import java.util.Vector ; 27 28 34 public class FontFamilyResolver { 35 36 40 public final static AWTFontFamily defaultFont = 41 new AWTFontFamily("SansSerif"); 42 43 47 protected final static Map fonts = new HashMap (); 48 49 protected final static Vector awtFontFamilies = new Vector (); 50 protected final static Vector awtFonts = new Vector (); 51 52 55 static { 56 fonts.put("sans-serif", "SansSerif"); 57 fonts.put("serif", "Serif"); 58 fonts.put("times", "Serif"); 59 fonts.put("times new roman", "Serif"); 60 fonts.put("cursive", "Dialog"); 61 fonts.put("fantasy", "Symbol"); 62 fonts.put("monospace", "Monospaced"); 63 fonts.put("monospaced", "Monospaced"); 64 fonts.put("courier", "Monospaced"); 65 66 70 GraphicsEnvironment env; 71 env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 72 String fontNames[] = env.getAvailableFontFamilyNames(); 73 74 int nFonts = fontNames != null ? fontNames.length : 0; 75 for(int i=0; i<nFonts; i++){ 76 fonts.put(fontNames[i].toLowerCase(), fontNames[i]); 77 78 StringTokenizer st = new StringTokenizer (fontNames[i]); 80 String fontNameWithoutSpaces = ""; 81 while (st.hasMoreTokens()) { 82 fontNameWithoutSpaces += st.nextToken(); 83 } 84 fonts.put(fontNameWithoutSpaces.toLowerCase(), fontNames[i]); 85 86 String fontNameWithDashes = fontNames[i].replace(' ', '-'); 88 if (!fontNameWithDashes.equals(fontNames[i])) { 89 fonts.put(fontNameWithDashes.toLowerCase(), fontNames[i]); 90 } 91 } 92 93 awtFontFamilies.add(defaultFont); 95 awtFonts.add(new AWTGVTFont(defaultFont.getFamilyName(), 0, 12)); 96 97 Collection fontValues = fonts.values(); 98 Iterator iter = fontValues.iterator(); 99 while(iter.hasNext()) { 100 String fontFamily = (String )iter.next(); 101 AWTFontFamily awtFontFamily = new AWTFontFamily(fontFamily); 102 awtFontFamilies.add(awtFontFamily); 103 AWTGVTFont font = new AWTGVTFont(fontFamily, 0, 12); 104 awtFonts.add(font); 105 } 106 107 } 108 109 113 protected static Map resolvedFontFamilies; 114 115 123 public static String lookup(String familyName) { 124 return (String )fonts.get(familyName.toLowerCase()); 125 } 126 127 136 public static GVTFontFamily resolve(String familyName) { 137 if (resolvedFontFamilies == null) { 138 resolvedFontFamilies = new HashMap (); 139 } 140 141 GVTFontFamily resolvedFF = 143 (GVTFontFamily)resolvedFontFamilies.get(familyName.toLowerCase()); 144 145 if (resolvedFF == null) { String awtFamilyName = (String )fonts.get(familyName.toLowerCase()); 149 if (awtFamilyName != null) { 150 resolvedFF = new AWTFontFamily(awtFamilyName); 151 } 152 153 resolvedFontFamilies.put(familyName.toLowerCase(), resolvedFF); 154 } 155 156 return resolvedFF; 164 } 165 166 175 public static GVTFontFamily resolve(UnresolvedFontFamily fontFamily) { 176 177 return resolve(fontFamily.getFamilyName()); 178 } 179 180 public static GVTFontFamily getFamilyThatCanDisplay(char c) { 181 for (int i = 0; i < awtFontFamilies.size(); i++) { 182 AWTFontFamily fontFamily = (AWTFontFamily)awtFontFamilies.get(i); 183 AWTGVTFont font = (AWTGVTFont)awtFonts.get(i); 184 if (font.canDisplay(c) && fontFamily.getFamilyName().indexOf("Song") == -1) { 185 return fontFamily; 187 } 188 } 189 190 return null; 191 } 192 193 } 194 | Popular Tags |