mod decoration_host;
pub(super) mod decoration_table;
pub(super) mod deferred;
pub(super) mod deferred_picture;
pub(super) mod deferred_placement;
pub(super) mod empty_float;
pub(super) mod flow_table;
pub(super) mod host_wrap;
pub(super) mod mixed_host_line;
pub(super) mod order;
pub(super) mod paragraph_flow;
pub(super) mod physical_tac;
pub(super) mod shape_flow;
mod square_host_frame;
pub(super) mod stored_tac;
pub(super) mod table_entry;
pub(super) mod tac_fit;
pub(super) mod tac_flow;
pub(super) mod tac_reconcile;
pub(super) mod wrap_absorption;
pub(super) mod wrap_flow;
pub(super) mod wrap_match;
pub(super) mod wrap_tail;
use super::paragraph::metrics::FormattedParagraph;
use super::{FormattedTable, TypesetState};
use crate::model::control::Control;
use crate::model::paragraph::Paragraph;
use crate::model::table::Table;
use crate::renderer::composer::ComposedParagraph;
use crate::renderer::float_placement::FloatLaneSet;
use crate::renderer::height_measurer::MeasuredTable;
use crate::renderer::hwpunit_to_px;
use crate::renderer::style_resolver::ResolvedStyleSet;
pub(super) fn prepare_no_table_host_wrap(
st: &mut TypesetState,
page_def: &crate::model::page::PageDef,
para: &Paragraph,
para_idx: usize,
has_table: bool,
) {
if !has_table {
if let Some(band) = host_wrap::stored_candidate(para) {
let col_w_hu = st.host_wrap_column_width_hu();
if host_wrap::can_arm(band, col_w_hu) {
st.arm_stored_host_wrap(para_idx, band);
if let Some(anchor) = host_wrap::host_anchor(para, page_def, para_idx, band) {
st.register_host_wrap_anchor(para_idx, anchor);
}
}
}
if st.host_wrap_needs_derived_lane() {
let col_w_hu = st.host_wrap_column_width_hu();
if let Some(lane) = super::square_float_left_lane_width(para, col_w_hu) {
st.arm_derived_host_wrap(para_idx, lane);
}
}
}
}
#[allow(clippy::too_many_arguments)]
pub(super) fn place_decoration_table(
st: &mut TypesetState,
para_idx: usize,
ctrl_idx: usize,
para: &Paragraph,
table: &Table,
next_para: Option<&Paragraph>,
paragraphs: &[Paragraph],
dpi: f64,
anchor_origin: Option<f64>,
format: impl FnOnce(bool) -> FormattedTable,
engine: &super::TypesetEngine,
styles: &ResolvedStyleSet,
) {
match place_decoration_table_with_storage(
st,
para_idx,
ctrl_idx,
para,
table,
next_para,
paragraphs,
dpi,
anchor_origin,
|top| Ok(format(top)),
engine,
styles,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn place_decoration_table_with_storage<S: crate::storage::Storage>(
st: &mut TypesetState,
para_idx: usize,
ctrl_idx: usize,
para: &Paragraph,
table: &Table,
next_para: Option<&Paragraph>,
paragraphs: &[Paragraph],
dpi: f64,
anchor_origin: Option<f64>,
format: impl FnOnce(bool) -> Result<FormattedTable, S::Error>,
engine: &super::TypesetEngine,
styles: &ResolvedStyleSet,
storage: &S,
) -> Result<(), S::Error> {
if anchor_origin.is_none()
&& !st.profile.session_edited()
&& (st.profile.hwp5_stored_pagination_layout() || st.profile.hwpx_stored_layout())
&& st.col_count == 1
&& st.current_zone_y_offset.abs() < f64::EPSILON
&& st.current_items.iter().skip(1).all(|item| {
matches!(item, crate::renderer::pagination::PageItem::FullParagraph { para_index }
if st.hidden_empty_paras.contains(para_index))
})
{
let anchor = st.current_items.first().and_then(|item| match item {
crate::renderer::pagination::PageItem::PartialTable {
para_index,
is_continuation: true,
..
} => Some(*para_index),
_ => None,
});
let placement = anchor.and_then(|anchor| {
crate::renderer::float_placement::ParagraphFloatPlacement::from_stored_overlay_guide_chain(
paragraphs, anchor, para_idx, st.current_height,
st.layout.body_area.height, styles, dpi,
)
});
if let Some(placement) = placement {
st.align_flow_to(placement.anchor_y);
let base = para.line_segs.first().map(|line| {
line.vertical_pos - crate::renderer::px_to_hwpunit(placement.anchor_y, dpi)
});
st.record_vpos_lazy_origin(base);
st.record_paragraph_float_placement_with_storage(
(para_idx, ctrl_idx),
placement,
storage,
)?;
}
}
st.emit_decoration_table_with_storage(para_idx, ctrl_idx, storage)?;
let origin = anchor_origin.unwrap_or_else(|| st.decoration_table_flow_height());
let ft = format(origin < 1.0)?;
let continuation = decoration_table::continuation(
para,
table,
next_para,
&ft,
origin,
dpi,
|| st.base_available_height(),
|row, available| {
decoration_table::partial_row_query(engine, st, table, &ft, styles, row, available)
},
);
if let Some(ref continuation) = continuation {
if std::env::var("RHWP_TABLE_DRIFT").is_ok() {
eprintln!(
"OVERLAY_CONT: pi={} ci={} start_row={} remaining={:.1} reserve={:.1} room={:.1}",
para_idx,
ctrl_idx,
continuation.first_unfit,
continuation.remaining_px,
continuation.reserve_px,
continuation.room,
);
}
}
st.finish_decoration_table_with_storage(para_idx, ctrl_idx, continuation, storage)?;
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(super) fn prepare_table_control(
st: &mut TypesetState,
para: &Paragraph,
table: &Table,
para_idx: usize,
ctrl_idx: usize,
order_pos: usize,
next_para: Option<&Paragraph>,
measured_tables: &[MeasuredTable],
has_tac: bool,
host_col_w: f64,
dpi: f64,
original_hwpx: impl FnOnce() -> bool,
) -> bool {
match prepare_table_control_with_storage(
st,
para,
table,
para_idx,
ctrl_idx,
order_pos,
next_para,
measured_tables,
has_tac,
host_col_w,
dpi,
original_hwpx,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn prepare_table_control_with_storage<S: crate::storage::Storage>(
st: &mut TypesetState,
para: &Paragraph,
table: &Table,
para_idx: usize,
ctrl_idx: usize,
order_pos: usize,
next_para: Option<&Paragraph>,
measured_tables: &[MeasuredTable],
has_tac: bool,
host_col_w: f64,
dpi: f64,
original_hwpx: impl FnOnce() -> bool,
storage: &S,
) -> Result<bool, S::Error> {
let page = st.table_control_page();
let follows_tac_frame = !page.profile.session_edited()
&& (page.profile.hwpx_stored_layout() || page.profile.hwp5_stored_pagination_layout())
&& crate::renderer::stored_control_frame::following_first_tac_frame(
para,
(st.base_available_height() * 7200.0 / dpi).round() as i32,
)
.is_some_and(|frame| {
crate::renderer::layout::control_line_seg_index(para, ctrl_idx) == Some(frame.line)
&& st.coanchored_table_page().1.iter().any(|item| {
use crate::renderer::pagination::PageItem;
matches!(item,
PageItem::Table { para_index, control_index }
| PageItem::PartialTable { para_index, control_index, .. }
| PageItem::Shape { para_index, control_index }
if *para_index == para_idx && *control_index == frame.before_control)
})
});
if follows_tac_frame {
st.advance_column_or_new_page_with_storage(storage)?;
}
if table_entry::needs_stored_line_advance(
para,
table,
order_pos,
st.table_control_page().has_items,
dpi,
) {
st.advance_column_or_new_page_with_storage(storage)?;
}
let frame = st.inline_flow_input(0.0, false);
Ok(table_entry::uses_decoration_placement(
para,
table,
para_idx,
ctrl_idx,
next_para,
measured_tables,
st.table_control_page().col_count,
has_tac,
host_col_w,
*frame.body,
frame.page_height,
dpi,
|| st.base_available_height(),
original_hwpx,
))
}
#[allow(clippy::too_many_arguments)]
pub(super) fn place_decoration_host_text(
st: &mut TypesetState,
para_idx: usize,
para: &Paragraph,
next_para: Option<&Paragraph>,
fmt: &FormattedParagraph,
decoration_host_text_pending: bool,
flow_table_owns_host_text: bool,
styles: &ResolvedStyleSet,
dpi: f64,
stored_layout: impl FnOnce() -> bool,
) {
match place_decoration_host_text_with_storage(
st,
para_idx,
para,
next_para,
fmt,
decoration_host_text_pending,
flow_table_owns_host_text,
styles,
dpi,
stored_layout,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn place_decoration_host_text_with_storage<S: crate::storage::Storage>(
st: &mut TypesetState,
para_idx: usize,
para: &Paragraph,
next_para: Option<&Paragraph>,
fmt: &FormattedParagraph,
decoration_host_text_pending: bool,
flow_table_owns_host_text: bool,
styles: &ResolvedStyleSet,
dpi: f64,
stored_layout: impl FnOnce() -> bool,
storage: &S,
) -> Result<(), S::Error> {
let _: () = if let Some(fragment) = decoration_host::plan(
para_idx,
para,
next_para,
fmt,
decoration_host_text_pending,
flow_table_owns_host_text,
styles,
dpi,
stored_layout,
) {
st.commit_decoration_host_text_with_storage(fragment, storage)?;
};
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(super) fn reconcile_tac_height(
st: &mut TypesetState,
para_idx: usize,
para: &Paragraph,
next_para: Option<&Paragraph>,
fmt: &FormattedParagraph,
styles: &ResolvedStyleSet,
measured_tables: &[MeasuredTable],
tac_count: usize,
height_before: f64,
measured_tac_floor: Option<f64>,
flow: tac_flow::TacFlowQuery<'_>,
) {
let dpi = flow.dpi();
let tac_reconcile::TacHeightCap {
tac_seg_total,
cap,
stored_step_px,
ladder_total,
ladder_omits_spacing,
} = tac_reconcile::measure(
para_idx,
para,
next_para,
fmt,
styles,
measured_tables,
tac_count,
height_before,
st.tac_height_page().profile,
&flow,
|ci, prior_tac_count, lh, ls_extra| {
if std::env::var("RHWP_DIAG_TACSIB").is_ok() {
eprintln!(
"DIAG_TACSIB pi={} ci={} line_idx={} add={:.1} first_seg={:.1}",
para_idx,
ci,
prior_tac_count,
lh + ls_extra,
para.line_segs
.first()
.map(|s0| hwpunit_to_px(
s0.line_height.saturating_add(s0.line_spacing),
dpi
))
.unwrap_or(0.0),
);
}
},
);
if ladder_omits_spacing {
st.commit_tac_spacing_omission(|| {
if std::env::var("RHWP_DIAG_LADSP").is_ok() {
eprintln!(
"DIAG_LADSP pi={} OMIT step={:.1} cap={:.1} fmt_total={:.1} sb={:.1} sa={:.1}",
para_idx,
stored_step_px.unwrap_or(0.0),
cap,
fmt.total_height,
fmt.spacing_before,
fmt.spacing_after
);
}
});
}
let snapped_base = tac_reconcile::snapped_base(
para,
height_before,
ladder_omits_spacing,
st.tac_height_page(),
dpi,
);
let cap =
tac_reconcile::effective_cap(cap, ladder_total, ladder_omits_spacing, measured_tac_floor);
let cap = if measured_tac_floor.is_some()
&& para.controls.iter().any(|control| match control {
Control::Table(table) => {
flow.single_tac_line_has_unstored_cell_text(para, table, fmt, tac_count)
}
_ => false,
}) {
cap.max(st.tac_height_page().current_height - snapped_base)
} else {
cap
};
if std::env::var("RHWP_DIAG_TACCAP").is_ok() {
eprintln!(
"DIAG_TACCAP pi={} tac_seg_total={:.1} cap={:.1} fmt_total={:.1} sb={:.1} cur_h={:.1} snapped_base={:.1} clamp={}",
para_idx,
tac_seg_total,
cap,
fmt.total_height,
fmt.spacing_before,
st.tac_height_page().current_height,
snapped_base,
st.tac_height_page().current_height - snapped_base > cap
);
}
let capped_bottom =
tac_reconcile::capped_bottom(para_idx, snapped_base, cap, st.tac_height_page());
st.commit_tac_capped_bottom(capped_bottom);
}
#[allow(clippy::too_many_arguments)]
pub(super) fn place_table_host_shape(
st: &mut TypesetState,
para_idx: usize,
ctrl_idx: usize,
ctrl: &Control,
para: &Paragraph,
para_start_height: f64,
styles: &ResolvedStyleSet,
dpi: f64,
) {
match place_table_host_shape_with_storage(
st,
para_idx,
ctrl_idx,
ctrl,
para,
para_start_height,
styles,
dpi,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn place_table_host_shape_with_storage<S: crate::storage::Storage>(
st: &mut TypesetState,
para_idx: usize,
ctrl_idx: usize,
ctrl: &Control,
para: &Paragraph,
para_start_height: f64,
styles: &ResolvedStyleSet,
dpi: f64,
storage: &S,
) -> Result<(), S::Error> {
if st.has_spilled_page_tail_float(para_idx, ctrl_idx) {
return Ok(());
}
let mut flow = shape_flow::prepare(ctrl, para, ctrl_idx, dpi);
if flow.tac_separate_line_h.is_some()
&& shape_flow::own_line_already_placed(para_idx, para, ctrl_idx, &st.current_items)
{
flow.tac_separate_line_h = None;
}
let offpage_top = (matches!(ctrl, Control::Shape(_)) && flow.non_tac_pushdown_h.is_some())
.then(|| shape_flow::para_offset_top_px(ctrl, dpi))
.flatten()
.filter(|top| para_start_height + top >= st.available_height());
let below_own_lines = if offpage_top.is_none() {
flow.non_tac_pushdown_h
.and_then(|height| {
shape_flow::below_own_lines_top_px(ctrl, para, dpi).map(|top| (top, height))
})
.map(|(top, height)| (para_start_height + top, height))
} else {
None
};
if offpage_top.is_some() || below_own_lines.is_some() {
flow.non_tac_pushdown_h = None;
}
if st.defer_table_host_ole_to_next_column(para_idx, ctrl_idx, ctrl, dpi) {
return Ok(());
}
let spills_to_leaving_page = flow.non_tac_pushdown_h.is_some_and(|extra| {
shape_flow::anchor_line_rewinds_to_next_page(para, ctrl_idx)
&& shape_flow::page_still_holds_lines_before_anchor(
para_idx,
para,
ctrl_idx,
&st.current_items,
)
&& st.layout.body_area.y + st.current_height + extra <= st.layout.page_height + 0.5
});
while !spills_to_leaving_page
&& flow.needs_advance(st.table_host_shape_page(), || st.available_height())
{
let common = match ctrl {
Control::Shape(shape) => Some(shape.common()),
Control::Picture(picture) => Some(&picture.common),
_ => None,
};
let column_frame = common.is_some_and(|common| {
matches!(common.horz_rel_to, crate::model::shape::HorzRelTo::Column)
&& common.width as f64 <= st.layout.column_width_hu() as f64
});
if column_frame && st.defer_table_host_shape_to_next_column(para_idx, ctrl_idx, flow) {
return Ok(());
}
st.advance_column_or_new_page_with_storage(storage)?;
}
let pushed_anchor_origin = match ctrl {
Control::Shape(shape) => {
let common = shape.common();
crate::renderer::layout::stored_pushed_anchor_line_offset_hu(
para,
common,
i64::from(common.margin.top) + i64::from(common.margin.bottom),
ctrl_idx,
)
.filter(|_| crate::renderer::layout::has_line_taking_tac_sibling_before(para, ctrl_idx))
.map(|hu| para_start_height + crate::renderer::hwpunit_to_px(hu, dpi))
}
_ => None,
};
let accepted_frame = if let Some(top) = offpage_top {
Some(shape_flow::offpage_shape_frame(
para_start_height,
top,
st.current_height,
))
} else if let Some((top, _)) = below_own_lines {
Some(shape_flow::below_own_lines_frame(
para_start_height,
top,
st.current_height,
))
} else {
match pushed_anchor_origin {
Some(origin) => flow.pushed_anchor_shape_frame(ctrl, origin, dpi),
None => flow.accepted_shape_frame(ctrl, st.current_height),
}
};
st.commit_table_host_shape_with_storage(para_idx, ctrl_idx, flow, accepted_frame, storage)?;
if let Some((top, height)) = below_own_lines {
st.add_visible_float_exclusion(super::VisibleFloatExclusion {
para_index: para_idx,
top,
bottom: top + height,
});
}
st.register_side_wrap_picture(para_idx, ctrl_idx, para, Some(para_start_height), styles);
if spills_to_leaving_page {
st.advance_column_or_new_page_with_storage(storage)?;
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(super) fn try_place_empty_para_float_table(
st: &mut TypesetState,
para_idx: usize,
ctrl_idx: usize,
para: &Paragraph,
table: &crate::model::table::Table,
ft: &FormattedTable,
composed: Option<&ComposedParagraph>,
prev_para: Option<&Paragraph>,
next_para: Option<&Paragraph>,
styles: &ResolvedStyleSet,
para_start_height: f64,
lanes: &mut FloatLaneSet,
paragraphs: &[Paragraph],
table_reflowed: bool,
preceding_fragment_reflowed: bool,
dpi: f64,
) -> bool {
let Some(placement) = empty_float::prepare(
para_idx,
ctrl_idx,
para,
table,
ft,
composed,
prev_para,
next_para,
styles,
para_start_height,
lanes,
st.empty_float_page(paragraphs),
table_reflowed,
preceding_fragment_reflowed,
|| st.empty_float_available_height(ft.table_footnote_height, ft.table_footnote_count),
dpi,
) else {
return false;
};
st.commit_empty_float_table(para_idx, ctrl_idx, placement, lanes);
true
}
pub(super) fn flush_deferred_tables(
st: &mut TypesetState,
paragraphs: &[Paragraph],
flush_point: deferred::DeferredTableFlushPoint,
mut place: impl FnMut(&mut TypesetState, deferred::DeferredTableControl),
) {
if !st.has_deferred_table_controls() {
return;
}
let pending = st.take_deferred_table_controls();
let mut remaining = Vec::new();
for deferred in pending {
let keep_pending = flush_point.keeps_pending(&deferred, paragraphs);
if keep_pending {
remaining.push(deferred);
continue;
}
let previous = st.replace_deferred_table_successor_start(Some(
flush_point.successor_start(paragraphs.len()),
));
place(st, deferred);
st.replace_deferred_table_successor_start(previous);
}
st.restore_deferred_table_controls(remaining);
}
pub(super) fn prepare_tac_paragraph(
st: &mut TypesetState,
para_idx: usize,
para: &Paragraph,
fmt: &FormattedParagraph,
measured_tables: &[MeasuredTable],
flow: tac_flow::TacFlowQuery<'_>,
) -> tac_fit::TacFitPlan {
let plan = tac_fit::prepare(
para_idx,
para,
fmt,
measured_tables,
st.tac_fit_page(),
|| st.available_height(),
flow,
);
if plan.advance_before_place {
st.advance_column_or_new_page();
}
plan
}
#[allow(clippy::too_many_arguments)]
pub(super) fn try_place_stored_tac_paragraph(
st: &mut TypesetState,
para_idx: usize,
para: &Paragraph,
fmt: &FormattedParagraph,
measured_tables: &[MeasuredTable],
engine: &super::TypesetEngine,
paragraphs: &[Paragraph],
styles: &crate::renderer::style_resolver::ResolvedStyleSet,
composed: Option<&ComposedParagraph>,
) -> bool {
let dpi = engine.dpi;
if para.controls.iter().any(|control| {
matches!(control, Control::Table(table)
if engine.render_normalization.inline_host_frame_expanded(table))
}) {
return false;
}
let source_frame = st.stored_tac_line_frame(para, paragraphs.get(para_idx + 1), styles);
if let Some(placement) = stored_tac::prepare_computed(
para_idx,
para,
paragraphs.get(para_idx + 1),
paragraphs
.get(para_idx + 1)
.and_then(|next| styles.para_styles.get(next.para_shape_id as usize))
.map_or(0.0, |style| style.spacing_before),
fmt,
measured_tables,
styles,
st.stored_tac_page(paragraphs),
|| st.available_height(),
source_frame,
dpi,
) {
let rebase_line_origin = placement.rebase_line_origin;
let lazy_origin = para.line_segs.first().map(|seg| {
seg.vertical_pos
.saturating_add(seg.line_height)
.saturating_add(seg.line_spacing)
.saturating_sub(crate::renderer::px_to_hwpunit(placement.end, dpi))
});
let owner = placement.control_index;
let host_origin = placement.inline.y;
let emit_decoration = |st: &mut TypesetState, index: usize, control: &Control| {
if let Control::Table(table) = control {
place_decoration_table(
st,
para_idx,
index,
para,
table,
paragraphs.get(para_idx + 1),
paragraphs,
dpi,
Some(host_origin),
|is_column_top| {
engine.format_table(
para,
para_idx,
index,
table,
measured_tables,
styles,
composed,
paragraphs.get(para_idx + 1),
is_column_top,
)
},
engine,
styles,
);
} else {
place_table_host_shape(
st,
para_idx,
index,
control,
para,
host_origin,
styles,
dpi,
);
}
};
for (index, control) in para.controls.iter().enumerate().take(owner) {
emit_decoration(st, index, control);
}
st.commit_stored_tac_control(para_idx, placement);
for (index, control) in para.controls.iter().enumerate().skip(owner + 1) {
emit_decoration(st, index, control);
}
if rebase_line_origin {
st.commit_deferred_table_anchor(para_idx, paragraphs);
st.record_vpos_lazy_origin(lazy_origin);
st.mark_vpos_ladder_dirty();
}
return true;
}
let shared_spacing_before =
crate::renderer::float_placement::stored_frame_successor_shared_spacing_px(
&st.paragraph_float_placements,
para_idx,
fmt.spacing_before,
st.current_height,
);
let Some(plan) = stored_tac::prepare(
para_idx,
para,
fmt,
measured_tables,
styles,
st.stored_tac_page(paragraphs),
shared_spacing_before,
para_idx
.checked_sub(1)
.and_then(|index| paragraphs.get(index)),
para_idx
.checked_sub(1)
.and_then(|index| paragraphs.get(index))
.and_then(|previous| styles.para_styles.get(previous.para_shape_id as usize))
.map_or(0.0, |shape| shape.spacing_after),
paragraphs.get(para_idx + 1),
paragraphs
.get(para_idx + 1)
.and_then(|next| styles.para_styles.get(next.para_shape_id as usize))
.map_or(0.0, |shape| shape.spacing_before),
styles
.para_styles
.get(para.para_shape_id as usize)
.is_some_and(|style| style.page_break_before),
|| st.available_height(),
source_frame,
dpi,
) else {
return false;
};
for line in &plan.lines {
let placement = plan.placement(line, fmt.spacing_after, dpi);
st.commit_stored_tac_control(para_idx, placement);
}
if let Some(frame) = plan.source_frame {
st.commit_deferred_table_anchor(para_idx, paragraphs);
st.record_vpos_page_origin(Some(frame.source_origin_hu));
st.record_vpos_column_anchor(frame.column_anchor_y);
st.record_vpos_origin_provenance(true);
st.record_vpos_lazy_origin(None);
}
true
}