KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > FontLib


1 package prefuse.util;
2
3 import java.awt.Font JavaDoc;
4
5 import prefuse.util.collections.IntObjectHashMap;
6
7 /**
8  * Library maintaining a cache of fonts and other useful font computation
9  * routines.
10  *
11  * @author <a HREF="http://jheer.org">jeffrey heer</a>
12  */

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     /**
20      * Get a Font instance with the given font family name and size. A
21      * plain font style is assumed.
22      * @param name the font name. Any font installed on your system should
23      * be valid. Common examples include "Arial", "Verdana", "Tahoma",
24      * "Times New Roman", "Georgia", and "Courier New".
25      * @param size the size, in points, of the font
26      * @return the requested Font instance
27      */

28     public static Font JavaDoc getFont(String JavaDoc name, double size) {
29         int isize = (int)Math.floor(size);
30         return getFont(name, Font.PLAIN, isize);
31     }
32     
33     /**
34      * Get a Font instance with the given font family name, style, and size
35      * @param name the font name. Any font installed on your system should
36      * be valid. Common examples include "Arial", "Verdana", "Tahoma",
37      * "Times New Roman", "Georgia", and "Courier New".
38      * @param style the font style, such as bold or italics. This field
39      * uses the same style values as the Java {@link java.awt.Font} class.
40      * @param size the size, in points, of the font
41      * @return the requested Font instance
42      */

43     public static Font JavaDoc getFont(String JavaDoc name, int style, double size) {
44         int isize = (int)Math.floor(size);
45         return getFont(name, style, isize);
46     }
47     
48     /**
49      * Get a Font instance with the given font family name, style, and size
50      * @param name the font name. Any font installed on your system should
51      * be valid. Common examples include "Arial", "Verdana", "Tahoma",
52      * "Times New Roman", "Georgia", and "Courier New".
53      * @param style the font style, such as bold or italics. This field
54      * uses the same style values as the Java {@link java.awt.Font} class.
55      * @param size the size, in points, of the font
56      * @return the requested Font instance
57      */

58     public static Font JavaDoc getFont(String JavaDoc name, int style, int size) {
59         int key = (name.hashCode()<<8)+(size<<2)+style;
60         Font JavaDoc f = null;
61         if ( (f=(Font JavaDoc)fontMap.get(key)) == null ) {
62             f = new Font JavaDoc(name, style, size);
63             fontMap.put(key, f);
64             misses++;
65         }
66         lookups++;
67         return f;
68     }
69     
70     /**
71      * Get the number of cache misses to the Font object cache.
72      * @return the number of cache misses
73      */

74     public static int getCacheMissCount() {
75         return misses;
76     }
77     
78     /**
79      * Get the number of cache lookups to the Font object cache.
80      * @return the number of cache lookups
81      */

82     public static int getCacheLookupCount() {
83         return lookups;
84     }
85     
86     /**
87      * Clear the Font object cache.
88      */

89     public static void clearCache() {
90         fontMap.clear();
91     }
92     
93     /**
94      * Interpolate between two font instances. Font sizes are interpolated
95      * linearly. If the interpolation fraction is under 0.5, the face and
96      * style of the starting font are used, otherwise the face and style of
97      * the second font are applied.
98      * @param f1 the starting font
99      * @param f2 the target font
100      * @param frac a fraction between 0 and 1.0 controlling the interpolation
101      * amount.
102      * @return an interpolated Font instance
103      */

104     public static Font JavaDoc getIntermediateFont(Font JavaDoc f1, Font JavaDoc f2, double frac) {
105         String JavaDoc 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 } // end of class FontLib
119
Popular Tags