Font and text

micro provides a bitmap font system: glyphs are stored in a texture atlas and rendered as textured quads. Both a built-in font and a builder for custom fonts are available.

Quick start — embedded font

The library ships with Pixuf, a compact 8-pixel-tall pixel font covering the full printable ASCII range (0x20–0x7E), embedded directly in the binary (no asset file needed):

auto fnt = micro::font::load_embedded(rend);
rend.draw(fnt, "Hello, world!", {10.f, 10.f});

Pixuf by erytauerytau.itch.io/pixuf

Drawing text

Text is rendered through the renderer. See Renderer → Text for drawing details.

Measuring text

[[nodiscard]] pointf measure(std::string_view text,
                             const text_options& opts = {}) const;

Returns the bounding (width, height) with scale applied from opts. Width is the sum of glyph advances; height is the font's maximum glyph height. Both are multiplied by opts.scale().

// Measure at scale 1×:
auto size = fnt.measure("Game Over");
pointf center{(320.f - size.x) / 2.f, 100.f};
rend.draw(fnt, "Game Over", center);

// Measure at 2× scale:
auto big_size = fnt.measure("Title", text_options{}.scale(2.f));

Font height

[[nodiscard]] int height() const noexcept;

Returns the maximum height of all glyphs in the font at scale 1×. Useful for line spacing and vertical layout:

int line_height = fnt.height();
rend.draw(fnt, "Line 1", {10.f, 10.f});
rend.draw(fnt, "Line 2", {10.f, 10.f + line_height});
rend.draw(fnt, "Line 3", {10.f, 10.f + line_height * 2});

Finding glyphs

[[nodiscard]] const glyph_info* find_glyph(char ch) const noexcept;

Looks up the glyph information for a specific character. Returns a pointer to the glyph info if found, or nullptr if the character is not in the font:

if (const auto* glyph = fnt.find_glyph('A')) {
    // Access glyph->bounds and glyph->advance
    int width = glyph->advance;
}

Useful for custom text layout or querying specific character metrics.

Accessing the atlas

[[nodiscard]] const texture& atlas() const noexcept;

Returns a reference to the underlying texture atlas containing all font glyphs. This provides read-only access for custom rendering or debugging fonts:

const auto& tex = fnt.atlas();
// Use tex for custom rendering or debugging

Building a custom font

auto img = micro::image::load_png("my_font.png");
micro::font_builder b{std::move(img)};

// With explicit advance:
b.add_glyph('A', {{0,  0, 8, 8}, 8});

// Advance == bounds.w:
b.add_glyph('B', {8, 0, 8, 8});

auto fnt = b.build(rend);

font_builder takes the atlas image and a glyph dictionary on the CPU. Calling build() uploads the atlas to the GPU and returns an immutable font.

glyph_info

struct glyph_info {
    rect bounds;  // pixel rectangle of the glyph in the atlas
    int  advance; // horizontal advance in pixels (may differ from bounds.w)
};