use super::shaper::ShapedGlyph;
use strata_primitives::geom::LayoutUnit;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LineBreakClass {
BK,
CR,
LF,
ZW,
WJ,
GL,
SP,
OP,
CL,
CP,
EX,
NS,
ID,
AL,
NU,
HY,
BA,
BB,
IN,
IS,
PR,
PO,
XX,
}
pub fn classify_line_break(c: char) -> LineBreakClass {
match c {
'\n' => LineBreakClass::LF,
'\r' => LineBreakClass::CR,
'\u{000C}' => LineBreakClass::BK,
'\u{200B}' => LineBreakClass::ZW,
'\u{2060}' | '\u{FEFF}' => LineBreakClass::WJ,
'\u{00A0}' | '\u{202F}' => LineBreakClass::GL,
' ' | '\t' => LineBreakClass::SP,
'(' | '[' | '{' | '<' | '(' | '[' | '{' | '〈' | '《' | '「' | '『' | '【' | '〔'
| '“' | '‘' | '«' => LineBreakClass::OP,
')' | ']' | '}' | '>' | ')' | ']' | '}' | '〉' | '》' | '」' | '』' | '】' | '〕'
| '”' | '’' | '»' | '。' | '、' | ',' | '.' => LineBreakClass::CL,
'!' | '?' | '!' | '?' => LineBreakClass::EX,
'ぁ' | 'ぃ' | 'ぅ' | 'ぇ' | 'ぉ' | 'っ' | 'ゃ' | 'ゅ' | 'ょ' | 'ゎ' | 'ァ' | 'ィ'
| 'ゥ' | 'ェ' | 'ォ' | 'ッ' | 'ャ' | 'ュ' | 'ョ' | 'ヮ' | 'ヶ' | 'ヵ' | 'ー' | '・'
| ':' | ';' => LineBreakClass::NS,
'-' => LineBreakClass::HY,
'—' | '–' | '\u{00AD}' => LineBreakClass::BA,
'…' | '‥' => LineBreakClass::IN,
'$' | '£' | '¥' | '€' | '¢' | '¥' => LineBreakClass::PR,
'%' | '‰' | '°' => LineBreakClass::PO,
',' | '.' | ':' | ';' => LineBreakClass::IS,
'0'..='9' | '٠'..='٩' => LineBreakClass::NU,
'\u{4E00}'..='\u{9FFF}'
| '\u{3400}'..='\u{4DBF}'
| '\u{20000}'..='\u{2A6DF}'
| '\u{3040}'..='\u{309F}'
| '\u{30A0}'..='\u{30FF}'
| '\u{AC00}'..='\u{D7AF}' => LineBreakClass::ID,
_ => LineBreakClass::AL,
}
}
#[inline]
pub fn is_prohibited_start(c: char) -> bool {
matches!(
classify_line_break(c),
LineBreakClass::CL | LineBreakClass::CP | LineBreakClass::EX | LineBreakClass::NS
)
}
#[inline]
pub fn is_prohibited_end(c: char) -> bool {
matches!(
classify_line_break(c),
LineBreakClass::OP | LineBreakClass::PR
)
}
pub fn can_break_between_chars(prev: char, curr: char) -> bool {
let p_class = classify_line_break(prev);
let c_class = classify_line_break(curr);
if matches!(
p_class,
LineBreakClass::BK | LineBreakClass::CR | LineBreakClass::LF
) {
return true;
}
if p_class == LineBreakClass::WJ || c_class == LineBreakClass::WJ {
return false;
}
if p_class == LineBreakClass::GL || c_class == LineBreakClass::GL {
return false;
}
if matches!(
c_class,
LineBreakClass::CL | LineBreakClass::CP | LineBreakClass::EX | LineBreakClass::NS
) {
return false;
}
if matches!(p_class, LineBreakClass::OP | LineBreakClass::PR) {
return false;
}
if p_class == LineBreakClass::ZW {
return true;
}
if p_class == LineBreakClass::SP {
return true;
}
if p_class == LineBreakClass::ID && c_class == LineBreakClass::ID {
return true;
}
if p_class == LineBreakClass::ID
&& (c_class == LineBreakClass::AL || c_class == LineBreakClass::NU)
{
return true;
}
if (p_class == LineBreakClass::AL || p_class == LineBreakClass::NU)
&& c_class == LineBreakClass::ID
{
return true;
}
if matches!(p_class, LineBreakClass::CL | LineBreakClass::CP)
&& (c_class == LineBreakClass::ID || c_class == LineBreakClass::AL)
{
return true;
}
if matches!(p_class, LineBreakClass::HY | LineBreakClass::BA) {
return true;
}
false
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WhiteSpaceMode {
#[default]
Normal,
NoWrap,
Pre,
PreWrap,
PreLine,
}
impl WhiteSpaceMode {
#[inline(always)]
pub fn collapses_spaces(self) -> bool {
matches!(
self,
WhiteSpaceMode::Normal | WhiteSpaceMode::NoWrap | WhiteSpaceMode::PreLine
)
}
#[inline(always)]
pub fn allows_soft_wrap(self) -> bool {
matches!(
self,
WhiteSpaceMode::Normal | WhiteSpaceMode::PreWrap | WhiteSpaceMode::PreLine
)
}
#[inline(always)]
pub fn preserves_newlines(self) -> bool {
matches!(
self,
WhiteSpaceMode::Pre | WhiteSpaceMode::PreWrap | WhiteSpaceMode::PreLine
)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TextLine {
pub glyphs: Vec<ShapedGlyph>,
pub content_width: LayoutUnit,
pub trailing_space_width: LayoutUnit,
pub is_hard_break: bool,
}
#[derive(Debug, Clone, Default)]
pub struct LineBreaker;
impl LineBreaker {
pub fn new() -> Self {
Self
}
pub fn break_lines(
&self,
glyphs: &[ShapedGlyph],
max_width: Option<LayoutUnit>,
mode: WhiteSpaceMode,
) -> Vec<TextLine> {
if glyphs.is_empty() {
return Vec::new();
}
let max_w = max_width.unwrap_or(LayoutUnit(i32::MAX));
let allows_soft_wrap = mode.allows_soft_wrap() && max_width.is_some();
let collapses_spaces = mode.collapses_spaces();
let mut lines = Vec::new();
let mut current_glyphs = Vec::new();
let mut current_width = LayoutUnit::ZERO;
let mut last_break_idx = None;
let mut prev_was_space = false;
let mut prev_char: Option<char> = None;
let mut idx = 0;
while idx < glyphs.len() {
let glyph = glyphs[idx];
let curr_char = char::from_u32(glyph.glyph_id).unwrap_or(' ');
if glyph.glyph_id == 10 || glyph.glyph_id == 13 {
let (content_w, trailing_w) = Self::calculate_widths(¤t_glyphs);
lines.push(TextLine {
glyphs: std::mem::take(&mut current_glyphs),
content_width: content_w,
trailing_space_width: trailing_w,
is_hard_break: true,
});
current_width = LayoutUnit::ZERO;
last_break_idx = None;
prev_was_space = false;
prev_char = None;
idx += 1;
continue;
}
if collapses_spaces && glyph.is_space {
if prev_was_space || current_glyphs.is_empty() {
idx += 1;
continue;
}
prev_was_space = true;
} else {
prev_was_space = false;
}
if let Some(p_ch) = prev_char {
if can_break_between_chars(p_ch, curr_char) {
last_break_idx = Some(current_glyphs.len());
}
}
let advance = glyph.x_advance;
let fits = current_width + advance <= max_w;
if !fits && allows_soft_wrap && !current_glyphs.is_empty() {
if let Some(break_pos) = last_break_idx {
if break_pos > 0 && break_pos < current_glyphs.len() {
let remainder: Vec<ShapedGlyph> =
current_glyphs.drain(break_pos..).collect();
let (content_w, trailing_w) = Self::calculate_widths(¤t_glyphs);
lines.push(TextLine {
glyphs: std::mem::take(&mut current_glyphs),
content_width: content_w,
trailing_space_width: trailing_w,
is_hard_break: false,
});
current_glyphs = remainder;
current_width = current_glyphs
.iter()
.map(|g| g.x_advance)
.fold(LayoutUnit::ZERO, |a, b| a + b);
last_break_idx = None;
} else {
let (content_w, trailing_w) = Self::calculate_widths(¤t_glyphs);
lines.push(TextLine {
glyphs: std::mem::take(&mut current_glyphs),
content_width: content_w,
trailing_space_width: trailing_w,
is_hard_break: false,
});
current_width = LayoutUnit::ZERO;
last_break_idx = None;
}
} else {
let (content_w, trailing_w) = Self::calculate_widths(¤t_glyphs);
lines.push(TextLine {
glyphs: std::mem::take(&mut current_glyphs),
content_width: content_w,
trailing_space_width: trailing_w,
is_hard_break: false,
});
current_width = LayoutUnit::ZERO;
last_break_idx = None;
}
}
current_width = current_width + advance;
current_glyphs.push(glyph);
prev_char = Some(curr_char);
idx += 1;
}
if !current_glyphs.is_empty() {
let (content_w, trailing_w) = Self::calculate_widths(¤t_glyphs);
lines.push(TextLine {
glyphs: current_glyphs,
content_width: content_w,
trailing_space_width: trailing_w,
is_hard_break: false,
});
}
lines
}
fn calculate_widths(glyphs: &[ShapedGlyph]) -> (LayoutUnit, LayoutUnit) {
if glyphs.is_empty() {
return (LayoutUnit::ZERO, LayoutUnit::ZERO);
}
let mut trailing_w = LayoutUnit::ZERO;
for g in glyphs.iter().rev() {
if g.is_space {
trailing_w = trailing_w + g.x_advance;
} else {
break;
}
}
let total_w = glyphs
.iter()
.map(|g| g.x_advance)
.fold(LayoutUnit::ZERO, |a, b| a + b);
let content_w = total_w - trailing_w;
(content_w, trailing_w)
}
}