jfig.gui
Class FontCache

java.lang.Object
  extended byjfig.gui.FontCache

public class FontCache
extends java.lang.Object

singleton class to manage text fonts and font metrics.

FontCache: a helper class to maintain information about Fonts and FontMetrics (for different zoom factors). This is a singleton class. Use getFontCache() to retrieve a reference to the global FontCache.

The main purpose of this class is to provide a fast mapping from FIG font index values to Java Font and FontMetrics objects. Internally, the FIG font index is first mapped to equivalent Java Font names (like "ItcBookman Light") and styles (like Font.ITALIC). These values are then used to create Java Font objects via the corresponding Font constructor.

For performance reasons, especially for Java 1.1 VMs, this class caches the Font and FontMetrics objects in hashtables indexed via (fig_font_index, font_size_in_pt) integer pairs. This currently incurs an Integer object allocation per get() request, which should be optimized away by using custom hashtables instead of java.util.Hashtable.

For portable Math/Symbol rendering, we try to load the Bakoma Truetype version of the Computer Modern fonts directly from the classpath, i.e. JAR-resources like "/jfig/fonts/cmsy10.ttf". This works only on JDK 1.3+ virtual machines.


Nested Class Summary
(package private)  class FontCache.ComputerModernFontsManager
           
 
Field Summary
static java.lang.String[] cmFontNames
           
static java.lang.String[][] defaultFontMapping
          an array with the original FIG Postscript font names, without spaces, and the default (JDK 1.1 compatible) mapping to Java font names.
static float[] postscriptFontMetrics
          this array contains the measured length in centimeters of the string "The quick brown fox jumps over the lazy dog." when printed at 12pt size at 100% magnification for each of the FIG format fonts.
static java.lang.String[] postscriptFontNames
           
static java.lang.String[] texFontNames
           
 
Method Summary
 void dumpFontDescriptions()
           
 void flushFontCache()
           
 java.awt.Font getFont(int fig_font, int size)
          get a Java Font object for the requested font.
static FontCache getFontCache()
          get a reference to the FontCache singleton instance.
 int getFontIndex(java.lang.String fontName)
          find the FIG font index from a font name.
 java.awt.FontMetrics getFontMetrics(java.awt.Font font)
          return the FontMetrics for the given Font.
 java.awt.FontMetrics getFontMetrics(int fig_font, int size)
          get the FontMetrics for the requested font.
 void initializeFontDescriptions()
          initialize the font description strings for all 34 FIG fonts.
 void loadComputerModernFonts()
           
static void main(java.lang.String[] argv)
          main: simple FontCache selftest, properties in /jfig/jfig.cnf
 void parseFontDescriptions()
          parse all font description strings and extract both the Font names (like "Times" or "ItcBookman Light") and the Font styles (like Font.PLAIN or Font.BOLDITALIC).
 void put(java.lang.Object key, int fig_font, int size)
          add a new Font with index fig_font and point size into our fontTable and fontMetricsTable hashtables, under the given key.
 void set_debug(boolean v)
          enable/disable debug output from this FontCache
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

postscriptFontNames

public static final java.lang.String[] postscriptFontNames

cmFontNames

public static final java.lang.String[] cmFontNames

postscriptFontMetrics

public static final float[] postscriptFontMetrics
this array contains the measured length in centimeters of the string "The quick brown fox jumps over the lazy dog." when printed at 12pt size at 100% magnification for each of the FIG format fonts. These measurements can be used to estimate whether Java fonts or substituted fonts will have approximately the right size.


texFontNames

public static final java.lang.String[] texFontNames

defaultFontMapping

public static java.lang.String[][] defaultFontMapping
an array with the original FIG Postscript font names, without spaces, and the default (JDK 1.1 compatible) mapping to Java font names.

Method Detail

getFontCache

public static FontCache getFontCache()
get a reference to the FontCache singleton instance. The first call to this method will create the FontCache singleton.


flushFontCache

public void flushFontCache()

set_debug

public void set_debug(boolean v)
enable/disable debug output from this FontCache

Parameters:
v - whether to use debug (true) or not (false)

initializeFontDescriptions

public void initializeFontDescriptions()
initialize the font description strings for all 34 FIG fonts. This maps each FIG font index to a String like "TimesRoman,plain".


dumpFontDescriptions

public void dumpFontDescriptions()

parseFontDescriptions

public void parseFontDescriptions()
parse all font description strings and extract both the Font names (like "Times" or "ItcBookman Light") and the Font styles (like Font.PLAIN or Font.BOLDITALIC). Put those values into the fontNames[] and fontStyles[] arrays.

This method expects a font description of the form "Font Family Name,Style" with a comma-separated pair of strings. Style may be either "plain", "italic", "bold", or "bolditalic". Invalid strings will be substituted by "Courier,plain".


getFontIndex

public int getFontIndex(java.lang.String fontName)
find the FIG font index from a font name. This method expects to match the font name exactly (case sensitive); if the font is not found, the index 12 (Courier,plain) is returned as a fallback.


put

public void put(java.lang.Object key,
                int fig_font,
                int size)
add a new Font with index fig_font and point size into our fontTable and fontMetricsTable hashtables, under the given key.


getFontMetrics

public java.awt.FontMetrics getFontMetrics(int fig_font,
                                           int size)
get the FontMetrics for the requested font.

Parameters:
fig_font - the FIG index of the font (0=Times .. 33=ZapfChancery)
size - the font size in point.

getFontMetrics

public java.awt.FontMetrics getFontMetrics(java.awt.Font font)
return the FontMetrics for the given Font. We dispatch to Toolkit.getDefaultToolkit().getFontMetrics() to do the work, which method is deprecated since JDK 1.2. Unfortunately, we have to keep this method to ensure JDK 1.1 compatibility.


getFont

public java.awt.Font getFont(int fig_font,
                             int size)
get a Java Font object for the requested font.

Parameters:
fig_font - the FIG index of the font (0=Times .. 33=ZapfChancery)
size - the font size in point.

loadComputerModernFonts

public void loadComputerModernFonts()

main

public static void main(java.lang.String[] argv)
main: simple FontCache selftest, properties in /jfig/jfig.cnf