use super::{ComposedParagraph, ResolvedStyleSet, SingleLineOverflowCache};
use crate::model::{control::Control, paragraph::Paragraph, table::Table};
pub(crate) fn table_cells_have_no_stored_lines(table: &Table) -> bool {
!table.cells.is_empty()
&& table.cells.iter().all(|cell| {
cell.paragraphs
.iter()
.all(crate::renderer::para_has_no_stored_line_segs)
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn recompose_cell_lines_with_nested_tables(
composed: &mut ComposedParagraph,
source: &Paragraph,
width: f64,
styles: &ResolvedStyleSet,
dpi: f64,
stored_geometry_policy: impl Into<crate::model::provenance::StoredGeometryPolicy>,
native_hwp5: bool,
cache: &SingleLineOverflowCache,
mut nested_height: impl FnMut(&Table) -> f64,
) {
match recompose_cell_lines_with_nested_tables_with_storage(
composed,
source,
width,
styles,
dpi,
stored_geometry_policy,
native_hwp5,
cache,
|table| Ok(nested_height(table)),
&crate::storage::Unbounded,
) {
Ok(()) => (),
Err(never) => match never {},
}
}
pub(crate) fn recompose_cell_lines_with_nested_tables_with_storage<S: crate::storage::Storage>(
composed: &mut ComposedParagraph,
source: &Paragraph,
width: f64,
styles: &ResolvedStyleSet,
dpi: f64,
stored_geometry_policy: impl Into<crate::model::provenance::StoredGeometryPolicy>,
native_hwp5: bool,
cache: &SingleLineOverflowCache,
mut nested_height: impl FnMut(&Table) -> Result<f64, S::Error>,
storage: &S,
) -> Result<(), S::Error> {
let stored_geometry_policy = stored_geometry_policy.into();
let original = source;
let width_projection = (!stored_geometry_policy.preserve_hwp3_origins
&& source.controls.iter().any(|control| {
matches!(control, Control::Table(table)
if super::inline_table_width_cache_is_stale(table))
}))
.then(|| {
let mut paragraph = storage.clone_paragraph(source)?;
paragraph.line_segs.clear();
Ok::<_, S::Error>(paragraph)
})
.transpose()?;
let source = width_projection.as_ref().unwrap_or(source);
let stored_projection = if !stored_geometry_policy.preserve_hwp3_origins
&& !crate::renderer::para_has_no_stored_line_segs(source)
&& source.controls.iter().any(|control| {
matches!(control, Control::Table(table) if table.common.treat_as_char)
|| matches!(control, Control::Form(form) if form.common.treat_as_char)
}) {
super::inline_box_rows::normalized_host_with_table_height_with_storage(
source,
styles,
|table, _| {
let measured = nested_height(table)?;
Ok((measured.is_finite() && measured > 0.0)
.then(|| crate::renderer::px_to_hwpunit(measured, dpi)))
},
storage,
)?
} else {
None
};
let source = stored_projection.as_ref().unwrap_or(source);
if stored_projection.is_some() {
let mut refreshed =
super::compose_paragraph_in_context_with_storage(source, styles, storage)?;
super::preserve_context_resolved_runs_with_storage(composed, &mut refreshed, storage)?;
*composed = refreshed;
}
let mut projected = None;
let mut heights = Vec::new();
let reflowed_table_host = (!native_hwp5 || width_projection.is_some())
&& !stored_geometry_policy.preserve_hwp3_origins
&& crate::renderer::para_has_no_stored_line_segs(source)
&& source.controls.iter().any(|control| {
matches!(control, Control::Table(table)
if super::inline_table_needs_reflow_measurement(table))
});
if reflowed_table_host {
for (index, control) in source.controls.iter().enumerate() {
let Control::Table(table) = control else {
continue;
};
if !table.common.treat_as_char {
continue;
}
let measured = nested_height(table)?;
let Ok(height) = u32::try_from(crate::renderer::px_to_hwpunit(measured, dpi)) else {
continue;
};
let height = if super::inline_table_needs_reflow_measurement(table) {
height
} else {
height.max(table.common.height)
};
storage.push(&mut heights, (index, height))?;
}
if !heights.is_empty() {
let mut paragraph = storage.clone_paragraph(source)?;
paragraph.line_segs.clear();
for &(index, height) in &heights {
if let Control::Table(table) = &mut paragraph.controls[index] {
table.common.height = height;
}
}
projected = Some(paragraph);
}
}
if let Some(paragraph) = &projected {
if let Some(rebuilt) =
super::recompose_stored_lines_in_frame_with_known_square_band_with_storage(
composed,
paragraph,
super::ParagraphBox::content_for_style(
width,
styles.para_styles.get(paragraph.para_shape_id as usize),
dpi,
),
width,
styles,
dpi,
stored_geometry_policy,
super::line_breaking::StoredRowMissPolicy::Reflow,
&[],
false,
false,
false,
&heights,
storage,
)?
{
*composed = rebuilt;
}
} else {
super::recompose_horizontal_cell_lines_for_width_with_storage(
composed,
source,
width,
styles,
dpi,
stored_geometry_policy,
native_hwp5,
cache,
storage,
)?;
}
if let Some(paragraph) = stored_projection {
storage.bytes(std::mem::size_of::<super::frame_geometry::FrameGeometry>())?;
storage.bytes(
paragraph.line_segs.len()
* (std::mem::size_of::<crate::model::paragraph::LineSeg>()
+ std::mem::size_of::<(u32, super::SpaceMetric)>()),
)?;
let geometry = composed.frame_geometry.get_or_insert_with(|| {
std::sync::Arc::new(super::frame_geometry::FrameGeometry {
space_metrics: paragraph
.line_segs
.iter()
.enumerate()
.map(|(index, line)| (line.text_start, paragraph.line_space_metric(index)))
.collect(),
line_segs: paragraph.line_segs.clone(),
inline_table_heights: Vec::new(),
stored_partition: true,
})
});
let geometry = std::sync::Arc::make_mut(geometry);
for (index, (old, new)) in original
.controls
.iter()
.zip(¶graph.controls)
.enumerate()
{
if let (Control::Table(old), Control::Table(new)) = (old, new) {
if old.common.height != new.common.height {
geometry
.inline_table_heights
.retain(|(owner, _)| *owner != index);
storage.push(
&mut geometry.inline_table_heights,
(index, new.common.height),
)?;
}
}
}
}
Ok(())
}