Crate line_straddler

source ·
Expand description

Figure out where lines should go when underlining/striking through text.

When you’re drawing text, you need to determine where the lines go for text decorations. This crate provides a renderer-agnostic LineGenerator that generates Line structures for a set of Glyphs.

Example

use line_straddler::{LineGenerator, Line, LineType, Glyph, GlyphStyle, Color};

// Take some glyphs from, e.g, cosmic-text
// For instance, this is two lines of two glyphs.
let style = GlyphStyle {
    boldness: 100,
    color: Color::rgba(0, 0, 0, 255),
};
let glyphs = [
    Glyph {
        line_y: 0.0,
        font_size: 4.0,
        width: 2.0,
        x: 0.0,
        style,
    },
    Glyph {
        line_y: 0.0,
        font_size: 4.0,
        width: 2.0,
        x: 3.0,
        style,
    },
    Glyph {
        line_y: 5.0,
        font_size: 4.0,
        width: 2.0,
        x: 0.0,
        style,
    },
    Glyph {
        line_y: 5.0,
        font_size: 4.0,
        width: 2.0,
        x: 3.0,
        style,
    },
];

// Create a line generator.
let mut alg = LineGenerator::new(LineType::Underline);

// Generate lines for the glyphs.
let mut lines = Vec::new();
for glyph in glyphs {
    lines.extend(alg.add_glyph(glyph));
}
lines.extend(alg.pop_line());

// Draw all of the lines.
for line in lines {
    let point_1 = (line.start_x, line.y);
    let point_2 = (line.end_x, line.y);
    draw_line(point_1, point_2, line.style);
}

Structs

Enums

  • What kind of lind are we trying to produce?