fdsm
A pure-Rust reimplementation of multi-channel signed distance field generation.
This implementation mostly follows Victor Chlumský’s master thesis.
Although it is not an exact translation of the C++ msdfgen
library, it does follow it for some parts of the code.
fdsm
also uses code adapted from msdfgen-rs
for its tests.
Crate features
ttf-parser
: import glyphs usingttf-parser
visualize
: helpers for visualization
Usage
use FillRule;
use generate_msdf;
use ;
use Shape;
use Transform;
use ;
use ;
use Face;
// First, acquire a [`Shape`]. This can be done by procedurally
// generating one or by loading one from a font:
let face = parse.unwrap;
let glyph_id = face.glyph_index.unwrap;
let mut shape = load_from_face;
// Prepare your transformation matrix and calculate the dimensions of
// the resulting signed distance field. As an example, we set this up
// using ‘shrinkage’ (font units per texel) and ‘range’ (number of
// texels for the margin) values.
// Note that since font files interpret a positive y-offset as
// pointing up, the resulting distance field will be upside-down.
// This can be corrected either by flipping the resulting image
// vertically or by modifying the transformation matrix. We omit
// this fix for simplicity.
let bbox = face.glyph_bounding_box.unwrap;
const RANGE: f64 = 4.0;
const SHRINKAGE: f64 = 16.0;
let transformation = ;
let width =
.ceil as u32;
let height =
.ceil as u32;
// Unlike msdfgen, the transformation is not passed into the
// `generate_msdf` function – the coordinates of the control points
// must be expressed in terms of pixels on the distance field. To get
// the correct units, we pre-transform the shape:
shape.transform;
// We now color the edges of the shape. We also have to prepare
// it for calculations:
let colored_shape = edge_coloring_simple;
let prepared_colored_shape = colored_shape.prepare;
// Set up the resulting image and generate the distance field:
let mut msdf = new;
generate_msdf;
correct_sign_msdf;
// As a test, try previewing the distance field:
let mut preview = new;
render_msdf;
Roadmap
Currently, fdsm
has the basic functionality of generating MSDFs and generates correct distance fields for the glyphs A
to Z
in Noto Sans. However, it does not have all of the features present in msdfgen
.
- Error correction
- Error estimation
- Sign correction
- Shape simplification (cf. Section 3.1 of (Chlumský, 2015))
- Alternative edge-coloring algorithms
- Benchmarks against
msdfgen