use crate::model::image::Picture;
use crate::model::paragraph::Paragraph;
use crate::model::provenance::LayoutCompatibilityProfile;
use crate::model::shape::{CaptionDirection, TextWrap, VertRelTo};
pub(crate) fn line_advance_hwp(
paragraph: &Paragraph,
picture: &Picture,
profile: LayoutCompatibilityProfile,
next_paragraph: Option<&Paragraph>,
) -> i32 {
if profile.session_edited()
|| picture.common.treat_as_char
|| picture.caption.is_none()
|| picture.common.text_wrap != TextWrap::TopAndBottom
|| picture.common.vert_rel_to != VertRelTo::Para
|| paragraph.controls.len() != 1
|| paragraph.line_segs.len() != 1
|| paragraph
.text
.chars()
.any(|c| c > '\u{001f}' && c != '\u{fffc}')
{
return 0;
}
let line = ¶graph.line_segs[0];
let advance = line.line_height.saturating_add(line.line_spacing);
if profile.hwp3_native_layout() {
return advance;
}
let Some(next) = next_paragraph else {
return 0;
};
if !profile.hwp5_stored_pagination_layout()
|| !picture.common.flow_with_text
|| !picture.common.allow_overlap
|| !picture
.caption
.as_ref()
.is_some_and(|caption| caption.direction == CaptionDirection::Bottom)
|| paragraph.stored_text_partition_is_dirty()
|| next.stored_text_partition_is_dirty()
|| !next.controls.is_empty()
|| next.line_segs.len() != 1
|| advance <= 0
{
return 0;
}
let successor = &next.line_segs[0];
let synthetic = crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY;
if line.tag & synthetic != 0
|| successor.tag & synthetic != 0
|| line.segment_width == 0
|| successor.line_height <= 0
{
return 0;
}
let origin = |para: &Paragraph| {
para.source_line_seg_vertical_pos
.as_ref()
.and_then(|source| source.first().copied())
.unwrap_or(para.line_segs[0].vertical_pos)
};
if i64::from(origin(next)) - i64::from(origin(paragraph)) == i64::from(advance) {
advance
} else {
0
}
}
pub(crate) fn stored_flow_end_px(
paragraph: &Paragraph,
picture: &Picture,
profile: LayoutCompatibilityProfile,
next_paragraph: Option<&Paragraph>,
paragraph_origin: f64,
dpi: f64,
) -> Option<f64> {
if !profile.hwp5_stored_pagination_layout() {
return None;
}
let host_line = line_advance_hwp(paragraph, picture, profile, next_paragraph);
if host_line <= 0 {
return None;
}
let offset = crate::renderer::float_placement::signed_hwpunit(picture.common.vertical_offset);
Some(
paragraph_origin
+ crate::renderer::hwpunit_to_px(offset, dpi)
+ crate::renderer::hwpunit_to_px(i32::from(picture.common.margin.top), dpi)
+ crate::renderer::composer::captioned_picture_height_px(picture, dpi)
+ crate::renderer::hwpunit_to_px(i32::from(picture.common.margin.bottom), dpi)
+ crate::renderer::hwpunit_to_px(host_line, dpi),
)
}