use crate::model::{
control::Control, document::Document, page::PageDef, paragraph::ColumnBreakType,
};
use crate::parser::FileFormat;
use crate::renderer::{px_to_hwpunit, style_resolver::ResolvedStyleSet};
pub(super) fn activate_page_geometry(document: &mut Document, format: FileFormat) -> Vec<usize> {
if !matches!(format, FileFormat::Hwp | FileFormat::Hwpx)
|| document.layout_profile().hwp3_layout()
{
return Vec::new();
}
let mut activated = Vec::new();
for (index, section) in document.sections.iter_mut().enumerate() {
let controls = || {
section
.paragraphs
.iter()
.flat_map(|paragraph| ¶graph.controls)
};
if !controls().any(|control| matches!(control, Control::SectionDef(_)))
|| controls().any(|control| matches!(control, Control::ColumnDef(_)))
{
continue;
}
let page = PageDef::a4_default();
section.section_def.page_def = page.clone();
for paragraph in &mut section.paragraphs {
for control in &mut paragraph.controls {
if let Control::SectionDef(definition) = control {
definition.page_def = page.clone();
}
}
}
activated.push(index);
}
activated
}
pub(super) fn rebase_body_frames(
document: &mut Document,
activated: &[usize],
styles: &ResolvedStyleSet,
dpi: f64,
) {
for &index in activated {
let mut next = 0_i32;
for (paragraph_index, paragraph) in
document.sections[index].paragraphs.iter_mut().enumerate()
{
if paragraph_index > 0 && paragraph.column_type != ColumnBreakType::None {
next = 0;
}
let style = styles.para_styles.get(paragraph.para_shape_id as usize);
let before = style.map_or(0.0, |style| style.spacing_before);
let after = style.map_or(0.0, |style| style.spacing_after);
if paragraph
.text
.chars()
.any(|ch| !ch.is_whitespace() && !ch.is_control() && ch != '\u{FFFC}')
{
paragraph.stored_text_partition_dirty = true;
}
next = next.saturating_add(px_to_hwpunit(before, dpi));
paragraph.source_line_seg_vertical_pos = None;
paragraph.layout_rebased_body_axis = true;
for line in &mut paragraph.line_segs {
line.vertical_pos = next;
next = next
.saturating_add(line.line_height)
.saturating_add(line.line_spacing);
}
next = next.saturating_add(px_to_hwpunit(after, dpi));
}
}
}