rhwp 0.9.0

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
Documentation
//! [#7203] 빈 host 자리차지(TopAndBottom) 표의 **저장 앵커 원점**을 한 곳에서 정한다.
//!
//! 종전에는 같은 이름의 게이트가 `typeset.rs` 와 `layout.rs` 에 각각 있었고 산식이 갈렸다.
//!
//! ```text
//!   layout.rs   need = 높이 + 아래여백 − 위여백 ,  윗변 = vpos − 위여백
//!   typeset.rs  need = 높이 + 위여백 + 아래여백 ,  윗변 = vpos
//! ```
//!
//! 대칭 여백(283/283)이면 need 가 `2 × 위여백` 만큼, 윗변이 `위여백` 만큼 갈린다. 그래서
//! 같은 표를 두고 **조판은 저장 앵커를 거부하고 렌더는 수용**하는 구간이 생기고, 둘 다
//! 수용해도 원점이 3.77px 어긋난다. `hwpctl_API_v2.4.hwp` 의 어긋난 자리차지 표는
//! 예외 없이 이 구간에 있었다(사다리 간격이 `높이 + 66 HU`).
//!
//! ## 정본이 정하는 원점
//!
//! `pdf/hwpctl_API_v2.4-hwp-2020.pdf` 의 가로 괘선과 원본 저장 사다리를 대조하면
//!
//! ```text
//!   정본 윗변 = 본문 상단 + (앵커 vpos − 위 바깥여백)      잔차 +0.26 .. +0.74px (9건)
//! ```
//!
//! 즉 저장 `vpos` 는 표 상자의 상단이 아니라 **위 바깥여백 뒤**를 가리킨다. 그러면 앵커
//! 아래로 필요한 공간은 `높이 + 아래여백 − 위여백` 이다. 이 모듈이 그 한 규칙을 갖고,
//! 조판·렌더가 같은 값을 소비한다.

use crate::model::paragraph::{LineSeg, Paragraph};
use crate::model::table::Table;
use crate::renderer::hwpunit_to_px;

/// 구현이 합성한 줄은 저장 사다리의 증거가 아니다.
fn stored_seg(para: &Paragraph) -> Option<&LineSeg> {
    para.line_segs
        .iter()
        .find(|seg| seg.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0)
}

fn stored_vpos(para: &Paragraph) -> Option<i64> {
    stored_seg(para).map(|seg| i64::from(seg.vertical_pos))
}

/// A successor on a new saved page does not authenticate spacing between two
/// stacked empty anchors. The preceding table's split policy does not change
/// which frame owns that successor. Both pagination and paint use this query.
pub(crate) fn stored_anchor_successor_rewinds(para: &Paragraph, next: &Paragraph) -> bool {
    !para.stored_text_partition_is_dirty()
        && !para.cell_format_vpos_dirty
        && !next.stored_text_partition_is_dirty()
        && !next.cell_format_vpos_dirty
        && para.line_segs.len() == 1
        && next.line_segs.len() == 1
        && [&para.line_segs[0], &next.line_segs[0]].iter().all(|line| {
            line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
                && line.vertical_pos >= 0
                && line.line_height > 0
        })
        && next.line_segs[0].vertical_pos < para.line_segs[0].vertical_pos
}

