class Eagle::TrueType

Overview

A TrueType font file, parsed in pure Crystal: glyph lookup, metrics, kerning and outlines.

Most games use Font.load, which wraps this in a TrueTypeFont. Use TrueType directly for tools, such as inspecting a font or rasterizing glyphs into your own atlas.

ttf = TrueType.load("res://fonts/Inter.ttf")
g_index = ttf.glyph_index('A')
bitmap = ttf.rasterize(g_index, 32.0_f32 / ttf.units_per_em)
image = bitmap.to_image

Supports glyf outlines, cmap formats 4, 6 and 12, composite glyphs and kern table format 0. OpenType CFF fonts (.otf) aren't supported.

Defined in:

eagle/graphics/truetype.cr

Constructors

Instance Method Summary

Constructor Detail

def self.load(path : String) : TrueType #

Reads and parses a font file.


def self.new(data : Bytes) #

Parses a font from raw bytes. Raises AssetError if it isn't a supported TrueType file.


Instance Method Detail

def advance(glyph : Int32) : Int32 #

Horizontal advance of a glyph, in font units.


def ascent : Int32 #

Height above the baseline, in font units.


def descent : Int32 #

Depth below the baseline, in font units (negative).


def family_name : String #

The family name from the font's name table, such as "Inter".


def glyph_count : Int32 #

Number of glyphs in the font.


def glyph_index(char : Char) : Int32 #

--- public metrics --- The glyph index for char, or 0 (the missing-glyph box) if the font lacks it.


def has_glyph?(char : Char) : Bool #

True when the font has a glyph for char.


def kern_pairs : Int32 #

Number of kerning pairs in the font.


def kerning(left : Int32, right : Int32) : Int32 #

Kerning between two glyph indices, in font units.


def left_side_bearing(glyph : Int32) : Int32 #

Left side bearing of a glyph, in font units.


def line_gap : Int32 #

Extra gap between lines, in font units.


def outline(glyph : Int32) : GlyphOutline #

--- outlines --- The contours of a glyph.


def rasterize(glyph : Int32, scale : Float32, pad : Int32 = 1) : Bitmap #

Rasterizes a glyph with anti-aliasing. scale is pixels per font unit.


def units_per_em : Int32 #

Font units per em. Multiply by pixel_size / units_per_em to convert to pixels.