Crates.io | wgpu_text |
lib.rs | wgpu_text |
version | 0.9.1 |
source | src |
created_at | 2022-03-20 14:22:07.500768 |
updated_at | 2024-11-02 11:20:13.563798 |
description | A simple 2D text renderer for wgpu |
homepage | |
repository | https://github.com/Blatko1/wgpu-text |
max_upload_size | |
id | 553586 |
size | 106,212 |
wgpu-text
is a wrapper over glyph-brush
for fast and easy text rendering by wgpu
. It supports .otf and .ttf fonts.
This project was inspired by and is similar to wgpu_glyph
but has additional features and is more straightforward. Also, there is no need to include glyph-brush in your project.
Since the glyph-brush crate is reexported and being heavily dependent on, it's recommended to go through Section docs and Section examples for better understanding of managing and adding text.
Add the following to your Cargo.toml
file:
[dependencies]
wgpu_text = "0.9.1"
use wgpu_text::{glyph_brush::{Section as TextSection, Text}, BrushBuilder, TextBrush};
let brush = BrushBuilder::using_font_bytes(font).unwrap()
/* .initial_cache_size((16_384, 16_384))) */ // use this to avoid resizing cache texture
.build(&device, config.width, config.height, config.format);
// Directly implemented from glyph_brush.
let section = TextSection::default().add_text(Text::new("Hello World"));
// on window resize:
brush.resize_view(config.width as f32, config.height as f32, &queue);
// window event loop:
winit::event::Event::RedrawRequested(_) => {
// Before are created Encoder and frame TextureView.
// Crashes if inner cache exceeds limits.
brush.queue(&device, &queue, [§ion, ...]).unwrap();
{
let mut rpass = encoder.begin_render_pass(...);
brush.draw(&mut rpass);
}
queue.submit([encoder.finish()]);
frame.present();
}
For more detailed examples, look through examples.
cargo run --example <example-name>
Run examples with --release
for accurate performance.
Besides basic text rendering and glyph-brush features, some features add customization:
All kinds of contributions are welcome.