rhwp 0.9.0

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
Documentation
//! 선언 최소 셀 높이와 별개로 원본 빈 줄이 여는 쪽 프레임을 확인한다.

use super::{CellUnit, LayoutEngine, ResolvedStyleSet};
use crate::model::{
    paragraph::LineSeg,
    table::{Cell, Table, TablePageBreak},
};
use crate::renderer::hwpunit_to_px;

fn authentic(line: &LineSeg) -> bool {
    line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
        && line.vertical_pos >= 0
        && line.line_height > 0
        && line.line_spacing >= 0
}

impl LayoutEngine {
    /// 순수 재조판 행에서 마지막 빈 줄도 자기 줄 상자와 다음 줄의 간격을 구분한다.
    /// 공통 조판 결과의 줄 높이는 보존하고, 쪽 밖으로 이월하는 뒤 간격만 반환한다.
    pub(super) fn plain_reflow_empty_line_cut_spacing(
        &self,
        table: &Table,
        cell: &Cell,
        unit: &CellUnit,
        styles: &ResolvedStyleSet,
    ) -> f64 {
        if !unit.empty_spacer
            || cell.dirty_flag
            || cell.text_direction != 0
            || unit.vis_end != unit.vis_start + 1
            || !matches!(
                table.common.text_wrap,
                crate::model::shape::TextWrap::TopAndBottom
            )
            || Self::stored_empty_restart_owns_advance(cell, unit)
            || !self.row_has_plain_reflow_line_units(table, usize::from(cell.row), styles)
        {
            return 0.0;
        }
        let Some(paragraph) = cell.paragraphs.get(unit.para_idx) else {
            return 0.0;
        };
        if !paragraph.text.is_empty()
            || !paragraph.controls.is_empty()
            || paragraph.stored_text_partition_is_dirty()
            || paragraph.cell_format_vpos_dirty
        {
            return 0.0;
        }
        if crate::renderer::para_has_no_stored_line_segs(paragraph) {
            // 빈 문단에는 composer의 실제 글줄이 없을 수 있다. 유닛 생산자가
            // 측정과 같은 메트릭에서 분리한 뒤 간격을 우선 소비한다.
            if unit.following_line_advance > 0.0 {
                return unit.following_line_advance.min(unit.height.max(0.0));
            }
            // 빈 문단의 합성 줄도 원본 저장 줄로 오인하지 않는다. 비어 있지 않은
            // 재조판 문단과 같은 측정 결과에서 줄 상자와 뒤 간격을 구분한다.
            let (left, right, _, _) = self.resolve_cell_padding(cell, table);
            let width =
                hwpunit_to_px(cell.width as i32, self.dpi) * self.render_table_width_scale(table);
            let inner_width =
                crate::renderer::composer::cell_inner_text_width(width, left, right, self.dpi);
            let composed = self.compose_cell_unit_paragraph(
                paragraph,
                cell.text_direction,
                inner_width,
                styles,
            );
            return composed.lines.get(unit.vis_start).map_or(0.0, |line| {
                hwpunit_to_px(line.line_spacing.max(0), self.dpi)
                    .min((unit.height - hwpunit_to_px(line.line_height, self.dpi)).max(0.0))
            });
        }
        let Some(line) = paragraph
            .line_segs
            .get(unit.vis_start)
            .filter(|line| authentic(line))
        else {
            return 0.0;
        };
        hwpunit_to_px(line.line_spacing, self.dpi)
            .min((unit.height - hwpunit_to_px(line.line_height, self.dpi)).max(0.0))
    }

    /// 첫 제목과 다음 원본 인라인 상자가 서로 다른 쪽 원점0을 소유한다.
    /// 뒤 상자의 저장 높이와 다음 줄 전진이 정확할 때만 제목 조각의
    /// 최소 물리 공간을 보존한다. 문단마다 임의로0인 로컬 좌표는 제외한다.
    pub(crate) fn stored_heading_before_inline_frame_cut(
        &self,
        table: &Table,
        row: usize,
        start: &[usize],
        end: &[usize],
        styles: &ResolvedStyleSet,
    ) -> bool {
        use crate::model::control::Control;
        let profile = self.profile.get();
        if !profile.hwp5_stored_pagination_layout()
            || profile.session_edited()
            || !start.is_empty()
            || end != [1]
            || table.common.treat_as_char
            || table.layout_page_break() != TablePageBreak::RowBreak
            || table.row_count <= 1
        {
            return false;
        }
        let owners = self.indexed_row_cut_cell_order(table, row);
        let [index] = owners.as_slice() else {
            return false;
        };
        let cell = &table.cells[*index];
        if self
            .render_normalization
            .borrow()
            .cell_frame_reflowed(table, cell)
            || usize::from(cell.row) != row
            || cell.row_span != 1
            || cell.col_span != table.col_count
            || cell.dirty_flag
            || cell.paragraphs.len() < 3
        {
            return false;
        }
        let (heading, host, successor) = (
            &cell.paragraphs[0],
            &cell.paragraphs[1],
            &cell.paragraphs[2],
        );
        if [heading, host, successor].iter().any(|paragraph| {
            paragraph.stored_text_partition_is_dirty() || paragraph.cell_format_vpos_dirty
        }) || heading.text.trim().is_empty()
            || !heading.controls.is_empty()
            || !host.text.trim().is_empty()
            || !successor.controls.is_empty()
        {
            return false;
        }
        let ([heading_line], [host_line], Some(after)) = (
            heading.line_segs.as_slice(),
            host.line_segs.as_slice(),
            successor.line_segs.first(),
        ) else {
            return false;
        };
        let [Control::Table(child)] = host.controls.as_slice() else {
            return false;
        };
        if ![heading_line, host_line, after]
            .iter()
            .all(|line| authentic(line))
            || heading_line.vertical_pos != 0
            || host_line.vertical_pos != 0
            || !child.common.treat_as_char
            || child.caption.is_some()
            || i64::from(host_line.line_height)
                != i64::from(child.common.height)
                    + i64::from(child.outer_margin_top)
                    + i64::from(child.outer_margin_bottom)
            || i64::from(after.vertical_pos)
                != i64::from(host_line.line_height) + i64::from(host_line.line_spacing)
        {
            return false;
        }
        let units = self.cell_units(cell, table, styles);
        units
            .first()
            .is_some_and(|unit| unit.para_idx == 0 && unit.vis_start == 0 && unit.vis_end == 1)
            && units
                .get(1)
                .is_some_and(|unit| unit.para_idx == 1 && unit.page_frame_reset_before)
    }

