use crate::model::control::Control;
use crate::model::paragraph::Paragraph;
use crate::model::shape::VertAlign;
use crate::model::table::Table;
use super::composer::ComposedParagraph;
use super::float_placement::{is_non_flow_decoration, is_para_topbottom_float, signed_hwpunit};
use super::style_resolver::{ResolvedParaStyle, ResolvedStyleSet};
pub(crate) fn neutral_anchor_metrics(
para: &Paragraph,
styles: &ResolvedStyleSet,
para_style: Option<&ResolvedParaStyle>,
hwp3_legacy_caps: bool,
dpi: f64,
) -> Option<(f64, f64, f64)> {
if super::para_is_unstored_empty_overlay_table_host(para) {
return super::unstored_overlay_table_host_line_metrics(para, styles, para_style);
}
if !para.text.trim().is_empty()
|| para.controls.is_empty()
|| !super::empty_host_controls_are_flow_neutral(para)
|| !super::para_has_no_stored_line_segs(para)
{
return None;
}
let shape = para
.char_shape_id_at(0)
.or_else(|| para.char_shapes.first().map(|shape| shape.char_shape_id))?;
let char_style = styles.char_styles.get(shape as usize)?;
let font_size = char_style.font_size;
if font_size <= 0.0 {
return None;
}
if hwp3_legacy_caps
&& (font_size > super::hwpunit_to_px(1000, dpi) + 0.1
|| (!char_style.bold && font_size < super::hwpunit_to_px(800, dpi) - 0.1))
{
return None;
}
let spacing_type = para_style.map_or(crate::model::style::LineSpacingType::Percent, |style| {
style.line_spacing_type
});
let spacing = para_style.map_or(160.0, |style| style.line_spacing);
let (height, leading) =
super::corrected_line_metrics(0.0, 0.0, font_size, spacing_type, spacing);
Some((height, leading, font_size))
}
pub(crate) fn trailing_advance(
para: &Paragraph,
table: &Table,
composed: Option<&ComposedParagraph>,
styles: &ResolvedStyleSet,
dpi: f64,
) -> Option<f64> {
if !super::para_has_no_stored_line_segs(para)
|| !is_para_topbottom_float(&table.common)
|| !table.common.flow_with_text
|| table.common.vert_align != VertAlign::Top
|| !para.text.is_empty()
|| para.controls.iter().any(|control| {
!matches!(
control,
Control::Table(_) | Control::SectionDef(_) | Control::ColumnDef(_)
) && !is_non_flow_decoration(control)
})
|| para
.controls
.iter()
.filter(|control| matches!(control, Control::Table(_)))
.count()
!= 1
{
return None;
}
let style_id = composed.map_or(para.para_shape_id, |paragraph| paragraph.para_style_id);
let style = styles.para_styles.get(style_id as usize)?;
let owns_line = signed_hwpunit(table.common.vertical_offset) >= 0
&& para.controls.iter().any(is_non_flow_decoration);
if style.indent <= 0.0 && !owns_line {
return Some(0.0);
}
let end_shape = para
.char_shapes
.iter()
.find(|shape| shape.start_pos == para.char_count.saturating_sub(1))
.map(|shape| shape.char_shape_id)
.or_else(|| para.char_shape_id_at(0))
.or_else(|| para.char_shapes.last().map(|shape| shape.char_shape_id))?;
let font_size = styles.char_styles.get(end_shape as usize)?.font_size;
if font_size <= 0.0 {
return None;
}
let (height, leading) = super::corrected_line_metrics(
0.0,
0.0,
font_size,
style.line_spacing_type,
style.line_spacing,
);
let band_top = super::hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi);
Some(if height <= band_top {
0.0
} else {
height + leading
})
}