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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/*
* 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.
*/
/* $Id$ */
package org.apache.fop.afp.fonts;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.afp.AFPConstants;
import org.apache.fop.afp.util.StringUtils;
/**
* The IBM Font Object Content Architecture (FOCA) supports presentation
* of character shapes by defining their characteristics, which include
* font description information for identifying the characters, font metric
* information for positioning the characters, and character shape information
* for presenting the character images.
* <p/>
* Presenting a graphic character on a presentation surface requires
* information on the rotation and position of character on the physical
* or logical page.
* <p/>
* This class proivdes font metric information for a particular font
* as identified by the character set name. This information is obtained
* directly from the AFP font files which must be installed in the path
* specified in the afp-fonts xml definition file.
* <p/>
*/
public class CharacterSet {
/** Static logging instance */
protected static final Log log = LogFactory.getLog(CharacterSet.class.getName());
/** default codepage */
public static final String DEFAULT_CODEPAGE = "T1V10500";
/** default encoding */
public static final String DEFAULT_ENCODING = "Cp500";
private static final int MAX_NAME_LEN = 8;
/** The code page to which the character set relates */
protected String codePage;
/** The encoding used for the code page */
protected String encoding;
/** The charset encoder corresponding to this encoding */
private CharsetEncoder encoder;
/** The character set relating to the font */
protected String name;
/** The path to the installed fonts */
protected String path;
/** Indicator as to whether to metrics have been loaded */
private boolean isMetricsLoaded = false;
/** The current orientation (currently only 0 is supported by FOP) */
private final String currentOrientation = "0";
/** The collection of objects for each orientation */
private Map characterSetOrientations = null;
/**
* Constructor for the CharacterSetMetric object, the character set is used
* to load the font information from the actual AFP font.
*
* @param codePage the code page identifier
* @param encoding the encoding of the font
* @param name the character set name
* @param path the path to the installed afp fonts
*/
public CharacterSet(String codePage, String encoding, String name, String path) {
if (name.length() > MAX_NAME_LEN) {
String msg = "Character set name '" + name + "' must be a maximum of "
+ MAX_NAME_LEN + " characters";
log.error("Constructor:: " + msg);
throw new IllegalArgumentException(msg);
}
if (name.length() < MAX_NAME_LEN) {
this.name = StringUtils.rpad(name, ' ', MAX_NAME_LEN);
} else {
this.name = name;
}
this.codePage = codePage;
this.encoding = encoding;
this.encoder = Charset.forName(encoding).newEncoder();
this.encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
this.path = path;
this.characterSetOrientations = new java.util.HashMap(4);
}
/**
* Add character set metric information for the different orientations
*
* @param cso the metrics for the orientation
*/
public void addCharacterSetOrientation(CharacterSetOrientation cso) {
characterSetOrientations.put(
String.valueOf(cso.getOrientation()),
cso);
}
/**
* Ascender height is the distance from the character baseline to the
* top of the character box. A negative ascender height signifies that
* all of the graphic character is below the character baseline. For
* a character rotation other than 0, ascender height loses its
* meaning when the character is lying on its side or is upside down
* with respect to normal viewing orientation. For the general case,
* Ascender Height is the characters most positive y-axis value.
* For bounded character boxes, for a given character having an
* ascender, ascender height and baseline offset are equal.
*
* @return the ascender value in millipoints
*/
public int getAscender() {
load();
return getCharacterSetOrientation().getAscender();
}
/**
* Cap height is the average height of the uppercase characters in
* a font. This value is specified by the designer of a font and is
* usually the height of the uppercase M.
*
* @return the cap height value in millipoints
*/
public int getCapHeight() {
load();
return getCharacterSetOrientation().getCapHeight();
}
/**
* Descender depth is the distance from the character baseline to
* the bottom of a character box. A negative descender depth signifies
* that all of the graphic character is above the character baseline.
*
* @return the descender value in millipoints
*/
public int getDescender() {
load();
return getCharacterSetOrientation().getDescender();
}
/**
* Returns the first character in the character set
*
* @return the first character in the character set
*/
public int getFirstChar() {
load();
return getCharacterSetOrientation().getFirstChar();
}
/**
* Returns the last character in the character set
*
* @return the last character in the character set
*/
public int getLastChar() {
load();
return getCharacterSetOrientation().getLastChar();
}
/**
* Returns the path where the font resources are installed
*
* @return the path where the font resources are installed
*/
public String getPath() {
return path;
}
/**
* Get the width (in 1/1000ths of a point size) of all characters
*
* @return the widths of all characters
*/
public int[] getWidths() {
load();
return getCharacterSetOrientation().getWidths();
}
/**
* XHeight refers to the height of the lower case letters above the baseline.
*
* @return the typical height of characters
*/
public int getXHeight() {
load();
return getCharacterSetOrientation().getXHeight();
}
/**
* Get the width (in 1/1000ths of a point size) of the character
* identified by the parameter passed.
*
* @param character the character from which the width will be calculated
* @return the width of the character
*/
public int getWidth(int character) {
load();
return getCharacterSetOrientation().getWidth(character);
}
/**
* Lazy creation of the character metrics, the afp font file will only
* be processed on a method call requiring the metric information.
*/
private void load() {
if (!isMetricsLoaded) {
AFPFontReader afpFontReader = new AFPFontReader();
try {
afpFontReader.loadCharacterSetMetric(this);
isMetricsLoaded = true;
} catch (IOException e) {
String msg = "Failed to load the character set metrics for code page " + codePage;
log.error(msg);
throw new RuntimeException(e.getMessage());
}
}
}
/**
* Returns the AFP character set identifier
*
* @return the AFP character set identifier
*/
public String getName() {
return name;
}
/**
* Returns the AFP character set identifier as a byte array
*
* @return the AFP character set identifier as a byte array
*/
public byte[] getNameBytes() {
byte[] nameBytes = null;
try {
nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
} catch (UnsupportedEncodingException usee) {
nameBytes = name.getBytes();
log.warn(
"UnsupportedEncodingException translating the name " + name);
}
return nameBytes;
}
/**
* Returns the AFP code page identifier
*
* @return the AFP code page identifier
*/
public String getCodePage() {
return codePage;
}
/**
* Returns the AFP code page encoding
*
* @return the AFP code page encoding
*/
public String getEncoding() {
return encoding;
}
/**
* Helper method to return the current CharacterSetOrientation, note
* that FOP does not yet implement the "reference-orientation"
* attribute therefore we always use the orientation zero degrees,
* Other orientation information is captured for use by a future
* implementation (whenever FOP implement the mechanism). This is also
* the case for landscape prints which use an orientation of 270 degrees,
* in 99.9% of cases the font metrics will be the same as the 0 degrees
* therefore the implementation currently will always use 0 degrees.
*
* @return characterSetOrentation The current orientation metrics.
*/
private CharacterSetOrientation getCharacterSetOrientation() {
CharacterSetOrientation c
= (CharacterSetOrientation) characterSetOrientations.get(currentOrientation);
return c;
}
/**
* Indicates whether the given char in the character set.
* @param c the character to check
* @return true if the character is in the character set
*/
public boolean hasChar(char c) {
return encoder.canEncode(c);
}
/**
* Encodes a character sequence to a byte array.
* @param chars the characters
* @return the encoded characters
* @throws CharacterCodingException if the encoding operation fails
*/
public byte[] encodeChars(CharSequence chars) throws CharacterCodingException {
ByteBuffer bb = encoder.encode(CharBuffer.wrap(chars));
if (bb.hasArray()) {
return bb.array();
} else {
bb.rewind();
byte[] bytes = new byte[bb.remaining()];
bb.get(bytes);
return bytes;
}
}
/**
* Map a Unicode character to a code point in the font.
* The code tables are already converted to Unicode therefore
* we can use the identity mapping.
*
* @param c character to map
* @return the mapped character
*/
public char mapChar(char c) {
//TODO This is not strictly correct but we'll let it be for the moment
return c;
}
}
|