gfx.font

index ยท gfx

Overview

Pixel-art text: strings drawn as scaled bitmap glyphs on a canvas.

A glyph is a monochrome bitmap with a fixed cell size. Each glyph is drawn once per scale into a white gfx.sprite art mask and cached. Drawing blits the mask with the text colour as a tint, so one mask serves every colour.

Functions

NameSignature
fontfont(spec) -> font

font(spec) -> font

Create a pixel-art font

spec fielddefaultdescription
glyphsthe 5x7 ASCII fontmap of character to rows of # and .
cell_w, cell_h5, 7glyph cell size in pixels
default_color{252,252,252,255}text colour
fallback" "character drawn for a missing glyph
spacing1gap between glyphs, in cell pixels
line_spacing1gap between lines, in cell pixels

A lowercase letter with no glyph draws its uppercase glyph.

font:draw(canvas, text, x, y, opts) draws text with its top-left at x, y and returns the pen x after the last glyph. A newline starts a line at x.

font:measure(text, opts) returns the width and height draw would cover, in pixels.

opts fielddescription
scaleinteger pixels per cell pixel, default 1
colortext colour, default default_color; draw only
blendpassed to canvas:blit; draw only
spacing, line_spacingoverride the font's gaps