Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / java / org / apache / poi / hssf / usermodel / StaticFontMetrics.java
blob41efa6d0797cd5ec7fd27f0651cedeb92a44eaf9
1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
18 package org.apache.poi.hssf.usermodel;
20 import java.util.*;
21 import java.awt.*;
22 import java.io.*;
24 /**
25 * Allows the user to lookup the font metrics for a particular font without
26 * actually having the font on the system. The font details are loaded
27 * as a resource from the POI jar file (or classpath) and should be contained
28 * in path "/font_metrics.properties". The font widths are for a 10 point
29 * version of the font. Use a multiplier for other sizes.
31 * @author Glen Stampoultzis (glens at apache.org)
33 class StaticFontMetrics
35 /** The font metrics property file we're using */
36 private static Properties fontMetricsProps;
37 /** Our cache of font details we've already looked up */
38 private static Map fontDetailsMap = new HashMap();
40 /**
41 * Retrieves the fake font details for a given font.
42 * @param font the font to lookup.
43 * @return the fake font.
45 public static FontDetails getFontDetails(Font font)
47 // If we haven't already identified out font metrics file,
48 // figure out which one to use and load it
49 if (fontMetricsProps == null)
51 InputStream metricsIn = null;
52 try
54 fontMetricsProps = new Properties();
56 // Check to see if the font metric file was specified
57 // as a system property
58 String propFileName = null;
59 try {
60 propFileName = System.getProperty("font.metrics.filename");
61 } catch(SecurityException e) {}
63 if (propFileName != null) {
64 File file = new File(propFileName);
65 if (!file.exists())
66 throw new FileNotFoundException("font_metrics.properties not found at path " + file.getAbsolutePath());
67 metricsIn = new FileInputStream(file);
69 else {
70 // Use the built-in font metrics file off the classpath
71 metricsIn = FontDetails.class.getResourceAsStream("/font_metrics.properties");
72 if (metricsIn == null)
73 throw new FileNotFoundException("font_metrics.properties not found in classpath");
75 fontMetricsProps.load(metricsIn);
77 catch ( IOException e )
79 throw new RuntimeException("Could not load font metrics: " + e.getMessage());
81 finally
83 if (metricsIn != null)
85 try
87 metricsIn.close();
89 catch ( IOException ignore ) { }
94 // Grab the base name of the font they've asked about
95 String fontName = font.getName();
97 // Some fonts support plain/bold/italic/bolditalic variants
98 // Others have different font instances for bold etc
99 // (eg font.dialog.plain.* vs font.Californian FB Bold.*)
100 String fontStyle = "";
101 if(font.isPlain()) fontStyle += "plain";
102 if(font.isBold()) fontStyle += "bold";
103 if(font.isItalic()) fontStyle += "italic";
105 // Do we have a definition for this font with just the name?
106 // If not, check with the font style added
107 if(fontMetricsProps.get(FontDetails.buildFontHeightProperty(fontName)) == null &&
108 fontMetricsProps.get(FontDetails.buildFontHeightProperty(fontName+"."+fontStyle)) != null) {
109 // Need to add on the style to the font name
110 fontName += "." + fontStyle;
113 // Get the details on this font
114 if (fontDetailsMap.get(fontName) == null) {
115 FontDetails fontDetails = FontDetails.create(fontName, fontMetricsProps);
116 fontDetailsMap.put( fontName, fontDetails );
117 return fontDetails;
118 } else {
119 return (FontDetails) fontDetailsMap.get(fontName);