Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class TrueTypeFont

  1. Object
  2. 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 IOExceptionReads a font from the bytes of a TrueType or OpenType file.
public static TrueTypeFont load(byte[] data, int index, double size) throws IOExceptionReads 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 IOException
Reads a font from the bytes of a TrueType or OpenType file.

Parameters

data byte[]
the file’s contents; the array is kept, not copied
size double
the size in pixels, the height of one em

Returns

the font

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
Reads one face of a font file, which for a .ttc collection may be any of its faces.

Parameters

data byte[]
the file’s contents; the array is kept, not copied
index int
the face in a collection, 0 for a single font
size double
the size in pixels, the height of one em

Returns

the font

Throws

IOException
if the data is not a font this class can read

createDefault

public static TrueTypeFont createDefault(double size)
Returns Aileron Regular, the scalable font embedded in the library, at size pixels. It is the font Pillow’s ImageFont.load_default() returns, and it covers printable ASCII.

Parameters

size double
the size in pixels

Returns

the font

getDefaultFontData

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.

Returns

the .ttf file

deriveFont

public TrueTypeFont deriveFont(double newSize)
Returns this face at another size. The parsed tables are shared; the glyph cache is not.

Parameters

newSize double
the size in pixels

Returns

the font at newSize

getSize

public double getSize()
Returns the size in pixels: the height of one em.

Returns

the size

getFamilyName

public String getFamilyName()
Returns the family name from the name table, such as Aileron.

Returns

the family, empty if the font names none

getStyleName

public String getStyleName()
Returns the style name from the name table, such as Regular.

Returns

the style, empty if the font names none

getUnitsPerEm

public int getUnitsPerEm()
Returns the font units per em: glyph coordinates are scaled by getSize() / getUnitsPerEm().

Returns

the units per em

getGlyphCount

public int getGlyphCount()
Returns the number of glyphs in the font.

Returns

the glyph count

getAscenderUnits

public int getAscenderUnits()
Returns the ascender of the hhea table, in font units.

Returns

the ascender, positive above the baseline

getDescenderUnits

public int getDescenderUnits()
Returns the descender of the hhea table, in font units.

Returns

the descender, negative below the baseline

getAscent

public double getAscent()
Returns the distance from the baseline to the top of the line, in pixels.

Returns

the ascent

getDescent

public double getDescent()
Returns the distance from the baseline to the bottom of the line, in pixels.

Returns

the descent, positive

getLineGap

public double getLineGap()
Returns the extra space the font asks for between lines, in pixels.

Returns

the line gap

glyphIndex

public int glyphIndex(int codePoint)
Returns the glyph that shows a character, from the cmap table.

Parameters

codePoint int
a Unicode code point

Returns

the glyph index, 0 (the missing glyph) when the font has none

canDisplay

public boolean canDisplay(int codePoint)
Returns whether the font has a glyph for a character.

Parameters

codePoint int
a Unicode code point

Returns

true if the character maps to a glyph other than the missing glyph

glyphAdvance

public int glyphAdvance(int glyph)
Returns a glyph’s advance width from the hmtx table, in font units.

Parameters

glyph int
the glyph index

Returns

the advance

kerning

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.

Parameters

left int
the first glyph index
right int
the glyph that follows it

Returns

the adjustment, 0 when the pair is not kerned

glyphBounds

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.

Parameters

glyph int
the glyph index

Returns

{xMin, yMin, xMax, yMax}, or null for a glyph with no outline

appendGlyph

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).

Parameters

path RasterPath
the path to append to
glyph int
the glyph index
x double
the pen position
y double
the baseline

getOutline

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.

Parameters

text String
the string
x double
left end of the baseline
y double
the baseline

Returns

the outline, filled with the non-zero rule

stringWidth

public double stringWidth(String text)
Returns the advance width of text: the sum of its glyph advances and kern table kerning, unrounded.

Parameters

text String
the string

Returns

the width in pixels