1 /* FontMetrics.java -- Information about about a fonts display characteristics
2 Copyright (C) 1999, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.font.FontRenderContext;
42 import java.awt.font.LineMetrics;
43 import java.awt.geom.Rectangle2D;
44 import java.text.CharacterIterator;
46 // FIXME: I leave many methods basically unimplemented. This
47 // should be reviewed.
50 * This class returns information about the display characteristics of
51 * a font. It is abstract, and concrete subclasses should implement at
52 * least the following methods:
55 * <li>getAscent()</li>
56 * <li>getDescent()</li>
57 * <li>getLeading()</li>
58 * <li>getMaxAdvance()</li>
59 * <li>charWidth(char)</li>
60 * <li>charsWidth(char[], int, int)</li>
63 * @author Aaron M. Renn (arenn@urbanophile.com)
65 public abstract class FontMetrics implements java.io.Serializable
67 // Serialization constant.
68 private static final long serialVersionUID = 1681126225205050147L;
71 * This is the font for which metrics will be returned.
76 * Initializes a new instance of <code>FontMetrics</code> for the
79 * @param font The font to return metric information for.
81 protected FontMetrics(Font font)
87 * Returns the font that this object is creating metric information fo.
89 * @return The font for this object.
97 * Returns the leading, or spacing between lines, for this font.
99 * @return The font leading.
101 public int getLeading()
107 * Returns the ascent of the font, which is the distance from the base
108 * to the top of the majority of characters in the set. Some characters
109 * can exceed this value however.
111 * @return The font ascent.
113 public int getAscent()
119 * Returns the descent of the font, which is the distance from the base
120 * to the bottom of the majority of characters in the set. Some characters
121 * can exceed this value however.
123 * @return The font descent.
125 public int getDescent()
131 * Returns the height of a line in this font. This will be the sum
132 * of the leading, the ascent, and the descent.
134 * @return The height of the font.
136 public int getHeight()
138 return getAscent() + getDescent() + getLeading();
142 * Returns the maximum ascent value. This is the maximum distance any
143 * character in the font rised above the baseline.
145 * @return The maximum ascent for this font.
147 public int getMaxAscent()
153 * Returns the maximum descent value. This is the maximum distance any
154 * character in the font extends below the baseline.
156 * @return The maximum descent for this font.
158 public int getMaxDescent()
160 return getMaxDecent();
164 * Returns the maximum descent value. This is the maximum distance any
165 * character in the font extends below the baseline.
167 * @return The maximum descent for this font.
169 * @deprecated This method is deprecated in favor of
170 * <code>getMaxDescent()</code>.
172 public int getMaxDecent()
178 * Returns the width of the widest character in the font.
180 * @return The width of the widest character in the font.
182 public int getMaxAdvance()
188 * Returns the width of the specified character.
190 * @param ch The character to return the width of.
192 * @return The width of the specified character.
194 public int charWidth(int ch)
196 return charWidth((char) ch);
200 * Returns the width of the specified character.
202 * @param ch The character to return the width of.
204 * @return The width of the specified character.
206 public int charWidth(char ch)
212 * Returns the total width of the specified string
214 * @param str The string to return the width of.
216 * @return The width of the string.
218 public int stringWidth(String str)
220 char[] buf = new char[str.length()];
221 str.getChars(0, str.length(), buf, 0);
223 return charsWidth(buf, 0, buf.length);
227 * Returns the total width of the specified character array.
229 * @param buf The character array containing the data.
230 * @param offset The offset into the array to start calculating from.
231 * @param len The total number of bytes to process.
233 * @return The width of the requested characters.
235 public int charsWidth(char[] buf, int offset, int len)
238 for (int i = offset; i < len; i++)
239 total_width += charWidth(buf[i]);
244 * Returns the total width of the specified byte array.
246 * @param buf The byte array containing the data.
247 * @param offset The offset into the array to start calculating from.
248 * @param len The total number of bytes to process.
250 * @return The width of the requested characters.
252 public int bytesWidth(byte[] buf, int offset, int len)
255 for (int i = offset; i < len; i++)
256 total_width = charWidth((char) buf[i]);
262 * Returns the widths of the first 256 characters in the font.
264 * @return The widths of the first 256 characters in the font.
266 public int[] getWidths()
268 int[] result = new int[256];
269 for (char i = 0; i < 256; i++)
270 result[i] = charWidth(i);
275 * Returns a string representation of this object.
277 * @return A string representation of this object.
279 public String toString()
281 return (this.getClass() + "[font=" + font + ",ascent=" + getAscent()
282 + ",descent=" + getDescent() + ",height=" + getHeight() + "]");
285 // Generic FontRenderContext used when getLineMetrics is called with a
286 // plain Graphics object.
287 private static final FontRenderContext gRC = new FontRenderContext(null,
292 * Returns a {@link LineMetrics} object constructed with the
293 * specified text and the {@link FontRenderContext} of the Graphics
294 * object when it is an instance of Graphics2D or a generic
295 * FontRenderContext with a null transform, not anti-aliased and not
296 * using fractional metrics.
298 * @param text The string to calculate metrics from.
299 * @param g The Graphics object that will be used.
301 * @return A new {@link LineMetrics} object.
303 public LineMetrics getLineMetrics(String text, Graphics g)
305 return getLineMetrics(text, 0, text.length(), g);
309 * Returns a {@link LineMetrics} object constructed with the
310 * specified text and the {@link FontRenderContext} of the Graphics
311 * object when it is an instance of Graphics2D or a generic
312 * FontRenderContext with a null transform, not anti-aliased and not
313 * using fractional metrics.
315 * @param text The string to calculate metrics from.
316 * @param begin Index of first character in <code>text</code> to measure.
317 * @param limit Index of last character in <code>text</code> to measure.
318 * @param g The Graphics object that will be used.
320 * @return A new {@link LineMetrics} object.
322 * @throws IndexOutOfBoundsException if the range [begin, limit] is
323 * invalid in <code>text</code>.
325 public LineMetrics getLineMetrics(String text, int begin, int limit,
328 FontRenderContext rc;
329 if (g instanceof Graphics2D)
330 rc = ((Graphics2D) g).getFontRenderContext();
333 return font.getLineMetrics(text, begin, limit, rc);
337 * Returns a {@link LineMetrics} object constructed with the
338 * specified text and the {@link FontRenderContext} of the Graphics
339 * object when it is an instance of Graphics2D or a generic
340 * FontRenderContext with a null transform, not anti-aliased and not
341 * using fractional metrics.
343 * @param chars The string to calculate metrics from.
344 * @param begin Index of first character in <code>text</code> to measure.
345 * @param limit Index of last character in <code>text</code> to measure.
346 * @param g The Graphics object that will be used.
348 * @return A new {@link LineMetrics} object.
350 * @throws IndexOutOfBoundsException if the range [begin, limit] is
351 * invalid in <code>text</code>.
353 public LineMetrics getLineMetrics(char[] chars, int begin, int limit,
356 FontRenderContext rc;
357 if (g instanceof Graphics2D)
358 rc = ((Graphics2D) g).getFontRenderContext();
361 return font.getLineMetrics(chars, begin, limit, rc);
365 * Returns a {@link LineMetrics} object constructed with the
366 * specified text and the {@link FontRenderContext} of the Graphics
367 * object when it is an instance of Graphics2D or a generic
368 * FontRenderContext with a null transform, not anti-aliased and not
369 * using fractional metrics.
371 * @param ci An iterator over the string to calculate metrics from.
372 * @param begin Index of first character in <code>text</code> to measure.
373 * @param limit Index of last character in <code>text</code> to measure.
374 * @param g The Graphics object that will be used.
376 * @return A new {@link LineMetrics} object.
378 * @throws IndexOutOfBoundsException if the range [begin, limit] is
379 * invalid in <code>text</code>.
381 public LineMetrics getLineMetrics(CharacterIterator ci, int begin,
382 int limit, Graphics g)
384 FontRenderContext rc;
385 if (g instanceof Graphics2D)
386 rc = ((Graphics2D) g).getFontRenderContext();
389 return font.getLineMetrics(ci, begin, limit, rc);
392 public Rectangle2D getStringBounds(String str, Graphics context)
394 return font.getStringBounds(str, getFontRenderContext(context));
397 public Rectangle2D getStringBounds(String str, int beginIndex, int limit,
400 return font.getStringBounds(str, beginIndex, limit,
401 getFontRenderContext(context));
404 public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit,
407 return font.getStringBounds(chars, beginIndex, limit,
408 getFontRenderContext(context));
411 public Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex,
412 int limit, Graphics context)
414 return font.getStringBounds(ci, beginIndex, limit,
415 getFontRenderContext(context));
418 private FontRenderContext getFontRenderContext(Graphics context)
420 if (context instanceof Graphics2D)
421 return ((Graphics2D) context).getFontRenderContext();