6 releases

0.3.2 Jun 20, 2024
0.3.1 Dec 9, 2023
0.3.0 Mar 22, 2023
0.2.0 Jul 19, 2022
0.1.1 Mar 10, 2021

#117 in Graphics APIs

Download history 30/week @ 2024-08-04 50/week @ 2024-09-01 48/week @ 2024-09-08 8/week @ 2024-09-15 58/week @ 2024-09-22 46/week @ 2024-09-29 6/week @ 2024-10-06 7/week @ 2024-10-20 1/week @ 2024-10-27 153/week @ 2024-11-03 26/week @ 2024-11-10 52/week @ 2024-11-17

232 downloads per month

MIT license

390KB
279 lines

fontdue-sdl2

docs.rs badge crates.io badge GitHub Actions badge

A crate for drawing text with sdl2::render, using fontdue for rasterization. This library is glue-code, all the good parts are from the aforementioned libraries.

The motivation for this crate is to allow easier, rustier, and better quality text rendering in projects that use SDL2's render module, compared to sdl2_ttf.

Note that fontdue is already a "full solution to text rendering", it's a very application-facing crate. This crate exists so that I don't need to write a text rendering cache for each SDL2 project I work on, and that part isn't implemented in fontdue probably because it varies based on rendering technology. This is a solution for fontdue + SDL2.

Documentation

Read the docs on docs.rs.

Compared to sdl2_ttf

This library draws each glyph as its own quad, from a single gylph cache texture. This is very fast on modern GPUs, as it can be done in a single draw call. Per-text-area caching can still be achieved by rendering this library's results into a render texture.

Performance and shortcomings

Reserving a spot on the glyph cache texture is currently proportional to the amount of previously allocated glyphs, so it gets slower over time. Some ad-hoc testing shows about 20 microseconds per glyph in release, up to 200 microseconds in debug, for a few paragraphs at different sizes. This rarely affects overall performance badly however, since this only needs to be done once per character/size/color/font combo. But there is still a lot of room for optimization.

Currently the crate is still a work in progress, so there are a few missing features:

  • The glyph cache texture isn't resized on the fly, the texture is always 1024x1024.
  • Unused glyphs can't be overwritten, every glyph that gets written will exist in the cache forever.

Removing unused glyphs (and detecting them, for that matter) may be too performance intensive in the end, so I may release a 1.0 after I implement on-the-fly texture resizing. Manual clearing of the cache might also be added, though the usefulness of that is questionable, as you could simply create a new FontTexture for a similar effect.

Screenshot

This mostly shows off fontdue (the text rasterization) and SDL2 (the window and rendering), but I think rendering crates should have screenshots for first impressions. This screenshot was taken of the testbed.rs example.

Screenshot of some text from Wikipedia rendered using this crate.

License

This library can be used under the terms of the MIT license.

Dependencies

~19MB
~396K SLoC