use crate::layout::line::{LayoutLine, PositionedRun};
use crate::shaping::shaper::TextDirection;
use crate::types::{CharacterGeometry, GeometryDirection, LineEnd, LineGeometry, LineSegment};
pub(crate) fn char_byte_table(text: &str) -> Vec<usize> {
let mut table = Vec::with_capacity(text.chars().count() + 1);
table.extend(text.char_indices().map(|(i, _)| i));
table.push(text.len());
table
}
pub(crate) fn byte_at(table: &[usize], index: usize) -> usize {
match table.get(index) {
Some(b) => *b,
None => table.last().copied().unwrap_or(0),
}
}
pub(crate) struct RawSegment {
pub char_range: std::ops::Range<usize>,
pub direction: GeometryDirection,
pub left: f32,
pub width: f32,
pub chars: Vec<(f32, f32)>,
}
impl RawSegment {
pub(crate) fn characters(&self) -> Vec<CharacterGeometry> {
let rtl = self.direction == GeometryDirection::RightToLeft;
let right = self.left + self.width;
let mut previous = 0.0f32;
self.chars
.iter()
.map(|&(leading, advance)| {
let raw = if rtl {
right - leading
} else {
leading - self.left
};
let position = raw.max(previous).max(0.0);
previous = position;
CharacterGeometry {
position,
width: advance.max(0.0),
}
})
.collect()
}
}
fn raw_segment_of_run(line: &LayoutLine, run: &PositionedRun) -> Option<RawSegment> {
let glyphs = &run.shaped_run.glyphs;
if glyphs.is_empty() {
return None;
}
let rtl = run.shaped_run.direction == TextDirection::RightToLeft;
let mut groups: Vec<(usize, f32, f32)> = Vec::new();
let mut pen = run.x;
for glyph in glyphs {
let cluster = glyph.cluster as usize;
match groups.last_mut() {
Some(last) if last.0 == cluster => last.2 += glyph.x_advance,
_ => groups.push((cluster, pen, glyph.x_advance)),
}
pen += glyph.x_advance;
}
let left = run.x;
let width = pen - run.x;
groups.sort_by_key(|(cluster, _, _)| *cluster);
let first_char = groups.first()?.0;
let last_cluster = groups.last()?.0;
let logical_end = line.cluster_end(last_cluster).max(last_cluster + 1);
let mut chars: Vec<(f32, f32)> = Vec::with_capacity(logical_end.saturating_sub(first_char));
for (i, &(cluster, visual_left, advance)) in groups.iter().enumerate() {
let next = groups
.get(i + 1)
.map(|(c, _, _)| *c)
.unwrap_or(logical_end)
.max(cluster + 1);
let leading = if rtl {
visual_left + advance
} else {
visual_left
};
chars.push((leading, advance));
for _ in (cluster + 1)..next {
chars.push((leading, 0.0));
}
}
let char_range = first_char..first_char + chars.len();
Some(RawSegment {
char_range,
direction: if rtl {
GeometryDirection::RightToLeft
} else {
GeometryDirection::LeftToRight
},
left,
width,
chars,
})
}
pub(crate) fn raw_segments(line: &LayoutLine) -> Vec<RawSegment> {
let mut raws: Vec<RawSegment> = line
.runs
.iter()
.filter_map(|r| raw_segment_of_run(line, r))
.collect();
raws.sort_by_key(|s| s.char_range.start);
let mut merged: Vec<RawSegment> = Vec::with_capacity(raws.len());
for seg in raws {
if let Some(last) = merged.last_mut()
&& last.direction == seg.direction
&& last.char_range.end == seg.char_range.start
{
let left = last.left.min(seg.left);
let right = (last.left + last.width).max(seg.left + seg.width);
last.left = left;
last.width = right - left;
last.char_range.end = seg.char_range.end;
last.chars.extend(seg.chars);
continue;
}
merged.push(seg);
}
merged
}
fn line_end(text: &str, table: &[usize], char_end: usize, total_chars: usize) -> LineEnd {
let byte_end = byte_at(table, char_end);
let upto = &text[..byte_end];
if upto.ends_with("\r\n") {
LineEnd::HardBreak { chars: 2, bytes: 2 }
} else if upto.ends_with('\n') {
LineEnd::HardBreak { chars: 1, bytes: 1 }
} else if char_end >= total_chars {
LineEnd::EndOfText
} else {
LineEnd::SoftWrap
}
}
pub(crate) fn line_geometry(
line: &LayoutLine,
index: usize,
y_top: f32,
text: &str,
table: &[usize],
total_chars: usize,
) -> LineGeometry {
let merged = raw_segments(line);
let line_height = line.line_height;
let segments: Vec<LineSegment> = merged
.iter()
.map(|seg| LineSegment {
byte_range: byte_at(table, seg.char_range.start)..byte_at(table, seg.char_range.end),
char_range: seg.char_range.clone(),
direction: seg.direction,
rect: [seg.left, y_top, seg.width, line_height],
characters: seg.characters(),
})
.collect();
let left = merged.iter().map(|s| s.left).fold(f32::INFINITY, f32::min);
let right = merged
.iter()
.map(|s| s.left + s.width)
.fold(f32::NEG_INFINITY, f32::max);
let (x, width) = if left.is_finite() && right.is_finite() {
(left, right - left)
} else {
(line.empty_caret_x, 0.0)
};
let char_range = line.char_range.start.min(total_chars)..line.char_range.end.min(total_chars);
LineGeometry {
index,
byte_range: byte_at(table, char_range.start)..byte_at(table, char_range.end),
end: line_end(text, table, char_range.end, total_chars),
char_range,
rect: [x, y_top, width, line_height],
baseline: y_top + line.ascent,
caret_x: line.empty_caret_x,
segments,
truncation: None,
}
}
pub(crate) fn char_at(table: &[usize], byte: usize) -> usize {
match table.binary_search(&byte) {
Ok(i) => i,
Err(i) => i.saturating_sub(1),
}
}
pub(crate) fn single_line_geometry(
text: &str,
runs: &[crate::shaping::run::ShapedRun],
emit_limit: Option<usize>,
truncation: Option<crate::types::LineTruncation>,
ascent: f32,
descent: f32,
leading: f32,
) -> crate::types::LayoutGeometry {
use crate::layout::line::RunDecorations;
let table = char_byte_table(text);
let total_chars = table.len().saturating_sub(1);
let mut positioned: Vec<PositionedRun> = Vec::with_capacity(runs.len());
let mut dropped_min: Option<usize> = None;
let mut emitted_max: Option<usize> = None;
let mut pen_x = 0.0f32;
let mut emitted = 0usize;
for run in runs {
let run_x = pen_x;
let mut glyphs = Vec::with_capacity(run.glyphs.len());
for glyph in &run.glyphs {
let cluster = char_at(&table, run.text_range.start + glyph.cluster as usize);
if emit_limit.is_some_and(|limit| emitted >= limit) {
dropped_min = Some(dropped_min.map_or(cluster, |m: usize| m.min(cluster)));
continue;
}
let mut g = glyph.clone();
g.cluster = cluster as u32;
pen_x += g.x_advance;
emitted += 1;
emitted_max = Some(emitted_max.map_or(cluster, |m: usize| m.max(cluster)));
glyphs.push(g);
}
if glyphs.is_empty() {
continue;
}
let advance: f32 = glyphs.iter().map(|g| g.x_advance).sum();
let mut shaped = run.clone();
shaped.glyphs = glyphs;
shaped.advance_width = advance;
positioned.push(PositionedRun {
shaped_run: shaped,
x: run_x,
decorations: RunDecorations::default(),
});
}
let drawn_is_logical_prefix = match (emitted_max, dropped_min) {
(_, None) => true,
(Some(last_drawn), Some(first_dropped)) => last_drawn < first_dropped,
(None, Some(_)) => true,
};
let covered = dropped_min.unwrap_or(total_chars);
let line_height = ascent + descent + leading;
let width = pen_x;
let line = LayoutLine {
runs: if drawn_is_logical_prefix {
positioned
} else {
Vec::new()
},
y: 0.0,
ascent,
descent,
leading,
line_height,
width,
char_range: 0..covered,
empty_caret_x: 0.0,
};
let mut geometry = line_geometry(&line, 0, 0.0, text, &table, total_chars);
geometry.char_range = 0..total_chars;
geometry.byte_range = 0..text.len();
geometry.end = line_end(text, &table, total_chars, total_chars);
geometry.truncation = truncation;
if geometry.segments.is_empty() {
geometry.rect = [0.0, 0.0, width, line_height];
}
crate::types::LayoutGeometry {
lines: vec![geometry],
dropped_lines: 0,
source_len: text.len(),
rendered_text: None,
links: Vec::new(),
}
}
pub(crate) fn stacked_lines_geometry(
lines: &[LayoutLine],
text: &str,
tops: impl Fn(usize, &LayoutLine) -> f32,
) -> Vec<LineGeometry> {
let table = char_byte_table(text);
let total_chars = table.len().saturating_sub(1);
lines
.iter()
.enumerate()
.map(|(i, line)| line_geometry(line, i, tops(i, line), text, &table, total_chars))
.collect()
}
pub(crate) fn character_geometry_over_lines(
lines: &[LayoutLine],
char_start: usize,
char_end: usize,
) -> Vec<CharacterGeometry> {
if char_start >= char_end {
return Vec::new();
}
let mut map: std::collections::BTreeMap<usize, (f32, f32)> = std::collections::BTreeMap::new();
for line in lines {
if line.char_range.end <= char_start || line.char_range.start >= char_end {
continue;
}
for seg in raw_segments(line) {
for (i, &(leading, advance)) in seg.chars.iter().enumerate() {
let c = seg.char_range.start + i;
if c >= char_start && c < char_end {
map.entry(c).or_insert((leading, advance));
}
}
}
}
let base = map.values().next().map(|(x, _)| *x).unwrap_or(0.0);
let mut out = Vec::with_capacity(char_end - char_start);
let mut carry = base;
for c in char_start..char_end {
match map.get(&c) {
Some(&(leading, advance)) => {
carry = leading + advance;
out.push(CharacterGeometry {
position: leading - base,
width: advance.max(0.0),
});
}
None => out.push(CharacterGeometry {
position: carry - base,
width: 0.0,
}),
}
}
out
}
pub(crate) fn paragraph_geometry(
lines: &[LayoutLine],
text: &str,
line_height: f32,
dropped_lines: usize,
rendered_text: Option<String>,
links: Vec<crate::types::LinkGeometry>,
) -> crate::types::LayoutGeometry {
crate::types::LayoutGeometry {
lines: stacked_lines_geometry(lines, text, |i, _| i as f32 * line_height),
dropped_lines,
source_len: text.len(),
rendered_text,
links,
}
}