KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > StaticFontMetrics


1 /* ====================================================================
2    Copyright 2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 package org.apache.poi.hssf.usermodel;
18
19 import java.util.*;
20 import java.awt.*;
21 import java.io.*;
22
23 /**
24  * Allows the user to lookup the font metrics for a particular font without
25  * actually having the font on the system. The font details are loaded
26  * as a resource from the POI jar file (or classpath) and should be contained
27  * in path "/font_metrics.properties". The font widths are for a 10 point
28  * version of the font. Use a multiplier for other sizes.
29  *
30  * @author Glen Stampoultzis (glens at apache.org)
31  */

32 class StaticFontMetrics
33 {
34     private static Properties fontMetricsProps;
35     private static Map fontDetailsMap = new HashMap();
36
37     /**
38      * Retrieves the fake font details for a given font.
39      * @param font the font to lookup.
40      * @return the fake font.
41      */

42     public static FontDetails getFontDetails(Font font)
43     {
44         if (fontMetricsProps == null)
45         {
46             InputStream metricsIn = null;
47             try
48             {
49                 fontMetricsProps = new Properties();
50                 if (System.getProperty("font.metrics.filename") != null)
51                 {
52                     String JavaDoc filename = System.getProperty("font.metrics.filename");
53                     File file = new File(filename);
54                     if (!file.exists())
55                         throw new FileNotFoundException("font_metrics.properties not found at path " + file.getAbsolutePath());
56                     metricsIn = new FileInputStream(file);
57                 }
58                 else
59                 {
60                     metricsIn = FontDetails.class.getResourceAsStream("/font_metrics.properties");
61                     if (metricsIn == null)
62                         throw new FileNotFoundException("font_metrics.properties not found in classpath");
63                 }
64                 fontMetricsProps.load(metricsIn);
65             }
66             catch ( IOException e )
67             {
68                 throw new RuntimeException JavaDoc("Could not load font metrics: " + e.getMessage());
69             }
70             finally
71             {
72                 if (metricsIn != null)
73                 {
74                     try
75                     {
76                         metricsIn.close();
77                     }
78                     catch ( IOException ignore ) { }
79                 }
80             }
81         }
82
83         String JavaDoc fontName = font.getName();
84
85         if (fontDetailsMap.get(fontName) == null)
86         {
87             FontDetails fontDetails = FontDetails.create(fontName, fontMetricsProps);
88             fontDetailsMap.put( fontName, fontDetails );
89             return fontDetails;
90         }
91         else
92         {
93             return (FontDetails) fontDetailsMap.get(fontName);
94         }
95
96     }
97 }
98
Popular Tags