pub struct Font(/* private fields */);Expand description
Font: a glyph atlas texture plus per-glyph metrics.
raylib ships a built-in default font (used when None or no font is specified in
draw-text calls). User-defined fonts are loaded via RaylibHandle methods:
RaylibHandle::load_font— loads the default size and full glyph set from a TTF/BDF/FNT file.RaylibHandle::load_font_ex— loads with an explicit point size and optional Unicode codepoint list (passNonefor the entire set).RaylibHandle::load_font_from_image— XNA-style bitmap font from an image.RaylibHandle::load_font_from_memory— loads from an in-memory file buffer.
Freed via UnloadFont on drop.
§Examples
Load a font with explicit size and a Latin-1 subset, then draw text:
use raylib::prelude::*;
let (mut rl, thread) = raylib::init().size(640, 480).title("font demo").build();
// Load a TTF at 32 pt, restricting to printable ASCII (codepoints 32-126).
let ascii: String = (32u8..=126).map(|c| c as char).collect();
let font = rl
.load_font_ex(&thread, "assets/Roboto.ttf", 32, Some(&ascii))
.unwrap();
while !rl.window_should_close() {
let mut d = rl.begin_drawing(&thread);
d.clear_background(Color::RAYWHITE);
d.draw_text_ex(&font, "Hello, raylib!", Vector2::new(10.0, 10.0), 32.0, 2.0, Color::BLACK);
}Implementations§
Source§impl Font
impl Font
Sourcepub unsafe fn from_raw(raw: Font) -> Self
pub unsafe fn from_raw(raw: Font) -> Self
converts raylib-sys object to a “safe” version. Make sure to call this function from the thread the resource was created.
§Safety
The caller must ensure raw is a valid, fully initialized raylib object
obtained from a raylib load function. Ownership is transferred to the
returned wrapper, which will call the appropriate unload function on drop.
Source§impl Font
impl Font
Sourcepub fn make_weak(self) -> WeakFont
pub fn make_weak(self) -> WeakFont
Converts this Font into a WeakFont that does not run UnloadFont on drop.
Use this when you want to hand a font to code that holds it by WeakFont (e.g. a
renderer that doesn’t own the resource) and you take responsibility for
unloading the GPU atlas yourself. Forgets the owning Font so the Drop impl
doesn’t run; the returned WeakFont keeps the same texture and glyph table but
will not free them on drop.
§Examples
use raylib::prelude::*;
let (mut rl, thread) = raylib::init().size(640, 480).title("font").build();
let font = rl
.load_font(&thread, "assets/font.ttf")
.expect("font load");
let weak = font.make_weak();
// `weak` is now an alias with no drop responsibility — the atlas leaks unless
// ownership is reclaimed before `weak` goes out of scope.§See also
Trait Implementations§
Source§impl AsRawMut<Font> for Font
impl AsRawMut<Font> for Font
Source§unsafe fn as_raw_mut(&mut self) -> &mut Font
unsafe fn as_raw_mut(&mut self) -> &mut Font
Source§impl RaylibFont for Font
impl RaylibFont for Font
Source§fn is_font_valid(&self) -> bool
fn is_font_valid(&self) -> bool
Source§fn export_font_as_code<A>(&self, filename: A) -> bool
fn export_font_as_code<A>(&self, filename: A) -> bool
Source§fn get_glyph_info(&self, codepoint: char) -> GlyphInfo
fn get_glyph_info(&self, codepoint: char) -> GlyphInfo
Source§fn get_glyph_index(&self, codepoint: char) -> i32
fn get_glyph_index(&self, codepoint: char) -> i32
font.