001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel;
019:
020: import java.util.*;
021: import java.awt.*;
022: import java.io.*;
023:
024: /**
025: * Allows the user to lookup the font metrics for a particular font without
026: * actually having the font on the system. The font details are loaded
027: * as a resource from the POI jar file (or classpath) and should be contained
028: * in path "/font_metrics.properties". The font widths are for a 10 point
029: * version of the font. Use a multiplier for other sizes.
030: *
031: * @author Glen Stampoultzis (glens at apache.org)
032: */
033: class StaticFontMetrics {
034: /** The font metrics property file we're using */
035: private static Properties fontMetricsProps;
036: /** Our cache of font details we've already looked up */
037: private static Map fontDetailsMap = new HashMap();
038:
039: /**
040: * Retrieves the fake font details for a given font.
041: * @param font the font to lookup.
042: * @return the fake font.
043: */
044: public static FontDetails getFontDetails(Font font) {
045: // If we haven't already identified out font metrics file,
046: // figure out which one to use and load it
047: if (fontMetricsProps == null) {
048: InputStream metricsIn = null;
049: try {
050: fontMetricsProps = new Properties();
051:
052: // Check to see if the font metric file was specified
053: // as a system property
054: String propFileName = null;
055: try {
056: propFileName = System
057: .getProperty("font.metrics.filename");
058: } catch (SecurityException e) {
059: }
060:
061: if (propFileName != null) {
062: File file = new File(propFileName);
063: if (!file.exists())
064: throw new FileNotFoundException(
065: "font_metrics.properties not found at path "
066: + file.getAbsolutePath());
067: metricsIn = new FileInputStream(file);
068: } else {
069: // Use the built-in font metrics file off the classpath
070: metricsIn = FontDetails.class
071: .getResourceAsStream("/font_metrics.properties");
072: if (metricsIn == null)
073: throw new FileNotFoundException(
074: "font_metrics.properties not found in classpath");
075: }
076: fontMetricsProps.load(metricsIn);
077: } catch (IOException e) {
078: throw new RuntimeException(
079: "Could not load font metrics: "
080: + e.getMessage());
081: } finally {
082: if (metricsIn != null) {
083: try {
084: metricsIn.close();
085: } catch (IOException ignore) {
086: }
087: }
088: }
089: }
090:
091: // Grab the base name of the font they've asked about
092: String fontName = font.getName();
093:
094: // Some fonts support plain/bold/italic/bolditalic variants
095: // Others have different font instances for bold etc
096: // (eg font.dialog.plain.* vs font.Californian FB Bold.*)
097: String fontStyle = "";
098: if (font.isPlain())
099: fontStyle += "plain";
100: if (font.isBold())
101: fontStyle += "bold";
102: if (font.isItalic())
103: fontStyle += "italic";
104:
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
108: .buildFontHeightProperty(fontName)) == null
109: && fontMetricsProps.get(FontDetails
110: .buildFontHeightProperty(fontName + "."
111: + fontStyle)) != null) {
112: // Need to add on the style to the font name
113: fontName += "." + fontStyle;
114: }
115:
116: // Get the details on this font
117: if (fontDetailsMap.get(fontName) == null) {
118: FontDetails fontDetails = FontDetails.create(fontName,
119: fontMetricsProps);
120: fontDetailsMap.put(fontName, fontDetails);
121: return fontDetails;
122: } else {
123: return (FontDetails) fontDetailsMap.get(fontName);
124: }
125:
126: }
127: }
|