Expand description
A font rendering crate for rendering text using signed distance fields.
This crate aims to provide a general and easy to use API for rendering text using wgpu. It can render text normally (using raster images), or with signed distance fields, which allows for performant scaling and outlining.
§Usage
Here is an example of how to use the crate. You first need to create a TextRenderer struct, then load a font using ab_glyph, then you can create a Text object, which is the thing that can be drawn.
let mut text_renderer =
TextRendererBuilder::new(target_format, target_size).build(&device);
let font = ab_glyph::FontRef::try_from_slice(include_bytes!("FiraSans-Regular.ttf"))?;
let font = text_renderer.load_font_with_sdf(font, 45., SdfSettings { radius: 15. });
let text = TextBuilder::new("Hello, world!", font, [100., 100.])
.outlined([1.; 4], 10.)
.build(&device, &queue, &mut text_renderer);
You can then draw this text object during a render pass like so:
text_renderer.draw(&mut render_pass, &text);
§Performance
Calculating the signed distance field for a character takes a small but not-insignificant amount of time. This will only happen once for each character in a font, and can be done ahead of time using TextRenderer::generate_char_textures, but is still a cost. If you don’t need the features provided by sdf rendering, you should use non-sdf rendering instead.
Re-exports§
pub use ab_glyph;
Structs§
- FontId
- A handle to a font stored in the TextRenderer.
- SdfSettings
- Settings for how the signed distance field calculation should work for a font.
- Text
- A piece of text that can be rendered to the screen.
- Text
Builder - A builder for a Text struct.
- Text
Renderer - The main struct that handles text rendering to the screen. Use this struct to load fonts and draw text during a render pass.
- Text
Renderer Builder - A builder for a TextRenderer struct.
Enums§
- Font
Size - Settings for font size.
- Horizontal
Alignment - Settings for horizontal text alignment
- Vertical
Alignment - Settings for vertical text alignment.