rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! 다음 저장 줄이 닫는 빈 호스트 표의 프레임을 측정·배치에서 공유한다.

use crate::model::{
    control::Control,
    paragraph::{ColumnBreakType, LineSeg, Paragraph},
    shape::{CommonObjAttr, HorzRelTo, TextWrap, VertAlign, VertRelTo},
    table::Table,
};
use crate::renderer::{hwpunit_to_px, px_to_hwpunit};

use super::{is_para_topbottom_float, signed_hwpunit, ParagraphFloatFlow, ParagraphFloatPlacement};

/// 종이·쪽에 고정된 장식은 보이더라도 호스트의 글줄 흐름을 밀지 않는다.
fn detached_overlay(common: &CommonObjAttr) -> bool {
    !common.treat_as_char
        && !common.flow_with_text
        && !common.affect_line_spacing
        && matches!(
            common.text_wrap,
            TextWrap::InFrontOfText | TextWrap::BehindText
        )
        && matches!(common.vert_rel_to, VertRelTo::Paper | VertRelTo::Page)
        && matches!(common.horz_rel_to, HorzRelTo::Paper | HorzRelTo::Page)
}

fn single_flow_table(para: &Paragraph) -> bool {
    para.controls
        .iter()
        .filter(|c| matches!(c, Control::Table(_)))
        .count()
        == 1
        && para.controls.iter().all(|control| match control {
            Control::Table(_) => true,
            Control::Shape(shape) => detached_overlay(shape.common()),
            Control::Picture(picture) => detached_overlay(&picture.common),
            _ => false,
        })
}

/// 표 아래 캡션이 바깥 상자에 더하는 세로 높이(HWPUNIT). 캡션이 없으면 0 이다.
///
/// 한/글은 아래 캡션을 표 본체와 바깥 아래 여백 사이에 `캡션 간격 + 캡션 줄` 로 둔다
/// (한글문서파일형식 39쪽 표 235: 저장 델타 10545 = 위 566 + 표 6193 + 간격 852 + 줄 950 +
/// 아래 1984). 저장 줄이 모두 한컴이 쓴 줄일 때만 그 높이를 읽는다. 위·왼쪽·오른쪽 캡션은
/// 표 본체 원점이나 폭을 바꾸는 다른 기하라 이 상자로 대체하지 않는다.
pub(crate) fn stored_bottom_caption_hu(table: &Table) -> Option<i64> {
    if table
        .caption
        .as_ref()
        .is_some_and(|caption| caption.direction != crate::model::shape::CaptionDirection::Bottom)
    {
        return None;
    }
    stored_vertical_caption_hu(table)
}

/// 위·아래 캡션이 원본 외곽 줄 상자에 포함한 높이와 본체 사이 간격.
/// 본체 원점의 방향은 caller가 유지하며 좌우 캡션으로 넓히지 않는다.
pub(crate) fn stored_vertical_caption_hu(table: &Table) -> Option<i64> {
    let Some(caption) = table.caption.as_ref() else {
        return Some(0);
    };
    if !matches!(
        caption.direction,
        crate::model::shape::CaptionDirection::Top | crate::model::shape::CaptionDirection::Bottom
    ) || caption.paragraphs.is_empty()
    {
        return None;
    }
    let mut height = 0i64;
    for para in &caption.paragraphs {
        let (first, last) = (para.line_segs.first()?, para.line_segs.last()?);
        if para.stored_text_partition_is_dirty()
            || para.cell_format_vpos_dirty
            || para
                .line_segs
                .iter()
                .any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
        {
            return None;
        }
        height = height.max(
            i64::from(last.vertical_pos) + i64::from(last.line_height)
                - i64::from(first.vertical_pos.min(0)),
        );
    }
    Some(i64::from(caption.spacing) + height)
}

fn stored_line(para: &Paragraph) -> Option<i32> {
    let line = para.line_segs.first()?;
    (line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
        && !para.stored_text_partition_is_dirty()
        && !para.cell_format_vpos_dirty)
        .then_some(line.vertical_pos)
}

