Crate fontdue_sdl2[][src]


This crate is glue code for rendering text with sdl2, rasterized and laid out by fontdue.


First, set up fontdue and layout some glyphs with the Color included as user data:

let font = include_bytes!("../examples/roboto/Roboto-Regular.ttf") as &[u8];
let roboto_regular = Font::from_bytes(font, Default::default()).unwrap();
let fonts = &[roboto_regular];
let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
layout.append(fonts, &TextStyle::with_user_data(
    "Hello, World!",           // The text to lay out
    32.0,                      // The font size
    0,                         // The font index (Roboto Regular)
    Color::RGB(0xFF, 0xFF, 0)  // The color of the text

Then draw them using a FontTexture. It needs a TextureCreator, just as any SDL Texture.

let mut font_texture = FontTexture::new(&texture_creator).unwrap();
let _ = font_texture.draw_text(&mut canvas, fonts, layout.glyphs());

Note that drawing text can fail if there are issues with the rendering setup. It’s often valid to simply ignore, or crash.

The FontTexture is intended to be created once, at the beginning of your program, and then used throughout. Generally, you should treat FontTexture similarly to the Font-slice passed to fontdue, and associate each FontTexture with a specific &[Font]. See the FontTexture documentation for more information.

See examples/ for a complete example program.



A text-rendering-enabled wrapper for Texture.