use crate::model::{control::Control, paragraph::Paragraph};
use crate::renderer::{hwpunit_to_px, style_resolver::ResolvedStyleSet};
pub(crate) mod initial_column_band;
fn has_paragraph_float_graphic(paragraph: &Paragraph) -> bool {
use crate::model::shape::{TextWrap, VertRelTo};
paragraph.controls.iter().any(|control| {
let common = match control {
Control::Shape(shape) => shape.common(),
Control::Picture(picture) => &picture.common,
_ => return false,
};
!common.treat_as_char
&& common.text_wrap == TextWrap::TopAndBottom
&& common.vert_rel_to == VertRelTo::Para
})
}
fn paragraph_float_frame_end_hu(paragraph: &Paragraph) -> Option<i64> {
use crate::model::shape::{TextWrap, VertAlign, VertRelTo};
let [Control::Table(table)] = paragraph.controls.as_slice() else {
return None;
};
if table.common.treat_as_char
|| !table.common.flow_with_text
|| table.common.allow_overlap
|| table.caption.is_some()
|| table.common.text_wrap != TextWrap::TopAndBottom
|| table.common.vert_rel_to != VertRelTo::Para
|| table.common.vert_align != VertAlign::Top
|| table.common.height == 0
|| table.common.height >= 0x8000_0000
|| (table.common.vertical_offset as i32) < 0
|| paragraph.stored_text_partition_is_dirty()
|| paragraph.cell_format_vpos_dirty
{
return None;
}
Some(
i64::from(table.common.vertical_offset)
+ i64::from(table.common.height)
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom),
)
}
fn control_only_overlapping_shape_frame(first: &Paragraph) -> bool {
use crate::model::paragraph::LineSeg;
if !first.text.is_empty()
|| !first.char_offsets.is_empty()
|| first.stored_text_partition_is_dirty()
|| first.cell_format_vpos_dirty
|| first.line_segs.len() < 2
|| !first.line_segs.windows(2).enumerate().all(|(index, _)| {
super::stored_line_ladder::stored_overlap_advance_hu(
first,
index,
first.line_segs.len(),
true,
)
.is_some()
})
{
return false;
}
let mut shapes = first
.controls
.iter()
.enumerate()
.filter_map(|(index, control)| {
if let Control::Shape(shape) = control {
shape.common().treat_as_char.then_some((index, shape))
} else {
None
}
});
let Some((index, shape)) = shapes.next() else {
return false;
};
if shapes.next().is_some()
|| first.controls.iter().enumerate().any(|(other, control)| {
other != index
&& (control.is_logical_inline()
|| matches!(
control,
Control::Shape(_)
| Control::Picture(_)
| Control::Table(_)
| Control::Form(_)
| Control::Equation(_)
| Control::AutoNumber(_)
| Control::Ruby(_)
| Control::Unknown(_)
))
})
|| first.empty_control_stream_position(index).is_none()
|| super::composer::control_slot_line_index(first, index) != Some(first.line_segs.len() - 1)
|| super::composer::shape_caption_for_layout(shape).is_some()
{
return false;
}
let last = first.line_segs.last().expect("두 저장 줄을 확인함");
let common = shape.common();
let frame = i64::from(shape.flow_height_hu())
+ i64::from(common.margin.top)
+ i64::from(common.margin.bottom);
last.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& frame > 0
&& i64::from(last.line_height) == frame
}
pub(crate) fn stored_first_margin_is_page_relative(
first: &Paragraph,
next: Option<&Paragraph>,
styles: &ResolvedStyleSet,
dpi: f64,
) -> bool {
let Some(first_seg) = first.line_segs.first() else {
return false;
};
let Some(last_seg) = first.line_segs.last() else {
return false;
};
let Some(next) = next else {
return false;
};
let Some(next_seg) = next.line_segs.first() else {
return false;
};
let first_before = styles
.para_styles
.get(first.para_shape_id as usize)
.map(|style| style.spacing_before)
.unwrap_or(0.0);
let next_before = styles
.para_styles
.get(next.para_shape_id as usize)
.map(|style| style.spacing_before)
.unwrap_or(0.0);
let first_after = styles
.para_styles
.get(first.para_shape_id as usize)
.map(|style| style.spacing_after)
.unwrap_or(0.0);
let float_closes_next = matches!(first.controls.as_slice(), [Control::Table(table)]
if paragraph_float_frame_end_hu(first) == Some(i64::from(next_seg.vertical_pos))
&& i64::from(last_seg.vertical_pos) + i64::from(last_seg.line_height)
<= i64::from(table.common.vertical_offset) + i64::from(table.outer_margin_top))
&& !first.stored_text_partition_is_dirty()
&& !next.stored_text_partition_is_dirty()
&& !first.cell_format_vpos_dirty
&& !next.cell_format_vpos_dirty
&& next.controls.is_empty();
let stored_gap_hu = i64::from(next_seg.vertical_pos)
- i64::from(last_seg.vertical_pos)
- i64::from(last_seg.line_height)
- i64::from(last_seg.line_spacing);
let stored_gap = stored_gap_hu as f64 * dpi / crate::renderer::HWPUNIT_PER_INCH;
let complete_margin_step = (stored_gap - first_after - next_before).abs() <= 0.5;
let next_margin_step = first.column_type != crate::model::paragraph::ColumnBreakType::Page
&& (stored_gap - next_before).abs() <= 0.5;
let overlapping_inline_frame = control_only_overlapping_shape_frame(first)
&& !next.stored_text_partition_is_dirty()
&& !next.cell_format_vpos_dirty;
let first_vpos_px = hwpunit_to_px(first_seg.vertical_pos, dpi);
let first_margin_matches = (first_vpos_px - first_before).abs() <= 0.5
|| (overlapping_inline_frame && first_vpos_px > 0.0 && first_vpos_px <= first_before);
(overlapping_inline_frame
|| !first.controls.iter().any(
|control| matches!(control, Control::Shape(shape) if shape.common().treat_as_char),
))
&& !has_paragraph_float_graphic(first)
&& !has_paragraph_float_graphic(next)
&& first_seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& last_seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& next_seg.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& next_before >= 0.0
&& first_before > 0.5
&& first_margin_matches
&& (float_closes_next || complete_margin_step || next_margin_step)
}
pub(crate) fn stored_inline_frame_restarts_page(
previous: &Paragraph,
current: &Paragraph,
next: Option<&Paragraph>,
styles: &ResolvedStyleSet,
dpi: f64,
) -> bool {
use crate::model::paragraph::LineSeg;
let [Control::Table(table)] = previous.controls.as_slice() else {
return false;
};
let [object_line] = previous.line_segs.as_slice() else {
return false;
};
let [first] = current.line_segs.as_slice() else {
return false;
};
let Some(next) = next else {
return false;
};
let Some(successor) = next.line_segs.first() else {
return false;
};
if !table.common.treat_as_char
|| table.common.height == 0
|| object_line.vertical_pos <= 0
|| object_line.line_height <= 0
|| object_line.line_spacing < 0
|| i64::from(object_line.line_height)
!= i64::from(table.common.height)
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom)
|| previous
.text
.chars()
.any(|ch| !ch.is_whitespace() && !ch.is_control() && ch != '\u{FFFC}')
|| first.vertical_pos != 0
|| first.line_height <= 0
|| first.line_spacing < 0
|| [previous, current, next]
.iter()
.any(|p| p.stored_text_partition_is_dirty() || p.cell_format_vpos_dirty)
|| [object_line, first, successor].iter().any(|seg| {
seg.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| seg.tag & LineSeg::TAG_FIRST_SEGMENT == 0
})
|| object_line.segment_width <= 0
|| successor.segment_width <= 0
{
return false;
}
let line_advance = i64::from(first.line_height) + i64::from(first.line_spacing);
let current_advance = match current.controls.as_slice() {
[] if !current.text.trim().is_empty() && first.segment_width > 0 => line_advance,
[Control::Table(table)]
if current
.text
.chars()
.all(|ch| ch.is_whitespace() || ch.is_control() || ch == '\u{fffc}') =>
{
if table.common.treat_as_char {
let frame = i64::from(table.common.height)
+ i64::from(table.outer_margin_top)
+ i64::from(table.outer_margin_bottom);
if first.segment_width <= 0 || i64::from(first.line_height) != frame {
return false;
}
line_advance
} else if let Some(frame) =
paragraph_float_frame_end_hu(current).filter(|_| table.common.width > 0)
{
line_advance.max(frame)
} else {
return false;
}
}
_ => return false,
};
let Some(current_style) = styles.para_styles.get(current.para_shape_id as usize) else {
return false;
};
let Some(next_style) = styles.para_styles.get(next.para_shape_id as usize) else {
return false;
};
let spacing = current_style.spacing_after + next_style.spacing_before;
if !spacing.is_finite() || spacing < 0.0 || !dpi.is_finite() || dpi <= 0.0 {
return false;
}
let expected = current_advance + (spacing * 7200.0 / dpi).round() as i64;
i64::from(successor.vertical_pos) == expected
}