    /// 다음 저장 줄이 빈 재시작 줄의 전체 전진량을 소비한다.
    /// 같은 셀의 원본 되감김 유닛만 허용하며 로컬 빈 장식 줄에는 적용하지 않는다.
    pub(super) fn stored_empty_restart_owns_advance(cell: &Cell, unit: &CellUnit) -> bool {
        if cell.dirty_flag
            || !unit.empty_spacer
            || !unit.page_frame_reset_before
            || !unit.stored_frame_break_before
            || unit.vis_start != 0
            || unit.vis_end != 1
        {
            return false;
        }
        let Some(paragraph) = cell.paragraphs.get(unit.para_idx) else {
            return false;
        };
        let Some(next) = cell.paragraphs.get(unit.para_idx + 1) else {
            return false;
        };
        if [paragraph, next].iter().any(|paragraph| {
            paragraph.stored_text_partition_is_dirty()
                || paragraph.cell_format_vpos_dirty
                || !paragraph.controls.is_empty()
        }) {
            return false;
        }
        let [line] = paragraph.line_segs.as_slice() else {
            return false;
        };
        next.line_segs.first().is_some_and(|following| {
            authentic(line)
                && authentic(following)
                && line.vertical_pos == 0
                && i64::from(following.vertical_pos)
                    == i64::from(line.line_height) + i64::from(line.line_spacing)
        })
    }

    /// 원본 프레임의 유닛 합이 저장된 마지막 전진 끝을 정확히 닫는지 확인한다.
    /// 셀 선언값은 최소 상자이지 여러 쪽에 걸친 내용의 물리 높이 상한이 아니다.
    pub(super) fn stored_empty_restart_closes_source_flow(
        &self,
        table: &Table,
        cell: &Cell,
        units: &[CellUnit],
        end: usize,
        styles: &ResolvedStyleSet,
    ) -> bool {
        let profile = self.profile.get();
        if !profile.hwp5_stored_pagination_layout()
            || profile.session_edited()
            || self
                .render_normalization
                .borrow()
                .cell_frame_reflowed(table, cell)
            || table.common.treat_as_char
            || table.layout_page_break() != TablePageBreak::RowBreak
            || cell.row_span != 1
            || cell.col != 0
            || cell.col_span != table.col_count
            || cell.paragraphs.iter().any(|paragraph| {
                paragraph.stored_text_partition_is_dirty()
                    || paragraph.cell_format_vpos_dirty
                    || paragraph.line_segs.iter().any(|line| !authentic(line))
            })
        {
            return false;
        }
        let Some(next) = units.get(end) else {
            return false;
        };
        let Some(previous) = end.checked_sub(1).and_then(|index| units.get(index)) else {
            return false;
        };
        if !Self::stored_empty_restart_owns_advance(cell, next)
            || previous.para_idx + 1 != next.para_idx
            || previous.vis_start + 1 != previous.vis_end
        {
            return false;
        }
        let paragraph = &cell.paragraphs[previous.para_idx];
        let Some(line) = paragraph.line_segs.get(previous.vis_start) else {
            return false;
        };
        if !paragraph.controls.is_empty()
            || previous.vis_end != paragraph.line_segs.len()
            || line.vertical_pos <= 0
        {
            return false;
        }
        let start = units[..end]
            .iter()
            .rposition(|unit| unit.page_frame_reset_before)
            .unwrap_or(0);
        let first = &units[start];
        if !cell.paragraphs[first.para_idx]
            .line_segs
            .get(first.vis_start)
            .is_some_and(|line| line.vertical_pos == 0)
        {
            return false;
        }
        let source_end = i64::from(line.vertical_pos)
            + i64::from(line.line_height)
            + i64::from(line.line_spacing);
        let Ok(source_end) = i32::try_from(source_end) else {
            return false;
        };
        let after = styles
            .para_styles
            .get(paragraph.para_shape_id as usize)
            .map_or(0.0, |style| style.spacing_after);
        let occupied = units[start..end]
            .iter()
            .map(|unit| unit.height)
            .sum::<f64>();
        (occupied - hwpunit_to_px(source_end, self.dpi) - after).abs() <= hwpunit_to_px(2, self.dpi)
    }
}