Skip to content
FutureBlitz

    Install

    Import std.image.font

    std.image.font

    TrueType and OpenType fonts, read safely from bytes or a file.

    Font: a TrueType or OpenType font, read by the runtime itself, which checks each offset in it before it follows it, so a broken font or one made to mislead is an IOError, or draws boxes, but never reads past its bytes: its glyphs' outlines, TrueType's, simple and composite, or CFF's, cmap's formats 4 and 12, and kern's kerning, from bytes or a file. Its sizes are in pixels an em; a Canvas's FillText draws its text.

    std/image/font.fb

    Types

    Font class

    Public Class Font

    A TrueType or OpenType font, whose glyphs a Canvas draws text with.

    Examples

    Local font = Try Font.Load(Path("fonts/Inter.ttf"))
    Local image = Image(400, 60)
    Local canvas = Canvas(image)
    canvas.FillText("Hello", font, 32, Vec2(10, 40), Color32.White)
    Print("{font.Width("Hello", 32)} pixels wide")
    // Bytes that aren't a font are an IOError, never a read past them.
    Local error = ExpectThrows<IOError>(Font.Parse([1, 2, 3, 4] As UInt8[]))
    Expect(error?.Kind == IOErrorKind.INVALID_DATA)

    std/image/font.fb, line 47

    • Public Static Method Parse(data: UInt8[]): Font Throws IOError static method #

      The font in data, a font file or a collection's first font. Throws an IOError of INVALID_DATA when it isn't one, or is broken, and of OTHER when it's a kind std.image doesn't read, like one of CFF2's outlines.

    • Public Static Method Load(path: Path): Font Throws IOError static method #

      The font in the file at path, which Parse reads from its bytes. Throws the IOError File.ReadBytes does for a file that can't be read, and Parse's, its Path the file's, for one it can't read a font of.

    • Public Static Async Method LoadAsync(path: Path): Font Throws IOError static method #

      Load's Async twin, which reads the file on libuv's thread pool while the thread that called it goes on, and parses it there once the Await resumes.

    • Public Property UnitsPerEm: Int property #

      The units its glyphs are in, an em of them.

    • @PanicsAtCaller Public Method Ascent(size: Float): Float method #

      How far its glyphs go above the baseline, at size pixels an em. Panics when the size isn't finite or is negative, as each of its metrics does.

    • @PanicsAtCaller Public Method Descent(size: Float): Float method #

      How far its glyphs go below the baseline, at size pixels an em, above 0.

    • @PanicsAtCaller Public Method LineGap(size: Float): Float method #

      The gap between one line's descent and the next one's ascent, at size pixels an em.

    • @PanicsAtCaller Public Method LineHeight(size: Float): Float method #

      How far apart its lines' baselines are, at size pixels an em: its ascent, descent, and line gap.

    • @PanicsAtCaller Public Method Width(text: String, size: Float): Float method #

      How wide the text is at size pixels an em, its widest line's, as FillText lays it out: each glyph's advance and the kerning between them. Panics when the size isn't finite or is negative.

    • Public Method HasGlyph(codePoint: Int): Bool method #

      Whether it has a glyph for the code point, rather than its .notdef, which draws in its place.

    Report an issue with this page or with FutureBlitz on GitHub.