use crate::document_core::DocumentCore;
use crate::model::control::Control;
use crate::model::document::Document;
use crate::model::paragraph::{LineSeg, Paragraph};
use crate::renderer::composer::paragraph_flow_end;
use crate::renderer::layout::control_line_seg_index;
use crate::renderer::pagination::{PageContent, PageItem};
use crate::renderer::px_to_hwpunit;
const ROUNDING_HU: i64 = 10;
pub(crate) type TacHostRowCandidate = (usize, usize, usize, f64);
impl DocumentCore {
pub(crate) fn tac_host_row_candidates(&self) -> Vec<TacHostRowCandidate> {
let mut candidates = Vec::new();
for (section_idx, dirty) in self.dirty_paragraphs.iter().enumerate() {
let (Some(dirty), Some(section), Some(measured)) = (
dirty.as_deref(),
self.document.sections.get(section_idx),
self.measured_sections.get(section_idx),
) else {
continue;
};
for (para_idx, para) in section.paragraphs.iter().enumerate() {
if !dirty.get(para_idx).copied().unwrap_or(false) {
continue;
}
for (control_idx, control) in para.controls.iter().enumerate() {
if !matches!(control, Control::Table(table) if table.common.treat_as_char) {
continue;
}
if let Some(previous) = measured.get_measured_table(para_idx, control_idx) {
candidates.push((
section_idx,
para_idx,
control_idx,
previous.total_height,
));
}
}
}
}
candidates
}
pub(crate) fn sync_tac_host_rows(&mut self, candidates: &[TacHostRowCandidate]) -> bool {
let rounding = crate::renderer::hwpunit_to_px(ROUNDING_HU as i32, self.dpi);
let mut changed = false;
for &(section_idx, para_idx, control_idx, previous) in candidates {
let Some(current) = self
.measured_sections
.get(section_idx)
.and_then(|section| section.get_measured_table(para_idx, control_idx))
.map(|table| table.total_height)
else {
continue;
};
if (current - previous).abs() <= rounding {
continue;
}
changed |= self.sync_tac_host_row(section_idx, para_idx, control_idx, current);
}
changed
}
pub(crate) fn shifted_page_starts(&self) -> Vec<(usize, usize)> {
let mut starts = Vec::new();
for (section_idx, result) in self.pagination.iter().enumerate() {
let Some(section) = self.document.sections.get(section_idx) else {
continue;
};
for para_idx in result.pages.iter().skip(1).filter_map(page_start_paragraph) {
if section
.paragraphs
.get(para_idx)
.is_some_and(|para| para.stored_flow_shifted)
&& continues_previous(§ion.paragraphs, para_idx).is_some()
{
starts.push((section_idx, para_idx));
}
}
}
starts
}
pub(crate) fn write_page_starts(&self, document: &mut Document, starts: &[(usize, usize)]) {
for &(section_idx, para_idx) in starts {
let Some(paragraphs) = document
.sections
.get_mut(section_idx)
.map(|section| &mut section.paragraphs)
else {
continue;
};
let Some((first, previous_end)) = continues_previous(paragraphs, para_idx) else {
continue;
};
let spacing_before = self
.styles
.para_styles
.get(paragraphs[para_idx].para_shape_id as usize)
.map_or(0, |style| {
px_to_hwpunit(style.spacing_before, self.dpi).max(0)
});
shift_following(
paragraphs,
para_idx,
Some(previous_end),
spacing_before.saturating_sub(first),
);
}
}
fn sync_tac_host_row(
&mut self,
section_idx: usize,
para_idx: usize,
control_idx: usize,
measured_px: f64,
) -> bool {
let Some(para) = self
.document
.sections
.get(section_idx)
.and_then(|section| section.paragraphs.get(para_idx))
else {
return false;
};
let Some(Control::Table(table)) = para.controls.get(control_idx) else {
return false;
};
if !table.common.treat_as_char || table.caption.is_some() || table.common.height == 0 {
return false;
}
let Some(row) = control_line_seg_index(para, control_idx) else {
return false;
};
let Some(seg) = para.line_segs.get(row) else {
return false;
};
let declared = i64::from(table.common.height);
let old_band =
declared + i64::from(table.outer_margin_top) + i64::from(table.outer_margin_bottom);
if seg.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| i64::from(seg.text_height) != old_band
{
return false;
}
let content = i64::from(px_to_hwpunit(measured_px, self.dpi));
let delta = content - declared;
if delta.abs() <= ROUNDING_HU || content <= 0 {
return false;
}
let (Ok(content), Ok(delta), Ok(old_band)) = (
u32::try_from(content),
i32::try_from(delta),
i32::try_from(old_band),
) else {
return false;
};
let new_band = old_band.saturating_add(delta);
let old_end = paragraph_flow_end(para);
let paragraphs = &mut self.document.sections[section_idx].paragraphs;
let para = &mut paragraphs[para_idx];
if let Some(Control::Table(table)) = para.controls.get_mut(control_idx) {
table.sync_ctrl_height(content);
}
for (index, seg) in para.line_segs.iter_mut().enumerate() {
if seg.line_height == old_band {
seg.line_height = new_band;
}
if index == row {
seg.text_height = new_band;
seg.baseline_distance = ((i64::from(seg.baseline_distance) * i64::from(new_band)
+ i64::from(old_band) / 2)
/ i64::from(old_band)) as i32;
} else if index > row {
seg.vertical_pos = seg.vertical_pos.saturating_add(delta);
}
}
shift_source_positions(para, row + 1, delta);
let shifted = shift_following(paragraphs, para_idx + 1, old_end, delta);
self.recompose_paragraph(section_idx, para_idx);
self.mark_paragraph_dirty(section_idx, para_idx);
for index in shifted {
self.mark_paragraph_dirty(section_idx, index);
}
self.mark_section_dirty(section_idx);
true
}
}
fn shift_following(
paragraphs: &mut [Paragraph],
from: usize,
mut previous_end: Option<i32>,
delta: i32,
) -> Vec<usize> {
let mut shifted = Vec::new();
for (index, next) in paragraphs.iter_mut().enumerate().skip(from) {
let Some(first) = next.line_segs.first() else {
continue;
};
if previous_end.is_some_and(|end| first.vertical_pos < end) {
break;
}
previous_end = paragraph_flow_end(next);
next.stored_flow_shifted = true;
for seg in &mut next.line_segs {
seg.vertical_pos = seg.vertical_pos.saturating_add(delta);
}
shift_source_positions(next, 0, delta);
shifted.push(index);
}
shifted
}
fn page_start_paragraph(page: &PageContent) -> Option<usize> {
let [column] = page.column_contents.as_slice() else {
return None;
};
match column.items.first()? {
PageItem::FullParagraph { para_index } | PageItem::Table { para_index, .. } => {
Some(*para_index)
}
PageItem::PartialParagraph {
para_index,
start_line: 0,
..
} => Some(*para_index),
_ => None,
}
}
fn continues_previous(paragraphs: &[Paragraph], para_idx: usize) -> Option<(i32, i32)> {
let first = paragraphs.get(para_idx)?.line_segs.first()?;
let previous_end = paragraph_flow_end(paragraphs.get(para_idx.checked_sub(1)?)?)?;
(first.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0 && first.vertical_pos >= previous_end)
.then_some((first.vertical_pos, previous_end))
}
fn shift_source_positions(para: &mut Paragraph, from: usize, delta: i32) {
if let Some(positions) = para.source_line_seg_vertical_pos.as_mut() {
for position in positions.iter_mut().skip(from) {
*position = position.saturating_add(delta);
}
}
}