use crate::{text::VisualLine, Rect, Vec2};
use std::sync::Arc;
use unicode_segmentation::UnicodeSegmentation;
const MAX_CHARACTERS: usize = 240;
const MAX_LENGTH: usize = u8::MAX as usize;
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct TextRun {
pub text: String,
pub start: usize,
pub rect: Rect,
pub lengths: Vec<u8>,
pub positions: Vec<f32>,
pub widths: Vec<f32>,
pub word_starts: Vec<u8>,
pub rtl: bool,
pub continues: bool,
}
impl TextRun {
pub fn character_at(&self, offset: usize) -> usize {
let mut end = 0;
self.lengths
.iter()
.take_while(|len| {
end += usize::from(**len);
end <= offset
})
.count()
}
pub fn offset_of(&self, index: usize) -> usize {
self.lengths
.iter()
.take(index)
.map(|len| usize::from(*len))
.sum()
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct TextModel {
pub runs: Vec<TextRun>,
}
struct Character {
start: usize,
end: usize,
x0: f32,
x1: f32,
word: bool,
}
fn finite(value: f32) -> f32 {
if value.is_finite() {
value
} else {
0.0
}
}
fn floor_boundary(text: &str, at: usize) -> usize {
let mut at = at.min(text.len());
while !text.is_char_boundary(at) {
at -= 1;
}
at
}
fn word_starts(paragraph: &str) -> Vec<usize> {
paragraph
.split_word_bound_indices()
.filter(|(_, word)| !word.chars().all(char::is_whitespace))
.map(|(start, _)| start)
.collect()
}
impl TextModel {
pub fn push_lines(
&mut self,
paragraph: &str,
base: usize,
lines: &[VisualLine],
origin: Vec2,
terminator: &str,
) {
let words = word_starts(paragraph);
let mut at = 0;
for (k, line) in lines.iter().enumerate() {
let last = k + 1 == lines.len();
let end = match lines.get(k + 1) {
Some(next) => floor_boundary(paragraph, next.start).max(at),
None => paragraph.len(),
};
let rtl = line.clusters.first().is_some_and(|c| c.rtl);
let (left, right) = line
.clusters
.iter()
.fold(None, |edges: Option<(f32, f32)>, c| {
let (x0, x1) = (finite(c.x0), finite(c.x1));
Some(edges.map_or((x0, x1), |(l, r)| (l.min(x0), r.max(x1))))
})
.unwrap_or((0.0, 0.0));
let mut edge = if rtl { right } else { left };
let mut characters = Vec::new();
let mut cluster = 0;
for (offset, grapheme) in paragraph[at..end].grapheme_indices(true) {
let start = at + offset;
while line.clusters.get(cluster).is_some_and(|c| c.start < start) {
cluster += 1;
}
let (x0, x1) = line
.clusters
.get(cluster)
.filter(|c| c.start == start)
.map_or((edge, edge), |c| (finite(c.x0), finite(c.x1)));
edge = if rtl { x0 } else { x1 };
characters.push(Character {
start,
end: start + grapheme.len(),
x0,
x1,
word: words.binary_search(&start).is_ok(),
});
}
let top = finite(line.top);
let bottom = finite(lines.get(k + 1).map_or(line.top + line.height, |n| n.top));
let vertical = (origin.y + top, origin.y + bottom.max(top));
let hard_break = (last && !terminator.is_empty()).then_some((terminator, edge));
self.push_line(
paragraph,
base,
&characters,
at..end,
rtl,
origin.x,
vertical,
hard_break,
);
at = end;
}
}
#[allow(clippy::too_many_arguments)]
fn push_line(
&mut self,
paragraph: &str,
base: usize,
characters: &[Character],
range: std::ops::Range<usize>,
rtl: bool,
x: f32,
(top, bottom): (f32, f32),
hard_break: Option<(&str, f32)>,
) {
let chunks = characters.chunks(MAX_CHARACTERS).count().max(1);
for n in 0..chunks {
let chunk = characters.chunks(MAX_CHARACTERS).nth(n).unwrap_or(&[]);
let start = chunk.first().map_or(range.start, |c| c.start);
let end = chunk.last().map_or(range.end, |c| c.end);
let (left, right) = chunk
.iter()
.fold(None, |edges: Option<(f32, f32)>, c| {
Some(edges.map_or((c.x0, c.x1), |(l, r)| (l.min(c.x0), r.max(c.x1))))
})
.unwrap_or_else(|| hard_break.map_or((0.0, 0.0), |(_, edge)| (edge, edge)));
let mut run = TextRun {
text: paragraph[start..end].to_owned(),
start: base + start,
rect: Rect::from_min_max(Vec2::new(x + left, top), Vec2::new(x + right, bottom)),
lengths: Vec::with_capacity(chunk.len() + 1),
positions: Vec::with_capacity(chunk.len() + 1),
widths: Vec::with_capacity(chunk.len() + 1),
word_starts: Vec::new(),
rtl,
continues: n > 0,
};
for c in chunk {
let position = if rtl { right - c.x1 } else { c.x0 - left };
if c.word && run.lengths.len() <= usize::from(u8::MAX) {
run.word_starts.push(run.lengths.len() as u8);
}
let mut piece = c.start;
while piece < c.end {
let next = floor_boundary(paragraph, (piece + MAX_LENGTH).min(c.end));
let next = if next > piece { next } else { c.end };
run.lengths.push((next - piece).min(MAX_LENGTH) as u8);
run.positions.push(position.max(0.0));
run.widths.push(if piece == c.start {
(c.x1 - c.x0).max(0.0)
} else {
0.0
});
piece = next;
}
}
if let Some((terminator, edge)) = hard_break.filter(|_| n + 1 == chunks) {
run.text.push_str(terminator);
run.lengths.push(terminator.len().min(MAX_LENGTH) as u8);
run.positions
.push(if rtl { right - edge } else { edge - left }.max(0.0));
run.widths.push(0.0);
}
self.runs.push(run);
}
}
pub fn push_text(&mut self, text: &str, lines: &[VisualLine], origin: Vec2) {
let mut base = 0;
let mut first = 0;
let mut paragraphs = text.split('\n').peekable();
while let Some(piece) = paragraphs.next() {
let paragraph = piece.strip_suffix('\r').unwrap_or(piece);
let mut count = 1.min(lines.len() - first.min(lines.len()));
while lines
.get(first + count)
.is_some_and(|line| line.start > lines[first + count - 1].start)
{
count += 1;
}
let more = paragraphs.peek().is_some();
let terminator = if more {
&text[base + paragraph.len()..base + piece.len() + 1]
} else {
""
};
let slice = lines.get(first..first + count).unwrap_or(&[]);
if slice.is_empty() {
self.runs
.push(TextRun::unshaped(paragraph, terminator, base, origin));
} else {
self.push_lines(paragraph, base, slice, origin, terminator);
}
first += count;
base += piece.len() + 1;
}
}
pub fn position(&self, offset: usize) -> Option<(usize, usize)> {
let run = self
.runs
.iter()
.rposition(|run| run.start <= offset)
.or((!self.runs.is_empty()).then_some(0))?;
let inside = offset.saturating_sub(self.runs[run].start);
Some((run, self.runs[run].character_at(inside)))
}
pub fn offset(&self, run: usize, index: usize) -> Option<usize> {
let run = self.runs.get(run)?;
Some(run.start + run.offset_of(index))
}
}
impl TextRun {
fn unshaped(paragraph: &str, terminator: &str, base: usize, origin: Vec2) -> Self {
let mut run = TextRun {
text: format!("{paragraph}{terminator}"),
start: base,
rect: Rect::from_min_size(origin, Vec2::ZERO),
lengths: Vec::new(),
positions: Vec::new(),
widths: Vec::new(),
word_starts: Vec::new(),
rtl: false,
continues: false,
};
for character in run.text.chars() {
run.lengths.push(character.len_utf8() as u8);
}
run.positions = vec![0.0; run.lengths.len()];
run.widths = vec![0.0; run.lengths.len()];
run
}
}
#[derive(Clone, Debug)]
pub(crate) struct TextRef(pub Arc<TextModel>);
impl PartialEq for TextRef {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0) || self.0 == other.0
}
}
impl From<TextModel> for TextRef {
fn from(model: TextModel) -> Self {
Self(Arc::new(model))
}
}