A simple library to generate 2-dimensional
Distance Fields
(DF)
written in pure Rust
🦀
The lib can import PNG images, convert them into a distance field and export the distances as a PNG again.
This project is not officially released yet. Its current status is pre-alpha and
every part of the lib is work in progress.
Please note that this is my personal playground to learn the Rust programming
language, so don't expect any highly sophisticated code samples here.
I will try to improve this library over time, however right it is not recommended
to use it for anything and especially not in production. 😉
Input image | Outer dist. (8bit) | Inner dist. (8bit) |
---|---|---|
Combined dist. (8bit) | 2-Channel (8bit) |
---|---|
Random dots | Regular dots |
---|---|
- Simple prototype with image input and output
- SourceProcessor trait
- Two-channel image output
- Project structure (modules, libs, crates, features, examples...)
- Simple CPU-based Renderer for SDFs (simplification of a FragmentShader)
- Update readme and add some documentation
- Unit- & Integration-tests
- More algorithms for distance generation (currently only sweeping)
- Real signed distance field output
- Simple WASM project with some WGPU Shader to demonstrate font rendering with SDFs
- Command line interface
- OTF/TTF Interface to convert font glyphs to SDFs
- Implement some kind of raw byte file output
-
Drawing Text with Signed Distance Fields in Mapbox GL
Interesting blog post about MapBox and how they SDF to render the labels on their maps - including some source code for a FragmentShader -
Signed Distance Fields
C# code repository of a SDF generator with different generation algorithms (8PSSDT sweep, brute force Eikonal etc.) -
Shader Fun
Blogpost series abouts SDFs -
Distance Field Fonts
Nixe explanation about the usage of SDF fonts in libGDX -
Ronjas Shader Tutorials
2D Signed Distance Field Basics - blog with some posts about SDFs -
The ‘dead reckoning’ signed distance transform
Paper about the dead reckoning algorithm for Distance Field calculation -
8-points Signed Sequential Euclidean Distance Transform
Explanation of the 8PSSDT algorithm
http://webstaff.itn.liu.se/~stegu/edtaa/
let g : DistanceGenerator = DistanceGenerator::new()
.input(PngInput::new("example/image.png"))
.output(PngOutput::new("example/image_generated.png",
ImageOutputChannels::One,
ImageOutputChannelDepth::Eight))
.export_filter(ExportFilter::Foreground)
.processor(EightSideSweepProcessor {});
let result = g.generate();
From ImageBuffer
From File (File -> ImageBuffer)
From TTF Font File
Additional Transformers would be nice too:
Rotate, Flip, B/W, ChannelMatrix (Re-Map Channels), Threshold,
scale (up/down) <- very important ... inc. linear/bi-linear/tri-linear interpolation
Bits per channel Channel (1/8/16/32/64) (Source format) -
But how do we calculate the output? -> B/W vs. Greyscale
ImageBuffer
File (just a special transformation of an ImageBuffers. Which channels do we use?
8/16/32 Bit?
Int or Float?
Even here we have the options to do multiple transformations (scale, rotate, flip)
Used Algorithm (Naive, 8SED, Dead Reckoning, etc.)
In theory it should be possible to apply this to TTF Fonts. For that, we should have an CLI executable
that should be configurable (used glyphs etc)
SDFs of the glyphs should be packed in some way to a texture atlas. What about hinting?
A good inspirational source is TextMeshPro!
Is there any known file format for SDF Fonts?