/// The first coanchored table can precede its own empty host line. A saved
/// line immediately below its complete outer box authenticates that leading
/// space; fitting only the table would leave it on the preceding page.
pub(crate) fn coanchored_host_restarts_page(
    previous: &Paragraph,
    host: &Paragraph,
    body_height: i32,
) -> bool {
    if !super::is_textless_coanchored_float_host(host)
        || host.column_type != ColumnBreakType::None
        || host.line_segs.len() != 1
    {
        return false;
    }
    let Some(table) = host.controls.iter().find_map(|control| match control {
        Control::Table(table) if is_para_topbottom_float(&table.common) => Some(table.as_ref()),
        _ => None,
    }) else {
        return false;
    };
    let Some(caption) = stored_bottom_caption_hu(table) else {
        return false;
    };
    let leading = i64::from(table.outer_margin_top)
        + i64::from(table.common.height)
        + caption
        + i64::from(table.outer_margin_bottom);
    let (Some(host_vpos), Some(_)) = (stored_line(host), stored_line(previous)) else {
        return false;
    };
    let line = &host.line_segs[0];
    let Some(last) = previous.line_segs.last() else {
        return false;
    };
    if previous.line_segs.iter().any(|line| {
        line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
            || line.line_height <= 0
            || line.line_spacing < 0
    }) || i64::from(host_vpos) != leading
    {
        return false;
    }
    crate::renderer::stored_frame_boundary::saved_positive_frame_reset(
        Some((
            last.vertical_pos,
            last.vertical_pos
                .saturating_add(last.line_height)
                .saturating_add(last.line_spacing),
        )),
        line,
        body_height,
    )
}

impl ParagraphFloatPlacement {
    /// 표 이어받기 옆의 빈 저장 줄이 새 쪽의 앵커 축을 닫는다.
    /// 이전 쪽의 마지막 줄간격은 새 쪽 호스트의 원점에 재가산하지 않는다.
    pub(crate) fn from_stored_overlay_guide_chain(
        paragraphs: &[Paragraph],
        anchor: usize,
        host: usize,
        flow: f64,
        body_height: f64,
        styles: &crate::renderer::style_resolver::ResolvedStyleSet,
        dpi: f64,
    ) -> Option<Self> {
        let source = paragraphs.get(anchor)?;
        let para = paragraphs.get(host)?;
        let [Control::Table(table)] = para.controls.as_slice() else {
            return None;
        };
        let [Control::Table(owner)] = source.controls.as_slice() else {
            return None;
        };
        let overlay = |table: &Table| {
            !table.common.treat_as_char
                && matches!(
                    table.common.text_wrap,
                    TextWrap::InFrontOfText | TextWrap::BehindText
                )
                && table.common.vert_rel_to == VertRelTo::Para
                && table.common.vert_align == VertAlign::Top
        };
        if anchor >= host
            || !overlay(table)
            || !overlay(owner)
            || !source.text.is_empty()
            || source.line_segs.len() != 1
            || !para.text.is_empty()
            || para.line_segs.len() != 1
            || !flow.is_finite()
            || !body_height.is_finite()
        {
            return None;
        }
        stored_line(source)?;
        let mut previous = source.line_segs.first()?;
        if previous.line_height <= 0 || previous.line_spacing < 0 {
            return None;
        }
        let body = px_to_hwpunit(body_height, dpi);
        let mut reset = false;
        for index in anchor + 1..=host {
            let paragraph = paragraphs.get(index)?;
            if !paragraph.text.is_empty()
                || paragraph.line_segs.len() != 1
                || paragraph.column_type != ColumnBreakType::None
                || (index < host && !paragraph.controls.is_empty())
            {
                return None;
            }
            stored_line(paragraph)?;
            let style = styles.para_styles.get(paragraph.para_shape_id as usize)?;
            // 줄 상자와 후행 간격으로 닫힌 축이다. 별도 문단 간격을
            // 같은 높이로 추정하거나 새 호스트의 앞 간격에 중복 반영하지 않는다.
            if style.spacing_before != 0.0 || style.spacing_after != 0.0 {
                return None;
            }
            let line = &paragraph.line_segs[0];
            if line.line_height <= 0 || line.line_spacing < 0 {
                return None;
            }
            let ink_end = previous.vertical_pos.checked_add(previous.line_height)?;
            let end = ink_end.checked_add(previous.line_spacing)?;
            if line.vertical_pos != end {
                // 줄의 내용은 앞 쪽에 들어가고 후행 간격만 쪽 끝에서 닫힌다.
                if reset || line.vertical_pos != 0 || ink_end > body || end <= body {
                    return None;
                }
                reset = true;
            }
            previous = line;
        }
        let origin = hwpunit_to_px(previous.vertical_pos, dpi);
        if !reset
            || origin < flow
            || previous.vertical_pos.checked_add(previous.line_height)? > body
        {
            return None;
        }
        Some(Self {
            flow: ParagraphFloatFlow::NextLine,
            anchor_y: origin,
            stored_host_origin: Some(origin),
            stored_successor_line_origin: None,
            table_left: None,
            table_top: origin
                + hwpunit_to_px(signed_hwpunit(table.common.vertical_offset), dpi)
                + hwpunit_to_px(i32::from(table.outer_margin_top), dpi),
            // 장식 개체는 뒤 본문의 줄 흐름을 점유하지 않는다.
            occupied_bottom: origin,
        })
    }

