Class TrueTypeFont

java.lang.Object
com.codename1.backend.image.TrueTypeFont
All Implemented Interfaces:
RasterFont

public final class TrueTypeFont extends Object implements 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 Type
    Method
    Description
    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).
    boolean
    canDisplay(int codePoint)
    Returns whether the font has a glyph for a character.
    createDefault(double size)
    Returns Aileron Regular, the scalable font embedded in the library, at size pixels.
    deriveFont(double newSize)
    Returns this face at another size.
    int
    Returns the ascender of the hhea table, in font units.
    double
    Returns 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.
    int
    Returns the descender of the hhea table, in font units.
    double
    Returns the distance from the baseline to the bottom of the line, in pixels.
    Returns the family name from the name table, such as Aileron.
    int
    Returns the number of glyphs in the font.
    double
    Returns the extra space the font asks for between lines, in pixels.
    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.
    double
    Returns the size in pixels: the height of one em.
    Returns the style name from the name table, such as Regular.
    int
    Returns the font units per em: glyph coordinates are scaled by getSize() / getUnitsPerEm().
    int
    glyphAdvance(int glyph)
    Returns a glyph's advance width from the hmtx table, 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.
    int
    glyphIndex(int codePoint)
    Returns the glyph that shows a character, from the cmap table.
    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.
    load(byte[] data, double size)
    Reads a font from the bytes of a TrueType or OpenType file.
    load(byte[] data, int index, double size)
    Reads one face of a font file, which for a .ttc collection may be any of its faces.
    double
    Returns the advance width of text: the sum of its glyph advances and kern table kerning, unrounded.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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: the file's contents; the array is kept, not copied
      • size: 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

      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: the file's contents; the array is kept, not copied
      • index: the face in a collection, 0 for a single font
      • size: 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

      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: 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: 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

      Specified by:
      getAscent in interface RasterFont
    • getDescent

      public double getDescent()

      Returns the distance from the baseline to the bottom of the line, in pixels.

      Returns

      the descent, positive

      Specified by:
      getDescent in interface RasterFont
    • 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: 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 hmtx table, 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 kern table, in font units, to add to the advance of the first.

      Parameters
      • left: the first glyph index
      • right: 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

      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: the path to append to
      • glyph: the glyph index
      • x: the pen position
      • y: 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: the string
      • x: left end of the baseline
      • y: the baseline
      Returns

      the outline, filled with the non-zero rule

      Specified by:
      getOutline in interface RasterFont
    • 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: the string
      Returns

      the width in pixels

      Specified by:
      stringWidth in interface RasterFont