use crate::model::{
control::Control,
paragraph::{ColumnBreakType, LineSeg, Paragraph},
shape::{CommonObjAttr, HorzRelTo, TextWrap, VertAlign, VertRelTo},
table::Table,
};
use crate::renderer::{hwpunit_to_px, px_to_hwpunit};
use super::{is_para_topbottom_float, signed_hwpunit, ParagraphFloatFlow, ParagraphFloatPlacement};
fn detached_overlay(common: &CommonObjAttr) -> bool {
!common.treat_as_char
&& !common.flow_with_text
&& !common.affect_line_spacing
&& matches!(
common.text_wrap,
TextWrap::InFrontOfText | TextWrap::BehindText
)
&& matches!(common.vert_rel_to, VertRelTo::Paper | VertRelTo::Page)
&& matches!(common.horz_rel_to, HorzRelTo::Paper | HorzRelTo::Page)
}
fn single_flow_table(para: &Paragraph) -> bool {
para.controls
.iter()
.filter(|c| matches!(c, Control::Table(_)))
.count()
== 1
&& para.controls.iter().all(|control| match control {
Control::Table(_) => true,
Control::Shape(shape) => detached_overlay(shape.common()),
Control::Picture(picture) => detached_overlay(&picture.common),
_ => false,
})
}
pub(crate) fn stored_bottom_caption_hu(table: &Table) -> Option<i64> {
if table
.caption
.as_ref()
.is_some_and(|caption| caption.direction != crate::model::shape::CaptionDirection::Bottom)
{
return None;
}
stored_vertical_caption_hu(table)
}
pub(crate) fn stored_vertical_caption_hu(table: &Table) -> Option<i64> {
let Some(caption) = table.caption.as_ref() else {
return Some(0);
};
if !matches!(
caption.direction,
crate::model::shape::CaptionDirection::Top | crate::model::shape::CaptionDirection::Bottom
) || caption.paragraphs.is_empty()
{
return None;
}
let mut height = 0i64;
for para in &caption.paragraphs {
let (first, last) = (para.line_segs.first()?, para.line_segs.last()?);
if para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para
.line_segs
.iter()
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
{
return None;
}
height = height.max(
i64::from(last.vertical_pos) + i64::from(last.line_height)
- i64::from(first.vertical_pos.min(0)),
);
}
Some(i64::from(caption.spacing) + height)
}
fn stored_line(para: &Paragraph) -> Option<i32> {
let line = para.line_segs.first()?;
(line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& !para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty)
.then_some(line.vertical_pos)
}
pub(crate) fn coanchored_host_restarts_page(
previous: &Paragraph,
host: &Paragraph,
body_height: i32,
) -> bool {
if !super::is_textless_coanchored_float_host(host)
|| host.column_type != ColumnBreakType::None
|| host.line_segs.len() != 1
{
return false;
}
let Some(table) = host.controls.iter().find_map(|control| match control {
Control::Table(table) if is_para_topbottom_float(&table.common) => Some(table.as_ref()),
_ => None,
}) else {
return false;
};
let Some(caption) = stored_bottom_caption_hu(table) else {
return false;
};
let leading = i64::from(table.outer_margin_top)
+ i64::from(table.common.height)
+ caption
+ i64::from(table.outer_margin_bottom);
let (Some(host_vpos), Some(_)) = (stored_line(host), stored_line(previous)) else {
return false;
};
let line = &host.line_segs[0];
let Some(last) = previous.line_segs.last() else {
return false;
};
if previous.line_segs.iter().any(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
|| line.line_spacing < 0
}) || i64::from(host_vpos) != leading
{
return false;
}
crate::renderer::stored_frame_boundary::saved_positive_frame_reset(
Some((
last.vertical_pos,
last.vertical_pos
.saturating_add(last.line_height)
.saturating_add(last.line_spacing),
)),
line,
body_height,
)
}
impl ParagraphFloatPlacement {
pub(crate) fn from_stored_overlay_guide_chain(
paragraphs: &[Paragraph],
anchor: usize,
host: usize,
flow: f64,
body_height: f64,
styles: &crate::renderer::style_resolver::ResolvedStyleSet,
dpi: f64,
) -> Option<Self> {
let source = paragraphs.get(anchor)?;
let para = paragraphs.get(host)?;
let [Control::Table(table)] = para.controls.as_slice() else {
return None;
};
let [Control::Table(owner)] = source.controls.as_slice() else {
return None;
};
let overlay = |table: &Table| {
!table.common.treat_as_char
&& matches!(
table.common.text_wrap,
TextWrap::InFrontOfText | TextWrap::BehindText
)
&& table.common.vert_rel_to == VertRelTo::Para
&& table.common.vert_align == VertAlign::Top
};
if anchor >= host
|| !overlay(table)
|| !overlay(owner)
|| !source.text.is_empty()
|| source.line_segs.len() != 1
|| !para.text.is_empty()
|| para.line_segs.len() != 1
|| !flow.is_finite()
|| !body_height.is_finite()
{
return None;
}
stored_line(source)?;
let mut previous = source.line_segs.first()?;
if previous.line_height <= 0 || previous.line_spacing < 0 {
return None;
}
let body = px_to_hwpunit(body_height, dpi);
let mut reset = false;
for index in anchor + 1..=host {
let paragraph = paragraphs.get(index)?;
if !paragraph.text.is_empty()
|| paragraph.line_segs.len() != 1
|| paragraph.column_type != ColumnBreakType::None
|| (index < host && !paragraph.controls.is_empty())
{
return None;
}
stored_line(paragraph)?;
let style = styles.para_styles.get(paragraph.para_shape_id as usize)?;
if style.spacing_before != 0.0 || style.spacing_after != 0.0 {
return None;
}
let line = ¶graph.line_segs[0];
if line.line_height <= 0 || line.line_spacing < 0 {
return None;
}
let ink_end = previous.vertical_pos.checked_add(previous.line_height)?;
let end = ink_end.checked_add(previous.line_spacing)?;
if line.vertical_pos != end {
if reset || line.vertical_pos != 0 || ink_end > body || end <= body {
return None;
}
reset = true;
}
previous = line;
}
let origin = hwpunit_to_px(previous.vertical_pos, dpi);
if !reset
|| origin < flow
|| previous.vertical_pos.checked_add(previous.line_height)? > body
{
return None;
}
Some(Self {
flow: ParagraphFloatFlow::NextLine,
anchor_y: origin,
stored_host_origin: Some(origin),
stored_successor_line_origin: None,
table_left: None,
table_top: origin
+ hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi)
+ hwpunit_to_px(i32::from(table.outer_margin_top), dpi),
occupied_bottom: origin,
})
}
pub(crate) fn from_stored_empty_overlay_host(
previous: &Paragraph,
para: &Paragraph,
table: &Table,
origin: f64,
spacing_before: f64,
page_base_hu: i32,
dpi: f64,
) -> Option<Self> {
let visible = |p: &Paragraph| {
p.text
.chars()
.any(|c| !c.is_whitespace() && !c.is_control() && c != '\u{FFFC}')
};
if !single_flow_table(para)
|| visible(para)
|| !visible(previous)
|| !previous.controls.is_empty()
|| table.common.treat_as_char
|| !table.common.flow_with_text
|| !matches!(
table.common.text_wrap,
TextWrap::InFrontOfText | TextWrap::BehindText
)
|| table.common.vert_rel_to != VertRelTo::Para
|| table.common.vert_align != VertAlign::Top
|| signed_hwpunit(table.common.vertical_offset) < 0
|| table.caption.is_some()
|| para.line_segs.len() != 1
|| previous.stored_text_partition_is_dirty()
|| previous.cell_format_vpos_dirty
|| !origin.is_finite()
|| !spacing_before.is_finite()
|| spacing_before < 0.0
{
return None;
}
let host = stored_line(para)?;
stored_line(previous)?;
let last = previous.line_segs.last()?;
if last.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| last.line_height <= 0
|| last.line_spacing < 0
{
return None;
}
let previous_end = i64::from(last.vertical_pos)
+ i64::from(last.line_height)
+ i64::from(last.line_spacing);
let before_hu = i64::from(px_to_hwpunit(spacing_before, dpi));
let relative_end = previous_end - i64::from(page_base_hu);
if previous_end + before_hu != i64::from(host)
|| (origin * 7200.0 / dpi - relative_end as f64).abs() > 2.0
{
return None;
}
let top = origin
+ (i64::from(signed_hwpunit(table.common.vertical_offset))
+ i64::from(table.outer_margin_top)) as f64
* dpi
/ 7200.0;
Some(Self {
flow: ParagraphFloatFlow::NextLine,
anchor_y: origin,
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: None,
table_top: top,
occupied_bottom: top,
})
}
}
pub(super) fn from_stored_ladder(
para: &Paragraph,
next: &Paragraph,
table: &Table,
origin: f64,
spacing_before: f64,
measured_table_height: f64,
origin_closes_float_frame: bool,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
from_ladder(
para,
next,
table,
origin,
spacing_before,
measured_table_height,
origin_closes_float_frame,
dpi,
false,
)
}
pub(super) fn from_reflowed_ladder(
para: &Paragraph,
next: &Paragraph,
table: &Table,
origin: f64,
spacing_before: f64,
measured_table_height: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
from_ladder(
para,
next,
table,
origin,
spacing_before,
measured_table_height,
false,
dpi,
true,
)
}
fn from_ladder(
para: &Paragraph,
next: &Paragraph,
table: &Table,
origin: f64,
spacing_before: f64,
measured_table_height: f64,
origin_closes_float_frame: bool,
dpi: f64,
reflowed: bool,
) -> Option<ParagraphFloatPlacement> {
let paragraph_square = !table.common.treat_as_char
&& table.common.text_wrap == TextWrap::Square
&& table.common.vert_rel_to == VertRelTo::Para;
if !single_flow_table(para)
|| para
.text
.chars()
.any(|c| !c.is_whitespace() && !c.is_control() && c != '\u{FFFC}')
|| !(is_para_topbottom_float(&table.common) || paragraph_square)
|| next.column_type != ColumnBreakType::None
|| table.common.vert_align != VertAlign::Top
|| signed_hwpunit(table.common.vertical_offset) < 0
|| !origin.is_finite()
|| !spacing_before.is_finite()
|| spacing_before < 0.0
|| !measured_table_height.is_finite()
|| measured_table_height < 0.0
{
return None;
}
let caption_hu = stored_bottom_caption_hu(table)?;
let declared = hwpunit_to_px(
(i64::from(table.common.height.min(i32::MAX as u32)) + caption_hu) as i32,
dpi,
);
if !reflowed && (measured_table_height - declared).abs() > 1.0 {
return None;
}
let host_vpos = stored_line(para)?;
let next_vpos = stored_line(next)?;
let physical_hu = i64::from(signed_hwpunit(table.common.vertical_offset))
+ i64::from(table.outer_margin_top)
+ i64::from(table.common.height.min(i32::MAX as u32))
+ caption_hu
+ i64::from(table.outer_margin_bottom);
let spacing_before_hu = px_to_hwpunit(spacing_before, dpi) as i64;
let delta = i64::from(next_vpos) - i64::from(host_vpos);
if delta <= 0 {
return None;
}
let applied = (delta - (physical_hu - spacing_before_hu)).abs() <= 2;
let absorbed = origin_closes_float_frame && (delta - physical_hu).abs() <= 2;
if !applied && !absorbed {
return None;
}
let table_top = origin
+ (i64::from(signed_hwpunit(table.common.vertical_offset))
+ crate::renderer::stored_float_anchor::stored_outer_box_table_top_hu(table))
as f64
* dpi
/ 7200.0;
let occupied_bottom = origin
+ physical_hu as f64 * dpi / 7200.0
+ if reflowed {
measured_table_height - declared
} else {
0.0
};
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::Exclusion,
anchor_y: origin,
stored_host_origin: None,
stored_successor_line_origin: Some(occupied_bottom),
table_left: None,
table_top,
occupied_bottom,
})
}
impl ParagraphFloatPlacement {
pub(crate) fn from_stored_tail_host(
previous: &Paragraph,
host: &Paragraph,
next: &Paragraph,
table: &Table,
origin: f64,
measured_height: f64,
dpi: f64,
) -> Option<Self> {
if !single_flow_table(host)
|| host
.text
.chars()
.any(|c| !c.is_whitespace() && !c.is_control() && c != '\u{FFFC}')
|| !is_para_topbottom_float(&table.common)
|| table.common.vert_align != VertAlign::Top
|| signed_hwpunit(table.common.vertical_offset) < 0
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
|| table.outer_margin_top < 0
|| table.outer_margin_bottom < 0
|| host.line_segs.len() != 1
|| previous
.controls
.iter()
.any(|control| !crate::renderer::composer::control_is_width_neutral_marker(control))
|| [previous, host, next].iter().any(|para| {
para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.line_segs.is_empty()
|| para.line_segs.iter().any(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
|| line.line_spacing < 0
})
})
|| ![origin, measured_height, dpi]
.iter()
.all(|value| value.is_finite())
|| origin < 0.0
|| dpi <= 0.0
{
return None;
}
let caption = stored_vertical_caption_hu(table)?;
let declared = i64::from(table.common.height) + caption;
if (measured_height - declared as f64 * dpi / 7200.0).abs() > dpi / 7200.0 {
return None;
}
let previous = previous.line_segs.last()?;
let previous_end = i64::from(previous.vertical_pos)
+ i64::from(previous.line_height)
+ i64::from(previous.line_spacing);
let offset = i64::from(signed_hwpunit(table.common.vertical_offset));
let physical = offset
+ i64::from(table.outer_margin_top)
+ declared
+ i64::from(table.outer_margin_bottom);
let line = &host.line_segs[0];
let tail = i64::from(line.line_height) + i64::from(line.line_spacing);
if i64::from(line.vertical_pos) - previous_end != physical
|| i64::from(next.line_segs[0].vertical_pos) - i64::from(line.vertical_pos) != tail
{
return None;
}
Some(Self {
flow: ParagraphFloatFlow::Exclusion,
anchor_y: origin,
stored_host_origin: None,
stored_successor_line_origin: Some(origin + (physical + tail) as f64 * dpi / 7200.0),
table_left: None,
table_top: origin + (offset + i64::from(table.outer_margin_top)) as f64 * dpi / 7200.0,
occupied_bottom: origin + (physical + tail) as f64 * dpi / 7200.0,
})
}
}