Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class TrueTypeFont
- Object
- TrueTypeFont
ImplementsRasterFont
A scalable font read from the bytes of a TrueType file (.ttf, an
OpenType .otf with TrueType outlines, or one face of a .ttc
collection), drawn by RasterGraphics.drawString(String, double, double).
The font is pure Java and needs no platform font engine, so it renders the
same text on a device and on a server. It reads the head, hhea,
maxp, cmap (formats 4 and 12), loca, glyf (composite glyphs
included), hmtx, kern (format 0) and name tables. Glyphs are not
hinted: outlines are scaled exactly, as quadratic curves, and the
rasterizer’s antialiasing smooths them. OpenType layout (GSUB, GPOS)
is not applied, so ligatures and GPOS kerning are absent, and fonts
with PostScript (CFF) outlines are refused with an IOException.
TrueTypeFont font = TrueTypeFont.createDefault(48);
RasterGraphics g = card.createGraphics();
g.setFont(font);
g.setColor(0xff202020);
g.drawString("Hello", 40, 40 + font.getAscent());
createDefault(double) is Aileron Regular, a font embedded in the
library, so text can be drawn without shipping a font file.
An instance keeps a bounded cache of glyph outlines and is not safe to use
from two threads at once; deriveFont(double) is cheap and gives each
thread its own.
Methods
public static TrueTypeFont load(byte[] data, double size)
throws IOException | Reads a font from the bytes of a TrueType or OpenType file. |
public static TrueTypeFont load(byte[] data, int index, double size)
throws IOException | Reads one face of a font file, which for a .ttc collection may be any of its faces. |
public static TrueTypeFont createDefault(double size) | Returns Aileron Regular, the scalable font embedded in the library, at size pixels. |
public static byte[] getDefaultFontData() | Returns the bytes of the embedded Aileron Regular font file, a new copy on every call, for code that wants the file itself. |
public TrueTypeFont deriveFont(double newSize) | Returns this face at another size. |
public double getSize() | Returns the size in pixels: the height of one em. |
public String getFamilyName() | Returns the family name from the name table, such as Aileron. |
public String getStyleName() | Returns the style name from the name table, such as Regular. |
public int getUnitsPerEm() | Returns the font units per em: glyph coordinates are scaled by getSize() / getUnitsPerEm(). |
public int getGlyphCount() | Returns the number of glyphs in the font. |
public int getAscenderUnits() | Returns the ascender of the hhea table, in font units. |
public int getDescenderUnits() | Returns the descender of the hhea table, in font units. |
public double getAscent() | Returns the distance from the baseline to the top of the line, in pixels. |
public double getDescent() | Returns the distance from the baseline to the bottom of the line, in pixels. |
public double getLineGap() | Returns the extra space the font asks for between lines, in pixels. |
public int glyphIndex(int codePoint) | Returns the glyph that shows a character, from the cmap table. |
public boolean canDisplay(int codePoint) | Returns whether the font has a glyph for a character. |
public int glyphAdvance(int glyph) | Returns a glyph’s advance width from the hmtx table, in font units. |
public int kerning(int left, int right) | Returns the kerning between two glyphs from the kern table, in font units, to add to the advance of the first. |
public double[] glyphBounds(int glyph) | Returns a glyph’s control box – the bounds of every point of its outline, on and off the curve – in font units, y up. |
public void appendGlyph(RasterPath path, int glyph, double x, double y) | Appends a glyph’s outline to a path, scaled to this size, with its origin on the baseline at (x, y). |
public RasterPath getOutline(String text, double x, double y) | Returns the outline of text with its baseline starting at (x, y), glyph after glyph by their advances and kern table kerning. |
public double stringWidth(String text) | Returns the advance width of text: the sum of its glyph advances and kern table kerning, unrounded. |
Inherited methods
Method details
load
public static TrueTypeFont load(byte[] data, double size)
throws IOExceptionParameters
databyte[]- the file’s contents; the array is kept, not copied
sizedouble- the size in pixels, the height of one em
Returns
Throws
IOException- if the data is not a font this class can read
load
public static TrueTypeFont load(byte[] data, int index, double size)
throws IOException.ttc collection may be
any of its faces.Parameters
databyte[]- the file’s contents; the array is kept, not copied
indexint- the face in a collection, 0 for a single font
sizedouble- the size in pixels, the height of one em
Returns
Throws
IOException- if the data is not a font this class can read
createDefault
public static TrueTypeFont createDefault(double size)size pixels. It is the font Pillow’s ImageFont.load_default()
returns, and it covers printable ASCII.Parameters
sizedouble- the size in pixels
Returns
getDefaultFontData
public static byte[] getDefaultFontData()Returns
.ttf filederiveFont
public TrueTypeFont deriveFont(double newSize)Parameters
newSizedouble- the size in pixels
Returns
newSizegetSize
public double getSize()Returns
getFamilyName
public String getFamilyName()name table, such as Aileron.Returns
getStyleName
public String getStyleName()name table, such as Regular.Returns
getUnitsPerEm
public int getUnitsPerEm()getSize() / getUnitsPerEm().Returns
getGlyphCount
public int getGlyphCount()Returns
getAscenderUnits
public int getAscenderUnits()hhea table, in font units.Returns
getDescenderUnits
public int getDescenderUnits()hhea table, in font units.Returns
getAscent
public double getAscent()Returns
getDescent
public double getDescent()Returns
getLineGap
public double getLineGap()Returns
glyphIndex
public int glyphIndex(int codePoint)cmap table.Parameters
codePointint- a Unicode code point
Returns
canDisplay
public boolean canDisplay(int codePoint)Parameters
codePointint- a Unicode code point
Returns
glyphAdvance
public int glyphAdvance(int glyph)hmtx table, in font units.Parameters
glyphint- the glyph index
Returns
kerning
public int kerning(int left, int right)kern table, in font
units, to add to the advance of the first.Parameters
leftint- the first glyph index
rightint- the glyph that follows it
Returns
glyphBounds
public double[] glyphBounds(int glyph)Parameters
glyphint- the glyph index
Returns
{xMin, yMin, xMax, yMax}, or null for a glyph with no outlineappendGlyph
public void appendGlyph(RasterPath path, int glyph, double x, double y)(x, y).Parameters
pathRasterPath- the path to append to
glyphint- the glyph index
xdouble- the pen position
ydouble- the baseline
getOutline
public RasterPath getOutline(String text, double x, double y)text with its baseline starting at (x, y),
glyph after glyph by their advances and kern table kerning.Parameters
textString- the string
xdouble- left end of the baseline
ydouble- the baseline
Returns
stringWidth
public double stringWidth(String text)text: the sum of its glyph advances and
kern table kerning, unrounded.Parameters
textString- the string