KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fonts > Font


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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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  */

17
18 /* $Id: Font.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.fonts;
21
22 import java.util.Map JavaDoc;
23
24 /**
25  * This class holds font state information and provides access to the font
26  * metrics.
27  */

28 public class Font {
29
30     /** Default fallback key */
31     public static final FontTriplet DEFAULT_FONT = new FontTriplet("any", "normal", 400);
32     /** Normal font weight */
33     public static final int NORMAL = 400;
34     /** Bold font weight */
35     public static final int BOLD = 700;
36
37     private String JavaDoc fontName;
38     private FontTriplet triplet;
39     private int fontSize;
40
41     /**
42      * normal or small-caps font
43      */

44     //private int fontVariant;
45

46     private FontMetrics metric;
47
48     /**
49      * Main constructor
50      * @param key key of the font
51      * @param triplet the font triplet that was used to lookup this font (may be null)
52      * @param met font metrics
53      * @param fontSize font size
54      */

55     public Font(String JavaDoc key, FontTriplet triplet, FontMetrics met, int fontSize) {
56         this.fontName = key;
57         this.triplet = triplet;
58         this.metric = met;
59         this.fontSize = fontSize;
60     }
61
62     /**
63      * Returns the font's ascender.
64      * @return the ascender
65      */

66     public int getAscender() {
67         return metric.getAscender(fontSize) / 1000;
68     }
69
70     /**
71      * Returns the font's CapHeight.
72      * @return the capital height
73      */

74     public int getCapHeight() {
75         return metric.getCapHeight(fontSize) / 1000;
76     }
77
78     /**
79      * Returns the font's Descender.
80      * @return the descender
81      */

82     public int getDescender() {
83         return metric.getDescender(fontSize) / 1000;
84     }
85
86     /**
87      * Returns the font's name.
88      * @return the font name
89      */

90     public String JavaDoc getFontName() {
91         return fontName;
92     }
93     
94     /** @return the font triplet that selected this font */
95     public FontTriplet getFontTriplet() {
96         return this.triplet;
97     }
98
99     /**
100      * Returns the font size
101      * @return the font size
102      */

103     public int getFontSize() {
104         return fontSize;
105     }
106
107     /**
108      * Returns the XHeight
109      * @return the XHeight
110      */

111     public int getXHeight() {
112         return metric.getXHeight(fontSize) / 1000;
113     }
114
115     /** @return true if the font has kerning info */
116     public boolean hasKerning() {
117         return metric.hasKerningInfo();
118     }
119     
120     /**
121      * Returns the font's kerning table
122      * @return the kerning table
123      */

124     public Map JavaDoc getKerning() {
125         if (metric.hasKerningInfo()) {
126             return metric.getKerningInfo();
127         } else {
128             return java.util.Collections.EMPTY_MAP;
129         }
130     }
131     
132     /**
133      * Returns the amount of kerning between two characters.
134      * @param ch1 first character
135      * @param ch2 second character
136      * @return the distance to adjust for kerning, 0 if there's no kerning
137      */

138     public int getKernValue(char ch1, char ch2) {
139         Map JavaDoc kernPair = (Map JavaDoc)getKerning().get(new Integer JavaDoc(ch1));
140         if (kernPair != null) {
141             Integer JavaDoc width = (Integer JavaDoc)kernPair.get(new Integer JavaDoc(ch2));
142             if (width != null) {
143                 return width.intValue();
144             }
145         }
146         return 0;
147     }
148
149     /**
150      * Returns the width of a character
151      * @param charnum character to look up
152      * @return width of the character
153      */

154     public int getWidth(int charnum) {
155         // returns width of given character number in millipoints
156
return (metric.getWidth(charnum, fontSize) / 1000);
157     }
158
159     /**
160      * Map a java character (unicode) to a font character.
161      * Default uses CodePointMapping.
162      * @param c character to map
163      * @return the mapped character
164      */

165     public char mapChar(char c) {
166
167         if (metric instanceof org.apache.fop.fonts.Typeface) {
168             return ((org.apache.fop.fonts.Typeface)metric).mapChar(c);
169         }
170
171         // Use default CodePointMapping
172
char d = CodePointMapping.getMapping("WinAnsiEncoding").mapChar(c);
173         if (d != 0) {
174             c = d;
175         } else {
176             c = '#';
177         }
178
179         return c;
180     }
181     
182     /**
183      * Determines whether this font contains a particular character/glyph.
184      * @param c character to check
185      * @return True if the character is supported, Falso otherwise
186      */

187     public boolean hasChar(char c) {
188         if (metric instanceof org.apache.fop.fonts.Typeface) {
189             return ((org.apache.fop.fonts.Typeface)metric).hasChar(c);
190         } else {
191             // Use default CodePointMapping
192
return (CodePointMapping.getMapping("WinAnsiEncoding").mapChar(c) > 0);
193         }
194     }
195
196     /**
197      * @see java.lang.Object#toString()
198      */

199     public String JavaDoc toString() {
200         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
201         sbuf.append('(');
202         /*
203         sbuf.append(fontFamily);
204         sbuf.append(',');*/

205         sbuf.append(fontName);
206         sbuf.append(',');
207         sbuf.append(fontSize);
208         /*
209         sbuf.append(',');
210         sbuf.append(fontStyle);
211         sbuf.append(',');
212         sbuf.append(fontWeight);*/

213         sbuf.append(')');
214         return sbuf.toString();
215     }
216
217     /**
218      * Helper method for getting the width of a unicode char
219      * from the current fontstate.
220      * This also performs some guessing on widths on various
221      * versions of space that might not exists in the font.
222      * @param c character to inspect
223      * @return the width of the character
224      */

225     public int getCharWidth(char c) {
226         int width;
227
228         if ((c == '\n') || (c == '\r') || (c == '\t') || (c == '\u00A0')) {
229             width = getCharWidth(' ');
230         } else {
231             if (hasChar(c)) {
232                 width = getWidth(mapChar(c));
233             } else {
234                 width = -1;
235             }
236             if (width <= 0) {
237                 // Estimate the width of spaces not represented in
238
// the font
239
int em = getFontSize(); //http://en.wikipedia.org/wiki/Em_(typography)
240
int en = em / 2; //http://en.wikipedia.org/wiki/En_(typography)
241

242                 if (c == ' ') {
243                     width = em;
244                 } else if (c == '\u2000') {
245                     width = en;
246                 } else if (c == '\u2001') {
247                     width = em;
248                 } else if (c == '\u2002') {
249                     width = em / 2;
250                 } else if (c == '\u2003') {
251                     width = getFontSize();
252                 } else if (c == '\u2004') {
253                     width = em / 3;
254                 } else if (c == '\u2005') {
255                     width = em / 4;
256                 } else if (c == '\u2006') {
257                     width = em / 6;
258                 } else if (c == '\u2007') {
259                     width = getCharWidth('0');
260                 } else if (c == '\u2008') {
261                     width = getCharWidth('.');
262                 } else if (c == '\u2009') {
263                     width = em / 5;
264                 } else if (c == '\u200A') {
265                     width = em / 10;
266                 } else if (c == '\u200B') {
267                     width = 0;
268                 } else if (c == '\u202F') {
269                     width = getCharWidth(' ') / 2;
270                 } else if (c == '\u3000') {
271                     width = getCharWidth(' ') * 2;
272                 } else {
273                     //Will be internally replaced by "#" if not found
274
width = getWidth(mapChar(c));
275                 }
276             }
277         }
278
279         return width;
280     }
281
282     /**
283      * Calculates the word width.
284      * @param word text to get width for
285      * @return the width of the text
286      */

287     public int getWordWidth(String JavaDoc word) {
288         if (word == null) {
289             return 0;
290         }
291         int wordLength = word.length();
292         int width = 0;
293         char[] characters = new char[wordLength];
294         word.getChars(0, wordLength, characters, 0);
295         for (int i = 0; i < wordLength; i++) {
296             width += getCharWidth(characters[i]);
297         }
298         return width;
299     }
300
301 }
302
303
304
Popular Tags