easy-sdl3-text

Crates.ioeasy-sdl3-text
lib.rseasy-sdl3-text
version0.3.2
created_at2025-08-27 20:21:25.830082+00
updated_at2025-09-15 22:15:07.422738+00
descriptionEasy text rendering for sdl3 (including sub-pixel rendering)
homepage
repositoryhttps://github.com/What42Pizza/easy-sdl3-text
max_upload_size
id1813169
size541,861
What42Pizza (What42Pizza)

documentation

README

Easy Sdl3 Text

This crate adds easy text rendering function for sdl3 using ab_glyph. Current features:

  • Cache for reusing textures
  • Both regular and sub-pixel rendering
  • Vertical and horizontal alignment
  • Multithreaded rasterization
  • Pure rust, no compilation headaches

This might work best as a starting point for you to make your own text rendering library, but it is already very usable on its own. Also, rendering uncached text usually takes over a millisecond (sometimes over 5 ms in the examples), but it's mostly a one-time cost, and frame-time spikes from text rasterizing should very quickly disappear as the program continues running.

NOTE: This currently depends on sdl3 version "0.15", ab_glyph version "0.2", and rayon version "1", if any of these crates update and you need this crate to update too, please let me know!


Example Output: (looks better in the demo, run cargo run --example subpixel --release with the downloaded crate to see the true sub-pixel rendering)

Example Image


Example Code:

use easy_sdl3_text::*;

pub fn example_draw_function<'a, F: ThreadSafeFont>(
	app_data: &AppData,
	canvas: &mut Canvas<Window>,
	texture_creator: &'a TextureCreator<WindowContext>,
	text_cache: &mut TextCache<'a, F>,
) -> anyhow::Result<()> {
	canvas.set_draw_color(Color::RGB(255, 255, 255));
	canvas.clear();
	
	let size: u32 = 25;
	let (mut x, mut y): (i32, i32) = (50, 50);
	let foreground = Color::RGB(30, 30, 30);
	let background = Color::RGB(255, 255, 255);
	
	// most arguments to the rendering functions are the same across calls, so they're all put into a reusable struct
	let mut text_rendering_settings = TextRenderingSettings::new_subpixel(
		size,
		HAlign::Left, VAlign::Center,
		foreground, background,
		canvas, texture_creator, text_cache
	);
	
	render_text_subpixel("Example text", x, y, &mut text_rendering_settings)?;
	y += size as i32;
	render_text_subpixel("More example text", x, y, &mut text_rendering_settings)?;
	y += size as i32;
	
	canvas.present();
	Ok(())
}

This crate and all its code is dedicated to the public domain, being licensed under CC0 1.0 Universal

Commit count: 21

cargo fmt