public class FontMetrics extends Object
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 and Description |
---|
FontMetrics(Font font,
ISurface surface)
Constructs a font metrics object referencing the given font and surface.
|
Modifier and Type | Method and Description |
---|---|
FontMetrics |
change(FontChange fc)
Return a new FontMetrics with the specified changes applied.
|
boolean |
equals(Object obj)
Returns if this object is considered equal to the other object.
|
void |
free() |
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(char[] chars,
int start,
int count,
FormattedTextSpecs fts,
int[] positions)
Returns the positions of each individual character in a formatted String.
|
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 |
getOvershoot()
Return the maximum number of pixels that any character in this font will be extended to the right
beyond the bounds given by getCharWidth() or getTextWidth().
|
ISurface |
getSurface() |
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.
|
int |
hashCode()
Returns a hashCode for the object.
|
boolean |
isRegular()
Returns true if you can accurately determine the width of text by summing the
sum of the widths of the individual characters in the text.
|
boolean |
isSame(Font f,
ISurface surface) |
boolean |
isSame(FontMetrics f)
Return if this Font is the same as another.
|
public FontMetrics(Font font, ISurface surface)
If you are trying to create a font metrics object in a Control subclass, use the getFontMetrics() method in the Control class.
Control.getFontMetrics(eve.fx.Font font)
public int getOvershoot()
How this is determined varies depending on the underlying system.
public boolean isRegular()
public int getAscent()
public int getCharWidth(char c)
public int getDescent()
public int getHeight()
public int getLeading()
public int getTextWidth(String s)
public int[] getFormattedTextPositions(String s, FormattedTextSpecs fts, int[] positions)
s
- The string to displayfts
- 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.public int[] getFormattedTextPositions(char[] chars, int start, int count, FormattedTextSpecs fts, int[] positions)
chars
- The chars to displaystart
- the first character to display.count
- the number of characters 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.public int getTextWidth(char[] chars, int start, int count)
chars
- the text character arraystart
- the start position in arraycount
- the number of characterspublic Font getFont()
public ISurface getSurface()
public void free()
public FontMetrics getNewFor(Font f)
f
- the new Font.public int hashCode()
Object
public boolean isSame(FontMetrics f)
f
- another FontMetrics.public boolean equals(Object obj)
Object
public FontMetrics change(FontChange fc)
fc
- a FontChange object.