public abstract 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 | Constructor and Description |
---|---|
protected |
Font(Format format,
byte[] fontData)
Creates a font.
|
Modifier and Type | Method and Description |
---|---|
protected byte[] |
allocateRenderableStringSNIContext(char[] chars,
int offset,
int length)
Allocates a byte array useful to speed-up the rendering of the renderable string.
|
protected int |
charsWidth(char[] chars,
int offset,
int length)
Computes the width of the given char array.
|
int |
charWidth(char character)
Returns the width of a character when it is drawn using this font.
|
protected void |
drawChars(GraphicsContext gc,
char[] chars,
int offset,
int length,
int x,
int y)
Draws a char array with this font at a position.
|
static Font[] |
getAllFonts()
Returns an array containing all the fonts of the system.
|
abstract 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.
|
static Font |
getFont(String path)
Returns the font matching a given path.
|
abstract int |
getHeight()
Returns the height of a line of text when it is drawn using this font.
|
byte[] |
getSNIContext()
Returns the SNI context data of this font.
|
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.
|
protected Font(Format format, byte[] fontData)
The format must be a custom font format, a value between Format.CUSTOM_0
and Format.CUSTOM_7
.
The given byte array is used to identify and to use a font in the native world.
format
- the custom font formatfontData
- the data of this fontIllegalArgumentException
- if the format is not a custom format.protected byte[] allocateRenderableStringSNIContext(char[] chars, int offset, int length)
The type and content of this array is specific to each subclass of Font. It can be retrieved by a call to
RenderableString.getSNIContext()
.
It should be passed as an argument to the implementation of
Painter.drawRenderableString(GraphicsContext, RenderableString, int, int)
.
Returns an empty array by default.
chars
- the char array of the renderable stringoffset
- the starting offset in the char arraylength
- the length of the renderable stringprotected int charsWidth(char[] chars, int offset, int length)
chars
- the char arrayoffset
- the starting offsetlength
- the lengthcharWidth(char)
,
stringWidth(String)
,
substringWidth(String, int, int)
public int charWidth(char character)
The width is the horizontal space that would be occupied if the given character were 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.
This method only supports characters in the range U+0000 to U+FFFF. It does not handle characters with code points greater than U+FFFF, which are represented as a pair of char values (referred to as "surrogate pair").
character
- the character to measure.protected void drawChars(GraphicsContext gc, char[] chars, int offset, int length, int x, int y)
The translation of the graphics context is already applied to the given x and y.
gc
- the graphics context to draw onchars
- the char arrayoffset
- the starting offsetlength
- the lengthx
- the x coordinate to draw aty
- the y coordinate to draw atPainter.drawString(GraphicsContext, String, Font, int, int)
,
Painter.drawSubstring(GraphicsContext, String, int, int, Font, int, int)
public static Font[] getAllFonts()
MicroUIException
- if MicroUI is not started.public abstract int getBaselinePosition()
public static Font getDefaultFont()
MicroUIException
- if no font is declared in the system.MicroUIException
- if MicroUI is not started.public static Font getFont(String path)
path
- the path of the desired font.MicroUIException
- if the resource path does not start with "/".MicroUIException
- if no font matches the given path.MicroUIException
- if MicroUI is not started.public abstract 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 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.
Implementors can throw a MicroUIException
with MicroUIException.RESOURCE_CLOSED
error code when
the font has been closed. It allows to catch the error in Java instead of having an unspecified behavior in the
native world while drawing a resource that no longer exist.
public int stringWidth(String string)
The width is the horizontal space that would be occupied if the given string were 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 space that would be occupied if the given substring were 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.