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
//! 개요 번호 전용 탐색 메타데이터.
//!
//! 텍스트 접두어(`1.`, `1.1` 등)를 추측하지 않는다. 문단 모양의
//! [`HeadType::Outline`]와 번호 정의만 사용하므로, 일반 본문의 숫자 문자열은
//! 탐색 목록에 포함되지 않는다.

use std::collections::HashMap;

use serde::Serialize;

use crate::document_core::queries::rendering::paragraph_text_with_equations;
use crate::document_core::DocumentCore;
use crate::error::HwpError;
use crate::model::control::Control;
use crate::model::document::Document;
use crate::model::paragraph::Paragraph;
use crate::model::style::HeadType;
use crate::renderer::layout::{
    default_outline_numbering, expand_numbering_format, resolve_numbering_id, NumberingState,
};

/// 표 안 표를 따라 내려갈 최대 깊이. 다른 질의(`table_extract`, `hidden_text`)와 같은 값이다.
const MAX_NEST_DEPTH: usize = 8;

/// 개요 번호 탐색 결과.
#[derive(Debug, Clone, Serialize)]
pub struct OutlineNavigation {
    pub outline: Vec<OutlineNavigationItem>,
}

/// 개요 번호 문단 하나의 탐색 정보.
#[derive(Debug, Clone, Serialize)]
pub struct OutlineNavigationItem {
    /// 개요 수준(1부터 시작).
    pub level: u8,
    /// 렌더러에 표시되는 실제 개요 번호.
    pub number: String,
    /// 개요 문단의 제목 텍스트.
    pub title: String,
    /// 사용자에게 표시하는 쪽 번호(1부터 시작). 조판 위치가 없으면 0이다.
    pub page: u32,
    /// 내부 이동용 구역 인덱스(0부터 시작).
    pub section: usize,
    /// 내부 이동용 문단 인덱스(0부터 시작). 표 셀 개요면 표를 품은 본문 문단이다.
    pub paragraph: usize,
    /// 표 셀 개요 문단의 셀 좌표. 본문 개요에는 없다.
    #[serde(flatten)]
    pub cell: Option<OutlineCellLocation>,
}

/// 표 셀 개요 문단의 좌표. 이름은 `hitTest`·`getCursorRectByPath` 와 같다.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OutlineCellLocation {
    /// 표를 품은 본문 문단 인덱스(`paragraph` 와 같다).
    pub parent_para_index: usize,
    /// 가장 바깥 표의 컨트롤 인덱스.
    pub control_index: usize,
    /// 가장 바깥 표의 셀 인덱스.
    pub cell_index: usize,
    /// 가장 바깥 셀 안의 문단 인덱스.
    pub cell_para_index: usize,
    /// 가장 바깥 표부터 개요 문단까지의 경로. 표 안 표면 둘 이상이다.
    pub cell_path: Vec<OutlineCellPathEntry>,
}

/// 셀 경로의 한 단계(표 → 셀 → 문단).
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OutlineCellPathEntry {
    pub control_index: usize,
    pub cell_index: usize,
    pub cell_para_index: usize,
}

