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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/*
* 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.render.java2d;
// Java
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.LineMetrics;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.Map;
/**
* This is a FontMetrics to be used for AWT rendering.
* It instanciates a font, depening on family and style
* values. The java.awt.FontMetrics for this font is then
* created to be used for the actual measurement.
* Since layout is word by word and since it is expected that
* two subsequent words often share the same style, the
* Font and FontMetrics is buffered and only changed if needed.
* <p>
* Since FontState and FontInfo multiply all factors by
* size, we assume a "standard" font of FONT_SIZE.
*/
public class Java2DFontMetrics {
/**
* Font size standard used for metric measurements
*/
public static final int FONT_SIZE = 1;
/**
* This factor multiplies the calculated values to scale
* to FOP internal measurements
*/
public static final int FONT_FACTOR = (1000 * 1000) / FONT_SIZE;
/**
* The width of all 256 character, if requested
*/
private int[] width = null;
/**
* The typical height of a small cap latter (often derived from "x", value in mpt)
*/
private int xHeight = 0;
/**
* The highest point of the font above the baseline (usually derived from "d", value in mpt)
*/
private int ascender = 0;
/**
* The lowest point of the font under the baseline (usually derived from "p", value in mpt)
*/
private int descender = 0;
/**
* Buffered font.
* f1 is bufferd for metric measurements during layout.
* fSized is buffered for display purposes
*/
private Font f1 = null; // , fSized = null;
/**
* The family type of the font last used
*/
private String family = "";
/**
* The style of the font last used
*/
private int style = 0;
/**
* The size of the font last used
*/
private float size = 0;
/**
* The FontMetrics object used to calculate character width etc.
*/
private FontMetrics fmt = null;
/** A LineMetrics to access high-resolution metrics information. */
private LineMetrics lineMetrics;
/**
* Temp graphics object needed to get the font metrics
*/
private final Graphics2D graphics;
/**
* Creates a Graphics2D object for the sole purpose of getting font metrics.
* @return a Graphics2D object
*/
private static Graphics2D createFontMetricsGraphics2D() {
BufferedImage fontImage = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = fontImage.createGraphics();
//The next line is important to get accurate font metrics!
graphics2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
return graphics2D;
}
/**
* Constructs a new Font-metrics.
*/
public Java2DFontMetrics() {
this.graphics = createFontMetricsGraphics2D();
}
/**
* Determines the font's maximum ascent of the Font described by the current
* FontMetrics object
* @param family font family (java name) to use
* @param style font style (java def.) to use
* @param size font size
* @return ascent in milliponts
*/
public int getMaxAscent(String family, int style, int size) {
setFont(family, style, size);
return Math.round(lineMetrics.getAscent() * FONT_FACTOR);
}
/**
* Determines the font ascent of the Font described by this
* FontMetrics object
* @param family font family (java name) to use
* @param style font style (java def.) to use
* @param size font size
* @return ascent in milliponts
*/
public int getAscender(String family, int style, int size) {
setFont(family, style, size);
return ascender * 1000;
// workaround for sun bug on FontMetrics.getAscent()
// http://developer.java.sun.com/developer/bugParade/bugs/4399887.html
/*
* Bug 4399887 has status Closed, not a bug. The comments on the bug
* are:
* The submitter is incorrectly assuming that the string he has used
* is displaying characters which represent those with the maximum
* ascent in the font. If (for example) the unicode character
* \u00c1 which is the A-acute character used in many European
* languages is placed in the bodu of the "Wrong" string it can be
* seen that the JDK is exactly correct in its determination of the
* ascent of the font.
* If the bounds of a particular string are interesting then the
* Rectangle FontMetrics.getStringBounds(..) method can be called.
* The y value of the rectangle is the offset from the origin
* (baseline) apparently needed by the sample test program
*
* xxxxx@xxxxx 2001-05-15
*/
/* I don't think this is right.
int realAscent = fmt.getAscent()
- (fmt.getDescent() + fmt.getLeading());
return FONT_FACTOR * realAscent;
*/
}
/**
* The size of a capital letter measured from the font's baseline
* @param family font family
* @param style font style
* @param size font size
* @return capital height in millipoints
*/
public int getCapHeight(String family, int style, int size) {
// currently just gets Ascent value but maybe should use
// getMaxAcent() at some stage
return getAscender(family, style, size);
}
/**
* Determines the font descent of the Font described by this
* FontMetrics object
* @param family font family (jave name) to use
* @param style font style (jave def.) to use
* @param size font size
* @return descent in milliponts
*/
public int getDescender(String family, int style, int size) {
setFont(family, style, size);
return descender * 1000;
}
/**
* Determines the typical font height of a small cap letter
* FontMetrics object
* @param family font family (jave name) to use
* @param style font style (jave def.) to use
* @param size font size
* @return font height in milliponts
*/
public int getXHeight(String family, int style, int size) {
setFont(family, style, size);
return xHeight * 1000;
}
public int getUnderlinePosition(String family, int style, int size) {
setFont(family, style, size);
return -Math.round(lineMetrics.getUnderlineOffset());
}
public int getUnderlineThickness(String family, int style, int size) {
setFont(family, style, size);
return Math.round(lineMetrics.getUnderlineThickness());
}
public int getStrikeoutPosition(String family, int style, int size) {
setFont(family, style, size);
return -Math.round(lineMetrics.getStrikethroughOffset());
}
public int getStrikeoutThickness(String family, int style, int size) {
setFont(family, style, size);
return Math.round(lineMetrics.getStrikethroughThickness());
}
/**
* Returns width (in 1/1000ths of point size) of character at
* code point i
* @param i the character for which to get the width
* @param family font family (jave name) to use
* @param style font style (jave def.) to use
* @param size font size
* @return character width in millipoints
*/
public int width(int i, String family, int style, int size) {
int w;
setFont(family, style, size);
w = internalCharWidth(i) * 1000;
return w;
}
private int internalCharWidth(int i) {
//w = (int)(fmt.charWidth(i) * 1000); //Not accurate enough!
char[] ch = {(char)i};
Rectangle2D rect = fmt.getStringBounds(ch, 0, 1, this.graphics);
return (int)Math.round(rect.getWidth() * 1000);
}
/**
* Return widths (in 1/1000ths of point size) of all
* characters
* @param family font family (jave name) to use
* @param style font style (jave def.) to use
* @param size font size
* @return array of character widths in millipoints
*/
public int[] getWidths(String family, int style, int size) {
int i;
if (width == null) {
width = new int[256];
}
setFont(family, style, size);
for (i = 0; i < 256; i++) {
width[i] = 1000 * internalCharWidth(i);
}
return width;
}
private Font getBaseFont(String family, int style, float size) {
Map atts = new java.util.HashMap();
atts.put(TextAttribute.FAMILY, family);
if ((style & Font.BOLD) != 0) {
atts.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
}
if ((style & Font.ITALIC) != 0) {
atts.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
}
atts.put(TextAttribute.SIZE, new Float(size)); //size in pt
return new Font(atts);
}
/**
* Checks whether the font for which values are
* requested is the one used immediately before or
* whether it is a new one
* @param family font family (jave name) to use
* @param style font style (jave def.) to use
* @param size font size
* @return true if the font was changed, false otherwise
*/
private boolean setFont(String family, int style, int size) {
boolean changed = false;
float s = size / 1000f;
if (f1 == null) {
f1 = getBaseFont(family, style, s);
fmt = graphics.getFontMetrics(f1);
changed = true;
} else {
if ((this.style != style) || !this.family.equals(family)
|| this.size != s) {
if (family.equals(this.family)) {
f1 = f1.deriveFont(style, s);
} else {
f1 = getBaseFont(family, style, s);
}
fmt = graphics.getFontMetrics(f1);
changed = true;
}
// else the font is unchanged from last time
}
if (changed) {
//x-Height
TextLayout layout = new TextLayout("x", f1, graphics.getFontRenderContext());
Rectangle2D rect = layout.getBounds();
xHeight = (int)Math.round(-rect.getY() * 1000);
//PostScript-compatible ascent
layout = new TextLayout("d", f1, graphics.getFontRenderContext());
rect = layout.getBounds();
ascender = (int)Math.round(-rect.getY() * 1000);
//PostScript-compatible descent
layout = new TextLayout("p", f1, graphics.getFontRenderContext());
rect = layout.getBounds();
descender = (int)Math.round((rect.getY() + rect.getHeight()) * -1000);
//Alternative way to get metrics but the ascender is again wrong for our purposes
lineMetrics = f1.getLineMetrics("", graphics.getFontRenderContext());
}
// save the family and style for later comparison
this.family = family;
this.style = style;
this.size = s;
return changed;
}
/**
* Returns a java.awt.Font instance for the desired
* family, style and size type.
* This is here, so that the font-mapping
* of FOP-defined fonts to java-fonts can be done
* in one place and does not need to occur in
* AWTFontRenderer.
* @param family font family (jave name) to use
* @param style font style (jave def.) to use
* @param size font size
* @return font with the desired characeristics.
*/
public java.awt.Font getFont(String family, int style, int size) {
setFont(family, style, size);
return f1;
/*
* if( setFont(family,style, size) ) fSized = null;
* if( fSized == null || this.size != size ) {
* fSized = f1.deriveFont( size / 1000f );
* }
* this.size = size;
* return fSized;
*/
}
/**
* Indicates whether the font contains a particular character/glyph.
* @param family font family (jave name) to use
* @param style font style (jave def.) to use
* @param size font size
* @param c the glyph to check
* @return true if the character is supported
*/
public boolean hasChar(String family, int style, int size, char c) {
setFont(family, style, size);
return f1.canDisplay(c);
}
}
|