    /// 원본 빈 겹침 호스트의 앞 간격은 글줄 앵커가 소유한다.
    /// 앞 가시 줄의 저장 끝과 현재 흐름이 같을 때 개체는 그 원점의 바깥 위 여백에서 열린다.
    pub(crate) fn from_stored_empty_overlay_host(
        previous: &Paragraph,
        para: &Paragraph,
        table: &Table,
        origin: f64,
        spacing_before: f64,
        page_base_hu: i32,
        dpi: f64,
    ) -> Option<Self> {
        let visible = |p: &Paragraph| {
            p.text
                .chars()
                .any(|c| !c.is_whitespace() && !c.is_control() && c != '\u{FFFC}')
        };
        if !single_flow_table(para)
            || visible(para)
            || !visible(previous)
            || !previous.controls.is_empty()
            || table.common.treat_as_char
            || !table.common.flow_with_text
            || !matches!(
                table.common.text_wrap,
                TextWrap::InFrontOfText | TextWrap::BehindText
            )
            || table.common.vert_rel_to != VertRelTo::Para
            || table.common.vert_align != VertAlign::Top
            || signed_hwpunit(table.common.vertical_offset) < 0
            || table.caption.is_some()
            || para.line_segs.len() != 1
            || previous.stored_text_partition_is_dirty()
            || previous.cell_format_vpos_dirty
            || !origin.is_finite()
            || !spacing_before.is_finite()
            || spacing_before < 0.0
        {
            return None;
        }
        let host = stored_line(para)?;
        stored_line(previous)?;
        let last = previous.line_segs.last()?;
        if last.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
            || last.line_height <= 0
            || last.line_spacing < 0
        {
            return None;
        }
        let previous_end = i64::from(last.vertical_pos)
            + i64::from(last.line_height)
            + i64::from(last.line_spacing);
        let before_hu = i64::from(px_to_hwpunit(spacing_before, dpi));
        let relative_end = previous_end - i64::from(page_base_hu);
        if previous_end + before_hu != i64::from(host)
            || (origin * 7200.0 / dpi - relative_end as f64).abs() > 2.0
        {
            return None;
        }
        let top = origin
            + (i64::from(signed_hwpunit(table.common.vertical_offset))
                + i64::from(table.outer_margin_top)) as f64
                * dpi
                / 7200.0;
        Some(Self {
            flow: ParagraphFloatFlow::NextLine,
            anchor_y: origin,
            stored_host_origin: None,
            stored_successor_line_origin: None,
            table_left: None,
            table_top: top,
            occupied_bottom: top,
        })
    }
}

/// 저장 호스트 앞 간격을 제외한 원점부터 다음 줄까지가 선언 본체·오프셋·
/// 바깥 여백을 정확히 포함할 때만 그 상자를 수용한다. 선언 높이가 같다는
/// 사실만으로 문단 뒤 간격이나 호스트 줄 높이를 추가하지 않는다.
pub(super) fn from_stored_ladder(
    para: &Paragraph,
    next: &Paragraph,
    table: &Table,
    origin: f64,
    spacing_before: f64,
    measured_table_height: f64,
    origin_closes_float_frame: bool,
    dpi: f64,
) -> Option<ParagraphFloatPlacement> {
    from_ladder(
        para,
        next,
        table,
        origin,
        spacing_before,
        measured_table_height,
        origin_closes_float_frame,
        dpi,
        false,
    )
}

