book/src/modules/text-and-fonts.md
raylib bundles a default bitmap font that is available as soon as the window is open.
Custom fonts are loaded via RaylibHandle::load_font (from a file) or
RaylibHandle::load_font_ex (with an explicit codepoint set for preloaded glyph
atlases). Both return a RAII Font that is automatically unloaded on drop.
Text drawing — including measuring — hangs off RaylibHandle for the default font
and off the Font struct or RaylibDraw trait for custom fonts.
RaylibHandle::load_font —
load a font from a file; returns Result<Font, …>.RaylibHandle::load_font_ex —
load a font with explicit font size and a codepoint array; useful for preloading a
specific non-ASCII character set.RaylibHandle::measure_text —
measure the pixel width of a string at a given size using the default font.Font::measure_text —
measure a string with a custom font, returning a Vector2 (width × height).RaylibDraw::draw_text —
draw a &str using the default font.RaylibDraw::draw_text_ex —
draw a &str using a custom Font, with position, size, spacing, and tint.Drawing text requires an open window. The example below opens a 640×480 window, draws a greeting with the default font each frame, and measures its width for centering.
# extern crate raylib;
use raylib::prelude::*;
fn main() {
let (mut rl, thread) = raylib::init()
.size(640, 480)
.title("Text demo")
.vsync()
.build();
let message = "Hello, raylib-rs!";
let font_size = 24;
while !rl.window_should_close() {
// Measure text width for the default font.
let width = rl.measure_text(message, font_size);
let mut d = rl.begin_drawing(&thread);
d.clear_background(Color::RAYWHITE);
// Centered draw using the default font.
d.draw_text(
message,
(640 - width) / 2,
(480 - font_size) / 2,
font_size,
Color::DARKGRAY,
);
}
}
load_font and load_font_ex call into raylib's
GPU atlas-building path, so they must be called after raylib::init().build().
Font data files (.ttf, .otf, .fnt, .bdf) must be on disk at the given path.load_font_ex takes an optional &[i32] codepoint list.
If None, raylib loads the default Latin set (32–127). Pass an explicit list if
your application draws CJK, emoji, or other extended Unicode ranges.docs/superpowers/plans/)
tracks which are exposed; check the
text module rustdoc for the
current set.Font docs.rs —
full rustdoc for custom font operations.Showcase examples that exercise this module: