Try the demo without cloning this repo here: https://gabdube.github.io/articles/rust_slug/rust_slug.html
A demo showing a WebGPU implementation of the Slug algorithm, originally created by Eric Lengyel. Text preprocessing is done in Rust.
This is a demo, not a rust "crate". The code is not production ready. The preprocessing logic is in slug.rs, feel free to build from it.
There are two things that differ from the reference implementation:
- The original HLSL shader uses textures to share the font data with the shader, my version uses storage buffers.
- I did not port the dynamic dilation in the vertex shader
- Mouse drag - Pan the view horizontally and vertically
- Mouse wheel - Zoom in and out
- Left/Right arrow keys - Cycle through loaded fonts and apply to all text
- Drag and drop .ttf/.otf files - Load new fonts into the demo
- Drag and drop .txt files - Update the displayed text content
- Mouse interaction - Pauses the automatic text scrolling animation
rust-slug/src/lib.rs- Main WASM library entry point with theRustSlugDemopublic API for text rendering and font management.rust-slug/src/slug.rs- Slug algorithm implementation including glyph atlas building, curve extraction, and text string processing.rust-slug/src/base.rs- Base utilities and data structures (AABB, Point, color types, matrix operations).rust-slug/src/shared.rs- Shared message types and data structures for communication between Rust and TypeScript.local-server/src/main.rs- Local development server for serving the demo application during development.
rust-slug-demo/demo.ts- Main demo application with WebGPU renderer and user controls.rust-slug-demo/generated.ts- Generated TypeScript bindings for WASM message types and serialization functions.
Because the wasm binaries and assets are included in the repo, the first two steps are optional.
- Build the rust wasm binary
cd rust-slug
wasm-pack build --out-dir ./build --target web
cp ./build/rust_slug.js ../build/
cp ./build/rust_slug_bg.wasm ../build/
- Build typescript source
npm install
npx rollup --config rollup.config.mjs --watch
- Start the local server
cargo run --release -p local-server
Thanks to Eric Lengyel for creating the Slug algorithm and releasing it into the public domain.