/// 원본 사다리가 증명한 간격 소유는 보존하되 본체는 현재 재조판 높이를 쓴다.
pub(super) fn from_reflowed_ladder(
    para: &Paragraph,
    next: &Paragraph,
    table: &Table,
    origin: f64,
    spacing_before: f64,
    measured_table_height: f64,
    dpi: f64,
) -> Option<ParagraphFloatPlacement> {
    from_ladder(
        para,
        next,
        table,
        origin,
        spacing_before,
        measured_table_height,
        false,
        dpi,
        true,
    )
}

fn from_ladder(
    para: &Paragraph,
    next: &Paragraph,
    table: &Table,
    origin: f64,
    spacing_before: f64,
    measured_table_height: f64,
    origin_closes_float_frame: bool,
    dpi: f64,
    reflowed: bool,
) -> Option<ParagraphFloatPlacement> {
    let paragraph_square = !table.common.treat_as_char
        && table.common.text_wrap == TextWrap::Square
        && table.common.vert_rel_to == VertRelTo::Para;
    if !single_flow_table(para)
        || para
            .text
            .chars()
            .any(|c| !c.is_whitespace() && !c.is_control() && c != '\u{FFFC}')
        || !(is_para_topbottom_float(&table.common) || paragraph_square)
        || next.column_type != ColumnBreakType::None
        || table.common.vert_align != VertAlign::Top
        || signed_hwpunit(table.common.vertical_offset) < 0
        || !origin.is_finite()
        || !spacing_before.is_finite()
        || spacing_before < 0.0
        || !measured_table_height.is_finite()
        || measured_table_height < 0.0
    {
        return None;
    }
    let caption_hu = stored_bottom_caption_hu(table)?;
    let declared = hwpunit_to_px(
        (i64::from(table.common.height.min(i32::MAX as u32)) + caption_hu) as i32,
        dpi,
    );
    if !reflowed && (measured_table_height - declared).abs() > 1.0 {
        return None;
    }
    let host_vpos = stored_line(para)?;
    let next_vpos = stored_line(next)?;
    let physical_hu = i64::from(signed_hwpunit(table.common.vertical_offset))
        + i64::from(table.outer_margin_top)
        + i64::from(table.common.height.min(i32::MAX as u32))
        + caption_hu
        + i64::from(table.outer_margin_bottom);
    let spacing_before_hu = px_to_hwpunit(spacing_before, dpi) as i64;
    let delta = i64::from(next_vpos) - i64::from(host_vpos);
    if delta <= 0 {
        return None;
    }
    // 원점은 문단 위끝이다. host 앞 간격이 적용되면 저장 줄은 그만큼 아래라 `델타 = 상자 −
    // 앞 간격` 이다. [#8149] 바로 앞 항목이 빈 host 자리차지 표 상자로 끝나 원점이 그 상자
    // 끝이면, 한/글은 이 host 의 앞 간격을 두지 않아 문단 위끝이 곧 저장 줄이고 `델타 = 상자`
    // 다(한글문서파일형식 39쪽 표 235: 앞은 표 234 조각, 정본 첫 행 255.4 = 줄 + 위 여백 566HU
    // + 칸 여백). 원점이 그 상자 끝이 아니면(글 host 의 밴드 뒤 — 156160455 7쪽 pi=71) 커서가
    // 저장 줄에 있다는 근거가 없으므로 이 등식으로 받지 않는다. 두 경우 모두 다음 저장 줄이
    // 상자 바닥이다.
    let applied = (delta - (physical_hu - spacing_before_hu)).abs() <= 2;
    let absorbed = origin_closes_float_frame && (delta - physical_hu).abs() <= 2;
    if !applied && !absorbed {
        return None;
    }
    // 점유 하단은 그림 원점이 아니라 다음 저장 줄이 닫는 전체 프레임이다.
    // 닫힌 등식과 동일한 저장 출처를 사용해 실제 본체 윗변을 정한다.
    // HWP5 앵커의 원점을 HWPX object frame의 위 여백으로 바꾸지 않는다.
    // 저장 등식이 없는 기존 앵커는 이 경로에 들어오지 않는다.
    let table_top = origin
        + (i64::from(signed_hwpunit(table.common.vertical_offset))
            + crate::renderer::stored_float_anchor::stored_outer_box_table_top_hu(table))
            as f64
            * dpi
            / 7200.0;
    let occupied_bottom = origin
        + physical_hu as f64 * dpi / 7200.0
        + if reflowed {
            measured_table_height - declared
        } else {
            0.0
        };
    Some(ParagraphFloatPlacement {
        flow: ParagraphFloatFlow::Exclusion,
        anchor_y: origin,
        stored_host_origin: None,
        stored_successor_line_origin: Some(occupied_bottom),
        table_left: None,
        table_top,
        occupied_bottom,
    })
}

