KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 /**
25  * Stores width and height details about a font.
26  *
27  * @author Glen Stampoultzis (glens at apache.org)
28  */

29 public class FontDetails
30 {
31     private String JavaDoc fontName;
32     private int height;
33     private Map JavaDoc charWidths = new HashMap JavaDoc();
34
35     /**
36      * Construct the font details with the given name and height.
37      *
38      * @param fontName The font name.
39      * @param height The height of the font.
40      */

41     public FontDetails( String JavaDoc fontName, int height )
42     {
43         this.fontName = fontName;
44         this.height = height;
45     }
46
47     public String JavaDoc getFontName()
48     {
49         return fontName;
50     }
51
52     public int getHeight()
53     {
54         return height;
55     }
56
57     public void addChar( char c, int width )
58     {
59         charWidths.put(new Character JavaDoc(c), new Integer JavaDoc(width));
60     }
61
62     /**
63      * Retrieves the width of the specified character. If the metrics for
64      * a particular character are not available it defaults to returning the
65      * width for the 'W' character.
66      */

67     public int getCharWidth( char c )
68     {
69         Integer JavaDoc widthInteger = (Integer JavaDoc)(charWidths.get(new Character JavaDoc(c)));
70         if (widthInteger == null && c != 'W')
71             return getCharWidth('W');
72         else
73             return widthInteger.intValue();
74     }
75
76     public void addChars( char[] characters, int[] widths )
77     {
78         for ( int i = 0; i < characters.length; i++ )
79         {
80             charWidths.put( new Character JavaDoc(characters[i]), new Integer JavaDoc(widths[i]));
81         }
82     }
83
84     /**
85      * Create an instance of <code>FontDetails</code> by loading them from the
86      * provided property object.
87      * @param fontName the font name
88      * @param fontMetricsProps the property object holding the details of this
89      * particular font.
90      * @return a new FontDetails instance.
91      */

92     public static FontDetails create( String JavaDoc fontName, Properties JavaDoc fontMetricsProps )
93     {
94         String JavaDoc heightStr = fontMetricsProps.getProperty( "font." + fontName + ".height");
95         String JavaDoc widthsStr = fontMetricsProps.getProperty( "font." + fontName + ".widths");
96         String JavaDoc charactersStr = fontMetricsProps.getProperty( "font." + fontName + ".characters");
97         int height = Integer.parseInt(heightStr);
98         FontDetails d = new FontDetails(fontName, height);
99         String JavaDoc[] charactersStrArray = split(charactersStr, ",", -1);
100         String JavaDoc[] widthsStrArray = split(widthsStr, ",", -1);
101         if (charactersStrArray.length != widthsStrArray.length)
102             throw new RuntimeException JavaDoc("Number of characters does not number of widths for font " + fontName);
103         for ( int i = 0; i < widthsStrArray.length; i++ )
104         {
105             if (charactersStrArray[i].length() != 0)
106                 d.addChar(charactersStrArray[i].charAt(0), Integer.parseInt(widthsStrArray[i]));
107         }
108         return d;
109     }
110
111     /**
112      * Gets the width of all characters in a string.
113      *
114      * @param str The string to measure.
115      * @return The width of the string for a 10 point font.
116      */

117     public int getStringWidth(String JavaDoc str)
118     {
119         int width = 0;
120         for (int i = 0; i < str.length(); i++)
121         {
122             width += getCharWidth(str.charAt(i));
123         }
124         return width;
125     }
126
127     /**
128      * Split the given string into an array of strings using the given
129      * delimiter.
130      */

131     private static String JavaDoc[] split(String JavaDoc text, String JavaDoc separator, int max)
132     {
133         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(text, separator);
134         int listSize = tok.countTokens();
135         if(max != -1 && listSize > max)
136             listSize = max;
137         String JavaDoc list[] = new String JavaDoc[listSize];
138         for(int i = 0; tok.hasMoreTokens(); i++)
139         {
140             if(max != -1 && i == listSize - 1)
141             {
142                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc((text.length() * (listSize - i)) / listSize);
143                 while(tok.hasMoreTokens())
144                 {
145                     buf.append(tok.nextToken());
146                     if(tok.hasMoreTokens())
147                         buf.append(separator);
148                 }
149                 list[i] = buf.toString().trim();
150                 break;
151             }
152             list[i] = tok.nextToken().trim();
153         }
154
155         return list;
156     }
157
158
159 }
160
Popular Tags