#line #glyph #determine #go #set #generate #renderer-agnostic

no-std line-straddler

Determine how lines through text (underlines, strikethroughs, etc) should be rendered

6 releases

0.2.3 Jan 5, 2024
0.2.2 Nov 28, 2023
0.2.1 Aug 5, 2023
0.2.0 Jul 7, 2023
0.1.0 May 11, 2023

#350 in Text processing

Download history 117/week @ 2023-12-20 4/week @ 2023-12-27 3/week @ 2024-01-03 80/week @ 2024-01-17 66/week @ 2024-02-07 2/week @ 2024-02-14 17/week @ 2024-02-21 21/week @ 2024-02-28 10/week @ 2024-03-06 10/week @ 2024-03-13 1/week @ 2024-03-20 16/week @ 2024-03-27 33/week @ 2024-04-03

61 downloads per month
Used in 6 crates (via piet-cosmic-text)

LGPL-3.0-or-later OR MPL-2.0

16KB
159 lines

line-straddler

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.

Source Code

The canonical code for this repository is kept on Codeberg. For convenience, a mirror is kept on GitHub.

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 {
    bold: false,
    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);
}

License

line-straddler is free software: you can redistribute it and/or modify it under the terms of either:

  • GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  • Mozilla Public License as published by the Mozilla Foundation, version 2.

line-straddler is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the Mozilla Public License for more details.

You should have received a copy of the GNU Lesser General Public License and the Mozilla Public License along with line-straddler. If not, see https://www.gnu.org/licenses/.

Dependencies

~105KB