impl ParagraphFloatPlacement {
    /// 앞 문단과 다음 문단이 표 외곽과 표 뒤의 빈 호스트 줄을 각각 닫는다.
    /// 호스트의 저장 위치를 표 앞 원점으로 읽지 않고 현재 pen에서 같은 상대 상자를 쓴다.
    pub(crate) fn from_stored_tail_host(
        previous: &Paragraph,
        host: &Paragraph,
        next: &Paragraph,
        table: &Table,
        origin: f64,
        measured_height: f64,
        dpi: f64,
    ) -> Option<Self> {
        if !single_flow_table(host)
            || host
                .text
                .chars()
                .any(|c| !c.is_whitespace() && !c.is_control() && c != '\u{FFFC}')
            || !is_para_topbottom_float(&table.common)
            || table.common.vert_align != VertAlign::Top
            || signed_hwpunit(table.common.vertical_offset) < 0
            || table.common.height == 0
            || table.common.height > i32::MAX as u32
            || table.outer_margin_top < 0
            || table.outer_margin_bottom < 0
            || host.line_segs.len() != 1
            || previous
                .controls
                .iter()
                .any(|control| !crate::renderer::composer::control_is_width_neutral_marker(control))
            || [previous, host, next].iter().any(|para| {
                para.stored_text_partition_is_dirty()
                    || para.cell_format_vpos_dirty
                    || para.line_segs.is_empty()
                    || para.line_segs.iter().any(|line| {
                        line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
                            || line.line_height <= 0
                            || line.line_spacing < 0
                    })
            })
            || ![origin, measured_height, dpi]
                .iter()
                .all(|value| value.is_finite())
            || origin < 0.0
            || dpi <= 0.0
        {
            return None;
        }
        let caption = stored_vertical_caption_hu(table)?;
        let declared = i64::from(table.common.height) + caption;
        if (measured_height - declared as f64 * dpi / 7200.0).abs() > dpi / 7200.0 {
            return None;
        }
        let previous = previous.line_segs.last()?;
        let previous_end = i64::from(previous.vertical_pos)
            + i64::from(previous.line_height)
            + i64::from(previous.line_spacing);
        let offset = i64::from(signed_hwpunit(table.common.vertical_offset));
        let physical = offset
            + i64::from(table.outer_margin_top)
            + declared
            + i64::from(table.outer_margin_bottom);
        let line = &host.line_segs[0];
        let tail = i64::from(line.line_height) + i64::from(line.line_spacing);
        if i64::from(line.vertical_pos) - previous_end != physical
            || i64::from(next.line_segs[0].vertical_pos) - i64::from(line.vertical_pos) != tail
        {
            return None;
        }
        Some(Self {
            flow: ParagraphFloatFlow::Exclusion,
            anchor_y: origin,
            stored_host_origin: None,
            stored_successor_line_origin: Some(origin + (physical + tail) as f64 * dpi / 7200.0),
            table_left: None,
            table_top: origin + (offset + i64::from(table.outer_margin_top)) as f64 * dpi / 7200.0,
            occupied_bottom: origin + (physical + tail) as f64 * dpi / 7200.0,
        })
    }
}