mod column_frame;
mod column_ole;
mod computed_host_origin;
mod first_line;
mod nested_wrap_flow;
mod split_host;
mod visible_host_successor;
pub use column_frame::StoredColumnFrame;
pub use first_line::FloatPushedFirstLine;
pub(crate) use nested_wrap_flow::stored_nested_wrap_anchor_height_hu;
pub(crate) use visible_host_successor::shared_spacing as stored_visible_host_successor_spacing;
pub(crate) mod restarted_tac;
mod stored_column_spacing;
mod stored_opening_row_frame;
mod stored_row_grid;
pub(crate) use column_ole::column_ole_placement;
pub(crate) use stored_opening_row_frame::{stored_plain_opening_row_frame, StoredOpeningRowFrame};
mod overlay_outer_frame;
pub(crate) use overlay_outer_frame::overlay_table_outer_margin_left_hu;
pub(crate) use stored_column_spacing::stored_column_top_spacing_before;
pub use stored_column_spacing::StoredColumnTopSpacing;
mod picture_box;
mod stored_cell_flow_picture;
pub(crate) use picture_box::{
paragraph_picture_band, picture_host_follows_cell_content, picture_outer_box,
};
pub(crate) use stored_cell_flow_picture::{
stored_cell_flow_picture_extent_hu, stored_cell_flow_picture_frame,
};
mod reflow_block_cell;
mod reflow_block_lines;
pub(crate) use reflow_block_lines::{
reflow_block_cell_flow_in_width, reflow_block_cell_flow_in_width_with_storage,
reflow_block_cell_flow_with_composed, reflow_block_cell_flow_with_composed_with_storage,
};
mod stored_closed_cell_frame;
pub(crate) use reflow_block_cell::{
has_reflow_block_cell_host, reflow_block_cell_flow, reflow_external_table_flow,
reflow_external_table_flow_with_storage, reflow_host_line_height,
supports_reflow_external_table_frame, ReflowBlockCellFlow, ReflowBlockOrigin,
ReflowExternalTableFlow,
};
pub(crate) mod stored_empty_host_frame;
mod stored_nested_before_host;
mod stored_nested_closing_host;
mod stored_nested_terminal_line;
mod stored_padded_line;
mod stored_row_picture_frame;
pub(crate) use stored_nested_before_host::{
stored_nested_before_host_placement, stored_opening_table_before_host_frame,
stored_table_before_host_frame,
};
pub(crate) use stored_nested_closing_host::stored_nested_host_closes_after_previous_line;
pub(crate) use stored_nested_terminal_line::stored_nested_terminal_host_line_height_hu;
mod stored_cut_outer_frame;
mod stored_header_frame;
pub(crate) use stored_cut_outer_frame::stored_hwpx_cut_owns_outer_frame;
pub(crate) use stored_header_frame::{
stored_independent_table_frame_host, stored_repeated_header_rowbreak_outer_frame,
stored_rowbreak_fragment_opens_outer_top, stored_rowbreak_opening_uses_host_budget,
stored_rowbreak_outer_frame_host, stored_whole_header_next_page_placement,
};
mod stored_span_opening;
pub(crate) use stored_span_opening::{stored_span_opening_frame, StoredSpanOpeningFrame};
mod stored_span_content_frame;
pub(crate) use stored_span_content_frame::stored_continuous_span_prefixes_hu;
mod picture_anchor;
mod square_lane;
pub(crate) use picture_anchor::stored_picture_anchor_delta;
pub(crate) use square_lane::{
square_successor_starts_beside_table, stored_line_lane_probe, StoredLineLaneProbe,
};
mod stored_text_page_frame;
pub(crate) use stored_text_page_frame::{stored_text_page_frame, StoredTextPageFrame};
use std::ops::Range;
use crate::model::control::Control;
use crate::model::image::Picture;
use crate::model::paragraph::Paragraph;
use crate::model::shape::{
CommonObjAttr, HorzAlign, HorzRelTo, TextFlow, TextWrap, VertAlign, VertRelTo,
};
use crate::model::style::Alignment;
use crate::model::table::{Table, TablePageBreak};
use crate::model::HwpUnit;
use super::hwpunit_to_px;
use super::layout::picture_flow_frame_size_hu;
use super::layout_frame::{FrameExclusion, FrameExclusionPolicy, LayoutFrame};
use super::page_layout::LayoutRect;
mod stored_editor_tail_frame;
pub(crate) use stored_editor_tail_frame::{stored_editor_tail_frame, StoredEditorTailFrame};
mod stored_square_top_band;
pub(crate) use stored_square_top_band::{
stored_empty_line_has_closed_source_box, stored_square_top_band, table_reserved_height_hu,
};
pub(crate) fn stored_host_line_end_hu(
para: &Paragraph,
next_para: Option<&Paragraph>,
) -> Option<i32> {
let first = para.line_segs.first()?;
let next = next_para?.line_segs.first()?;
let synthetic = crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY;
let end = first.vertical_pos.saturating_add(first.line_height);
(first.tag & synthetic == 0
&& next.tag & synthetic == 0
&& next.vertical_pos == end.saturating_add(first.line_spacing))
.then_some(end)
}
pub(crate) fn stored_text_cell_required_height(content: f64, declared: f64, padding: f64) -> f64 {
if declared <= 0.0
|| content > declared + 1.5
|| (content + 0.5 < declared && content + padding > declared)
{
content + padding
} else {
content
}
}
pub(crate) fn stored_text_line_cell_minimum(
cell: &crate::model::table::Cell,
content: f64,
padding: f64,
) -> Option<f64> {
if cell.dirty_flag
|| !cell
.paragraphs
.iter()
.any(|paragraph| !paragraph.text.is_empty())
|| cell.paragraphs.iter().any(|paragraph| {
paragraph.stored_text_partition_is_dirty()
|| paragraph.cell_format_vpos_dirty
|| paragraph.controls.iter().any(|control| {
!matches!(control, Control::Equation(equation) if equation.common.treat_as_char)
})
|| paragraph.line_segs.is_empty()
|| paragraph.line_segs.iter().any(|line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
})
})
{
return None;
}
Some(content + padding)
}
pub(crate) fn stored_plain_closed_cell_ink_height(
cell: &crate::model::table::Cell,
table: &Table,
) -> Option<i32> {
let ink_end = stored_plain_cell_ink_height(cell)?;
let padding = cell.effective_vertical_padding_hu(&table.padding);
((i64::from(ink_end) + i64::from(padding) - i64::from(cell.height)).abs() <= 1)
.then_some(ink_end)
.or_else(|| stored_padded_line::closed_content_height_hu(cell, padding))
}
pub(crate) fn stored_plain_cell_ink_height(cell: &crate::model::table::Cell) -> Option<i32> {
use crate::model::paragraph::LineSeg;
if cell.dirty_flag || cell.text_direction != 0 || cell.height >= 0x8000_0000 {
return None;
}
let mut ink_end = 0_i32;
let mut first = true;
for para in &cell.paragraphs {
if !para.controls.is_empty()
|| para.column_type != crate::model::paragraph::ColumnBreakType::None
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.line_segs.is_empty()
{
return None;
}
for line in ¶.line_segs {
if line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
|| line.line_spacing < 0
|| line.vertical_pos < ink_end
|| (first && line.vertical_pos != 0)
{
return None;
}
ink_end = line.vertical_pos.checked_add(line.line_height)?;
first = false;
}
}
(!first).then_some(ink_end)
}
pub(crate) fn stored_complete_column_frame(table: &Table) -> bool {
complete_declared_column_grid(table)
&& table.cells.iter().all(|cell| {
cell.paragraphs
.iter()
.all(|paragraph| !super::para_has_no_stored_line_segs(paragraph))
})
}
pub(crate) fn complete_declared_column_grid(table: &Table) -> bool {
let rows = usize::from(table.row_count);
let cols = usize::from(table.col_count);
if rows == 0 || cols == 0 || table.common.width == 0 {
return false;
}
let mut widths = vec![0_i64; rows];
let mut ownership = vec![vec![false; cols]; rows];
for cell in &table.cells {
let r = usize::from(cell.row);
let c = usize::from(cell.col);
let rs = usize::from(cell.row_span);
let cs = usize::from(cell.col_span);
if rs == 0
|| cs == 0
|| r + rs > rows
|| c + cs > cols
|| cell.dirty_flag
|| cell.paragraphs.is_empty()
|| cell
.paragraphs
.iter()
.any(|p| p.stored_text_partition_is_dirty() || p.cell_format_vpos_dirty)
{
return false;
}
for row in r..r + rs {
if ownership[row][c..c + cs].iter().any(|owned| *owned) {
return false;
}
ownership[row][c..c + cs].fill(true);
widths[row] +=
i64::from(cell.width) - i64::from(table.cell_spacing) * cs.saturating_sub(1) as i64;
}
}
widths.iter().zip(&ownership).all(|(width, owned)| {
owned.iter().all(|owned| *owned)
&& *width + table.inner_cell_spacing_hu(cols) + i64::from(table.outer_cell_spacing_hu())
== i64::from(table.common.width)
})
}
pub(crate) fn stored_table_terminal_suffix_start(
host: &Paragraph,
successor: &Paragraph,
table: &Table,
) -> Option<usize> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
if !host.text.is_empty()
|| !matches!(host.controls.as_slice(), [Control::Table(_)])
|| host.column_type != ColumnBreakType::None
|| successor.column_type != ColumnBreakType::None
|| host.stored_text_partition_is_dirty()
|| successor.stored_text_partition_is_dirty()
|| host.cell_format_vpos_dirty
|| successor.cell_format_vpos_dirty
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| table.common.vert_rel_to != VertRelTo::Para
|| !matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
|| table.layout_page_break() != TablePageBreak::RowBreak
|| !table.repeat_header
|| !stored_complete_column_frame(table)
{
return None;
}
let [anchor] = host.line_segs.as_slice() else {
return None;
};
let next = successor.line_segs.first()?;
if anchor.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| next.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| next.vertical_pos <= 0
|| next.vertical_pos >= anchor.vertical_pos
{
return None;
}
let rows = usize::from(table.row_count);
let headers = table.leading_header_rows();
if headers.is_empty() {
return None;
}
let mut heights = vec![0_i64; rows];
for cell in &table.cells {
if cell.row_span == 1 && cell.height < 0x8000_0000 {
let row = usize::from(cell.row);
heights[row] = heights[row].max(i64::from(cell.height));
}
}
if headers.iter().any(|&row| heights[row] <= 0) {
return None;
}
let spacing = i64::from(table.cell_spacing);
let repeated = headers
.iter()
.map(|&row| heights[row] + spacing)
.sum::<i64>();
let mut suffix =
repeated + i64::from(table.outer_margin_top) + i64::from(table.outer_margin_bottom);
for row in (headers.len()..rows).rev() {
if heights[row] <= 0 {
return None;
}
suffix += heights[row];
if row + 1 < rows {
suffix += spacing;
}
if suffix == i64::from(next.vertical_pos) {
return Some(row);
}
if suffix > i64::from(next.vertical_pos) {
break;
}
}
None
}
mod empty_successor;
pub(crate) use empty_successor::{stored_empty_successor_advance, stored_empty_successor_geometry};
pub(crate) fn stored_inline_picture_successor_advance(
current: &Paragraph,
next: &Paragraph,
styles: &super::style_resolver::ResolvedStyleSet,
dpi: f64,
) -> Option<f64> {
use crate::model::paragraph::LineSeg;
let [Control::Picture(picture)] = current.controls.as_slice() else {
return None;
};
let [line] = current.line_segs.as_slice() else {
return None;
};
let successor = next.line_segs.first()?;
let height = i64::from(picture.common.height)
+ i64::from(picture.common.margin.top)
+ i64::from(picture.common.margin.bottom);
if !picture.common.treat_as_char
|| picture.caption.is_some()
|| !current.text.trim().is_empty()
|| !next.controls.is_empty()
|| current.stored_text_partition_is_dirty()
|| next.stored_text_partition_is_dirty()
|| current.cell_format_vpos_dirty
|| next.cell_format_vpos_dirty
|| (line.tag | successor.tag) & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_spacing >= 0
|| i64::from(line.line_height) != height
{
return None;
}
let after = styles
.para_styles
.get(current.para_shape_id as usize)?
.spacing_after;
let before = styles
.para_styles
.get(next.para_shape_id as usize)?
.spacing_before;
let advance = i64::from(line.line_height) + i64::from(line.line_spacing);
let delta = i64::from(successor.vertical_pos) - i64::from(line.vertical_pos);
(advance >= 0 && delta == advance + i64::from(super::px_to_hwpunit(after + before, dpi)))
.then_some(hwpunit_to_px(advance as i32, dpi) + after)
}
pub(crate) fn stored_complete_row_frame_heights(table: &Table) -> Option<Vec<i32>> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
let row_count = usize::from(table.row_count);
if row_count < 2 || table.common.height > i32::MAX as u32 {
return None;
}
let mut rows = vec![0_i32; row_count];
let mut constrained_rows = vec![0_i32; row_count];
let mut declarations_valid = true;
let mut nonnegative_spacing = true;
let mut columns = vec![0_usize; row_count];
for cell in &table.cells {
let row = usize::from(cell.row);
if row >= row_count
|| cell.row_span != 1
|| cell.dirty_flag
|| cell.text_direction != 0
|| cell.paragraphs.is_empty()
{
return None;
}
let padding = cell.effective_padding(&table.padding);
if padding.top < 0 || padding.bottom < 0 {
return None;
}
let mut last_end = None;
for para in &cell.paragraphs {
let contents_valid = if para.controls.is_empty() {
para.column_type == ColumnBreakType::None
} else {
stored_row_picture_frame::controls_preserve_stored_picture_lines(para)
};
if !contents_valid
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| super::para_has_no_stored_line_segs(para)
{
return None;
}
for line in ¶.line_segs {
if line.vertical_pos < 0
|| line.line_height <= 0
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| last_end.is_some_and(|end| line.vertical_pos < end)
{
return None;
}
nonnegative_spacing &= line.line_spacing >= 0;
last_end = Some(line.vertical_pos.checked_add(line.line_height)?);
}
}
let physical_height = last_end?
.checked_add(i32::from(padding.top))?
.checked_add(i32::from(padding.bottom))?;
rows[row] = rows[row].max(physical_height);
if let Ok(declared) = i32::try_from(cell.height) {
constrained_rows[row] = constrained_rows[row].max(physical_height).max(declared);
} else {
declarations_valid = false;
}
columns[row] = columns[row].checked_add(usize::from(cell.col_span))?;
}
if rows.iter().any(|height| *height <= 0)
|| columns
.iter()
.any(|count| *count != usize::from(table.col_count))
{
return None;
}
let total = rows.iter().map(|height| i64::from(*height)).sum::<i64>()
+ table.inner_cell_spacing_hu(row_count)
+ i64::from(table.outer_cell_spacing_hu());
if nonnegative_spacing && total == i64::from(table.common.height) {
return Some(rows);
}
let constrained_total = constrained_rows
.iter()
.map(|height| i64::from(*height))
.sum::<i64>()
+ table.inner_cell_spacing_hu(row_count)
+ i64::from(table.outer_cell_spacing_hu());
(declarations_valid && constrained_total == i64::from(table.common.height))
.then_some(constrained_rows)
}
pub(crate) fn stored_plain_object_row_frame_heights(table: &Table) -> Option<Vec<i32>> {
let row_count = usize::from(table.row_count);
if row_count < 2
|| table.common.treat_as_char
|| table.layout_page_break() != TablePageBreak::RowBreak
|| !is_para_topbottom_float(&table.common)
|| table.common.height > i32::MAX as u32
|| table.cells.iter().any(|cell| cell.row_span != 1)
|| !stored_complete_column_frame(table)
{
return None;
}
let mut rows = table
.get_raw_row_heights()
.into_iter()
.map(|height| i32::try_from(height).ok())
.collect::<Option<Vec<_>>>()?;
if rows.len() != row_count || rows.iter().any(|height| *height <= 0) {
return None;
}
let prefix = rows[..row_count - 1]
.iter()
.map(|height| i64::from(*height))
.sum::<i64>()
+ table.inner_cell_spacing_hu(row_count)
+ i64::from(table.outer_cell_spacing_hu());
let closing = i32::try_from(i64::from(table.common.height) - prefix).ok()?;
if closing <= 0 || closing >= rows[row_count - 1] {
return None;
}
rows[row_count - 1] = closing;
for cell in &table.cells {
let ink = stored_plain_cell_ink_height(cell)?;
let required = ink.checked_add(cell.effective_vertical_padding_hu(&table.padding))?;
if required > *rows.get(usize::from(cell.row))? {
return None;
}
}
Some(rows)
}
pub(crate) fn stored_coincident_empty_cell_height(
cell: &crate::model::table::Cell,
table: &Table,
) -> Option<i32> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
if cell.row_span != 1 || cell.dirty_flag || cell.text_direction != 0 {
return None;
}
let initial = cell.paragraphs.first()?;
let [first] = initial.line_segs.as_slice() else {
return None;
};
let next = cell.paragraphs.get(1)?;
if !initial.text.trim().is_empty()
|| !initial.controls.is_empty()
|| next.text.trim().is_empty()
|| first.vertical_pos != 0
|| next.line_segs.first()?.vertical_pos != 0
{
return None;
}
let mut previous = None;
let mut end = 0_i32;
for (index, para) in cell.paragraphs.iter().enumerate() {
if !para.controls.is_empty()
|| para.column_type != ColumnBreakType::None
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.line_segs.is_empty()
{
return None;
}
for (li, line) in para.line_segs.iter().enumerate() {
if line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos < 0
|| line.line_height <= 0
|| line.line_spacing < 0
|| previous.is_some_and(|origin| {
line.vertical_pos < origin
|| (line.vertical_pos == origin && !(index == 1 && li == 0))
})
{
return None;
}
end = end.max(line.vertical_pos.checked_add(line.line_height)?);
previous = Some(line.vertical_pos);
}
}
let padding = cell.effective_padding(&table.padding);
let physical = end
.checked_add(i32::from(padding.top))?
.checked_add(i32::from(padding.bottom))?;
let duplicate = first.line_height.checked_add(first.line_spacing)?;
(duplicate > 0 && i64::from(physical) + i64::from(duplicate) == i64::from(cell.height))
.then_some(physical)
}
pub(crate) fn native_empty_topbottom_rewind_anchor_saved_para_y(
native_hwp5_layout: bool,
prev_para: Option<&Paragraph>,
para: &Paragraph,
next_para: Option<&Paragraph>,
table: &crate::model::table::Table,
flow_para_y: f64,
col_area: &LayoutRect,
dpi: f64,
) -> Option<f64> {
const MAX_REWIND_DRIFT_PX: f64 = 24.0;
if !native_hwp5_layout
|| para.text.chars().any(|c| c > '\u{001F}' && c != '\u{FFFC}')
|| !is_para_topbottom_float(&table.common)
|| !matches!(
table.common.vert_rel_to,
crate::model::shape::VertRelTo::Para
)
|| para
.controls
.iter()
.filter(|control| matches!(control, Control::Table(_)))
.count()
!= 1
{
return None;
}
let stored_vpos = |paragraph: &Paragraph| {
paragraph
.line_segs
.iter()
.rev()
.find(|seg| {
seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
.map(|seg| seg.vertical_pos)
};
let host_vpos = stored_vpos(para)?;
let prev_vpos = stored_vpos(prev_para?)?;
let next_vpos = stored_vpos(next_para?)?;
if host_vpos <= 0 || host_vpos >= prev_vpos || next_vpos <= host_vpos {
return None;
}
let saved_para_y = col_area.y + hwpunit_to_px(host_vpos, dpi);
let drift = flow_para_y - saved_para_y;
if !(0.0 < drift && drift <= MAX_REWIND_DRIFT_PX) {
return None;
}
let v_off = hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi).max(0.0);
let bottom = saved_para_y + v_off + hwpunit_to_px(table.common.height as i32, dpi);
(bottom <= col_area.y + col_area.height + 0.5).then_some(saved_para_y)
}
pub(crate) fn stored_relocated_measured_table_successor_origin(
stored_layout: bool,
host: &Paragraph,
table: &Table,
successor: Option<&Paragraph>,
measured_height: f64,
dpi: f64,
) -> Option<f64> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
let next = successor?;
let [anchor] = host.line_segs.as_slice() else {
return None;
};
let line = next.line_segs.first()?;
if !stored_layout
|| !host.text.trim().is_empty()
|| !matches!(host.controls.as_slice(), [Control::Table(_)])
|| host.stored_text_partition_is_dirty()
|| host.cell_format_vpos_dirty
|| next.stored_text_partition_is_dirty()
|| next.cell_format_vpos_dirty
|| next.column_type != ColumnBreakType::None
|| anchor.line_height <= 0
|| line.line_height <= 0
|| anchor.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| table.cells.iter().any(|cell| cell.dirty_flag)
|| !is_para_topbottom_float(&table.common)
|| !table.common.flow_with_text
|| table.common.allow_overlap
|| table.caption.is_some()
|| table.layout_page_break() != TablePageBreak::RowBreak
|| !measured_height.is_finite()
|| measured_height <= 0.0
|| !dpi.is_finite()
|| dpi <= 0.0
{
return None;
}
let host_vpos = ladder_vpos(host, 0, anchor.vertical_pos);
let next_vpos = ladder_vpos(next, 0, line.vertical_pos);
let extent = (measured_height * 7200.0 / dpi).round() as i64
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom);
(next_vpos > 0 && host_vpos > next_vpos && extent == i64::from(next_vpos))
.then(|| hwpunit_to_px(next_vpos, dpi))
}
pub(crate) fn stored_relocated_picture_successor_origin(
stored_layout: bool,
host_para: &Paragraph,
table: &crate::model::table::Table,
next_para: Option<&Paragraph>,
dpi: f64,
) -> Option<f64> {
if !stored_layout
|| host_para.stored_text_partition_is_dirty()
|| host_para.cell_format_vpos_dirty
|| host_para
.text
.chars()
.any(|c| c > '\u{001F}' && c != '\u{FFFC}')
|| host_para.controls.len() != 1
|| table.row_count != 1
|| table.col_count != 1
|| table.cells.len() != 1
|| !matches!(table.layout_page_break(), TablePageBreak::RowBreak)
|| !is_para_topbottom_float(&table.common)
|| !matches!(table.common.vert_rel_to, VertRelTo::Para)
{
return None;
}
let host_seg = host_para
.line_segs
.iter()
.find(|seg| seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0)?;
let next_para = next_para?;
if next_para.stored_text_partition_is_dirty() || next_para.cell_format_vpos_dirty {
return None;
}
let next_seg = next_para
.line_segs
.iter()
.find(|seg| seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0)?;
if host_seg.vertical_pos <= 0
|| next_seg.vertical_pos <= 0
|| next_seg.vertical_pos >= host_seg.vertical_pos
{
return None;
}
let cell = table.cells.first()?;
if cell.row != 0
|| cell.col != 0
|| cell.row_span != 1
|| cell.col_span != 1
|| cell.height <= table.common.height
|| cell.paragraphs.len() != 1
{
return None;
}
let cell_para = cell.paragraphs.first()?;
let cell_seg = cell_para
.line_segs
.iter()
.find(|seg| seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0)?;
let Control::Picture(picture) = cell_para.controls.first()? else {
return None;
};
if !cell_para.text.trim().is_empty()
|| cell_para.controls.len() != 1
|| cell_para.line_segs.len() != 1
|| cell_seg.vertical_pos != 0
|| picture.common.treat_as_char
|| !picture.common.flow_with_text
|| !matches!(picture.common.text_wrap, TextWrap::TopAndBottom)
|| !matches!(picture.common.vert_rel_to, VertRelTo::Para)
|| !picture.caption.as_ref().is_some_and(|caption| {
matches!(
caption.direction,
crate::model::shape::CaptionDirection::Bottom
) && !caption.paragraphs.is_empty()
})
{
return None;
}
let boundary_sum = i64::from(host_seg.vertical_pos)
+ i64::from(signed_hwpunit(table.common.vertical_offset))
+ i64::from(signed_hwpunit(picture.common.vertical_offset));
if boundary_sum.abs() > 8 {
return None;
}
Some(hwpunit_to_px(next_seg.vertical_pos, dpi))
}
pub(crate) fn cell_paragraph_has_mixed_object_frame(para: &Paragraph) -> bool {
if !para.text.trim().is_empty() || para.text.contains(['\r', '\n']) {
return false;
}
let mut has_flow_picture = false;
let mut has_overlay_shape = false;
for control in ¶.controls {
let common = match control {
Control::Picture(picture) => {
has_flow_picture |= picture.common.flow_with_text
&& picture.common.text_wrap == TextWrap::TopAndBottom;
&picture.common
}
Control::Shape(shape) => {
has_overlay_shape |= matches!(
shape.common().text_wrap,
TextWrap::InFrontOfText | TextWrap::BehindText
);
shape.common()
}
_ => return false,
};
if common.treat_as_char
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
{
return false;
}
}
has_flow_picture && has_overlay_shape
}
pub(crate) fn parallel_cell_float_band_height(controls: &[Control], dpi: f64) -> Option<f64> {
if controls.len() < 2 {
return None;
}
let mut latest_start = f64::NEG_INFINITY;
let mut earliest_end = f64::INFINITY;
let mut bottom = 0.0f64;
for control in controls {
let common = match control {
Control::Picture(picture) => &picture.common,
Control::Shape(shape) => shape.common(),
_ => return None,
};
if common.treat_as_char
|| !matches!(
common.text_wrap,
TextWrap::Square | TextWrap::Tight | TextWrap::Through
)
|| common.vert_rel_to != VertRelTo::Para
{
return None;
}
let start = hwpunit_to_px(signed_hwpunit(common.vertical_offset), dpi);
let height = hwpunit_to_px(signed_hwpunit(common.height), dpi)
+ hwpunit_to_px(i32::from(common.margin.top), dpi)
+ hwpunit_to_px(i32::from(common.margin.bottom), dpi);
if height <= 0.5 {
return None;
}
latest_start = latest_start.max(start);
earliest_end = earliest_end.min(start + height);
bottom = bottom.max(start.max(0.0) + height);
}
(latest_start + 0.5 < earliest_end).then_some(bottom)
}
fn topbottom_picture_band_height(picture: &Picture, dpi: f64) -> Option<f64> {
use crate::model::shape::CaptionDirection;
let common = &picture.common;
if common.treat_as_char
|| !common.flow_with_text
|| common.text_wrap != TextWrap::TopAndBottom
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| picture
.caption
.as_ref()
.is_some_and(|caption| caption.direction != CaptionDirection::Bottom)
{
return None;
}
let (_, height) = picture_flow_frame_size_hu(picture);
if height <= 0 {
return None;
}
let caption = picture.caption.as_ref().map_or(0.0, |caption| {
super::composer::caption_height_px(&picture.caption, dpi)
+ hwpunit_to_px(caption.spacing as i32, dpi)
});
Some(
hwpunit_to_px(height, dpi)
+ hwpunit_to_px(
i32::from(common.margin.top) + i32::from(common.margin.bottom),
dpi,
)
+ caption,
)
}
pub(crate) fn reflow_picture_host_advance(para: &Paragraph, dpi: f64) -> Option<f64> {
if !para.text.trim().is_empty() || para.line_segs.len() != 1 || para.controls.is_empty() {
return None;
}
let mut advance = 0.0f64;
for control in ¶.controls {
let Control::Picture(picture) = control else {
return None;
};
let offset = signed_hwpunit(picture.common.vertical_offset);
if offset < 0 {
return None;
}
advance =
advance.max(hwpunit_to_px(offset, dpi) + topbottom_picture_band_height(picture, dpi)?);
}
Some(advance)
}
pub(crate) fn deferred_picture_band_height(para: &Paragraph, dpi: f64) -> Option<f64> {
use crate::model::paragraph::LineSeg;
let [line] = para.line_segs.as_slice() else {
return None;
};
if !para.text.trim().is_empty()
|| para.text.contains(['\r', '\n'])
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| line.line_height <= 0
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| para.controls.is_empty()
{
return None;
}
let mut band = 0.0_f64;
for control in ¶.controls {
let Control::Picture(picture) = control else {
return None;
};
band = band.max(topbottom_picture_band_height(picture, dpi)?);
}
Some(band)
}
pub(crate) fn stored_inline_table_successor_advance(
host: &Paragraph,
next: &Paragraph,
styles: &crate::renderer::style_resolver::ResolvedStyleSet,
dpi: f64,
) -> Option<f64> {
if !matches!(host.controls.as_slice(), [Control::Table(_)]) {
return None;
}
stored_tac_line_successor_advance(host, 0, next, styles, dpi)
}
pub(crate) fn stored_tac_line_successor_advance(
host: &Paragraph,
control_index: usize,
next: &Paragraph,
styles: &crate::renderer::style_resolver::ResolvedStyleSet,
dpi: f64,
) -> Option<f64> {
use crate::model::paragraph::LineSeg;
let Control::Table(table) = host.controls.get(control_index)? else {
return None;
};
let [line] = host.line_segs.as_slice() else {
return None;
};
if host.controls.iter().enumerate().any(|(index, control)| {
index != control_index
&& match control {
Control::Table(other) => {
other.common.treat_as_char || is_para_topbottom_float(&other.common)
}
Control::Picture(picture) => {
picture.common.treat_as_char || is_para_topbottom_float(&picture.common)
}
Control::Shape(shape) => {
shape.common().treat_as_char || is_para_topbottom_float(shape.common())
}
Control::Equation(_) => true,
_ => false,
}
}) {
return None;
}
let successor = next.line_segs.first()?;
if !table.common.treat_as_char
|| table.common.height == 0
|| host
.text
.chars()
.any(|ch| !ch.is_whitespace() && !ch.is_control() && ch != '\u{FFFC}')
|| host.stored_text_partition_is_dirty()
|| next.stored_text_partition_is_dirty()
|| host.cell_format_vpos_dirty
|| next.cell_format_vpos_dirty
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| successor.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
|| line.line_spacing < 0
|| i64::from(line.line_height)
!= i64::from(table.common.height)
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom)
{
return None;
}
let after = styles
.para_styles
.get(host.para_shape_id as usize)?
.spacing_after;
let before = styles
.para_styles
.get(next.para_shape_id as usize)?
.spacing_before;
let advance = line.line_height.checked_add(line.line_spacing)?;
let delta = crate::renderer::composer::stored_tac_successor_delta_hu(
host,
line,
successor,
super::px_to_hwpunit(after + before, dpi),
)?;
(delta == advance.checked_add(super::px_to_hwpunit(after + before, dpi))?)
.then(|| hwpunit_to_px(advance, dpi) + after)
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct CellNoOverlapSquareStack {
pub shifts: Vec<(usize, f64)>,
pub bottom: f64,
pub members: Vec<usize>,
}
#[derive(Debug, Default)]
pub(crate) struct NoOverlapSquarePlacer {
placed: Vec<(f64, f64, f64, f64)>,
}
impl NoOverlapSquarePlacer {
pub(crate) fn place(
&mut self,
stack: Option<&CellNoOverlapSquareStack>,
control_idx: usize,
common: &CommonObjAttr,
x: f64,
y: f64,
dpi: f64,
) -> f64 {
if !stack.is_some_and(|stack| stack.members.contains(&control_idx)) {
return y;
}
const EPS: f64 = 0.01;
let px = |hu: i64| hwpunit_to_px(hu as i32, dpi);
let outer_w = px(i64::from(signed_hwpunit(common.width))
+ i64::from(common.margin.left)
+ i64::from(common.margin.right));
let outer_h = px(i64::from(signed_hwpunit(common.height))
+ i64::from(common.margin.top)
+ i64::from(common.margin.bottom));
let (x0, x1) = (x, x + outer_w);
let mut y0 = y;
while let Some(lower) = self
.placed
.iter()
.filter(|&&(px0, px1, py0, py1)| {
x0 < px1 - EPS && px0 < x1 - EPS && y0 < py1 - EPS && py0 < y0 + outer_h - EPS
})
.map(|&(_, _, _, py1)| py1)
.reduce(f64::max)
{
y0 = lower;
}
self.placed.push((x0, x1, y0, y0 + outer_h));
y0
}
}
pub(crate) fn cell_paragraph_no_overlap_square_stack(
paragraphs: &[Paragraph],
para_idx: usize,
dpi: f64,
) -> Option<CellNoOverlapSquareStack> {
let host = paragraphs.get(para_idx)?;
if !host.text.trim().is_empty() {
return None;
}
cell_no_overlap_square_stack(&host.controls, dpi)
}
pub(crate) fn cell_no_overlap_square_stack(
controls: &[Control],
dpi: f64,
) -> Option<CellNoOverlapSquareStack> {
struct StackBox {
control_idx: usize,
x0: i64,
x1: i64,
y: i64,
outer_h: i64,
visual_h: i64,
}
let mut boxes: Vec<StackBox> = Vec::new();
let mut horz_rel_to = None;
for (control_idx, control) in controls.iter().enumerate() {
let Control::Picture(picture) = control else {
continue;
};
let common = &picture.common;
if common.treat_as_char
|| common.allow_overlap
|| !matches!(common.text_wrap, TextWrap::Square)
{
continue;
}
if !matches!(common.vert_rel_to, VertRelTo::Para)
|| !matches!(common.vert_align, VertAlign::Top)
|| !matches!(common.horz_rel_to, HorzRelTo::Para | HorzRelTo::Column)
|| !matches!(common.horz_align, HorzAlign::Left)
|| *horz_rel_to.get_or_insert(common.horz_rel_to) != common.horz_rel_to
{
return None;
}
let x0 = i64::from(signed_hwpunit(common.horizontal_offset));
let outer_w = i64::from(signed_hwpunit(common.width))
+ i64::from(common.margin.left)
+ i64::from(common.margin.right);
let visual_h = i64::from(signed_hwpunit(common.height));
let outer_h = visual_h + i64::from(common.margin.top) + i64::from(common.margin.bottom);
if outer_w <= 0 || visual_h <= 0 {
return None;
}
boxes.push(StackBox {
control_idx,
x0,
x1: x0 + outer_w,
y: i64::from(signed_hwpunit(common.vertical_offset).max(0)),
outer_h,
visual_h: visual_h + i64::from(common.margin.top),
});
}
if boxes.len() < 2 {
return None;
}
let mut shifts = Vec::new();
let mut bottom = 0i64;
for index in 0..boxes.len() {
let (placed, rest) = boxes.split_at_mut(index);
let current = &mut rest[0];
let original_y = current.y;
while let Some(lower) = placed
.iter()
.filter(|prev| {
current.x0 < prev.x1
&& prev.x0 < current.x1
&& current.y < prev.y + prev.outer_h
&& prev.y < current.y + current.outer_h
})
.map(|prev| prev.y + prev.outer_h)
.max()
{
current.y = lower;
}
if current.y > original_y {
shifts.push((
current.control_idx,
hwpunit_to_px((current.y - original_y) as i32, dpi),
));
}
bottom = bottom.max(current.y + current.visual_h);
}
(!shifts.is_empty()).then(|| CellNoOverlapSquareStack {
members: boxes.iter().map(|b| b.control_idx).collect(),
shifts,
bottom: hwpunit_to_px(bottom as i32, dpi),
})
}
pub(crate) fn parallel_cell_picture_band_height(para: &Paragraph, dpi: f64) -> Option<f64> {
if !para.text.trim().is_empty()
|| para.text.contains(['\r', '\n'])
|| para.line_segs.len() > 1
|| !para.controls.iter().all(|control| {
matches!(control,
Control::Picture(picture) if picture.common.flow_with_text
&& picture.common.text_wrap == TextWrap::Square
&& picture.common.vert_align == VertAlign::Top
)
})
{
return None;
}
parallel_cell_float_band_height(¶.controls, dpi)
}
pub(crate) fn reflow_block_table_host_occupied_height(
para: &Paragraph,
line_height: f64,
table_height: f64,
) -> Option<f64> {
if !crate::renderer::para_has_no_stored_line_segs(para) || !para.text.is_empty() {
return None;
}
let mut has_table = false;
for control in ¶.controls {
match control {
Control::Table(table)
if !table.common.treat_as_char
&& table.common.flow_with_text
&& table.common.text_wrap == TextWrap::TopAndBottom
&& table.common.vert_rel_to == VertRelTo::Para
&& table.common.vert_align == VertAlign::Top
&& signed_hwpunit(table.common.vertical_offset) == 0 =>
{
has_table = true;
}
Control::ColumnDef(_) => {}
_ => return None,
}
}
has_table.then_some(line_height.max(table_height))
}
pub(crate) fn block_table_trailing_whitespace_line_hu(
para: &Paragraph,
table: &Table,
control_index: usize,
) -> Option<i32> {
if para.text.is_empty()
|| !para.text.chars().all(char::is_whitespace)
|| para.controls.len() != 1
|| !table.common.flow_with_text
|| !is_para_topbottom_float(&table.common)
|| table.common.vert_align != VertAlign::Top
|| signed_hwpunit(table.common.vertical_offset) != 0
|| para.control_text_positions().get(control_index).copied()? >= para.text.chars().count()
|| para.line_segs.len() != 1
{
return None;
}
let line = ¶.line_segs[0];
let advance = line.line_height.checked_add(line.line_spacing)?;
(line.line_height > 0
&& advance > 0
&& line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0)
.then_some(advance)
}
pub(crate) fn whitespace_follows_block_table(para: &Paragraph) -> bool {
let [Control::Table(table)] = para.controls.as_slice() else {
return false;
};
!para.text.is_empty()
&& para.text.chars().all(char::is_whitespace)
&& !para.text.contains(['\r', '\n'])
&& para.char_offsets.len() == para.text.chars().count()
&& para.control_text_positions().first() == Some(&0)
&& is_para_topbottom_float(&table.common)
&& table.common.flow_with_text
&& table.common.vert_align == VertAlign::Top
&& table.common.vertical_offset == 0
&& table.caption.is_none()
}
pub(crate) fn unrestricted_cell_float(common: &CommonObjAttr) -> bool {
!common.treat_as_char
&& !common.flow_with_text
&& common.text_wrap == TextWrap::TopAndBottom
&& common.vert_rel_to == VertRelTo::Para
}
pub(crate) fn partial_table_defers_visible_host(
para: &Paragraph,
table: &Table,
control_index: usize,
) -> bool {
!table.common.treat_as_char
&& is_para_topbottom_float(&table.common)
&& table.layout_page_break() == TablePageBreak::RowBreak
&& para_has_non_whitespace_text(para)
&& !super::layout::stored_host_lines_precede_float(para, table, control_index)
}
pub(crate) fn partial_table_opening_host_owns_lead(
para: &Paragraph,
table: &Table,
control_index: usize,
host_was_emitted: bool,
) -> bool {
!host_was_emitted
&& !table.common.treat_as_char
&& !partial_table_defers_visible_host(para, table, control_index)
&& para.text.chars().any(|c| c > '\u{001f}' && c != '\u{fffc}')
}
pub(crate) fn reflow_table_uses_row_height_constraints(table: &Table) -> bool {
!table.common.treat_as_char
&& !table.common.size_protect
&& table.raw_table_record_attr & 0x08 == 0
&& table.row_count > 0
&& !table.cells.is_empty()
&& table.cells.iter().all(|cell| {
!cell.paragraphs.is_empty()
&& cell
.paragraphs
.iter()
.all(crate::renderer::para_has_no_stored_line_segs)
})
}
pub(crate) fn table_body_height_with_declared_floor(table: &Table, measured: f64, dpi: f64) -> f64 {
if reflow_table_uses_row_height_constraints(table) {
measured
} else {
measured.max(crate::renderer::hwpunit_to_px(
table.common.height as i32,
dpi,
))
}
}
pub(crate) fn topbottom_flow_vertical_offset_hu(common: &CommonObjAttr) -> i32 {
signed_hwpunit(common.vertical_offset).max(0)
}
pub(crate) fn captioned_table_flow_vertical_offset_hu(table: &Table) -> i32 {
let common = &table.common;
if !common.treat_as_char
&& common.flow_with_text
&& !common.allow_overlap
&& common.text_wrap == TextWrap::TopAndBottom
&& common.vert_rel_to == VertRelTo::Para
&& common.vert_align == VertAlign::Top
&& table
.caption
.as_ref()
.is_some_and(|caption| caption.direction == crate::model::shape::CaptionDirection::Top)
{
topbottom_flow_vertical_offset_hu(common)
} else {
signed_hwpunit(common.vertical_offset)
}
}
#[derive(Debug, Clone)]
pub(crate) struct StoredPartitionedPictureCellFlow {
pub controls: Vec<(usize, i32)>,
pub opening_count: usize,
pub opening_slack_hu: i32,
pub closing_line_height_hu: i32,
}
impl StoredPartitionedPictureCellFlow {
pub fn content_height_hu(&self) -> i32 {
self.controls.iter().map(|(_, height)| height).sum::<i32>()
+ self.opening_slack_hu
+ self.closing_line_height_hu
}
}
pub(crate) fn stored_partitioned_picture_cell_flow(
cell: &crate::model::table::Cell,
table: &Table,
) -> Option<StoredPartitionedPictureCellFlow> {
use crate::model::paragraph::LineSeg;
if table.common.treat_as_char
|| table.layout_page_break() == TablePageBreak::None
|| table.cell_spacing != 0
|| table.col_count != 1
|| cell.row_span != 1
|| cell.row + 1 != table.row_count
|| cell.dirty_flag
|| cell.text_direction != 0
{
return None;
}
let [para] = cell.paragraphs.as_slice() else {
return None;
};
let [line] = para.line_segs.as_slice() else {
return None;
};
if !para.text.is_empty()
|| para.empty_control_stream_position(0).is_none()
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.column_type != crate::model::paragraph::ColumnBreakType::None
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos <= 0
|| line.line_height <= 0
|| line.segment_width <= 0
|| para.controls.len() < 2
{
return None;
}
let mut pictures = Vec::new();
let mut intersection_left = i32::MIN;
let mut intersection_right = i32::MAX;
let mut total = 0_i32;
for (index, control) in para.controls.iter().enumerate() {
let Control::Picture(picture) = control else {
return None;
};
let common = &picture.common;
let height = signed_hwpunit(common.height);
let width = signed_hwpunit(common.width);
if common.treat_as_char
|| !common.flow_with_text
|| common.allow_overlap
|| common.text_wrap != TextWrap::TopAndBottom
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| common.horz_rel_to != HorzRelTo::Column
|| common.horz_align != HorzAlign::Left
|| signed_hwpunit(common.vertical_offset) > 0
|| common.margin.top != 0
|| common.margin.bottom != 0
|| picture.caption.is_some()
|| picture.shape_attr.rotation_angle.rem_euclid(360) != 0
|| height <= 0
|| width <= 0
{
return None;
}
let x = signed_hwpunit(common.horizontal_offset);
intersection_left = intersection_left.max(x);
intersection_right = intersection_right.min(x.checked_add(width)?);
total = total.checked_add(height)?;
pictures.push((index, height));
}
if intersection_left >= intersection_right {
return None;
}
let consumed = total.checked_sub(line.vertical_pos)?;
if consumed <= 0 {
return None;
}
let previous_rows: i32 = table
.cells
.iter()
.filter(|other| other.row < cell.row)
.try_fold(0_i32, |sum, other| {
(other.row_span == 1 && signed_hwpunit(other.height) > 0)
.then(|| sum.checked_add(signed_hwpunit(other.height)))
.flatten()
})?;
let opening_content = signed_hwpunit(table.common.height)
.checked_sub(previous_rows)?
.checked_sub(cell.effective_vertical_padding_hu(&table.padding))?;
if opening_content < consumed {
return None;
}
let mut remaining = opening_content;
let mut opening = Vec::new();
let mut deferred = Vec::new();
for picture in pictures {
if picture.1 <= remaining {
remaining -= picture.1;
opening.push(picture);
} else {
deferred.push(picture);
}
}
if opening.is_empty()
|| deferred.is_empty()
|| opening.iter().map(|(_, height)| height).sum::<i32>() != consumed
|| deferred.iter().map(|(_, height)| height).sum::<i32>() != line.vertical_pos
{
return None;
}
let opening_count = opening.len();
opening.extend(deferred);
opening_content
.checked_add(line.vertical_pos)?
.checked_add(line.line_height)?;
Some(StoredPartitionedPictureCellFlow {
controls: opening,
opening_count,
opening_slack_hu: remaining,
closing_line_height_hu: line.line_height,
})
}
pub(crate) fn cell_wrap_vertical_offset_hu(common: &CommonObjAttr) -> i32 {
let offset = signed_hwpunit(common.vertical_offset);
if !common.treat_as_char
&& common.flow_with_text
&& matches!(common.vert_rel_to, VertRelTo::Para)
&& matches!(
common.text_wrap,
TextWrap::Square | TextWrap::Tight | TextWrap::Through
)
{
offset.max(0)
} else {
offset
}
}
pub(crate) fn topbottom_shape_top_margin_hu(common: &CommonObjAttr) -> i32 {
if !common.treat_as_char
&& common.text_wrap == TextWrap::TopAndBottom
&& matches!(common.vert_align, VertAlign::Top | VertAlign::Inside)
{
i32::from(common.margin.top)
} else {
0
}
}
pub(crate) fn shape_wraps_beside_text(common: &CommonObjAttr) -> bool {
!common.treat_as_char
&& matches!(
common.text_wrap,
TextWrap::Square | TextWrap::Tight | TextWrap::Through
)
}
pub(crate) fn beside_shape_top_margin_hu(common: &CommonObjAttr) -> i32 {
if shape_wraps_beside_text(common)
&& matches!(common.vert_align, VertAlign::Top | VertAlign::Inside)
{
i32::from(common.margin.top)
} else {
0
}
}
pub(crate) fn overlay_shape_margin_origin_hu(common: &CommonObjAttr) -> (i32, i32) {
if common.treat_as_char
|| !matches!(
common.text_wrap,
TextWrap::InFrontOfText | TextWrap::BehindText
)
{
return (0, 0);
}
(
if matches!(common.horz_align, HorzAlign::Left | HorzAlign::Inside) {
i32::from(common.margin.left)
} else {
0
},
if matches!(common.vert_align, VertAlign::Top | VertAlign::Inside) {
i32::from(common.margin.top)
} else {
0
},
)
}
pub(crate) fn stored_hwpx_no_adjust_cell_content_end(
cell: &crate::model::table::Cell,
table: &Table,
dpi: f64,
hwpx_stored: bool,
session_edited: bool,
text_reflowed: bool,
padding_in_stored_frame: bool,
) -> Option<f64> {
if !padding_in_stored_frame
|| !hwpx_stored
|| session_edited
|| text_reflowed
|| table.raw_table_record_attr & 0x08 == 0
|| cell.text_direction != 0
|| cell.paragraphs.is_empty()
{
return None;
}
let mut previous = None;
let mut end = 0i64;
for (pi, para) in cell.paragraphs.iter().enumerate() {
if !para.controls.is_empty() || para.line_segs.is_empty() {
return None;
}
for (li, seg) in para.line_segs.iter().enumerate() {
if seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| seg.vertical_pos < 0
|| seg.line_height <= 0
|| (previous.is_none() && seg.vertical_pos != 0)
|| (previous.is_some()
&& (seg.is_first_line_of_page() || seg.is_first_line_of_column()))
|| previous.is_some_and(|pos| {
seg.vertical_pos < pos || (pi > 0 && li == 0 && seg.vertical_pos == pos)
})
{
return None;
}
previous = Some(seg.vertical_pos);
end = end.max(i64::from(seg.vertical_pos) + i64::from(seg.line_height));
}
}
i32::try_from(end).ok().map(|hu| hwpunit_to_px(hu, dpi))
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct StoredInlineContentFrame {
pub content_height_hu: i32,
pub body_height_hu: i32,
}
pub(crate) fn stored_inline_content_frame(
cell: &crate::model::table::Cell,
table: &Table,
) -> Option<StoredInlineContentFrame> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
use crate::model::shape::SizeCriterion;
if !table.common.treat_as_char
|| table.row_count != 1
|| table.col_count != 1
|| table.cells.len() != 1
|| table.caption.is_some()
|| table.common.width_criterion != SizeCriterion::Absolute
|| table.common.height_criterion != SizeCriterion::Absolute
|| cell.row_span != 1
|| cell.col_span != 1
|| cell.text_direction != 0
|| cell.paragraphs.is_empty()
|| !crate::renderer::cell_vpos_ladder_is_intact(&cell.paragraphs)
{
return None;
}
let pad = cell.effective_padding(&table.padding);
if pad.top < 0 || pad.bottom < 0 {
return None;
}
let mut previous_position = None;
let mut content_end = 0_i32;
for para in &cell.paragraphs {
if para.column_type != ColumnBreakType::None
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| crate::renderer::para_has_no_stored_line_segs(para)
{
return None;
}
for line in ¶.line_segs {
if line.vertical_pos < 0
|| line.line_height <= 0
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| previous_position.is_some_and(|previous| line.vertical_pos < previous)
{
return None;
}
previous_position = Some(line.vertical_pos);
content_end = content_end.max(line.vertical_pos.checked_add(line.line_height)?);
}
}
let body_height = content_end
.checked_add(i32::from(pad.top))?
.checked_add(i32::from(pad.bottom))?;
if body_height <= 0 || body_height != signed_hwpunit(table.common.height) {
return None;
}
let mut has_inline_picture = false;
for para in &cell.paragraphs {
let owners = stored_control_line_indices(para)?;
for (control, owner) in para.controls.iter().zip(owners) {
let (common, height) = match control {
Control::Picture(picture) if picture.caption.is_none() => {
let (_, height) = picture_flow_frame_size_hu(picture);
has_inline_picture |= picture.common.treat_as_char;
(&picture.common, height)
}
Control::Shape(shape) if !shape.common().treat_as_char => {
(shape.common(), signed_hwpunit(shape.common().height))
}
_ => return None,
};
if height <= 0
|| signed_hwpunit(common.width) <= 0
|| common.width_criterion != SizeCriterion::Absolute
|| common.height_criterion != SizeCriterion::Absolute
{
return None;
}
let object_end = if common.treat_as_char {
let owner = para.line_segs.get(owner)?;
let occupied_height = height
.checked_add(i32::from(common.margin.top))?
.checked_add(i32::from(common.margin.bottom))?;
if occupied_height > owner.line_height {
return None;
}
owner.vertical_pos.checked_add(occupied_height)?
} else {
if common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| !matches!(
common.text_wrap,
TextWrap::BehindText | TextWrap::InFrontOfText
)
{
return None;
}
let top = para
.line_segs
.first()?
.vertical_pos
.checked_add(signed_hwpunit(common.vertical_offset))?;
if top < 0 {
return None;
}
top.checked_add(height)?
};
if object_end > content_end {
return None;
}
}
}
has_inline_picture.then_some(StoredInlineContentFrame {
content_height_hu: content_end,
body_height_hu: body_height,
})
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct StoredEmptyPictureCellFrame {
pub line_offset_hu: i32,
pub picture_band_origin_hu: i32,
pub content_height_hu: i32,
}
impl StoredEmptyPictureCellFrame {
pub(crate) fn paragraph_origin_offset_px(
self,
inner_height: f64,
align: crate::model::table::VerticalAlign,
dpi: f64,
) -> f64 {
use crate::model::table::VerticalAlign;
let free = (inner_height - hwpunit_to_px(self.content_height_hu, dpi)).max(0.0);
let alignment = match align {
VerticalAlign::Top => 0.0,
VerticalAlign::Center => free / 2.0,
VerticalAlign::Bottom => free,
};
alignment + hwpunit_to_px(self.line_offset_hu, dpi)
}
}
fn stored_empty_square_picture_cell_frame(
cell: &crate::model::table::Cell,
) -> Option<StoredEmptyPictureCellFrame> {
use crate::model::paragraph::LineSeg;
use crate::model::table::VerticalAlign;
if cell.dirty_flag
|| cell.text_direction != 0
|| !matches!(
cell.vertical_align,
VerticalAlign::Center | VerticalAlign::Bottom
)
{
return None;
}
let [para] = cell.paragraphs.as_slice() else {
return None;
};
let [line] = para.line_segs.as_slice() else {
return None;
};
if !para.text.is_empty()
|| !matches!(
para.column_type,
crate::model::paragraph::ColumnBreakType::None
| crate::model::paragraph::ColumnBreakType::MultiColumn
)
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.controls.is_empty()
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos != 0
|| line.line_height <= 0
{
return None;
}
let mut top = 0_i32;
let mut bottom = line.line_height;
let mut has_picture = false;
for control in ¶.controls {
if matches!(control, Control::SectionDef(_) | Control::ColumnDef(_)) {
continue;
}
let Control::Picture(picture) = control else {
return None;
};
let common = &picture.common;
let height = signed_hwpunit(common.height);
if common.treat_as_char
|| !common.flow_with_text
|| common.text_wrap != TextWrap::Square
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| common.margin.top != 0
|| common.margin.bottom != 0
|| picture.caption.is_some()
|| picture.shape_attr.rotation_angle.rem_euclid(360) != 0
|| height <= 0
{
return None;
}
let offset = signed_hwpunit(common.vertical_offset);
top = top.min(offset);
bottom = bottom.max(offset.checked_add(height)?);
has_picture = true;
}
if !has_picture {
return None;
}
let origin = top.checked_neg()?;
Some(StoredEmptyPictureCellFrame {
line_offset_hu: origin,
picture_band_origin_hu: origin,
content_height_hu: bottom.checked_sub(top)?,
})
}
pub(crate) fn stored_picture_cell_content_end_hu(cell: &crate::model::table::Cell) -> Option<i32> {
stored_picture_cell_flow(cell).map(|flow| flow.content_end_hu)
}
pub(crate) struct StoredPictureCellFlow {
pub paragraph_origins_hu: Vec<i32>,
pub control_offsets_hu: Vec<Vec<i32>>,
pub initial_offset_hu: i32,
pub content_end_hu: i32,
}
impl StoredPictureCellFlow {
pub fn picture_start_hu(&self, paragraph: usize, control: usize) -> i32 {
self.paragraph_origins_hu[paragraph]
+ if paragraph == 0 {
0
} else {
self.initial_offset_hu
}
+ self.control_offsets_hu[paragraph][control]
}
pub fn line_start_hu(&self, paragraph: usize, stored_position: i32) -> i32 {
stored_position
+ if paragraph == 0 {
0
} else {
self.initial_offset_hu
}
}
}
pub(crate) fn stored_picture_cell_flow(
cell: &crate::model::table::Cell,
) -> Option<StoredPictureCellFlow> {
use crate::model::paragraph::LineSeg;
if cell.text_direction != 0 || cell.dirty_flag || cell.paragraphs.is_empty() {
return None;
}
let mut end = 0;
let mut has_picture = false;
let mut origins = Vec::new();
let mut offsets = Vec::new();
let mut initial_offset = 0;
for (index, para) in cell.paragraphs.iter().enumerate() {
let [line] = para.line_segs.as_slice() else {
return None;
};
if !para.text.trim().is_empty()
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.column_type != crate::model::paragraph::ColumnBreakType::None
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
|| line.vertical_pos < end
|| (index == 0 && line.vertical_pos != 0)
{
return None;
}
let first_band = para.controls.first().and_then(|control| {
let Control::Picture(picture) = control else {
return None;
};
signed_hwpunit(picture.common.vertical_offset)
.checked_add(signed_hwpunit(picture.common.height))
});
let origin = if index > 0
&& first_band
.is_some_and(|band| band > 0 && end.checked_add(band) == Some(line.vertical_pos))
{
end
} else {
line.vertical_pos
};
origins.push(origin);
end = line.vertical_pos.checked_add(line.line_height)?;
let mut control_offsets = Vec::new();
let mut picture_end = i32::MIN;
for (control_index, control) in para.controls.iter().enumerate() {
let Control::Picture(picture) = control else {
return None;
};
let common = &picture.common;
if !is_para_topbottom_float(common)
|| !common.flow_with_text
|| common.allow_overlap
|| common.vert_align != VertAlign::Top
|| picture.caption.is_some()
|| common.margin.top != 0
|| common.margin.bottom != 0
|| signed_hwpunit(common.height) <= 0
{
return None;
}
let offset = signed_hwpunit(common.vertical_offset).max(picture_end);
if index == 0 && control_index == 0 {
initial_offset = offset.min(0);
}
control_offsets.push(offset);
picture_end = offset.checked_add(signed_hwpunit(common.height))?;
if origin.checked_add(picture_end)? <= 0 {
return None;
}
has_picture = true;
end = end.max(origin.checked_add(picture_end)?);
}
offsets.push(control_offsets);
}
has_picture.then_some(StoredPictureCellFlow {
paragraph_origins_hu: origins,
control_offsets_hu: offsets,
initial_offset_hu: initial_offset,
content_end_hu: end,
})
}
pub(crate) fn stored_empty_picture_cell_frame(
cell: &crate::model::table::Cell,
table: &Table,
) -> Option<StoredEmptyPictureCellFrame> {
use crate::model::paragraph::LineSeg;
use crate::model::table::VerticalAlign;
if let Some(frame) = stored_empty_square_picture_cell_frame(cell) {
return Some(frame);
}
if cell.row_span != 1
|| cell.text_direction != 0
|| cell.vertical_align != VerticalAlign::Bottom
{
return None;
}
let [para] = cell.paragraphs.as_slice() else {
return None;
};
let [line] = para.line_segs.as_slice() else {
return None;
};
if !para.text.trim().is_empty()
|| para.column_type != crate::model::paragraph::ColumnBreakType::None
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.controls.is_empty()
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos <= 0
|| line.line_height <= 0
{
return None;
}
let padding = cell.effective_padding(&table.padding);
let tail_height = line
.line_height
.checked_add(i32::from(padding.top))?
.checked_add(i32::from(padding.bottom))?;
let row_height = table
.cells
.iter()
.filter(|other| other.row == cell.row && other.row_span == 1)
.map(|other| signed_hwpunit(other.height))
.max()?;
if padding.top < 0
|| padding.bottom < 0
|| signed_hwpunit(cell.height) != tail_height
|| line.vertical_pos.checked_add(tail_height)? != row_height
{
return None;
}
let mut picture_band_end = 0;
let mut has_flow_picture = false;
for control in ¶.controls {
let Control::Picture(picture) = control else {
return None;
};
let common = &picture.common;
let offset = signed_hwpunit(common.vertical_offset);
if common.treat_as_char
|| !common.flow_with_text
|| picture.caption.is_some()
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| !matches!(
common.text_wrap,
TextWrap::TopAndBottom | TextWrap::InFrontOfText
)
|| offset < 0
|| signed_hwpunit(common.height) <= 0
|| common.margin.top != 0
|| common.margin.bottom != 0
{
return None;
}
has_flow_picture |= common.text_wrap == TextWrap::TopAndBottom;
picture_band_end = picture_band_end.max(offset.checked_add(signed_hwpunit(common.height))?);
}
if !has_flow_picture || picture_band_end > line.vertical_pos {
return None;
}
Some(StoredEmptyPictureCellFrame {
line_offset_hu: line.vertical_pos,
picture_band_origin_hu: line.vertical_pos - picture_band_end,
content_height_hu: line.vertical_pos.checked_add(line.line_height)?,
})
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct StoredEmptyOpeningRowFrame {
pub opening_height: f64,
pub continuation_height: f64,
}
#[derive(Debug, Clone)]
pub(crate) struct StoredCompletePrefixFrame {
pub end_row: usize,
pub opening_height: f64,
pub last_row_height: f64,
pub closing_row_height: f64,
pub end_cut: Vec<usize>,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct StoredSingleCellRowFrames {
pub opening_anchor_hu: i32,
pub opening_table_top_hu: i32,
pub opening_end: usize,
pub opening_height: f64,
pub closing_height: f64,
}
pub(crate) fn stored_square_sibling_outer_frame(para: &Paragraph) -> bool {
if !para.text.trim().is_empty()
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.controls.len() < 2
|| !matches!(para.line_segs.as_slice(), [line]
if line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& line.vertical_pos > 0 && line.line_height > 0)
{
return false;
}
para.controls.iter().all(|control| {
matches!(control, Control::Table(t)
if !t.common.treat_as_char && t.common.flow_with_text
&& t.caption.is_none()
&& signed_hwpunit(t.common.width) > 0 && signed_hwpunit(t.common.height) > 0
&& t.common.text_wrap == TextWrap::Square
&& t.common.vert_rel_to == VertRelTo::Para
&& t.common.vert_align == VertAlign::Top
&& t.common.horz_rel_to == HorzRelTo::Column
&& t.common.horz_align == HorzAlign::Left
&& signed_hwpunit(t.common.horizontal_offset) >= 0
&& t.cells.iter().all(|cell| cell.paragraphs.iter().all(|p|
!p.stored_text_partition_is_dirty() && !p.cell_format_vpos_dirty)))
})
}
pub(crate) fn control_leaves_table_anchor_empty(control: &Control) -> bool {
match control {
Control::SectionDef(_) | Control::ColumnDef(_) => true,
Control::Picture(picture) => detached_from_anchor_line(&picture.common),
Control::Shape(shape) => detached_from_anchor_line(shape.common()),
_ => false,
}
}
fn detached_from_anchor_line(common: &crate::model::shape::CommonObjAttr) -> bool {
!common.treat_as_char && matches!(common.vert_rel_to, VertRelTo::Paper | VertRelTo::Page)
}
pub(crate) fn object_only_saved_table_anchor(para: &Paragraph, table: &Table) -> bool {
let allow_frame_settings = !para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& matches!(para.line_segs.as_slice(), [line]
if line.vertical_pos >= 0
&& line.segment_width == 0
&& line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0);
let mut objects = para.controls.iter().filter(|control| {
!(allow_frame_settings
&& (matches!(control, Control::PageNumberPos(_))
|| control_leaves_table_anchor_empty(control)))
});
let only_table = matches!(objects.next(), Some(Control::Table(_))) && objects.next().is_none();
!table.common.treat_as_char
&& is_para_topbottom_float(&table.common)
&& para.text.is_empty()
&& only_table
&& matches!(para.line_segs.as_slice(), [line] if line.segment_width == 0 && line.tag & 0x8000_0000 == 0)
}
pub(crate) fn block_table_caption_host_spacing_px(
para: &Paragraph,
table: &Table,
dpi: f64,
) -> f64 {
if object_only_saved_table_anchor(para, table)
|| textless_single_float_host(para, table)
|| saved_inline_sibling_owns_caption_host_line(para, table, dpi)
{
0.0
} else {
para.line_segs
.first()
.map_or(0.0, |line| hwpunit_to_px(line.line_spacing, dpi))
}
}
pub(crate) fn textless_single_float_host(para: &Paragraph, table: &Table) -> bool {
!table.common.treat_as_char
&& is_para_topbottom_float(&table.common)
&& para.text.is_empty()
&& para
.controls
.iter()
.filter(|control| !matches!(control, Control::SectionDef(_) | Control::ColumnDef(_)))
.all(|control| matches!(control, Control::Table(t) if std::ptr::eq(t.as_ref(), table)))
}
fn saved_inline_sibling_owns_caption_host_line(para: &Paragraph, table: &Table, dpi: f64) -> bool {
if table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| !para.text.is_empty()
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
{
return false;
}
let [line] = para.line_segs.as_slice() else {
return false;
};
if line.line_height <= 0
|| line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
{
return false;
}
let Some(owner) = para.controls.iter().position(|control| {
matches!(control, Control::Table(candidate) if std::ptr::eq(candidate.as_ref(), table))
}) else { return false; };
let Some(owner_line) = crate::renderer::layout::control_line_seg_index(para, owner) else {
return false;
};
para.controls.iter().enumerate().any(|(index, control)| {
let Control::Table(sibling) = control else {
return false;
};
if !sibling.common.treat_as_char
|| crate::renderer::layout::control_line_seg_index(para, index) != Some(owner_line)
{
return false;
}
let caption_hu = sibling.caption.as_ref().map_or(0_i64, |caption| {
i64::from(crate::renderer::px_to_hwpunit(
crate::renderer::composer::caption_height_px(&sibling.caption, dpi),
dpi,
)) + i64::from(caption.spacing)
});
i64::from(line.line_height)
== i64::from(sibling.common.height)
+ caption_hu
+ i64::from(sibling.outer_margin_top)
+ i64::from(sibling.outer_margin_bottom)
})
}
pub(crate) fn stored_body_filling_rowbreak_frame(
para: &Paragraph,
table: &Table,
body_height: f64,
dpi: f64,
hwpx_stored: bool,
session_edited: bool,
) -> bool {
if !hwpx_stored
|| session_edited
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.common.vert_rel_to != VertRelTo::Para
|| table.common.vert_align != VertAlign::Top
|| signed_hwpunit(table.common.vertical_offset) != 0
|| table.common.height == 0
|| table.common.height >= 0x8000_0000
|| table.outer_margin_top == 0
|| body_height <= 0.0
|| !para.text.is_empty()
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
{
return false;
}
let [line] = para.line_segs.as_slice() else {
return false;
};
if line.vertical_pos != 0
|| line.segment_width != 0
|| line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| para
.controls
.iter()
.filter(|ctrl| matches!(ctrl, Control::Table(_)))
.count()
!= 1
|| para.controls.iter().any(|ctrl| {
!matches!(
ctrl,
Control::Table(_)
| Control::SectionDef(_)
| Control::ColumnDef(_)
| Control::PageNumberPos(_)
)
})
{
return false;
}
let frame_hu = i64::from(table.common.height)
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom);
let body_hu = (body_height * 7200.0 / dpi).round() as i64;
(frame_hu - body_hu).abs() <= 2
}
fn stored_inline_host_has_separate_float_frame(para: &Paragraph, table: &Table) -> bool {
let [line] = para.line_segs.as_slice() else {
return false;
};
if !para.text.is_empty()
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| line.vertical_pos < 0
|| line.line_height <= 0
|| line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| signed_hwpunit(table.common.vertical_offset) < line.line_height
{
return false;
}
let mut float_count = 0;
let mut inline_band = None;
for (index, control) in para.controls.iter().enumerate() {
match control {
Control::Table(inline) if inline.common.treat_as_char => {
if crate::renderer::layout::control_line_seg_index(para, index) != Some(0) {
return false;
}
let band = i64::from(inline.common.height)
+ i64::from(inline.outer_margin_top)
+ i64::from(inline.outer_margin_bottom);
inline_band = Some(inline_band.map_or(band, |previous: i64| previous.max(band)));
}
Control::Table(_) => float_count += 1,
Control::SectionDef(_) | Control::ColumnDef(_) => {}
_ => return false,
}
}
float_count == 1 && inline_band == Some(i64::from(line.line_height))
}
pub(crate) fn column_rowbreak_fragment_opens_outer_top(
hwpx_stored: bool,
native_host: Option<&Paragraph>,
table: &Table,
is_continuation: bool,
start_row: usize,
start_cut: &[usize],
starts_at_column_top: bool,
) -> bool {
let native_inline_frame =
native_host.is_some_and(|para| stored_inline_host_has_separate_float_frame(para, table));
let native_object_frame =
native_host.is_some_and(|para| stored_independent_table_frame_host(para, table));
let para_anchor_below_first_line = native_inline_frame
|| native_host.is_some_and(|host| {
object_only_saved_table_anchor(host, table)
&& host
.line_segs
.first()
.is_some_and(|line| line.vertical_pos >= line.line_height)
});
let single_cell_frame = table.row_count == 1 && table.col_count == 1;
let para_anchor_below_text_line = !single_cell_frame
&& native_host.is_some_and(|host| {
let [line] = host.line_segs.as_slice() else {
return false;
};
para_has_non_whitespace_text(host)
&& !host.stored_text_partition_is_dirty()
&& !host.cell_format_vpos_dirty
&& line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& line.vertical_pos >= 0
&& line.line_height > 0
&& matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
&& signed_hwpunit(table.common.vertical_offset) >= line.line_height
});
(hwpx_stored || native_object_frame || native_inline_frame)
&& !table.common.treat_as_char
&& is_para_topbottom_float(&table.common)
&& (table.common.horz_rel_to == HorzRelTo::Column
|| (table.common.horz_rel_to == HorzRelTo::Para
&& table.common.vert_rel_to == VertRelTo::Para
&& (native_host.is_some_and(|host| object_only_saved_table_anchor(host, table))
|| para_anchor_below_text_line
|| ((hwpx_stored || is_continuation) && para_anchor_below_first_line))))
&& table.layout_page_break() == TablePageBreak::RowBreak
&& table.outer_margin_top > 0
&& ((!is_continuation && start_row == 0 && start_cut.is_empty())
|| (is_continuation
&& starts_at_column_top
&& (table.common.horz_rel_to == HorzRelTo::Column
|| (native_host.is_some_and(|host| {
!host.stored_text_partition_is_dirty()
&& !host.cell_format_vpos_dirty
&& !host.line_segs.is_empty()
})))))
}
pub(crate) fn paragraph_following_overlay_rowbreak_frame(table: &Table) -> bool {
!table.common.treat_as_char
&& table.common.flow_with_text
&& table.common.vert_rel_to == VertRelTo::Para
&& table.common.text_wrap == TextWrap::InFrontOfText
&& table.layout_page_break() == TablePageBreak::RowBreak
}
pub(crate) fn column_rowbreak_grid_bottom_margin_px(
opens_outer_frame: bool,
table: &Table,
is_continuation: bool,
has_grid_boundary: bool,
is_nonterminal: bool,
dpi: f64,
) -> f64 {
if opens_outer_frame
&& is_continuation
&& has_grid_boundary
&& is_nonterminal
&& table.caption.is_none()
{
hwpunit_to_px(table.outer_margin_bottom as i32, dpi)
} else {
0.0
}
}
pub(crate) fn column_rowbreak_caption_outer_spacing_px(
fragment_opens_outer_margin: bool,
para: &Paragraph,
table: &Table,
dpi: f64,
) -> f64 {
if fragment_opens_outer_margin
&& object_only_saved_table_anchor(para, table)
&& table.caption.as_ref().is_some_and(|caption| {
matches!(
caption.direction,
crate::model::shape::CaptionDirection::Top
| crate::model::shape::CaptionDirection::Bottom
)
})
{
hwpunit_to_px(table.outer_margin_bottom as i32, dpi)
} else {
0.0
}
}
pub(crate) fn stored_after_partial_table_shared_spacing_px(
stored_layout: bool,
previous_is_partial_table: bool,
para: &Paragraph,
spacing_before: f64,
flow_from_body_top: f64,
dpi: f64,
) -> f64 {
if !stored_layout
|| !previous_is_partial_table
|| spacing_before <= 0.0
|| !para.controls.is_empty()
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
{
return 0.0;
}
let Some(first) = para.line_segs.first() else {
return 0.0;
};
if first.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0 {
return 0.0;
}
let saved_top = hwpunit_to_px(first.vertical_pos, dpi);
if saved_top > 0.0 && (saved_top - flow_from_body_top).abs() <= 1.0 {
spacing_before
} else {
0.0
}
}
pub(crate) fn hwpx_after_picture_caption_shared_spacing_px(
hwpx_stored: bool,
previous: Option<&Paragraph>,
para: &Paragraph,
spacing_before: f64,
flow_from_body_top: f64,
dpi: f64,
) -> f64 {
if !hwpx_stored || spacing_before <= 0.0 {
return 0.0;
}
let has_bottom_caption = previous.is_some_and(|previous| {
previous.controls.iter().any(|control| {
matches!(control,
Control::Picture(picture)
if !picture.common.treat_as_char
&& picture.common.text_wrap == TextWrap::TopAndBottom
&& picture.caption.as_ref().is_some_and(|caption|
caption.direction == crate::model::shape::CaptionDirection::Bottom))
})
});
if !has_bottom_caption {
return 0.0;
}
let Some(first) = para.line_segs.first() else {
return 0.0;
};
if first.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0 {
return 0.0;
}
let saved_top = hwpunit_to_px(first.vertical_pos, dpi);
if saved_top > 0.0 && (saved_top - flow_from_body_top).abs() <= 1.0 {
spacing_before
} else {
0.0
}
}
#[derive(Debug, Clone)]
pub(crate) struct StoredTableFrameWithGuides {
pub guide_range: Range<usize>,
pub top_hu: i32,
pub bottom_hu: i32,
}
pub(crate) fn stored_table_frame_with_guides(
paragraphs: &[Paragraph],
host_index: usize,
table: &Table,
) -> Option<StoredTableFrameWithGuides> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
let host = paragraphs.get(host_index)?;
if !host.text.is_empty()
|| !matches!(host.controls.as_slice(), [Control::Table(_)])
|| host.column_type != ColumnBreakType::None
|| host.stored_text_partition_is_dirty()
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| !matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
|| table.common.vert_rel_to != VertRelTo::Para
|| table.common.horz_rel_to != HorzRelTo::Column
|| table.layout_page_break() != TablePageBreak::RowBreak
|| signed_hwpunit(table.common.vertical_offset) <= 0
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
{
return None;
}
let [anchor] = host.line_segs.as_slice() else {
return None;
};
if anchor.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0 || anchor.vertical_pos <= 0 {
return None;
}
let top_hu = i32::from(table.outer_margin_top);
let bottom_hu = i32::try_from(
i64::from(top_hu) + i64::from(table.common.height) + i64::from(table.outer_margin_bottom),
)
.ok()?;
let mut end = host_index + 1;
let mut previous = anchor;
while let Some(guide) = paragraphs.get(end) {
if guide.para_shape_id != host.para_shape_id
|| !guide.text.is_empty()
|| !guide.controls.is_empty()
|| guide.column_type != ColumnBreakType::None
|| guide.stored_text_partition_is_dirty()
{
break;
}
let [line] = guide.line_segs.as_slice() else {
return None;
};
if line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| previous.line_height.saturating_add(previous.line_spacing) <= 0
|| line.vertical_pos
!= previous
.vertical_pos
.checked_add(previous.line_height)?
.checked_add(previous.line_spacing)?
{
return None;
}
previous = line;
end += 1;
}
if end == host_index + 1 {
return None;
}
let successor = paragraphs.get(end)?;
if successor.stored_text_partition_is_dirty() || successor.column_type != ColumnBreakType::None
{
return None;
}
let first = successor.line_segs.first()?;
if first.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| first.vertical_pos != bottom_hu
|| first.vertical_pos >= anchor.vertical_pos
{
return None;
}
Some(StoredTableFrameWithGuides {
guide_range: host_index + 1..end,
top_hu,
bottom_hu,
})
}
pub(crate) fn stored_interior_control_table_frame(
host: &Paragraph,
successor: &Paragraph,
control_index: usize,
table: &Table,
measured_height: f64,
frame_vpos: i32,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
use crate::model::paragraph::LineSeg;
let valid = |para: &Paragraph| {
!para.stored_text_partition_is_dirty()
&& !para.line_segs.is_empty()
&& para
.line_segs
.iter()
.all(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0)
&& para.line_segs.windows(2).all(|pair| {
pair[1].text_start > pair[0].text_start
&& pair[1].vertical_pos > pair[0].vertical_pos
})
};
if !valid(host)
|| !valid(successor)
|| host.text.trim().is_empty()
|| host.controls.len() != 1
|| !matches!(host.controls.first(), Some(Control::Table(_)))
|| !table.common.flow_with_text
|| !is_para_topbottom_float(&table.common)
|| table.common.horz_rel_to != HorzRelTo::Column
|| !matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
|| signed_hwpunit(table.common.vertical_offset) <= 0
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.caption.is_some()
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
|| (measured_height * 7200.0 / dpi).round() != f64::from(table.common.height)
{
return None;
}
let line_index = *stored_control_line_indices(host)?.get(control_index)?;
if line_index == 0 || line_index + 1 >= host.line_segs.len() {
return None;
}
let anchor = host.line_segs.get(line_index)?.vertical_pos;
let top = i64::from(anchor)
+ i64::from(signed_hwpunit(table.common.vertical_offset))
+ i64::from(table.outer_margin_top);
let bottom = top + i64::from(table.common.height) + i64::from(table.outer_margin_bottom);
let host_end = host
.line_segs
.iter()
.map(|line| i64::from(line.vertical_pos) + i64::from(line.line_height))
.max()?;
if anchor < frame_vpos
|| top < host_end
|| bottom != i64::from(successor.line_segs.first()?.vertical_pos)
{
return None;
}
let px = |value: i64| (value - i64::from(frame_vpos)) as f64 * dpi / 7200.0;
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::NextLine,
anchor_y: px(i64::from(anchor)),
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: None,
table_top: px(top),
occupied_bottom: px(bottom),
})
}
pub(crate) fn stored_float_frame_before_tac_line(
host: &Paragraph,
control_index: usize,
table: &Table,
measured_height: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
use crate::model::paragraph::LineSeg;
host.empty_control_stream_position(control_index)?;
let [line] = host.line_segs.as_slice() else {
return None;
};
let [Control::Table(a), Control::Table(b)] = host.controls.as_slice() else {
return None;
};
let tac = if std::ptr::eq(a.as_ref(), table) {
b
} else if std::ptr::eq(b.as_ref(), table) {
a
} else {
return None;
};
let offset = signed_hwpunit(table.common.vertical_offset);
if host.stored_text_partition_is_dirty()
|| host.cell_format_vpos_dirty
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| table.common.treat_as_char
|| !tac.common.treat_as_char
|| !table.common.flow_with_text
|| !is_para_topbottom_float(&table.common)
|| table.common.vert_rel_to != VertRelTo::Para
|| !matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
|| table.caption.is_some()
|| tac.caption.is_some()
|| offset != 0
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
|| !measured_height.is_finite()
|| !dpi.is_finite()
|| dpi <= 0.0
|| (measured_height * 7200.0 / dpi).round() != f64::from(table.common.height)
|| i64::from(line.line_height)
!= i64::from(tac.common.height)
+ i64::from(tac.outer_margin_top)
+ i64::from(tac.outer_margin_bottom)
{
return None;
}
let bottom = i64::from(ladder_vpos(host, 0, line.vertical_pos));
let top = bottom - i64::from(table.common.height) - i64::from(table.outer_margin_bottom);
let anchor = top - i64::from(offset) - i64::from(table.outer_margin_top);
if anchor != 0 {
return None;
}
let px = |hu: i64| hu as f64 * dpi / 7200.0;
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::NextLine,
anchor_y: px(anchor),
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: None,
table_top: px(top),
occupied_bottom: px(bottom),
})
}
pub(crate) fn stored_plain_table_source_is_current(table: &Table) -> bool {
table
.raw_ctrl_seal
.is_some_and(|sealed| sealed == crate::model::raw_provenance::record_digest(&table.common))
&& table.caption.is_none()
&& !table.cells.is_empty()
&& table.cells.iter().all(|cell| {
!cell.dirty_flag
&& !cell.paragraphs.is_empty()
&& cell.paragraphs.iter().all(|para| {
!para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& para.controls.iter().all(|control| matches!(control,
Control::Field(field) if field.field_type == crate::model::control::FieldType::Hyperlink))
&& !para.line_segs.is_empty()
&& para.line_segs.iter().all(|line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY
== 0
&& line.vertical_pos >= 0
&& line.line_height > 0
&& line.line_spacing >= 0
})
&& para
.source_line_seg_vertical_pos
.as_ref()
.is_none_or(|source| {
source
.iter()
.copied()
.eq(para.line_segs.iter().map(|line| line.vertical_pos))
})
})
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn stored_empty_control_table_frame(
host: &Paragraph,
successor: &Paragraph,
table: &Table,
measured_height: f64,
host_spacing_before: f64,
frame_vpos: i32,
dpi: f64,
hwpx_object_frame: bool,
) -> Option<ParagraphFloatPlacement> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
let [anchor] = host.line_segs.as_slice() else {
return None;
};
let next = successor.line_segs.first()?;
let offset = signed_hwpunit(table.common.vertical_offset);
let anchor_vpos = ladder_vpos(host, 0, anchor.vertical_pos);
let next_vpos = ladder_vpos(successor, 0, next.vertical_pos);
let single_column = host
.controls
.iter()
.any(|control| matches!(control, Control::ColumnDef(column) if column.column_count == 1));
let single_table_host = host
.controls
.iter()
.filter(|control| matches!(control, Control::Table(_)))
.count()
== 1
&& host.controls.iter().all(|control| {
matches!(control, Control::Table(_) | Control::SectionDef(_))
|| matches!(control, Control::ColumnDef(column) if column.column_count == 1)
});
if !host.text.trim().is_empty()
|| !single_table_host
|| host.stored_text_partition_is_dirty()
|| successor.stored_text_partition_is_dirty()
|| host.cell_format_vpos_dirty
|| successor.cell_format_vpos_dirty
|| !(matches!(
host.column_type,
ColumnBreakType::None | ColumnBreakType::Page | ColumnBreakType::Section
) || (host.column_type == ColumnBreakType::MultiColumn && single_column))
|| successor.column_type != ColumnBreakType::None
|| anchor.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| next.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| anchor.line_height <= 0
|| next.line_height <= 0
|| anchor_vpos < frame_vpos
|| !table.common.flow_with_text
|| !is_para_topbottom_float(&table.common)
|| !matches!(
table.common.horz_rel_to,
HorzRelTo::Column | HorzRelTo::Para
)
|| !matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
|| offset < 0
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
|| !measured_height.is_finite()
|| !host_spacing_before.is_finite()
|| !dpi.is_finite()
|| dpi <= 0.0
{
return None;
}
let caption_extent = if let Some(caption) = &table.caption {
use crate::model::shape::CaptionDirection;
if !matches!(
caption.direction,
CaptionDirection::Top | CaptionDirection::Bottom
) || caption.spacing < 0
|| caption.paragraphs.is_empty()
|| caption.paragraphs.iter().any(|para| {
para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para
.controls
.iter()
.any(|control| !matches!(control, Control::AutoNumber(_)))
|| para.line_segs.is_empty()
|| para.line_segs.iter().any(|line| {
line.line_height <= 0
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
})
})
{
return None;
}
let height = super::composer::caption_height_px(&table.caption, dpi);
if height <= 0.0 || !height.is_finite() {
return None;
}
height * 7200.0 / dpi + f64::from(caption.spacing)
} else {
0.0
};
let measured_extent = (measured_height * 7200.0 / dpi).round();
if measured_extent != (f64::from(table.common.height) + caption_extent).round() {
return None;
}
let unpositioned_extent = i64::from(table.outer_margin_top)
+ measured_extent as i64
+ i64::from(table.outer_margin_bottom);
let offset_consumed = frame_vpos == 0
&& next_vpos > 0
&& next_vpos < anchor_vpos
&& unpositioned_extent == i64::from(next_vpos);
let positioned_offset = if offset_consumed {
0
} else {
i64::from(offset)
};
let object_extent = positioned_offset + unpositioned_extent;
let leading_band = (host_spacing_before * 7200.0 / dpi).round() as i64;
let complete_rewound_frame = frame_vpos == 0
&& next_vpos > 0
&& next_vpos < anchor_vpos
&& object_extent == i64::from(next_vpos);
let anchor_origin = if (host.column_type == ColumnBreakType::Page || complete_rewound_frame)
&& object_extent == i64::from(next_vpos)
{
0
} else if table.caption.is_some()
&& leading_band > 0
&& leading_band <= i64::from(anchor_vpos)
&& i64::from(anchor_vpos) - leading_band + object_extent == i64::from(next_vpos)
{
i64::from(anchor_vpos) - leading_band
} else {
i64::from(anchor_vpos)
};
let paint_inset = if table.caption.is_none()
&& anchor_origin == i64::from(anchor_vpos)
&& positioned_offset == 0
&& crate::renderer::stored_float_anchor::stored_topbottom_flow_advance_hu(
host,
Some(successor),
table,
) == Some(object_extent)
{
crate::renderer::stored_float_anchor::stored_topbottom_object_span(
host,
Some(successor),
table,
hwpx_object_frame,
)
.0
} else {
i64::from(table.outer_margin_top)
};
let top = anchor_origin + positioned_offset + paint_inset;
let bottom = anchor_origin + object_extent;
if bottom != i64::from(next_vpos) {
return None;
}
let px = |value: i64| (value - i64::from(frame_vpos)) as f64 * dpi / 7200.0;
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::NextLine,
anchor_y: px(anchor_origin),
stored_host_origin: None,
stored_successor_line_origin: Some(px(bottom)),
table_left: None,
table_top: px(top),
occupied_bottom: px(bottom),
})
}
pub(crate) fn stored_text_cell_padding_height_hu(
cell: &crate::model::table::Cell,
table: &Table,
) -> Option<i32> {
use crate::model::paragraph::LineSeg;
if table.common.treat_as_char
|| table.layout_page_break() != crate::model::table::TablePageBreak::RowBreak
|| cell.dirty_flag
|| cell.text_direction != 0
|| cell.height == 0
|| cell.height >= 0x8000_0000
|| cell.vertical_padding_guard_height_hu(table) != cell.height
|| cell.paragraphs.is_empty()
{
return None;
}
let mut minimum_line = i32::MAX;
for para in &cell.paragraphs {
if para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.line_segs.is_empty()
|| !para.controls.iter().all(
|control| matches!(control, Control::ColumnDef(column) if column.column_count == 1),
)
{
return None;
}
for line in ¶.line_segs {
if line.line_height <= 0
|| line.vertical_pos < 0
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
{
return None;
}
minimum_line = minimum_line.min(line.line_height);
}
}
if minimum_line <= i32::try_from(cell.height).ok()? {
return None;
}
Some(minimum_line)
}
#[derive(Clone, Debug)]
pub(crate) struct StoredNestedRowFrames {
pub lines: Vec<(usize, usize, i32)>,
pub reset: usize,
pub opening_height_hu: i32,
pub closing_height_hu: i32,
pub parent_row_height_hu: i32,
}
pub(crate) fn stored_nested_row_frames(
parent: &Table,
cell: &crate::model::table::Cell,
child: &Table,
) -> Option<StoredNestedRowFrames> {
match stored_nested_row_frames_with_storage(parent, cell, child, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn stored_nested_row_frames_with_storage<S: crate::storage::Storage>(
parent: &Table,
cell: &crate::model::table::Cell,
child: &Table,
storage: &S,
) -> Result<Option<StoredNestedRowFrames>, S::Error> {
crate::storage::with_optional(|failure| {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
let structural = |para: &Paragraph| {
!para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& !para.line_segs.is_empty()
&& para.controls.iter().all(
|control| matches!(control, Control::ColumnDef(column) if column.column_count == 1),
)
&& para.line_segs.iter().all(|line| {
line.line_height > 0 && line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
};
let text_end = |text_cell: &crate::model::table::Cell, table: &Table| {
if text_cell.dirty_flag || text_cell.text_direction != 0 {
return None;
}
let mut previous = None;
let mut end = 0i64;
for para in &text_cell.paragraphs {
if !structural(para) {
return None;
}
for (index, line) in para.line_segs.iter().enumerate() {
let top = ladder_vpos(para, index, line.vertical_pos);
if top < 0 || previous.is_some_and(|before| top < before) {
return None;
}
previous = Some(top);
end = end.max(i64::from(top) + i64::from(line.line_height));
}
}
let padding = text_cell.effective_padding(&table.padding);
Some(end + i64::from(padding.top) + i64::from(padding.bottom))
};
let last_row = usize::from(parent.row_count).checked_sub(1)?;
let host = cell.paragraphs.first()?;
let successor = cell.paragraphs.get(1)?;
let [anchor] = host.line_segs.as_slice() else {
return None;
};
let [child_cell] = child.cells.as_slice() else {
return None;
};
if last_row == 0
|| usize::from(cell.row) != last_row
|| cell.row_span != 1
|| cell.dirty_flag
|| parent.layout_page_break() != crate::model::table::TablePageBreak::RowBreak
|| !is_para_topbottom_float(&parent.common)
|| !is_para_topbottom_float(&child.common)
|| !child.common.flow_with_text
|| child.common.allow_overlap
|| child.common.vertical_offset != 0
|| child.outer_margin_top != 0
|| child.outer_margin_bottom != 0
|| child.caption.is_some()
|| child.row_count != 1
|| child.col_count != 1
|| child_cell.dirty_flag
|| child_cell.text_direction != 0
|| host.stored_text_partition_is_dirty()
|| host.cell_format_vpos_dirty
|| !host.text.trim().is_empty()
|| anchor.line_height <= 0
|| anchor.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| ladder_vpos(host, 0, anchor.vertical_pos) != 0
|| !matches!(
host.column_type,
ColumnBreakType::None | ColumnBreakType::MultiColumn
)
|| host
.controls
.iter()
.filter(|control| matches!(control, Control::Table(_)))
.count()
!= 1
|| !host.controls.iter().all(|control| {
matches!(control, Control::Table(table) if std::ptr::eq(table.as_ref(), child))
|| matches!(control, Control::ColumnDef(column) if column.column_count == 1)
})
|| !structural(successor)
|| parent.common.height == 0
|| child.common.height == 0
{
return None;
}
let mut lines = Vec::new();
let mut reset = None;
for (pi, para) in child_cell.paragraphs.iter().enumerate() {
if !structural(para) {
return None;
}
for (li, line) in para.line_segs.iter().enumerate() {
if super::height_measurer::stored_seg_is_row_fragment(para, li) {
continue;
}
let top = ladder_vpos(para, li, line.vertical_pos);
if top < 0 || (lines.is_empty() && top != 0) {
return None;
}
if lines.last().is_some_and(|&(_, _, before)| top <= before) {
if top != 0 || reset.replace(lines.len()).is_some() {
return None;
}
}
crate::storage::optional(storage.push(&mut lines, (pi, li, top)), failure)?;
}
}
let reset = reset?;
let padding = child_cell.effective_padding(&child.padding);
let (pi, li, top) = *lines.get(reset.checked_sub(1)?)?;
let first_ink_end = i64::from(top)
+ i64::from(child_cell.paragraphs[pi].line_segs[li].line_height)
+ i64::from(padding.top)
+ i64::from(padding.bottom);
if first_ink_end > i64::from(child.common.height) {
return None;
}
let (pi, li, top) = *lines.last()?;
let closing = i64::from(top)
+ i64::from(child_cell.paragraphs[pi].line_segs[li].line_height)
+ i64::from(padding.top)
+ i64::from(padding.bottom);
let next = successor.line_segs.first()?;
if i64::from(ladder_vpos(successor, 0, next.vertical_pos)) != closing {
return None;
}
let mut row_heights = crate::storage::optional(storage.filled(last_row, None), failure)?;
for other in &parent.cells {
if other.row_span != 1 {
return None;
}
if usize::from(other.row) < last_row {
let height = text_end(other, parent)?;
let slot = &mut row_heights[usize::from(other.row)];
*slot = Some(slot.unwrap_or(0i64).max(height));
}
}
let prefix = row_heights
.into_iter()
.try_fold(0i64, |sum, height| Some(sum + height?))?
+ i64::from(parent.cell_spacing) * last_row as i64;
let parent_padding = cell.effective_padding(&parent.padding);
let parent_pad = i64::from(parent_padding.top) + i64::from(parent_padding.bottom);
let opening = i64::from(child.common.height) + parent_pad;
if prefix + opening != i64::from(parent.common.height) {
return None;
}
for other in parent
.cells
.iter()
.filter(|other| usize::from(other.row) == last_row)
{
if !std::ptr::eq(other, cell) && text_end(other, parent)? > opening {
return None;
}
}
let mut final_end = closing;
for para in cell.paragraphs.iter().skip(1) {
if !structural(para) {
return None;
}
for (index, line) in para.line_segs.iter().enumerate() {
let top = i64::from(ladder_vpos(para, index, line.vertical_pos));
if top < closing {
return None;
}
final_end = final_end.max(top + i64::from(line.line_height));
}
}
Some(StoredNestedRowFrames {
lines,
reset,
opening_height_hu: i32::try_from(child.common.height).ok()?,
closing_height_hu: i32::try_from(closing).ok()?,
parent_row_height_hu: i32::try_from(opening + final_end + parent_pad).ok()?,
})
})
}
pub(crate) fn stored_float_before_whitespace_line_frame(
previous: &Paragraph,
host: &Paragraph,
successor: &Paragraph,
table: &Table,
measured_height: f64,
successor_spacing_before: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
use crate::model::{
control::Control,
paragraph::{ColumnBreakType, LineSeg},
};
let [anchor] = host.line_segs.as_slice() else {
return None;
};
let before = previous.line_segs.last()?;
let after = successor.line_segs.first()?;
if host.text.is_empty()
|| !host.text.chars().all(char::is_whitespace)
|| !matches!(host.controls.as_slice(), [Control::Table(_)])
|| !previous.controls.is_empty()
|| host.column_type != ColumnBreakType::None
|| successor.column_type != ColumnBreakType::None
|| [previous, host, successor].iter().any(|para| {
para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.source_line_seg_vertical_pos.is_some()
|| para
.line_segs
.iter()
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
})
|| before.line_height <= 0
|| anchor.line_height <= 0
|| i64::from(anchor.line_height) + i64::from(anchor.line_spacing) <= 0
|| table.common.treat_as_char
|| !table.common.flow_with_text
|| !is_para_topbottom_float(&table.common)
|| table.caption.is_some()
|| signed_hwpunit(table.common.vertical_offset) < 0
|| !measured_height.is_finite()
|| !successor_spacing_before.is_finite()
|| successor_spacing_before < 0.0
|| !dpi.is_finite()
|| dpi <= 0.0
|| (measured_height * 7200.0 / dpi).round() != f64::from(table.common.height)
{
return None;
}
let start = i64::from(before.vertical_pos)
+ i64::from(before.line_height)
+ i64::from(before.line_spacing);
let top = start
+ i64::from(signed_hwpunit(table.common.vertical_offset))
+ i64::from(table.outer_margin_top);
let end = top + i64::from(table.common.height) + i64::from(table.outer_margin_bottom);
let host_line_end = end + i64::from(anchor.line_height) + i64::from(anchor.line_spacing);
let next_before = (successor_spacing_before * 7200.0 / dpi).round() as i64;
if start < 0
|| end != i64::from(anchor.vertical_pos)
|| !(host_line_end == i64::from(after.vertical_pos)
|| host_line_end.checked_add(next_before) == Some(i64::from(after.vertical_pos)))
{
return None;
}
let px = |hu: i64| hu as f64 * dpi / 7200.0;
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::NextLine,
anchor_y: px(start),
stored_host_origin: Some(px(end)),
stored_successor_line_origin: Some(px(i64::from(after.vertical_pos))),
table_left: None,
table_top: px(top),
occupied_bottom: px(i64::from(after.vertical_pos)),
})
}
pub(crate) fn stored_adjacent_line_table_frame(
previous: &Paragraph,
host: &Paragraph,
successor: &Paragraph,
table: &Table,
measured_height: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
let [anchor] = host.line_segs.as_slice() else {
return None;
};
let before = previous.line_segs.last()?;
let after = successor.line_segs.first()?;
let anchor_vpos = ladder_vpos(host, 0, anchor.vertical_pos);
let before_vpos = ladder_vpos(previous, previous.line_segs.len() - 1, before.vertical_pos);
let after_vpos = ladder_vpos(successor, 0, after.vertical_pos);
let valid = |para: &Paragraph| {
!para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& !para.line_segs.is_empty()
&& para.line_segs.iter().all(|line| {
line.line_height > 0 && line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
};
if !valid(previous)
|| !valid(host)
|| !valid(successor)
|| !matches!(previous.column_type, ColumnBreakType::None | ColumnBreakType::Page)
|| host.column_type != ColumnBreakType::None
|| !matches!(successor.column_type, ColumnBreakType::None | ColumnBreakType::Page)
|| !previous.controls.is_empty()
|| !successor.controls.is_empty()
|| para_has_non_whitespace_text(host)
|| !matches!(host.controls.as_slice(), [Control::Table(_)])
|| anchor_vpos <= 0
|| before_vpos < 0
|| after_vpos != 0
|| table.common.treat_as_char
|| !table.common.flow_with_text
|| !is_para_topbottom_float(&table.common)
|| !matches!(table.common.horz_rel_to, HorzRelTo::Column | HorzRelTo::Para)
|| !matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.caption.is_some()
|| signed_hwpunit(table.common.vertical_offset) < 0
|| table.common.height == 0
|| !measured_height.is_finite()
|| dpi <= 0.0
|| (measured_height * 7200.0 / dpi).round() != f64::from(table.common.height)
{
return None;
}
let frame_start_hu =
i64::from(before_vpos) + i64::from(before.line_height) + i64::from(before.line_spacing);
let frame_end_hu = frame_start_hu
+ i64::from(signed_hwpunit(table.common.vertical_offset))
+ i64::from(table.outer_margin_top)
+ i64::from(table.common.height)
+ i64::from(table.outer_margin_bottom);
if ![frame_start_hu, frame_end_hu].contains(&i64::from(anchor_vpos))
|| (i64::from(anchor_vpos) != frame_start_hu && !host.text.is_empty())
{
return None;
}
let anchor_y = hwpunit_to_px(i32::try_from(frame_start_hu).ok()?, dpi);
let table_top = anchor_y
+ hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi)
+ hwpunit_to_px(i32::from(table.outer_margin_top), dpi);
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::NextLine,
anchor_y,
stored_host_origin: Some(anchor_y),
stored_successor_line_origin: None,
table_left: None,
table_top,
occupied_bottom: table_top
+ measured_height
+ hwpunit_to_px(i32::from(table.outer_margin_bottom), dpi),
})
}
pub(crate) fn stored_rowbreak_closing_frame_height(
host: &Paragraph,
successor: &Paragraph,
table: &Table,
dpi: f64,
) -> Option<f64> {
use crate::model::paragraph::LineSeg;
let before = host.line_segs.first()?;
let after = successor.line_segs.first()?;
if host.stored_text_partition_is_dirty()
|| successor.stored_text_partition_is_dirty()
|| host
.line_segs
.iter()
.chain(&successor.line_segs)
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
|| !successor.controls.is_empty()
|| after.vertical_pos <= 0
|| before.vertical_pos <= after.vertical_pos
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.row_count <= 1
|| table.common.height == 0
|| table.caption.is_some()
{
return None;
}
let rows = table.get_raw_row_heights();
if rows.len() != usize::from(table.row_count) || rows.contains(&0) {
return None;
}
let whole = rows.iter().map(|height| i64::from(*height)).sum::<i64>()
+ i64::from(table.cell_spacing) * (rows.len() - 1) as i64;
let remaining = whole - i64::from(table.common.height);
let margins = i64::from(table.outer_margin_top) + i64::from(table.outer_margin_bottom);
(remaining > 0 && remaining + margins == i64::from(after.vertical_pos))
.then(|| remaining as f64 * dpi / 7200.0)
}
pub(crate) fn stored_single_cell_closing_frame_bottom(
host: &Paragraph,
successor: &Paragraph,
table: &Table,
dpi: f64,
) -> Option<f64> {
use crate::model::paragraph::LineSeg;
let [cell] = table.cells.as_slice() else {
return None;
};
let [next] = successor.line_segs.as_slice() else {
return None;
};
if table.row_count != 1
|| table.col_count != 1
|| cell.row_span != 1
|| cell.col_span != 1
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.caption.is_some()
|| table.common.height == 0
|| table.common.height >= cell.height
|| cell.height > i32::MAX as u32
|| !host.text.is_empty()
|| !matches!(host.controls.as_slice(), [Control::Table(_)])
|| !successor.text.is_empty()
|| !successor.controls.is_empty()
|| host.stored_text_partition_is_dirty()
|| successor.stored_text_partition_is_dirty()
|| host.cell_format_vpos_dirty
|| successor.cell_format_vpos_dirty
|| next.vertical_pos <= 0
|| host.line_segs.is_empty()
|| host
.line_segs
.iter()
.chain(&successor.line_segs)
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
{
return None;
}
if table.outer_margin_bottom == 0 {
return Some(hwpunit_to_px(next.vertical_pos, dpi));
}
stored_closed_cell_frame::terminal_cell_border_before_successor_hu(table, successor)
.map(|bottom| hwpunit_to_px(bottom, dpi))
.or_else(|| {
stored_two_frame_successor_origin_hu(table, successor)?;
stored_single_cell_closing_frame_height(host, successor, table, dpi)
})
}
pub(crate) fn stored_single_cell_closing_frame_height(
host: &Paragraph,
successor: &Paragraph,
table: &Table,
dpi: f64,
) -> Option<f64> {
use crate::model::paragraph::LineSeg;
let [cell] = table.cells.as_slice() else {
return None;
};
let [next] = successor.line_segs.as_slice() else {
return None;
};
if table.row_count != 1
|| table.col_count != 1
|| cell.row_span != 1
|| cell.col_span != 1
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.caption.is_some()
|| table.common.height == 0
|| table.common.height >= cell.height
|| cell.height > i32::MAX as u32
|| !host.text.trim().is_empty()
|| !matches!(host.controls.as_slice(), [Control::Table(_)])
|| !successor.text.trim().is_empty()
|| !successor.controls.is_empty()
|| cell.dirty_flag
|| host.stored_text_partition_is_dirty()
|| successor.stored_text_partition_is_dirty()
|| host.cell_format_vpos_dirty
|| successor.cell_format_vpos_dirty
|| next.vertical_pos <= 0
|| host.line_segs.is_empty()
|| host
.line_segs
.iter()
.chain(&successor.line_segs)
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
{
return None;
}
if table.outer_margin_bottom > 0 {
let remaining = i64::from(cell.height) - i64::from(table.common.height);
let outer = i64::from(table.outer_margin_top) + i64::from(table.outer_margin_bottom);
if remaining + outer != i64::from(next.vertical_pos) {
return None;
}
Some(hwpunit_to_px(
next.vertical_pos
.checked_sub(i32::from(table.outer_margin_bottom))?,
dpi,
))
} else {
Some(hwpunit_to_px(next.vertical_pos, dpi))
}
}
pub(crate) fn stored_cumulative_rowbreak_opening_frame_height(
host: &Paragraph,
successor: &Paragraph,
table: &Table,
dpi: f64,
) -> Option<f64> {
let first = host.line_segs.first()?;
let next = successor.line_segs.first()?;
if host.stored_text_partition_is_dirty()
|| successor.stored_text_partition_is_dirty()
|| !para_has_non_whitespace_text(host)
|| !matches!(host.controls.as_slice(), [Control::Table(_)])
|| !successor.text.is_empty()
|| !successor.controls.is_empty()
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.row_count <= 1
|| table.common.height == 0
|| table.common.height > i32::MAX as u32
{
return None;
}
let offset = signed_hwpunit(table.common.vertical_offset);
let expected =
i64::from(first.vertical_pos) + i64::from(offset) + i64::from(table.common.height);
(offset > 0 && expected == i64::from(next.vertical_pos))
.then(|| hwpunit_to_px(table.common.height as i32, dpi))
}
pub(crate) fn stored_terminal_rowbreak_outer_margin_px(
host: &Paragraph,
successor: &Paragraph,
table: &Table,
fragment_bottom: f64,
dpi: f64,
) -> Option<f64> {
stored_terminal_rowbreak_outer_margin_with_consumed_host_px(
host,
successor,
table,
fragment_bottom,
false,
dpi,
)
}
pub(crate) fn stored_terminal_rowbreak_outer_margin_with_consumed_host_px(
host: &Paragraph,
successor: &Paragraph,
table: &Table,
fragment_bottom: f64,
host_consumed_in_previous_frame: bool,
dpi: f64,
) -> Option<f64> {
use crate::model::paragraph::LineSeg;
let line = successor.line_segs.first()?;
let consumed_host = host_consumed_in_previous_frame
&& host
.controls
.iter()
.filter(|c| matches!(c, Control::Table(_)))
.count()
== 1
&& host.controls.iter().all(|c| {
matches!(
c,
Control::Table(_) | Control::Footnote(_) | Control::Endnote(_)
)
})
&& successor.text.is_empty()
&& successor.controls.is_empty();
if (!consumed_host
&& (para_has_non_whitespace_text(host)
|| !matches!(host.controls.as_slice(), [Control::Table(_)])))
|| host.stored_text_partition_is_dirty()
|| successor.stored_text_partition_is_dirty()
|| host
.line_segs
.iter()
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos <= 0
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.outer_margin_bottom == 0
{
return None;
}
let margin = hwpunit_to_px(i32::from(table.outer_margin_bottom), dpi);
((fragment_bottom + margin - hwpunit_to_px(line.vertical_pos, dpi)).abs() <= 0.5)
.then_some(margin)
}
pub(crate) fn stored_terminal_rowbreak_outer_margin_after_prefix_px(
host: &Paragraph,
successor: &Paragraph,
table: &Table,
successor_start_line: usize,
fragment_bottom: f64,
dpi: f64,
) -> Option<f64> {
let previous = successor
.line_segs
.get(successor_start_line.checked_sub(1)?)?;
let line = successor.line_segs.get(successor_start_line)?;
if !ParagraphFloatPlacement::stored_head_host_lines_are_valid(host, table, 0)
|| !matches!(host.controls.as_slice(), [Control::Table(_)])
|| !successor.controls.is_empty()
|| successor.stored_text_partition_is_dirty()
|| successor.cell_format_vpos_dirty
|| line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos <= 0
|| line.vertical_pos >= previous.vertical_pos
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.outer_margin_bottom == 0
{
return None;
}
let margin = hwpunit_to_px(i32::from(table.outer_margin_bottom), dpi);
((fragment_bottom + margin - hwpunit_to_px(line.vertical_pos, dpi)).abs() <= dpi / 7200.0)
.then_some(margin)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ParagraphFloatPlacement {
pub flow: ParagraphFloatFlow,
pub anchor_y: f64,
pub stored_host_origin: Option<f64>,
pub stored_successor_line_origin: Option<f64>,
pub table_left: Option<f64>,
pub table_top: f64,
pub occupied_bottom: f64,
}
crate::storage::clone_copy!(ParagraphFloatPlacement);
pub(crate) fn captioned_para_table_frame(
table: &Table,
outer_top: f64,
before: f64,
measured_height: f64,
after: f64,
) -> Option<ParagraphFloatPlacement> {
let caption = table.caption.as_ref()?;
if !is_para_topbottom_float(&table.common)
|| !table.common.flow_with_text
|| !matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
|| !matches!(
caption.direction,
crate::model::shape::CaptionDirection::Top
| crate::model::shape::CaptionDirection::Bottom
)
|| caption.paragraphs.is_empty()
|| ![outer_top, before, measured_height, after]
.iter()
.all(|value| value.is_finite())
|| measured_height <= 0.0
{
return None;
}
let table_top = outer_top + before;
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::NextLine,
anchor_y: outer_top,
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: None,
table_top,
occupied_bottom: table_top + measured_height + after,
})
}
pub(crate) fn preceding_control_flow_bottom_px(
placements: &std::collections::HashMap<(usize, usize), ParagraphFloatPlacement>,
inline_placements: &std::collections::HashMap<(usize, usize), InlineBoxPlacement>,
para_index: usize,
) -> Option<f64> {
placements
.iter()
.filter(|((index, _), placement)| {
*index < para_index && placement.advances_paragraph_flow()
})
.map(|(_, placement)| placement.occupied_bottom)
.chain(
inline_placements
.iter()
.filter(|((index, _), _)| *index < para_index)
.filter_map(|(_, placement)| placement.advance_end),
)
.filter(|bottom| bottom.is_finite())
.reduce(f64::max)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ParagraphFloatFlow {
Exclusion,
SplitHost {
prefix_lines: usize,
},
StoredSquareWrap,
EmptyHost,
ComputedEmptyHost {
line_advance: f64,
outer_box_top: f64,
},
NextLine,
CoanchoredTable,
StoredPicture {
next_flow_y: f64,
},
StoredPictureGroup {
next_flow_y: f64,
},
InlineHostPicture {
next_flow_y: f64,
},
AbsolutePageFrame,
}
pub(crate) fn stored_future_paper_shape_frame(
following: &[Paragraph],
reset_line: &crate::model::paragraph::LineSeg,
body: &LayoutRect,
paper_width: f64,
dpi: f64,
) -> Option<(usize, usize, ParagraphFloatPlacement)> {
use crate::model::paragraph::LineSeg;
let reset_vpos = reset_line.vertical_pos;
if reset_vpos <= 0 || !dpi.is_finite() || dpi <= 0.0 {
return None;
}
let valid_line = |line: &LineSeg| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0 && line.line_height > 0
};
let origin = hwpunit_to_px(reset_vpos, dpi);
if origin >= body.height {
return None;
}
let mut result = None;
for (owner, para) in following.iter().enumerate() {
if para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.line_segs.is_empty()
|| !para.line_segs.iter().all(valid_line)
{
continue;
}
for (ci, control) in para.controls.iter().enumerate() {
let Control::Shape(shape) = control else {
continue;
};
let common = shape.common();
if common.treat_as_char
|| !matches!(common.text_wrap, TextWrap::TopAndBottom | TextWrap::Square)
|| common.vert_rel_to != VertRelTo::Paper
|| common.vert_align != VertAlign::Top
|| common.height == 0
|| common.height > i32::MAX as u32
|| common.width == 0
|| common.width > i32::MAX as u32
{
continue;
}
let square_frame = common.text_wrap == TextWrap::Square;
if square_frame {
let full_width = |line: &LineSeg| {
line.column_start == 0
&& (hwpunit_to_px(line.segment_width as i32, dpi) - body.width).abs()
<= dpi / 7200.0
};
if !valid_line(reset_line)
|| !full_width(reset_line)
|| !para
.line_segs
.iter()
.all(|line| line.vertical_pos >= reset_vpos && full_width(line))
|| !para.line_segs.windows(2).all(|lines| {
lines[1].vertical_pos >= lines[0].vertical_pos
&& lines[1].text_start >= lines[0].text_start
})
{
continue;
}
}
let caption = super::composer::shape_caption_for_layout(shape);
if caption.as_ref().is_some_and(|caption| {
caption.paragraphs.iter().any(|p| {
p.stored_text_partition_is_dirty()
|| p.cell_format_vpos_dirty
|| p.line_segs.is_empty()
|| !p.line_segs.iter().all(valid_line)
|| !p.controls.iter().all(|c| {
matches!(
c,
Control::AutoNumber(_) | Control::Field(_) | Control::Hyperlink(_)
)
})
})
}) {
continue;
}
let frame_height = super::composer::captioned_shape_height_px(
hwpunit_to_px(common.height as i32, dpi),
&caption,
dpi,
);
let top_margin = if square_frame {
i32::from(common.margin.top)
} else {
topbottom_shape_top_margin_hu(common)
};
let top = hwpunit_to_px(signed_hwpunit(common.vertical_offset), dpi)
+ hwpunit_to_px(top_margin, dpi);
let bottom = top + frame_height + hwpunit_to_px(i32::from(common.margin.bottom), dpi);
if top < 0.0 || !bottom.is_finite() || (bottom - body.y - origin).abs() > dpi / 7200.0 {
continue;
}
let width = hwpunit_to_px(common.width as i32, dpi);
let offset = hwpunit_to_px(signed_hwpunit(common.horizontal_offset), dpi);
let (ref_x, ref_width) = if common.horz_rel_to == HorzRelTo::Paper {
(0.0, paper_width)
} else {
(body.x, body.width)
};
let mut left = match common.horz_align {
HorzAlign::Left | HorzAlign::Inside => ref_x + offset,
HorzAlign::Center => ref_x + (ref_width - width) / 2.0 + offset,
HorzAlign::Right | HorzAlign::Outside => ref_x + ref_width - width - offset,
};
if square_frame && matches!(common.horz_align, HorzAlign::Left | HorzAlign::Inside) {
left += hwpunit_to_px(i32::from(common.margin.left), dpi);
}
if left + width < body.x || left > body.x + body.width {
continue;
}
if result.is_some() {
return None;
}
result = Some((
owner,
ci,
ParagraphFloatPlacement {
flow: ParagraphFloatFlow::AbsolutePageFrame,
anchor_y: 0.0,
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: square_frame.then_some(left),
table_top: top - body.y,
occupied_bottom: origin,
},
));
}
}
result
}
pub(crate) fn stored_frame_successor_shared_spacing_px(
placements: &std::collections::HashMap<(usize, usize), ParagraphFloatPlacement>,
para_index: usize,
spacing_before: f64,
current_y: f64,
) -> f64 {
if spacing_before <= 0.0 {
return 0.0;
}
placements
.iter()
.find_map(|(&(owner, _), placement)| {
let origin = placement.stored_successor_line_origin?;
(owner.checked_add(1) == Some(para_index) && (origin - current_y).abs() <= 0.5)
.then_some(spacing_before)
})
.unwrap_or(0.0)
}
pub(crate) fn empty_host_float_successor_absorbed_spacing_px(
previous_item: Option<&crate::renderer::pagination::PageItem>,
paragraphs: &[Paragraph],
para_index: usize,
spacing_before: f64,
stored_layout: bool,
hwpx_container: bool,
hwp5_origin_hwpx: bool,
flow_from_body_top: f64,
dpi: f64,
) -> f64 {
use crate::renderer::pagination::PageItem;
if spacing_before <= 0.0 {
return 0.0;
}
let (owner, control_index) = match previous_item {
Some(PageItem::Table {
para_index,
control_index,
}) => (*para_index, *control_index),
Some(PageItem::PartialTable {
para_index,
control_index,
end_row,
is_continuation,
..
}) => {
let complete = paragraphs
.get(*para_index)
.and_then(|host| host.controls.get(*control_index))
.is_some_and(|control| matches!(control, Control::Table(t) if *end_row >= t.row_count as usize));
if !complete {
return 0.0;
}
let missing_line_needs_spacing = hwpx_container
&& (!hwp5_origin_hwpx
|| paragraphs
.get(*para_index)
.is_none_or(|host| host.line_segs.is_empty()));
if *is_continuation
&& paragraphs
.get(para_index.saturating_add(1))
.and_then(|next| next.line_segs.first())
.map_or(missing_line_needs_spacing, |line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY
!= 0
})
{
return 0.0;
}
(*para_index, *control_index)
}
_ => return 0.0,
};
if owner.checked_add(1) != Some(para_index) {
return 0.0;
}
let Some(host) = paragraphs.get(owner) else {
return 0.0;
};
if host
.text
.chars()
.any(|c| !c.is_whitespace() && c > '\u{001F}' && c != '\u{FFFC}')
{
return 0.0;
}
let Some(Control::Table(table)) = host.controls.get(control_index) else {
return 0.0;
};
if !is_para_topbottom_float(&table.common) || signed_hwpunit(table.common.vertical_offset) != 0
{
return 0.0;
}
let outer_bottom = hwpunit_to_px(i32::from(table.outer_margin_bottom), dpi).max(0.0);
if spacing_before > outer_bottom {
return 0.0;
}
let absorbed = spacing_before;
let stored_consumption = stored_layout
.then(|| paragraphs.get(para_index))
.flatten()
.filter(|para| !para.stored_text_partition_is_dirty() && !para.cell_format_vpos_dirty)
.and_then(|para| para.line_segs.first())
.filter(|line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
.map(|line| hwpunit_to_px(line.vertical_pos, dpi))
.filter(|saved_top| *saved_top > 0.0)
.map(|saved_top| flow_from_body_top + spacing_before - saved_top)
.filter(|consumed| (-1.0..=spacing_before + 1.0).contains(consumed));
match stored_consumption {
Some(consumed) if (consumed - absorbed).abs() <= 1.0 => absorbed,
Some(consumed) if consumed.abs() <= 1.0 => 0.0,
Some(consumed) if (consumed - spacing_before).abs() <= 1.0 => spacing_before,
Some(consumed) => consumed.clamp(0.0, spacing_before),
None => absorbed,
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct StoredParagraphSuccessorFlow {
flow_origin_hu: i32,
content_bottom_hu: i32,
flow_end_hu: i32,
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct StoredParagraphFlowProjection {
pub content_bottom: f64,
pub advance_end: f64,
pub paint_end: Option<f64>,
}
impl StoredParagraphSuccessorFlow {
pub(crate) fn project(
self,
current_y: f64,
base_hu: Option<i32>,
anchor_y: f64,
dpi: f64,
) -> Option<StoredParagraphFlowProjection> {
let (flow_origin, content_bottom, flow_end) = if let Some(base) = base_hu {
(
anchor_y + hwpunit_to_px(self.flow_origin_hu.checked_sub(base)?, dpi),
anchor_y + hwpunit_to_px(self.content_bottom_hu.checked_sub(base)?, dpi),
anchor_y + hwpunit_to_px(self.flow_end_hu.checked_sub(base)?, dpi),
)
} else {
(
current_y,
current_y
+ hwpunit_to_px(
self.content_bottom_hu.checked_sub(self.flow_origin_hu)?,
dpi,
),
current_y + hwpunit_to_px(self.flow_end_hu.checked_sub(self.flow_origin_hu)?, dpi),
)
};
(current_y.is_finite()
&& current_y <= flow_origin + hwpunit_to_px(1, dpi)
&& flow_origin <= content_bottom
&& content_bottom <= flow_end)
.then_some(StoredParagraphFlowProjection {
content_bottom,
advance_end: flow_end,
paint_end: base_hu.map(|_| flow_end),
})
}
}
pub(crate) fn stored_terminal_empty_line_gap(
next: &Paragraph,
table: &Table,
styles: &super::style_resolver::ResolvedStyleSet,
profile: crate::model::provenance::LayoutCompatibilityProfile,
flow_end: f64,
dpi: f64,
) -> Option<f64> {
use crate::model::paragraph::LineSeg;
let [line] = next.line_segs.as_slice() else {
return None;
};
if profile.session_edited()
|| !(profile.hwp5_stored_pagination_layout() || profile.hwpx_stored_layout())
|| !is_para_topbottom_float(&table.common)
|| table.outer_margin_bottom <= 0
|| !next.text.trim().is_empty()
|| !next.controls.is_empty()
|| next.stored_text_partition_is_dirty()
|| next.cell_format_vpos_dirty
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
|| !flow_end.is_finite()
|| flow_end < 0.0
{
return None;
}
let source_top = match &next.source_line_seg_vertical_pos {
Some(source) if source.len() == 1 => source[0],
Some(_) => return None,
None => line.vertical_pos,
};
let before = styles
.para_styles
.get(next.para_shape_id as usize)?
.spacing_before;
if source_top < 0 || !before.is_finite() || before < 0.0 {
return None;
}
let before_hu = super::px_to_hwpunit(before, dpi);
let cursor_hu = super::px_to_hwpunit(flow_end, dpi);
let margin_hu = i32::from(table.outer_margin_bottom);
let delta =
i64::from(source_top) - i64::from(before_hu) - i64::from(cursor_hu) - i64::from(margin_hu);
(delta.abs() <= 1).then(|| hwpunit_to_px(margin_hu, dpi))
}
pub(crate) fn stored_table_column_origin(
para: &Paragraph,
next: Option<&Paragraph>,
has_host_line: bool,
placement: Option<&ParagraphFloatPlacement>,
dpi: f64,
) -> Option<i32> {
let source = para.line_segs.first()?.vertical_pos;
if has_host_line && super::composer::stored_tac_host_frame(para).is_none() {
placement
.and_then(|placement| placement.stored_host_origin)
.map(|origin| source.saturating_sub(super::px_to_hwpunit(origin, dpi)))
} else {
Some(stored_textless_float_box_origin(para, next, source).unwrap_or(source))
}
}
fn stored_textless_float_box_origin(
para: &Paragraph,
next: Option<&Paragraph>,
source: i32,
) -> Option<i32> {
let [Control::Table(table)] = para.controls.as_slice() else {
return None;
};
if !textless_single_float_host(para, table)
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| table.common.vertical_offset != 0
|| table.common.height >= 0x8000_0000
{
return None;
}
let next = next?;
let next_line = next.line_segs.first().filter(|line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})?;
if next.stored_text_partition_is_dirty() || next.cell_format_vpos_dirty {
return None;
}
let frame = i64::from(table.common.vertical_offset)
+ i64::from(table.outer_margin_top)
+ i64::from(table.common.height)
+ stored_empty_host_frame::stored_bottom_caption_hu(table)?
+ i64::from(table.outer_margin_bottom);
let origin = i64::from(next_line.vertical_pos) - frame;
((0..i64::from(source)).contains(&origin)
&& i64::from(source) < origin + i64::from(table.outer_margin_top))
.then(|| i32::try_from(origin).ok())
.flatten()
}
pub(crate) fn stored_margin_page_restart(
previous: &Paragraph,
current: &Paragraph,
next: Option<&Paragraph>,
styles: &super::style_resolver::ResolvedStyleSet,
frame_origin_hu: i32,
actual_previous_end: f64,
dpi: f64,
) -> bool {
use crate::model::paragraph::LineSeg;
let Some(last) = previous.line_segs.last() else {
return false;
};
let Some(first) = current.line_segs.first() else {
return false;
};
if frame_origin_hu != 0
|| previous.stored_text_partition_is_dirty()
|| previous.cell_format_vpos_dirty
|| current.stored_text_partition_is_dirty()
|| current.cell_format_vpos_dirty
|| !previous.controls.is_empty()
|| !current.controls.is_empty()
|| current.text.trim().is_empty()
|| last.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| last.line_height <= 0
|| last.line_spacing < 0
|| first.vertical_pos <= 0
|| first.vertical_pos >= last.vertical_pos
|| !super::layout::stored_first_margin_is_page_relative(current, next, styles, dpi)
{
return false;
}
let after = styles
.para_styles
.get(previous.para_shape_id as usize)
.map_or(0.0, |style| style.spacing_after);
let source_end =
i64::from(last.vertical_pos) + i64::from(last.line_height) + i64::from(last.line_spacing)
- i64::from(frame_origin_hu);
source_end >= 0
&& source_end <= i64::from(i32::MAX)
&& actual_previous_end.is_finite()
&& after.is_finite()
&& after >= 0.0
&& (hwpunit_to_px(source_end as i32, dpi) + after - actual_previous_end).abs()
<= hwpunit_to_px(1, dpi)
}
pub(crate) fn stored_paragraph_successor_flow(
para: &Paragraph,
next: &Paragraph,
styles: &super::style_resolver::ResolvedStyleSet,
profile: crate::model::provenance::LayoutCompatibilityProfile,
dpi: f64,
) -> Option<StoredParagraphSuccessorFlow> {
use crate::model::paragraph::LineSeg;
use stored_column_spacing::source_line_position;
let text_before_flow_table = para.controls.is_empty()
&& !para.text.trim().is_empty()
&& next.text.trim().is_empty()
&& next.controls.iter().any(|control| {
matches!(control, Control::Table(table)
if is_para_topbottom_float(&table.common)
|| (table.common.treat_as_char
&& super::layout::LayoutEngine::tac_stored_band_is_outer_box(next, table)))
});
let inline_shape_before_text = matches!(para.controls.as_slice(), [Control::Shape(_)])
&& stored_column_spacing::closed_inline_object_line(para)
&& next.controls.is_empty()
&& !next.text.trim().is_empty()
&& next.column_type == crate::model::paragraph::ColumnBreakType::None;
if profile.session_edited()
|| !(profile.hwp5_stored_pagination_layout() || profile.hwpx_stored_layout())
|| !(text_before_flow_table || inline_shape_before_text)
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| next.stored_text_partition_is_dirty()
|| next.cell_format_vpos_dirty
|| para.line_segs.iter().any(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos < 0
|| line.line_height <= 0
|| line.line_spacing < 0
})
|| para.line_segs.windows(2).any(|pair| {
pair[0]
.vertical_pos
.checked_add(pair[0].line_height)
.is_none_or(|bottom| bottom > pair[1].vertical_pos)
})
{
return None;
}
let last = para.line_segs.last()?;
let after = next.line_segs.first()?;
if after.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0 {
return None;
}
let own_style = styles.para_styles.get(para.para_shape_id as usize)?;
let spacing_before = own_style.spacing_before;
let spacing_after = own_style.spacing_after;
let next_before = styles
.para_styles
.get(next.para_shape_id as usize)?
.spacing_before;
let hu_per_px = 1.0 / hwpunit_to_px(1, dpi);
let flow_origin_hu =
source_line_position(para, 0)?.checked_sub((spacing_before * hu_per_px).round() as i32)?;
let content_bottom_hu = source_line_position(para, para.line_segs.len().checked_sub(1)?)?
.checked_add(last.line_height)?;
let flow_end_hu = content_bottom_hu
.checked_add(last.line_spacing)?
.checked_add((spacing_after * hu_per_px).round() as i32)?;
(spacing_before.is_finite()
&& spacing_before >= 0.0
&& spacing_after.is_finite()
&& spacing_after >= 0.0
&& next_before.is_finite()
&& next_before >= 0.0
&& flow_end_hu.checked_add((next_before * hu_per_px).round() as i32)
== Some(source_line_position(next, 0)?))
.then_some(StoredParagraphSuccessorFlow {
flow_origin_hu,
content_bottom_hu,
flow_end_hu,
})
}
pub(crate) fn stored_two_frame_successor_origin_hu(
table: &Table,
successor: &Paragraph,
) -> Option<i32> {
let [cell] = table.cells.as_slice() else {
return None;
};
let [line] = successor.line_segs.as_slice() else {
return None;
};
if cell.dirty_flag
|| table.row_count != 1
|| table.col_count != 1
|| table.common.height == 0
|| cell.height > i32::MAX as u32
|| successor.stored_text_partition_is_dirty()
|| successor.cell_format_vpos_dirty
|| !successor.controls.is_empty()
|| !successor.text.trim().is_empty()
|| line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos <= 0
{
return None;
}
let mut previous = None;
let mut resets = 0;
let mut flat_index = 0;
for para in &cell.paragraphs {
if para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.line_segs.is_empty()
{
return None;
}
for (index, seg) in para.line_segs.iter().enumerate() {
if seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| seg.vertical_pos < 0
|| seg.line_height <= 0
{
return None;
}
let zero_restart = index == 1
&& para.line_segs[0].vertical_pos == 0
&& seg.vertical_pos == 0
&& para.line_segs.get(2).is_some_and(|next| {
next.vertical_pos == seg.line_height.saturating_add(seg.line_spacing.max(0))
});
if previous.is_some_and(|pos| seg.vertical_pos < pos)
|| zero_restart
|| previous.is_some_and(|pos| flat_index == 1 && pos == 0 && seg.vertical_pos == 0)
{
if seg.vertical_pos != 0 {
return None;
}
resets += 1;
}
previous = Some(seg.vertical_pos);
flat_index += 1;
}
}
let extent = i64::from(table.common.height) + i64::from(line.vertical_pos)
- i64::from(table.outer_margin_top)
- i64::from(table.outer_margin_bottom);
(resets == 1 && extent == i64::from(cell.height)).then_some(line.vertical_pos)
}
#[derive(Debug, Clone, Copy)]
pub struct ParagraphHostLine {
pub char_start: usize,
pub top: f64,
pub height: f64,
}
pub fn stored_picture_successor_placement(
para: &Paragraph,
successor: &Paragraph,
spacing_before: f64,
successor_spacing_before: f64,
frame_vpos: i32,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
stored_picture_successor_with_following_placement(
para,
successor,
None,
spacing_before,
0.0,
successor_spacing_before,
frame_vpos,
f64::NAN,
None,
dpi,
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn stored_picture_successor_with_following_placement(
para: &Paragraph,
successor: &Paragraph,
following: Option<(&Paragraph, f64)>,
spacing_before: f64,
spacing_after: f64,
successor_spacing_before: f64,
frame_vpos: i32,
actual_host_flow_y: f64,
actual_host_flow_end: Option<f64>,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
if let Some(placement) = stored_captioned_picture_successor_placement(
para,
successor,
spacing_before,
successor_spacing_before,
frame_vpos,
actual_host_flow_y,
dpi,
) {
return Some(placement);
}
let [Control::Picture(picture)] = para.controls.as_slice() else {
return None;
};
let [host] = para.line_segs.as_slice() else {
return None;
};
let next = successor.line_segs.first()?;
let common = &picture.common;
let independent_host_line = host.segment_width > 0;
if (host.segment_width == 0 && para.text.chars().any(|c| c > '\u{001f}' && c != '\u{fffc}'))
|| host.segment_width < 0
|| para.stored_text_partition_is_dirty()
|| successor.stored_text_partition_is_dirty()
|| !successor.controls.is_empty()
|| successor.column_type != crate::model::paragraph::ColumnBreakType::None
|| host.tag & 0x8000_0000 != 0
|| next.tag & 0x8000_0000 != 0
|| host.line_height <= 0
|| next.line_height <= 0
|| common.treat_as_char
|| common.text_wrap != TextWrap::TopAndBottom
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| common.margin.top != 0
|| picture.caption.is_some()
|| !dpi.is_finite()
|| dpi <= 0.0
|| !spacing_before.is_finite()
|| !spacing_after.is_finite()
|| spacing_after < 0.0
|| !successor_spacing_before.is_finite()
|| successor_spacing_before < 0.0
{
return None;
}
let host_y = hwpunit_to_px(host.vertical_pos.checked_sub(frame_vpos)?, dpi);
let anchor_y = host_y - spacing_before;
let top = anchor_y + hwpunit_to_px(signed_hwpunit(common.vertical_offset), dpi);
let (_, height) = picture_flow_frame_size_hu(picture);
let bottom =
top + hwpunit_to_px(height, dpi) + hwpunit_to_px(i32::from(common.margin.bottom), dpi);
if independent_host_line
&& (!actual_host_flow_y.is_finite()
|| (actual_host_flow_y - anchor_y).abs() > dpi / 7200.0
|| !actual_host_flow_end
.is_some_and(|end| end.is_finite() && end <= top + dpi / 7200.0))
{
return None;
}
let next_y = hwpunit_to_px(next.vertical_pos.checked_sub(frame_vpos)?, dpi);
let successor_has_text = !successor.text.is_empty();
if successor_spacing_before == 0.0 || successor_has_text || independent_host_line {
let (following, following_spacing_before) = following?;
let after = following.line_segs.first()?;
let advance = next.line_height.checked_add(next.line_spacing)?;
let advance_px = hwpunit_to_px(advance, dpi) + following_spacing_before;
let after_px = hwpunit_to_px(after.vertical_pos.checked_sub(next.vertical_pos)?, dpi);
if successor.line_segs.len() != 1
|| following.column_type != crate::model::paragraph::ColumnBreakType::None
|| after.tag & 0x8000_0000 != 0
|| advance <= 0
|| !following_spacing_before.is_finite()
|| following_spacing_before < 0.0
|| (after_px - advance_px).abs() > dpi / 7200.0
|| !actual_host_flow_y.is_finite()
|| (actual_host_flow_y - anchor_y).abs() > dpi / 7200.0
{
return None;
}
}
if anchor_y < 0.0 || height <= 0 || (bottom + spacing_after - next_y).abs() > dpi / 7200.0 {
return None;
}
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::StoredPicture {
next_flow_y: next_y - successor_spacing_before,
},
anchor_y,
stored_host_origin: Some(host_y),
stored_successor_line_origin: None,
table_left: None,
table_top: top,
occupied_bottom: bottom,
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn stored_picture_group_frame_placement(
para: &Paragraph,
successor: &Paragraph,
spacing_before: f64,
successor_spacing_before: f64,
frame_vpos: i32,
actual_host_flow_y: f64,
dpi: f64,
) -> Option<Vec<ParagraphFloatPlacement>> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
let [host] = para.line_segs.as_slice() else {
return None;
};
let next = successor.line_segs.first()?;
if para.controls.len() < 2
|| para.stored_text_partition_is_dirty()
|| successor.stored_text_partition_is_dirty()
|| [host, next].iter().any(|line| {
line.line_height <= 0 || line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
})
|| !matches!(
successor.column_type,
ColumnBreakType::None | ColumnBreakType::Page
)
|| ![
dpi,
spacing_before,
successor_spacing_before,
actual_host_flow_y,
]
.iter()
.all(|value| value.is_finite())
|| dpi <= 0.0
|| spacing_before < 0.0
|| successor_spacing_before < 0.0
{
return None;
}
let host_y = hwpunit_to_px(host.vertical_pos.checked_sub(frame_vpos)?, dpi);
let anchor_y = host_y - spacing_before;
let unit = dpi / 7200.0;
if anchor_y < 0.0 || (actual_host_flow_y - anchor_y).abs() > unit {
return None;
}
let mut bottom = host_y + hwpunit_to_px(host.line_height, dpi);
let mut tops = Vec::with_capacity(para.controls.len());
for control in ¶.controls {
let Control::Picture(picture) = control else {
return None;
};
let (height, caption_space) = stored_group_picture_height(picture, dpi)?;
let top = anchor_y + hwpunit_to_px(signed_hwpunit(picture.common.vertical_offset), dpi);
let text_bottom = host_y + hwpunit_to_px(host.line_height, dpi);
if top < anchor_y || (para_has_non_whitespace_text(para) && top < text_bottom) {
return None;
}
let picture_bottom = top + height + caption_space;
bottom = bottom.max(picture_bottom);
tops.push(top);
}
let successor_origin = if successor.column_type == ColumnBreakType::None {
let next_y = hwpunit_to_px(next.vertical_pos.checked_sub(frame_vpos)?, dpi);
if (bottom - next_y).abs() > unit {
return None;
}
Some(next_y)
} else {
None
};
Some(
tops.into_iter()
.map(|top| ParagraphFloatPlacement {
flow: ParagraphFloatFlow::StoredPictureGroup {
next_flow_y: bottom
- successor_origin.map_or(0.0, |_| successor_spacing_before),
},
anchor_y,
stored_host_origin: Some(host_y),
stored_successor_line_origin: successor_origin,
table_left: None,
table_top: top,
occupied_bottom: bottom,
})
.collect(),
)
}
fn stored_group_picture_height(picture: &Picture, dpi: f64) -> Option<(f64, f64)> {
use crate::model::paragraph::LineSeg;
use crate::model::shape::CaptionDirection;
let common = &picture.common;
if common.treat_as_char
|| !common.flow_with_text
|| common.allow_overlap
|| common.text_wrap != TextWrap::TopAndBottom
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| common.margin.top != 0
{
return None;
}
let caption_spacing = if let Some(caption) = &picture.caption {
if caption.direction != CaptionDirection::Bottom
|| caption.spacing < 0
|| caption.paragraphs.is_empty()
|| caption.paragraphs.iter().any(|p| {
p.stored_text_partition_is_dirty()
|| p.cell_format_vpos_dirty
|| p.line_segs.is_empty()
|| !p
.controls
.iter()
.all(|c| matches!(c, Control::AutoNumber(_)))
|| p.line_segs.iter().any(|line| {
line.line_height <= 0
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
})
})
{
return None;
}
hwpunit_to_px(i32::from(caption.spacing), dpi)
} else {
0.0
};
let (width, height) = picture_flow_frame_size_hu(picture);
let caption_height = super::composer::caption_height_px(&picture.caption, dpi);
if width <= 0 || height <= 0 || !caption_height.is_finite() {
return None;
}
Some((
hwpunit_to_px(height, dpi) + hwpunit_to_px(i32::from(common.margin.bottom), dpi),
caption_spacing + caption_height,
))
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn stored_picture_group_before_host_placement(
previous: &Paragraph,
para: &Paragraph,
successor: &Paragraph,
spacing_after: f64,
successor_spacing_before: f64,
frame_vpos: i32,
actual_host_flow_y: f64,
dpi: f64,
) -> Option<Vec<ParagraphFloatPlacement>> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
let [host] = para.line_segs.as_slice() else {
return None;
};
let prev = previous.line_segs.last()?;
let next = successor.line_segs.first()?;
if para.controls.len() < 2
|| para_has_non_whitespace_text(para)
|| [previous, para, successor]
.iter()
.any(|p| p.stored_text_partition_is_dirty())
|| para.column_type != ColumnBreakType::None
|| successor.column_type != ColumnBreakType::None
|| [prev, host, next].iter().any(|line| {
line.line_height <= 0 || line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
})
|| ![
dpi,
spacing_after,
successor_spacing_before,
actual_host_flow_y,
]
.iter()
.all(|value| value.is_finite())
|| dpi <= 0.0
|| spacing_after < 0.0
|| successor_spacing_before < 0.0
{
return None;
}
let prev_end = prev
.vertical_pos
.checked_add(prev.line_height)?
.checked_add(prev.line_spacing)?;
let top = hwpunit_to_px(prev_end.checked_sub(frame_vpos)?, dpi);
let host_y = hwpunit_to_px(host.vertical_pos.checked_sub(frame_vpos)?, dpi);
let next_y = hwpunit_to_px(next.vertical_pos.checked_sub(frame_vpos)?, dpi);
let next_flow_y = next_y - successor_spacing_before;
let host_end = host_y
+ hwpunit_to_px(host.line_height.checked_add(host.line_spacing)?, dpi)
+ spacing_after;
let unit = dpi / 7200.0;
if top < 0.0 || (top - actual_host_flow_y).abs() > unit || (host_end - next_flow_y).abs() > unit
{
return None;
}
let (max_height, offsets) = stored_parallel_picture_row(para, dpi)?;
if offsets.iter().any(|offset| *offset >= 0) || (top + max_height - host_y).abs() > unit {
return None;
}
Some(
offsets
.into_iter()
.map(|offset| ParagraphFloatPlacement {
flow: ParagraphFloatFlow::StoredPictureGroup { next_flow_y },
anchor_y: top - hwpunit_to_px(offset, dpi),
stored_host_origin: Some(host_y),
stored_successor_line_origin: Some(next_y),
table_left: None,
table_top: top,
occupied_bottom: host_y,
})
.collect(),
)
}
fn stored_parallel_picture_row(para: &Paragraph, dpi: f64) -> Option<(f64, Vec<i32>)> {
if para.controls.len() < 2 {
return None;
}
let mut max_height = 0.0f64;
let mut offsets = Vec::with_capacity(para.controls.len());
let mut intervals = Vec::with_capacity(para.controls.len());
let mut horizontal_reference = None;
for control in ¶.controls {
let Control::Picture(picture) = control else {
return None;
};
let common = &picture.common;
let (height, caption_space) = stored_group_picture_height(picture, dpi)?;
let offset = signed_hwpunit(common.vertical_offset);
if common.horz_align != HorzAlign::Left {
return None;
}
if horizontal_reference.is_some_and(|reference| reference != common.horz_rel_to) {
return None;
}
horizontal_reference = Some(common.horz_rel_to);
let (width, _) = picture_flow_frame_size_hu(picture);
let left =
i64::from(signed_hwpunit(common.horizontal_offset)) - i64::from(common.margin.left);
let right = left
+ i64::from(width)
+ i64::from(common.margin.left)
+ i64::from(common.margin.right);
intervals.push((left, right));
offsets.push(offset);
max_height = max_height.max(height + caption_space);
}
intervals.sort_unstable();
if intervals.windows(2).any(|pair| pair[0].1 > pair[1].0) {
return None;
}
Some((max_height, offsets))
}
#[allow(clippy::too_many_arguments)]
fn stored_captioned_picture_successor_placement(
para: &Paragraph,
successor: &Paragraph,
spacing_before: f64,
successor_spacing_before: f64,
frame_vpos: i32,
actual_host_flow_y: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
use crate::model::shape::CaptionDirection;
let [Control::Picture(picture)] = para.controls.as_slice() else {
return None;
};
let host = super::layout::stored_single_visual_line(para)?;
let next = successor.line_segs.first()?;
let caption = picture.caption.as_ref()?;
let common = &picture.common;
if para_has_non_whitespace_text(para)
|| para.stored_text_partition_is_dirty()
|| successor.stored_text_partition_is_dirty()
|| !successor.controls.is_empty()
|| successor.column_type != ColumnBreakType::None
|| para.cell_format_vpos_dirty
|| successor.cell_format_vpos_dirty
|| para.line_segs.iter().any(|line| {
line.line_height <= 0 || line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
})
|| successor.line_segs.iter().any(|line| {
line.line_height <= 0 || line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
})
|| common.treat_as_char
|| !common.flow_with_text
|| common.allow_overlap
|| common.text_wrap != TextWrap::TopAndBottom
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| common.margin.top != 0
|| !matches!(
caption.direction,
CaptionDirection::Top | CaptionDirection::Bottom
)
|| caption.spacing < 0
|| caption.paragraphs.is_empty()
|| caption.paragraphs.iter().any(|p| {
p.stored_text_partition_is_dirty()
|| p.cell_format_vpos_dirty
|| p.line_segs.is_empty()
|| !p
.controls
.iter()
.all(|c| matches!(c, Control::AutoNumber(_)))
|| p.line_segs.iter().any(|line| {
line.line_height <= 0 || line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
})
})
|| ![
dpi,
spacing_before,
successor_spacing_before,
actual_host_flow_y,
]
.iter()
.all(|value| value.is_finite())
|| dpi <= 0.0
|| spacing_before < 0.0
|| successor_spacing_before < 0.0
{
return None;
}
let host_y = hwpunit_to_px(host.vertical_pos.checked_sub(frame_vpos)?, dpi);
let anchor_y = host_y - spacing_before;
let top = anchor_y + hwpunit_to_px(signed_hwpunit(common.vertical_offset), dpi);
let (_, height) = picture_flow_frame_size_hu(picture);
let caption_height = super::composer::caption_height_px(&picture.caption, dpi);
let bottom = top
+ hwpunit_to_px(height, dpi)
+ hwpunit_to_px(
i32::from(common.margin.bottom) + i32::from(caption.spacing),
dpi,
)
+ caption_height;
let next_y = hwpunit_to_px(next.vertical_pos.checked_sub(frame_vpos)?, dpi);
let unit = dpi / 7200.0;
if anchor_y < 0.0
|| height <= 0
|| caption_height <= 0.0
|| !caption_height.is_finite()
|| (actual_host_flow_y - anchor_y).abs() > unit
|| (bottom - next_y).abs() > unit
{
return None;
}
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::StoredPicture {
next_flow_y: next_y - successor_spacing_before,
},
anchor_y,
stored_host_origin: Some(host_y),
stored_successor_line_origin: Some(next_y),
table_left: None,
table_top: top,
occupied_bottom: bottom,
})
}
pub(crate) fn stored_tail_square_picture_placement(
para: &Paragraph,
successor: &Paragraph,
control_index: usize,
actual_host_flow_y: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
use crate::model::paragraph::LineSeg;
let Control::Picture(picture) = para.controls.get(control_index)? else {
return None;
};
let common = &picture.common;
let valid = |para: &Paragraph| {
!para.stored_text_partition_is_dirty()
&& !para.line_segs.is_empty()
&& para.line_segs.iter().all(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0 && line.line_height > 0
})
&& para.line_segs.windows(2).all(|pair| {
pair[1].vertical_pos >= pair[0].vertical_pos
&& pair[1].text_start >= pair[0].text_start
})
};
if !valid(para)
|| !valid(successor)
|| para.text.trim().is_empty()
|| common.treat_as_char
|| common.text_wrap != TextWrap::Square
|| common.vert_rel_to != VertRelTo::Para
|| common.horz_rel_to != HorzRelTo::Column
|| common.vert_align != VertAlign::Top
|| common.horz_align != HorzAlign::Left
|| !common.flow_with_text
|| !actual_host_flow_y.is_finite()
|| !dpi.is_finite()
|| dpi <= 0.0
{
return None;
}
let owner = *stored_control_line_indices(para)?.get(control_index)?;
if owner == 0 || owner + 1 != para.line_segs.len() {
return None;
}
let first = para.line_segs.first()?;
let line = para.line_segs.get(owner)?;
let top = i64::from(line.vertical_pos) + i64::from(signed_hwpunit(common.vertical_offset));
let left = signed_hwpunit(common.horizontal_offset);
let (width, height) = picture_flow_frame_size_hu(picture);
let right = left.checked_add(width)?;
if width <= 0
|| height <= 0
|| line.vertical_pos <= first.vertical_pos
|| top < i64::from(line.vertical_pos) + i64::from(line.line_height)
|| !successor.line_segs.iter().any(|before| {
let end = i64::from(before.vertical_pos) + i64::from(before.line_height);
i64::from(before.vertical_pos) <= top
&& top < end
&& before.column_start + before.segment_width as i32 == left
&& successor.line_segs.iter().any(|after| {
after.vertical_pos == before.vertical_pos && after.column_start == right
})
})
{
return None;
}
let anchor_y = actual_host_flow_y + hwpunit_to_px(line.vertical_pos - first.vertical_pos, dpi);
let top = anchor_y + hwpunit_to_px(signed_hwpunit(common.vertical_offset), dpi);
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::Exclusion,
anchor_y,
stored_host_origin: Some(actual_host_flow_y),
stored_successor_line_origin: None,
table_left: None,
table_top: top,
occupied_bottom: top + hwpunit_to_px(height, dpi),
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn stored_picture_before_host_placement(
previous: &Paragraph,
para: &Paragraph,
successor: &Paragraph,
spacing_after: f64,
successor_spacing_before: f64,
frame_vpos: i32,
actual_host_flow_y: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
let [Control::Picture(picture)] = para.controls.as_slice() else {
return None;
};
let [host] = para.line_segs.as_slice() else {
return None;
};
let prev = previous.line_segs.last()?;
let next = successor.line_segs.first()?;
let common = &picture.common;
if para.column_type != crate::model::paragraph::ColumnBreakType::None
|| successor.column_type != crate::model::paragraph::ColumnBreakType::None
|| [prev, host, next]
.iter()
.any(|line| line.tag & 0x8000_0000 != 0 || line.line_height <= 0)
|| common.treat_as_char
|| common.text_wrap != TextWrap::TopAndBottom
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| !dpi.is_finite()
|| dpi <= 0.0
|| !actual_host_flow_y.is_finite()
|| !spacing_after.is_finite()
|| !successor_spacing_before.is_finite()
{
return None;
}
let prev_end = prev
.vertical_pos
.checked_add(prev.line_height)?
.checked_add(prev.line_spacing)?;
let anchor_y = hwpunit_to_px(prev_end.checked_sub(frame_vpos)?, dpi);
let host_y = hwpunit_to_px(host.vertical_pos.checked_sub(frame_vpos)?, dpi);
let top = anchor_y + hwpunit_to_px(signed_hwpunit(common.vertical_offset), dpi);
let (_, height) = picture_flow_frame_size_hu(picture);
let bottom = top
+ super::composer::captioned_picture_height_px(picture, dpi)
+ hwpunit_to_px(
i32::from(common.margin.top) + i32::from(common.margin.bottom),
dpi,
);
let host_end = host_y
+ hwpunit_to_px(host.line_height.checked_add(host.line_spacing)?, dpi)
+ spacing_after;
let next_y = hwpunit_to_px(next.vertical_pos.checked_sub(frame_vpos)?, dpi);
let next_flow_y = next_y - successor_spacing_before;
let unit = dpi / 7200.0;
if anchor_y < 0.0
|| top < anchor_y
|| height <= 0
|| (actual_host_flow_y - anchor_y).abs() > unit
|| (bottom - host_y).abs() > unit
|| (host_end - next_flow_y).abs() > unit
{
return None;
}
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::StoredPicture { next_flow_y },
anchor_y,
stored_host_origin: Some(host_y),
stored_successor_line_origin: Some(next_y),
table_left: None,
table_top: top,
occupied_bottom: bottom,
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn stored_picture_empty_host_placement(
previous: &Paragraph,
para: &Paragraph,
successor: &Paragraph,
successor_spacing_before: f64,
frame_vpos: i32,
actual_host_flow_y: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
let [Control::Picture(picture)] = para.controls.as_slice() else {
return None;
};
let [host] = para.line_segs.as_slice() else {
return None;
};
let prev = previous.line_segs.last()?;
let next = successor.line_segs.first()?;
let common = &picture.common;
if para.text.chars().any(|c| c > '\u{001f}' && c != '\u{fffc}')
|| para.column_type != crate::model::paragraph::ColumnBreakType::None
|| successor.column_type != crate::model::paragraph::ColumnBreakType::None
|| [prev, host, next]
.iter()
.any(|line| line.tag & 0x8000_0000 != 0 || line.line_height <= 0)
|| common.treat_as_char
|| common.text_wrap != TextWrap::TopAndBottom
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| picture.caption.is_some()
|| !dpi.is_finite()
|| dpi <= 0.0
|| !actual_host_flow_y.is_finite()
|| !successor_spacing_before.is_finite()
{
return None;
}
let prev_end = prev
.vertical_pos
.checked_add(prev.line_height)?
.checked_add(prev.line_spacing)?;
if prev_end != host.vertical_pos {
return None;
}
let host_y = hwpunit_to_px(host.vertical_pos.checked_sub(frame_vpos)?, dpi);
let top = host_y + hwpunit_to_px(signed_hwpunit(common.vertical_offset), dpi);
let (_, height) = picture_flow_frame_size_hu(picture);
let bottom = top
+ hwpunit_to_px(height, dpi)
+ hwpunit_to_px(
i32::from(common.margin.top) + i32::from(common.margin.bottom),
dpi,
);
let next_y = hwpunit_to_px(next.vertical_pos.checked_sub(frame_vpos)?, dpi);
let unit = dpi / 7200.0;
if host_y < 0.0
|| top < host_y
|| height <= 0
|| (actual_host_flow_y - host_y).abs() > unit
|| (bottom - next_y).abs() > unit
{
return None;
}
Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::StoredPicture {
next_flow_y: next_y - successor_spacing_before,
},
anchor_y: host_y,
stored_host_origin: Some(host_y),
stored_successor_line_origin: Some(next_y),
table_left: None,
table_top: top,
occupied_bottom: bottom,
})
}
pub(crate) fn stored_picture_next_page_placement(
para: &Paragraph,
following: &[Paragraph],
frame_vpos: i32,
actual_host_flow_y: f64,
available_height: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
let [Control::Picture(picture)] = para.controls.as_slice() else {
return None;
};
let [host] = para.line_segs.as_slice() else {
return None;
};
let common = &picture.common;
if para.text.chars().any(|c| c > '\u{001f}' && c != '\u{fffc}')
|| host.tag & 0x8000_0000 != 0
|| host.line_height <= 0
|| common.treat_as_char
|| common.allow_overlap
|| !common.flow_with_text
|| common.text_wrap != TextWrap::TopAndBottom
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| picture.caption.is_some()
|| !dpi.is_finite()
|| dpi <= 0.0
|| !available_height.is_finite()
|| !actual_host_flow_y.is_finite()
{
return None;
}
let host_y = hwpunit_to_px(host.vertical_pos.checked_sub(frame_vpos)?, dpi);
let offset = hwpunit_to_px(signed_hwpunit(common.vertical_offset), dpi);
let (_, height) = picture_flow_frame_size_hu(picture);
let frame_height = height
.checked_add(i32::from(common.margin.top))?
.checked_add(i32::from(common.margin.bottom))?;
let bottom = hwpunit_to_px(frame_height, dpi);
if host_y < 0.0
|| offset < 0.0
|| frame_height <= 0
|| bottom > available_height
|| (actual_host_flow_y - host_y).abs() > dpi / 7200.0
|| host_y + offset + bottom <= available_height
{
return None;
}
let mut previous_end = host.vertical_pos;
for next in following {
if !next.controls.is_empty() {
return None;
}
let first = next.line_segs.first()?;
if next
.line_segs
.iter()
.any(|line| line.tag & 0x8000_0000 != 0 || line.line_height <= 0)
{
return None;
}
if first.vertical_pos < previous_end {
if first.vertical_pos != frame_height
|| !next.text.chars().any(|c| c > '\u{001f}' && c != '\u{fffc}')
{
return None;
}
return Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::StoredPicture {
next_flow_y: bottom,
},
anchor_y: -offset,
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: None,
table_top: 0.0,
occupied_bottom: bottom,
});
}
previous_end = next.line_segs.last()?.vertical_pos;
}
None
}
pub(crate) fn stored_picture_group_next_page_placements(
para: &Paragraph,
following: &[Paragraph],
frame_vpos: i32,
actual_host_flow_y: f64,
available_height: f64,
dpi: f64,
) -> Option<Vec<ParagraphFloatPlacement>> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
let [host] = para.line_segs.as_slice() else {
return None;
};
if para_has_non_whitespace_text(para)
|| para.stored_text_partition_is_dirty()
|| host.line_height <= 0
|| host.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| ![dpi, actual_host_flow_y, available_height]
.iter()
.all(|v| v.is_finite())
|| dpi <= 0.0
{
return None;
}
let (height, offsets) = stored_parallel_picture_row(para, dpi)?;
if offsets.iter().copied().max() != Some(0) {
return None;
}
let host_y = hwpunit_to_px(host.vertical_pos.checked_sub(frame_vpos)?, dpi);
let unit = dpi / 7200.0;
if host_y < 0.0
|| height > available_height
|| host_y + height <= available_height
|| (actual_host_flow_y - host_y).abs() > unit
{
return None;
}
let mut previous = host;
for next in following {
if !next.controls.is_empty()
|| next.stored_text_partition_is_dirty()
|| next.column_type != ColumnBreakType::None
|| next.line_segs.is_empty()
|| next.line_segs.iter().any(|line| {
line.line_height <= 0 || line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
})
{
return None;
}
let first = &next.line_segs[0];
if first.vertical_pos < previous.vertical_pos {
if (hwpunit_to_px(first.vertical_pos, dpi) - height).abs() > unit {
return None;
}
return Some(
offsets
.into_iter()
.map(|offset| ParagraphFloatPlacement {
flow: ParagraphFloatFlow::StoredPictureGroup {
next_flow_y: height,
},
anchor_y: -hwpunit_to_px(offset, dpi),
stored_host_origin: None,
stored_successor_line_origin: Some(height),
table_left: None,
table_top: 0.0,
occupied_bottom: height,
})
.collect(),
);
}
for line in &next.line_segs {
let end = previous
.vertical_pos
.checked_add(previous.line_height)?
.checked_add(previous.line_spacing)?;
if line.vertical_pos != end {
return None;
}
previous = line;
}
}
None
}
pub(crate) fn stored_background_picture_next_page_placement(
para: &Paragraph,
following: &[Paragraph],
frame_vpos: i32,
actual_host_flow_y: f64,
available_height: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
use crate::model::paragraph::LineSeg;
let [Control::Picture(picture)] = para.controls.as_slice() else {
return None;
};
let [host] = para.line_segs.as_slice() else {
return None;
};
let common = &picture.common;
if para_has_non_whitespace_text(para)
|| para.stored_text_partition_is_dirty()
|| host.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| host.line_height <= 0
|| common.treat_as_char
|| !common.allow_overlap
|| !common.flow_with_text
|| common.text_wrap != TextWrap::BehindText
|| common.vert_rel_to != VertRelTo::Para
|| common.vert_align != VertAlign::Top
|| picture.caption.is_some()
|| !dpi.is_finite()
|| dpi <= 0.0
|| !actual_host_flow_y.is_finite()
|| !available_height.is_finite()
{
return None;
}
let host_y = hwpunit_to_px(host.vertical_pos.checked_sub(frame_vpos)?, dpi);
let offset = hwpunit_to_px(signed_hwpunit(common.vertical_offset), dpi);
let (_, height) = picture_flow_frame_size_hu(picture);
let picture_height = hwpunit_to_px(height, dpi);
if host_y < 0.0
|| offset < 0.0
|| height <= 0
|| offset + picture_height > available_height
|| (actual_host_flow_y - host_y).abs() > dpi / 7200.0
|| host_y + offset + picture_height <= available_height
{
return None;
}
let mut previous = host;
for next in following {
let [line] = next.line_segs.as_slice() else {
return None;
};
if next.stored_text_partition_is_dirty()
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
|| para_has_non_whitespace_text(next)
{
return None;
}
if line.vertical_pos < previous.vertical_pos {
if line.vertical_pos != 0
|| next.controls.is_empty()
|| !next.controls.iter().all(|control| {
matches!(control, Control::Table(table)
if !table.common.treat_as_char && table.common.allow_overlap
&& table.common.vert_rel_to == VertRelTo::Para
&& table.common.horz_rel_to == HorzRelTo::Column
&& signed_hwpunit(table.common.vertical_offset) >= 0
&& i64::from(signed_hwpunit(table.common.vertical_offset))
+ i64::from(table.common.height) <= i64::from(height))
})
{
return None;
}
return Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::StoredPicture { next_flow_y: 0.0 },
anchor_y: -offset,
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: None,
table_top: 0.0,
occupied_bottom: picture_height,
});
}
if i64::from(line.vertical_pos)
!= i64::from(previous.vertical_pos)
+ i64::from(previous.line_height)
+ i64::from(previous.line_spacing)
{
return None;
}
previous = line;
}
None
}
pub(crate) fn stored_table_next_page_placement(
para: &Paragraph,
following: &[Paragraph],
table: &Table,
styles: &super::style_resolver::ResolvedStyleSet,
measured_height: f64,
frame_vpos: i32,
actual_host_flow_y: f64,
available_height: f64,
dpi: f64,
) -> Option<ParagraphFloatPlacement> {
use crate::model::paragraph::LineSeg;
let host = para.line_segs.first()?;
let visible_host = para_has_non_whitespace_text(para);
let valid_saved_line = |line: &LineSeg| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0 && line.line_height > 0
};
if para
.controls
.iter()
.filter(|control| matches!(control, Control::Table(_)))
.count()
!= 1
|| !para.controls.iter().all(|control| {
matches!(
control,
Control::Table(_) | Control::Field(_) | Control::Hyperlink(_)
)
})
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| !para.line_segs.iter().all(valid_saved_line)
|| !para.line_segs.windows(2).all(|lines| {
i64::from(lines[1].vertical_pos)
== i64::from(lines[0].vertical_pos)
+ i64::from(lines[0].line_height)
+ i64::from(lines[0].line_spacing)
})
|| table.layout_page_break() != TablePageBreak::None
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| !table.common.flow_with_text
|| table.common.vert_align != VertAlign::Top
|| !matches!(
table.common.horz_rel_to,
HorzRelTo::Column | HorzRelTo::Para
)
|| !measured_height.is_finite()
|| !dpi.is_finite()
|| dpi <= 0.0
|| !available_height.is_finite()
|| !actual_host_flow_y.is_finite()
{
return None;
}
let caption_extent_hu = if let Some(caption) = &table.caption {
use crate::model::shape::CaptionDirection;
if !matches!(
caption.direction,
CaptionDirection::Top | CaptionDirection::Bottom
) || caption.spacing < 0
|| caption.paragraphs.is_empty()
|| caption.paragraphs.iter().any(|paragraph| {
paragraph.stored_text_partition_is_dirty()
|| paragraph.cell_format_vpos_dirty
|| paragraph.line_segs.is_empty()
|| !paragraph.line_segs.iter().all(valid_saved_line)
|| !paragraph.controls.iter().all(|control| {
matches!(
control,
Control::AutoNumber(_) | Control::Field(_) | Control::Hyperlink(_)
)
})
})
{
return None;
}
let height = super::composer::caption_height_px(&table.caption, dpi);
if !height.is_finite() || height <= 0.0 {
return None;
}
(height * 7200.0 / dpi).round() as i64 + i64::from(caption.spacing)
} else {
0
};
let measured_extent_hu = (measured_height * 7200.0 / dpi).round() as i64;
if measured_extent_hu != i64::from(table.common.height) + caption_extent_hu {
return None;
}
let top = hwpunit_to_px(i32::from(table.outer_margin_top), dpi);
let frame_height_hu = measured_extent_hu
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom);
let bottom = frame_height_hu as f64 * dpi / 7200.0;
let host_y = hwpunit_to_px(host.vertical_pos.checked_sub(frame_vpos)?, dpi);
let offset = hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi);
if host_y <= 0.0
|| offset < 0.0
|| bottom > available_height
|| (!visible_host && (actual_host_flow_y - host_y).abs() > dpi / 7200.0)
|| (visible_host && actual_host_flow_y + offset + bottom <= available_height)
|| host_y + offset + bottom <= available_height
{
return None;
}
let mut previous = para.line_segs.last()?;
let mut previous_para = para;
for (next_index, next) in following.iter().enumerate() {
if next.stored_text_partition_is_dirty() || next.line_segs.is_empty() {
return None;
}
for (line_index, line) in next.line_segs.iter().enumerate() {
if line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0 || line.line_height <= 0 {
return None;
}
if line.vertical_pos < previous.vertical_pos {
let terminal_blank_tail = following[next_index..].iter().all(|p| {
!para_has_non_whitespace_text(p)
&& p.controls.is_empty()
&& !p.stored_text_partition_is_dirty()
&& !p.line_segs.is_empty()
&& p.line_segs.iter().all(|s| {
s.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0 && s.line_height > 0
})
});
if i64::from(line.vertical_pos) != frame_height_hu
|| !(para_has_non_whitespace_text(next) || terminal_blank_tail)
{
return None;
}
return Some(ParagraphFloatPlacement {
flow: ParagraphFloatFlow::NextLine,
anchor_y: 0.0,
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: None,
table_top: top,
occupied_bottom: bottom,
});
}
let paragraph_gap_hu = if line_index == 0 {
let after = styles
.para_styles
.get(previous_para.para_shape_id as usize)?
.spacing_after;
let before = styles
.para_styles
.get(next.para_shape_id as usize)?
.spacing_before;
i64::from(super::px_to_hwpunit(after + before, dpi))
} else {
0
};
if i64::from(line.vertical_pos)
!= i64::from(previous.vertical_pos)
+ i64::from(previous.line_height)
+ i64::from(previous.line_spacing)
+ paragraph_gap_hu
{
return None;
}
previous = line;
}
previous_para = next;
}
None
}
pub fn stored_cellbreak_fragment_row_end(
table: &Table,
host: &Paragraph,
successor: &Paragraph,
) -> Option<usize> {
let [line] = host.line_segs.as_slice() else {
return None;
};
let next = successor.line_segs.first()?;
if table.layout_page_break() != TablePageBreak::CellBreak
|| table.common.treat_as_char
|| table.common.text_wrap != TextWrap::TopAndBottom
|| table.common.vert_rel_to != VertRelTo::Para
|| table.caption.is_some()
|| table.cell_spacing != 0
|| table.outer_margin_top != 0
|| table.outer_margin_bottom != 0
|| host.text.chars().any(|c| c > '\u{001f}' && c != '\u{fffc}')
|| line.segment_width != 0
|| line.tag & 0x8000_0000 != 0
|| next.tag & 0x8000_0000 != 0
|| next.vertical_pos <= 0
|| next.vertical_pos >= line.vertical_pos
|| !successor.controls.is_empty()
{
return None;
}
let heights = (0..table.row_count)
.map(|row| {
table
.cells
.iter()
.filter(|cell| cell.row == row && cell.row_span == 1)
.map(|cell| i64::from(cell.height))
.max()
.filter(|h| *h > 0 && *h <= i64::from(i32::MAX))
})
.collect::<Option<Vec<_>>>()?;
let mut prefix = 0_i64;
let end = heights.iter().position(|height| {
prefix += height;
prefix == i64::from(table.common.height)
})? + 1;
if end >= heights.len()
|| table.cells.iter().any(|cell| {
usize::from(cell.row) < end && usize::from(cell.row) + usize::from(cell.row_span) > end
})
{
return None;
}
let header_height: i64 = if table.repeat_header {
table
.leading_header_rows()
.iter()
.map(|&row| heights[row])
.sum()
} else {
0
};
let tail: i64 = heights[end..].iter().sum::<i64>() + header_height;
(tail == i64::from(next.vertical_pos)).then_some(end)
}
impl ParagraphFloatPlacement {
pub(crate) fn with_cell_column_anchor(
mut self,
table: &Table,
area: &LayoutRect,
table_width: f64,
dpi: f64,
) -> Self {
if !table.common.treat_as_char
&& table.common.text_wrap == TextWrap::TopAndBottom
&& table.common.horz_rel_to == HorzRelTo::Column
{
let left = area.x + hwpunit_to_px(table.outer_margin_left as i32, dpi);
let right = topbottom_float_outer_margin_right_hu(table)
.map_or(0.0, |hu| hwpunit_to_px(hu, dpi));
let width = area.width - hwpunit_to_px(table.outer_margin_left as i32, dpi) - right;
let offset = hwpunit_to_px(signed_hwpunit(table.common.horizontal_offset), dpi);
self.table_left = Some(match table.common.horz_align {
HorzAlign::Left | HorzAlign::Inside => left + offset,
HorzAlign::Center => left + (width - table_width) / 2.0 + offset,
HorzAlign::Right | HorzAlign::Outside => left + width - table_width - offset,
});
}
self
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn from_empty_sibling_box(
para: &Paragraph,
table: &Table,
flow_top: f64,
host_origin: f64,
table_height: f64,
before: f64,
after: f64,
dpi: f64,
) -> Option<Self> {
if !empty_topbottom_sibling_host(para, table)
|| ![flow_top, host_origin, table_height, before, after, dpi]
.iter()
.all(|value| value.is_finite())
|| dpi <= 0.0
|| table_height <= 0.0
{
return None;
}
let table_top = flow_top + before;
let coanchored = is_textless_coanchored_float_host(para);
let occupied_after = if coanchored {
hwpunit_to_px(i32::from(table.outer_margin_bottom), dpi)
} else {
after
};
Some(Self {
flow: if coanchored && signed_hwpunit(table.common.vertical_offset) > 0 {
ParagraphFloatFlow::Exclusion
} else {
ParagraphFloatFlow::NextLine
},
anchor_y: host_origin,
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: None,
table_top,
occupied_bottom: table_top + table_height + occupied_after,
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn from_stored_empty_host_after_line(
previous: &Paragraph,
host: &Paragraph,
table: &Table,
styles: &super::style_resolver::ResolvedStyleSet,
page_base: i32,
column_anchor: f64,
flow_start: f64,
table_height: f64,
dpi: f64,
) -> Option<Self> {
if !dpi.is_finite()
|| dpi <= 0.0
|| !flow_start.is_finite()
|| !table_height.is_finite()
|| table_height <= 0.0
|| para_has_non_whitespace_text(host)
|| host.controls.len() != 1
|| !matches!(host.controls.first(), Some(Control::Table(_)))
|| !is_para_topbottom_float(&table.common)
|| table.common.vert_align != VertAlign::Top
|| signed_hwpunit(table.common.vertical_offset) < 0
|| table.outer_margin_top == 0
|| table.caption.is_some()
|| previous.stored_text_partition_is_dirty()
|| host.stored_text_partition_is_dirty()
|| !previous
.controls
.iter()
.all(super::composer::control_is_width_neutral_marker)
{
return None;
}
let last = previous.line_segs.last()?;
let first = host.line_segs.first()?;
if [last, first].iter().any(|line| {
line.line_height <= 0
|| line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
}) {
return None;
}
let after = styles
.para_styles
.get(previous.para_shape_id as usize)?
.spacing_after;
let before = styles
.para_styles
.get(host.para_shape_id as usize)?
.spacing_before;
let line_end = last
.vertical_pos
.checked_add(last.line_height)?
.checked_add(last.line_spacing)?;
let gap = hwpunit_to_px(first.vertical_pos.checked_sub(line_end)?, dpi);
let expected_flow =
column_anchor + hwpunit_to_px(line_end.checked_sub(page_base)?, dpi) + after;
if (gap - after - before).abs() > dpi / 7200.0
|| (flow_start - expected_flow).abs() > dpi / 7200.0
{
return None;
}
let table_top = flow_start
+ hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi)
+ hwpunit_to_px(i32::from(table.outer_margin_top), dpi);
Some(Self {
flow: if object_only_saved_table_anchor(host, table) {
ParagraphFloatFlow::EmptyHost
} else {
ParagraphFloatFlow::Exclusion
},
anchor_y: flow_start,
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: None,
table_top,
occupied_bottom: table_top
+ table_height
+ hwpunit_to_px(i32::from(table.outer_margin_bottom), dpi),
})
}
pub fn paragraph_end(self, text_flow_end: f64, spacing_after: f64) -> f64 {
match self.flow {
ParagraphFloatFlow::Exclusion | ParagraphFloatFlow::StoredSquareWrap => text_flow_end,
ParagraphFloatFlow::EmptyHost => self.occupied_bottom,
ParagraphFloatFlow::ComputedEmptyHost { line_advance, .. } => {
self.anchor_y + line_advance
}
ParagraphFloatFlow::NextLine
| ParagraphFloatFlow::CoanchoredTable
| ParagraphFloatFlow::SplitHost { .. } => {
match self
.stored_successor_line_origin
.filter(|origin| origin.is_finite() && *origin >= self.occupied_bottom - 0.5)
{
Some(origin) => text_flow_end.max(origin.max(self.occupied_bottom)),
None => text_flow_end.max(self.occupied_bottom + spacing_after),
}
}
ParagraphFloatFlow::InlineHostPicture { next_flow_y } => text_flow_end.max(next_flow_y),
ParagraphFloatFlow::StoredPicture { next_flow_y }
| ParagraphFloatFlow::StoredPictureGroup { next_flow_y } => next_flow_y,
ParagraphFloatFlow::AbsolutePageFrame => text_flow_end.max(self.occupied_bottom),
}
}
pub fn with_tail_line_space(
mut self,
remaining_width: Option<f64>,
table: &Table,
dpi: f64,
) -> Self {
if self.flow != ParagraphFloatFlow::Exclusion {
return self;
}
let width = hwpunit_to_px(table.common.width as i32, dpi)
+ hwpunit_to_px(i32::from(table.outer_margin_left), dpi)
+ hwpunit_to_px(i32::from(table.outer_margin_right), dpi);
self.flow = if remaining_width.is_some_and(|remaining| {
dpi.is_finite()
&& dpi > 0.0
&& remaining.is_finite()
&& width.is_finite()
&& width > 0.0
&& width > remaining
}) {
ParagraphFloatFlow::NextLine
} else {
ParagraphFloatFlow::Exclusion
};
self
}
pub fn for_first_fragment(
mut self,
table: &Table,
paragraph_top: f64,
applied_spacing_before: f64,
host_line_height: f64,
dpi: f64,
) -> Self {
self.stored_successor_line_origin = None;
let box_tail = self.occupied_bottom - self.table_top;
let anchor_is_paragraph_top =
applied_spacing_before > 0.5 && (self.anchor_y - paragraph_top).abs() <= 0.5;
let (paragraph_anchor, host_origin) = if anchor_is_paragraph_top {
(
self.anchor_y,
self.stored_host_origin
.filter(|origin| (origin - applied_spacing_before - self.anchor_y).abs() <= 0.5)
.unwrap_or(self.anchor_y + applied_spacing_before),
)
} else {
(self.anchor_y - applied_spacing_before, self.anchor_y)
};
let positioned_top =
paragraph_anchor + hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi);
self.table_top = positioned_top.max(host_origin + host_line_height);
self.occupied_bottom = self.table_top + box_tail;
self
}
pub fn with_stored_band_origin(
mut self,
para: &Paragraph,
next: &Paragraph,
frame_vpos: i32,
frame_origin: f64,
dpi: f64,
) -> Self {
use crate::model::paragraph::LineSeg;
let ([host], Some(successor)) = (para.line_segs.as_slice(), next.line_segs.first()) else {
return self;
};
if para.controls.len() != 1
|| para.stored_text_partition_is_dirty()
|| next.stored_text_partition_is_dirty()
|| [host, successor]
.iter()
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
|| successor.vertical_pos <= host.vertical_pos
|| host.vertical_pos < frame_vpos
|| !next.controls.is_empty()
|| !frame_origin.is_finite()
|| !dpi.is_finite()
|| dpi <= 0.0
{
return self;
}
let source_advance =
(i64::from(successor.vertical_pos) - i64::from(host.vertical_pos)) as f64 * dpi
/ 7200.0;
let measured_advance = self.occupied_bottom - self.anchor_y;
if (source_advance - measured_advance).abs() > dpi / 7200.0 {
return self;
}
let origin = frame_origin
+ (i64::from(host.vertical_pos) - i64::from(frame_vpos)) as f64 * dpi / 7200.0;
if !origin.is_finite() || origin < self.anchor_y {
return self; }
let shift = origin - self.anchor_y;
self.anchor_y = origin;
self.stored_host_origin = Some(origin);
self.table_top += shift;
self.occupied_bottom += shift;
self
}
fn text_head_control_position(para: &Paragraph, control_index: usize) -> Option<usize> {
let text_len = para.text.chars().count();
if text_len == 0 || para.char_offsets.len() != text_len {
return None;
}
let position = *para.control_text_positions().get(control_index)?;
(position == 0
&& !para.text.contains('\n')
&& para
.text
.chars()
.any(|ch| !ch.is_whitespace() && !ch.is_control() && ch != '\u{FFFC}'))
.then_some(position)
}
fn text_tail_control_position(para: &Paragraph, control_index: usize) -> Option<usize> {
let text_len = para.text.chars().count();
if text_len == 0 || para.char_offsets.len() != text_len {
return None;
}
let position = *para.control_text_positions().get(control_index)?;
(position == text_len
&& para.text.rsplit('\n').next().is_some_and(|line| {
line.chars()
.any(|ch| !ch.is_whitespace() && !ch.is_control() && ch != '\u{FFFC}')
}))
.then_some(position)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn from_stored_inline_host(
para: &Paragraph,
table: &Table,
stored_origin: Option<f64>,
table_height: f64,
before: f64,
after: f64,
dpi: f64,
) -> Option<Self> {
let origin = stored_origin?;
if !stored_inline_host_has_separate_float_frame(para, table)
|| ![origin, table_height, before, after, dpi]
.iter()
.all(|value| value.is_finite())
|| dpi <= 0.0
|| table_height < 0.0
{
return None;
}
let table_top =
origin + before + hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi);
Some(Self {
flow: ParagraphFloatFlow::Exclusion,
anchor_y: origin,
stored_host_origin: Some(origin),
stored_successor_line_origin: None,
table_left: None,
table_top,
occupied_bottom: table_top + table_height + after,
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn from_empty_reflow_host(
para: &Paragraph,
table: &Table,
origin: f64,
stored_origin: Option<f64>,
applied_spacing_before: f64,
table_height: f64,
before: f64,
after: f64,
dpi: f64,
) -> Option<Self> {
let stored_origin = stored_origin.filter(|_| saved_whole_empty_table_anchor(para, table));
if !(empty_table_host_uses_formatted_box(para, table) || stored_origin.is_some()) {
return None;
}
Self::empty_host_box(
table,
stored_origin.map_or(origin, |stored| stored - applied_spacing_before),
stored_origin,
table_height,
before,
after,
dpi,
)
}
pub(crate) fn from_saved_whole_empty_host(
para: &Paragraph,
table: &Table,
stored_origin: Option<f64>,
applied_spacing_before: f64,
table_height: f64,
tail: f64,
dpi: f64,
) -> Option<Self> {
let origin = stored_origin.filter(|_| saved_empty_table_anchor_props(para, table))?;
let mut placement = Self::empty_host_box(
table,
origin - applied_spacing_before,
Some(origin),
table_height,
hwpunit_to_px(i32::from(table.outer_margin_top), dpi),
tail,
dpi,
)?;
placement.anchor_y = origin;
Some(placement)
}
pub(crate) fn from_saved_prefix_empty_host(
para: &Paragraph,
table: &Table,
origin: f64,
prefix_height: f64,
dpi: f64,
) -> Option<Self> {
if !saved_empty_table_anchor_props(para, table) {
return None;
}
Self::empty_host_box(
table,
origin,
Some(origin),
prefix_height,
hwpunit_to_px(i32::from(table.outer_margin_top), dpi),
0.0,
dpi,
)
}
fn empty_host_box(
table: &Table,
origin: f64,
stored_origin: Option<f64>,
table_height: f64,
before: f64,
after: f64,
dpi: f64,
) -> Option<Self> {
if ![origin, table_height, before, after]
.iter()
.all(|value| value.is_finite())
|| table_height < 0.0
{
return None;
}
let table_top =
origin + before + hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi);
Some(Self {
flow: ParagraphFloatFlow::Exclusion,
anchor_y: origin,
stored_host_origin: stored_origin,
stored_successor_line_origin: None,
table_left: None,
table_top,
occupied_bottom: table_top + table_height + after,
})
}
pub(crate) fn from_empty_stored_ladder_host(
para: &Paragraph,
next: &Paragraph,
table: &Table,
origin: f64,
spacing_before: f64,
measured_table_height: f64,
origin_closes_float_frame: bool,
dpi: f64,
) -> Option<Self> {
stored_empty_host_frame::from_stored_ladder(
para,
next,
table,
origin,
spacing_before,
measured_table_height,
origin_closes_float_frame,
dpi,
)
}
pub(crate) fn from_reflowed_empty_ladder_host(
para: &Paragraph,
next: &Paragraph,
table: &Table,
origin: f64,
spacing_before: f64,
measured_table_height: f64,
dpi: f64,
) -> Option<Self> {
stored_empty_host_frame::from_reflowed_ladder(
para,
next,
table,
origin,
spacing_before,
measured_table_height,
dpi,
)
}
pub fn from_computed_host(
para: &Paragraph,
table: &Table,
control_index: usize,
text_origin: f64,
lines: &[ParagraphHostLine],
table_height: f64,
dpi: f64,
) -> Option<Self> {
let char_pos = Self::text_tail_control_position(para, control_index)?;
Self::from_computed_host_at(char_pos, para, table, text_origin, lines, table_height, dpi)
}
pub fn from_computed_head_host(
para: &Paragraph,
table: &Table,
control_index: usize,
text_origin: f64,
lines: &[ParagraphHostLine],
table_height: f64,
dpi: f64,
) -> Option<Self> {
let char_pos = Self::text_head_control_position(para, control_index)?;
Self::from_computed_host_at(char_pos, para, table, text_origin, lines, table_height, dpi)
}
pub(crate) fn from_computed_whitespace_host(
para: &Paragraph,
table: &Table,
control_index: usize,
text_origin: f64,
lines: &[ParagraphHostLine],
table_height: f64,
dpi: f64,
) -> Option<Self> {
let text_len = para.text.chars().count();
if text_len == 0
|| !para.text.chars().all(|ch| matches!(ch, ' ' | '\t'))
|| !para.line_segs.is_empty()
|| para.char_offsets.len() != text_len
|| !matches!(para.controls.as_slice(), [Control::Table(_)])
{
return None;
}
let char_pos = *para.control_text_positions().get(control_index)?;
Self::from_computed_host_at(char_pos, para, table, text_origin, lines, table_height, dpi)
}
fn from_computed_host_at(
char_pos: usize,
para: &Paragraph,
table: &Table,
text_origin: f64,
lines: &[ParagraphHostLine],
table_height: f64,
dpi: f64,
) -> Option<Self> {
if !dpi.is_finite()
|| dpi <= 0.0
|| !table_height.is_finite()
|| table_height < 0.0
|| !is_para_topbottom_float(&table.common)
|| !matches!(table.common.vert_align, VertAlign::Top)
|| lines
.first()
.is_none_or(|line| line.char_start != 0 || line.top != 0.0)
{
return None;
}
let text_len = para.text.chars().count();
if lines.iter().any(|line| {
line.char_start > text_len
|| !line.top.is_finite()
|| !line.height.is_finite()
|| line.height < 0.0
}) || lines
.windows(2)
.any(|pair| pair[1].char_start < pair[0].char_start || pair[1].top < pair[0].top)
{
return None;
}
let anchor = lines
.iter()
.rev()
.find(|line| line.char_start <= char_pos)?;
if signed_hwpunit(table.common.vertical_offset) <= 0 {
return computed_host_origin::stacked_frame(
para,
table,
char_pos,
text_origin,
lines,
table_height,
dpi,
);
}
let offset_top =
anchor.top + hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi);
if lines.iter().any(|line| line.top + line.height > offset_top) {
return None;
}
let anchor_y = text_origin + anchor.top;
let table_top =
text_origin + offset_top + hwpunit_to_px(table.outer_margin_top as i32, dpi);
let occupied_bottom =
table_top + table_height + hwpunit_to_px(table.outer_margin_bottom as i32, dpi);
[anchor_y, table_top, occupied_bottom]
.iter()
.all(|v| v.is_finite())
.then_some(Self {
flow: ParagraphFloatFlow::Exclusion,
anchor_y,
stored_host_origin: (!computed_host_origin::shares_float_anchor(para, char_pos))
.then_some(text_origin),
stored_successor_line_origin: None,
table_left: None,
table_top,
occupied_bottom,
})
}
pub fn clear_occupied_bands(mut self, bands: impl IntoIterator<Item = Range<f64>>) -> Self {
let mut bands: Vec<_> = bands
.into_iter()
.filter(|band| band.start.is_finite() && band.end.is_finite() && band.start < band.end)
.collect();
bands.sort_by(|a, b| a.start.total_cmp(&b.start));
for band in bands {
if self.table_top < band.end && self.occupied_bottom > band.start {
let shift = band.end - self.table_top;
self.table_top = band.end;
self.occupied_bottom += shift;
}
}
self
}
pub fn from_stored_host(
para: &Paragraph,
table: &Table,
control_index: usize,
text_origin: f64,
table_height: f64,
dpi: f64,
) -> Option<Self> {
Self::text_tail_control_position(para, control_index)?;
Self::from_stored_host_at(para, table, control_index, text_origin, table_height, dpi)
}
pub(crate) fn from_empty_stored_offset_host(
para: &Paragraph,
table: &Table,
control_index: usize,
text_origin: f64,
table_height: f64,
dpi: f64,
) -> Option<Self> {
if !matches!(para.controls.as_slice(), [Control::Table(_)])
|| para
.text
.chars()
.any(|ch| !ch.is_whitespace() && !ch.is_control() && ch != '\u{FFFC}')
|| para.cell_format_vpos_dirty
{
return None;
}
Self::from_stored_host_at(para, table, control_index, text_origin, table_height, dpi)
}
pub fn from_stored_head_host(
para: &Paragraph,
table: &Table,
control_index: usize,
text_origin: f64,
table_height: f64,
dpi: f64,
) -> Option<Self> {
Self::text_head_control_position(para, control_index)?;
Self::from_stored_host_at(para, table, control_index, text_origin, table_height, dpi)
}
pub(crate) fn stored_head_host_lines_are_valid(
para: &Paragraph,
table: &Table,
control_index: usize,
) -> bool {
Self::text_head_control_position(para, control_index).is_some()
&& Self::stored_host_lines_are_valid(para, table, control_index)
}
fn from_stored_host_at(
para: &Paragraph,
table: &Table,
control_index: usize,
text_origin: f64,
table_height: f64,
dpi: f64,
) -> Option<Self> {
if !dpi.is_finite()
|| dpi <= 0.0
|| !table_height.is_finite()
|| table_height < 0.0
|| !is_para_topbottom_float(&table.common)
|| !matches!(table.common.vert_align, VertAlign::Top)
|| !Self::stored_host_lines_are_valid(para, table, control_index)
{
return None;
}
let anchor_y = text_origin
+ super::layout::stored_float_anchor_offset_px(para, table, control_index, dpi);
let table_top = anchor_y
+ hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi)
+ hwpunit_to_px(table.outer_margin_top as i32, dpi);
let occupied_bottom =
table_top + table_height + hwpunit_to_px(table.outer_margin_bottom as i32, dpi);
[anchor_y, table_top, occupied_bottom]
.iter()
.all(|v| v.is_finite())
.then_some(Self {
flow: ParagraphFloatFlow::Exclusion,
anchor_y,
stored_host_origin: None,
stored_successor_line_origin: None,
table_left: None,
table_top,
occupied_bottom,
})
}
pub(crate) fn stored_host_lines_are_valid(
para: &Paragraph,
table: &Table,
control_index: usize,
) -> bool {
is_para_topbottom_float(&table.common)
&& table.common.vert_align == VertAlign::Top
&& super::layout::stored_host_lines_precede_float(para, table, control_index)
&& !para.stored_text_partition_is_dirty()
&& !para.line_segs.is_empty()
&& para.line_segs.iter().all(|line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
&& para.line_segs.windows(2).all(|pair| {
pair[1].vertical_pos >= pair[0].vertical_pos
&& pair[1].text_start >= pair[0].text_start
})
}
}
pub(crate) struct ObjectPlacementFrame<'a> {
pub(crate) container: &'a LayoutRect,
pub(crate) column: &'a LayoutRect,
pub(crate) body: &'a LayoutRect,
pub(crate) paper: &'a LayoutRect,
pub(crate) paragraph_y: f64,
pub(crate) alignment: Alignment,
pub(crate) dpi: f64,
}
impl ObjectPlacementFrame<'_> {
pub(crate) fn position(&self, common: &CommonObjAttr, width: f64, height: f64) -> (f64, f64) {
let h_offset = hwpunit_to_px(signed_hwpunit(common.horizontal_offset), self.dpi);
let v_offset = hwpunit_to_px(signed_hwpunit(common.vertical_offset), self.dpi);
let x = if common.treat_as_char {
match self.alignment {
Alignment::Center | Alignment::Distribute => {
self.container.x + (self.container.width - width).max(0.0) / 2.0
}
Alignment::Right => self.container.x + (self.container.width - width).max(0.0),
_ => self.container.x,
}
} else {
let reference = match common.horz_rel_to {
HorzRelTo::Paper => self.paper,
HorzRelTo::Page => self.body,
HorzRelTo::Column => self.column,
HorzRelTo::Para => self.container,
};
match common.horz_align {
HorzAlign::Left | HorzAlign::Inside => reference.x + h_offset,
HorzAlign::Center => reference.x + (reference.width - width) / 2.0 + h_offset,
HorzAlign::Right | HorzAlign::Outside => {
reference.x + reference.width - width - h_offset
}
}
};
let y = if common.treat_as_char {
self.paragraph_y
} else {
let (reference_y, reference_height) = match common.vert_rel_to {
VertRelTo::Paper => (self.paper.y, self.paper.height),
VertRelTo::Page => (self.body.y, self.body.height),
VertRelTo::Para => (self.paragraph_y, self.container.height),
};
match common.vert_align {
VertAlign::Top | VertAlign::Inside => reference_y + v_offset,
VertAlign::Center => reference_y + (reference_height - height) / 2.0 + v_offset,
VertAlign::Bottom | VertAlign::Outside => {
reference_y + reference_height - height - v_offset
}
}
};
(x, y)
}
pub(crate) fn page_anchored_topbottom_band(&self, control: &Control) -> Option<(f64, f64)> {
let common = match control {
Control::Picture(picture) => &picture.common,
Control::Shape(shape) => shape.common(),
_ => return None,
};
if common.treat_as_char
|| common.text_wrap != TextWrap::TopAndBottom
|| !matches!(common.vert_rel_to, VertRelTo::Page | VertRelTo::Paper)
|| common.width == 0
|| common.height == 0
{
return None;
}
let width = hwpunit_to_px(
common.width as i32 + i32::from(common.margin.left) + i32::from(common.margin.right),
self.dpi,
);
let content_height = hwpunit_to_px(common.height as i32, self.dpi);
let caption = match control {
Control::Picture(picture) => picture.caption.clone(),
Control::Shape(shape) => super::composer::shape_caption_for_layout(shape),
_ => None,
};
let height = super::composer::captioned_shape_height_px(content_height, &caption, self.dpi)
+ hwpunit_to_px(
i32::from(common.margin.top) + i32::from(common.margin.bottom),
self.dpi,
);
let (x, y) = self.position(common, width, height);
if ![x, y].iter().all(|v| v.is_finite())
|| x + width <= self.column.x + 0.5
|| x >= self.column.x + self.column.width - 0.5
{
return None;
}
Some((y, y + height))
}
pub(crate) fn stored_topbottom_host_line_origin(&self, para: &Paragraph) -> Option<f64> {
use crate::model::paragraph::LineSeg;
let [line] = para.line_segs.as_slice() else {
return None;
};
if para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos <= 0
|| line.line_height <= 0
|| para
.text
.chars()
.any(|ch| !ch.is_whitespace() && !ch.is_control() && ch != '\u{fffc}')
{
return None;
}
let origin = hwpunit_to_px(line.vertical_pos, self.dpi);
let mut witnessed = false;
for control in ¶.controls {
match control {
Control::SectionDef(_) => continue,
Control::ColumnDef(columns) if columns.column_count == 1 => continue,
_ => {}
}
let (_, bottom) = self.page_anchored_topbottom_band(control)?;
if (bottom - self.column.y - origin).abs() > self.dpi / 7200.0 {
return None;
}
witnessed = true;
}
witnessed.then_some(origin)
}
pub(crate) fn picture_exclusion(
&self,
picture: &Picture,
paragraph: &Paragraph,
control_index: usize,
styles: &super::style_resolver::ResolvedStyleSet,
) -> Option<FrameExclusion> {
let common = &picture.common;
if common.treat_as_char || common.text_wrap != TextWrap::Square {
return None;
}
let (width, height) = picture_flow_frame_size_hu(picture);
if width <= 0 || height <= 0 {
return None;
}
let mut width = hwpunit_to_px(width, self.dpi);
let mut height = hwpunit_to_px(height, self.dpi);
if let Some(caption) = &picture.caption {
use crate::model::shape::CaptionDirection;
let spacing = hwpunit_to_px(caption.spacing as i32, self.dpi);
match caption.direction {
CaptionDirection::Top | CaptionDirection::Bottom => {
height +=
super::composer::caption_height_px(&picture.caption, self.dpi) + spacing;
}
CaptionDirection::Left | CaptionDirection::Right => {
width += hwpunit_to_px(caption.width as i32, self.dpi) + spacing;
}
}
}
width += hwpunit_to_px(
i32::from(common.margin.left) + i32::from(common.margin.right),
self.dpi,
);
height += hwpunit_to_px(
i32::from(common.margin.top) + i32::from(common.margin.bottom),
self.dpi,
);
let (x, mut y) = self.position(common, width, height);
y += stored_picture_anchor_delta(
paragraph,
control_index,
self.column.width,
styles,
self.dpi,
)
.unwrap_or(0.0);
if common.flow_with_text && common.vert_rel_to == VertRelTo::Para {
y = y.min((self.column.y + self.column.height - height).max(self.column.y));
}
if width <= 0.0
|| height <= 0.0
|| ![x, y, width, height].iter().all(|value| value.is_finite())
{
return None;
}
let hu = |px| super::px_to_hwpunit(px, self.dpi);
Some(FrameExclusion {
horizontal: hu(x)..hu(x + width),
vertical: hu(y)..hu(y + height),
policy: frame_exclusion_policy(common.text_flow),
})
}
pub(crate) fn shape_exclusion(
&self,
shape: &crate::model::shape::ShapeObject,
) -> Option<FrameExclusion> {
let common = shape.common();
if common.treat_as_char
|| common.text_wrap != TextWrap::Square
|| super::composer::shape_caption_for_layout(shape).is_some()
{
return None;
}
let margin = &common.margin;
let width = i64::from(common.width) + i64::from(margin.left) + i64::from(margin.right);
let height = i64::from(common.height) + i64::from(margin.top) + i64::from(margin.bottom);
let width = hwpunit_to_px(i32::try_from(width).ok()?, self.dpi);
let height = hwpunit_to_px(i32::try_from(height).ok()?, self.dpi);
let (x, y) = self.position(common, width, height);
if width <= 0.0
|| height <= 0.0
|| ![x, y, width, height].iter().all(|value| value.is_finite())
{
return None;
}
let hu = |px| super::px_to_hwpunit(px, self.dpi);
Some(FrameExclusion {
horizontal: hu(x)..hu(x + width),
vertical: hu(y)..hu(y + height),
policy: frame_exclusion_policy(common.text_flow),
})
}
pub(crate) fn page_anchored_square_table_exclusion(
&self,
table: &Table,
) -> Option<FrameExclusion> {
let common = &table.common;
if common.treat_as_char
|| common.text_wrap != TextWrap::Square
|| !matches!(common.vert_rel_to, VertRelTo::Page | VertRelTo::Paper)
|| table.caption.is_some()
{
return None;
}
let margin = |a: i16, b: i16| hwpunit_to_px(i32::from(a) + i32::from(b), self.dpi);
let width = hwpunit_to_px(common.width as i32, self.dpi)
+ margin(table.outer_margin_left, table.outer_margin_right);
let height = hwpunit_to_px(common.height as i32, self.dpi)
+ margin(table.outer_margin_top, table.outer_margin_bottom);
if width <= 0.0 || height <= 0.0 {
return None;
}
let (x, y) = self.position(common, width, height);
if ![x, y].iter().all(|value| value.is_finite()) {
return None;
}
let hu = |px| super::px_to_hwpunit(px, self.dpi);
Some(FrameExclusion {
horizontal: hu(x)..hu(x + width),
vertical: hu(y)..hu(y + height),
policy: frame_exclusion_policy(common.text_flow),
})
}
}
fn frame_exclusion_policy(flow: TextFlow) -> FrameExclusionPolicy {
match flow {
TextFlow::BothSides => FrameExclusionPolicy::BothSides,
TextFlow::LargestOnly => FrameExclusionPolicy::LargestSide,
TextFlow::LeftOnly => FrameExclusionPolicy::LeftSide,
TextFlow::RightOnly => FrameExclusionPolicy::RightSide,
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct InlineBoxPlacement {
pub x: f64,
pub y: f64,
pub clearance: f64,
pub resolved_x: bool,
pub advance_end: Option<f64>,
pub stored_horizontal_pen: Option<f64>,
pub stored_column_frame: Option<StoredColumnFrame>,
}
crate::storage::clone_copy!(InlineBoxPlacement);
impl InlineBoxPlacement {
pub(crate) fn owns_vertical_frame(&self) -> bool {
self.stored_horizontal_pen.is_none() || self.advance_end.is_some()
}
}
pub(crate) fn place_inline_box(
horizontal: Range<f64>,
top: f64,
width: f64,
height: f64,
alignment: Alignment,
exclusions: &[FrameExclusion],
dpi: f64,
) -> Option<InlineBoxPlacement> {
if exclusions.is_empty()
|| ![horizontal.start, horizontal.end, top, width, height, dpi]
.iter()
.all(|v| v.is_finite())
|| horizontal.end <= horizontal.start
|| width <= 0.0
|| height <= 0.0
|| dpi <= 0.0
{
return None;
}
let hu = |px| super::px_to_hwpunit(px, dpi);
let base = hu(horizontal.start)..hu(horizontal.end);
let base_width = base
.end
.checked_sub(base.start)
.filter(|width| *width > 0)?;
let start = hu(top);
let band_height = hu(height).max(1);
if !exclusions.iter().any(|e| {
e.horizontal.start < base.end
&& base.start < e.horizontal.end
&& e.vertical.start < start.saturating_add(band_height)
&& start < e.vertical.end
}) {
return None;
}
let mut frame = LayoutFrame::new(base.clone(), start, exclusions.to_vec());
frame.minimum_width = hu(width).max(1).min(base_width);
let intervals = frame.carve(band_height);
let lane = match alignment {
Alignment::Right => intervals.last(),
_ => intervals.first(),
}?;
let left = hwpunit_to_px(lane.start, dpi);
let right = hwpunit_to_px(lane.end, dpi);
let x = match alignment {
Alignment::Right => (right - width).max(left),
Alignment::Center => (left + (right - left - width) / 2.0).max(left),
_ => left,
};
let y = hwpunit_to_px(frame.top, dpi).max(top);
Some(InlineBoxPlacement {
x,
y,
clearance: y - top,
resolved_x: true,
advance_end: None,
stored_horizontal_pen: None,
stored_column_frame: None,
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct FloatCarveEvidence {
pub(crate) width: i32,
pub(crate) vertical: Range<i32>,
}
impl FloatCarveEvidence {
pub(crate) fn matches_stored_rows(
&self,
missing_width: i32,
rows: &[crate::model::paragraph::LineSeg],
tolerance: i32,
) -> bool {
(missing_width - self.width).abs() <= tolerance
&& rows.iter().any(|segment| {
let top = segment.vertical_pos;
let bottom = top.saturating_add(segment.line_height.max(1));
top < self.vertical.end && self.vertical.start < bottom
})
}
}
pub(crate) fn signed_hwpunit(value: HwpUnit) -> i32 {
value as i32
}
pub(crate) fn para_nested_table_line_indices(para: &Paragraph) -> Option<Vec<Option<usize>>> {
match para_nested_table_line_indices_with_storage(para, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn para_nested_table_line_indices_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
storage: &S,
) -> Result<Option<Vec<Option<usize>>>, S::Error> {
let Some(lines) = stored_control_line_indices_with_storage(para, storage)? else {
return Ok(None);
};
Ok(Some(
storage.collect(
para.controls
.iter()
.zip(lines)
.map(|(ctrl, line)| matches!(ctrl, Control::Table(_)).then_some(line)),
)?,
))
}
pub(crate) fn stored_control_line_indices(para: &Paragraph) -> Option<Vec<usize>> {
match stored_control_line_indices_with_storage(para, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn stored_control_line_indices_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
storage: &S,
) -> Result<Option<Vec<usize>>, S::Error> {
if para.line_segs.is_empty() || crate::renderer::para_has_no_stored_line_segs(para) {
return Ok(None);
}
Ok(Some(
storage.collect(
para.control_utf16_positions_with_storage(storage)?
.into_iter()
.map(|pos| {
(0..para.line_segs.len())
.rfind(|&li| para.line_seg_text_start(li) <= pos)
.unwrap_or(0)
}),
)?,
))
}
pub(crate) struct NestedTableGroup {
pub(crate) line: Option<usize>,
pub(crate) controls: Vec<usize>,
pub(crate) side_by_side: bool,
}
pub(crate) struct StoredBlockCellFlow {
pub(crate) paragraph_origins: Vec<f64>,
pub(crate) origins: Vec<Vec<Option<f64>>>,
pub(crate) bottom: f64,
pub(crate) closed_before_host: bool,
}
pub(crate) struct StoredSingleColumnCellFlow {
pub(crate) line_origins_hu: Vec<Vec<i32>>,
pub(crate) content_end_hu: i32,
}
impl StoredSingleColumnCellFlow {
pub(crate) fn line_advances_hu(&self) -> Vec<(usize, usize, i32)> {
let lines: Vec<_> = self
.line_origins_hu
.iter()
.enumerate()
.flat_map(|(pi, lines)| {
lines
.iter()
.enumerate()
.map(move |(li, &top)| (pi, li, top))
})
.collect();
lines
.iter()
.enumerate()
.map(|(index, &(pi, li, top))| {
let next = lines
.get(index + 1)
.map_or(self.content_end_hu, |line| line.2);
(pi, li, next - top + if index == 0 { top } else { 0 })
})
.collect()
}
}
pub(crate) fn stored_single_column_cell_flow(
paragraphs: &[Paragraph],
section_column_spacing_hu: i32,
) -> Option<StoredSingleColumnCellFlow> {
use crate::model::paragraph::{ColumnBreakType, LineSeg};
if section_column_spacing_hu < 0
|| !paragraphs.iter().skip(1).any(|para| {
para.controls
.iter()
.any(|control| matches!(control, Control::ColumnDef(_)))
})
{
return None;
}
let mut band_origin = 0_i64;
let mut occupied_end = 0_i64;
let mut previous_top = None;
let mut has_transition = false;
let mut origins = Vec::with_capacity(paragraphs.len());
for (pi, para) in paragraphs.iter().enumerate() {
if para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
|| para.line_segs.is_empty()
|| !matches!(
para.column_type,
ColumnBreakType::None | ColumnBreakType::MultiColumn
)
{
return None;
}
let positions = para.control_utf16_positions();
let mut defines_band = false;
for (ci, control) in para.controls.iter().enumerate() {
let Control::ColumnDef(columns) = control else {
return None;
};
if defines_band || columns.column_count != 1 || positions.get(ci).copied() != Some(0) {
return None;
}
defines_band = true;
}
if defines_band && pi > 0 {
band_origin = occupied_end.checked_add(i64::from(section_column_spacing_hu))?;
has_transition = true;
}
let mut lines = Vec::with_capacity(para.line_segs.len());
for line in ¶.line_segs {
if line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.vertical_pos < 0
|| line.line_height <= 0
{
return None;
}
let top = band_origin.checked_add(i64::from(line.vertical_pos))?;
if previous_top.is_some_and(|previous| top <= previous) {
return None;
}
occupied_end = occupied_end.max(top.checked_add(i64::from(line.line_height))?);
previous_top = Some(top);
lines.push(i32::try_from(top).ok()?);
}
origins.push(lines);
}
if !has_transition {
return None;
}
Some(StoredSingleColumnCellFlow {
line_origins_hu: origins,
content_end_hu: i32::try_from(occupied_end).ok()?,
})
}
pub(crate) fn stored_block_cell_flow(
paragraphs: &[Paragraph],
styles: &super::style_resolver::ResolvedStyleSet,
dpi: f64,
mut table_height: impl FnMut(&Table) -> f64,
) -> Option<StoredBlockCellFlow> {
match stored_block_cell_flow_with_storage(
paragraphs,
styles,
dpi,
|table| Ok(table_height(table)),
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn stored_block_cell_flow_with_storage<S: crate::storage::Storage>(
paragraphs: &[Paragraph],
styles: &super::style_resolver::ResolvedStyleSet,
dpi: f64,
mut table_height: impl FnMut(&Table) -> Result<f64, S::Error>,
storage: &S,
) -> Result<Option<StoredBlockCellFlow>, S::Error> {
if !super::cell_vpos_ladder_is_intact(paragraphs)
|| paragraphs.iter().any(|p| {
p.stored_text_partition_is_dirty()
|| p.cell_format_vpos_dirty
|| p.line_segs.is_empty()
|| p.line_segs.iter().any(|s| {
s.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
})
|| p.controls.iter().any(|c| match c {
Control::Table(t) => {
!p.text.trim().is_empty()
|| p.line_segs.len() != 1
|| t.common.treat_as_char
|| t.common.text_wrap != TextWrap::TopAndBottom
|| t.common.vert_rel_to != VertRelTo::Para
|| t.common.vert_align != VertAlign::Top
|| signed_hwpunit(t.common.vertical_offset) < 0
|| t.caption.is_some()
}
Control::Field(_) => false,
Control::ColumnDef(columns) => columns.column_count > 1,
_ => true,
})
})
{
return Ok(None);
}
let mut heights: Vec<Vec<f64>> = storage.vec(paragraphs.len())?;
let mut unabsorbed_block = false;
for (pi, para) in paragraphs.iter().enumerate() {
let h: Vec<f64> = storage.try_collect(para.controls.iter().map(|c| match c {
Control::Table(t) => table_height(t),
_ => Ok(0.0),
}))?;
let block_height: f64 = nested_table_groups_with_storage(para, false, storage)?
.iter()
.map(|group| nested_group_occupied_height(para, group, &h, 0.0, dpi))
.sum();
if block_height > 0.0 {
let before_host_frame = para.controls.iter().enumerate().any(|(ci, control)| {
let Control::Table(table) = control else {
return false;
};
let body = h[ci]
- hwpunit_to_px(
i32::from(table.outer_margin_top) + i32::from(table.outer_margin_bottom),
dpi,
);
stored_nested_before_host::stored_table_before_host_frame(
paragraphs, pi, table, ci, 0.0, body, styles, dpi,
)
.is_some()
});
if before_host_frame {
storage.push(&mut heights, h)?;
continue;
}
if let Some(next) = paragraphs.get(pi + 1) {
let saved_gap = hwpunit_to_px(
next.line_segs[0]
.vertical_pos
.saturating_sub(para.line_segs[0].vertical_pos),
dpi,
);
unabsorbed_block |= saved_gap < block_height;
} else {
unabsorbed_block |=
hwpunit_to_px(para.line_segs[0].line_height, dpi) < block_height;
}
}
storage.push(&mut heights, h)?;
}
if !unabsorbed_block {
return Ok(stored_closed_cell_frame::closed_before_host_flow(
paragraphs, styles, &heights, dpi,
));
}
let mut result = StoredBlockCellFlow {
paragraph_origins: storage.vec(paragraphs.len())?,
origins: {
let mut origins = storage.vec(paragraphs.len())?;
for p in paragraphs {
origins.push(storage.filled(p.controls.len(), None)?);
}
origins
},
bottom: 0.0,
closed_before_host: false,
};
let mut cursor = hwpunit_to_px(paragraphs[0].line_segs[0].vertical_pos, dpi);
for (pi, para) in paragraphs.iter().enumerate() {
let style = styles.para_styles.get(para.para_shape_id as usize);
let has_block = para.controls.iter().any(|c| matches!(c, Control::Table(_)));
let before = if has_block {
0.0
} else {
super::cell_paragraph_spacing_before(para, pi, style.map_or(0.0, |s| s.spacing_before))
};
cursor += before;
storage.push(&mut result.paragraph_origins, cursor)?;
if has_block {
for group in nested_table_groups_with_storage(para, false, storage)? {
let group_top = cursor;
let mut advance = 0.0_f64;
for ci in group.controls {
let Control::Table(table) = ¶.controls[ci] else {
continue;
};
let lead =
super::layout::table_layout::para_relative_float_table_lead(table, dpi);
result.origins[pi][ci] =
Some(group_top + if group.side_by_side { 0.0 } else { advance });
advance = if group.side_by_side {
advance.max(lead + heights[pi][ci])
} else {
advance + lead + heights[pi][ci]
};
}
cursor += advance;
}
for (ci, control) in para.controls.iter().enumerate() {
if let Control::Table(table) = control {
if let Some(advance) = block_table_trailing_whitespace_line_hu(para, table, ci)
{
cursor += hwpunit_to_px(advance, dpi);
}
}
}
} else {
for (li, seg) in para.line_segs.iter().enumerate() {
if super::height_measurer::stored_seg_is_row_fragment(para, li) {
continue;
}
cursor += hwpunit_to_px(seg.line_height, dpi);
if pi + 1 != paragraphs.len() || li + 1 != para.line_segs.len() {
cursor += hwpunit_to_px(seg.line_spacing, dpi);
}
}
}
if pi + 1 < paragraphs.len() {
cursor += style.map_or(0.0, |s| s.spacing_after);
}
result.bottom = cursor;
}
Ok(Some(result))
}
pub(crate) fn nested_table_is_overlay(table: &Table) -> bool {
!table.common.treat_as_char
&& matches!(
table.common.text_wrap,
TextWrap::BehindText | TextWrap::InFrontOfText
)
&& table.common.vert_rel_to == VertRelTo::Para
&& matches!(
table.common.horz_rel_to,
HorzRelTo::Column | HorzRelTo::Para
)
}
fn stored_empty_flow_lines(para: &Paragraph) -> Option<&[crate::model::paragraph::LineSeg]> {
use crate::model::paragraph::LineSeg;
if para_has_non_whitespace_text(para)
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
{
return None;
}
let lines = para.line_segs.as_slice();
(!lines.is_empty()
&& lines.iter().all(|line| {
line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& line.vertical_pos >= 0
&& line.line_height > 0
&& line.text_height > 0
&& line.text_height <= line.line_height
&& line.baseline_distance >= 0
&& line.baseline_distance <= line.line_height
&& line.line_spacing >= 0
&& line.segment_width > 0
}))
.then_some(lines)
}
fn stored_empty_flow_line(para: &Paragraph) -> Option<&crate::model::paragraph::LineSeg> {
let [line] = stored_empty_flow_lines(para)? else {
return None;
};
Some(line)
}
pub(crate) fn stored_overlay_host_flow_line<'a>(
para: &'a Paragraph,
successor: Option<&Paragraph>,
) -> Option<&'a crate::model::paragraph::LineSeg> {
if para.controls.is_empty() || !crate::renderer::empty_host_controls_are_flow_neutral(para) {
return None;
}
let line = stored_empty_flow_line(para)?;
let successor = successor?;
if successor.column_type != crate::model::paragraph::ColumnBreakType::None {
return None;
}
let next = stored_empty_flow_line(successor)?;
let end = line
.vertical_pos
.checked_add(line.line_height)?
.checked_add(line.line_spacing)?;
(end == next.vertical_pos).then_some(line)
}
pub(crate) fn stored_overlay_guide_owns_flow(paragraphs: &[Paragraph], index: usize) -> bool {
let Some(current) = paragraphs.get(index) else {
return false;
};
if !current.controls.is_empty()
|| current.column_type != crate::model::paragraph::ColumnBreakType::None
|| stored_empty_flow_line(current).is_none()
{
return false;
}
let Some(owner) = (0..index).rev().find(|&previous| {
!paragraphs[previous].controls.is_empty()
|| para_has_non_whitespace_text(¶graphs[previous])
}) else {
return false;
};
if stored_overlay_host_flow_line(¶graphs[owner], paragraphs.get(owner + 1)).is_none() {
return false;
}
for cursor in owner + 1..=index {
let previous = stored_empty_flow_line(¶graphs[cursor - 1]);
let next = stored_empty_flow_line(¶graphs[cursor]);
let (Some(previous), Some(next)) = (previous, next) else {
return false;
};
if paragraphs[cursor].column_type != crate::model::paragraph::ColumnBreakType::None
|| previous
.vertical_pos
.checked_add(previous.line_height)
.and_then(|end| end.checked_add(previous.line_spacing))
!= Some(next.vertical_pos)
{
return false;
}
}
true
}
pub(crate) fn nested_overlay_outer_frame(
table: &Table,
paragraph_origin: f64,
outer_height: f64,
dpi: f64,
) -> Option<Range<f64>> {
if !nested_table_is_overlay(table)
|| !matches!(table.common.vert_align, VertAlign::Top)
|| !paragraph_origin.is_finite()
|| !outer_height.is_finite()
|| outer_height < 0.0
{
return None;
}
let top = paragraph_origin + hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi);
Some(top..top + outer_height)
}
pub(crate) fn nested_overlay_stored_line_lead(
para: &Paragraph,
para_index: usize,
spacing_before: f64,
dpi: f64,
) -> f64 {
if crate::renderer::para_has_no_stored_line_segs(para)
|| para.stored_text_partition_is_dirty()
|| para.cell_format_vpos_dirty
{
return 0.0;
}
if para_index == 0 {
crate::renderer::cell_first_para_stored_lead(para, spacing_before, dpi)
} else {
spacing_before
}
}
pub(crate) fn nested_group_occupied_height(
para: &Paragraph,
group: &NestedTableGroup,
heights: &[f64],
stored_line_lead: f64,
dpi: f64,
) -> f64 {
let mut flow_height = 0.0_f64;
let mut overlay_height = 0.0_f64;
for &ci in &group.controls {
let Control::Table(table) = ¶.controls[ci] else {
continue;
};
if nested_table_is_overlay(table) {
let bottom = nested_overlay_outer_frame(table, -stored_line_lead, heights[ci], dpi)
.map_or(heights[ci], |frame| frame.end);
overlay_height = overlay_height.max(bottom);
} else if group.side_by_side {
flow_height = flow_height.max(heights[ci]);
} else {
flow_height += heights[ci];
}
}
flow_height.max(overlay_height)
}
pub(crate) fn nested_host_gap_start(
paragraphs: &[Paragraph],
pidx: usize,
para_top: f64,
prior_nested_end: f64,
dpi: f64,
) -> f64 {
let line_end = paragraphs
.iter()
.take(pidx)
.flat_map(|prev| prev.line_segs.iter())
.map(|s| crate::renderer::hwpunit_to_px(s.vertical_pos.saturating_add(s.line_height), dpi))
.filter(|&e| e <= para_top + 0.5)
.fold(f64::NEG_INFINITY, f64::max);
if prior_nested_end > 0.0 {
line_end.max(prior_nested_end.min(para_top))
} else {
line_end
}
}
pub(crate) fn nested_table_groups(para: &Paragraph, _hwpx_stored: bool) -> Vec<NestedTableGroup> {
match nested_table_groups_with_storage(para, _hwpx_stored, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn nested_table_groups_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
_hwpx_stored: bool,
storage: &S,
) -> Result<Vec<NestedTableGroup>, S::Error> {
let lines = para_nested_table_line_indices_with_storage(para, storage)?;
let mut groups: Vec<NestedTableGroup> = Vec::new();
for (ci, ctrl) in para.controls.iter().enumerate() {
if !matches!(ctrl, Control::Table(_)) {
continue;
}
let line = lines.as_ref().and_then(|v| v[ci]);
if let Some(group) = groups.iter_mut().find(|g| g.line == line) {
storage.push(&mut group.controls, ci)?;
} else {
storage.push(
&mut groups,
NestedTableGroup {
line,
controls: storage.collect([ci])?,
side_by_side: false,
},
)?;
}
}
for group in &mut groups {
let tables: Vec<&Table> =
storage.collect(
group
.controls
.iter()
.filter_map(|&ci| match ¶.controls[ci] {
Control::Table(t) if !nested_table_is_overlay(t) => Some(t.as_ref()),
_ => None,
}),
)?;
group.side_by_side = if group.line.is_some() {
line_nested_table_group_is_side_by_side_with_storage(&tables, storage)?
} else {
para_float_group_is_side_by_side_with_storage(para, storage)?
};
}
Ok(groups)
}
pub(crate) fn line_nested_table_group_is_side_by_side(tables: &[&Table]) -> bool {
match line_nested_table_group_is_side_by_side_with_storage(tables, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn line_nested_table_group_is_side_by_side_with_storage<S: crate::storage::Storage>(
tables: &[&Table],
storage: &S,
) -> Result<bool, S::Error> {
if tables.len() < 2 {
return Ok(false);
}
if tables.iter().all(|table| table.common.treat_as_char) {
return Ok(true);
}
let mut spans: Vec<(i32, i32)> = Vec::new();
for table in tables {
if !para_float_group_member_is_eligible(table) {
return Ok(false);
}
let start = signed_hwpunit(table.common.horizontal_offset);
let width = table.common.width.min(i32::MAX as u32) as i32;
storage.push(&mut spans, (start, start.saturating_add(width)))?;
}
spans.sort_unstable();
Ok(
spans.windows(2).all(|w| w[0].1 <= w[1].0 + 1),
)
}
pub(crate) fn para_float_group_is_side_by_side(para: &Paragraph) -> bool {
match para_float_group_is_side_by_side_with_storage(para, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn para_float_group_is_side_by_side_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
storage: &S,
) -> Result<bool, S::Error> {
let tables: Vec<&Table> =
storage.collect(para.controls.iter().filter_map(|ctrl| match ctrl {
Control::Table(table) => Some(table.as_ref()),
_ => None,
}))?;
Ok(tables.iter().all(|t| !t.common.treat_as_char)
&& line_nested_table_group_is_side_by_side_with_storage(&tables, storage)?)
}
pub(crate) fn para_float_group_member_is_eligible(table: &Table) -> bool {
!table.common.treat_as_char
&& matches!(table.common.text_wrap, TextWrap::TopAndBottom)
&& matches!(table.common.vert_rel_to, VertRelTo::Para)
&& matches!(table.common.horz_rel_to, HorzRelTo::Para)
&& signed_hwpunit(table.common.horizontal_offset) >= 0
}
pub(crate) fn paper_or_page_float_carve_evidence(
paragraphs: &[crate::model::paragraph::Paragraph],
) -> Vec<FloatCarveEvidence> {
match paper_or_page_float_carve_evidence_with_storage(paragraphs, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn paper_or_page_float_carve_evidence_with_storage<S: crate::storage::Storage>(
paragraphs: &[crate::model::paragraph::Paragraph],
storage: &S,
) -> Result<Vec<FloatCarveEvidence>, S::Error> {
use crate::model::control::Control;
let mut evidence = Vec::new();
for para in paragraphs {
for control in ¶.controls {
let common = match control {
Control::Picture(picture) => &picture.common,
Control::Shape(shape) => shape.common(),
Control::Table(table) => &table.common,
_ => continue,
};
if common.treat_as_char
|| !matches!(
common.text_wrap,
TextWrap::Square | TextWrap::Tight | TextWrap::Through
)
{
continue;
}
if !matches!(common.vert_rel_to, VertRelTo::Paper | VertRelTo::Page)
|| !matches!(common.vert_align, VertAlign::Top | VertAlign::Inside)
{
continue;
}
let width = (common.width as i32)
.saturating_add(i32::from(common.margin.left))
.saturating_add(i32::from(common.margin.right));
let height = (common.height as i32)
.saturating_add(i32::from(common.margin.top))
.saturating_add(i32::from(common.margin.bottom));
let top = signed_hwpunit(common.vertical_offset);
let vertical = top..top.saturating_add(height);
let candidate = FloatCarveEvidence { width, vertical };
if width > 0 && height > 0 && !evidence.contains(&candidate) {
storage.push(&mut evidence, candidate)?;
}
}
}
Ok(evidence)
}
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct PaperOrigin {
pub(crate) body_left: i32,
pub(crate) body_top: i32,
pub(crate) band_abs_top: i32,
}
pub(crate) fn resolve_picture_exclusion(
picture: &Picture,
column_horizontal: Range<i32>,
paragraph_horizontal: Range<i32>,
paragraph_top: i32,
paper: PaperOrigin,
) -> Option<FrameExclusion> {
resolve_float_exclusion(
&picture.common,
picture_flow_frame_size_hu(picture),
false,
column_horizontal,
paragraph_horizontal,
paragraph_top,
paper,
)
}
pub(crate) fn resolve_shape_exclusion(
common: &CommonObjAttr,
column_horizontal: Range<i32>,
paragraph_horizontal: Range<i32>,
paragraph_top: i32,
paper: PaperOrigin,
) -> Option<FrameExclusion> {
if !shape_wraps_beside_text(common) {
return None;
}
resolve_float_exclusion(
common,
(
i32::try_from(common.width).ok()?,
i32::try_from(common.height).ok()?,
),
true,
column_horizontal,
paragraph_horizontal,
paragraph_top,
paper,
)
}
fn resolve_float_exclusion(
common: &CommonObjAttr,
(width, height): (i32, i32),
offset_is_outer_origin: bool,
column_horizontal: Range<i32>,
paragraph_horizontal: Range<i32>,
paragraph_top: i32,
paper: PaperOrigin,
) -> Option<FrameExclusion> {
let paper_relative_horz = matches!(common.horz_rel_to, HorzRelTo::Paper | HorzRelTo::Page);
let paper_relative_vert = matches!(common.vert_rel_to, VertRelTo::Paper | VertRelTo::Page);
if common.treat_as_char
|| (common.text_wrap != TextWrap::Square && !offset_is_outer_origin)
|| !matches!(
common.horz_rel_to,
HorzRelTo::Column | HorzRelTo::Para | HorzRelTo::Paper | HorzRelTo::Page
)
|| !matches!(
common.vert_rel_to,
VertRelTo::Para | VertRelTo::Paper | VertRelTo::Page
)
|| !matches!(common.vert_align, VertAlign::Top | VertAlign::Inside)
{
return None;
}
let policy = match common.text_flow {
TextFlow::BothSides => FrameExclusionPolicy::BothSides,
TextFlow::LargestOnly => FrameExclusionPolicy::LargestSide,
TextFlow::LeftOnly | TextFlow::RightOnly => return None,
};
if column_horizontal.is_empty() || paragraph_horizontal.is_empty() || width <= 0 || height <= 0
{
return None;
}
let horizontal_offset = signed_hwpunit(common.horizontal_offset);
let column_end = column_horizontal.end;
let reference = match common.horz_rel_to {
HorzRelTo::Column => column_horizontal,
HorzRelTo::Para => paragraph_horizontal,
HorzRelTo::Paper | HorzRelTo::Page => {
(-paper.body_left)..(column_end.saturating_sub(paper.body_left))
}
};
let reference_width = reference.end.saturating_sub(reference.start);
let visible_left = match common.horz_align {
HorzAlign::Left | HorzAlign::Inside => reference.start.saturating_add(horizontal_offset),
HorzAlign::Center => reference
.start
.saturating_add(
reference_width
.saturating_sub(width)
.max(0)
.saturating_div(2),
)
.saturating_add(horizontal_offset),
HorzAlign::Right | HorzAlign::Outside => reference
.end
.saturating_sub(width)
.saturating_sub(horizontal_offset),
};
let visible_top = if paper_relative_vert {
signed_hwpunit(common.vertical_offset)
.saturating_sub(paper.body_top)
.saturating_sub(paper.band_abs_top)
} else {
paragraph_top.saturating_add(signed_hwpunit(common.vertical_offset))
};
let _ = paper_relative_horz;
let (visible_left, visible_top) = if offset_is_outer_origin {
(
if matches!(common.horz_align, HorzAlign::Left | HorzAlign::Inside) {
visible_left.saturating_add(i32::from(common.margin.left))
} else {
visible_left
},
visible_top.saturating_add(beside_shape_top_margin_hu(common)),
)
} else {
(visible_left, visible_top)
};
let horizontal = visible_left.saturating_sub(i32::from(common.margin.left))
..visible_left
.saturating_add(width)
.saturating_add(i32::from(common.margin.right));
let vertical = visible_top.saturating_sub(i32::from(common.margin.top))
..visible_top
.saturating_add(height)
.saturating_add(i32::from(common.margin.bottom));
(!horizontal.is_empty() && !vertical.is_empty()).then_some(FrameExclusion {
horizontal,
vertical,
policy,
})
}
pub(crate) fn is_textless_coanchored_float_host(para: &Paragraph) -> bool {
if para_has_non_whitespace_text(para) {
return false;
}
let mut offsets = para.controls.iter().filter_map(|control| match control {
Control::Table(table) if is_para_topbottom_float(&table.common) => {
Some(signed_hwpunit(table.common.vertical_offset))
}
_ => None,
});
let first = offsets.next();
let second = offsets.next();
let third = offsets.next();
matches!((first, second, third), (Some(0), Some(offset), None) if offset > 0)
}
pub(crate) fn is_last_para_topbottom_float(para: &Paragraph, control_index: usize) -> bool {
para.controls
.iter()
.enumerate()
.rfind(|(_, control)| {
matches!(control, Control::Table(table) if is_para_topbottom_float(&table.common))
})
.is_some_and(|(index, _)| index == control_index)
}
pub(crate) fn is_para_topbottom_float(common: &CommonObjAttr) -> bool {
!common.treat_as_char
&& matches!(common.text_wrap, TextWrap::TopAndBottom)
&& matches!(common.vert_rel_to, VertRelTo::Para)
}
pub(crate) fn float_pushed_line_spacing_before(
paragraph_top: f64,
spacing_before: f64,
pushed_line_top: f64,
) -> f64 {
let spacing = (paragraph_top + spacing_before.max(0.0) - pushed_line_top).max(0.0);
if spacing.is_finite() {
spacing
} else {
0.0
}
}
pub(crate) fn empty_topbottom_sibling_host(para: &Paragraph, table: &Table) -> bool {
is_para_topbottom_float(&table.common)
&& matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
&& !para.text.chars().any(|c| c > '\u{001F}' && c != '\u{FFFC}')
&& para.controls.iter().filter(|control| {
matches!(control, Control::Table(t) if is_para_topbottom_float(&t.common))
}).take(2).count() >= 2
}
pub(crate) fn is_non_flow_decoration(control: &Control) -> bool {
let common = match control {
Control::Shape(shape) => shape.common(),
Control::Picture(picture) => &picture.common,
_ => return false,
};
!common.treat_as_char
&& matches!(
common.text_wrap,
TextWrap::BehindText | TextWrap::InFrontOfText
)
}
pub(crate) fn split_float_successor_spacing_before(
fragment_host: &Paragraph,
successor: &Paragraph,
column_top: f64,
spacing_before: f64,
fragment_bottom: f64,
) -> Option<f64> {
let table = fragment_host
.controls
.iter()
.find_map(|control| match control {
Control::Table(table) if is_para_topbottom_float(&table.common) => Some(table),
_ => None,
})?;
if computed_empty_table_host(fragment_host, table) {
return Some(float_pushed_line_spacing_before(
column_top,
spacing_before,
fragment_bottom,
));
}
let stored = |seg: &&crate::model::paragraph::LineSeg| seg.tag & 0x8000_0000 == 0;
let host_line = fragment_host.line_segs.first().filter(stored)?;
let successor_line = successor.line_segs.first().filter(stored)?;
let box_end = i64::from(host_line.vertical_pos)
+ i64::from(signed_hwpunit(table.common.vertical_offset))
+ i64::from(table.outer_margin_top)
+ i64::from(table.common.height)
+ i64::from(table.outer_margin_bottom);
(i64::from(successor_line.vertical_pos) == box_end)
.then(|| float_pushed_line_spacing_before(column_top, spacing_before, fragment_bottom))
}
pub(crate) fn reflow_empty_table_host(para: &Paragraph, table: &Table) -> bool {
para.line_segs.is_empty() && computed_empty_table_host(para, table)
}
pub(crate) fn computed_empty_table_host(para: &Paragraph, table: &Table) -> bool {
let mut objects = para.controls.iter().filter(|control| {
!control_leaves_table_anchor_empty(control) && !is_non_flow_decoration(control)
});
let only_table = matches!(objects.next(), Some(Control::Table(_))) && objects.next().is_none();
para.text.is_empty()
&& !para.layout_object_projection
&& para.line_segs.iter().all(|line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
})
&& only_table
&& is_para_topbottom_float(&table.common)
&& matches!(table.common.vert_align, VertAlign::Top)
&& signed_hwpunit(table.common.vertical_offset) >= 0
}
pub(crate) fn computed_rowbreak_owns_outer_frame(
para: &Paragraph,
table: &Table,
start_cut: &[usize],
) -> bool {
table.layout_page_break() == TablePageBreak::RowBreak
&& start_cut.iter().all(|&cut| cut == 0)
&& computed_empty_table_host(para, table)
}
pub(crate) fn stored_rowbreak_owns_fragment_outer_frame(para: &Paragraph, table: &Table) -> bool {
stored_row_grid::owns_fragment_outer_frame(para, table)
}
pub(crate) fn empty_table_host_uses_formatted_box(para: &Paragraph, table: &Table) -> bool {
reflow_empty_table_host(para, table)
|| (table.layout_page_break() == TablePageBreak::None
&& matches!(
para.column_type,
crate::model::paragraph::ColumnBreakType::Page
| crate::model::paragraph::ColumnBreakType::Column
)
&& object_only_saved_table_anchor(para, table)
&& !para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& para.line_segs.iter().all(|line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
&& table.common.vert_align == VertAlign::Top
&& signed_hwpunit(table.common.vertical_offset) >= 0)
}
fn saved_whole_empty_table_anchor(para: &Paragraph, table: &Table) -> bool {
table.layout_page_break() == TablePageBreak::None && saved_empty_table_anchor_props(para, table)
}
fn saved_empty_table_anchor_props(para: &Paragraph, table: &Table) -> bool {
object_only_saved_table_anchor(para, table)
&& !para.stored_text_partition_is_dirty()
&& !para.cell_format_vpos_dirty
&& para.line_segs.iter().all(|line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
&& table.common.vert_align == VertAlign::Top
&& signed_hwpunit(table.common.vertical_offset) >= 0
}
pub(crate) fn empty_table_anchor_ladder_lacks_outer_box(
para: &Paragraph,
table: &Table,
next: Option<&Paragraph>,
) -> bool {
let stored_vpos = |para: &Paragraph| {
para.line_segs
.first()
.filter(|line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
.map(|line| line.vertical_pos)
};
let (Some(host), Some(next)) = (stored_vpos(para), next.and_then(stored_vpos)) else {
return false;
};
let mut objects = para
.controls
.iter()
.filter(|control| !control_leaves_table_anchor_empty(control));
let only_table = matches!(objects.next(), Some(Control::Table(_))) && objects.next().is_none();
let outer_box = i32::from(table.outer_margin_top)
.saturating_add(signed_hwpunit(table.common.height))
.saturating_add(i32::from(table.outer_margin_bottom));
only_table
&& para.text.is_empty()
&& para.line_segs.len() == 1
&& table.layout_page_break() == TablePageBreak::None
&& is_para_topbottom_float(&table.common)
&& table.common.vert_align == VertAlign::Top
&& signed_hwpunit(table.common.vertical_offset) >= 0
&& (host..host.saturating_add(outer_box)).contains(&next)
}
pub(crate) fn empty_table_host_uses_shared_formatted_box(
para: &Paragraph,
table: &Table,
placement: Option<&ParagraphFloatPlacement>,
) -> bool {
placement.is_some_and(|plan| {
matches!(
plan.flow,
ParagraphFloatFlow::EmptyHost | ParagraphFloatFlow::ComputedEmptyHost { .. }
)
}) || empty_table_host_uses_formatted_box(para, table)
|| (saved_whole_empty_table_anchor(para, table)
&& placement.is_some_and(|plan| {
plan.flow == ParagraphFloatFlow::Exclusion && plan.stored_host_origin.is_some()
}))
}
pub(crate) fn absolute_table_vertical_geometry(
table: &Table,
ref_y: f64,
ref_h: f64,
height: f64,
dpi: f64,
) -> (f64, f64) {
let offset = hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi);
let top_margin = hwpunit_to_px(i32::from(table.outer_margin_top), dpi);
let bottom_margin = hwpunit_to_px(i32::from(table.outer_margin_bottom), dpi);
let top = match table.common.vert_align {
VertAlign::Top | VertAlign::Inside => ref_y + offset + top_margin,
VertAlign::Center => ref_y + (ref_h - height) / 2.0 + offset,
VertAlign::Bottom | VertAlign::Outside => ref_y + ref_h - height - offset - bottom_margin,
};
(top, top + height + bottom_margin)
}
pub(crate) fn empty_offset_float_deferred_text_ladder_hu(
host: &Paragraph,
table: &Table,
following: &Paragraph,
) -> Option<(i32, i32)> {
let has_non_whitespace_text = |paragraph: &Paragraph| {
paragraph
.text
.chars()
.any(|ch| !ch.is_whitespace() && ch != '\u{FFFC}')
};
let qualifies = is_para_topbottom_float(&table.common)
&& table.common.flow_with_text
&& signed_hwpunit(table.common.vertical_offset) > 0
&& host.controls.len() == 1
&& !has_non_whitespace_text(host)
&& has_non_whitespace_text(following)
&& following.line_segs.iter().all(|segment| {
segment.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
});
let first_line_height = following.line_segs.first()?.line_height;
let table_top =
i64::from(signed_hwpunit(table.common.vertical_offset)) + i64::from(table.outer_margin_top);
qualifies.then(|| {
host.line_segs
.iter()
.find(|segment| {
segment.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
.filter(|segment| {
first_line_height > 0
&& i64::from(
segment
.line_height
.saturating_add(segment.line_spacing)
.max(0),
) + i64::from(first_line_height)
<= table_top
})
.map(|segment| {
(
segment.vertical_pos,
segment
.line_height
.saturating_add(segment.line_spacing)
.max(0),
)
})
})?
}
pub(crate) fn original_hwpx_infront_para_flow_paginates(
original_hwpx: bool,
table: &Table,
) -> bool {
original_hwpx
&& !table.common.treat_as_char
&& table.common.flow_with_text
&& matches!(table.common.text_wrap, TextWrap::InFrontOfText)
&& matches!(table.common.vert_rel_to, VertRelTo::Para)
&& matches!(table.common.horz_rel_to, HorzRelTo::Para)
&& table.row_count >= 40
&& table.col_count >= 6
}
pub(crate) fn native_empty_host_rowbreak_line_advance_hu(
native_hwp5_layout: bool,
para: &Paragraph,
table: &Table,
next_para: Option<&Paragraph>,
) -> Option<i32> {
let has_non_whitespace_text = |paragraph: &Paragraph| {
paragraph
.text
.chars()
.any(|ch| ch > '\u{001F}' && ch != '\u{FFFC}' && !ch.is_whitespace())
};
if !native_hwp5_layout
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| has_non_whitespace_text(para)
|| !matches!(table.common.vert_rel_to, VertRelTo::Para)
|| !matches!(table.layout_page_break(), TablePageBreak::RowBreak)
|| signed_hwpunit(table.common.vertical_offset) <= 0
|| para
.controls
.iter()
.filter(|control| matches!(control, Control::Table(_)))
.count()
!= 1
|| para
.controls
.iter()
.filter(|control| {
matches!(control, Control::Table(candidate)
if is_para_topbottom_float(&candidate.common))
})
.count()
!= 1
|| !next_para.is_some_and(|next| has_non_whitespace_text(next) && next.controls.is_empty())
{
return None;
}
let host_seg = para
.line_segs
.iter()
.find(|seg| seg.tag & 0x80000000 == 0 && seg.line_height > 0)?;
let advance = host_seg.line_height + host_seg.line_spacing;
if advance <= 0 {
return None;
}
let next_vpos = next_para
.and_then(|next| {
next.line_segs
.iter()
.find(|seg| seg.tag & 0x80000000 == 0 && seg.line_height > 0)
})
.map(|seg| seg.vertical_pos)?;
if (next_vpos - host_seg.vertical_pos - advance).abs() > 1 {
return None;
}
Some(advance)
}
pub(crate) fn stored_empty_anchor_band_host_line_advance_hu(
stored_layout: bool,
para: &Paragraph,
control_index: usize,
next_para: Option<&Paragraph>,
) -> Option<i32> {
if !stored_layout || !para.text.is_empty() {
return None;
}
if para.stored_text_partition_is_dirty()
|| next_para.is_some_and(Paragraph::stored_text_partition_is_dirty)
{
return None;
}
if para.controls.iter().any(|control| match control {
Control::Table(table) => {
crate::renderer::empty_host_line_absorbed_by_topbottom_float(para, &table.common)
}
Control::Picture(picture) => {
crate::renderer::empty_host_line_absorbed_by_topbottom_float(para, &picture.common)
}
Control::Shape(shape) => {
crate::renderer::empty_host_line_absorbed_by_topbottom_float(para, shape.common())
}
_ => false,
}) {
return None;
}
stored_anchor_band_host_line_from_ladder(
para,
control_index,
next_text_paragraph_vpos(next_para),
)
}
pub(crate) fn visible_host_text_flows_below_offset_float(
para: &Paragraph,
table: &Table,
next_vpos: Option<i32>,
) -> bool {
let offset = signed_hwpunit(table.common.vertical_offset);
let stored = |seg: &&crate::model::paragraph::LineSeg| seg.tag & 0x8000_0000 == 0;
offset > 0
&& is_para_topbottom_float(&table.common)
&& para
.line_segs
.first()
.filter(stored)
.is_some_and(|first| first.line_height > offset)
&& para
.line_segs
.last()
.filter(stored)
.zip(next_vpos)
.is_some_and(|(last, next)| {
i64::from(next) - i64::from(last.vertical_pos)
== i64::from(last.line_height) + i64::from(last.line_spacing)
})
}
pub(crate) fn stored_visible_anchor_band_host_line_advance_hu(
stored_layout: bool,
para: &Paragraph,
control_index: usize,
next_para: Option<&Paragraph>,
) -> Option<i32> {
stored_visible_anchor_band_host_line_advance_from_vpos(
stored_layout,
para,
control_index,
next_plain_text_vpos(next_para),
)
}
pub(crate) fn stored_visible_anchor_band_host_line_advance_from_vpos(
stored_layout: bool,
para: &Paragraph,
control_index: usize,
next_plain_text_vpos: Option<i32>,
) -> Option<i32> {
if !stored_layout || !para_has_non_whitespace_text(para) {
return None;
}
stored_anchor_band_host_line_from_ladder(para, control_index, next_plain_text_vpos)
}
fn next_plain_text_vpos(next_para: Option<&Paragraph>) -> Option<i32> {
let next = next_para?;
if !next.controls.is_empty() {
return None;
}
next_text_paragraph_vpos(Some(next))
}
fn next_text_paragraph_vpos(next_para: Option<&Paragraph>) -> Option<i32> {
let next = next_para?;
if !para_has_non_whitespace_text(next) {
return None;
}
next_stored_paragraph_vpos(Some(next))
}
fn next_stored_paragraph_vpos(next_para: Option<&Paragraph>) -> Option<i32> {
let next = next_para?;
next.line_segs
.iter()
.enumerate()
.find(|(_, seg)| seg.tag & 0x8000_0000 == 0 && seg.line_height > 0)
.map(|(index, seg)| ladder_vpos(next, index, seg.vertical_pos))
}
fn ladder_vpos(paragraph: &Paragraph, index: usize, fallback: i32) -> i32 {
paragraph
.source_line_seg_vertical_pos
.as_ref()
.and_then(|source| source.get(index).copied())
.unwrap_or(fallback)
}
fn stored_anchor_band_host_line_from_ladder(
para: &Paragraph,
control_index: usize,
next_plain_text_vpos: Option<i32>,
) -> Option<i32> {
let mut last_float = None;
for (index, control) in para.controls.iter().enumerate() {
let common = match control {
Control::Table(table) => &table.common,
Control::Picture(picture) => &picture.common,
Control::Shape(shape) => shape.common(),
_ => continue,
};
if !is_para_topbottom_float(common) {
return None;
}
last_float = Some(index);
}
if last_float != Some(control_index) {
return None;
}
let next_vpos = next_plain_text_vpos?;
let (host_index, host_seg) = para
.line_segs
.iter()
.enumerate()
.find(|(_, seg)| seg.tag & 0x8000_0000 == 0 && seg.line_height > 0)?;
let advance = host_seg.line_height + host_seg.line_spacing;
if advance <= 0 {
return None;
}
let host_vpos = ladder_vpos(para, host_index, host_seg.vertical_pos);
((next_vpos - host_vpos - advance).abs() <= 1).then_some(advance)
}
fn para_has_non_whitespace_text(para: &Paragraph) -> bool {
para.text
.chars()
.any(|ch| ch > '\u{001F}' && ch != '\u{FFFC}' && !ch.is_whitespace())
}
pub(crate) fn stored_empty_offset_rowbreak_fragment_repeats_outer_margin(
stored_layout: bool,
para: &Paragraph,
table: &Table,
) -> bool {
let float_then_inline_tables =
match para.controls.split_first() {
Some((Control::Table(first), rest)) => rest.is_empty()
|| (std::ptr::eq(first.as_ref(), table)
&& rest.iter().all(
|control| matches!(control, Control::Table(t) if t.common.treat_as_char),
)),
_ => false,
};
stored_layout
&& table.layout_page_break() == TablePageBreak::RowBreak
&& !para_has_non_whitespace_text(para)
&& float_then_inline_tables
&& !para.cell_format_vpos_dirty
&& para
.line_segs
.first()
.is_some_and(|line| line.segment_width > 0)
&& ParagraphFloatPlacement::stored_host_lines_are_valid(para, table, 0)
}
pub(crate) fn empty_host_cellbreak_fragment_repeats_outer_margin(
stored_layout: bool,
para: &Paragraph,
table: &Table,
) -> bool {
let has_non_whitespace_text = |paragraph: &Paragraph| {
paragraph
.text
.chars()
.any(|ch| ch > '\u{001F}' && ch != '\u{FFFC}' && !ch.is_whitespace())
};
stored_layout
&& !table.common.treat_as_char
&& is_para_topbottom_float(&table.common)
&& matches!(table.layout_page_break(), TablePageBreak::CellBreak)
&& !has_non_whitespace_text(para)
}
pub(crate) const SINGLE_CELL_PAGE_FRAGMENT_BOTTOM_INSET_HU: i32 = 100;
pub(crate) fn rowbreak_fragment_bottom_inset_hu(table: &Table) -> i32 {
if (table.attr | table.raw_table_record_attr) & 0x08 != 0 {
0
} else {
SINGLE_CELL_PAGE_FRAGMENT_BOTTOM_INSET_HU
}
}
pub(crate) fn native_single_cell_rowbreak_page_fragment(
native_hwp5_layout: bool,
table: &Table,
) -> bool {
native_hwp5_layout
&& table.row_count == 1
&& table.col_count == 1
&& !table.common.treat_as_char
&& matches!(table.layout_page_break(), TablePageBreak::RowBreak)
}
pub(crate) fn native_multirow_rowbreak_reopens_outer_top(
native_hwp5_layout: bool,
table: &Table,
is_continuation: bool,
start_cut: &[usize],
) -> bool {
native_hwp5_layout
&& is_continuation
&& table.row_count > 1
&& table.col_count > 1
&& !start_cut.is_empty()
&& table.outer_margin_top > 0
&& is_para_topbottom_float(&table.common)
&& matches!(table.layout_page_break(), TablePageBreak::RowBreak)
}
pub(crate) fn single_cell_page_fragment_bottom(table: &Table, body_bottom: f64, dpi: f64) -> f64 {
body_bottom
- hwpunit_to_px(i32::from(table.outer_margin_bottom), dpi)
- hwpunit_to_px(rowbreak_fragment_bottom_inset_hu(table), dpi)
}
mod rowbreak_frame;
pub(crate) use rowbreak_frame::{rowbreak_fragment_bounds, rowbreak_fragment_frame_bottom};
pub(crate) fn para_relative_left_aligned_outer_margin_left_hu(table: &Table) -> Option<i32> {
if table.common.treat_as_char
|| !matches!(table.common.horz_rel_to, HorzRelTo::Para)
|| !matches!(table.common.horz_align, HorzAlign::Left | HorzAlign::Inside)
|| table.outer_margin_left <= 0
{
return None;
}
Some(i32::from(table.outer_margin_left))
}
pub(crate) fn square_float_outer_margin_top_hu(table: &Table) -> Option<i32> {
if !matches!(table.common.text_wrap, TextWrap::Square)
|| table.common.treat_as_char
|| !matches!(table.common.vert_rel_to, VertRelTo::Para)
|| !matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
|| !matches!(table.common.horz_align, HorzAlign::Left | HorzAlign::Inside)
|| table.outer_margin_top <= 0
{
return None;
}
Some(i32::from(table.outer_margin_top))
}
pub(crate) fn topbottom_float_outer_margin_left_hu(table: &Table) -> Option<i32> {
if !matches!(table.common.text_wrap, TextWrap::TopAndBottom)
|| table.common.treat_as_char
|| !matches!(
table.common.horz_rel_to,
HorzRelTo::Para | HorzRelTo::Column
)
|| !matches!(table.common.horz_align, HorzAlign::Left | HorzAlign::Inside)
|| table.outer_margin_left <= 0
{
return None;
}
Some(i32::from(table.outer_margin_left))
}
pub(crate) fn topbottom_float_outer_margin_right_hu(table: &Table) -> Option<i32> {
if table.common.treat_as_char
|| table.common.text_wrap != TextWrap::TopAndBottom
|| !matches!(
table.common.horz_rel_to,
HorzRelTo::Para | HorzRelTo::Column
)
|| !matches!(
table.common.horz_align,
HorzAlign::Right | HorzAlign::Outside
)
|| table.outer_margin_right <= 0
{
return None;
}
Some(i32::from(table.outer_margin_right))
}
pub(crate) fn original_hwpx_column_rowbreak_equal_outer_margin_hu(
original_hwpx: bool,
table: &Table,
) -> Option<i32> {
let declared_height = signed_hwpunit(table.common.height);
if !original_hwpx
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| !matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
|| !matches!(table.common.horz_rel_to, HorzRelTo::Column)
|| !matches!(table.common.horz_align, HorzAlign::Left | HorzAlign::Inside)
|| signed_hwpunit(table.common.horizontal_offset) != 0
|| signed_hwpunit(table.common.vertical_offset) != 0
|| !matches!(table.layout_page_break(), TablePageBreak::RowBreak)
|| table.row_count == 0
|| table.col_count != 1
|| table.cells.len() != usize::from(table.row_count)
|| !table.cells.iter().enumerate().all(|(row, cell)| {
cell.row == row as u16 && cell.col == 0 && cell.row_span == 1 && cell.col_span == 1
})
|| signed_hwpunit(table.common.width) <= 0
|| declared_height <= 0
|| table.outer_margin_left <= 0
|| table.outer_margin_right <= 0
|| table.outer_margin_top <= 0
|| table.outer_margin_bottom <= 0
|| table.outer_margin_left != table.outer_margin_right
|| table.outer_margin_left != table.outer_margin_top
|| table.outer_margin_left != table.outer_margin_bottom
|| table.caption.is_some()
{
return None;
}
Some(i32::from(table.outer_margin_left))
}
pub(crate) fn empty_host_physical_ladder_extras_hu(
table: &Table,
host_vpos: i32,
next_vpos: i32,
) -> Option<i64> {
if matches!(table.layout_page_break(), TablePageBreak::RowBreak) {
return None;
}
let extras = i64::from(signed_hwpunit(table.common.vertical_offset).max(0))
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom);
if extras <= 0 {
return None;
}
let stored_delta = i64::from(next_vpos) - i64::from(host_vpos);
let physical_delta = extras + i64::from(table.common.height.min(i32::MAX as u32));
((stored_delta - physical_delta).abs() <= 2).then_some(extras)
}
pub(crate) fn native_multirow_internal_reset_rowbreak_anchor_advance_hu(
native_hwp5_layout: bool,
para: &Paragraph,
table: &Table,
next_para: Option<&Paragraph>,
) -> Option<i32> {
let has_non_whitespace_text = |paragraph: &Paragraph| {
paragraph
.text
.chars()
.any(|ch| ch > '\u{001F}' && ch != '\u{FFFC}' && !ch.is_whitespace())
};
if !native_hwp5_layout
|| table.row_count <= 1
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| !matches!(table.layout_page_break(), TablePageBreak::RowBreak)
|| has_non_whitespace_text(para)
|| para
.controls
.iter()
.filter(|control| matches!(control, Control::Table(_)))
.count()
!= 1
{
return None;
}
let host_seg = para.line_segs.iter().find(|seg| {
seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& seg.line_height > 0
})?;
let next_seg = next_para?
.line_segs
.iter()
.find(|seg| seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0)?;
if next_seg.vertical_pos >= host_seg.vertical_pos {
return None;
}
let has_internal_reset = table.cells.iter().any(|cell| {
let mut previous_vpos = None;
for cell_para in &cell.paragraphs {
for seg in cell_para.line_segs.iter().filter(|seg| {
seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
}) {
if previous_vpos.is_some_and(|previous| previous > 0 && seg.vertical_pos <= 0) {
return true;
}
previous_vpos = Some(seg.vertical_pos);
}
}
false
});
if !has_internal_reset {
return None;
}
let advance = host_seg.line_height + host_seg.line_spacing.max(0);
(advance > 0).then_some(advance)
}
pub(crate) fn native_empty_host_physical_outer_box_paint_inset(
native_hwp5_layout: bool,
para: &Paragraph,
table: &Table,
next_para: Option<&Paragraph>,
) -> bool {
let has_non_whitespace_text = |paragraph: &Paragraph| {
paragraph
.text
.chars()
.any(|ch| ch > '\u{001F}' && ch != '\u{FFFC}' && !ch.is_whitespace())
};
let declared_height = signed_hwpunit(table.common.height);
if !native_hwp5_layout
|| has_non_whitespace_text(para)
|| para.controls.len() != 1
|| !matches!(para.controls.first(), Some(Control::Table(_)))
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| !matches!(table.common.vert_align, VertAlign::Top | VertAlign::Inside)
|| !matches!(table.common.horz_rel_to, HorzRelTo::Column)
|| !matches!(table.common.horz_align, HorzAlign::Left)
|| signed_hwpunit(table.common.horizontal_offset) != 0
|| signed_hwpunit(table.common.vertical_offset) != 0
|| !matches!(table.layout_page_break(), TablePageBreak::RowBreak)
|| table.row_count <= 1
|| table.col_count != 1
|| table.cells.len() != usize::from(table.row_count)
|| !table.cells.iter().enumerate().all(|(row, cell)| {
cell.row == row as u16
&& cell.col == 0
&& cell.row_span == 1
&& cell.col_span == 1
})
|| signed_hwpunit(table.common.width) <= 0
|| declared_height <= 0
|| table.outer_margin_left <= 0
|| table.outer_margin_right <= 0
|| table.outer_margin_top <= 0
|| table.outer_margin_bottom <= 0
|| table.outer_margin_left != table.outer_margin_right
|| table.outer_margin_left != table.outer_margin_top
|| table.outer_margin_left != table.outer_margin_bottom
|| table.caption.is_some()
|| next_para.is_some_and(|next| has_non_whitespace_text(next) || !next.controls.is_empty())
{
return false;
}
fn stored_seg(paragraph: &Paragraph) -> Option<&crate::model::paragraph::LineSeg> {
paragraph.line_segs.iter().find(|seg| {
seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& seg.line_height > 0
})
}
let Some(host_seg) = stored_seg(para) else {
return false;
};
let Some(next_seg) = next_para.and_then(stored_seg) else {
return false;
};
let stored_advance = i64::from(next_seg.vertical_pos) - i64::from(host_seg.vertical_pos);
let physical_outer_height = i64::from(declared_height)
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom);
stored_advance > 0 && (stored_advance - physical_outer_height).abs() <= 1
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct NativeStoredResetFragmentPaintGeometry {
pub(crate) outer_top_hu: i32,
pub(crate) first_fragment_height_hu: Option<i32>,
}
pub(crate) fn native_hwp5_stored_reset_fragment_paint_geometry(
native_hwp5_layout: bool,
host_para: &Paragraph,
table: &Table,
is_continuation: bool,
start_cut: &[usize],
end_cut: &[usize],
) -> Option<NativeStoredResetFragmentPaintGeometry> {
let has_non_whitespace_text = |paragraph: &Paragraph| {
paragraph
.text
.chars()
.any(|ch| ch > '\u{001F}' && ch != '\u{FFFC}' && !ch.is_whitespace())
};
let cell = table.cells.first()?;
let declared_height_hu = signed_hwpunit(table.common.height);
if !native_hwp5_layout
|| has_non_whitespace_text(host_para)
|| host_para.controls.len() != 1
|| !matches!(host_para.controls.first(), Some(Control::Table(_)))
|| table.common.treat_as_char
|| !is_para_topbottom_float(&table.common)
|| !matches!(table.common.vert_align, VertAlign::Top)
|| !matches!(table.common.horz_rel_to, HorzRelTo::Column)
|| !matches!(table.common.horz_align, HorzAlign::Left)
|| signed_hwpunit(table.common.horizontal_offset) != 0
|| signed_hwpunit(table.common.vertical_offset) != 0
|| !matches!(table.layout_page_break(), TablePageBreak::RowBreak)
|| table.row_count != 1
|| table.col_count != 1
|| table.cells.len() != 1
|| cell.row != 0
|| cell.col != 0
|| cell.row_span != 1
|| cell.col_span != 1
|| signed_hwpunit(table.common.width) <= 0
|| declared_height_hu <= 0
|| table.caption.is_some()
|| table.outer_margin_left <= 0
|| table.outer_margin_right <= 0
|| table.outer_margin_top <= 0
|| table.outer_margin_bottom <= 0
|| table.outer_margin_left != table.outer_margin_right
|| table.outer_margin_left != table.outer_margin_top
|| table.outer_margin_left != table.outer_margin_bottom
{
return None;
}
let effective_padding = cell.effective_padding(&table.padding);
if effective_padding.top < 0 || effective_padding.bottom < 0 {
return None;
}
let mut previous: Option<(usize, &crate::model::paragraph::LineSeg)> = None;
let mut stored_lines_before = 0usize;
let mut reset_witness = None;
'paragraphs: for (para_index, paragraph) in cell.paragraphs.iter().enumerate() {
for seg in paragraph.line_segs.iter().filter(|seg| {
seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& seg.text_height > 0
}) {
if let Some((previous_para_index, previous_seg)) = previous {
if previous_para_index != para_index
&& previous_seg.vertical_pos > 0
&& seg.vertical_pos == 0
{
reset_witness = Some((stored_lines_before, previous_seg));
break 'paragraphs;
}
}
stored_lines_before += 1;
previous = Some((para_index, seg));
}
}
let (reset_unit_end, previous_seg) = reset_witness?;
let stored_head_height_hu = i64::from(previous_seg.vertical_pos)
+ i64::from(previous_seg.text_height)
+ i64::from(effective_padding.top)
+ i64::from(effective_padding.bottom);
if (stored_head_height_hu - i64::from(declared_height_hu)).abs() > 1 {
return None;
}
let is_first_fragment = !is_continuation
&& start_cut.is_empty()
&& end_cut.len() == 1
&& end_cut[0] == reset_unit_end;
let is_final_successor = is_continuation
&& start_cut.len() == 1
&& start_cut[0] == reset_unit_end
&& end_cut.is_empty();
if !is_first_fragment && !is_final_successor {
return None;
}
Some(NativeStoredResetFragmentPaintGeometry {
outer_top_hu: i32::from(table.outer_margin_top),
first_fragment_height_hu: is_first_fragment.then_some(declared_height_hu),
})
}
pub(crate) fn is_page_bottom_fixed_float(common: &CommonObjAttr) -> bool {
!common.treat_as_char
&& matches!(common.text_wrap, TextWrap::TopAndBottom)
&& matches!(common.vert_rel_to, VertRelTo::Page)
&& matches!(common.vert_align, VertAlign::Bottom)
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct FloatPlacementContext {
pub col_area: LayoutRect,
pub body_area: Option<LayoutRect>,
pub paper_width: Option<f64>,
pub host_margin_left: f64,
pub host_margin_right: f64,
}
impl FloatPlacementContext {
pub(crate) fn new(col_area: LayoutRect) -> Self {
Self {
col_area,
body_area: None,
paper_width: None,
host_margin_left: 0.0,
host_margin_right: 0.0,
}
}
pub(crate) fn with_body_area(mut self, body_area: LayoutRect) -> Self {
self.body_area = Some(body_area);
self
}
pub(crate) fn with_paper_width(mut self, paper_width: f64) -> Self {
self.paper_width = Some(paper_width);
self
}
pub(crate) fn with_host_margins(mut self, left: f64, right: f64) -> Self {
self.host_margin_left = left;
self.host_margin_right = right;
self
}
}
pub(crate) fn horizontal_range(
common: &CommonObjAttr,
width_px: f64,
ctx: FloatPlacementContext,
dpi: f64,
) -> (f64, f64) {
let h_offset = hwpunit_to_px(signed_hwpunit(common.horizontal_offset), dpi);
let col_area = ctx.col_area;
let (ref_x, ref_w) = match common.horz_rel_to {
HorzRelTo::Paper => {
let fallback_paper_w = if width_px > col_area.width {
col_area.x * 2.0 + width_px
} else {
col_area.x * 2.0 + col_area.width
};
let paper_w = ctx.paper_width.unwrap_or(fallback_paper_w);
(0.0, paper_w)
}
HorzRelTo::Page => ctx
.body_area
.filter(|body| body.width > 0.0)
.map(|body| (body.x, body.width))
.unwrap_or((col_area.x, col_area.width)),
HorzRelTo::Para => (
col_area.x + ctx.host_margin_left,
col_area.width - ctx.host_margin_left,
),
HorzRelTo::Column => (col_area.x, col_area.width),
};
let x = crate::renderer::object_alignment::aligned_horizontal_origin(
ref_x,
ref_w,
width_px,
common.horz_align,
h_offset,
) + overlay_outer_frame::overlay_outer_margin_left_hu(common)
.map(|margin| hwpunit_to_px(margin, dpi))
.unwrap_or(0.0);
(x, x + width_px.max(0.0))
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct FloatLane {
pub control_index: Option<usize>,
pub x_start: f64,
pub x_end: f64,
pub bottom: f64,
}
impl FloatLane {
fn overlaps_x(&self, x_start: f64, x_end: f64) -> bool {
ranges_overlap(self.x_start, self.x_end, x_start, x_end)
}
}
#[derive(Debug, Default, Clone)]
pub(crate) struct FloatLaneSet {
lanes: Vec<FloatLane>,
}
impl FloatLaneSet {
pub(crate) fn new() -> Self {
Self { lanes: Vec::new() }
}
pub(crate) fn clear(&mut self) {
self.lanes.clear();
}
pub(crate) fn lanes(&self) -> &[FloatLane] {
&self.lanes
}
pub(crate) fn pushed_top(&self, x_start: f64, x_end: f64, raw_top: f64) -> f64 {
self.lanes
.iter()
.filter(|lane| lane.overlaps_x(x_start, x_end))
.fold(raw_top, |top, lane| top.max(lane.bottom))
}
pub(crate) fn place(
&mut self,
control_index: Option<usize>,
x_start: f64,
x_end: f64,
raw_top: f64,
height: f64,
) -> FloatLane {
let top = self.pushed_top(x_start, x_end, raw_top);
let lane = FloatLane {
control_index,
x_start,
x_end,
bottom: top + height.max(0.0),
};
self.lanes.push(lane);
lane
}
pub(crate) fn max_bottom(&self) -> f64 {
self.lanes
.iter()
.map(|lane| lane.bottom)
.fold(0.0, f64::max)
}
}
pub(crate) fn ranges_overlap(a_start: f64, a_end: f64, b_start: f64, b_end: f64) -> bool {
let a0 = a_start.min(a_end);
let a1 = a_start.max(a_end);
let b0 = b_start.min(b_end);
let b1 = b_start.max(b_end);
a0 < b1 && b0 < a1
}
pub(crate) fn para_offset_consumed_by_page_break(
para: &Paragraph,
table: &Table,
available_height: f64,
dpi: f64,
) -> bool {
const MIN_FRAGMENT_KEEP_PX: f64 = 25.0;
let common = &table.common;
if common.treat_as_char || !matches!(common.vert_rel_to, VertRelTo::Para) {
return false;
}
let offset = signed_hwpunit(common.vertical_offset);
if offset <= 0 {
return false;
}
if !available_height.is_finite() || available_height <= 0.0 {
return false;
}
let anchor_top = para
.line_segs
.first()
.map(|seg| hwpunit_to_px(seg.vertical_pos, dpi))
.unwrap_or(0.0);
let first_row_cells = || {
table
.cells
.iter()
.filter(|cell| cell.row == 0 && cell.row_span <= 1)
};
let unsplittable_first_row = first_row_cells().next().is_some()
&& first_row_cells().all(|cell| {
cell.paragraphs.len() <= 1
&& cell.paragraphs.iter().all(|para| para.line_segs.len() <= 1)
});
let entry_need = if unsplittable_first_row {
first_row_cells()
.map(|cell| hwpunit_to_px(cell.height as i32, dpi))
.fold(MIN_FRAGMENT_KEEP_PX, f64::max)
} else {
MIN_FRAGMENT_KEEP_PX
};
anchor_top + hwpunit_to_px(offset, dpi) + entry_need >= available_height
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::paragraph::LineSeg;
use crate::model::shape::{HorzAlign, HorzRelTo, VertAlign};
use crate::model::table::Cell;
use crate::renderer::layout_frame::FrameExclusionPolicy;
fn base_common() -> CommonObjAttr {
CommonObjAttr {
text_wrap: TextWrap::TopAndBottom,
vert_rel_to: VertRelTo::Para,
horz_rel_to: HorzRelTo::Column,
horz_align: HorzAlign::Left,
..Default::default()
}
}
fn supported_picture(flow: TextFlow) -> Picture {
Picture {
common: CommonObjAttr {
width: 300,
height: 200,
text_wrap: TextWrap::Square,
text_flow: flow,
vert_rel_to: VertRelTo::Para,
vert_align: VertAlign::Top,
horz_rel_to: HorzRelTo::Column,
horz_align: HorzAlign::Left,
..Default::default()
},
..Default::default()
}
}
#[test]
fn picture_exclusion_maps_only_recovered_side_wrap_flows() {
let both_sides = resolve_picture_exclusion(
&supported_picture(TextFlow::BothSides),
0..1_000,
0..1_000,
50,
PaperOrigin::default(),
)
.expect("BothSides is represented by the physical row frame");
assert_eq!(both_sides.policy, FrameExclusionPolicy::BothSides);
let largest_only = resolve_picture_exclusion(
&supported_picture(TextFlow::LargestOnly),
0..1_000,
0..1_000,
50,
PaperOrigin::default(),
)
.expect("LargestOnly has a recovered frame policy");
assert_eq!(largest_only.policy, FrameExclusionPolicy::LargestSide);
assert!(resolve_picture_exclusion(
&supported_picture(TextFlow::LeftOnly),
0..1_000,
0..1_000,
50,
PaperOrigin::default(),
)
.is_none());
assert!(resolve_picture_exclusion(
&supported_picture(TextFlow::RightOnly),
0..1_000,
0..1_000,
50,
PaperOrigin::default(),
)
.is_none());
}
#[test]
fn picture_exclusion_uses_flow_frame_for_oversized_current_square_float() {
let mut picture = supported_picture(TextFlow::BothSides);
picture.shape_attr.current_width = 900;
picture.shape_attr.current_height = 800;
let exclusion =
resolve_picture_exclusion(&picture, 0..1_000, 0..1_000, 50, PaperOrigin::default())
.expect("Square side-wrap float has a physical row frame");
assert_eq!(exclusion.horizontal, 0..300);
assert_eq!(exclusion.vertical, 50..250);
}
#[test]
fn picture_exclusion_rejects_non_square_or_inline_hosts() {
let mut picture = supported_picture(TextFlow::BothSides);
picture.common.treat_as_char = true;
assert!(
resolve_picture_exclusion(&picture, 0..1_000, 0..1_000, 0, PaperOrigin::default())
.is_none()
);
picture.common.treat_as_char = false;
picture.common.text_wrap = TextWrap::TopAndBottom;
assert!(
resolve_picture_exclusion(&picture, 0..1_000, 0..1_000, 0, PaperOrigin::default())
.is_none()
);
}
#[test]
fn signed_hwpunit_preserves_negative_offsets() {
assert_eq!(signed_hwpunit((-43892i32) as u32), -43892);
assert_eq!(signed_hwpunit(51100), 51100);
}
#[test]
fn para_topbottom_float_predicate_requires_non_tac_para_topbottom() {
let mut common = base_common();
assert!(is_para_topbottom_float(&common));
common.treat_as_char = true;
assert!(!is_para_topbottom_float(&common));
common.treat_as_char = false;
common.text_wrap = TextWrap::Square;
assert!(!is_para_topbottom_float(&common));
common.text_wrap = TextWrap::TopAndBottom;
common.vert_rel_to = VertRelTo::Page;
assert!(!is_para_topbottom_float(&common));
}
#[test]
fn lane_set_does_not_push_non_overlapping_ranges() {
let mut lanes = FloatLaneSet::new();
let first = lanes.place(None, 0.0, 100.0, 10.0, 40.0);
let second = lanes.place(None, 120.0, 200.0, 10.0, 20.0);
assert_eq!(first.bottom, 50.0);
assert_eq!(second.bottom, 30.0);
assert_eq!(lanes.max_bottom(), 50.0);
}
#[test]
fn lane_set_pushes_overlapping_ranges() {
let mut lanes = FloatLaneSet::new();
lanes.place(None, 0.0, 100.0, 10.0, 40.0);
let second = lanes.place(None, 90.0, 160.0, 10.0, 20.0);
assert_eq!(second.bottom, 70.0);
assert_eq!(lanes.max_bottom(), 70.0);
}
#[test]
fn horizontal_range_matches_column_right_offset_rule() {
let mut common = base_common();
common.horz_align = HorzAlign::Right;
common.horizontal_offset = 10;
let ctx = FloatPlacementContext::new(LayoutRect {
x: 20.0,
y: 0.0,
width: 200.0,
height: 100.0,
});
let (x0, x1) = horizontal_range(&common, 50.0, ctx, 7200.0);
assert_eq!(x0, 160.0);
assert_eq!(x1, 210.0);
}
#[test]
fn horizontal_range_uses_body_area_for_page_relative_objects() {
let mut common = base_common();
common.horz_rel_to = HorzRelTo::Page;
common.horz_align = HorzAlign::Center;
let ctx = FloatPlacementContext::new(LayoutRect {
x: 20.0,
y: 0.0,
width: 200.0,
height: 100.0,
})
.with_body_area(LayoutRect {
x: 40.0,
y: 0.0,
width: 300.0,
height: 100.0,
});
let (x0, x1) = horizontal_range(&common, 100.0, ctx, 7200.0);
assert_eq!(x0, 140.0);
assert_eq!(x1, 240.0);
}
fn stored_reset_fragment_candidate() -> (Paragraph, Table) {
let mut cell = Cell::new_empty(0, 0, 41_954, 2_282, 1);
cell.paragraphs = vec![
Paragraph {
line_segs: vec![
LineSeg {
vertical_pos: 0,
line_height: 2_000,
text_height: 1_000,
line_spacing: 1_000,
..Default::default()
},
LineSeg {
vertical_pos: 1_000,
line_height: 2_000,
text_height: 1_000,
line_spacing: 1_000,
..Default::default()
},
],
..Default::default()
},
Paragraph {
line_segs: vec![LineSeg {
vertical_pos: 0,
line_height: 2_000,
text_height: 1_000,
line_spacing: 1_000,
..Default::default()
}],
..Default::default()
},
];
let table = Table {
row_count: 1,
col_count: 1,
page_break: TablePageBreak::RowBreak,
padding: crate::model::Padding {
top: 141,
bottom: 141,
..Default::default()
},
common: CommonObjAttr {
width: 41_954,
height: 2_282,
treat_as_char: false,
text_wrap: TextWrap::TopAndBottom,
vert_rel_to: VertRelTo::Para,
vert_align: VertAlign::Top,
horz_rel_to: HorzRelTo::Column,
horz_align: HorzAlign::Left,
..Default::default()
},
outer_margin_left: 283,
outer_margin_right: 283,
outer_margin_top: 283,
outer_margin_bottom: 283,
cells: vec![cell],
..Default::default()
};
let host = Paragraph {
controls: vec![Control::Table(Box::new(table.clone()))],
..Default::default()
};
(host, table)
}
#[test]
fn stored_reset_fragment_geometry_separates_first_paint_height_from_successor_origin() {
{
let table = Table {
common: CommonObjAttr {
height: 50,
flow_with_text: true,
..base_common()
},
outer_margin_top: 2,
outer_margin_bottom: 2,
..Default::default()
};
let previous = Paragraph {
line_segs: vec![LineSeg {
vertical_pos: 100,
line_height: 20,
line_spacing: 10,
..Default::default()
}],
..Default::default()
};
let host = Paragraph {
text: " ".into(),
controls: vec![Control::Table(Box::new(table.clone()))],
line_segs: vec![LineSeg {
vertical_pos: 184,
line_height: 15,
line_spacing: -7,
..Default::default()
}],
..Default::default()
};
let mut next = Paragraph {
line_segs: vec![LineSeg {
vertical_pos: 195,
line_height: 10,
..Default::default()
}],
..Default::default()
};
let frame = stored_float_before_whitespace_line_frame(
&previous, &host, &next, &table, 50.0, 3.0, 7200.0,
)
.expect("저장 상자와 후속 줄의 간격이 닫힌다");
assert_eq!(frame.table_top, 132.0);
assert_eq!(frame.stored_successor_line_origin, Some(195.0));
next.line_segs[0].vertical_pos += 1;
assert!(stored_float_before_whitespace_line_frame(
&previous, &host, &next, &table, 50.0, 3.0, 7200.0,
)
.is_none());
}
let (host, table) = stored_reset_fragment_candidate();
assert_eq!(
native_hwp5_stored_reset_fragment_paint_geometry(true, &host, &table, false, &[], &[2],),
Some(NativeStoredResetFragmentPaintGeometry {
outer_top_hu: 283,
first_fragment_height_hu: Some(2_282),
})
);
assert_eq!(
native_hwp5_stored_reset_fragment_paint_geometry(true, &host, &table, true, &[2], &[],),
Some(NativeStoredResetFragmentPaintGeometry {
outer_top_hu: 283,
first_fragment_height_hu: None,
})
);
assert!(native_hwp5_stored_reset_fragment_paint_geometry(
true,
&host,
&table,
false,
&[],
&[1],
)
.is_none());
}
#[test]
fn stored_reset_fragment_geometry_rejects_unproven_neighboring_shapes() {
let (host, table) = stored_reset_fragment_candidate();
assert!(native_hwp5_stored_reset_fragment_paint_geometry(
false,
&host,
&table,
false,
&[],
&[2],
)
.is_none());
let mut visible_host = host.clone();
visible_host.text = "표 제목".to_string();
assert!(native_hwp5_stored_reset_fragment_paint_geometry(
true,
&visible_host,
&table,
false,
&[],
&[2],
)
.is_none());
let mut wrong_declared_height = table.clone();
wrong_declared_height.common.height += 1_000;
assert!(native_hwp5_stored_reset_fragment_paint_geometry(
true,
&host,
&wrong_declared_height,
false,
&[],
&[2],
)
.is_none());
let mut asymmetric_margin = table.clone();
asymmetric_margin.outer_margin_right += 1;
assert!(native_hwp5_stored_reset_fragment_paint_geometry(
true,
&host,
&asymmetric_margin,
false,
&[],
&[2],
)
.is_none());
let mut same_paragraph_rewind = table.clone();
let reset = same_paragraph_rewind.cells[0].paragraphs.remove(1);
same_paragraph_rewind.cells[0].paragraphs[0]
.line_segs
.extend(reset.line_segs);
assert!(native_hwp5_stored_reset_fragment_paint_geometry(
true,
&host,
&same_paragraph_rewind,
false,
&[],
&[2],
)
.is_none());
}
fn physical_outer_box_candidate() -> (Paragraph, Table, Paragraph) {
let table = Table {
row_count: 6,
col_count: 1,
page_break: TablePageBreak::RowBreak,
common: CommonObjAttr {
width: 41_954,
height: 23_790,
treat_as_char: false,
text_wrap: TextWrap::TopAndBottom,
vert_rel_to: VertRelTo::Para,
vert_align: VertAlign::Top,
horz_rel_to: HorzRelTo::Column,
horz_align: HorzAlign::Left,
..Default::default()
},
outer_margin_left: 283,
outer_margin_right: 283,
outer_margin_top: 283,
outer_margin_bottom: 283,
cells: (0..6)
.map(|row| Cell::new_empty(0, row, 41_954, 3_965, 1))
.collect(),
..Default::default()
};
let host = Paragraph {
line_segs: vec![LineSeg {
vertical_pos: 0,
line_height: 1,
..Default::default()
}],
controls: vec![Control::Table(Box::new(table.clone()))],
..Default::default()
};
let next = Paragraph {
line_segs: vec![LineSeg {
vertical_pos: 24_356,
line_height: 1_000,
..Default::default()
}],
..Default::default()
};
(host, table, next)
}
#[test]
fn physical_outer_box_paint_inset_requires_exact_native_stored_ladder() {
let (host, table, next) = physical_outer_box_candidate();
assert!(native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&table,
Some(&next),
));
assert!(!native_empty_host_physical_outer_box_paint_inset(
false,
&host,
&table,
Some(&next),
));
let mut short = next.clone();
short.line_segs[0].vertical_pos = 23_790;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&table,
Some(&short),
));
let mut mismatched = next.clone();
mismatched.line_segs[0].vertical_pos += 2;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&table,
Some(&mismatched),
));
let mut synthetic_host = host.clone();
synthetic_host.line_segs[0].tag = LineSeg::TAG_IMPLEMENTATION_PROPERTY;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&synthetic_host,
&table,
Some(&next),
));
let mut synthetic_next = next.clone();
synthetic_next.line_segs[0].tag = LineSeg::TAG_IMPLEMENTATION_PROPERTY;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&table,
Some(&synthetic_next),
));
}
#[test]
fn physical_outer_box_paint_inset_rejects_neighboring_float_contracts() {
let (host, table, next) = physical_outer_box_candidate();
let mut positive_offset = table.clone();
positive_offset.common.vertical_offset = 350;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&positive_offset,
Some(&next),
));
let mut horizontal_offset = table.clone();
horizontal_offset.common.horizontal_offset = 350;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&horizontal_offset,
Some(&next),
));
let mut visible_host = host.clone();
visible_host.text = "표 제목".to_string();
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&visible_host,
&table,
Some(&next),
));
let mut two_tables = host.clone();
two_tables
.controls
.push(Control::Table(Box::new(table.clone())));
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&two_tables,
&table,
Some(&next),
));
let mut next_object_host = next.clone();
next_object_host
.controls
.push(Control::Table(Box::new(table.clone())));
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&table,
Some(&next_object_host),
));
let mut next_visible = next.clone();
next_visible.text = "다음 본문".to_string();
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&table,
Some(&next_visible),
));
let mut tac = table.clone();
tac.common.treat_as_char = true;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&tac,
Some(&next),
));
let mut square = table.clone();
square.common.text_wrap = TextWrap::Square;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&square,
Some(&next),
));
let mut page_relative = table.clone();
page_relative.common.vert_rel_to = VertRelTo::Page;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&page_relative,
Some(&next),
));
let mut right_aligned = table.clone();
right_aligned.common.horz_align = HorzAlign::Right;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&right_aligned,
Some(&next),
));
let mut missing_margin = table.clone();
missing_margin.outer_margin_left = 0;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&missing_margin,
Some(&next),
));
let mut asymmetric_margin = table.clone();
asymmetric_margin.outer_margin_right += 1;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&asymmetric_margin,
Some(&next),
));
let mut one_by_one = table.clone();
one_by_one.row_count = 1;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&one_by_one,
Some(&next),
));
let mut two_columns = table.clone();
two_columns.col_count = 2;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&two_columns,
Some(&next),
));
let mut missing_cells = table.clone();
missing_cells.cells.clear();
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&missing_cells,
Some(&next),
));
let mut duplicate_row = table.clone();
duplicate_row.cells[1].row = 0;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&duplicate_row,
Some(&next),
));
let mut spanning_row = table.clone();
spanning_row.cells[0].row_span = 2;
assert!(!native_empty_host_physical_outer_box_paint_inset(
true,
&host,
&spanning_row,
Some(&next),
));
}
}