FileDocCategorySizeDatePackage
StaticFontMetrics.javaAPI DocApache Poi 3.0.13655Mon Jan 01 12:39:36 GMT 2007org.apache.poi.hssf.usermodel

StaticFontMetrics

public class StaticFontMetrics extends Object
Allows the user to lookup the font metrics for a particular font without actually having the font on the system. The font details are loaded as a resource from the POI jar file (or classpath) and should be contained in path "/font_metrics.properties". The font widths are for a 10 point version of the font. Use a multiplier for other sizes.
author
Glen Stampoultzis (glens at apache.org)

Fields Summary
private static Properties
fontMetricsProps
private static Map
fontDetailsMap
Constructors Summary
Methods Summary
public static org.apache.poi.hssf.usermodel.FontDetailsgetFontDetails(java.awt.Font font)
Retrieves the fake font details for a given font.

param
font the font to lookup.
return
the fake font.


                              
        
    
        if (fontMetricsProps == null)
        {
            InputStream metricsIn = null;
            try
            {
                fontMetricsProps = new Properties();
                if (System.getProperty("font.metrics.filename") != null)
                {
                    String filename = System.getProperty("font.metrics.filename");
                    File file = new File(filename);
                    if (!file.exists())
                        throw new FileNotFoundException("font_metrics.properties not found at path " + file.getAbsolutePath());
                    metricsIn = new FileInputStream(file);
                }
                else
                {
                    metricsIn = FontDetails.class.getResourceAsStream("/font_metrics.properties");
                    if (metricsIn == null)
                        throw new FileNotFoundException("font_metrics.properties not found in classpath");
                }
                fontMetricsProps.load(metricsIn);
            }
            catch ( IOException e )
            {
                throw new RuntimeException("Could not load font metrics: " + e.getMessage());
            }
            finally
            {
                if (metricsIn != null)
                {
                    try
                    {
                        metricsIn.close();
                    }
                    catch ( IOException ignore ) { }
                }
            }
        }

        String fontName = font.getName();

        if (fontDetailsMap.get(fontName) == null)
        {
            FontDetails fontDetails = FontDetails.create(fontName, fontMetricsProps);
            fontDetailsMap.put( fontName, fontDetails );
            return fontDetails;
        }
        else
        {
            return (FontDetails) fontDetailsMap.get(fontName);
        }