use crate::model::{
control::Control,
page::{ColumnDef, PageDef},
paragraph::{ColumnBreakType, LineSeg, Paragraph},
};
use crate::renderer::{
composer::{
project_current_body_box_rows, project_current_marker_geometry_rows,
project_current_source_program_rows, project_stored_soft_wraps,
projects_current_marker_geometry, ParagraphBox,
},
page_layout::PageLayoutInfo,
px_to_hwpunit,
style_resolver::ResolvedStyleSet,
};
fn uniform_column_width(page: &PageDef, columns: &ColumnDef, dpi: f64) -> Option<f64> {
let layout = PageLayoutInfo::from_page_def(page, columns, dpi);
let width = layout.column_areas.first()?.width;
layout
.column_areas
.iter()
.all(|column| px_to_hwpunit(column.width, dpi) == px_to_hwpunit(width, dpi))
.then_some(width)
}
fn frame_advance(lines: &[LineSeg]) -> Option<i32> {
let first = lines.first()?;
let last = lines.last()?;
last.vertical_pos
.checked_add(last.line_height)?
.checked_add(last.line_spacing)?
.checked_sub(first.vertical_pos)
}
fn paragraph_owns_line_spacing(paragraph: &Paragraph) -> bool {
use crate::model::shape::TextWrap;
paragraph
.controls
.iter()
.enumerate()
.all(|(index, control)| {
if crate::renderer::cell_control_is_text_metadata(control) {
return true;
}
if let Control::Picture(picture) = control {
if picture.common.treat_as_char {
let (_, height) =
crate::renderer::composer::inline_picture_occupied_size_hu(picture);
return height > 0
&& crate::renderer::layout::control_line_seg_index(paragraph, index)
.and_then(|line| paragraph.line_segs.get(line))
.is_some_and(|line| line.line_height >= height);
}
}
let common = match control {
Control::Shape(shape) => shape.common(),
Control::Picture(picture) => &picture.common,
Control::Table(table) => &table.common,
_ => return false,
};
!common.treat_as_char
&& matches!(
common.text_wrap,
TextWrap::InFrontOfText | TextWrap::BehindText
)
})
}
fn stored_closed_table_row_frames(
table: &crate::model::table::Table,
) -> Option<std::collections::HashMap<u16, u32>> {
match stored_closed_table_row_frames_with_storage(table, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn stored_closed_table_row_frames_with_storage<S: crate::storage::Storage>(
table: &crate::model::table::Table,
storage: &S,
) -> Result<Option<std::collections::HashMap<u16, u32>>, S::Error> {
crate::storage::with_optional(|failure| {
if table.common.height == 0
|| !crate::renderer::float_placement::stored_complete_column_frame(table)
{
return None;
}
let mut rows = std::collections::HashMap::<u16, u32>::new();
for cell in &table.cells {
if cell.row_span != 1 || cell.line_wrap != 0 {
return None;
}
let padding = cell.effective_padding(&table.padding);
if padding.top < 0 || padding.bottom < 0 {
return None;
}
let ink = crate::renderer::float_placement::stored_plain_cell_ink_height(cell)?;
let end = u32::try_from(ink)
.ok()?
.checked_add(padding.top as u32)?
.checked_add(padding.bottom as u32)?;
if !rows.contains_key(&cell.row) {
crate::storage::optional(storage.reserve_map(&mut rows, 1), failure)?;
}
let height = rows.entry(cell.row).or_default();
*height = (*height).max(cell.height).max(end);
}
let total = (0..table.row_count).try_fold(0_u64, |sum, row| {
sum.checked_add(u64::from(*rows.get(&row)?))
})?;
let spacing = u64::try_from(table.cell_spacing)
.ok()?
.checked_mul(u64::from(table.row_count.saturating_sub(1)))?;
(total.checked_add(spacing)? == u64::from(table.common.height)).then_some(rows)
})
}
#[derive(Clone, Copy)]
struct NormalizationPolicy<'a> {
refresh_markers: bool,
previous_margin_styles: Option<&'a ResolvedStyleSet>,
}
pub(crate) fn normalize(
original: &[Paragraph],
styles: &ResolvedStyleSet,
page: &PageDef,
initial_columns: &ColumnDef,
refresh_markers: bool,
dpi: f64,
previous_margin_styles: Option<&ResolvedStyleSet>,
) -> Option<Vec<Paragraph>> {
match normalize_with_storage(
original,
styles,
page,
initial_columns,
refresh_markers,
dpi,
previous_margin_styles,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn normalize_with_storage<S: crate::storage::Storage>(
original: &[Paragraph],
styles: &ResolvedStyleSet,
page: &PageDef,
initial_columns: &ColumnDef,
refresh_markers: bool,
dpi: f64,
previous_margin_styles: Option<&ResolvedStyleSet>,
storage: &S,
) -> Result<Option<Vec<Paragraph>>, S::Error> {
normalize_flow_with_storage(
original,
styles,
uniform_column_width(page, initial_columns, dpi),
Some(page),
true,
NormalizationPolicy {
refresh_markers,
previous_margin_styles,
},
dpi,
storage,
)
}
fn normalize_flow(
original: &[Paragraph],
styles: &ResolvedStyleSet,
mut width: Option<f64>,
body_page: Option<&PageDef>,
refresh_text: bool,
policy: NormalizationPolicy<'_>,
dpi: f64,
) -> Option<Vec<Paragraph>> {
match normalize_flow_with_storage(
original,
styles,
width,
body_page,
refresh_text,
policy,
dpi,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
fn normalize_flow_with_storage<S: crate::storage::Storage>(
original: &[Paragraph],
styles: &ResolvedStyleSet,
mut width: Option<f64>,
body_page: Option<&PageDef>,
refresh_text: bool,
policy: NormalizationPolicy<'_>,
dpi: f64,
storage: &S,
) -> Result<Option<Vec<Paragraph>>, S::Error> {
crate::storage::with_optional(|failure| {
let NormalizationPolicy {
refresh_markers,
previous_margin_styles,
} = policy;
let mut probe_width = width;
let mut rebuild_body_spacing = false;
if body_page.is_some() {
for paragraph in original {
for control in ¶graph.controls {
if let (Some(page), Control::ColumnDef(columns)) = (body_page, control) {
probe_width = uniform_column_width(page, columns, dpi);
}
}
if let Some(width) = probe_width {
let style = styles.para_styles.get(paragraph.para_shape_id as usize);
let box_ = ParagraphBox::body_for_style(width, style, dpi);
if crate::storage::optional(
crate::renderer::composer::project_current_body_box_rows_with_storage(
paragraph, &box_, styles, dpi, storage,
),
failure,
)?
.is_some()
{
rebuild_body_spacing = true;
break;
}
}
}
}
let mut shift = 0_i32;
let mut previous_source_vpos = None;
let mut result = crate::storage::optional(storage.vec(original.len()), failure)?;
let mut changed = false;
for (source_index, source) in original.iter().enumerate() {
if source.column_type != ColumnBreakType::None
|| styles
.para_styles
.get(source.para_shape_id as usize)
.is_some_and(|style| style.page_break_before)
{
shift = 0;
}
for control in &source.controls {
if let (Some(page), Control::ColumnDef(columns)) = (body_page, control) {
width = uniform_column_width(page, columns, dpi);
shift = 0;
}
}
if rebuild_body_spacing
&& source.column_type == ColumnBreakType::None
&& !styles
.para_styles
.get(source.para_shape_id as usize)
.is_some_and(|style| style.page_break_before)
{
if let Some(previous) = source_index.checked_sub(1).and_then(|i| original.get(i)) {
if paragraph_owns_line_spacing(previous)
&& paragraph_owns_line_spacing(source)
&& !previous.stored_text_partition_is_dirty()
&& !source.stored_text_partition_is_dirty()
{
if let Some((last, first)) =
previous.line_segs.last().zip(source.line_segs.first())
{
let gap = i64::from(first.vertical_pos)
- i64::from(last.vertical_pos)
- i64::from(last.line_height)
- i64::from(last.line_spacing);
let after = styles
.para_styles
.get(previous.para_shape_id as usize)
.map_or(0, |style| px_to_hwpunit(style.spacing_after, dpi));
let before = styles
.para_styles
.get(source.para_shape_id as usize)
.map_or(0, |style| px_to_hwpunit(style.spacing_before, dpi));
let declared = i64::from(after) + i64::from(before);
if after >= 0
&& before >= 0
&& gap < declared
&& (gap == 0 || gap == i64::from(after) || gap == i64::from(before))
&& !first.is_first_line_of_page()
&& !first.is_first_line_of_column()
&& first.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& last.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
{
shift = shift.checked_add(i32::try_from(declared - gap).ok()?)?;
}
}
}
}
}
let mut paragraph = crate::storage::optional(storage.clone_paragraph(source), failure)?;
for control in &mut paragraph.controls {
let Control::Table(table) = control else {
continue;
};
let mut row_frames = std::collections::HashMap::<u16, u32>::new();
for cell in &table.cells {
if cell.row_span == 1 {
if !row_frames.contains_key(&cell.row) {
crate::storage::optional(
storage.reserve_map(&mut row_frames, 1),
failure,
)?;
}
let height = row_frames.entry(cell.row).or_default();
*height = (*height).max(cell.height);
}
}
if let Some(closed) = crate::storage::optional(
stored_closed_table_row_frames_with_storage(table, storage),
failure,
)? {
row_frames = closed;
}
for cell in &mut table.cells {
if cell.text_direction != 0 || cell.line_wrap != 0 {
continue;
}
let padding = cell.effective_padding(&table.padding);
let Some(inner_hu) = i32::try_from(cell.width).ok().and_then(|width| {
width
.checked_sub(i32::from(padding.left))?
.checked_sub(i32::from(padding.right))
}) else {
continue;
};
if inner_hu <= 0 {
continue;
}
let inner = crate::renderer::composer::cell_inner_text_width(
crate::renderer::hwpunit_to_px(inner_hu, dpi),
0.0,
0.0,
dpi,
);
let frame = if cell.row_span == 1 {
row_frames.get(&cell.row).copied().unwrap_or(cell.height)
} else {
cell.height
};
if let Some(previous) = previous_margin_styles {
if let Some(projected) =
crate::storage::optional(crate::renderer::composer::stored_margin_context::project_cell_with_storage(
&cell.paragraphs,
inner,
styles,
previous,
dpi, storage,
), failure)?
{
cell.paragraphs = projected;
changed = true;
continue;
}
}
let stale = stored_text_exceeds_frame(cell, frame, &padding);
if let Some(projected) = crate::storage::optional(
normalize_flow_with_storage(
&cell.paragraphs,
styles,
Some(inner),
None,
stale,
policy,
dpi,
storage,
),
failure,
)? {
cell.paragraphs = projected;
changed = true;
}
}
}
let mut origin_changed = false;
for line in &mut paragraph.line_segs {
let source_vpos = line.vertical_pos;
if line.is_first_line_of_page()
|| line.is_first_line_of_column()
|| previous_source_vpos.is_some_and(|previous| source_vpos < previous)
{
shift = 0;
}
previous_source_vpos = Some(source_vpos);
line.vertical_pos = source_vpos.checked_add(shift)?;
origin_changed |= shift != 0;
}
if origin_changed {
paragraph.source_line_seg_vertical_pos = None;
}
if let Some(width) = width {
if let Some(previous) = previous_margin_styles.filter(|_| body_page.is_some()) {
if let Some(projected) = crate::storage::optional(
crate::renderer::composer::stored_margin_context::project_body_with_storage(
¶graph, width, styles, previous, dpi, storage,
),
failure,
)? {
let old_advance = frame_advance(¶graph.line_segs)?;
let new_advance = frame_advance(&projected.line_segs)?;
shift = shift.checked_add(new_advance.checked_sub(old_advance)?)?;
paragraph = projected;
changed = true;
}
}
let style = styles.para_styles.get(paragraph.para_shape_id as usize);
let box_ = if body_page.is_some() {
ParagraphBox::body_for_style(width, style, dpi)
} else {
ParagraphBox::content_for_style(width, style, dpi)
};
let spans_physical_frames =
crate::renderer::composer::stored_rows_cross_physical_frame(¶graph);
let mut current_rows = if body_page.is_some() {
crate::storage::optional(
crate::renderer::composer::project_current_body_box_rows_with_storage(
¶graph, &box_, styles, dpi, storage,
),
failure,
)?
} else {
None
};
if current_rows.is_none() {
current_rows = crate::storage::optional(
crate::renderer::composer::project_current_source_program_rows_with_storage(
¶graph, &box_, styles, dpi, storage,
),
failure,
)?;
}
if current_rows.is_none() {
current_rows = crate::storage::optional(
crate::renderer::composer::project_current_marker_geometry_rows_with_storage(
¶graph, &box_, styles, dpi, storage,
),
failure,
)?;
}
let selected_program_rows = crate::storage::optional(crate::renderer::composer::project_stored_rows_with_selected_program_with_storage(¶graph, &box_, styles, dpi, storage), failure)?;
let marker_rows = || {
if !refresh_markers
|| !(refresh_text || spans_physical_frames)
|| !projects_current_marker_geometry(¶graph, &box_, styles)
{
return None;
}
let first = paragraph.line_segs[0].vertical_pos;
let frame = box_.frame(first);
crate::storage::optional(
crate::renderer::composer::project_stored_soft_wraps_with_storage(
¶graph, &frame, styles, dpi, false, storage,
),
failure,
)?
};
if let Some((lines, spaces)) =
selected_program_rows.or(current_rows).or_else(marker_rows)
{
let old_advance = frame_advance(¶graph.line_segs)?;
let new_advance = frame_advance(&lines)?;
shift = shift.checked_add(new_advance.checked_sub(old_advance)?)?;
paragraph.replace_line_segs_with_space_metrics(lines, spaces);
changed = true;
}
}
crate::storage::optional(storage.push(&mut result, paragraph), failure)?;
}
changed.then_some(result)
})
}
fn stored_text_exceeds_frame(
cell: &crate::model::table::Cell,
frame_height: u32,
padding: &crate::model::Padding,
) -> bool {
if cell.dirty_flag || frame_height == 0 || frame_height > i32::MAX as u32 {
return false;
}
let mut end = 0_i64;
let mut first = true;
for para in &cell.paragraphs {
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 false;
}
for line in ¶.line_segs {
if line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| line.line_height <= 0
|| line.line_spacing < 0
|| i64::from(line.vertical_pos) < end
|| (!first && (line.is_first_line_of_page() || line.is_first_line_of_column()))
{
return false;
}
end = i64::from(line.vertical_pos) + i64::from(line.line_height);
first = false;
}
}
!first && end + i64::from(padding.top) + i64::from(padding.bottom) > i64::from(frame_height)
}