/// 저장 host 기준의 표 윗변 offset 과 흐름 점유 끝(HWPUNIT).
///
/// 저장 사다리가 무엇을 쟀는지에 따라 `vpos` 의 뜻이 갈린다.
///
/// - 다음 앵커가 `높이 + 양쪽 여백`만큼 **정확히** 전진하면 `vpos` 는 **바깥 여백 상자의
///   위끝**이다. 표 자신의 윗변은 거기서 `outMargin.top` 만큼 아래고, 점유 끝은
///   `vpos + 상자 높이` 그대로다(`om_top + 높이 + om_bottom` 과 같다).
/// - 그 밖의 수용된 저장 앵커는 위여백 **뒤**를 가리키므로 윗변이 `vpos − om_top` 이다.
///
/// [#7203] 앞 갈래는 종전에 offset 0 이었다 — paint 와 예약을 맞추려는 값이었지 기하가
/// 아니었다. `pdf/hwpctl_API_v2.4-hwp-2020.pdf` 전수 대조가 두 갈래를 갈라 준다.
///
/// ```text
///   사다리 advance == 높이+위+아래  (18건)  정본 − rhwp = +3.41px  ← 위여백 한 개만큼 위였다
///   사다리 advance == 높이 + 66HU   (16건)  정본 − rhwp = +0.55px  ← 이미 맞다
/// ```
///
/// 두 코호트는 같은 문서·같은 선언 여백(283HU)이고 사다리 간격만 다르다. 점유 끝은
/// 양쪽 모두 바뀌지 않으므로 예약 높이는 그대로다.
pub(crate) fn stored_topbottom_object_span(
    para: &Paragraph,
    next_para: Option<&Paragraph>,
    table: &Table,
    hwpx_object_frame: bool,
) -> (i64, i64) {
    if let Some(outer_box_height) = stored_topbottom_flow_advance_hu(para, next_para, table) {
        // 닫힌 세로 바깥 상자의 위여백은 좌우 여백과 별개다.
        // 위·아래가 0인 구분선 표도 같은 값 0을 소비한다.
        let top = stored_outer_box_table_top_hu(table);
        let offset = if offset_float_follows_inline_line(para, table) {
            i64::from(table.common.vertical_offset)
        } else {
            0
        };
        (top + offset, outer_box_height)
    } else {
        let top = -i64::from(table.outer_margin_top);
        (
            top,
            top + i64::from(table.common.height) + i64::from(table.outer_margin_bottom),
        )
    }
}

/// 다음 저장 줄로 인증된 바깥 상자 안에서 표 본체가 시작하는 위치.
/// 세로 원점은 원본 위여백을 소비하며, 좌우 여백의 대칭 여부로 바꾸지 않는다.
pub(crate) fn stored_outer_box_table_top_hu(table: &Table) -> i64 {
    i64::from(table.outer_margin_top)
}

/// 저장 사다리가 증명한 전체 흐름 상자의 advance. 단순 테두리 원점은
/// 이 높이를 증명하지 않으므로 None을 반환한다. 조판 원점 선택과 최종 flow 소비가
/// 같은 근거를 사용하며, paint 좌표 차이를 예약 높이로 오인하지 않는다.
pub(crate) fn stored_topbottom_flow_advance_hu(
    para: &Paragraph,
    next_para: Option<&Paragraph>,
    table: &Table,
) -> Option<i64> {
    let outer_box_height = i64::from(table.common.height)
        + i64::from(table.outer_margin_top)
        + i64::from(table.outer_margin_bottom);
    let offset = if offset_float_follows_inline_line(para, table) {
        i64::from(table.common.vertical_offset)
    } else {
        0
    };
    let occupied_height = outer_box_height + offset;
    let stored_outer_box = stored_vpos(para)
        .zip(next_para.and_then(stored_vpos))
        .is_some_and(|(current, next)| next - current == occupied_height);
    stored_outer_box.then_some(occupied_height)
}

/// 같은 저장 줄의 인라인 표가 양수 offset float 앞 공간에 들어가는가.
/// 실제 소유 줄과 바깥 상자를 대조하며 다른 가시 개체는 추정하지 않는다.
fn offset_float_follows_inline_line(para: &Paragraph, table: &Table) -> bool {
    use crate::model::control::Control;
    let offset = table.common.vertical_offset;
    offset > 0
        && offset <= i32::MAX as u32
        && para.controls.len() > 1
        && para.controls.iter().enumerate().all(|(ci, control)| {
            let Control::Table(sibling) = control else {
                return false;
            };
            if std::ptr::eq(sibling.as_ref(), table) {
                return true;
            }
            sibling.common.treat_as_char
                && sibling.caption.is_none()
                && crate::renderer::layout::control_line_seg_index(para, ci)
                    .and_then(|owner| para.line_segs.get(owner))
                    .is_some_and(|line| {
                        line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
                            && i64::from(line.line_height)
                                == i64::from(sibling.common.height)
                                    + i64::from(sibling.outer_margin_top)
                                    + i64::from(sibling.outer_margin_bottom)
                            && line.line_height > 0
                            && line.line_height as u32 <= offset
                    })
        })
}