/// 문단 하나의 번호 카운터를 렌더러와 같은 규칙으로 전진시킨다.
///
/// 반환값은 화면에 그려지는 번호 문자열과 수준 인덱스다. `Number` 문단이거나
/// 번호가 비는 문단은 `None`을 돌려주지만, 카운터는 이미 전진한 상태다 —
/// 목록에서 빠지는 문단도 뒤 개요의 번호에는 영향을 주기 때문이다.
fn advance_paragraph_number(
    document: &Document,
    numbering_state: &mut NumberingState,
    paragraph: &Paragraph,
    outline_numbering_id: u16,
) -> Option<(usize, String)> {
    let para_shape = document
        .doc_info
        .para_shapes
        .get(paragraph.para_shape_id as usize)?;

    if !matches!(para_shape.head_type, HeadType::Outline | HeadType::Number) {
        return None;
    }

    let numbering_id = resolve_numbering_id(
        para_shape.head_type,
        para_shape.numbering_id,
        outline_numbering_id,
    );
    let synthesized_default;
    let numbering = match numbering_id
        .checked_sub(1)
        .and_then(|index| document.doc_info.numberings.get(index as usize))
    {
        Some(numbering) => numbering,
        // 렌더러와 동일하게 정의 없는 Outline은 한컴 기본 모양으로 처리한다.
        None if para_shape.head_type == HeadType::Outline => {
            synthesized_default = default_outline_numbering();
            &synthesized_default
        }
        // 정의 없는 Number 문단은 렌더러가 번호를 표시하지 않고 카운터도 전진하지 않는다.
        None => return None,
    };

    let counters = numbering_state.advance(
        numbering_id,
        para_shape.para_level,
        paragraph.numbering_restart,
    );
    let level_index =
        (para_shape.para_level as usize).min(crate::model::style::NUMBERING_LEVELS - 1);
    let format = &numbering.level_formats[level_index];
    if format.is_empty() {
        return None;
    }
    let number = expand_numbering_format(
        format,
        &counters,
        numbering,
        &numbering.level_start_numbers,
        level_index,
    );
    if number.is_empty() || para_shape.head_type != HeadType::Outline {
        return None;
    }

    Some((level_index, number))
}

/// 문단이 품은 표의 셀 문단으로 내려가 번호 카운터를 전진시킨다.
///
/// 렌더러는 문단을 배치하면서 그 문단의 표를 같은 자리에서 조판하므로, 셀 안
/// `Outline`/`Number` 문단도 문서 순서에 맞춰 카운터를 밀어낸다. 카운터를
/// 빼먹으면 표 뒤의 개요 번호가 화면과 어긋난다. 셀의 개요 문단은 한컴 개요
/// 탐색처럼 목록에도 넣는다 — `on_outline` 이 셀 경로와 함께 받는다.
fn advance_nested_table_numbers(
    document: &Document,
    numbering_state: &mut NumberingState,
    paragraph: &Paragraph,
    outline_numbering_id: u16,
    path: &mut Vec<OutlineCellPathEntry>,
    on_outline: &mut dyn FnMut(&[OutlineCellPathEntry], usize, String, &Paragraph),
) {
    if path.len() >= MAX_NEST_DEPTH {
        return;
    }

    for (control_index, control) in paragraph.controls.iter().enumerate() {
        let Control::Table(table) = control else {
            continue;
        };
        // `Table::cells`는 행 우선 순서 — 렌더러가 셀 본문을 배치하는 순서와 같다.
        for (cell_index, cell) in table.cells.iter().enumerate() {
            for (cell_para_index, cell_paragraph) in cell.paragraphs.iter().enumerate() {
                path.push(OutlineCellPathEntry {
                    control_index,
                    cell_index,
                    cell_para_index,
                });
                if let Some((level_index, number)) = advance_paragraph_number(
                    document,
                    numbering_state,
                    cell_paragraph,
                    outline_numbering_id,
                ) {
                    on_outline(path, level_index, number, cell_paragraph);
                }
                advance_nested_table_numbers(
                    document,
                    numbering_state,
                    cell_paragraph,
                    outline_numbering_id,
                    path,
                    on_outline,
                );
                path.pop();
            }
        }
    }
}

