text_typeset/layout/
line.rs1use std::ops::Range;
2
3use crate::shaping::run::ShapedRun;
4
5pub struct LayoutLine {
6 pub runs: Vec<PositionedRun>,
7 pub y: f32,
9 pub ascent: f32,
10 pub descent: f32,
11 pub leading: f32,
12 pub line_height: f32,
14 pub width: f32,
16 pub char_range: Range<usize>,
18}
19
20impl LayoutLine {
21 pub fn x_for_offset(&self, offset: usize) -> f32 {
26 for (i, run) in self.runs.iter().enumerate() {
27 let mut glyph_x = run.x;
28 for glyph in &run.shaped_run.glyphs {
29 if glyph.cluster as usize >= offset {
30 return glyph_x;
31 }
32 glyph_x += glyph.x_advance;
33 }
34 let next_run_start = self
36 .runs
37 .get(i + 1)
38 .and_then(|r| r.shaped_run.glyphs.first())
39 .map(|g| g.cluster as usize);
40 match next_run_start {
41 Some(next_start) if offset >= next_start => continue,
42 _ => return glyph_x,
43 }
44 }
45 self.runs
46 .last()
47 .map(|r| r.x + r.shaped_run.advance_width)
48 .unwrap_or(0.0)
49 }
50}
51
52pub struct PositionedRun {
53 pub shaped_run: ShapedRun,
54 pub x: f32,
56 pub decorations: RunDecorations,
58}
59
60#[derive(Clone, Debug, Default)]
62pub struct RunDecorations {
63 pub underline_style: crate::types::UnderlineStyle,
64 pub overline: bool,
65 pub strikeout: bool,
66 pub is_link: bool,
67 pub foreground_color: Option<[f32; 4]>,
69 pub underline_color: Option<[f32; 4]>,
71 pub background_color: Option<[f32; 4]>,
73 pub anchor_href: Option<String>,
75 pub tooltip: Option<String>,
77 pub vertical_alignment: crate::types::VerticalAlignment,
79}