use crate::document::Paragraph;
use crate::style::HangingEnd;
use crate::text::line_boxes::Slot;
use crate::text::{GlyphRun, hyphenation::SOFT_HYPHEN};
use icu_properties::{CodePointMapData, props::GeneralCategory};
use std::ops::Range;
const STOPS_AND_COMMAS: [char; 13] = [
'\u{002c}', '\u{002e}', '\u{060c}', '\u{06d4}', '\u{3001}', '\u{3002}', '\u{ff0c}', '\u{ff0e}',
'\u{fe50}', '\u{fe51}', '\u{fe52}', '\u{ff61}', '\u{ff64}',
];
const ASCII_QUOTES: [char; 2] = ['\'', '"'];
pub(super) const IDEOGRAPHIC_SPACE: char = '\u{3000}';
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Hangable {
start: f32,
end: f32,
conditional: bool,
}
impl Hangable {
pub(super) fn fit(self) -> f32 {
self.start + self.end
}
pub(super) fn intrinsic(self, min_content: bool) -> f32 {
self.start
+ if self.conditional && !min_content {
0.0
} else {
self.end
}
}
pub(super) fn hanging(self, width: f32, available: f32, rtl: bool) -> [f32; 2] {
let end = if self.conditional {
(width - self.start - available).clamp(0.0, self.end)
} else {
self.end
};
if rtl {
[end, self.start]
} else {
[self.start, end]
}
}
}
fn opening(ch: char) -> bool {
matches!(
CodePointMapData::<GeneralCategory>::new().get(ch),
GeneralCategory::OpenPunctuation
| GeneralCategory::InitialPunctuation
| GeneralCategory::FinalPunctuation
) || ASCII_QUOTES.contains(&ch)
|| ch == IDEOGRAPHIC_SPACE
}
fn closing(ch: char) -> bool {
matches!(
CodePointMapData::<GeneralCategory>::new().get(ch),
GeneralCategory::ClosePunctuation
| GeneralCategory::InitialPunctuation
| GeneralCategory::FinalPunctuation
) || ASCII_QUOTES.contains(&ch)
}
fn edge_cluster(paragraph: &Paragraph, run: &GlyphRun, right: bool) -> Option<(usize, char, f32)> {
let edge = if right {
run.glyphs.len().checked_sub(1)?
} else {
0
};
let range = run.glyphs[edge].text_range.clone();
let offset = run.source_range.start + range.start;
if run.leader || !run.source_range.contains(&offset) {
return None;
}
let ch = paragraph.text()[offset..].chars().next()?;
let width = (0..run.glyphs.len())
.filter(|&index| run.glyphs[index].text_range == range)
.map(|index| run.advance(index))
.sum();
(ch != SOFT_HYPHEN).then_some((offset, ch, width))
}
pub(super) fn hangable(
paragraph: &Paragraph,
range: Range<usize>,
visible: Range<usize>,
runs: &[GlyphRun],
order: &[Slot],
) -> Hangable {
let mut hangable = Hangable::default();
if !paragraph
.spans()
.iter()
.any(|span| span.style.line_breaking.hanging_punctuation != Default::default())
{
return hangable;
}
let rtl = paragraph.style().rtl;
let edge_run = |right: bool| {
let mut slots = order
.iter()
.filter(|slot| !matches!(slot, Slot::Edge { width, .. } if *width == 0.0));
let slot = if right {
slots.next_back()
} else {
slots.next()
};
match slot {
Some(Slot::Run(index)) => Some(&runs[*index]),
_ => None,
}
};
if let Some((offset, ch, width)) =
edge_run(rtl).and_then(|run| edge_cluster(paragraph, run, rtl))
&& range.start == 0
&& paragraph.first_formatted_line()
&& offset == visible.start
&& opening(ch)
&& paragraph
.style_at(offset)
.line_breaking
.hanging_punctuation
.first
{
hangable.start = width;
}
if let Some((offset, ch, width)) =
edge_run(!rtl).and_then(|run| edge_cluster(paragraph, run, !rtl))
&& offset + ch.len_utf8() == visible.end
{
let hanging = paragraph.style_at(offset).line_breaking.hanging_punctuation;
if let Some(end) = hanging.end.filter(|_| STOPS_AND_COMMAS.contains(&ch)) {
hangable.end = width;
hangable.conditional = end == HangingEnd::Allow;
} else if hanging.last
&& closing(ch)
&& range.end == paragraph.text().len()
&& paragraph.last_formatted_line()
{
hangable.end = width;
}
}
hangable
}