/// 문서 순서대로 개요 번호를 계산한다.
///
/// 번호 카운터와 서식 확장은 렌더러와 같은 구현을 쓴다. `Number` 문단도 카운터에는
/// 반영하지만 결과에는 `Outline` 문단만 넣어, 번호 체계가 섞인 문서에서도 화면에
/// 그려진 개요 번호와 일치시킨다. 표 셀의 `Outline` 문단은 셀 좌표(`cell`)를 붙인다.
///
/// `table_row_pages` 는 `build_table_row_page_index` 의 결과다 — 쪽을 넘는 표의 셀
/// 개요는 그 행이 놓인 쪽을 쓴다(#3403 의 grep 과 같은 규칙).
fn build_outline_navigation(
    document: &Document,
    paragraph_pages: &HashMap<(usize, usize), u32>,
    table_row_pages: &HashMap<(usize, usize, usize), Vec<(usize, usize, u32)>>,
) -> OutlineNavigation {
    let mut numbering_state = NumberingState::default();
    let mut outline = Vec::new();

    for (section_index, section) in document.sections.iter().enumerate() {
        let outline_numbering_id = section.section_def.outline_numbering_id;

        for (paragraph_index, paragraph) in section.paragraphs.iter().enumerate() {
            let host_page = paragraph_pages
                .get(&(section_index, paragraph_index))
                .copied();
            if let Some((level_index, number)) = advance_paragraph_number(
                document,
                &mut numbering_state,
                paragraph,
                outline_numbering_id,
            ) {
                outline.push(OutlineNavigationItem {
                    level: level_index as u8 + 1,
                    number,
                    title: paragraph_text_with_equations(paragraph).trim().to_owned(),
                    page: host_page.map_or(0, |page| page + 1),
                    section: section_index,
                    paragraph: paragraph_index,
                    cell: None,
                });
            }

            advance_nested_table_numbers(
                document,
                &mut numbering_state,
                paragraph,
                outline_numbering_id,
                &mut Vec::new(),
                &mut |path, level_index, number, cell_paragraph| {
                    let outer = path[0];
                    let row = match paragraph.controls.get(outer.control_index) {
                        Some(Control::Table(table)) => table
                            .cells
                            .get(outer.cell_index)
                            .map(|cell| cell.row as usize),
                        _ => None,
                    };
                    let row_page = row.and_then(|row| {
                        table_row_pages
                            .get(&(section_index, paragraph_index, outer.control_index))?
                            .iter()
                            .find(|(start, end, _)| row >= *start && row < *end)
                            .map(|(_, _, page)| *page)
                    });
                    outline.push(OutlineNavigationItem {
                        level: level_index as u8 + 1,
                        number,
                        title: paragraph_text_with_equations(cell_paragraph)
                            .trim()
                            .to_owned(),
                        page: row_page.or(host_page).map_or(0, |page| page + 1),
                        section: section_index,
                        paragraph: paragraph_index,
                        cell: Some(OutlineCellLocation {
                            parent_para_index: paragraph_index,
                            control_index: outer.control_index,
                            cell_index: outer.cell_index,
                            cell_para_index: outer.cell_para_index,
                            cell_path: path.to_vec(),
                        }),
                    });
                },
            );
        }
    }

    OutlineNavigation { outline }
}

