1 21 24 package org.lobobrowser.util.gui; 25 26 import java.util.*; 27 import java.awt.*; 28 import java.util.logging.*; 29 30 import org.lobobrowser.util.Objects; 31 32 35 public class FontFactory { 36 private static final Logger logger = Logger.getLogger(FontFactory.class.getName()); 37 private static final boolean loggableInfo = logger.isLoggable(Level.INFO); 38 private static final FontFactory instance = new FontFactory(); 39 private final Set fontFamilies = new HashSet(); 40 private final Map fontMap = new HashMap(); 41 42 45 private FontFactory() { 46 boolean liflag = loggableInfo; 47 String [] ffns = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 48 Set fontFamilies = this.fontFamilies; 49 synchronized(this) { 50 for(int i = 0; i < ffns.length; i++) { 51 if(liflag) { 52 logger.info("FontFactory(): family=" + ffns[i]); 53 } 54 fontFamilies.add(ffns[i].toLowerCase()); 55 } 56 } 57 } 58 59 public static final FontFactory getInstance() { 60 return instance; 61 } 62 63 private final Map registeredFonts = new HashMap(0); 64 65 72 public void registerFont(String fontName, int fontFormat, java.io.InputStream fontStream) throws java.awt.FontFormatException , java.io.IOException { 73 Font f = Font.createFont(fontFormat, fontStream); 74 synchronized(this) { 75 this.registeredFonts.put(fontName.toLowerCase(), f); 76 } 77 } 78 79 83 public void unregisterFont(String fontName) { 84 synchronized(this) { 85 this.registeredFonts.remove(fontName.toLowerCase()); 86 } 87 } 88 89 public Font getFont(String fontFamily, String fontStyle, String fontVariant, String fontWeight, float fontSize) { 90 FontKey key = new FontKey(fontFamily, fontStyle, fontVariant, fontWeight, fontSize); 91 synchronized(this) { 92 Font font = (Font) this.fontMap.get(key); 93 if(font == null) { 94 font = this.createFont(key); 95 this.fontMap.put(key, font); 96 } 97 return font; 98 } 99 } 100 101 private final Font createFont(FontKey key) { 102 String fontNames = key.fontFamily; 103 String fontFam = null; 104 Set fontFamilies = this.fontFamilies; 105 Map registeredFonts = this.registeredFonts; 106 Font baseFont = null; 107 if(fontNames != null) { 108 StringTokenizer tok = new StringTokenizer(fontNames, ","); 109 while(tok.hasMoreTokens()) { 110 fontFam = tok.nextToken().trim(); 111 String fontFamTL = fontFam.toLowerCase(); 112 if(registeredFonts.containsKey(fontFamTL)) { 113 baseFont = (Font) registeredFonts.get(fontFamTL); 114 break; 115 } 116 else if(fontFamilies.contains(fontFamTL)) { 117 break; 118 } 119 } 120 } 121 int fontStyle = Font.PLAIN; 122 if("italic".equalsIgnoreCase(key.fontStyle)) { 123 fontStyle |= Font.ITALIC; 124 } 125 if("bold".equalsIgnoreCase(key.fontWeight) || "bolder".equalsIgnoreCase(key.fontWeight)) { 126 fontStyle |= Font.BOLD; 127 } 128 if(baseFont != null) { 129 return baseFont.deriveFont(fontStyle, key.fontSize); 130 } 131 else { 132 return new Font(fontFam, fontStyle,(int) Math.round(key.fontSize)); 133 } 134 } 135 136 private static class FontKey { 137 public final String fontFamily; 138 public final String fontStyle; 139 public final String fontVariant; 140 public final String fontWeight; 141 public final float fontSize; 142 143 144 151 public FontKey(final String fontFamily, final String fontStyle, 152 final String fontVariant, final String fontWeight, 153 final float fontSize) { 154 this.fontFamily = fontFamily; 155 this.fontStyle = fontStyle; 156 this.fontVariant = fontVariant; 157 this.fontWeight = fontWeight; 158 this.fontSize = fontSize; 159 } 160 161 public boolean equals(Object other) { 162 if(!(other instanceof FontKey)) { 163 return false; 164 } 165 FontKey ors = (FontKey) other; 166 return this.fontSize == ors.fontSize && 168 Objects.equals(this.fontFamily,ors.fontFamily) && 169 Objects.equals(this.fontStyle,ors.fontStyle) && 170 Objects.equals(this.fontWeight,ors.fontWeight) && 171 Objects.equals(this.fontVariant,ors.fontVariant); 172 } 173 174 public int hashCode() { 175 String ff = this.fontFamily; 176 if(ff == null) { 177 ff = ""; 178 } 179 String fw = this.fontWeight; 180 if(fw == null) { 181 fw = ""; 182 } 183 String fs = this.fontStyle; 184 if(fs == null) { 185 fs = ""; 186 } 187 return ff.hashCode() ^ 188 fw.hashCode() ^ 189 fs.hashCode() ^ 190 (int) this.fontSize; 191 } 192 193 public String toString() { 194 return "FontKey[family=" + this.fontFamily + ",size=" + this.fontSize + ",style=" + this.fontStyle + ",weight=" + this.fontWeight + ",variant=" + this.fontVariant + "]"; 195 } 196 } 197 } | Popular Tags |