public class Font extends Object
Font
defines how characters are drawn on a GraphicsContext
.
A font has a set of identifiers. An identifier is an integer which specifies whether the font is able to render
specific languages and alphabets. A value has been assigned to the built-in identifiers: see FontIdentifiers
.
Custom identifiers may be defined in order to identify or find a specific font. For example, a font which contains
some special characters like arrows or emojis can be tagged by the font creator with a specific identifier.
A font has a style, which is a combination of the following style constants: STYLE_PLAIN
,
STYLE_BOLD
and SYTLE_ITALIC
.
A font has a descriptor, which is a string describing the font.
Fonts can not be created by applications and are rather retrieved from the implementation environment. An application can get the list of all the fonts, the default font or a font with a specific path.
Modifier and Type | Field and Description |
---|---|
static int |
STYLE_BOLD
The bold style constant.
|
static int |
STYLE_ITALIC
The italic style constant.
|
static int |
STYLE_PLAIN
The plain style constant.
|
Modifier and Type | Method and Description |
---|---|
int |
charWidth(char character)
Returns the width of a character when it is drawn using this font.
|
static Font[] |
getAllFonts()
Returns an array containing all the fonts of the system.
|
int |
getBaselinePosition()
Returns the vertical distance in pixels between the top of this font and its baseline.
|
static Font |
getDefaultFont()
Returns the default font of the system.
|
String |
getDescriptor()
Returns the descriptor of this font.
|
static Font |
getFont(String path)
Returns the font matching a given path.
|
int |
getHeight()
Returns the height of a line of text when it is drawn using this font.
|
int[] |
getIdentifiers()
Returns an array containing all the identifiers supported by this font.
|
byte[] |
getSNIContext()
Returns the SNI context data of this font.
|
int |
getStyle()
Returns the style of this font.
|
boolean |
isBold()
Returns whether this font is bold.
|
boolean |
isIdentifierSupported(int identifier)
Returns whether this font supports the given identifier.
|
boolean |
isItalic()
Returns whether this font is italic.
|
boolean |
isMonospaced()
Returns whether this font is monospaced.
|
boolean |
isPlain()
Returns whether this font is plain.
|
int |
stringWidth(String string)
Returns the width of a string when it is drawn using this font.
|
int |
substringWidth(String string,
int offset,
int length)
Returns the width of a part of a string when it is drawn using this font.
|
public static final int STYLE_BOLD
public static final int STYLE_ITALIC
public static final int STYLE_PLAIN
public int charWidth(char character)
The width is the horizontal distance that would be occupied if the given character was drawn using this font. It also includes the horizontal space that would be added after the character to separate it appropriately from the following characters.
character
- the character to measure.public static Font[] getAllFonts()
MicroUIException
- if MicroUI is not started.public int getBaselinePosition()
public static Font getDefaultFont()
MicroUIException
- if no font is declared in the system.MicroUIException
- if MicroUI is not started.public String getDescriptor()
This method returns an empty string if this font does not have a descriptor.
public static Font getFont(String path)
path
- the path of the desired font.MicroUIException
- if the resource path doesn't start with "/".MicroUIException
- if no font matches the given path.MicroUIException
- if MicroUI is not started.public int getHeight()
The height includes the size of the font as well as sufficient spacing below the line of text to separate it appropriately from the following lines.
public int[] getIdentifiers()
An identifier can be either one of the identifier constants specified in this class or an specific identifier value defined by the MicroUI implementation.
public byte[] getSNIContext()
The SNI context can be used to call a native method with SNI. This allows to identify and to use a font in the native world.
The data format is implementation specific.
public int getStyle()
The returned value is a combination of the following style constants: STYLE_PLAIN
,
STYLE_BOLD
and SYTLE_ITALIC
.
public boolean isBold()
true
if this font is bold, false
otherwise.public boolean isIdentifierSupported(int identifier)
identifier
- the identifier to check.true
if this font supports the given identifier, false
otherwise.public boolean isItalic()
true
if this font is italic, false
otherwise.public boolean isMonospaced()
A monospaced font is a font in which every character has the same width.
true
if this font is monospaced, false
otherwise.public boolean isPlain()
true
if this font is plain, false
otherwise.public int stringWidth(String string)
The width is the horizontal distance that would be occupied if the given string was drawn using this font. It also includes the horizontal space between the characters of the string to separate them appropriately.
string
- the string to measure.public int substringWidth(String string, int offset, int length)
The width is the horizontal distance that would be occupied if the given substring was drawn using this font. It also includes the horizontal space between the characters of the substring to separate them appropriately.
string
- the string containing the substring to measure.offset
- the index of the first character in the substring to measure.length
- the number of characters to measure.StringIndexOutOfBoundsException
- if the given offset and length are out of the string bounds.