001: package prefuse.util;
002:
003: import java.awt.Font;
004:
005: import prefuse.util.collections.IntObjectHashMap;
006:
007: /**
008: * Library maintaining a cache of fonts and other useful font computation
009: * routines.
010: *
011: * @author <a href="http://jheer.org">jeffrey heer</a>
012: */
013: public class FontLib {
014:
015: private static final IntObjectHashMap fontMap = new IntObjectHashMap();
016: private static int misses = 0;
017: private static int lookups = 0;
018:
019: /**
020: * Get a Font instance with the given font family name and size. A
021: * plain font style is assumed.
022: * @param name the font name. Any font installed on your system should
023: * be valid. Common examples include "Arial", "Verdana", "Tahoma",
024: * "Times New Roman", "Georgia", and "Courier New".
025: * @param size the size, in points, of the font
026: * @return the requested Font instance
027: */
028: public static Font getFont(String name, double size) {
029: int isize = (int) Math.floor(size);
030: return getFont(name, Font.PLAIN, isize);
031: }
032:
033: /**
034: * Get a Font instance with the given font family name, style, and size
035: * @param name the font name. Any font installed on your system should
036: * be valid. Common examples include "Arial", "Verdana", "Tahoma",
037: * "Times New Roman", "Georgia", and "Courier New".
038: * @param style the font style, such as bold or italics. This field
039: * uses the same style values as the Java {@link java.awt.Font} class.
040: * @param size the size, in points, of the font
041: * @return the requested Font instance
042: */
043: public static Font getFont(String name, int style, double size) {
044: int isize = (int) Math.floor(size);
045: return getFont(name, style, isize);
046: }
047:
048: /**
049: * Get a Font instance with the given font family name, style, and size
050: * @param name the font name. Any font installed on your system should
051: * be valid. Common examples include "Arial", "Verdana", "Tahoma",
052: * "Times New Roman", "Georgia", and "Courier New".
053: * @param style the font style, such as bold or italics. This field
054: * uses the same style values as the Java {@link java.awt.Font} class.
055: * @param size the size, in points, of the font
056: * @return the requested Font instance
057: */
058: public static Font getFont(String name, int style, int size) {
059: int key = (name.hashCode() << 8) + (size << 2) + style;
060: Font f = null;
061: if ((f = (Font) fontMap.get(key)) == null) {
062: f = new Font(name, style, size);
063: fontMap.put(key, f);
064: misses++;
065: }
066: lookups++;
067: return f;
068: }
069:
070: /**
071: * Get the number of cache misses to the Font object cache.
072: * @return the number of cache misses
073: */
074: public static int getCacheMissCount() {
075: return misses;
076: }
077:
078: /**
079: * Get the number of cache lookups to the Font object cache.
080: * @return the number of cache lookups
081: */
082: public static int getCacheLookupCount() {
083: return lookups;
084: }
085:
086: /**
087: * Clear the Font object cache.
088: */
089: public static void clearCache() {
090: fontMap.clear();
091: }
092:
093: /**
094: * Interpolate between two font instances. Font sizes are interpolated
095: * linearly. If the interpolation fraction is under 0.5, the face and
096: * style of the starting font are used, otherwise the face and style of
097: * the second font are applied.
098: * @param f1 the starting font
099: * @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 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)
115: * f1.getSize());
116: return getFont(name, style, size);
117: }
118:
119: } // end of class FontLib
|