const EPSILON: f32 = 1.0;
pub(super) struct SelectedTextFragment {
pub(super) x: f32,
pub(super) end_x: f32,
pub(super) top: f32,
pub(super) bottom: f32,
pub(super) text: String,
}
impl SelectedTextFragment {
fn beside(&self, other: &Self) -> bool {
overlap(self.top, self.bottom, other.top, other.bottom) && !overlap(self.x, self.end_x, other.x, other.end_x)
}
}
fn overlap(start: f32, end: f32, other_start: f32, other_end: f32) -> bool {
start < other_end - EPSILON && other_start < end - EPSILON
}
pub(super) fn join_selected_text(mut fragments: Vec<SelectedTextFragment>) -> String {
fragments.sort_by(|a, b| a.top.total_cmp(&b.top).then(a.x.total_cmp(&b.x)));
let mut lines: Vec<Vec<SelectedTextFragment>> = Vec::new();
for fragment in fragments {
match lines.last_mut() {
Some(line) if line.iter().all(|other| fragment.beside(other)) => line.push(fragment),
_ => lines.push(vec![fragment]),
}
}
let mut selected = String::new();
for (index, line) in lines.iter_mut().enumerate() {
if index > 0 {
selected.push('\n');
}
line.sort_by(|a, b| a.x.total_cmp(&b.x));
let mut previous_end_x: Option<f32> = None;
for fragment in line.iter() {
if let Some(end_x) = previous_end_x
&& fragment.x - end_x > EPSILON
&& !selected.chars().last().is_some_and(char::is_whitespace)
&& !fragment.text.chars().next().is_some_and(char::is_whitespace)
{
selected.push(' ');
}
selected.push_str(&fragment.text);
previous_end_x = Some(fragment.end_x);
}
}
selected
}
#[cfg(test)]
mod tests {
use super::{SelectedTextFragment, join_selected_text};
fn fragment(text: &str, [x, top, end_x, bottom]: [f32; 4]) -> SelectedTextFragment {
SelectedTextFragment {
x,
end_x,
top,
bottom,
text: text.to_owned(),
}
}
#[test]
fn texts_stacked_beside_a_taller_text_keep_their_own_lines() {
let copied = join_selected_text(vec![
fragment("third", [200.0, 40.0, 260.0, 60.0]),
fragment("tall", [0.0, 0.0, 100.0, 60.0]),
fragment("second", [200.0, 20.0, 260.0, 40.0]),
fragment("first", [200.0, 0.0, 260.0, 20.0]),
]);
assert_eq!(copied, "tall first\nsecond\nthird");
}
#[test]
fn boxes_that_only_touch_are_lines_of_their_own() {
let copied = join_selected_text(vec![
fragment("below", [0.0, 20.0, 50.0, 40.0]),
fragment("above", [60.0, 0.0, 110.0, 20.5]),
]);
assert_eq!(copied, "above\nbelow");
}
}