impl DocumentCore {
    /// 개요 번호 탐색 정보를 JSON으로 반환한다.
    ///
    /// 이 질의는 `HeadType::Outline`만 표시하며 일반 문단의 번호 문자열을 분석하지 않는다.
    pub fn get_outline_navigation_native(&self) -> Result<String, HwpError> {
        let paragraph_pages = self.build_paragraph_page_index();
        let table_row_pages = self.build_table_row_page_index();
        let navigation =
            build_outline_navigation(&self.document, &paragraph_pages, &table_row_pages);
        serde_json::to_string(&navigation).map_err(|error| {
            HwpError::RenderError(format!("개요 탐색 JSON 직렬화에 실패했습니다: {error}"))
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    use crate::model::document::Section;
    use crate::model::paragraph::Paragraph;
    use crate::model::style::{Numbering, ParaShape};
    use crate::model::table::{Cell, Table};

    fn outline_shape(level: u8) -> ParaShape {
        ParaShape {
            head_type: HeadType::Outline,
            para_level: level,
            numbering_id: 0,
            ..ParaShape::default()
        }
    }

    /// 개요와 **같은** 번호 정의(id 1)를 쓰는 `Number` 문단 모양.
    fn number_shape(level: u8) -> ParaShape {
        ParaShape {
            head_type: HeadType::Number,
            para_level: level,
            numbering_id: 1,
            ..ParaShape::default()
        }
    }

    fn paragraph(text: &str, para_shape_id: u16) -> Paragraph {
        Paragraph {
            text: text.to_owned(),
            para_shape_id,
            ..Paragraph::default()
        }
    }

    /// `cell_paragraphs`를 담은 1×1 표를 품은 문단.
    fn table_host_paragraph(para_shape_id: u16, cell_paragraphs: Vec<Paragraph>) -> Paragraph {
        let table = Table {
            row_count: 1,
            col_count: 1,
            cells: vec![Cell {
                paragraphs: cell_paragraphs,
                ..Cell::default()
            }],
            ..Table::default()
        };
        Paragraph {
            para_shape_id,
            controls: vec![Control::Table(Box::new(table))],
            ..Paragraph::default()
        }
    }

    /// 전 수준 `^N` 서식을 쓰는 번호 정의 한 개를 가진 문서 골격.
    fn document_with_shapes(para_shapes: Vec<ParaShape>) -> Document {
        let mut numbering = Numbering::default();
        for format in &mut numbering.level_formats {
            *format = "^N".to_owned();
        }
        numbering.level_start_numbers = [1; crate::model::style::NUMBERING_LEVELS];

        let mut document = Document::default();
        document.doc_info.para_shapes = para_shapes;
        document.doc_info.numberings.push(numbering);
        document
    }

    fn section_with(paragraphs: Vec<Paragraph>) -> Section {
        Section {
            section_def: crate::model::document::SectionDef {
                outline_numbering_id: 1,
                ..crate::model::document::SectionDef::default()
            },
            paragraphs,
            ..Section::default()
        }
    }

    #[test]
    fn uses_outline_metadata_and_not_text_prefixes() {
        let mut numbering = Numbering::default();
        for format in &mut numbering.level_formats {
            *format = "^N".to_owned();
        }
        numbering.level_start_numbers = [1; crate::model::style::NUMBERING_LEVELS];

        let mut document = Document::default();
        document.doc_info.para_shapes =
            vec![outline_shape(0), outline_shape(1), ParaShape::default()];
        document.doc_info.numberings.push(numbering);
        document.sections.push(Section {
            section_def: crate::model::document::SectionDef {
                outline_numbering_id: 1,
                ..crate::model::document::SectionDef::default()
            },
            paragraphs: vec![
                paragraph("개요", 0),
                paragraph("목적", 1),
                // 숫자로 시작해도 Outline 속성이 아니면 목록에 넣지 않는다.
                paragraph("1. 일반 본문", 2),
                paragraph("요구사항", 0),
            ],
            ..Section::default()
        });
        let paragraph_pages = HashMap::from([((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((0, 3), 3)]);

        let navigation = build_outline_navigation(&document, &paragraph_pages, &HashMap::new());

        assert_eq!(navigation.outline.len(), 3);
        assert_eq!(navigation.outline[0].number, "1.");
        assert_eq!(navigation.outline[0].level, 1);
        assert_eq!(navigation.outline[0].page, 1);
        assert_eq!(navigation.outline[1].number, "1.1.");
        assert_eq!(navigation.outline[1].level, 2);
        assert_eq!(navigation.outline[1].page, 2);
        assert_eq!(navigation.outline[2].number, "2.");
        assert_eq!(navigation.outline[2].title, "요구사항");
        assert_eq!(navigation.outline[2].page, 4);
    }

    /// 표 셀의 `Number` 문단은 목록에 넣지 않지만 카운터는 렌더러처럼 전진시킨다.
    ///
    /// `앞 Outline 1. → 표 셀 Number 2. → 뒤 Outline 3.` — 셀 문단을 건너뛰면 뒤
    /// 개요가 `2.`가 되어 화면과 어긋난다(PR #4093 리뷰 지적).
    #[test]
    fn table_cell_number_paragraph_advances_counter_without_being_listed() {
        let mut document = document_with_shapes(vec![
            outline_shape(0),
            ParaShape::default(),
            number_shape(0),
        ]);
        document.sections.push(section_with(vec![
            paragraph("앞 개요", 0),
            table_host_paragraph(1, vec![paragraph("셀 번호 문단", 2)]),
            paragraph("뒤 개요", 0),
        ]));
        let paragraph_pages = HashMap::from([((0, 0), 0), ((0, 1), 0), ((0, 2), 0)]);

        let navigation = build_outline_navigation(&document, &paragraph_pages, &HashMap::new());

        // 셀의 `Number` 문단은 개요가 아니므로 목록에서 빠진다.
        assert_eq!(navigation.outline.len(), 2);
        assert_eq!(navigation.outline[0].number, "1.");
        assert_eq!(navigation.outline[0].paragraph, 0);
        assert_eq!(navigation.outline[1].number, "3.");
        assert_eq!(navigation.outline[1].title, "뒤 개요");
        assert_eq!(navigation.outline[1].paragraph, 2);
    }

    /// 표 안 표(중첩)의 번호 문단도 같은 문서 순서로 카운터에 반영된다.
    #[test]
    fn nested_table_number_paragraph_advances_counter() {
        let mut document = document_with_shapes(vec![
            outline_shape(0),
            ParaShape::default(),
            number_shape(0),
        ]);
        let inner_host = table_host_paragraph(1, vec![paragraph("중첩 셀 번호 문단", 2)]);
        document.sections.push(section_with(vec![
            paragraph("앞 개요", 0),
            table_host_paragraph(1, vec![paragraph("셀 번호 문단", 2), inner_host]),
            paragraph("뒤 개요", 0),
        ]));
        let paragraph_pages = HashMap::from([((0, 0), 0), ((0, 1), 0), ((0, 2), 0)]);

        let navigation = build_outline_navigation(&document, &paragraph_pages, &HashMap::new());

        assert_eq!(navigation.outline.len(), 2);
        assert_eq!(navigation.outline[0].number, "1.");
        // 셀 Number 2. + 중첩 셀 Number 3. 이 지나간 뒤의 개요.
        assert_eq!(navigation.outline[1].number, "4.");
    }

    /// 표 셀의 `Outline` 문단은 셀 좌표와 함께 목록에 들어간다(한컴 개요 탐색과 같다).
    #[test]
    fn table_cell_outline_paragraph_is_listed_with_cell_path() {
        let mut document = document_with_shapes(vec![
            outline_shape(0),
            ParaShape::default(),
            outline_shape(0),
        ]);
        document.sections.push(section_with(vec![
            paragraph("앞 개요", 0),
            table_host_paragraph(1, vec![paragraph("셀 개요", 2)]),
            paragraph("뒤 개요", 0),
        ]));
        let paragraph_pages = HashMap::from([((0, 0), 0), ((0, 1), 0), ((0, 2), 0)]);
        // 표가 쪽을 넘어 셀의 행이 셋째 쪽(0부터 2)에 놓였다고 둔다.
        let table_row_pages = HashMap::from([((0, 1, 0), vec![(0, usize::MAX, 2)])]);

        let navigation = build_outline_navigation(&document, &paragraph_pages, &table_row_pages);

        let numbers: Vec<_> = navigation
            .outline
            .iter()
            .map(|item| {
                (
                    item.number.as_str(),
                    item.title.as_str(),
                    item.paragraph,
                    item.page,
                )
            })
            .collect();
        assert_eq!(
            numbers,
            vec![
                ("1.", "앞 개요", 0, 1),
                ("2.", "셀 개요", 1, 3),
                ("3.", "뒤 개요", 2, 1)
            ]
        );
        assert!(navigation.outline[0].cell.is_none());
        let cell = navigation.outline[1].cell.as_ref().expect("셀 좌표");
        assert_eq!(
            (
                cell.parent_para_index,
                cell.control_index,
                cell.cell_index,
                cell.cell_para_index,
                cell.cell_path.len()
            ),
            (1, 0, 0, 0, 1)
        );
    }
}