use super::{
native_hwp5_footnote_reset_fragments, para_has_non_whitespace_text,
queued_table_footnote_content_height, FormattedTable, TableCellFootnote,
};
use crate::model::control::Control;
use crate::model::{paragraph::Paragraph, provenance::LayoutCompatibilityProfile, table::Table};
use crate::renderer::composer::ComposedParagraph;
use crate::renderer::float_placement::is_para_topbottom_float;
use crate::renderer::height_measurer::{
fit_measured_table_declared_tail_to_declared_height,
fit_measured_table_nested_tail_to_declared_height, fit_measured_table_to_declared_height,
fit_stored_hwpx_no_adjust_rowspans, fit_stored_inline_picture_frame,
trim_stored_hwpx_inline_row_trailing_spacing, MeasuredTable,
};
use crate::renderer::hwpunit_to_px;
use crate::renderer::pagination::estimate_footnote_note_height;
use crate::renderer::style_resolver::ResolvedStyleSet;
pub(super) mod block;
pub(super) mod continuation;
pub(super) mod deferred_float_flow;
pub(super) mod footnotes;
mod host_spacing;
pub(super) mod scan;
mod source_frame_fit;
pub(super) struct TableFormatInput<'a> {
pub(super) para: &'a Paragraph,
pub(super) para_idx: usize,
pub(super) ctrl_idx: usize,
pub(super) table: &'a Table,
pub(super) measured_tables: &'a [MeasuredTable],
pub(super) styles: &'a ResolvedStyleSet,
pub(super) composed: Option<&'a ComposedParagraph>,
pub(super) next_para: Option<&'a Paragraph>,
pub(super) is_column_top: bool,
pub(super) external_flow: Option<crate::renderer::float_placement::ReflowExternalTableFlow>,
pub(super) unchanged_coanchored_frame: bool,
}
pub(super) fn format(
input: TableFormatInput<'_>,
dpi: f64,
profile: impl Fn() -> LayoutCompatibilityProfile,
uses_tac_flow: impl FnOnce() -> bool,
) -> FormattedTable {
match format_with_storage(
input,
dpi,
profile,
uses_tac_flow,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn format_with_storage<S: crate::storage::Storage>(
input: TableFormatInput<'_>,
dpi: f64,
profile: impl Fn() -> LayoutCompatibilityProfile,
uses_tac_flow: impl FnOnce() -> bool,
storage: &S,
) -> Result<FormattedTable, S::Error> {
let TableFormatInput {
para,
para_idx,
ctrl_idx,
table,
measured_tables,
styles,
composed,
next_para,
is_column_top,
external_flow,
unchanged_coanchored_frame,
} = input;
let mt = measured_tables
.iter()
.find(|mt| mt.para_index == para_idx && mt.control_index == ctrl_idx);
let closed_coanchored_mt = if unchanged_coanchored_frame {
mt.map(|measured| {
crate::renderer::height_measurer::fit_measured_table_declared_tail_to_declared_height_with_storage(
measured, table, dpi, storage,
)
})
.transpose()?
.flatten()
} else {
None
};
let fitted_visible_mt = match closed_coanchored_mt {
Some(closed) => Some(closed),
None => fit_measured_for_host_with_storage(
para, next_para, table, mt, dpi, styles, &profile, storage,
)?,
};
let mt = fitted_visible_mt.as_ref().or(mt);
let is_tac = uses_tac_flow();
let host_spacing::HostSpacingResult {
host_spacing,
strict_following_plain_text_fit,
} = host_spacing::resolve(
host_spacing::HostSpacingInput {
para,
ctrl_idx,
table,
styles,
composed,
next_para,
is_column_top,
is_tac,
},
dpi,
&profile,
);
let (
row_heights,
cell_spacing,
effective_height,
caption_height,
cumulative_heights,
page_break,
cells,
header_row_count,
) = if let Some(mt) = mt {
let hrc = if mt.repeat_header && mt.has_header_cells {
1
} else {
0
};
(
crate::storage::try_clone(&mt.row_heights, storage)?,
mt.cell_spacing,
mt.total_height,
mt.caption_height,
crate::storage::try_clone(&mt.cumulative_heights, storage)?,
mt.page_break,
crate::storage::try_clone(&mt.cells, storage)?,
hrc,
)
} else {
(
Vec::new(),
0.0,
0.0,
0.0,
storage.collect([0.0])?,
Default::default(),
Vec::new(),
0,
)
};
let effective_height = crate::renderer::height_measurer::whole_table_frame_height(
table,
effective_height,
dpi,
profile().hwp5_stored_pagination_layout() || profile().hwpx_stored_layout(),
styles,
);
let effective_height = external_flow.map_or(effective_height, |flow| flow.effective_height);
let total_height = effective_height + host_spacing.before + host_spacing.after;
let mut table_footnote_height = 0.0;
let source_notes =
crate::renderer::pagination::table_cell_footnotes_with_storage(table, storage)?;
let table_footnote_count = source_notes.len();
let mut table_footnotes = Vec::new();
for (cell_path, fn_ctrl) in source_notes {
let owner = cell_path[0];
let fn_height = estimate_footnote_note_height(fn_ctrl, dpi);
table_footnote_height += fn_height;
let fragment_split = (profile().hwp5_stored_pagination_layout()
|| (profile().hwpx_stored_layout() && !profile().session_edited()))
.then(|| native_hwp5_footnote_reset_fragments(fn_ctrl, dpi))
.flatten();
storage.push(
&mut table_footnotes,
TableCellFootnote {
number: fn_ctrl.number,
row: table.cells[owner.cell_index].row as usize,
cell_path,
content_height: queued_table_footnote_content_height(fn_ctrl, dpi),
fragment_split,
},
)?;
}
Ok(FormattedTable {
row_heights,
cell_spacing,
header_row_count,
host_spacing,
effective_height,
total_height,
caption_height,
is_tac,
cumulative_heights,
page_break,
cells,
table_footnote_height,
table_footnote_count,
table_footnotes,
strict_following_plain_text_fit,
external_flow,
})
}
pub(super) fn first_fragment_flow_origin(
para: &Paragraph,
table: &Table,
para_start_height: f64,
current_height: f64,
anchor_items_on_frame: bool,
) -> f64 {
use crate::model::shape::VertRelTo;
let empty_host_para_offset_float = !table.common.treat_as_char
&& matches!(table.common.vert_rel_to, VertRelTo::Para)
&& (table.common.vertical_offset as i32) > 0
&& !para_has_non_whitespace_text(para);
if empty_host_para_offset_float && anchor_items_on_frame {
para_start_height.min(current_height)
} else {
current_height
}
}
pub(crate) fn fit_measured_for_host(
para: &Paragraph,
next_para: Option<&Paragraph>,
table: &Table,
mt: Option<&MeasuredTable>,
dpi: f64,
styles: &ResolvedStyleSet,
profile: impl Fn() -> LayoutCompatibilityProfile,
) -> Option<MeasuredTable> {
match fit_measured_for_host_with_storage(
para,
next_para,
table,
mt,
dpi,
styles,
profile,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn fit_measured_for_host_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
next_para: Option<&Paragraph>,
table: &Table,
mt: Option<&MeasuredTable>,
dpi: f64,
styles: &ResolvedStyleSet,
profile: impl Fn() -> LayoutCompatibilityProfile,
storage: &S,
) -> Result<Option<MeasuredTable>, S::Error> {
crate::storage::with_optional(|failure| {
if crate::renderer::height_measurer::transparent_table_wrapper_child(table, styles)
.is_some_and(|nested| {
profile().hwp5_stored_pagination_layout()
|| profile().hwpx_stored_layout()
|| crate::renderer::height_measurer::unrestricted_wrapper_frame(
table, nested, dpi,
)
.is_some()
})
{
return crate::storage::optional(
mt.map(|value| crate::storage::try_clone(value, storage))
.transpose(),
failure,
)?;
}
if profile().hwpx_stored_layout() && !profile().session_edited() {
if let Some(fitted) = mt.and_then(|measured| {
crate::storage::optional(
crate::renderer::height_measurer::fit_stored_inline_picture_frame_with_storage(
measured, table, dpi, storage,
),
failure,
)?
}) {
return Some(fitted);
}
if let Some(fitted) =
mt.and_then(|measured| crate::storage::optional(crate::renderer::height_measurer::fit_stored_hwpx_no_adjust_rowspans_with_storage(measured, table, dpi, storage), failure)?)
{
return Some(fitted);
}
}
if table.common.treat_as_char
&& profile().hwpx_stored_layout()
&& !profile().session_edited()
{
if let Some(fitted) = mt
.and_then(|measured| crate::storage::optional(crate::renderer::height_measurer::trim_stored_hwpx_inline_row_trailing_spacing_with_storage(measured, table, dpi, storage), failure)?)
{
return Some(fitted);
}
}
if is_para_topbottom_float(&table.common) {
mt.and_then(|measured| {
if crate::storage::optional(
source_frame_fit::stored_following_tail_owns_rows(
para, next_para, table, measured, profile(), dpi, storage,
),
failure,
)? {
return crate::storage::optional(crate::storage::try_clone(measured, storage), failure);
}
let body_height = measured.row_heights.iter().sum::<f64>()
+ measured.cell_spacing * measured.row_heights.len().saturating_sub(1) as f64;
if !profile().session_edited()
&& (profile().hwp5_stored_pagination_layout() || profile().hwpx_stored_layout())
&& hwpunit_to_px(table.common.height as i32, dpi) > body_height
&& crate::renderer::table_row_frame::stored_rows_own_body(
table,
&measured.row_heights,
dpi,
)
{
return crate::storage::optional(crate::storage::try_clone(measured, storage), failure);
}
let (fitted, shrink_blocked_by_content) =
crate::storage::optional(crate::renderer::height_measurer::fit_measured_table_to_declared_height_with_outcome_with_storage(measured, table, dpi, storage), failure)?;
let shrunk = fitted.row_heights.iter().sum::<f64>()
< measured.row_heights.iter().sum::<f64>() - 0.01;
if shrunk
&& para_has_non_whitespace_text(para)
&& (profile().session_edited()
|| crate::storage::optional(crate::renderer::height_measurer::measured_table_has_grown_text_row_with_storage(
measured, table, dpi,
storage), failure)?)
{
return crate::storage::optional(crate::storage::try_clone(measured, storage), failure);
}
let stored_declared_tail = !profile().session_edited()
&& (profile().hwp5_stored_pagination_layout()
|| profile().hwpx_stored_layout())
&& matches!(
table.layout_page_break(),
crate::model::table::TablePageBreak::RowBreak
| crate::model::table::TablePageBreak::CellBreak
)
&& table.row_count > 1;
if (shrunk || shrink_blocked_by_content) && stored_declared_tail {
if let Some(closed) =
crate::storage::optional(crate::renderer::height_measurer::fit_measured_table_declared_tail_to_declared_height_with_storage(measured, table, dpi, storage), failure)?
{
return Some(closed);
}
}
Some(if (shrunk || shrink_blocked_by_content) && !para_has_non_whitespace_text(para) {
let native_empty_rowbreak_nested_tail = profile().hwp5_stored_pagination_layout()
&& !table.common.treat_as_char
&& matches!(
table.layout_page_break(),
crate::model::table::TablePageBreak::RowBreak
)
&& table.row_count > 1
&& table.cells.iter().all(|cell| cell.row_span == 1);
let adjusted = if native_empty_rowbreak_nested_tail {
crate::storage::optional(crate::renderer::height_measurer::fit_measured_table_nested_tail_to_declared_height_with_storage(measured, table, dpi, storage), failure)?
} else { None };
match adjusted {
Some(adjusted) => adjusted,
None => crate::storage::optional(crate::storage::try_clone(measured, storage), failure)?,
}
} else {
fitted
})
})
} else {
None
}
})
}