Small, lightweight and fast library for rendering text with wgpu.

Overview

wgpu-text

Build Status Licence crates.io Documentation

wgpu-text is a wrapper over glyph-brush for fast and easy text rendering in wgpu.

This project was inspired by and is similar to wgpu_glyph, but has additional features and is simpler. Also there is no need to include glyph-brush in your project.

Some features are directly implemented from glyph-brush so you should go trough Section docs and Section examples for better understanding of managing and adding text.

Installation

Add the following to your Cargo.toml file:

[dependencies]
wgpu_text = "0.6.1"

Usage

use wgpu_text::BrushBuilder;
use wgpu_text::section::{Section, Text, Layout, HorizontalAlign};

let brush = BrushBuilder::using_font_bytes(font).unwrap()
 /* .initial_cache_size((1024, 1024))) */ // use this to avoid resizing cache texture
 /* .with_depth_testing(true) */ // enable/disable depth testing
    .build(&device, &config);
    
// Directly implemented from glyph_brush.
let section = Section::default()
    .add_text(Text::new("Hello World"))
    .with_layout(Layout::default().h_align(HorizontalAlign::Center));

// on window resize:
        brush.resize_view(config.width as f32, config.height as f32, &queue);

// window event loop:
    winit::event::Event::RedrawRequested(_) => {
        // Has to be queued every frame.
        brush.queue(&section);
        
        let text_buffer = brush.draw(&device, &view, &queue);
        
        // Has to be submitted last so text won't be overlapped.
        queue.submit([some_other_encoder.finish(), text_buffer]);
        
        frame.present();
    }

Examples

For more detailed examples look trough examples.

  • cargo run --example <example-name>

Run examples with --release for true performance.

Features

Besides basic text rendering and glyph-brush features, there are some features that add customization:

builtin matrix - default matrix for orthographic projection, feel free to use it for creating custom matrices

custom matrix - ability of providing a custom matrix for purposes of custom view, rotation... (the downside is that it applies to all rendered text)

depth testing - by adding z coordinate, text can be set on top or below other text (if enabled)

Goals

  • try to improve docs
  • maybe some new features
  • (wgpu stuff: maybe change to StagingBelt instead of Queue)

Contributing

All contributions are welcome.

You might also like...
Generic framebuffer implementation in Rust for use with embedded-graphics library

Fraramebuffer implementation for Rust's Embedded-graphics Framebuffer approach helps to deal with display flickering when you update multiple parts of

Python library for embedding large graphs in 2D space, using force-directed layouts.

Graph Force A python/rust library for embedding graphs in 2D space, using force-directed layouts. Installation pip install graph_force Usage The first

CLI for image processing with histograms, binary treshold and other functions

Image-Processing-CLI-in-Rust CLI for processing images in Rust. Some implementation is custom and for some functionality it uses 3rd party libraries.

GLFW3 bindings and idiomatic wrapper for Rust.

