use crate::model::{
control::Control,
paragraph::Paragraph,
table::{Cell, Table},
};
use crate::renderer::{
composer::{self, BreakToken, ParagraphBox},
hwpunit_to_px, px_to_hwpunit,
style_resolver::ResolvedStyleSet,
};
fn paragraph_minimum_width(para: &Paragraph, styles: &ResolvedStyleSet, dpi: f64) -> u32 {
let style = styles.para_styles.get(para.para_shape_id as usize);
let chars: Vec<_> = para.text.chars().collect();
let text_width = composer::tokenize_paragraph(
&chars,
¶.char_offsets,
¶.char_shapes,
styles,
style.map_or(0, |s| s.english_break_unit),
style.map_or(0, |s| s.korean_break_unit),
)
.into_iter()
.filter_map(|token| match token {
BreakToken::Text { width, .. } => Some(width),
_ => None,
})
.fold(0.0_f64, f64::max);
let margins = style.map_or(0.0, |s| s.margin_left + s.margin_right + s.indent.max(0.0));
let text_width = px_to_hwpunit((text_width + margins).max(0.0), dpi).max(0) as u32;
let object_width = para
.controls
.iter()
.filter_map(|control| match control {
Control::Table(table) => Some(
table
.get_column_widths()
.iter()
.copied()
.sum::<u32>()
.saturating_add(table.outer_margin_left.max(0) as u32)
.saturating_add(table.outer_margin_right.max(0) as u32),
),
Control::Picture(picture) => Some(
picture
.common
.width
.saturating_add(picture.common.margin.left.max(0) as u32)
.saturating_add(picture.common.margin.right.max(0) as u32),
),
_ => None,
})
.max()
.unwrap_or(0);
text_width.max(object_width)
}
pub(super) fn reserve_content_widths(
cells: &mut [Cell],
columns: &mut [u32],
table_padding: &crate::model::Padding,
styles: &ResolvedStyleSet,
dpi: f64,
) {
for cell in cells.iter() {
let effective = cell.effective_padding(table_padding);
let padding = effective.left.max(0) as u32 + effective.right.max(0) as u32;
let required = cell
.paragraphs
.iter()
.map(|para| paragraph_minimum_width(para, styles, dpi))
.max()
.unwrap_or(0)
.saturating_add(padding);
let start = usize::from(cell.col);
let end = start + usize::from(cell.col_span.max(1));
let Some(span) = columns.get_mut(start..end) else {
continue;
};
let deficit = required.saturating_sub(span.iter().sum());
let count = span.len() as u32;
for (index, width) in span.iter_mut().enumerate() {
*width =
width.saturating_add(deficit / count + u32::from((index as u32) < deficit % count));
}
}
for cell in cells {
let start = usize::from(cell.col);
let end = start + usize::from(cell.col_span.max(1));
let Some(span) = columns.get(start..end) else {
continue;
};
let width = span.iter().sum();
cell.width = width;
if let Some(raw) = cell.raw_list_extra.get_mut(..4) {
raw.copy_from_slice(&width.to_le_bytes());
}
let effective = cell.effective_padding(table_padding);
let inner = composer::cell_inner_text_width(
hwpunit_to_px(width.min(i32::MAX as u32) as i32, dpi),
hwpunit_to_px(i32::from(effective.left), dpi),
hwpunit_to_px(i32::from(effective.right), dpi),
dpi,
);
for para in &mut cell.paragraphs {
composer::reflow_line_segs(
para,
ParagraphBox::content_width_px(inner, dpi),
styles,
dpi,
);
}
}
}
pub(super) fn reserve_content_heights(table: &mut Table, styles: &ResolvedStyleSet, dpi: f64) {
let minimum_heights = table.get_row_heights();
let measured = crate::renderer::height_measurer::HeightMeasurer::new(dpi)
.with_session_edited(true)
.measure_table(table, 0, 0, styles);
let rows = if crate::renderer::height_measurer::transparent_table_wrapper_child(table, styles)
.is_some()
{
vec![
crate::renderer::height_measurer::unwrapped_table_whole_height(
table,
measured.total_height,
dpi,
styles,
),
]
} else {
measured.row_heights
};
let heights: Vec<u32> = rows
.iter()
.enumerate()
.map(|(row, height)| {
(px_to_hwpunit(*height, dpi).max(0) as u32)
.max(minimum_heights.get(row).copied().unwrap_or(0))
})
.collect();
for cell in &mut table.cells {
let start = usize::from(cell.row);
let end = start + usize::from(cell.row_span.max(1));
if let Some(rows) = heights.get(start..end) {
cell.height = rows.iter().sum();
}
}
table.sync_ctrl_height(heights.iter().sum());
}