ewe.fx
Class FontMetrics

java.lang.Object
  extended byewe.fx.FontMetrics

public class FontMetrics
extends Object

FontMetrics computes font metrics including string width and height.

FontMetrics are usually used to obtain information about the widths and heights of characters and strings when drawing text on a surface. A FontMetrics object references a font and surface since fonts may have different metrics on different surfaces.

Here is an example that uses FontMetrics to get the width of a string:

 ...
 Font font = new Font("Helvetica", Font.BOLD, 10);
 FontMetrics fm = getFontMetrics();
 String s = "This is a line of text.";
 int stringWidth = fm.getTextWidth(s);
 ...
 


Constructor Summary
FontMetrics(Font font, ISurface surface)
          Constructs a font metrics object referencing the given font and surface.
 
Method Summary
 boolean equals(Object other)
          Returns if this object is considered equal to the other object.
 int getAscent()
          Returns the ascent of the font.
 int getCharWidth(char c)
          Returns the width of the given character in pixels.
 int getDescent()
          Returns the descent of a font.
 Font getFont()
           
 int[] getFormattedTextPositions(String s, FormattedTextSpecs fts, int[] positions)
          Returns the positions of each individual character in a formatted String.
 int getHeight()
          Returns the height of the referenced font.
 int getLeading()
          Returns the external leading which is the space between lines.
 FontMetrics getNewFor(Font f)
          Get a new FontMetrics for a different font but the same ISurface.
 int getTextWidth(char[] chars, int start, int count)
          Returns the width of the given text in pixels.
 int getTextWidth(String s)
          Returns the width of the given text string in pixels.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, toString
 

Constructor Detail

FontMetrics

public FontMetrics(Font font,
                   ISurface surface)
Constructs a font metrics object referencing the given font and surface.

If you are trying to create a font metrics object in a Control subclass, use the getFontMetrics() method in the Control class.

See Also:
Control.getFontMetrics(ewe.fx.Font font)
Method Detail

getAscent

public int getAscent()
Returns the ascent of the font. This is the distance from the baseline of a character to its top.


getCharWidth

public int getCharWidth(char c)
Returns the width of the given character in pixels.


getDescent

public int getDescent()
Returns the descent of a font. This is the distance from the baseline of a character to the bottom of the character.


getHeight

public int getHeight()
Returns the height of the referenced font. This is equal to the font's ascent plus its descent. This does not include leading (the space between lines).


getLeading

public int getLeading()
Returns the external leading which is the space between lines.


getTextWidth

public int getTextWidth(String s)
Returns the width of the given text string in pixels.


getFormattedTextPositions

public int[] getFormattedTextPositions(String s,
                                       FormattedTextSpecs fts,
                                       int[] positions)
Returns the positions of each individual character in a formatted String. This takes into account special characters like TAB (9). You can also use this to find the total width of the string. This would be equal to the last entry in the int array.

Parameters:
s - The string to display
fts - Options for the formatted text.
positions - This is the destination for the positions of each character. If this is null or not big enough to hold all the widths, a new one will be created which is big enough and returned. The value at index 0, will be the position of the character at index 1. The position of the character at index 0 is always 0.
Returns:
The array of character widths - one entry for each character in s.

getTextWidth

public int getTextWidth(char[] chars,
                        int start,
                        int count)
Returns the width of the given text in pixels.

Parameters:
chars - the text character array
start - the start position in array
count - the number of characters

getFont

public Font getFont()

getNewFor

public FontMetrics getNewFor(Font f)
Get a new FontMetrics for a different font but the same ISurface.

Parameters:
f - the new Font.
Returns:
the FontMetrics for the new font.

equals

public boolean equals(Object other)
Description copied from class: Object
Returns if this object is considered equal to the other object.

Overrides:
equals in class Object
Parameters:
other - Another object to compare to.
Returns:
true if this object is considered equal to the other object.