use super::{
estimate_text_width, estimate_text_width_exact, find_next_tab_stop, TabStop, TextStyle,
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum PendingTab {
StoredStart {
position: f64,
fill_type: u8,
},
Right {
position: f64,
fill_type: u8,
},
Center {
position: f64,
fill_type: u8,
},
}
impl PendingTab {
pub(super) fn block_start(self, block_width: f64) -> f64 {
match self {
Self::StoredStart { position, .. } => position,
Self::Right { position, .. } => position - block_width,
Self::Center { position, .. } => position - block_width / 2.0,
}
}
pub(super) fn is_right_alignment(self) -> bool {
matches!(self, Self::Right { .. })
}
pub(super) fn blank_start(self, current: f64) -> f64 {
match self {
Self::StoredStart { position, .. } => position,
_ => current,
}
}
pub(crate) fn carry_over_blank(self, text: &str, style: &TextStyle) -> Self {
match self {
Self::StoredStart {
position,
fill_type,
} => Self::StoredStart {
position: position + estimate_text_width_exact(text, style),
fill_type,
},
_ => self,
}
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn resolve_last_tab_pending(
run_text: &str,
last_inline_idx: usize,
extensions: &[[u16; 7]],
style: &TextStyle,
stops: &[TabStop],
tab_width: f64,
auto_right: bool,
available_width: f64,
) -> Option<PendingTab> {
let last_tab = run_text.rfind('\t')?;
let before = &run_text[..last_tab];
if let Some(ext) = extensions
.get(last_inline_idx)
.filter(|ext| !crate::model::paragraph::tab_ext_is_placeholder(ext))
{
match ext[2] >> 8 {
0 | 1 => return None,
2 => {
let width_hu = u32::from(ext[0]) | (u32::from(ext[1]) << 16);
if width_hu > 0 {
return Some(PendingTab::StoredStart {
position: style.line_x_offset
+ estimate_text_width_exact(before, style)
+ f64::from(width_hu) * 96.0 / 7200.0
+ estimate_text_width_exact(&run_text[last_tab + 1..], style),
fill_type: (ext[2] & 0xff) as u8,
});
}
}
3 => {}
_ => return None,
}
}
let (position, kind, fill_type) = find_next_tab_stop(
style.line_x_offset + estimate_text_width(before, style),
stops,
if tab_width > 0.0 { tab_width } else { 48.0 },
auto_right,
available_width,
style.text_start_offset,
);
match kind {
1 => Some(PendingTab::Right {
position,
fill_type,
}),
2 => Some(PendingTab::Center {
position,
fill_type,
}),
_ => None,
}
}