use crate::model::{
paragraph::{LineSeg, Paragraph},
provenance::LayoutCompatibilityProfile,
};
use crate::renderer::{
hwpunit_to_px,
pagination::{ColumnContent, PageContent},
style_resolver::ResolvedStyleSet,
};
pub(crate) fn resolve(
paragraphs: &[Paragraph],
styles: &ResolvedStyleSet,
profile: LayoutCompatibilityProfile,
column_count: usize,
dpi: f64,
) -> Option<f64> {
if column_count < 2
|| profile.session_edited()
|| profile.hwp3_layout()
|| profile.hwp3_native_layout()
|| !(profile.hwp5_stored_pagination_layout() || profile.hwpx_stored_layout())
|| !dpi.is_finite()
|| dpi <= 0.0
{
return None;
}
let first = paragraphs.first()?;
let next = paragraphs.get(1)?;
let [line] = first.line_segs.as_slice() else {
return None;
};
let successor = next.line_segs.first()?;
let valid = |p: &Paragraph, line: &LineSeg| {
!p.stored_text_partition_is_dirty()
&& !p.cell_format_vpos_dirty
&& line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& line.tag & LineSeg::TAG_FIRST_SEGMENT != 0
&& line.segment_width > 0
&& line.line_height > 0
&& line.line_spacing >= 0
};
if !valid(first, line)
|| !valid(next, successor)
|| super::has_paragraph_float_graphic(first)
|| super::has_paragraph_float_graphic(next)
|| first.controls.iter().any(control_owns_flow)
|| line.vertical_pos <= 0
|| line.vertical_pos >= line.line_height
{
return None;
}
let first_style = styles.para_styles.get(first.para_shape_id as usize)?;
let next_style = styles.para_styles.get(next.para_shape_id as usize)?;
if first_style.spacing_before != 0.0
|| first_style.spacing_after != 0.0
|| next_style.spacing_before != 0.0
|| i64::from(line.vertical_pos) + i64::from(line.line_height) + i64::from(line.line_spacing)
!= i64::from(successor.vertical_pos)
|| i64::from(line.column_start) + i64::from(line.segment_width)
!= i64::from(successor.column_start) + i64::from(successor.segment_width)
{
return None;
}
Some(hwpunit_to_px(line.vertical_pos, dpi))
}
fn control_owns_flow(control: &crate::model::control::Control) -> bool {
use crate::model::{control::Control, shape::VertRelTo};
let common = match control {
Control::Table(table) => &table.common,
Control::Shape(shape) => shape.common(),
Control::Picture(picture) => &picture.common,
_ => return false,
};
common.treat_as_char
|| common.flow_with_text
|| !common.allow_overlap
|| !matches!(common.vert_rel_to, VertRelTo::Paper | VertRelTo::Page)
}
pub(crate) fn for_paint(
paragraphs: &[Paragraph],
styles: &ResolvedStyleSet,
profile: LayoutCompatibilityProfile,
column_count: usize,
column: &ColumnContent,
page: &PageContent,
dpi: f64,
) -> f64 {
if column.endnote_flow
|| column.zone_y_offset != 0.0
|| !page.column_contents.iter().any(|c| {
!c.endnote_flow
&& c.zone_y_offset == 0.0
&& c.items.iter().any(|item| {
matches!(
item,
crate::renderer::pagination::PageItem::FullParagraph { para_index: 0 }
)
})
})
{
return 0.0;
}
resolve(paragraphs, styles, profile, column_count, dpi).unwrap_or(0.0)
}