/// 원본 저장 사다리가 빈 호스트 표의 전체 바깥 상자를 증명하는가.
/// 셀 수 대신 실제 점유 높이와 다음 문단 원점을 대조한다. 내용이 선언보다
/// 커졌거나 편집된 문단은 저장 상자로 강제하지 않고 재조판 경로에 남긴다.
pub(crate) fn stored_empty_topbottom_outer_box_is_valid(
    para: &Paragraph,
    next_para: Option<&Paragraph>,
    table: &Table,
    measured_height: Option<f64>,
    dpi: f64,
) -> bool {
    use crate::model::control::Control;
    !para
        .text
        .chars()
        .any(|c| !c.is_whitespace() && c > '\u{001F}' && c != '\u{FFFC}')
        && !para.stored_text_partition_is_dirty()
        && next_para.is_some_and(|next| !next.stored_text_partition_is_dirty())
        && crate::renderer::float_placement::is_para_topbottom_float(&table.common)
        && (table.common.vertical_offset == 0 || offset_float_follows_inline_line(para, table))
        && table.common.height > 0
        && table.common.height <= i32::MAX as u32
        && (para
            .controls
            .iter()
            .filter(|c| matches!(c, Control::Table(_)))
            .count()
            == 1
            || offset_float_follows_inline_line(para, table))
        && stored_topbottom_flow_advance_hu(para, next_para, table).is_some()
        && measured_height
            .is_some_and(|height| height <= hwpunit_to_px(table.common.height as i32, dpi) + 0.5)
}

/// 다음 저장 `vpos` 사다리가 이 개체가 점유할 높이를 실제로 비우는가 [#3925].
///
/// 비우지 않는 host 의 raw `vpos` 를 물리 anchor 로 읽으면 개체가 사다리보다 훨씬 아래에
/// 놓여 쪽 소비가 부풀고 뒤 문단이 다음 쪽으로 밀린다 — `36324768_결재문서본문.hwpx`
/// pi=11 은 host `lh` 가 14.7px 인데 표는 253.1px 라 쪽 소비가 187px 늘어 2→3쪽이 됐다.
/// host 줄 높이만으로는 #3738 표적을 악화시키므로 다음 문단과의 `vpos` 간격만 증거로 쓴다.
pub(crate) fn stored_ladder_leaves_object_room(
    para: &Paragraph,
    next_para: Option<&Paragraph>,
    table: &Table,
) -> bool {
    let (_, occupied_bottom) = stored_topbottom_object_span(para, next_para, table, false);
    let need = occupied_bottom.max(0);
    match (stored_vpos(para), next_para.and_then(stored_vpos)) {
        (Some(current), Some(next)) => next - current >= need,
        _ => false,
    }
}

