/* ==================================================================== Licensed to the Apache Software Foundation (ASF) Under one or more contributor license agreements. See the NOTICE file distributed with this work for Additional information regarding copyright ownership. The ASF licenses this file to You Under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed Under the License is distributed on an "AS Is" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations Under the License. ==================================================================== */ namespace HH.WMS.Utils.NPOI.HSSF.UserModel { using System; using System.Collections; using System.IO; using HH.WMS.Utils.NPOI.Util; using System.Text; using HH.WMS.Utils.NPOI.Util.Collections; using System.Globalization; /// /// Stores width and height details about a font. /// @author Glen Stampoultzis (glens at apache.org) /// public class FontDetails { private String fontName; private int height; private Hashtable charWidths = new Hashtable(); /// /// Construct the font details with the given name and height. /// /// The font name. /// The height of the font. public FontDetails(String fontName, int height) { this.fontName = fontName; this.height = height; } /// /// Gets the name of the font. /// /// public String GetFontName() { return fontName; } /// /// Gets the height. /// /// public int GetHeight() { return height; } /// /// Adds the char. /// /// The c. /// The width. public void AddChar(char c, int width) { charWidths[c] = width; } /// /// Retrieves the width of the specified Char. If the metrics for /// a particular Char are not available it defaults to returning the /// width for the 'W' Char. /// /// The character. /// public int GetCharWidth(char c) { object widthInteger = charWidths[c]; if (widthInteger == null && c != 'W') return GetCharWidth('W'); else return (int)widthInteger; } /// /// Adds the chars. /// /// The chars. /// The widths. public void AddChars(char[] Chars, int[] widths) { for (int i = 0; i < Chars.Length; i++) { if (Chars[i] != ' ') { charWidths[Chars[i]] = widths[i]; } } } /// /// Builds the font height property. /// /// Name of the font. /// public static String BuildFontHeightProperty(String fontName) { return "font." + fontName + ".height"; } /// /// Builds the font widths property. /// /// Name of the font. /// public static String BuildFontWidthsProperty(String fontName) { return "font." + fontName + ".widths"; } /// /// Builds the font chars property. /// /// Name of the font. /// public static String BuildFontCharsProperty(String fontName) { return "font." + fontName + ".characters"; } /// /// Create an instance of /// FontDetails /// by loading them from the /// provided property object. /// /// the font name. /// the property object holding the details of this /// particular font. /// a new FontDetails instance. public static FontDetails Create(String fontName, Properties fontMetricsProps) { String heightStr = fontMetricsProps[BuildFontHeightProperty(fontName)]; String widthsStr = fontMetricsProps[BuildFontWidthsProperty(fontName)]; String CharsStr = fontMetricsProps[BuildFontCharsProperty(fontName)]; // Ensure that this Is a font we know about if (heightStr == null || widthsStr == null || CharsStr == null) { // We don't know all we need to about this font // Since we don't know its sizes, we can't work with it throw new ArgumentException("The supplied FontMetrics doesn't know about the font '" + fontName + "', so we can't use it. Please Add it to your font metrics file (see StaticFontMetrics.GetFontDetails"); } int height = int.Parse(heightStr, CultureInfo.InvariantCulture); FontDetails d = new FontDetails(fontName, height); String[] CharsStrArray = Split(CharsStr, ",", -1); String[] widthsStrArray = Split(widthsStr, ",", -1); if (CharsStrArray.Length != widthsStrArray.Length) throw new Exception("Number of Chars does not number of widths for font " + fontName); for (int i = 0; i < widthsStrArray.Length; i++) { if (CharsStrArray[i].Trim().Length != 0) d.AddChar(CharsStrArray[i].Trim()[0], int.Parse(widthsStrArray[i], CultureInfo.InvariantCulture)); } return d; } /// /// Gets the width of all Chars in a string. /// /// The string to measure. /// The width of the string for a 10 point font. public int GetStringWidth(String str) { int width = 0; for (int i = 0; i < str.Length; i++) { width += GetCharWidth(str[i]); } return width; } /// /// Split the given string into an array of strings using the given /// delimiter. /// /// The text. /// The separator. /// The max. /// private static String[] Split(String text, String separator, int max) { String[] list = text.Split(separator.ToCharArray()); return list; } } }