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