KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > afp > fonts > RasterFont


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: RasterFont.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.fonts;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.fop.render.afp.exceptions.FontRuntimeException;
29
30 /**
31  * A font where each character is stored as an array of pixels (a bitmap). Such
32  * fonts are not easily scalable, in contrast to vectored fonts. With this type
33  * of font, the font metrics information is held in character set files (one for
34  * each size and style). <p/>
35  *
36  */

37 public class RasterFont extends AFPFont {
38
39     /** Static logging instance */
40     protected static final Log log = LogFactory.getLog("org.apache.fop.render.afp.fonts");
41
42     private HashMap JavaDoc _characterSets = new HashMap JavaDoc();
43
44     private CharacterSet _characterSet = null;
45
46     /**
47      * Constructor for the raster font requires the name, weight and style
48      * attribute to be available as this forms the key to the font.
49      *
50      * @param name
51      * the name of the font
52      */

53     public RasterFont(String JavaDoc name) {
54         super(name);
55     }
56
57     public void addCharacterSet(int size, CharacterSet characterSet) {
58
59         _characterSets.put(String.valueOf(size), characterSet);
60
61         _characterSet = characterSet;
62
63     }
64
65     /**
66      * Get the character set metrics for the specified point size.
67      *
68      * @param size the point size
69      * @return the character set metrics
70      */

71     public CharacterSet getCharacterSet(int size) {
72
73         String JavaDoc pointsize = String.valueOf(size / 1000);
74         CharacterSet csm = (CharacterSet) _characterSets.get(pointsize);
75         if (csm == null) {
76             csm = (CharacterSet) _characterSets.get(size + "mpt");
77         }
78         if (csm == null) {
79             // Get char set with nearest font size
80
int distance = Integer.MAX_VALUE;
81             for (Iterator JavaDoc it = _characterSets.entrySet().iterator(); it.hasNext(); ) {
82                 Map.Entry JavaDoc me = (Map.Entry JavaDoc)it.next();
83                 String JavaDoc key = (String JavaDoc)me.getKey();
84                 if (!key.endsWith("mpt")) {
85                     int mpt = Integer.parseInt(key) * 1000;
86                     if (Math.abs(size - mpt) < distance) {
87                         distance = Math.abs(size - mpt);
88                         pointsize = (String JavaDoc)me.getKey();
89                         csm = (CharacterSet)me.getValue();
90                     }
91                 }
92             }
93             if (csm != null) {
94                 _characterSets.put(size + "mpt", csm);
95                 String JavaDoc msg = "No " + (size / 1000) + "pt font " + _name
96                     + " found, substituted with " + pointsize + "pt font";
97                 log.warn(msg);
98             }
99         }
100         if (csm == null) {
101             String JavaDoc msg = "No font found for font " + _name
102                 + " with point size " + pointsize;
103             log.error(msg);
104             throw new FontRuntimeException(msg);
105         }
106         return csm;
107
108     }
109
110     /**
111      * Get the first character in this font.
112      */

113     public int getFirstChar() {
114
115         Iterator JavaDoc i = _characterSets.values().iterator();
116         if (i.hasNext()) {
117             CharacterSet csm = (CharacterSet) i.next();
118             return csm.getFirstChar();
119         } else {
120             String JavaDoc msg = "getFirstChar() - No character set found for font:" + _name;
121             log.error(msg);
122             throw new FontRuntimeException(msg);
123         }
124
125     }
126
127     /**
128      * Get the last character in this font.
129      */

130     public int getLastChar() {
131
132         Iterator JavaDoc i = _characterSets.values().iterator();
133         if (i.hasNext()) {
134             CharacterSet csm = (CharacterSet) i.next();
135             return csm.getLastChar();
136         } else {
137             String JavaDoc msg = "getLastChar() - No character set found for font:" + _name;
138             log.error(msg);
139             throw new FontRuntimeException(msg);
140         }
141
142     }
143
144     /**
145      * The ascender is the part of a lowercase letter that extends above the
146      * "x-height" (the height of the letter "x"), such as "d", "t", or "h". Also
147      * used to denote the part of the letter extending above the x-height.
148      *
149      * @param size the point size
150      */

151     public int getAscender(int size) {
152
153         return getCharacterSet(size).getAscender();
154
155     }
156
157     /**
158      * Obtains the height of capital letters for the specified point size.
159      *
160      * @param size the point size
161      */

162     public int getCapHeight(int size) {
163
164         return getCharacterSet(size).getCapHeight();
165
166     }
167
168     /**
169      * The descender is the part of a lowercase letter that extends below the
170      * base line, such as "g", "j", or "p". Also used to denote the part of the
171      * letter extending below the base line.
172      *
173      * @param size the point size
174      */

175     public int getDescender(int size) {
176
177         return getCharacterSet(size).getDescender();
178
179     }
180
181     /**
182      * The "x-height" (the height of the letter "x").
183      *
184      * @param size the point size
185      */

186     public int getXHeight(int size) {
187
188         return getCharacterSet(size).getXHeight();
189
190     }
191
192     /**
193      * Obtain the width of the character for the specified point size.
194      */

195     public int getWidth(int character, int size) {
196
197         return getCharacterSet(size).width(character);
198
199     }
200
201     /**
202      * Get the getWidth (in 1/1000ths of a point size) of all characters in this
203      * character set.
204      *
205      * @param size
206      * the point size
207      * @return the widths of all characters
208      */

209     public int[] getWidths(int size) {
210
211         return getCharacterSet(size).getWidths();
212
213     }
214
215     /**
216      * Get the getWidth (in 1/1000ths of a point size) of all characters in this
217      * character set.
218      *
219      * @return the widths of all characters
220      */

221     public int[] getWidths() {
222
223         return getWidths(1000);
224
225     }
226
227     /**
228      * Map a Unicode character to a code point in the font.
229      * @param c character to map
230      * @return the mapped character
231      */

232     public char mapChar(char c) {
233         return _characterSet.mapChar(c);
234     }
235
236     /**
237      * Get the encoding of the font.
238      * @return the encoding
239      */

240     public String JavaDoc getEncoding() {
241         return _characterSet.getEncoding();
242     }
243
244 }
Popular Tags