abstract class Eagle::Font

Overview

A font you can draw text with, through Graphics#print or a Label.

Eagle ships a crisp built-in pixel font, Font.default, so text works with no files. For anything else, load a TrueType file at a pixel size. Glyphs are rasterized in Crystal on first use and packed into an atlas texture.

title = Font.load("res://fonts/Inter.ttf", 48)
g.print("Eagle", 20, 20, font: title)

w = Font.default.width("Score: 100") # measure before drawing
lines = Font.default.wrap("A long sentence that needs wrapping.", 120)

Load each size you need separately. Scaling a TrueType font with scale: blurs it, while the pixel font scales cleanly by whole numbers.

Direct Known Subclasses

Defined in:

eagle/graphics/font.cr
eagle/graphics/truetype.cr

Constructors

Instance Method Summary

Constructor Detail

def self.default : Font #

The built-in pixel font. It needs no files, so it works everywhere, including the web.


def self.load(path : String, size : Number, filter : GPU::Filter = GPU::Filter::Linear) : Font #

Loads a TrueType font at a pixel size. Cached by path and size, so calling it every frame is cheap.


Instance Method Detail

abstract def glyph(char : Char) : Glyph | Nil #

The glyph for char, or nil if the font doesn't have it.


def height : Float32 #

Line height after #scale.


def kerning(a : Char, b : Char) : Float32 #

Extra horizontal adjustment between the pair a, b, such as pulling "AV" together.


def letter_spacing : Float32 #

Extra space between characters, in pixels.


def letter_spacing=(letter_spacing : Float32) #

Extra space between characters, in pixels.


abstract def line_height : Float32 #

Distance between baselines, in pixels, before #scale.


def measure(text : String) : Vec2 #

Width and height of text, counting every line.


def scale : Float32 #

Scale applied whenever this font is drawn. The pixel font defaults to 2 so it stays readable.


def scale=(scale : Float32) #

Scale applied whenever this font is drawn. The pixel font defaults to 2 so it stays readable.


abstract def texture : Texture #

The atlas texture holding the glyphs.


def width(text : String) : Float32 #

Width of text in pixels, including kerning and scale. Uses the longest line for multi-line text.


def wrap(text : String, max_width : Number) : Array(String) #

Splits text into lines that each fit within max_width pixels, breaking at spaces.