use super::LayoutEngine;
use crate::model::paragraph::{LineSeg, NumberingMarker};
use crate::model::style::HeadType;
use crate::model::table::{Table, TablePageBreak};
use crate::renderer::style_resolver::ResolvedStyleSet;
#[derive(Clone, Copy)]
enum CutSource {
Stored,
Reflow,
}
impl LayoutEngine {
pub(crate) fn row_cut_completes_unlinked_plain_paragraphs(
&self,
table: &Table,
row: usize,
start_cut: &[usize],
end_cut: &[usize],
styles: &ResolvedStyleSet,
) -> bool {
self.row_cut_plain_paragraph_contract(
table,
row,
start_cut,
end_cut,
styles,
(true, CutSource::Stored),
)
.is_some()
}
pub(crate) fn row_cut_closes_independent_list_items(
&self,
table: &Table,
row: usize,
start_cut: &[usize],
end_cut: &[usize],
styles: &ResolvedStyleSet,
) -> bool {
if !self.row_cut_completes_unlinked_plain_paragraphs(table, row, start_cut, end_cut, styles)
{
return false;
}
let mut has_item = false;
for (index, cell_index) in Self::row_cut_cell_order(table, row).into_iter().enumerate() {
let cell = &table.cells[cell_index];
let units = self.cell_units(cell, table, styles);
let end = end_cut[index];
if end == units.len() {
continue;
}
let paragraph = &cell.paragraphs[units[end - 1].para_idx];
let style = &styles.para_styles[paragraph.para_shape_id as usize];
let has_marker = match style.head_type {
HeadType::Number => matches!(
¶graph.numbering_marker,
NumberingMarker::Text(text, _) if !text.trim().is_empty()
),
HeadType::Bullet => {
super::paragraph_layout::bullet_marker_text(style, styles).is_some()
}
_ => false,
};
if !has_marker {
return false;
}
has_item = true;
}
has_item
}
pub(crate) fn row_cut_respects_plain_paragraph_keeps(
&self,
table: &Table,
row: usize,
start_cut: &[usize],
end_cut: &[usize],
styles: &ResolvedStyleSet,
) -> bool {
self.row_cut_plain_paragraph_contract(
table,
row,
start_cut,
end_cut,
styles,
(false, CutSource::Stored),
)
.is_some()
}
pub(crate) fn reflow_row_cut_respects_plain_paragraph_keeps(
&self,
table: &Table,
row: usize,
start_cut: &[usize],
end_cut: &[usize],
styles: &ResolvedStyleSet,
) -> bool {
self.row_cut_plain_paragraph_contract(
table,
row,
start_cut,
end_cut,
styles,
(false, CutSource::Reflow),
)
.is_some()
}
pub(crate) fn row_cut_keeps_close_a_plain_paragraph(
&self,
table: &Table,
row: usize,
start_cut: &[usize],
end_cut: &[usize],
styles: &ResolvedStyleSet,
) -> bool {
self.row_cut_plain_paragraph_contract(
table,
row,
start_cut,
end_cut,
styles,
(false, CutSource::Stored),
) == Some(true)
}
fn row_cut_plain_paragraph_contract(
&self,
table: &Table,
row: usize,
start_cut: &[usize],
end_cut: &[usize],
styles: &ResolvedStyleSet,
(require_complete_paragraphs, source): (bool, CutSource),
) -> Option<bool> {
let profile = self.profile.get();
if !(profile.hwp5_stored_pagination_layout() || profile.hwpx_stored_layout())
|| table.layout_page_break() != TablePageBreak::RowBreak
|| table.common.treat_as_char
{
return None;
}
let mut has_kept_cut = false;
let mut closes_paragraph = false;
for (index, cell_index) in Self::row_cut_cell_order(table, row).into_iter().enumerate() {
let cell = &table.cells[cell_index];
let units = self.cell_units(cell, table, styles);
let &end = end_cut.get(index)?;
if end > units.len() {
return None;
}
if end == units.len() {
continue;
}
let cell_reflowed = self
.render_normalization
.borrow()
.cell_frame_reflowed(table, cell);
if matches!(source, CutSource::Stored) && cell_reflowed {
return None;
}
let start = start_cut.get(index).copied().unwrap_or(0);
if end <= start
|| cell.text_direction != 0
|| cell.paragraphs.iter().any(|para| {
!para
.controls
.iter()
.all(crate::renderer::composer::control_is_width_neutral_marker)
})
{
return None;
}
let closing = &units[end - 1];
let following = &units[end];
let complete = closing.closes_before_next_paragraph(following);
if !complete
&& (require_complete_paragraphs || !closing.continues_same_paragraph(following))
{
return None;
}
let paragraph = cell.paragraphs.get(closing.para_idx)?;
if paragraph.text.trim().is_empty() {
return None;
}
match source {
CutSource::Stored => {
if paragraph.stored_text_partition_is_dirty()
|| paragraph.cell_format_vpos_dirty
|| (paragraph.line_segs.is_empty() && !complete)
|| paragraph
.line_segs
.iter()
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
{
return None;
}
}
CutSource::Reflow => {
if !(cell_reflowed || crate::renderer::para_has_no_stored_line_segs(paragraph))
|| !closing.has_visible_line()
{
return None;
}
}
}
let style = styles.para_styles.get(paragraph.para_shape_id as usize)?;
if (complete && style.keep_with_next)
|| (!complete && (style.keep_lines || style.widow_orphan))
{
return None;
}
has_kept_cut = true;
closes_paragraph |= complete;
}
has_kept_cut.then_some(closes_paragraph)
}
}