1 package prefuse.util; 2 3 import java.awt.Font ; 4 5 import prefuse.util.collections.IntObjectHashMap; 6 7 13 public class FontLib { 14 15 private static final IntObjectHashMap fontMap = new IntObjectHashMap(); 16 private static int misses = 0; 17 private static int lookups = 0; 18 19 28 public static Font getFont(String name, double size) { 29 int isize = (int)Math.floor(size); 30 return getFont(name, Font.PLAIN, isize); 31 } 32 33 43 public static Font getFont(String name, int style, double size) { 44 int isize = (int)Math.floor(size); 45 return getFont(name, style, isize); 46 } 47 48 58 public static Font getFont(String name, int style, int size) { 59 int key = (name.hashCode()<<8)+(size<<2)+style; 60 Font f = null; 61 if ( (f=(Font )fontMap.get(key)) == null ) { 62 f = new Font (name, style, size); 63 fontMap.put(key, f); 64 misses++; 65 } 66 lookups++; 67 return f; 68 } 69 70 74 public static int getCacheMissCount() { 75 return misses; 76 } 77 78 82 public static int getCacheLookupCount() { 83 return lookups; 84 } 85 86 89 public static void clearCache() { 90 fontMap.clear(); 91 } 92 93 104 public static Font getIntermediateFont(Font f1, Font f2, double frac) { 105 String name; 106 int size, style; 107 if ( frac < 0.5 ) { 108 name = f1.getName(); 109 style = f1.getStyle(); 110 } else { 111 name = f2.getName(); 112 style = f2.getStyle(); 113 } 114 size = (int)Math.round(frac*f2.getSize()+(1-frac)*f1.getSize()); 115 return getFont(name,style,size); 116 } 117 118 } | Popular Tags |