pub(crate) mod cached_frame_owner;
mod stored_nested_text_frames;
pub(crate) use stored_nested_text_frames::{StoredNestedTextFrames, StoredTextFrame};
use crate::model::control::Control;
use crate::model::document::Document;
use crate::model::table::{Cell, Table};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
pub(crate) mod math_table;
pub(crate) mod math_textbox;
pub(crate) mod stored_band_crossing_line;
pub(crate) mod stored_text_metrics;
pub(crate) mod stored_word_tail;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum RenderPathEntry {
TableCell {
control_index: usize,
cell_index: usize,
paragraph_index: usize,
},
TableCaption {
control_index: usize,
paragraph_index: usize,
},
ShapeTextBox {
control_index: usize,
textbox_index: usize,
paragraph_index: usize,
},
PictureCaption {
control_index: usize,
paragraph_index: usize,
},
}
impl crate::storage::CloneWithStorage for RenderPathEntry {
fn clone_with_storage<S: crate::storage::Storage>(
&self,
storage: &S,
) -> Result<Self, S::Error> {
let _ = storage;
Ok(self.clone())
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct RenderPath {
pub section_index: usize,
pub parent_paragraph_index: usize,
pub entries: Vec<RenderPathEntry>,
pub target_control_index: Option<usize>,
}
crate::storage::clone_fields!(RenderPath, {section_index, parent_paragraph_index, entries, target_control_index});
pub(crate) const NESTED_STRETCH_MIN_RATIO: f64 = 1.0;
impl RenderPath {
pub fn top_level(section_index: usize, parent_paragraph_index: usize) -> Self {
Self {
section_index,
parent_paragraph_index,
entries: Vec::new(),
target_control_index: None,
}
}
}
#[derive(Clone, Debug)]
pub struct NestedTableWidthProjection {
pub path: RenderPath,
pub source_width: u32,
pub effective_width: u32,
pub width_scale: f64,
pub use_owner_content_box: bool,
table_pointer: usize,
}
#[derive(Clone, Debug, Default)]
pub struct RenderNormalizationOverlay {
nested_table_widths_by_path: HashMap<RenderPath, Arc<NestedTableWidthProjection>>,
nested_table_widths_by_pointer: HashMap<usize, Arc<NestedTableWidthProjection>>,
text_reflowed_tables_by_pointer: HashSet<usize>,
rendered_cell_frame_changes: HashMap<usize, HashSet<(u16, u16)>>,
rebased_table_frames_by_pointer: HashSet<usize>,
expanded_inline_host_tables_by_pointer: HashSet<usize>,
nested_tables_with_reflowed_hosts: HashSet<usize>,
stored_nested_text_frames_by_pointer: HashMap<usize, Arc<StoredNestedTextFrames>>,
}
impl RenderNormalizationOverlay {
pub fn from_document(document: &Document) -> Self {
Self::from_document_reusing(document, &Self::default())
}
pub fn from_document_reusing(document: &Document, previous: &Self) -> Self {
match Self::from_document_reusing_with_storage(
document,
previous,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn from_document_reusing_with_storage<S: crate::storage::Storage>(
document: &Document,
previous: &Self,
storage: &S,
) -> Result<Self, S::Error> {
Ok({
let mut overlay = Self {
stored_nested_text_frames_by_pointer:
stored_nested_text_frames::collect_with_storage(document, storage)?,
..Self::default()
};
let hwp5_stored_pagination_layout =
document.layout_profile().hwp5_stored_pagination_layout();
for (section_index, section) in document.sections.iter().enumerate() {
for (parent_paragraph_index, paragraph) in section.paragraphs.iter().enumerate() {
for (control_index, control) in paragraph.controls.iter().enumerate() {
let Control::Table(table) = control else {
continue;
};
let path = RenderPath::top_level(section_index, parent_paragraph_index);
if hwp5_stored_pagination_layout && table.common.treat_as_char {
let page_def = §ion.section_def.page_def;
let body_width = page_def
.width
.saturating_sub(page_def.margin_left)
.saturating_sub(page_def.margin_right);
let source_width = table.common.width;
if body_width > 0
&& source_width > body_width
&& f64::from(body_width) >= f64::from(source_width) * 0.9
&& stored_first_cell_line_says_shrunk(
table,
body_width,
source_width,
)
{
let mut top_path = crate::storage::try_clone(&path, storage)?;
top_path.target_control_index = Some(control_index);
let table_pointer = table.as_ref() as *const Table as usize;
let projection = {
storage
.bytes(std::mem::size_of::<NestedTableWidthProjection>())?;
Arc::new(NestedTableWidthProjection {
path: crate::storage::try_clone(&top_path, storage)?,
source_width,
effective_width: body_width,
width_scale: f64::from(body_width)
/ f64::from(source_width),
use_owner_content_box: false,
table_pointer,
})
};
storage
.reserve_map(&mut overlay.nested_table_widths_by_pointer, 1)?;
overlay
.nested_table_widths_by_pointer
.insert(table_pointer, Arc::clone(&projection));
storage.reserve_map(&mut overlay.nested_table_widths_by_path, 1)?;
overlay
.nested_table_widths_by_path
.insert(top_path, projection);
}
}
overlay.collect_nested_tables_with_storage(
table,
path,
control_index,
previous,
storage,
)?;
}
}
}
overlay
})
}
pub(crate) fn stored_nested_text_frames(
&self,
table: &Table,
) -> Option<Arc<StoredNestedTextFrames>> {
if self.table_text_reflowed(table) {
return None;
}
self.stored_nested_text_frames_by_pointer
.get(&(table as *const Table as usize))
.cloned()
}
fn collect_nested_tables(
&mut self,
owner_table: &Table,
path: RenderPath,
owner_control_index: usize,
previous: &Self,
) {
match self.collect_nested_tables_with_storage(
owner_table,
path,
owner_control_index,
previous,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn collect_nested_tables_with_storage<S: crate::storage::Storage>(
&mut self,
owner_table: &Table,
path: RenderPath,
owner_control_index: usize,
previous: &Self,
storage: &S,
) -> Result<(), S::Error> {
for (cell_index, cell) in owner_table.cells.iter().enumerate() {
if cell.width >= 0x8000_0000 {
continue;
}
let host_reflowed = crate::renderer::typeset::is_reparsed_single_column_cell_split_row(
owner_table,
usize::from(cell.row),
);
for (paragraph_index, paragraph) in cell.paragraphs.iter().enumerate() {
for (control_index, control) in paragraph.controls.iter().enumerate() {
let Control::Table(nested) = control else {
continue;
};
if host_reflowed {
storage.reserve_set(&mut self.nested_tables_with_reflowed_hosts, 1)?;
self.nested_tables_with_reflowed_hosts
.insert(nested.as_ref() as *const Table as usize);
}
let mut nested_path = crate::storage::try_clone(&path, storage)?;
storage.push(
&mut nested_path.entries,
RenderPathEntry::TableCell {
control_index: owner_control_index,
cell_index,
paragraph_index,
},
)?;
nested_path.target_control_index = Some(control_index);
let source_width = nested.common.width;
let keeps_legacy_near_fit_projection = !nested.common.treat_as_char
&& source_width > 0
&& u64::from(source_width) < u64::from(cell.width)
&& f64::from(source_width)
>= f64::from(cell.width) * NESTED_STRETCH_MIN_RATIO;
if keeps_legacy_near_fit_projection {
let effective_width = cell.width;
let table_pointer = nested.as_ref() as *const Table as usize;
let projection = if let Some(projection) = previous
.nested_table_widths_by_path
.get(&nested_path)
.filter(|projection| {
projection.source_width == source_width
&& projection.effective_width == effective_width
&& !projection.use_owner_content_box
&& projection.table_pointer == table_pointer
})
.map(Arc::clone)
{
projection
} else {
storage.bytes(std::mem::size_of::<NestedTableWidthProjection>())?;
Arc::new(NestedTableWidthProjection {
path: crate::storage::try_clone(&nested_path, storage)?,
source_width,
effective_width,
width_scale: f64::from(effective_width) / f64::from(source_width),
use_owner_content_box: false,
table_pointer,
})
};
storage.reserve_map(&mut self.nested_table_widths_by_pointer, 1)?;
self.nested_table_widths_by_pointer
.insert(projection.table_pointer, Arc::clone(&projection));
storage.reserve_map(&mut self.nested_table_widths_by_path, 1)?;
self.nested_table_widths_by_path.insert(
crate::storage::try_clone(&nested_path, storage)?,
projection,
);
}
self.collect_nested_tables_with_storage(
nested,
nested_path,
control_index,
previous,
storage,
)?;
}
}
}
Ok(())
}
#[inline]
pub fn nested_table_width_scale(&self, table: &Table) -> f64 {
let key = table as *const Table as usize;
self.nested_table_widths_by_pointer
.get(&key)
.map(|projection| projection.width_scale)
.unwrap_or(1.0)
}
#[inline]
pub fn uses_owner_content_box(&self, table: &Table) -> bool {
let key = table as *const Table as usize;
self.nested_table_widths_by_pointer
.get(&key)
.is_some_and(|projection| projection.use_owner_content_box)
}
pub fn projection_for_path(
&self,
path: &RenderPath,
) -> Option<Arc<NestedTableWidthProjection>> {
self.nested_table_widths_by_path.get(path).map(Arc::clone)
}
pub fn nested_table_projection_count(&self) -> usize {
self.nested_table_widths_by_path.len()
}
pub(crate) fn register_text_reflowed_table(&mut self, table: &Table) {
match self.register_text_reflowed_table_with_storage(table, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn register_text_reflowed_table_with_storage<S: crate::storage::Storage>(
&mut self,
table: &Table,
storage: &S,
) -> Result<(), S::Error> {
storage.reserve_set(&mut self.text_reflowed_tables_by_pointer, 1)?;
self.rendered_cell_frame_changes
.remove(&(table as *const Table as usize));
self.text_reflowed_tables_by_pointer
.insert(table as *const Table as usize);
Ok(())
}
pub(crate) fn register_rendered_text_partitions(
&mut self,
source: &[crate::model::paragraph::Paragraph],
rendered: &[crate::model::paragraph::Paragraph],
styles: &crate::renderer::style_resolver::ResolvedStyleSet,
) -> (bool, bool) {
match self.register_rendered_text_partitions_with_storage(
source,
rendered,
styles,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn register_rendered_text_partitions_with_storage<S: crate::storage::Storage>(
&mut self,
source: &[crate::model::paragraph::Paragraph],
rendered: &[crate::model::paragraph::Paragraph],
styles: &crate::renderer::style_resolver::ResolvedStyleSet,
storage: &S,
) -> Result<(bool, bool), S::Error> {
let mut changed = source.len() != rendered.len();
let mut flow_changed = changed;
let mut upstream_flow_changed = false;
for (original, current) in source.iter().zip(rendered) {
if original.column_type != crate::model::paragraph::ColumnBreakType::None
|| styles
.para_styles
.get(original.para_shape_id as usize)
.is_some_and(|style| style.page_break_before)
{
upstream_flow_changed = false;
}
let partition_changed = original.line_segs.len() != current.line_segs.len()
|| (0..original.line_segs.len()).any(|index| {
original.line_seg_text_start(index) != current.line_seg_text_start(index)
});
let physical_slots_preserved = partition_changed
&& !original.line_segs.is_empty()
&& original.line_segs.len() == current.line_segs.len()
&& original.source_line_seg_vertical_pos == current.source_line_seg_vertical_pos
&& !original.stored_text_partition_is_dirty()
&& !current.stored_text_partition_is_dirty()
&& !original.cell_format_vpos_dirty
&& !current.cell_format_vpos_dirty
&& original
.line_segs
.iter()
.zip(¤t.line_segs)
.all(|(source, rendered)| {
if source.tag
& crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY
!= 0
{
return false;
}
let mut same_slot = source.clone();
same_slot.text_start = rendered.text_start;
same_slot == *rendered
});
changed |= partition_changed && !physical_slots_preserved;
let extent = |paragraph: &crate::model::paragraph::Paragraph| {
paragraph
.line_segs
.first()
.zip(paragraph.line_segs.last())
.map(|(first, last)| {
i64::from(last.vertical_pos)
+ i64::from(last.line_height)
+ i64::from(last.line_spacing)
- i64::from(first.vertical_pos)
})
};
let mut paragraph_flow_changed = extent(original) != extent(current)
|| original.line_segs.len() != current.line_segs.len()
|| original
.line_segs
.iter()
.zip(¤t.line_segs)
.any(|(a, b)| {
a.line_height != b.line_height || a.line_spacing != b.line_spacing
});
for (old, new) in original.controls.iter().zip(¤t.controls) {
let (Control::Table(old), Control::Table(new)) = (old, new) else {
continue;
};
let old_pointer = old.as_ref() as *const Table as usize;
let new_pointer = new.as_ref() as *const Table as usize;
if old.common.treat_as_char && new.common.treat_as_char {
if let ([before], [after]) =
(original.line_segs.as_slice(), current.line_segs.as_slice())
{
let source_box_height = i64::from(old.common.height)
+ i64::from(old.outer_margin_top)
+ i64::from(old.outer_margin_bottom);
if i64::from(before.line_height) < source_box_height
&& after.line_height > before.line_height
{
storage.insert_set(
&mut self.expanded_inline_host_tables_by_pointer,
old_pointer,
)?;
storage.insert_set(
&mut self.expanded_inline_host_tables_by_pointer,
new_pointer,
)?;
}
}
}
if !self.rendered_cell_frame_changes.contains_key(&old_pointer) {
storage.reserve_map(&mut self.rendered_cell_frame_changes, 1)?;
}
self.rendered_cell_frame_changes
.entry(old_pointer)
.or_default();
if !self.rendered_cell_frame_changes.contains_key(&new_pointer) {
storage.reserve_map(&mut self.rendered_cell_frame_changes, 1)?;
}
self.rendered_cell_frame_changes
.entry(new_pointer)
.or_default();
let mut table_changed = old.cells.len() != new.cells.len();
let mut table_flow_changed = table_changed;
for (old_cell, new_cell) in old.cells.iter().zip(&new.cells) {
let (text, flow) = self.register_rendered_text_partitions_with_storage(
&old_cell.paragraphs,
&new_cell.paragraphs,
styles,
storage,
)?;
if text || flow {
storage.insert_set(
self.rendered_cell_frame_changes
.get_mut(&old_pointer)
.unwrap(),
(old_cell.row, old_cell.col),
)?;
storage.insert_set(
self.rendered_cell_frame_changes
.get_mut(&new_pointer)
.unwrap(),
(new_cell.row, new_cell.col),
)?;
}
table_changed |= text;
table_flow_changed |= flow;
}
if table_changed {
storage.insert_set(&mut self.text_reflowed_tables_by_pointer, old_pointer)?;
storage.insert_set(&mut self.text_reflowed_tables_by_pointer, new_pointer)?;
}
if upstream_flow_changed {
self.register_rebased_source_frames_with_storage(old, new, storage)?;
}
changed |= table_changed;
paragraph_flow_changed |= table_flow_changed;
}
flow_changed |= paragraph_flow_changed;
upstream_flow_changed |= paragraph_flow_changed;
}
Ok((changed, flow_changed))
}
fn register_rebased_source_frames(&mut self, source: &Table, rendered: &Table) -> bool {
match self.register_rebased_source_frames_with_storage(
source,
rendered,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
fn register_rebased_source_frames_with_storage<S: crate::storage::Storage>(
&mut self,
source: &Table,
rendered: &Table,
storage: &S,
) -> Result<bool, S::Error> {
if source.common.treat_as_char
|| source.common.text_wrap != crate::model::shape::TextWrap::TopAndBottom
|| source.layout_page_break() != crate::model::table::TablePageBreak::RowBreak
{
return Ok(false);
}
let mut frames = source.cells.iter().any(|cell| {
let mut previous = None;
cell.paragraphs
.iter()
.flat_map(|paragraph| ¶graph.line_segs)
.any(|line| {
let boundary = previous.is_some_and(|vpos| {
line.is_first_line_of_page()
|| line.is_first_line_of_column()
|| line.vertical_pos < vpos
});
previous = Some(line.vertical_pos);
boundary
})
});
for (old_cell, new_cell) in source.cells.iter().zip(&rendered.cells) {
for (old_para, new_para) in old_cell.paragraphs.iter().zip(&new_cell.paragraphs) {
for (old, new) in old_para.controls.iter().zip(&new_para.controls) {
if let (Control::Table(old), Control::Table(new)) = (old, new) {
frames |=
self.register_rebased_source_frames_with_storage(old, new, storage)?;
}
}
}
}
if frames {
for table in [source, rendered] {
let pointer = table as *const Table as usize;
storage.insert_set(&mut self.text_reflowed_tables_by_pointer, pointer)?;
storage.insert_set(&mut self.rebased_table_frames_by_pointer, pointer)?;
}
}
Ok(frames)
}
#[inline]
pub(crate) fn table_text_reflowed(&self, table: &Table) -> bool {
self.text_reflowed_tables_by_pointer
.contains(&(table as *const Table as usize))
}
pub(crate) fn inline_host_frame_expanded(&self, table: &Table) -> bool {
self.expanded_inline_host_tables_by_pointer
.contains(&(table as *const Table as usize))
}
pub(crate) fn table_frame_reflowed_without_cell_scope(&self, table: &Table) -> bool {
self.rebased_table_frames_by_pointer
.contains(&(table as *const Table as usize))
|| (self.table_text_reflowed(table)
&& !self
.rendered_cell_frame_changes
.contains_key(&(table as *const Table as usize)))
}
pub(crate) fn cell_frame_reflowed(
&self,
table: &Table,
cell: &crate::model::table::Cell,
) -> bool {
self.rebased_table_frames_by_pointer
.contains(&(table as *const Table as usize))
|| self.cell_text_partition_reflowed(table, cell)
}
pub(crate) fn cell_text_partition_reflowed(&self, table: &Table, cell: &Cell) -> bool {
self.rendered_cell_frame_changes
.get(&(table as *const Table as usize))
.map_or_else(
|| self.table_text_reflowed(table),
|cells| cells.contains(&(cell.row, cell.col)),
)
}
pub(crate) fn cell_has_current_render_partition(&self, table: &Table, cell: &Cell) -> bool {
self.rendered_cell_frame_changes
.get(&(table as *const Table as usize))
.is_some_and(|cells| cells.contains(&(cell.row, cell.col)))
}
pub(crate) fn row_frame_reflowed(&self, table: &Table, row: usize) -> bool {
table.cells.iter().any(|cell| {
usize::from(cell.row) <= row
&& row < usize::from(cell.row) + usize::from(cell.row_span)
&& self.cell_frame_reflowed(table, cell)
})
}
pub(crate) fn frame_prefix_reflowed(
&self,
table: &Table,
rows: &[f64],
spacing: f64,
height: f64,
) -> bool {
let mut top = 0.0;
for (row, row_height) in rows.iter().enumerate() {
if top >= height {
break;
}
if self.row_frame_reflowed(table, row) {
return true;
}
top += row_height + spacing;
}
false
}
#[inline]
pub(crate) fn nested_table_has_reflowed_host(&self, table: &Table) -> bool {
self.nested_tables_with_reflowed_hosts
.contains(&(table as *const Table as usize))
}
pub(crate) fn stored_opening_row_frame(
&self,
table: &Table,
) -> Option<super::float_placement::StoredOpeningRowFrame> {
if self.table_text_reflowed(table) || self.nested_table_has_reflowed_host(table) {
return None;
}
super::float_placement::stored_plain_opening_row_frame(table)
}
}
fn stored_first_cell_line_says_shrunk(table: &Table, body_width: u32, source_width: u32) -> bool {
let Some(cell) = table.cells.iter().find(|c| c.row == 0 && c.col == 0) else {
return false;
};
let Some(seg) = cell
.paragraphs
.iter()
.find_map(|paragraph| paragraph.line_segs.first())
else {
return false;
};
if seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0 {
return false;
}
let padding = cell.effective_padding(&table.padding);
let padding_h = i64::from(padding.left) + i64::from(padding.right);
let declared_inner = i64::from(cell.width) - padding_h;
let scale = f64::from(body_width) / f64::from(source_width);
let shrunk_inner = (f64::from(cell.width) * scale) as i64 - padding_h;
let stored = i64::from(seg.column_start.max(0)) + i64::from(seg.segment_width);
(stored - shrunk_inner).abs() < (stored - declared_inner).abs()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::document::Section;
use crate::model::paragraph::Paragraph;
use crate::model::shape::{TextWrap, VertRelTo};
use crate::model::table::TablePageBreak;
#[test]
fn nested_tables_keep_declared_width() {
let narrow = document_with_nested_table(1_358, 2_000); let overlay = RenderNormalizationOverlay::from_document(&narrow);
assert_eq!(overlay.nested_table_projection_count(), 0);
let near_fit = document_with_nested_table(1_912, 2_000); let overlay = RenderNormalizationOverlay::from_document(&near_fit);
assert_eq!(overlay.nested_table_projection_count(), 0);
}
fn document_with_nested_table(source_width: u32, parent_width: u32) -> Document {
let mut nested = Table::default();
nested.row_count = 1;
nested.col_count = 1;
nested.common.width = source_width;
nested.cells.push(Cell {
col_span: 1,
row_span: 1,
width: source_width,
paragraphs: vec![Paragraph::default()],
..Cell::default()
});
let mut cell_paragraph = Paragraph::default();
cell_paragraph
.controls
.push(Control::Table(Box::new(nested)));
let mut owner = Table::default();
owner.row_count = 1;
owner.col_count = 1;
owner.common.width = parent_width;
owner.cells.push(Cell {
col_span: 1,
row_span: 1,
width: parent_width,
paragraphs: vec![cell_paragraph],
..Cell::default()
});
let mut parent = Paragraph::default();
parent.controls.push(Control::Table(Box::new(owner)));
let mut section = Section::default();
section.paragraphs.push(parent);
let mut document = Document::default();
document.sections.push(section);
document
}
fn nested_table(document: &Document) -> &Table {
let Control::Table(owner) = &document.sections[0].paragraphs[0].controls[0] else {
panic!("owner table");
};
let Control::Table(nested) = &owner.cells[0].paragraphs[0].controls[0] else {
panic!("nested table");
};
nested
}
#[test]
fn parent_and_child_height_order_does_not_expand_absolute_child_width() {
let document = short_rowbreak_document(1_912, 2_000, 1_000, 2_000);
let overlay = RenderNormalizationOverlay::from_document(&document);
let nested = nested_table_at_final_row(&document);
assert!(overlay
.projection_for_path(&short_rowbreak_nested_path())
.is_none());
assert_eq!(nested.common.width, 1_912, "원본 절대 폭 보존");
assert_eq!(overlay.nested_table_width_scale(nested), 1.0);
assert!(!overlay.uses_owner_content_box(nested));
let long_owner = short_rowbreak_document(1_912, 2_000, 5_000, 1_000);
let overlay = RenderNormalizationOverlay::from_document(&long_owner);
let nested = nested_table_at_final_row(&long_owner);
assert!(overlay
.projection_for_path(&short_rowbreak_nested_path())
.is_none());
assert!(!overlay.uses_owner_content_box(nested));
}
fn short_rowbreak_document(
source_width: u32,
parent_width: u32,
parent_height: u32,
child_height: u32,
) -> Document {
let mut nested = Table::default();
nested.row_count = 1;
nested.col_count = 1;
nested.common.width = source_width;
nested.common.height = child_height;
nested.cells.push(Cell {
col_span: 1,
row_span: 1,
width: source_width,
paragraphs: vec![Paragraph::default()],
..Cell::default()
});
let mut host = Paragraph::default();
host.controls.push(Control::Table(Box::new(nested)));
let mut owner = Table::default();
owner.row_count = 2;
owner.col_count = 1;
owner.page_break = TablePageBreak::RowBreak;
owner.common.width = parent_width;
owner.common.height = parent_height;
owner.common.text_wrap = TextWrap::TopAndBottom;
owner.common.vert_rel_to = VertRelTo::Para;
owner.cells.push(Cell {
row: 0,
col_span: 1,
row_span: 1,
width: parent_width,
paragraphs: vec![Paragraph::default()],
..Cell::default()
});
owner.cells.push(Cell {
row: 1,
col_span: 1,
row_span: 1,
width: parent_width,
paragraphs: vec![host, Paragraph::default()],
..Cell::default()
});
let mut parent = Paragraph::default();
parent.controls.push(Control::Table(Box::new(owner)));
let mut section = Section::default();
section.paragraphs.push(parent);
let mut document = Document::default();
document.sections.push(section);
document
}
fn nested_table_at_final_row(document: &Document) -> &Table {
let Control::Table(owner) = &document.sections[0].paragraphs[0].controls[0] else {
panic!("owner table");
};
let Control::Table(nested) = &owner.cells[1].paragraphs[0].controls[0] else {
panic!("nested table");
};
nested
}
fn short_rowbreak_nested_path() -> RenderPath {
RenderPath {
section_index: 0,
parent_paragraph_index: 0,
entries: vec![RenderPathEntry::TableCell {
control_index: 0,
cell_index: 1,
paragraph_index: 0,
}],
target_control_index: Some(0),
}
}
fn nested_path() -> RenderPath {
RenderPath {
section_index: 0,
parent_paragraph_index: 0,
entries: vec![RenderPathEntry::TableCell {
control_index: 0,
cell_index: 0,
paragraph_index: 0,
}],
target_control_index: Some(0),
}
}
#[test]
fn near_fit_nested_table_has_no_render_width_projection() {
let document = document_with_nested_table(1_900, 2_000);
let overlay = RenderNormalizationOverlay::from_document(&document);
let nested = nested_table(&document);
assert_eq!(nested.common.width, 1_900, "source width must not change");
assert_eq!(nested.cells[0].width, 1_900, "source cell width");
assert!(
(overlay.nested_table_width_scale(nested) - 1.0).abs() < f64::EPSILON,
"stored nested-table width must be used without parent-cell projection"
);
assert!(overlay.projection_for_path(&nested_path()).is_none());
}
#[test]
fn repeated_normalization_has_no_stale_width_projection() {
let document = document_with_nested_table(1_900, 2_000);
let first = RenderNormalizationOverlay::from_document(&document);
let second = RenderNormalizationOverlay::from_document_reusing(&document, &first);
assert_eq!(first.nested_table_projection_count(), 0);
assert_eq!(second.nested_table_projection_count(), 0);
assert!(first.projection_for_path(&nested_path()).is_none());
assert!(second.projection_for_path(&nested_path()).is_none());
}
#[test]
fn removed_source_path_does_not_reuse_stale_projection() {
let mut document = document_with_nested_table(1_900, 2_000);
let first = RenderNormalizationOverlay::from_document(&document);
let Control::Table(owner) = &mut document.sections[0].paragraphs[0].controls[0] else {
panic!("owner table");
};
owner.cells[0].paragraphs[0].controls.clear();
let second = RenderNormalizationOverlay::from_document_reusing(&document, &first);
assert_eq!(second.nested_table_projection_count(), 0);
assert!(
second.projection_for_path(&nested_path()).is_none(),
"a missing logical source path must never fall back to the previous projection"
);
}
}