Class MsdfFont

java.lang.Object
lol.trq.helios.text.MsdfFont
All Implemented Interfaces:
AutoCloseable

public final class MsdfFont extends Object implements AutoCloseable
A multi-channel signed distance field font: glyph metrics, kerning and an RGB atlas, read from an .hfnt file that the fontgen module writes.

Metrics are in em units, y pointing down from the baseline. The atlas texture is uploaded on first use and lives until close().

  • Method Details

    • read

      public static MsdfFont read(InputStream in) throws IOException
      Reads a font.
      Parameters:
      in - the .hfnt bytes; not closed
      Returns:
      the font
      Throws:
      IOException - if the data is not a valid font
    • resource

      public static MsdfFont resource(ClassLoader loader, String resource)
      Reads a font from the class path.
      Parameters:
      loader - the class loader
      resource - the resource name
      Returns:
      the font
      Throws:
      UncheckedIOException - if it is missing or invalid
    • name

      public String name()
      Returns the font name.
      Returns:
      the name
    • ascent

      public float ascent()
      Distance from the baseline up to the top of the tallest glyphs, in em.
      Returns:
      the ascent
    • descent

      public float descent()
      Distance from the baseline down to the bottom of descenders, in em.
      Returns:
      the descent
    • lineHeight

      public float lineHeight()
      Distance from one baseline to the next, in em.
      Returns:
      the line height
    • pxRange

      public float pxRange()
      The distance range of the atlas in texels.
      Returns:
      the range
    • emSize

      public float emSize()
      Texels per em in the atlas.
      Returns:
      the em size
    • atlasWidth

      public int atlasWidth()
      Atlas width in texels.
      Returns:
      the width
    • atlasHeight

      public int atlasHeight()
      Atlas height in texels.
      Returns:
      the height
    • glyph

      public MsdfFont.Glyph glyph(int codepoint)
      Returns a glyph, or the replacement glyph when the font lacks it.
      Parameters:
      codepoint - the character
      Returns:
      the glyph; never null when the font has a replacement or ?
    • glyphOrNull

      public MsdfFont.Glyph glyphOrNull(int codepoint)
      Returns a glyph without falling back.
      Parameters:
      codepoint - the character
      Returns:
      the glyph, or null
    • kerning

      public float kerning(int left, int right)
      Returns the kerning between two characters.
      Parameters:
      left - the first character
      right - the second character
      Returns:
      the adjustment in em, usually negative
    • atlasPixels

      public ByteBuffer atlasPixels()
      Returns the atlas pixels: tightly packed RGB bytes, rows top to bottom.
      Returns:
      a read-only view
    • texture

      public int texture(GlBackend gl)
      Returns the atlas texture, uploading it on first use.
      Parameters:
      gl - the backend of the current context
      Returns:
      the texture id
    • close

      public void close()
      Deletes the atlas texture, if uploaded. The font can upload it again.
      Specified by:
      close in interface AutoCloseable