use f_office_paint::Turn;
use f_office_units::Length;
use crate::lines::{EndHyphen, Line, LineEnd, Piece};
use crate::text::{AUTOMATIC_HYPHEN, LINE_END_HYPHEN, NON_BREAKING_HYPHEN, SOFT_HYPHEN};
use crate::text_map::text_offset;
const OBJECT: char = '\u{FFFC}';
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LineText {
pub text: String,
pub clusters: Vec<TextCluster>,
pub left: Length,
pub end: LineTextEnd,
pub hyphen: Option<EndHyphen>,
pub turn: Option<LineTurn>,
pub(crate) runs: Vec<RunSpan>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct RunSpan {
pub(crate) start: u32,
pub(crate) end: u32,
pub(crate) run: Option<u32>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LineTurn {
pub from: (Length, Length),
pub to: (Length, Length),
pub turn: Turn,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TextCluster {
pub start: u32,
pub end: u32,
pub left: Length,
pub right: Length,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LineTextEnd {
#[default]
Wrap,
LineBreak,
PageBreak,
ColumnBreak,
Paragraph,
Cell,
}
pub(super) fn line_text(
line: &Line,
(start, edges): (Length, &[(Length, Length)]),
order: &[usize],
last: bool,
) -> LineText {
let visual_of = visual_places(order, line.placed.len());
let hyphen = line.hyphen.and_then(|_| {
line.placed
.iter()
.rposition(|placed| matches!(placed.piece, Piece::Glyph(_)))
});
let mut read = Reading::default();
for (own, placed) in line.placed.iter().enumerate() {
if let Some(ch) = piece_char(&placed.piece).filter(|_| Some(own) != hyphen) {
let visual = visual_of.get(own).copied().unwrap_or(own);
let joins = matches!(placed.piece, Piece::Glyph(glyph) if glyph.part)
|| unicode_normalization::char::is_combining_mark(ch);
let edge = edges.get(visual).copied().unwrap_or_default();
let start = read.text.len();
read.add(ch, joins, (visual, edge));
if let Piece::Glyph(glyph) = placed.piece {
let run = (!glyph.label).then_some(glyph.place.run).flatten();
read.span(run, start);
}
}
}
let end = line_end(line.end, last);
let right = edges.iter().map(|&(_, right)| right).max();
LineText {
hyphen: line.hyphen,
..read.close(end, right.unwrap_or(start))
}
}
fn visual_places(order: &[usize], count: usize) -> Vec<usize> {
if order.is_empty() {
return (0..count).collect();
}
let mut visual_of = vec![0; count];
for (visual, &own) in order.iter().enumerate() {
if let Some(slot) = visual_of.get_mut(own) {
*slot = visual;
}
}
visual_of
}
const fn line_end(end: LineEnd, last: bool) -> LineTextEnd {
match end {
_ if last => LineTextEnd::Paragraph,
LineEnd::Wrap | LineEnd::Split => LineTextEnd::Wrap,
LineEnd::Break(_) => LineTextEnd::LineBreak,
LineEnd::PageBreak => LineTextEnd::PageBreak,
LineEnd::ColumnBreak => LineTextEnd::ColumnBreak,
}
}
#[derive(Default)]
struct Reading {
text: String,
clusters: Vec<(usize, TextCluster)>,
runs: Vec<RunSpan>,
}
impl Reading {
fn span(&mut self, run: Option<u32>, start: usize) {
let end = text_offset(self.text.len());
match self.runs.last_mut().filter(|span| span.run == run) {
Some(span) => span.end = end,
None => self.runs.push(RunSpan {
start: text_offset(start),
end,
run,
}),
}
}
fn add(&mut self, ch: char, joins: bool, (visual, (left, right)): (usize, (Length, Length))) {
let start = text_offset(self.text.len());
self.text.push(ch);
let end = text_offset(self.text.len());
match self.clusters.last_mut().filter(|_| joins) {
Some((_, cluster)) => {
cluster.end = end;
cluster.left = cluster.left.min(left);
cluster.right = cluster.right.max(right);
}
None => self.clusters.push((
visual,
TextCluster {
start,
end,
left,
right,
},
)),
}
}
fn close(mut self, end: LineTextEnd, right: Length) -> LineText {
self.clusters.sort_by_key(|&(visual, _)| visual);
let breaks = matches!(
end,
LineTextEnd::LineBreak | LineTextEnd::PageBreak | LineTextEnd::ColumnBreak
);
if breaks && !self.text.ends_with('\n') {
self.add('\n', false, (usize::MAX, (right, right)));
}
let clusters: Vec<TextCluster> = self
.clusters
.into_iter()
.map(|(_, cluster)| cluster)
.collect();
LineText {
text: self.text,
left: clusters.first().map_or(right, |cluster| cluster.left),
clusters,
end,
hyphen: None,
turn: None,
runs: self.runs,
}
}
}
fn piece_char(piece: &Piece) -> Option<char> {
match piece {
Piece::Glyph(glyph) => match glyph.ch {
NON_BREAKING_HYPHEN => Some('\u{2011}'),
SOFT_HYPHEN => Some('\u{AD}'),
AUTOMATIC_HYPHEN | LINE_END_HYPHEN => None,
ch => Some(ch),
},
Piece::Tab(_) | Piece::PositionTab(..) => Some('\t'),
Piece::LineBreak(_) | Piece::PageBreak | Piece::ColumnBreak => Some('\n'),
Piece::Object { .. } | Piece::CheckBox { .. } => Some(OBJECT),
Piece::Note(_)
| Piece::NoteMarkEnd
| Piece::Comment(_)
| Piece::Anchor(_)
| Piece::Strut(_)
| Piece::Clear
| Piece::Rule { .. }
| Piece::Shift(_)
| Piece::Advance(_)
| Piece::Bar { .. }
| Piece::Vinculum { .. } => None,
}
}