#line #glyph #determine #text #go #how #generate

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

#356 in Text processing

Download history 22/week @ 2024-06-15 23/week @ 2024-06-22 3/week @ 2024-06-29 1/week @ 2024-07-06 16/week @ 2024-07-13 12/week @ 2024-07-20 9/week @ 2024-07-27 11/week @ 2024-08-03 8/week @ 2024-08-10 18/week @ 2024-08-17 38/week @ 2024-08-24 51/week @ 2024-08-31 25/week @ 2024-09-07 25/week @ 2024-09-14 50/week @ 2024-09-21 22/week @ 2024-09-28

124 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