glfw-rs GLFW bindings and wrapper for The Rust Programming Language. Example extern crate glfw; use glfw::{Action, Context, Key}; fn main() { le

Safe and rich Rust wrapper around the Vulkan API
Safe and rich Rust wrapper around the Vulkan API

Vulkano See also vulkano.rs. Vulkano is a Rust wrapper around the Vulkan graphics API. It follows the Rust philosophy, which is that as long as you do

A high-performance SVG renderer, powered by Rust based resvg and napi-rs.
A high-performance SVG renderer, powered by Rust based resvg and napi-rs.

resvg-js resvg-js is a high-performance SVG renderer, powered by Rust based resvg and napi-rs. Fast, safe and zero dependencies! No need for node-gyp

 A simple and elegant, pipewire graph editor
A simple and elegant, pipewire graph editor

pw-viz A simple and elegant, pipewire graph editor This is still a WIP, node layouting is kinda jank at the moment. Installation A compiled binary is

A cargo subcommand for creating GraphViz DOT files and dependency graphs
A cargo subcommand for creating GraphViz DOT files and dependency graphs

cargo-graph Linux: A cargo subcommand for building GraphViz DOT files of dependency graphs. This subcommand was originally based off and inspired by t

Szalinski: A Tool for Synthesizing Structured CAD Models with Equality Saturation and Inverse Transformations

Szalinski: A Tool for Synthesizing Structured CAD Models with Equality Saturation and Inverse Transformations

Comments
  • Expose brush dependencies, simplify BrushBuilder

    Expose brush dependencies, simplify BrushBuilder

    Make important dependencies accessible for the end user. Like FontRef for example, so a brush can be stored in a struct.

    In my project I have a state struct so I can split a lot of the render code, but I can't use your library because dependencies are private. Anyway keep up the good work.

    enhancement 
    opened by Norlock 3
  • Update WGSL to new syntax

    Update WGSL to new syntax

    I also changed the example to use FIFO instead of MAILBOX since FIFO is guaranteed to be available everywhere. I couldn't get it to run at all on M1 Mac otherwise.

    enhancement 
    opened by aztecrex 1
  • Expose glyph_brush::GlyphCruncher in TextBrush

    Expose glyph_brush::GlyphCruncher in TextBrush

    It would be useful to me to be able to figure out exactly how much space a particular Section will need before it is drawn. This would allow me to compute a layout with other text sections, and other types of drawings. My understanding is that the GlyphCruncher trait in glyph_brush can measure a Section using the glyph_bounds method. Is there a way to use this trait with a TextBrush?

    Since glyph_brush::Glyph_Brush implements the trait, and TextBrush is a wrapper around GlyphBrush, it should be pretty simple to add some new methods to TextBrush to allow for measuring text before drawing. Alternatively, the inner brush could be made public, so that traits and methods from glyph_brush could be used, though I think this would require a reexport of glyph_brush. I'm not sure which approach would be best.

    Thanks for the awesome crate!

    enhancement good first issue 
    opened by Dieff 0
Releases(v0.6.3)
Owner
Leon
Leon
An SVG rendering library.

resvg resvg is an SVG rendering library. Purpose resvg can be used as a Rust library, a C library and as a CLI application to render SVG files based o

Evgeniy Reizner 1.8k Jan 7, 2023
A cool, fast maze generator and solver written in Rust

MazeCruncher Welcome to maze cruncher! Download Standalone Here Usage To get started, just run the standalone .exe in target/release or compile and ru

null 69 Sep 20, 2022
A fast isosurface extraction algorithm.

fast-surface-nets A fast, chunk-friendly implementation of Naive Surface Nets on regular grids. Surface Nets is an algorithm for extracting an isosurf

Duncan 40 Dec 18, 2022
Baryon is a compact 3D engine focused on fast prototyping in code.

baryon Baryon is a compact 3D engine focused on fast prototyping in code. No big dependencies, no fancy run-times, GUI editors, or other magic. Depend

Dzmitry Malyshau 63 Jan 1, 2023
Graph data structure library for Rust.

petgraph Graph data structure library. Supports Rust 1.41 and later. Please read the API documentation here Crate feature flags: graphmap (default) en

null 2k Jan 9, 2023
A graph library for Rust.

Gamma A graph library for Rust. Gamma provides primitives and traversals for working with graphs. It is based on ideas presented in A Minimal Graph AP

Metamolecular, LLC 122 Dec 29, 2022
Simple but powerful graph library for Rust

Graphlib Graphlib is a simple and powerful Rust graph library. This library attempts to provide a generic api for building, mutating and iterating ove

Purple Protocol 177 Nov 22, 2022
Library for Rubik's cube applications.

Rubik Master cube-demo3.mov Do you like to solve Rubik's cube? I do. As a cuber and programmer, I want to build a toolset to build applications like S

Akira Hayakawa 12 Nov 3, 2022
The library provides basic functions to work with Graphviz dot lang from rust code.

Description The library provides the basic access to the graphs in graphviz format with ability to import into or export from it. Base examples: Parse

Boris 28 Dec 16, 2022
Rust library for of graph ensembles

Rust library for random graph ensembles Minimal Rust version: 1.55.0 Implements simple sampling and monte carlo (or rather markov-) steps, that can be

Yannick Feld 2 Dec 14, 2022