use super::controls::decoration_table::OverlayContinuation;
use super::controls::deferred::DeferredTableControl;
use super::controls::empty_float::{EmptyFloatPage, EmptyFloatPlacement};
use super::controls::host_wrap::StoredHostBand;
use super::controls::shape_flow::{TableHostShapeFlow, TableHostShapePage};
use super::controls::stored_tac::{StoredTacControlPlacement, StoredTacPage};
use super::controls::table_entry::TableControlPage;
use super::controls::tac_fit::TacFitPage;
use super::controls::tac_reconcile::TacHeightPage;
use super::controls::wrap_match::WrapBand;
use super::inline_flow::plan::InlineFlowInput;
use super::paragraph::fit::saved_tail_overflow_to_fit;
use super::paragraph::overflow::OverflowPage;
use super::paragraph::placement::ParagraphFragment;
use super::paragraph::scan::LineScanPage;
use super::paragraph::split_entry::SplitEntryPage;
use crate::model::{control::Control, paragraph::Paragraph};
use crate::renderer::float_placement::FloatLaneSet;
use crate::renderer::inline_flow::InlineFlowPlan;
use crate::renderer::page_layout::LayoutRect;
use crate::renderer::pagination::PageItem;
impl TypesetState {
pub(in crate::renderer::typeset) fn sibling_tables_emitted_on_previous_pages(
&self,
paragraph_index: usize,
current_control: usize,
host: &Paragraph,
) -> bool {
host.controls.len() > 1
&& host.controls.iter().enumerate().all(|(index, control)| {
index == current_control
|| matches!(control, Control::Table(table) if table.common.treat_as_char
&& self.data.pages.iter().filter(|page| page.section_index == self.data.section_index)
.flat_map(|page| &page.column_contents)
.flat_map(|column| &column.items)
.any(|item| matches!(item, PageItem::Table { para_index, control_index }
if *para_index == paragraph_index && *control_index == index)))
})
}
pub(in crate::renderer::typeset) fn table_flow_successor<'a>(
&self,
host_index: usize,
paragraphs: &'a [crate::model::paragraph::Paragraph],
) -> Option<(usize, &'a crate::model::paragraph::Paragraph)> {
paragraphs
.iter()
.enumerate()
.skip(
self.data
.deferred_table_successor_start
.unwrap_or(host_index + 1)
.max(host_index + 1),
)
.find(|(index, _)| {
!self.data.prefilled_paras.contains(index)
&& !self.data.pre_emitted_host_paras.contains(index)
})
}
pub(super) fn current_column_has_only_overlay_shapes(&self) -> bool {
self.data.current_height <= 0.5
&& !self.data.current_items.is_empty()
&& self
.data
.current_items
.iter()
.all(|item| matches!(item, PageItem::Shape { .. }))
}
pub(super) fn deferred_picture_page(
&self,
) -> super::controls::deferred_picture::DeferredPicturePage<'_> {
super::controls::deferred_picture::DeferredPicturePage {
profile: self.data.profile,
col_count: self.data.col_count,
current_items: &self.data.current_items,
current_footnote_height: self.data.current_footnote_height,
current_height: self.data.current_height,
}
}
pub(super) fn following_wrap_active(&self) -> bool {
self.data.wrap_around_cs >= 0
}
pub(super) fn following_wrap_is_derived(&self) -> bool {
self.data.wrap_around_derived_band
}
pub(super) fn following_wrap_layout(&self) -> &crate::renderer::page_layout::PageLayoutInfo {
&self.data.layout
}
pub(super) fn following_wrap_column_width(&self) -> f64 {
self.data
.layout
.column_areas
.get(self.data.current_column as usize)
.map(|area| area.width)
.unwrap_or(self.data.layout.body_area.width)
}
pub(super) fn following_wrap_has_items(&self) -> bool {
!self.data.current_items.is_empty()
}
pub(super) fn following_wrap_column_count(&self) -> u16 {
self.data.col_count
}
pub(super) fn wrap_tail_needs_advance(&self, suffix_height: f64) -> bool {
!self.data.current_items.is_empty()
&& self.data.current_height + suffix_height > self.available_height() + 0.5
}
pub(super) fn end_following_wrap(&mut self) {
self.data.wrap_around_cs = -1;
self.data.wrap_around_sw = -1;
self.data.wrap_around_any_seg = false;
self.data.wrap_around_derived_band = false;
self.close_square_band();
}
pub(super) fn end_following_wrap_before(&mut self, para_idx: usize) {
let before = self.data.current_height;
self.end_following_wrap();
let after = self.data.current_height;
if after > before + 0.5 {
self.data.page_band_flow_starts.insert(para_idx, after);
}
}
pub(super) fn commit_wrap_tail(
&mut self,
para_idx: usize,
wrap_prefix_len: usize,
end_line: usize,
suffix_height: f64,
content_height: f64,
last_line_spacing: Option<f64>,
) {
self.record_paragraph_content_bottom(
(para_idx, end_line),
content_height,
suffix_height,
last_line_spacing,
);
self.data.current_items.push(PageItem::PartialParagraph {
para_index: para_idx,
start_line: wrap_prefix_len,
end_line,
});
self.data.current_height += suffix_height;
self.data.vpos_ladder_dirty = true;
}
pub(super) fn commit_wrap_absorption(
&mut self,
absorption: super::controls::wrap_absorption::WrapAbsorption,
) {
if let Some(source_offset_px) = absorption.source_offset_px {
self.extend_square_band_to_source_bottom(source_offset_px);
}
self.record_wrap_around_para(absorption.paragraph);
}
fn extend_square_band_to_source_bottom(&mut self, source_offset_px: f64) {
if source_offset_px <= 0.0 {
return;
}
if let Some(top) = self.data.square_band_top {
self.data.square_band_bottom = self.data.square_band_bottom.max(top + source_offset_px);
let pen = top + source_offset_px;
self.data.square_band_source_pen = Some(
self.data
.square_band_source_pen
.map_or(pen, |current| current.max(pen)),
);
}
}
fn record_wrap_around_para(&mut self, wrap_para: crate::renderer::pagination::WrapAroundPara) {
let anchor = wrap_para.table_para_index;
let is_first_fragment = |it: &PageItem| match it {
PageItem::Table { para_index, .. } => *para_index == anchor,
PageItem::PartialTable {
para_index,
is_continuation,
..
} => *para_index == anchor && !*is_continuation,
_ => false,
};
if !self.data.current_items.iter().any(is_first_fragment) {
for page in self.data.pages.iter_mut() {
for col in page.column_contents.iter_mut() {
if col.items.iter().any(is_first_fragment) {
col.wrap_around_paras.push(wrap_para);
return;
}
}
}
}
self.data.current_column_wrap_around_paras.push(wrap_para);
}
pub(super) fn square_band_lane_pen(&self) -> Option<f64> {
self.data.square_band_top?;
let pen = self
.data
.square_band_source_pen
.map_or(self.data.current_height, |pen| {
pen.max(self.data.current_height)
});
(pen + 0.5 < self.data.square_band_bottom).then_some(pen)
}
pub(super) fn start_paragraph_at_band_pen(&mut self, para_idx: usize, pen: f64) {
self.data.current_height = pen;
self.data.page_band_flow_starts.insert(para_idx, pen);
}
pub(super) fn following_wrap_band(&self) -> WrapBand {
WrapBand {
cs: self.data.wrap_around_cs,
sw: self.data.wrap_around_sw,
anchor_para: self.data.wrap_around_table_para,
any_seg: self.data.wrap_around_any_seg,
}
}
pub(super) fn stored_wrap_rows_start_at_flow_pen(&self, para: &Paragraph, dpi: f64) -> bool {
let Some(base) = self.data.vpos_page_base.or(self.data.vpos_lazy_base) else {
return false;
};
let Some(offset) = para
.line_segs
.first()
.and_then(|line| line.vertical_pos.checked_sub(base))
else {
return false;
};
let top = self.data.vpos_col_anchor + crate::renderer::hwpunit_to_px(offset, dpi);
(top - self.data.current_height).abs() <= dpi / 7200.0
}
pub(super) fn register_following_wrap_anchor(
&mut self,
para_idx: usize,
anchor: crate::renderer::pagination::WrapAnchorRef,
) {
self.data
.current_column_wrap_anchors
.insert(para_idx, anchor);
}
pub(super) fn host_wrap_column_width_hu(&self) -> i32 {
self.data.layout.column_width_hu()
}
pub(super) fn host_wrap_needs_derived_lane(&self) -> bool {
self.data.wrap_around_cs < 0
}
pub(super) fn arm_stored_host_wrap(&mut self, para_idx: usize, band: StoredHostBand) {
self.data.wrap_around_cs = band.anchor_cs;
self.data.wrap_around_sw = band.anchor_sw;
self.data.wrap_around_table_para = para_idx;
self.data.wrap_around_any_seg = true;
self.data.wrap_around_derived_band = false;
}
pub(super) fn register_host_wrap_anchor(
&mut self,
para_idx: usize,
anchor: crate::renderer::pagination::WrapAnchorRef,
) {
self.data
.current_column_wrap_anchors
.insert(para_idx, anchor);
}
pub(super) fn arm_derived_host_wrap(&mut self, para_idx: usize, lane: i32) {
self.data.wrap_around_cs = 0;
self.data.wrap_around_sw = lane;
self.data.wrap_around_table_para = para_idx;
self.data.wrap_around_any_seg = false;
self.data.wrap_around_derived_band = true;
}
pub(super) fn paragraph_flow_profile(
&self,
) -> crate::model::provenance::LayoutCompatibilityProfile {
self.data.profile
}
pub(super) fn deferred_table_column_width(&self) -> f64 {
self.data
.layout
.column_areas
.get(self.data.current_column as usize)
.map(|a| a.width)
.unwrap_or(self.data.layout.body_area.width)
}
pub(super) fn deferred_table_anchor_height(&self) -> f64 {
self.data.current_height
}
pub(super) fn trace_table_paragraph_entry(&self, para_idx: usize) {
if std::env::var("RHWP_DIAG_TAC").is_ok() {
eprintln!(
"DIAG_TBLP pi={} cur_h={:.1} items={} page_base={:?} anchor={:.1}",
para_idx,
self.data.current_height,
self.data.current_items.len(),
self.data.vpos_page_base,
self.data.vpos_col_anchor,
);
}
}
pub(super) fn prepare_table_paragraph_column(
&mut self,
line_probe_height: f64,
para_idx: usize,
) -> f64 {
self.apply_visible_float_exclusions(line_probe_height, Some(para_idx));
self.data
.layout
.column_areas
.get(self.data.current_column as usize)
.map(|a| a.width)
.unwrap_or(self.data.layout.body_area.width)
}
pub(super) fn table_paragraph_flow_position(&self) -> (f64, usize) {
(self.data.current_height, self.data.pages.len())
}
pub(super) fn has_fragment_queued_table_footnotes(
&self,
para_idx: usize,
ctrl_idx: usize,
) -> bool {
self.data
.fragment_queued_table_footnotes
.contains(&(para_idx, ctrl_idx))
}
pub(super) fn flow_table_column_top(&self) -> bool {
self.data.current_height < 1.0
}
pub(super) fn prepare_flow_table_anchor(
&mut self,
natural_top_lead: Option<f64>,
para_start_height: f64,
) -> f64 {
if let Some(natural_top_lead) = natural_top_lead {
self.apply_visible_float_exclusions_for_para_float(natural_top_lead);
self.data.current_height
} else {
para_start_height
}
}
pub(super) fn flow_table_page_count(&self) -> usize {
self.data.pages.len()
}
pub(super) fn coanchored_table_page(&self) -> (usize, &[PageItem]) {
(self.data.pages.len(), &self.data.current_items)
}
pub(super) fn decoration_table_flow_height(&self) -> f64 {
self.data.current_height
}
pub(super) fn emit_decoration_table(&mut self, para_idx: usize, ctrl_idx: usize) {
match self.emit_decoration_table_with_storage(
para_idx,
ctrl_idx,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn emit_decoration_table_with_storage<S: crate::storage::Storage>(
&mut self,
para_idx: usize,
ctrl_idx: usize,
storage: &S,
) -> Result<(), S::Error> {
storage.push(
&mut self.data.current_items,
PageItem::Shape {
para_index: para_idx,
control_index: ctrl_idx,
},
)?;
Ok(())
}
pub(super) fn finish_decoration_table(
&mut self,
para_idx: usize,
ctrl_idx: usize,
continuation: Option<OverlayContinuation>,
) {
match self.finish_decoration_table_with_storage(
para_idx,
ctrl_idx,
continuation,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn finish_decoration_table_with_storage<S: crate::storage::Storage>(
&mut self,
para_idx: usize,
ctrl_idx: usize,
continuation: Option<OverlayContinuation>,
storage: &S,
) -> Result<(), S::Error> {
if let Some(continuation) = continuation {
let fragment = continuation.row_fragment;
storage.push(
&mut self.data.pending_overlay_continuations,
crate::renderer::pagination::OverlayContinuation {
para_index: para_idx,
control_index: ctrl_idx,
start_row: continuation.first_unfit,
start_cut: match &fragment {
Some(f) => storage.copy_slice(&f.end_cut)?,
None => Vec::new(),
},
start_row_height: fragment.as_ref().map(|f| f.remaining_height),
reserve_px: continuation.reserve_px,
},
)?;
storage.push(
&mut self.data.current_column_overlay_cuts,
(
para_idx,
ctrl_idx,
crate::renderer::pagination::OverlayCut {
end_row: continuation.first_unfit + usize::from(fragment.is_some()),
end_row_height: fragment.as_ref().map(|f| f.opening_height),
owns_stored_frame: fragment.as_ref().is_some_and(|f| f.owns_stored_frame),
end_cut: fragment.map_or_else(Vec::new, |f| f.end_cut),
},
),
)?;
}
self.data.overlay_shape_shortcut_para = Some(para_idx);
Ok(())
}
pub(super) fn table_control_page(&self) -> TableControlPage {
TableControlPage {
has_items: !self.data.current_items.is_empty(),
col_count: self.data.col_count,
profile: self.data.profile,
}
}
pub(super) fn commit_decoration_host_text(&mut self, fragment: ParagraphFragment) {
match self.commit_decoration_host_text_with_storage(fragment, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn commit_decoration_host_text_with_storage<S: crate::storage::Storage>(
&mut self,
fragment: ParagraphFragment,
storage: &S,
) -> Result<(), S::Error> {
storage.push(&mut self.data.current_items, fragment.item)?;
self.data.current_height += fragment.height;
Ok(())
}
pub(super) fn tac_height_page(&self) -> TacHeightPage<'_> {
TacHeightPage {
profile: self.data.profile,
current_height: self.data.current_height,
vpos_page_base: self.data.vpos_page_base,
vpos_col_anchor: self.data.vpos_col_anchor,
inline_placements: &self.data.inline_placements,
inline_box_flow_bottom: self.data.inline_box_flow_bottom,
}
}
pub(super) fn commit_tac_spacing_omission(&mut self, trace: impl FnOnce()) {
self.data.stored_ladder_spacing_omitted = true;
trace();
self.data.vpos_ladder_dirty = true;
}
pub(super) fn commit_tac_capped_bottom(&mut self, capped_bottom: f64) {
if self.data.current_height > capped_bottom {
self.data.current_height = capped_bottom;
}
}
pub(super) fn restore_block_table_flow_bottom(&mut self, bottom: f64) {
if bottom > self.data.current_height {
self.data.block_table_growth_page = Some(self.data.pages.len());
}
self.data.current_height = self.data.current_height.max(bottom);
}
pub(super) fn has_spilled_page_tail_float(&self, para_idx: usize, ctrl_idx: usize) -> bool {
self.data
.page_tail_spilled_floats
.contains(&(para_idx, ctrl_idx))
}
pub(super) fn table_host_shape_page(&self) -> TableHostShapePage {
TableHostShapePage {
has_items: !self.data.current_items.is_empty(),
current_height: self.data.current_height,
}
}
pub(super) fn commit_table_host_shape(
&mut self,
para_idx: usize,
ctrl_idx: usize,
flow: TableHostShapeFlow,
accepted_frame: Option<crate::renderer::float_placement::ParagraphFloatPlacement>,
) {
match self.commit_table_host_shape_with_storage(
para_idx,
ctrl_idx,
flow,
accepted_frame,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn commit_table_host_shape_with_storage<S: crate::storage::Storage>(
&mut self,
para_idx: usize,
ctrl_idx: usize,
flow: TableHostShapeFlow,
accepted_frame: Option<crate::renderer::float_placement::ParagraphFloatPlacement>,
storage: &S,
) -> Result<(), S::Error> {
storage.push(
&mut self.data.current_items,
PageItem::Shape {
para_index: para_idx,
control_index: ctrl_idx,
},
)?;
if let Some(frame) = accepted_frame {
self.data.current_height = frame.occupied_bottom;
storage.insert_map(
&mut self.data.paragraph_float_placements,
(para_idx, ctrl_idx),
frame,
)?;
} else if let Some(line_h) = flow.tac_separate_line_h {
self.data.current_height += line_h;
} else if let Some(extra) = flow.non_tac_pushdown_h {
self.data.current_height += extra;
}
Ok(())
}
pub(super) fn defer_table_host_shape_to_next_column(
&mut self,
para_idx: usize,
ctrl_idx: usize,
flow: TableHostShapeFlow,
) -> bool {
let Some(height) = flow.non_tac_pushdown_h else {
return false;
};
if self.data.current_column + 1 >= self.data.col_count {
return false;
}
let reserved: f64 = self
.data
.deferred_column_shapes
.iter()
.filter_map(|(_, _, flow)| flow.non_tac_pushdown_h)
.sum();
if self.data.pending_body_wide_top_reserve + reserved + height > self.available_height() {
return false;
}
self.data
.deferred_column_shapes
.push((para_idx, ctrl_idx, flow));
true
}
pub(in crate::renderer::typeset) fn finish_deferred_column_shapes(&mut self) {
match self.finish_deferred_column_shapes_with_storage(&crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(in crate::renderer::typeset) fn finish_deferred_column_shapes_with_storage<
S: crate::storage::Storage,
>(
&mut self,
storage: &S,
) -> Result<(), S::Error> {
if !self.data.deferred_column_shapes.is_empty() {
self.advance_column_or_new_page_with_storage(storage)?;
}
Ok(())
}
pub(super) fn empty_float_page(
&self,
paragraphs: &[crate::model::paragraph::Paragraph],
) -> EmptyFloatPage<'_> {
EmptyFloatPage {
layout: &self.data.layout,
current_column: self.data.current_column,
profile: self.data.profile,
current_height: self.data.current_height,
current_items: &self.data.current_items,
visible_float_exclusions: &self.data.visible_float_exclusions,
original_column_origin: self.data.current_zone_y_offset == 0.0
&& self.data.vpos_col_anchor == 0.0
&& self.data.current_items.first().is_some_and(|item| {
let PageItem::FullParagraph { para_index } = item else { return false; };
paragraphs.get(*para_index).is_some_and(|first| {
first.controls.is_empty()
&& !first.stored_text_partition_is_dirty()
&& first.source_line_seg_vertical_pos.is_none()
&& first.line_segs.first().is_some_and(|line| {
line.vertical_pos == 0
&& line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
})
})
}),
}
}
pub(super) fn empty_float_available_height(
&self,
note_content_height: f64,
note_count: usize,
) -> f64 {
let total_footnote = self.projected_footnote_height(note_content_height, note_count);
(self.base_available_height() - total_footnote - self.data.current_zone_y_offset).max(0.0)
}
pub(super) fn commit_empty_float_table(
&mut self,
para_idx: usize,
ctrl_idx: usize,
placement: EmptyFloatPlacement,
lanes: &mut FloatLaneSet,
) {
let EmptyFloatPlacement {
x_start,
x_end,
raw_top,
reserved_height,
resolved,
} = placement;
if let Some(resolved) = resolved {
self.record_paragraph_float_placement((para_idx, ctrl_idx), resolved);
}
self.data.current_items.push(PageItem::Table {
para_index: para_idx,
control_index: ctrl_idx,
});
lanes.place(Some(ctrl_idx), x_start, x_end, raw_top, reserved_height);
self.data.current_height = self.data.current_height.max(lanes.max_bottom());
}
pub(super) fn has_deferred_table_controls(&self) -> bool {
!self.data.deferred_table_controls.is_empty()
}
pub(super) fn enqueue_deferred_table_controls(&mut self, deferred: Vec<DeferredTableControl>) {
self.data.deferred_table_controls.extend(deferred);
}
pub(super) fn take_deferred_table_controls(&mut self) -> Vec<DeferredTableControl> {
std::mem::take(&mut self.data.deferred_table_controls)
}
pub(super) fn restore_deferred_table_controls(&mut self, remaining: Vec<DeferredTableControl>) {
self.data.deferred_table_controls = remaining;
}
pub(super) fn tac_fit_page(&self) -> TacFitPage<'_> {
TacFitPage {
profile: self.data.profile,
current_height: self.data.current_height,
vpos_page_base: self.data.vpos_page_base,
current_items: &self.data.current_items,
}
}
pub(super) fn commit_stored_tac_control(
&mut self,
para_idx: usize,
placement: StoredTacControlPlacement,
) {
self.data
.inline_placements
.insert((para_idx, placement.control_index), placement.inline);
self.data.current_items.push(PageItem::Table {
para_index: para_idx,
control_index: placement.control_index,
});
self.data.current_height = placement.end;
}
pub(super) fn paragraph_whole_fit_page(&self) -> super::paragraph::whole_fit::WholeFitPage<'_> {
super::paragraph::whole_fit::WholeFitPage {
profile: self.data.profile,
omit_fresh_recalc_doc: self.data.omit_fresh_recalc_doc,
col_count: self.data.col_count,
current_items: &self.data.current_items,
current_height: self.data.current_height,
body_height: self.base_available_height(),
visible_float_exclusions: &self.data.visible_float_exclusions,
hangul2024_reclaimed: self.data.hangul2024_reclaimed,
}
}
pub(super) fn begin_empty_paragraph_page(&mut self) {
let current_page_idx = self.data.pages.len();
if current_page_idx != self.data.hidden_empty_page_idx {
self.data.hidden_empty_lines = 0;
self.data.hidden_empty_page_idx = current_page_idx;
}
}
pub(super) fn hide_empty_paragraph(&mut self, para_idx: usize) {
match self.hide_empty_paragraph_with_storage(para_idx, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn hide_empty_paragraph_with_storage<S: crate::storage::Storage>(
&mut self,
para_idx: usize,
storage: &S,
) -> Result<(), S::Error> {
storage.insert_set(&mut self.data.hidden_empty_paras, para_idx)?;
Ok(())
}
pub(super) fn commit_counted_hidden_paragraph(&mut self, para_idx: usize) {
match self
.commit_counted_hidden_paragraph_with_storage(para_idx, &crate::storage::Unbounded)
{
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn commit_counted_hidden_paragraph_with_storage<S: crate::storage::Storage>(
&mut self,
para_idx: usize,
storage: &S,
) -> Result<(), S::Error> {
self.data.hidden_empty_lines += 1;
storage.insert_set(&mut self.data.hidden_empty_paras, para_idx)?;
storage.push(
&mut self.data.current_items,
PageItem::FullParagraph {
para_index: para_idx,
},
)?;
Ok(())
}
pub(super) fn place_unadvanced_empty_paragraph(&mut self, para_idx: usize) {
match self
.place_unadvanced_empty_paragraph_with_storage(para_idx, &crate::storage::Unbounded)
{
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn place_unadvanced_empty_paragraph_with_storage<S: crate::storage::Storage>(
&mut self,
para_idx: usize,
storage: &S,
) -> Result<(), S::Error> {
storage.push(
&mut self.data.current_items,
PageItem::FullParagraph {
para_index: para_idx,
},
)?;
Ok(())
}
pub(super) fn paragraph_empty_tail_page(&self) -> super::paragraph::empty::EmptyTailPage<'_> {
super::paragraph::empty::EmptyTailPage {
col_count: self.data.col_count,
current_items: &self.data.current_items,
current_height: self.data.current_height,
body_height: self.base_available_height(),
current_zone_y_offset: self.data.current_zone_y_offset,
current_footnote_height: self.data.current_footnote_height,
painted_footnote_area_reserved: self.data.current_footnote_body_bottom_reserved,
}
}
pub(super) fn fill_paragraph_entry_page_tail(&mut self) {
self.data.current_height = self.data.current_height.max(self.available_height());
}
pub(super) fn take_strict_paragraph_fit(
&mut self,
para: &crate::model::paragraph::Paragraph,
) -> bool {
super::take_strict_plain_text_fit_after_empty_host_float_once(
&mut self.data.strict_plain_text_fit_after_empty_host_float_once,
para,
)
}
pub(super) fn begin_empty_line_paragraph(&mut self, para_idx: usize) {
match self.begin_empty_line_paragraph_with_storage(para_idx, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn begin_empty_line_paragraph_with_storage<S: crate::storage::Storage>(
&mut self,
para_idx: usize,
storage: &S,
) -> Result<(), S::Error> {
storage.push(
&mut self.data.current_items,
PageItem::FullParagraph {
para_index: para_idx,
},
)?;
Ok(())
}
pub(super) fn paragraph_split_entry_page(&self) -> SplitEntryPage<'_> {
SplitEntryPage {
profile: self.data.profile,
col_count: self.data.col_count,
current_height: self.data.current_height,
current_items: &self.data.current_items,
zone_y_offset: self.data.current_zone_y_offset,
plain_text_zone: self.data.current_zone_plain_text,
body_height: self.base_available_height(),
stored_ladder_spacing_omitted: self.data.stored_ladder_spacing_omitted,
hangul2024_reclaimed: self.data.hangul2024_reclaimed,
}
}
pub(super) fn mark_blank_paragraph_spill(&mut self, para_idx: usize) {
self.data.hangul2024_spill_para = Some(para_idx);
}
pub(super) fn paragraph_overflow_page(
&self,
current_page_vpos_base: Option<i32>,
) -> OverflowPage {
OverflowPage {
col_count: self.data.col_count,
current_height: self.data.current_height,
has_items: !self.data.current_items.is_empty(),
body_height: self.base_available_height(),
body_area_height: self.data.layout.body_area.height,
hwp3_layout: self.data.profile.hwp3_layout(),
stored_layout_valid: !self.data.profile.session_edited(),
current_page_vpos_base,
}
}
pub(super) fn record_stored_paragraph_boundary_carry(
&mut self,
para: usize,
start_line: usize,
) {
self.data.stored_paragraph_boundary_carry = Some((para, start_line));
}
pub(super) fn stored_paragraph_boundary_already_open(
&self,
paragraphs: &[Paragraph],
para_idx: usize,
) -> bool {
let Some((owner, cut)) = self.data.stored_paragraph_boundary_carry else {
return false;
};
self.data.col_count == 1
&& !self.data.profile.session_edited()
&& owner < para_idx
&& paragraphs[para_idx].column_type == super::ColumnBreakType::None
&& paragraphs[owner + 1..para_idx]
.iter()
.all(|p| p.controls.is_empty() && p.text.trim().is_empty())
&& matches!(self.data.current_items.first(), Some(PageItem::PartialParagraph {
para_index, start_line, ..
}) if *para_index == owner && *start_line == cut)
&& self.data.current_items.iter().skip(1).all(|item| {
super::page_item_para_index(item)
.and_then(|idx| paragraphs.get(idx))
.is_some_and(|p| p.controls.is_empty() && p.text.trim().is_empty())
})
}
pub(super) fn stored_boundary_crossed_by_overflow(
&self,
paragraphs: &[Paragraph],
para_idx: usize,
dpi: f64,
) -> bool {
if self.data.col_count != 1
|| self.data.profile.session_edited()
|| !(self.data.profile.hwpx_stored_layout()
|| self.data.profile.hwp5_stored_pagination_layout())
{
return false;
}
let Some(first) = self.data.current_items.first() else {
return false;
};
if !matches!(
first,
PageItem::FullParagraph { .. } | PageItem::Table { .. }
) {
return false;
}
let Some(start) = super::page_item_para_index(first) else {
return false;
};
if start == 0 || start >= para_idx {
return false;
}
let stored_first = |p: &Paragraph| {
p.line_segs
.first()
.filter(|line| !super::is_synthetic_line_seg(line))
.map(|line| line.vertical_pos)
};
let stored_last = |p: &Paragraph| {
p.line_segs
.last()
.filter(|line| !super::is_synthetic_line_seg(line))
.map(|line| line.vertical_pos)
};
let rows_never_rewind = |p: &Paragraph| {
p.line_segs
.windows(2)
.all(|pair| pair[1].vertical_pos >= pair[0].vertical_pos)
};
if self.data.block_table_growth_page != Some(self.data.pages.len().saturating_sub(1)) {
return false;
}
let Some(previous_start) = self
.data
.pages
.iter()
.rev()
.nth(1)
.and_then(|page| page.column_contents.first())
.and_then(|column| column.items.first())
.and_then(super::page_item_para_index)
else {
return false;
};
if previous_start >= start {
return false;
}
let stored_bottom_px = paragraphs[start]
.line_segs
.last()
.filter(|line| !super::is_synthetic_line_seg(line))
.map(|line| {
crate::renderer::hwpunit_to_px(
line.vertical_pos.saturating_add(line.line_height),
dpi,
)
});
if !stored_bottom_px.is_some_and(|bottom| bottom <= self.data.layout.body_area.height + 0.5)
{
return false;
}
if !(previous_start..para_idx).all(|index| rows_never_rewind(¶graphs[index])) {
return false;
}
(previous_start + 1..para_idx).all(|index| {
let para = ¶graphs[index];
para.column_type == super::ColumnBreakType::None
&& matches!(
(stored_last(¶graphs[index - 1]), stored_first(para)),
(Some(previous), Some(current)) if current > 0 && current >= previous
)
})
}
pub(super) fn begin_atomic_overflow_paragraph(&mut self, para_idx: usize) {
match self
.begin_atomic_overflow_paragraph_with_storage(para_idx, &crate::storage::Unbounded)
{
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn begin_atomic_overflow_paragraph_with_storage<S: crate::storage::Storage>(
&mut self,
para_idx: usize,
storage: &S,
) -> Result<(), S::Error> {
storage.push(
&mut self.data.current_items,
PageItem::FullParagraph {
para_index: para_idx,
},
)?;
Ok(())
}
pub(super) fn advance_atomic_overflow_paragraph(
&mut self,
advance: f64,
total_height: f64,
body_bottom_vpos: Option<i32>,
) {
self.data.current_height += advance;
self.data.flow_underrun += (total_height - advance).max(0.0);
if let Some(v) = body_bottom_vpos {
self.data.prev_body_bottom_vpos = Some(v);
}
}
pub(super) fn commit_tail_overflow_paragraph(
&mut self,
para_idx: usize,
total_height: f64,
body_bottom_vpos: Option<i32>,
) {
match self.commit_tail_overflow_paragraph_with_storage(
para_idx,
total_height,
body_bottom_vpos,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn commit_tail_overflow_paragraph_with_storage<S: crate::storage::Storage>(
&mut self,
para_idx: usize,
total_height: f64,
body_bottom_vpos: Option<i32>,
storage: &S,
) -> Result<(), S::Error> {
storage.push(
&mut self.data.current_items,
PageItem::FullParagraph {
para_index: para_idx,
},
)?;
self.data.vpos_prev_trimmed_sb_px = 0.0;
self.data.current_height += total_height;
let _: () = if let Some(v) = body_bottom_vpos {
self.data.prev_body_bottom_vpos = Some(v);
};
Ok(())
}
pub(super) fn insert_fitted_paragraph(&mut self, para_idx: usize, defer_preceding_float: bool) {
match self.insert_fitted_paragraph_with_storage(
para_idx,
defer_preceding_float,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn insert_fitted_paragraph_with_storage<S: crate::storage::Storage>(
&mut self,
para_idx: usize,
defer_preceding_float: bool,
storage: &S,
) -> Result<(), S::Error> {
storage.reserve(&mut self.data.current_items, 1)?;
let paragraph_item = PageItem::FullParagraph {
para_index: para_idx,
};
let _: () = if defer_preceding_float {
let table_item = self
.data
.current_items
.pop()
.expect("checked trailing table item");
storage.push(&mut self.data.current_items, paragraph_item)?;
storage.push(&mut self.data.current_items, table_item)?;
} else {
storage.push(&mut self.data.current_items, paragraph_item)?;
};
Ok(())
}
pub(super) fn apply_full_paragraph_flow(
&mut self,
advance: f64,
total_height: f64,
trimmed_spacing_before: f64,
body_bottom_vpos: Option<i32>,
) {
self.data.vpos_prev_trimmed_sb_px = trimmed_spacing_before;
self.data.current_height += advance;
self.data.flow_underrun += (total_height - advance).max(0.0);
if let Some(v) = body_bottom_vpos {
self.data.prev_body_bottom_vpos = Some(v);
}
}
pub(super) fn commit_split_paragraph_fragment(&mut self, fragment: ParagraphFragment) {
match self
.commit_split_paragraph_fragment_with_storage(fragment, &crate::storage::Unbounded)
{
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn commit_split_paragraph_fragment_with_storage<S: crate::storage::Storage>(
&mut self,
fragment: ParagraphFragment,
storage: &S,
) -> Result<(), S::Error> {
if let (
PageItem::PartialParagraph {
para_index,
end_line,
..
},
Some(height),
) = (&fragment.item, fragment.content_height)
{
self.record_paragraph_content_bottom_with_storage(
(*para_index, *end_line),
height,
fragment.height,
fragment.last_line_spacing,
storage,
)?;
}
storage.push(&mut self.data.current_items, fragment.item)?;
self.data.vpos_prev_trimmed_sb_px = 0.0;
self.data.current_height += fragment.height;
Ok(())
}
pub(super) fn commit_multicolumn_paragraph_fragment(&mut self, fragment: ParagraphFragment) {
match self.commit_multicolumn_paragraph_fragment_with_storage(
fragment,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn commit_multicolumn_paragraph_fragment_with_storage<S: crate::storage::Storage>(
&mut self,
fragment: ParagraphFragment,
storage: &S,
) -> Result<(), S::Error> {
storage.push(&mut self.data.current_items, fragment.item)?;
self.data.current_height += fragment.height;
Ok(())
}
pub(super) fn advance_after_multicolumn_fragment(&mut self) {
match self.advance_after_multicolumn_fragment_with_storage(&crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn advance_after_multicolumn_fragment_with_storage<S: crate::storage::Storage>(
&mut self,
storage: &S,
) -> Result<(), S::Error> {
let _: () = if self.data.current_column + 1 < self.data.col_count
&& self.data.deferred_column_shapes.is_empty()
{
self.flush_column_with_storage(storage)?;
self.data.current_column += 1;
self.data.current_height = 0.0;
self.activate_next_column_stored_frames_with_storage(storage)?;
} else {
self.advance_column_or_new_page_with_storage(storage)?;
};
Ok(())
}
pub(super) fn paragraph_line_scan_page(&self) -> LineScanPage {
LineScanPage {
profile: self.data.profile,
col_count: self.data.col_count,
body_height: self.base_available_height(),
current_height: self.data.current_height,
has_items: !self.data.current_items.is_empty(),
vpos_ladder_dirty: self.data.vpos_ladder_dirty,
stored_vpos_base: self.data.vpos_page_base,
current_footnote_height: self.data.current_footnote_height,
footnote_separator_overhead: self.data.footnote_separator_overhead,
footnote_between_notes_margin: self.data.footnote_between_notes_margin,
current_zone_y_offset: self.data.current_zone_y_offset,
current_bottom_fixed_exclusion: self.data.current_bottom_fixed_exclusion,
next_page_stored_body_origin: self
.data
.deferred_next_page_stored_frames
.iter()
.filter(|frame| {
matches!(
frame.kind,
super::DeferredStoredFrameKind::Table
| super::DeferredStoredFrameKind::AbsoluteShape
)
})
.map(|frame| frame.placement.occupied_bottom)
.reduce(f64::max),
}
}
pub(super) fn has_pending_picture_group_reset(&self, origin: f64, dpi: f64) -> bool {
self.data
.deferred_next_page_stored_frames
.iter()
.any(|frame| {
matches!(
frame.placement.flow,
crate::renderer::float_placement::ParagraphFloatFlow::StoredPictureGroup { .. }
) && (frame.placement.occupied_bottom - origin).abs() <= dpi / 7200.0
})
}
pub(super) fn inline_flow_column(&self) -> LayoutRect {
let layout = self
.data
.current_zone_layout
.as_ref()
.unwrap_or(&self.data.layout);
let mut column = layout
.column_areas
.get(self.data.current_column as usize)
.copied()
.unwrap_or(layout.body_area);
column.y += self.data.current_zone_y_offset;
column.height = (column.height - self.data.current_zone_y_offset).max(0.0);
column
}
pub(super) fn inline_flow_input(&self, start: f64, preceding: bool) -> InlineFlowInput<'_> {
InlineFlowInput {
column: self.inline_flow_column(),
body: &self.data.layout.body_area,
page_width: self.data.layout.page_width,
page_height: self.data.layout.page_height,
start,
exclusions: preceding.then_some(&self.data.side_wrap_exclusions),
visible_float_exclusions: preceding.then_some(&self.data.visible_float_exclusions),
reflow_stored_text: false,
}
}
pub(super) fn stored_body_wide_host_frame(&self) -> Option<(LayoutRect, f64)> {
(self.data.current_items.is_empty()
&& self.data.current_column == 0
&& self.data.col_count > 1
&& self.data.current_start_height == 0.0
&& self.data.current_zone_y_offset == 0.0
&& !self.data.current_endnote_flow)
.then_some((self.data.layout.body_area, self.data.layout.page_height))
}
pub(super) fn commit_stored_body_wide_host(
&mut self,
para_index: usize,
para: &crate::model::paragraph::Paragraph,
plan: InlineFlowPlan,
) {
match self.commit_stored_body_wide_host_with_storage(
para_index,
para,
plan,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn commit_stored_body_wide_host_with_storage<S: crate::storage::Storage>(
&mut self,
para_index: usize,
para: &crate::model::paragraph::Paragraph,
plan: InlineFlowPlan,
storage: &S,
) -> Result<(), S::Error> {
if self.data.current_column != 0
|| !matches!(self.data.current_items.first(),
Some(PageItem::PartialParagraph { para_index: owner, start_line: 0, end_line })
if *owner == para_index && *end_line == para.line_segs.len())
|| plan.end > self.available_height()
{
return Ok(());
}
storage.insert_map(&mut self.data.inline_flow_plans, para_index, plan)?;
Ok(())
}
pub(super) fn commit_inline_flow(&mut self, para_index: usize, plan: InlineFlowPlan) {
match self.commit_inline_flow_with_storage(para_index, plan, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(super) fn commit_inline_flow_with_storage<S: crate::storage::Storage>(
&mut self,
para_index: usize,
plan: InlineFlowPlan,
storage: &S,
) -> Result<(), S::Error> {
for item in &plan.floating_shapes {
if let Some(exclusion) = &item.exclusion {
let exclusion = crate::storage::try_clone(exclusion, storage)?;
storage.insert_tree_map(
&mut self.data.side_wrap_exclusions,
(para_index, item.control),
exclusion,
)?;
}
storage.insert_map(
&mut self.data.paragraph_float_placements,
(para_index, item.control),
item.placement,
)?;
storage.push(
&mut self.data.current_items,
PageItem::Shape {
para_index,
control_index: item.control,
},
)?;
}
storage.push(
&mut self.data.current_items,
PageItem::FullParagraph { para_index },
)?;
self.data.current_height = plan.end;
self.data.inline_box_flow_bottom = self.data.inline_box_flow_bottom.max(plan.end);
storage.insert_map(&mut self.data.inline_flow_plans, para_index, plan)?;
self.data.vpos_ladder_dirty = true;
Ok(())
}
pub(super) fn record_square_host_flow(&mut self, para_index: usize, plan: InlineFlowPlan) {
if let (Some(control), Some(mut exclusion)) =
(plan.square_host_control, plan.square_host_exclusion.clone())
{
let column = self.inline_flow_column();
let dx = crate::renderer::px_to_hwpunit(column.x, self.data.layout.dpi);
let dy = crate::renderer::px_to_hwpunit(column.y + plan.start, self.data.layout.dpi);
exclusion.horizontal.start += dx;
exclusion.horizontal.end += dx;
exclusion.vertical.start += dy;
exclusion.vertical.end += dy;
self.data
.side_wrap_exclusions
.insert((para_index, control), exclusion);
}
if let Some(placement) = plan.square_host_placement {
if let Some(control) = plan.square_host_control {
self.data
.paragraph_float_placements
.insert((para_index, control), placement);
}
}
self.data.inline_flow_plans.insert(para_index, plan);
self.data.vpos_ladder_dirty = true;
}
pub(super) fn take_paragraph_safety_margin(
&mut self,
strict_after_empty_host_float: bool,
prev_is_partial_table: bool,
layout_drift_safety_px: f64,
) -> f64 {
if strict_after_empty_host_float {
self.data.skip_safety_margin_once = false;
}
if self.data.skip_safety_margin_once {
self.data.skip_safety_margin_once = false;
0.0
} else if prev_is_partial_table {
0.0
} else if self.data.vpos_page_base_stored && self.data.vpos_page_base.is_some() {
0.0
} else {
layout_drift_safety_px
}
}
pub(super) fn take_paragraph_tail_overflow(
&mut self,
strict_after_empty_host_float: bool,
fit_height: f64,
) -> f64 {
if strict_after_empty_host_float {
self.data.tail_saved_bounds_once = None;
0.0
} else {
self.data
.tail_saved_bounds_once
.take()
.and_then(|bounds| {
saved_tail_overflow_to_fit(
bounds,
self.data.current_height,
fit_height,
self.base_available_height(),
self.data.current_footnote_height,
)
})
.unwrap_or(0.0)
}
}
}
mod commands;
mod computed_float_line;
mod data;
mod empty_run;
pub(super) mod finalize;
mod footnote_frames;
mod mixed_host_line;
mod notes;
mod paragraph_fragment;
mod stored_tac;
mod transition;
mod whole_float_successor;
pub(super) struct TypesetState {
data: Revised<data::StateView>,
}
impl std::ops::Deref for TypesetState {
type Target = data::StateView;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl TypesetState {
pub(in crate::renderer::typeset) fn revision(&self) -> u64 {
self.data.revision
}
}
struct Revised<T> {
value: T,
revision: u64,
}
static NEXT_STATE_REVISION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
fn next_state_revision() -> u64 {
NEXT_STATE_REVISION.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
impl<T> Revised<T> {
fn new(value: T) -> Self {
Self {
value,
revision: next_state_revision(),
}
}
}
impl<T> std::ops::Deref for Revised<T> {
type Target = T;
fn deref(&self) -> &T {
&self.value
}
}
impl<T> std::ops::DerefMut for Revised<T> {
fn deref_mut(&mut self) -> &mut T {
self.revision = next_state_revision();
&mut self.value
}
}