zhao
2021-06-11 98186752629a7bd38965418af84db382d90b9c07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* ====================================================================
   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;
 
    /// <summary>
    /// Stores width and height details about a font.
    /// @author Glen Stampoultzis (glens at apache.org)
    /// </summary>
    public class FontDetails
    {
        private String fontName;
        private int height;
        private Hashtable charWidths = new Hashtable();
 
        /// <summary>
        /// Construct the font details with the given name and height.
        /// </summary>
        /// <param name="fontName">The font name.</param>
        /// <param name="height">The height of the font.</param>
        public FontDetails(String fontName, int height)
        {
            this.fontName = fontName;
            this.height = height;
        }
 
        /// <summary>
        /// Gets the name of the font.
        /// </summary>
        /// <returns></returns>
        public String GetFontName()
        {
            return fontName;
        }
 
        /// <summary>
        /// Gets the height.
        /// </summary>
        /// <returns></returns>
        public int GetHeight()
        {
            return height;
        }
 
        /// <summary>
        /// Adds the char.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <param name="width">The width.</param>
        public void AddChar(char c, int width)
        {
            charWidths[c] = width;
        }
 
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="c">The character.</param>
        /// <returns></returns>
        public int GetCharWidth(char c)
        {
            object widthInteger = charWidths[c];
            if (widthInteger == null && c != 'W')
                return GetCharWidth('W');
            else
                return (int)widthInteger;
        }
 
        /// <summary>
        /// Adds the chars.
        /// </summary>
        /// <param name="Chars">The chars.</param>
        /// <param name="widths">The widths.</param>
        public void AddChars(char[] Chars, int[] widths)
        {
            for (int i = 0; i < Chars.Length; i++)
            {
                if (Chars[i] != ' ')
                {
                    charWidths[Chars[i]] = widths[i];
                }
            }
        }
 
        /// <summary>
        /// Builds the font height property.
        /// </summary>
        /// <param name="fontName">Name of the font.</param>
        /// <returns></returns>
        public static String BuildFontHeightProperty(String fontName)
        {
            return "font." + fontName + ".height";
        }
        /// <summary>
        /// Builds the font widths property.
        /// </summary>
        /// <param name="fontName">Name of the font.</param>
        /// <returns></returns>
        public static String BuildFontWidthsProperty(String fontName)
        {
            return "font." + fontName + ".widths";
        }
        /// <summary>
        /// Builds the font chars property.
        /// </summary>
        /// <param name="fontName">Name of the font.</param>
        /// <returns></returns>
        public static String BuildFontCharsProperty(String fontName)
        {
            return "font." + fontName + ".characters";
        }
 
        /// <summary>
        /// Create an instance of 
        /// <c>FontDetails</c>
        ///  by loading them from the
        /// provided property object.
        /// </summary>
        /// <param name="fontName">the font name.</param>
        /// <param name="fontMetricsProps">the property object holding the details of this
        /// particular font.</param>
        /// <returns>a new FontDetails instance.</returns>
        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;
        }
 
        /// <summary>
        /// Gets the width of all Chars in a string.
        /// </summary>
        /// <param name="str">The string to measure.</param>
        /// <returns>The width of the string for a 10 point font.</returns>
        public int GetStringWidth(String str)
        {
            int width = 0;
            for (int i = 0; i < str.Length; i++)
            {
                width += GetCharWidth(str[i]);
            }
            return width;
        }
 
        /// <summary>
        /// Split the given string into an array of strings using the given
        /// delimiter.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="separator">The separator.</param>
        /// <param name="max">The max.</param>
        /// <returns></returns>
        private static String[] Split(String text, String separator, int max)
        {
            String[] list = text.Split(separator.ToCharArray());
            return list;
        }
 
 
    }
}