Class TrueTypeFont
- All Implemented Interfaces:
RasterFont
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.
-
Method Summary
Modifier and TypeMethodDescriptionvoidappendGlyph(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).booleancanDisplay(int codePoint) Returns whether the font has a glyph for a character.static TrueTypeFontcreateDefault(double size) Returns Aileron Regular, the scalable font embedded in the library, atsizepixels.deriveFont(double newSize) Returns this face at another size.intReturns the ascender of thehheatable, in font units.doubleReturns the distance from the baseline to the top of the line, in pixels.static byte[]Returns the bytes of the embedded Aileron Regular font file, a new copy on every call, for code that wants the file itself.intReturns the descender of thehheatable, in font units.doubleReturns the distance from the baseline to the bottom of the line, in pixels.Returns the family name from thenametable, such asAileron.intReturns the number of glyphs in the font.doubleReturns the extra space the font asks for between lines, in pixels.getOutline(String text, double x, double y) Returns the outline oftextwith its baseline starting at(x, y), glyph after glyph by their advances andkerntable kerning.doublegetSize()Returns the size in pixels: the height of one em.Returns the style name from thenametable, such asRegular.intReturns the font units per em: glyph coordinates are scaled bygetSize() / getUnitsPerEm().intglyphAdvance(int glyph) Returns a glyph's advance width from thehmtxtable, in font units.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.intglyphIndex(int codePoint) Returns the glyph that shows a character, from thecmaptable.intkerning(int left, int right) Returns the kerning between two glyphs from thekerntable, in font units, to add to the advance of the first.static TrueTypeFontload(byte[] data, double size) Reads a font from the bytes of a TrueType or OpenType file.static TrueTypeFontload(byte[] data, int index, double size) Reads one face of a font file, which for a.ttccollection may be any of its faces.doublestringWidth(String text) Returns the advance width oftext: the sum of its glyph advances andkerntable kerning, unrounded.
-
Method Details
-
load
Reads a font from the bytes of a TrueType or OpenType file.
Parameters
data: the file's contents; the array is kept, not copiedsize: 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
- Throws:
IOException
-
load
Reads one face of a font file, which for a
.ttccollection may be any of its faces.Parameters
data: the file's contents; the array is kept, not copiedindex: the face in a collection, 0 for a single fontsize: 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
- Throws:
IOException
-
createDefault
Returns Aileron Regular, the scalable font embedded in the library, at
sizepixels. It is the font Pillow'sImageFont.load_default()returns, and it covers printable ASCII.Parameters
size: 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
.ttffile -
deriveFont
Returns this face at another size. The parsed tables are shared; the glyph cache is not.
Parameters
newSize: 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
Returns the family name from the
nametable, such asAileron.Returns
the family, empty if the font names none
-
getStyleName
Returns the style name from the
nametable, such asRegular.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
hheatable, in font units.Returns
the ascender, positive above the baseline
-
getDescenderUnits
public int getDescenderUnits()Returns the descender of the
hheatable, 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
- Specified by:
getAscentin interfaceRasterFont
-
getDescent
public double getDescent()Returns the distance from the baseline to the bottom of the line, in pixels.
Returns
the descent, positive
- Specified by:
getDescentin interfaceRasterFont
-
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
cmaptable.Parameters
codePoint: 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: 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
hmtxtable, in font units.Parameters
glyph: the glyph index
Returns
the advance
-
kerning
public int kerning(int left, int right) Returns the kerning between two glyphs from the
kerntable, in font units, to add to the advance of the first.Parameters
left: the first glyph indexright: 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: the glyph index
Returns
{xMin, yMin, xMax, yMax}, or null for a glyph with no outline -
appendGlyph
Appends a glyph's outline to a path, scaled to this size, with its origin on the baseline at
(x, y).Parameters
path: the path to append toglyph: the glyph indexx: the pen positiony: the baseline
-
getOutline
Returns the outline of
textwith its baseline starting at(x, y), glyph after glyph by their advances andkerntable kerning.Parameters
text: the stringx: left end of the baseliney: the baseline
Returns
the outline, filled with the non-zero rule
- Specified by:
getOutlinein interfaceRasterFont
-
stringWidth
Returns the advance width of
text: the sum of its glyph advances andkerntable kerning, unrounded.Parameters
text: the string
Returns
the width in pixels
- Specified by:
stringWidthin interfaceRasterFont
-