/// 저장 앵커가 가리키는 표 상자 윗변 — 단 상단 기준 px. 쓸 수 없으면 `None`.
///
/// `available_px` 는 단(칸) 높이다. 반환값은 그 단 상단으로부터의 offset 이라
/// 조판(상대 lane)과 렌더(절대 lane)가 각자의 기준점만 더하면 된다.
pub(crate) fn stored_single_topbottom_top_px(
    para: &Paragraph,
    next_para: Option<&Paragraph>,
    table: &Table,
    available_px: f64,
    dpi: f64,
    hwpx_object_frame: bool,
) -> Option<f64> {
    // 문단 기준 세로 오프셋이 걸린 개체는 상자 윗변이 `앵커 + 오프셋` 이라 이 산식이
    // 성립하지 않는다. 오프셋을 더해 읽을 근거(정본 실측)가 없으므로 흐름 배치에 맡긴다.
    // `rowbreak-problem-pages.hwp` 구역1 의 `vertical_offset` 152·3019 HU 표가 이 갈래다 —
    // 앵커를 그대로 믿으면 본문이 쪽 밖 480px 까지 밀린다.
    if table.common.vertical_offset != 0 {
        return None;
    }
    let current = stored_vpos(para)?;
    let next = next_para.and_then(stored_vpos)?;
    // vpos 가 다음 문단에서 되감기면 이 표는 다음 물리 쪽의 첫 anchor 일 수 있다.
    // raw vpos 를 현재 쪽 좌표로 강제하면 p14 그림 8처럼 상단 그림이 하단에 떨어진다.
    if next <= current {
        return None;
    }
    if !stored_ladder_leaves_object_room(para, next_para, table) {
        return None;
    }
    let (top_offset, _) = stored_topbottom_object_span(para, next_para, table, hwpx_object_frame);
    let top = hwpunit_to_px((current + top_offset) as i32, dpi);
    let bottom = top + hwpunit_to_px(table.common.height as i32, dpi);
    // 본문 안에 온전히 드는 앵커만 물리 좌표로 읽는다.
    (top >= -0.5 && bottom <= available_px + 0.5).then_some(top)
}

/// 단일 셀의 저장 높이는 내용 뒤의 빈 밴드까지 소유할 수 있다.
/// 첫 개체 높이 뒤의 잔여 셀 높이와 양끝 바깥 여백이 다음 쪽 첫 줄 위치와
/// 같으면, 작은 개체 높이는 전체 표가 아니라 첫 조각의 높이다.
/// 저장 문자 줄이 첫 조각에 모두 들어가는 경우만 빈 꼬리 밴드를 이어받는다.
pub(crate) fn stored_single_cell_first_frame_height_hu(
    para: &Paragraph,
    next_para: Option<&Paragraph>,
    table: &Table,
) -> Option<i32> {
    use crate::model::paragraph::LineSeg;
    use crate::model::table::TablePageBreak;
    if para.stored_text_partition_is_dirty()
        || !para.text.trim().is_empty()
        || para.controls.len() != 1
        || para.line_segs.len() != 1
        || para.line_segs[0].segment_width != 0
        || table.common.treat_as_char
        || !crate::renderer::float_placement::is_para_topbottom_float(&table.common)
        || table.common.vertical_offset != 0
        || table.caption.is_some()
        || table.row_count != 1
        || table.col_count != 1
        || table.cells.len() != 1
        || table.layout_page_break() != TablePageBreak::RowBreak
        || table.common.height == 0
        || table.common.height > i32::MAX as u32
    {
        return None;
    }
    let cell = &table.cells[0];
    let next = next_para?;
    let host_y = stored_vpos(para)?;
    let next_y = stored_vpos(next)?;
    if next.stored_text_partition_is_dirty()
        || cell.row != 0
        || cell.col != 0
        || cell.row_span != 1
        || cell.col_span != 1
        || cell.height <= table.common.height
        || host_y <= 0
        || next_y <= 0
        || next_y >= host_y
        || cell.paragraphs.is_empty()
    {
        return None;
    }
    let tail = i64::from(cell.height) - i64::from(table.common.height);
    let continuation_end =
        tail + i64::from(table.outer_margin_top) + i64::from(table.outer_margin_bottom);
    if continuation_end != next_y {
        return None;
    }
    let padding = cell.effective_padding(&table.padding);
    for paragraph in &cell.paragraphs {
        if paragraph.stored_text_partition_is_dirty()
            || !paragraph.controls.is_empty()
            || paragraph.line_segs.is_empty()
            || paragraph.line_segs.iter().any(|line| {
                line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
                    || line.vertical_pos < 0
                    || i64::from(line.vertical_pos)
                        + i64::from(line.line_height)
                        + i64::from(padding.top)
                        + i64::from(padding.bottom)
                        > i64::from(table.common.height)
            })
        {
            return None;
        }
    }
    Some(table.common.height as i32)
}