Skip to main content
The Font class provides a unified interface for rendering text using both bitmap fonts and TrueType fonts (TTF). It handles font loading, rendering, and text metrics calculation.

Font Types

enum class FontType {
    Bitmap,  // Built-in bitmap font
    TTF      // TrueType font
};

Static Methods

TTF Font Management

loadTTFFont()

static bool loadTTFFont(const std::string& name, const std::string& fontPath)
Loads a TrueType font from a file and registers it with the given name.
name
const std::string&
required
Name to register the font under
fontPath
const std::string&
required
Path to the .ttf font file
return
bool
True if the font was loaded successfully, false otherwise
Font::loadTTFFont("main", "assets/fonts/OpenSans-Regular.ttf");

setDefaultTTFFont()

static void setDefaultTTFFont(const std::string& name)
Sets the default TTF font to use when no font name is specified.
name
const std::string&
required
Name of the registered font to use as default
Font::setDefaultTTFFont("main");

hasTTFFont()

static bool hasTTFFont()
return
bool
True if at least one TTF font is loaded, false otherwise

Text Rendering

renderText()

static void renderText(Canvas* canvas, const std::string& text, int x, int y, 
                      int size, uint32_t color, FontType type = FontType::Bitmap)
Unified text rendering method that supports both bitmap and TTF fonts.
canvas
Canvas*
required
Canvas to render text on
text
const std::string&
required
Text string to render
x
int
required
X-coordinate for text position
y
int
required
Y-coordinate for text position
size
int
required
Font size (scale for bitmap, point size for TTF)
color
uint32_t
required
Text color (32-bit RGBA)
type
FontType
default:"FontType::Bitmap"
Font type to use (Bitmap or TTF)
Font::renderText(canvas, "Hello World", 100, 50, 24, Colors::White, FontType::TTF);

renderBitmap()

static void renderBitmap(Canvas* canvas, const std::string& text, int x, int y, 
                        int size, uint32_t color)
Renders text using the built-in bitmap font.
canvas
Canvas*
required
Canvas to render text on
text
const std::string&
required
Text string to render
x
int
required
X-coordinate for text position
y
int
required
Y-coordinate for text position
size
int
required
Font scale factor
color
uint32_t
required
Text color (32-bit RGBA)
Font::renderBitmap(canvas, "Bitmap Text", 50, 50, 2, Colors::White);

renderTTF()

static void renderTTF(Canvas* canvas, const std::string& text, int x, int y, 
                     int size, uint32_t color, const std::string& fontName = "")
Renders text using a TrueType font.
canvas
Canvas*
required
Canvas to render text on
text
const std::string&
required
Text string to render
x
int
required
X-coordinate for text position
y
int
required
Y-coordinate for text position
size
int
required
Font size in points
color
uint32_t
required
Text color (32-bit RGBA)
fontName
const std::string&
default:""
Name of the font to use (empty string uses default)
Font::renderTTF(canvas, "TTF Text", 100, 100, 24, Colors::White, "main");

Text Metrics

getTextWidth()

static int getTextWidth(const std::string& text, int size, FontType type = FontType::Bitmap)
Calculates the width of a text string in pixels.
text
const std::string&
required
Text string to measure
size
int
required
Font size
type
FontType
default:"FontType::Bitmap"
Font type to use
return
int
Text width in pixels
int width = Font::getTextWidth("Hello", 24, FontType::TTF);

getTextHeight()

static int getTextHeight(int size, FontType type = FontType::Bitmap)
Calculates the height of text at the given size.
size
int
required
Font size
type
FontType
default:"FontType::Bitmap"
Font type to use
return
int
Text height in pixels
int height = Font::getTextHeight(24, FontType::TTF);

TTF Namespace Helpers

The TTF namespace provides convenient helper functions for working with TrueType fonts.

TTF::load()

bool TTF::load(const std::string& name, const std::string& path)
Loads a TTF font (equivalent to Font::loadTTFFont).

TTF::setDefault()

void TTF::setDefault(const std::string& name)
Sets the default TTF font (equivalent to Font::setDefaultTTFFont).

TTF::render()

void TTF::render(Canvas* canvas, const std::string& text, int x, int y, 
                int size, uint32_t color, const std::string& fontName = "")
Renders TTF text (equivalent to Font::renderTTF).

TTF::textWidth()

int TTF::textWidth(const std::string& text, int size, const std::string& fontName = "")
Calculates TTF text width.

TTF::textHeight()

int TTF::textHeight(int size, const std::string& fontName = "")
Calculates TTF text height.

Example Usage

Loading and Using TTF Fonts

// Load fonts
Font::loadTTFFont("main", "assets/fonts/OpenSans-Regular.ttf");
Font::loadTTFFont("bold", "assets/fonts/OpenSans-Bold.ttf");
Font::setDefaultTTFFont("main");

// Render with default font
Font::renderTTF(canvas, "Hello World", 100, 50, 24, Colors::White);

// Render with specific font
Font::renderTTF(canvas, "Bold Text", 100, 100, 32, Colors::Red, "bold");

Using TTF Helpers

TTF::load("main", "assets/fonts/OpenSans-Regular.ttf");
TTF::setDefault("main");
TTF::render(canvas, "Easy TTF", 50, 50, 20, Colors::White);

Centered Text

std::string text = "Centered Text";
int textWidth = Font::getTextWidth(text, 24, FontType::TTF);
int x = (canvasWidth - textWidth) / 2;
Font::renderTTF(canvas, text, x, 100, 24, Colors::White);

Build docs developers (and LLMs) love