rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
Documentation
//! 재조판하는 인라인 호스트의 개체 슬롯을 프레임 입력으로 투영한다.
//! 원본 텍스트·제어문은 바꾸지 않고 원시 UTF-16 경계를 그대로 발행한다.

use super::*;

/// 글앞·글뒤 개체의 본체는 종이층 소유지만 원시 앵커는 문단 줄에 속한다.
/// 폭·높이는 개체에서 가져오지 않고 그 줄의 글자 상자를 유지한다.
/// 배제 밴드를 만드는 어울림·자리차지 개체는 각 흐름 소유자에 남긴다.
pub(super) fn is_nonflow_anchor(control: &Control) -> bool {
    use crate::model::shape::TextWrap;
    let common = match control {
        Control::Table(table) => &table.common,
        Control::Picture(picture) => &picture.common,
        Control::Shape(shape) => shape.common(),
        _ => return false,
    };
    !common.treat_as_char
        && matches!(
            common.text_wrap,
            TextWrap::InFrontOfText | TextWrap::BehindText
        )
}

pub(super) fn project(
    source: &Paragraph,
    styles: &ResolvedStyleSet,
    dpi: f64,
) -> Option<(Paragraph, Vec<FlowInlineControl>)> {
    match project_with_storage(source, styles, dpi, &crate::storage::Unbounded) {
        Ok(value) => value,
        Err(never) => match never {},
    }
}

pub(crate) fn project_with_storage<S: crate::storage::Storage>(
    source: &Paragraph,
    styles: &ResolvedStyleSet,
    dpi: f64,
    storage: &S,
) -> Result<Option<(Paragraph, Vec<FlowInlineControl>)>, S::Error> {
    Ok({
        let chars: Vec<char> = storage.collect(source.text.chars())?;
        let mut slots = storage.vec(0)?;
        for ((control, position), raw) in source
            .controls
            .iter()
            .zip(source.try_control_text_positions(storage)?)
            .zip(source.control_utf16_positions_with_storage(storage)?)
        {
            let (width, height) = match control {
                // 머리말/꼬리말 내용은 별도 프레임이 그리지만 본문에 있는
                // 8-unit 기준 문자는 폭 0인 줄 토큰이다. 넘친 TAC 개체 뒤의
                // 기준 문자를 지우면 한/글이 남기는 다음 줄까지 사라진다.
                Control::Header(_) | Control::Footer(_) => (0, 0),
                _ => {
                    let Some(size) = inline_control_occupied_size_hwp_with_storage(
                        control, styles, dpi, storage,
                    )?
                    else {
                        continue;
                    };
                    size
                }
            };
            let position = if chars.is_empty() && source.char_offsets.is_empty() {
                0
            } else {
                position
            };
            if position <= chars.len() {
                storage.push(&mut slots, (control, position, raw, width, height))?;
            }
        }
        if slots.is_empty() {
            return Ok(None);
        }
        let mut input = storage.clone_paragraph(source)?;
        input.text.clear();
        input.char_offsets.clear();
        input.layout_field_values.clear();
        let mut controls = storage.vec(slots.len())?;
        let mut scalar_index = 0;
        for position in 0..=chars.len() {
            let mut represented = false;
            for &(control, owner, raw, width, height) in &slots {
                if owner != position {
                    continue;
                }
                storage.push_char(&mut input.text, '\u{FFFC}')?;
                storage.push(&mut input.char_offsets, raw)?;
                storage.push(
                    &mut controls,
                    FlowInlineControl {
                        char_position: scalar_index,
                        utf16_position: raw,
                        width_hwp: width,
                        height_hwp: height,
                        baseline_distance_hwp: flow_inline_baseline_hwp(control, height),
                        spacing_height_hwp: inline_control_spacing_height_hwp(control),
                    },
                )?;
                scalar_index += 1;
                represented |= chars.get(position) == Some(&'\u{FFFC}')
                    && source.char_offsets.get(position) == Some(&raw);
            }
            if let Some(&character) = chars.get(position).filter(|_| !represented) {
                if let Some((_, value)) = source
                    .layout_field_values
                    .iter()
                    .find(|(at, _)| *at == position)
                {
                    storage.push(
                        &mut input.layout_field_values,
                        (scalar_index, storage.string(value)?),
                    )?;
                }
                storage.push_char(&mut input.text, character)?;
                storage.push(
                    &mut input.char_offsets,
                    char_index_to_utf16_offset(source, position),
                )?;
                scalar_index += 1;
            }
        }
        Some((input, controls))
    })
}