KickJava   Java API By Example, From Geeks To Geeks.

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


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: CharacterSet.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.fonts;
21
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.fop.render.afp.modca.AFPConstants;
28 import org.apache.fop.render.afp.tools.StringUtils;
29
30 /**
31  * The IBM Font Object Content Architecture (FOCA) supports presentation
32  * of character shapes by defining their characteristics, which include
33  * font description information for identifying the characters, font metric
34  * information for positioning the characters, and character shape information
35  * for presenting the character images.
36  * <p/>
37  * Presenting a graphic character on a presentation surface requires
38  * information on the rotation and position of character on the physical
39  * or logical page.
40  * <p/>
41  * This class proivdes font metric information for a particular font
42  * as identified by the character set name. This information is obtained
43  * directly from the AFP font files which must be installed in the path
44  * specified in the afp-fonts xml definition file.
45  * <p/>
46  */

47 public class CharacterSet {
48
49     /**
50      * Static logging instance
51      */

52     protected static final Log log = LogFactory.getLog(CharacterSet.class.getName());
53
54     /**
55      * The code page to which the character set relates
56      */

57     protected String JavaDoc _codePage;
58
59     /**
60      * The encoding used for the code page
61      */

62     protected String JavaDoc _encoding;
63
64     /**
65      * The character set relating to the font
66      */

67     protected String JavaDoc _name;
68
69     /**
70      * The name of the character set as EBCIDIC bytes
71      */

72     private byte[] _nameBytes;
73
74     /**
75      * The path to the installed fonts
76      */

77     protected String JavaDoc _path;
78
79     /**
80      * Indicator as to whether to metrics have been loaded
81      */

82     private boolean _isMetricsLoaded = false;
83
84     /**
85      * The current orientation (currently only 0 is suppoted by FOP)
86      */

87     private String JavaDoc _currentOrientation = "0";
88
89     /**
90      * The collection of objects for each orientation
91      */

92     private HashMap JavaDoc _characterSetOrientations;
93
94     /**
95      * Constructor for the CharacterSetMetric object, the character set is used
96      * to load the font information from the actual AFP font.
97      * @param codePage the code page identifier
98      * @param encoding the encoding of the font
99      * @param name the character set name
100      * @param path the path to the installed afp fonts
101      */

102     public CharacterSet(
103         String JavaDoc codePage,
104         String JavaDoc encoding,
105         String JavaDoc name,
106         String JavaDoc path) {
107
108         if (name.length() > 8) {
109             String JavaDoc msg = "Character set name must be a maximum of 8 characters " + name;
110             log.error("Constructor:: " + msg);
111             throw new IllegalArgumentException JavaDoc(msg);
112         }
113
114         if (name.length() < 8) {
115             _name = StringUtils.rpad(name, ' ', 8);
116         } else {
117             _name = name;
118         }
119
120         try {
121
122             _nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
123
124         } catch (UnsupportedEncodingException JavaDoc usee) {
125
126             _nameBytes = name.getBytes();
127             log.warn(
128                 "Constructor:: UnsupportedEncodingException translating the name "
129                 + name);
130
131         }
132
133         _codePage = codePage;
134         _encoding = encoding;
135         _path = path;
136         _characterSetOrientations = new HashMap JavaDoc(4);
137
138     }
139
140     /**
141      * Add character set metric information for the different orientations
142      * @param cso the metrics for the orientation
143      */

144     public void addCharacterSetOrientation(CharacterSetOrientation cso) {
145
146         _characterSetOrientations.put(
147             String.valueOf(cso.getOrientation()),
148             cso);
149
150     }
151
152     /**
153      * Ascender height is the distance from the character baseline to the
154      * top of the character box. A negative ascender height signifies that
155      * all of the graphic character is below the character baseline. For
156      * a character rotation other than 0, ascender height loses its
157      * meaning when the character is lying on its side or is upside down
158      * with respect to normal viewing orientation. For the general case,
159      * Ascender Height is the character´┐Żs most positive y-axis value.
160      * For bounded character boxes, for a given character having an
161      * ascender, ascender height and baseline offset are equal.
162      * @return the ascender value in millipoints
163      */

164     public int getAscender() {
165         load();
166         return getCharacterSetOrientation().getAscender();
167     }
168
169     /**
170      * Cap height is the average height of the uppercase characters in
171      * a font. This value is specified by the designer of a font and is
172      * usually the height of the uppercase M.
173      * @return the cap height value in millipoints
174      */

175     public int getCapHeight() {
176         load();
177         return getCharacterSetOrientation().getCapHeight();
178     }
179
180     /**
181      * Descender depth is the distance from the character baseline to
182      * the bottom of a character box. A negative descender depth signifies
183      * that all of the graphic character is above the character baseline.
184      * @return the descender value in millipoints
185      */

186     public int getDescender() {
187         load();
188         return getCharacterSetOrientation().getDescender();
189     }
190
191     /**
192      * The first character in the character set
193      * @return the first character
194      */

195     public int getFirstChar() {
196         load();
197         return getCharacterSetOrientation().getFirstChar();
198     }
199
200     /**
201      * The last character in the character set
202      * @return the last character
203      */

204     public int getLastChar() {
205         load();
206         return getCharacterSetOrientation().getLastChar();
207     }
208
209     /**
210      * @return the path where the font resources are installed
211      */

212     public String JavaDoc getPath() {
213         return _path;
214     }
215
216     /**
217      * Get the width (in 1/1000ths of a point size) of all characters
218      * @return the widths of all characters
219      */

220     public int[] getWidths() {
221         load();
222         return getCharacterSetOrientation().getWidths();
223     }
224
225     /**
226      * XHeight refers to the height of the lower case letters above the baseline.
227      * @return the typical height of characters
228      */

229     public int getXHeight() {
230         load();
231         return getCharacterSetOrientation().getXHeight();
232     }
233
234     /**
235      * Get the width (in 1/1000ths of a point size) of the character
236      * identified by the parameter passed.
237      * @param character the character from which the width will be calculated
238      * @return the width of the character
239      */

240     public int width(int character) {
241         load();
242         return getCharacterSetOrientation().width(character);
243     }
244
245     /**
246      * Lazy creation of the character metrics, the afp font file will only
247      * be processed on a method call requiring the metric information.
248      */

249     private void load() {
250
251         if (!_isMetricsLoaded) {
252
253             AFPFontReader.loadCharacterSetMetric(this);
254             _isMetricsLoaded = true;
255
256         }
257
258     }
259
260     /**
261      * Returns the AFP character set identifier
262      * @return String
263      */

264     public String JavaDoc getName() {
265         return _name;
266     }
267
268     /**
269      * Returns the AFP character set identifier
270      * @return byte[]
271      */

272     public byte[] getNameBytes() {
273         return _nameBytes;
274     }
275
276     /**
277      * Returns the AFP code page identifier
278      * @return String
279      */

280     public String JavaDoc getCodePage() {
281         return _codePage;
282     }
283
284     /**
285      * Returns the AFP code page encoding
286      * @return String
287      */

288     public String JavaDoc getEncoding() {
289         return _encoding;
290     }
291
292     /**
293      * Helper method to return the current CharacterSetOrientation, note
294      * that FOP does not yet implement the "reference-orientation"
295      * attribute therefore we always use the orientation zero degrees,
296      * Other orientation information is captured for use by a future
297      * implementation (whenever FOP implement the mechanism). This is also
298      * the case for landscape prints which use an orientation of 270 degrees,
299      * in 99.9% of cases the font metrics will be the same as the 0 degrees
300      * therefore the implementation currely will always use 0 degrees.
301      * @return characterSetOrentation The current orientation metrics.
302      */

303     private CharacterSetOrientation getCharacterSetOrientation() {
304
305         CharacterSetOrientation c =
306             (CharacterSetOrientation) _characterSetOrientations.get(
307             _currentOrientation);
308         return c;
309
310     }
311
312     /**
313      * Map a Unicode character to a code point in the font.
314      * The code tables are already converted to Unicode therefore
315      * we can use the identity mapping.
316      * @param c character to map
317      * @return the mapped character
318      */

319     public char mapChar(char c) {
320         return c;
321     }
322
323 }
324
Popular Tags