rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
Documentation
//! 옛 호스트 원점과 새 쪽의 뒤 저장 줄이 공유하는 TAC 줄 프레임.

use crate::model::{control::Control, paragraph::LineSeg, paragraph::Paragraph};
use crate::renderer::{hwpunit_to_px, px_to_hwpunit};

use super::InlineBoxPlacement;
use crate::renderer::{pagination::PageItem, style_resolver::ResolvedStyleSet};
use std::collections::HashMap;

/// 이전 쪽 원점을 가진 TAC 줄이 새 쪽에서 앞 간격까지 보존했는지 확인한다.
/// 다음 원본 줄이 앞 간격·개체 소유 줄·뒤 간격을 정수 단위로 정확히 닫을 때만
/// 새 원점을 발행한다. 다음 소유자가 TAC 표여도 그 자체의 저장 줄이 유효해야 한다.
pub(crate) fn restarted_tac_opening_before(
    para: &Paragraph,
    next: &Paragraph,
    spacing_before: f64,
    spacing_after: f64,
    next_spacing_before: f64,
    dpi: f64,
) -> Option<f64> {
    if !dpi.is_finite()
        || dpi <= 0.0
        || !spacing_before.is_finite()
        || spacing_before <= 0.0
        || !spacing_after.is_finite()
        || !next_spacing_before.is_finite()
        || para.line_segs.len() != 1
        || para.cell_format_vpos_dirty
        || next.cell_format_vpos_dirty
        || next.stored_text_partition_is_dirty()
        || next.raw_break_type != 0
        || next.column_type != crate::model::paragraph::ColumnBreakType::None
        || !(next.controls.is_empty()
            || (matches!(next.controls.as_slice(), [Control::Table(_)])
                && crate::renderer::composer::stored_first_tac_line(next).is_some()))
        || !matches!(para.controls.as_slice(), [Control::Table(_)])
    {
        return None;
    }
    let line = crate::renderer::composer::stored_first_tac_line(para)?;
    let after = next.line_segs.first()?;
    if line.line_spacing < 0
        || after.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
        || after.vertical_pos < 0
        || after.vertical_pos >= line.vertical_pos
    {
        return None;
    }
    let before = px_to_hwpunit(spacing_before, dpi);
    let end = before
        .checked_add(line.line_height)?
        .checked_add(line.line_spacing)?
        .checked_add(px_to_hwpunit(spacing_after, dpi))?
        .checked_add(px_to_hwpunit(next_spacing_before, dpi))?;
    (before > 0 && end == after.vertical_pos).then(|| hwpunit_to_px(before, dpi))
}

/// 표의 전체 점유 줄과 후행 간격이 새 로컬 사다리의 첫 줄을 닫는다.
/// 물리 쪽/단의 수용 여부와 원점은 호출자가 별도로 확인한다.
pub(crate) fn restarted_tac_line_advance(
    para: &Paragraph,
    next: &Paragraph,
    spacing_after: f64,
    next_spacing_before: f64,
    dpi: f64,
) -> Option<f64> {
    if !dpi.is_finite()
        || dpi <= 0.0
        || !spacing_after.is_finite()
        || !next_spacing_before.is_finite()
        || para.line_segs.len() != 1
        || para.cell_format_vpos_dirty
        || next.cell_format_vpos_dirty
        || next.stored_text_partition_is_dirty()
        || !next.controls.is_empty()
        || next.raw_break_type != 0
        || next.column_type != crate::model::paragraph::ColumnBreakType::None
    {
        return None;
    }
    let [Control::Table(_)] = para.controls.as_slice() else {
        return None;
    };
    let line = crate::renderer::composer::stored_first_tac_line(para)?;
    let after = next.line_segs.first()?;
    if line.line_spacing < 0
        || after.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
        || after.vertical_pos < 0
        || after.vertical_pos >= line.vertical_pos
    {
        return None;
    }
    let end = line
        .line_height
        .checked_add(line.line_spacing)?
        .checked_add(px_to_hwpunit(spacing_after, dpi))?;
    (end.checked_add(px_to_hwpunit(next_spacing_before, dpi))? == after.vertical_pos)
        .then(|| hwpunit_to_px(end, dpi))
}

/// 수용된 표의 실제 원점·전진 끝도 같은 프레임일 때만 쪽의 0축을 보존한다.
pub(crate) fn accepted_opening_origin(
    para: &Paragraph,
    next: &Paragraph,
    spacing_before: f64,
    spacing_after: f64,
    next_spacing_before: f64,
    placement: &InlineBoxPlacement,
    dpi: f64,
) -> Option<i32> {
    let opening_before = restarted_tac_opening_before(
        para,
        next,
        spacing_before,
        spacing_after,
        next_spacing_before,
        dpi,
    );
    let leading = opening_before.unwrap_or(0.0);
    let end = if opening_before.is_some() {
        let line = crate::renderer::composer::stored_first_tac_line(para)?;
        leading
            + hwpunit_to_px(line.line_height.checked_add(line.line_spacing)?, dpi)
            + spacing_after
    } else {
        restarted_tac_line_advance(para, next, spacing_after, next_spacing_before, dpi)?
    };
    let tolerance = hwpunit_to_px(1, dpi);
    // StoredTacPlan은 바깥여백을 포함한 줄 원점을 발행한다.
    // 테두리 원점의 위 여백은 paint가 소비하므로 여기서 다시 더하지 않는다.
    ((placement.y - leading).abs() <= tolerance
        && (placement.advance_end? - end).abs() <= tolerance)
        .then_some(0)
}

/// 단을 실제로 여는 표 항목과 확정 배치에서 같은 원본 축을 조회한다.
pub(crate) fn accepted_column_origin(
    items: &[PageItem],
    placements: &HashMap<(usize, usize), InlineBoxPlacement>,
    paragraphs: &[Paragraph],
    styles: &ResolvedStyleSet,
    dpi: f64,
) -> Option<(usize, i32)> {
    let PageItem::Table {
        para_index,
        control_index,
    } = items.first()?
    else {
        return None;
    };
    let para = paragraphs.get(*para_index)?;
    let next = paragraphs.get(para_index.checked_add(1)?)?;
    let first_style = styles.para_styles.get(para.para_shape_id as usize)?;
    let before = styles
        .para_styles
        .get(next.para_shape_id as usize)?
        .spacing_before;
    let origin = accepted_opening_origin(
        para,
        next,
        first_style.spacing_before,
        first_style.spacing_after,
        before,
        placements.get(&(*para_index, *control_index))?,
        dpi,
    )?;
    Some((*para_index, origin))
}