#font #sdl #font-rendering #unicode #text #font-file #unifont

sdl2-unifont

Embedded GNU Unifont for Rust SDL programs, for simple font rendering tasks

3 stable releases

Uses old Rust 2015

1.0.2 Apr 24, 2021
1.0.1 Oct 14, 2018

#485 in GUI

30 downloads per month
Used in simple-game-engine

GPL-3.0-or-later

1MB
406 lines

sdl2-unifont Latest Version Documentation

Sometimes, you just need to draw some text in your SDL application, and don't need any kind of advanced rendering features. You don't want the hassle of distributing a separate font file, relying on system fonts which often vary in location, or having to use SDL_ttf, just to display a few lines of text.

This library embeds the GNU Unifont, xzipped, inside the application binary, and decompresses it automatically before use. The library tries somewhat to minimise both it's on-disk and in-memory size.

Getting Started

Check here for the latest version of sdl2-unifont.

Or add the following to your Cargo.toml:

[dependencies]
sdl2-unifont = "1.0.2"

Demo


The example program's output

Run the included example, with cargo run --example demo --features plane-1 to produce the above screen.

Example

extern crate sdl2_unifont;

use sdl2_unifont::renderer::SurfaceRenderer;

use sdl2::pixels::Color;

fn main() {
    // Red text with transparent background
    let mut renderer =
        SurfaceRenderer::new(Color::RGB(255, 0, 0), Color::RGBA(0, 0, 0, 0));

    // Draw text to a surface, which can be used like any other. .draw() returns
    // an error result if the string contains a character which is not in the
    // Unifont.
    let surface = renderer.draw("Sample Text").unwrap();

    // Renderer simply holds state for producing new text surfaces
    renderer.bg_color = Color::RGB(255, 255, 0);
    renderer.bold = true;
    renderer.scale = 2;
    let example2 = renderer.draw("Big and bold").unwrap();
}

Consult examples/demo.rs for additional example code.

Dependencies

~16MB
~346K SLoC