JFLib

render2d/msdf.rs#

Source: lib/render2d/msdf.rs

msdf -- multi-channel signed distance fields of glyph outlines on the CPU (G6c, decision 27; docs/RENDER2D.md "Text").

A glyph's contours (lines, quadratic and cubic Bézier segments in em units, y up) become a tile of RGBA texels: red,
green and blue each the signed pseudo-distance to the nearest edge of its colour, alpha the true signed distance to the
nearest edge of any colour (an MTSDF), positive inside, 0.5 on the outline, range / 2 texels on either side clamped.
The median of red, green and blue keeps a corner sharp at any magnification: edge colouring gives the two edges at a
corner different colours, so that two channels meet there as two straight half planes.  As msdfgen (Chlumsky, MIT;
rewritten from its description, nothing taken over as code):

  edge colouring   per contour the corners (a turn sharper than CORNER_CROSS, or back); none: every edge white (all
                   channels); one: three colours along the contour (an edge split in thirds where it has fewer than
                   three); more: cyan, magenta, yellow switching at each corner, the last spline different from the first
  distances        per texel and channel the edge of that colour with the least true distance (ties: the one met more
                   squarely), then its pseudo-distance (an end's tangent extended beyond the edge); alpha the least
                   true distance.  The nearest point of a curve: samples, then a safe Newton step in the bracket around
                   each sampled minimum (no trigonometry: the same bits on every target)
  sign correction  each texel's centre inside or out by the non-zero winding of a scanline; where the median says
                   otherwise every channel is mirrored at 0.5 (wrong signs from overlapping or touching contours)
  error correction a texel whose channels clash with a neighbour's (two channels jumping across 0.5 by more than a
                   texel's worth while the third stays: interpolating between them would make a false edge, at corners
                   and where contours come near each other) takes its median in all three channels; then the same for
                   the diagonal neighbours

Everything is f64 with +, -, *, / and a square root by Newton's method (math::sqrt): the same tile bytes on every
target (Windows, Linux, wasm32, Android), which the browser's and Vulkan's images rely on.
  • type P = [f64; 2]

  • enum Seg a segment of a contour (em units, y up)

    • Line(P, P)
    • Quad(P, P, P)
    • Cubic(P, P, P, P)
/// a corner is a turn whose cross product of the unit directions exceeds this (sin 3 rad, msdfgen's default angle) or
/// that turns back (dot <= 0)
CORNER_CROSS: f64 = 0.141_120_008_059_867_22

impl Seg

  • fn start(&self) -> P

  • fn end(&self) -> P

  • fn point(&self, t: f64) -> P the point at t (de Casteljau)

  • fn dir_start(&self) -> P the direction at its start (a control point on the end: the next one), msdfgen's direction(0)

  • fn dir_end(&self) -> P

  • fn bbox(&self) -> [f64; 4] its control hull's box: x0, y0, x1, y1

  • struct Params how a size class of the atlas makes its tiles: texels per em, the distance range (texels, both sides together), the texels around the glyph's box

    • em: f64
    • range: f64
    • pad: u32
  • struct Tile a glyph's field: w x h texels of RGBA (row 0 the lowest), texel (i, j) centred at ((x0 + i + 0.5) / em, (y0 + j + 0.5) / em) em units

    • w: u32
    • h: u32
    • x0: i32
    • y0: i32
    • px: Vec<u8>
  • fn bounds(contours: &[Vec<Seg>]) -> Option<[f64; 4]> the box of the contours' control hulls (None: no segment)

  • fn tile_box(b: [f64; 4], p: &Params) -> (i32, i32, u32, u32) the tile's texel box for contours of this box: (x0, y0, w, h) in texels of the class

  • fn tile(contours: &[Vec<Seg>], p: &Params) -> Option<Tile> a glyph's tile (None: no outline). contours in em units, y up, closed (each segment starts where the one before ended); fill rule non-zero

  • fn contours_of(pts: &[(f32, f32)], tags: &[u8], ends: &[u32], s: f64) -> Vec<Vec<Seg>> the outline as quadratic and cubic segments from lib/text's outline buffer (font units, TrueType contours with their implied on-curve midpoints, CFF contours of cubics), scaled by s (1 / units per em: em units)

  • fn ink_area(contours: &[Vec<Seg>]) -> f64 the area the contours fill (non-zero; em units squared), each segment as 16 chords: what a glyph's ink weighs

Source: docs/API.md in the JFLib repository.