rhwp 0.9.0

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
fn try_identify_inline_controls<S: crate::storage::Storage>(
    para: &Paragraph,
    storage: &S,
) -> Result<Vec<InlineControl>, S::Error> {
    let mut result = storage.vec(0)?;

    for (ctrl_idx, ctrl) in para.controls.iter().enumerate() {
        let control_type = match ctrl {
            Control::Table(t) if t.common.treat_as_char => InlineControlType::Table,
            Control::Shape(shape) if shape.common().treat_as_char => InlineControlType::Shape,
            Control::Picture(pic) if pic.common.treat_as_char => InlineControlType::Shape,
            Control::Equation(eq) if eq.common.treat_as_char => InlineControlType::Shape,
            Control::Form(form) if form.common.treat_as_char => InlineControlType::Shape,
            Control::SectionDef(_) | Control::ColumnDef(_) => InlineControlType::Other,
            _ => continue,
        };

        // 이 컨트롤이 어느 줄에 속하는지 결정
        // 컨트롤은 문단의 controls 배열에 순서대로 저장됨
        // 정확한 줄 위치는 텍스트 내 제어 문자 위치로 결정해야 하지만,
        // 현재는 첫 번째 줄에 배치 (향후 정확한 위치 계산 가능)
        let line_index = 0;

        storage.push(
            &mut result,
            InlineControl {
                line_index,
                control_index: ctrl_idx,
                control_type,
            },
        )?;
    }

    Ok(result)
}

fn is_render_inline_control(ctrl: &Control) -> bool {
    match ctrl {
        Control::Picture(pic) => pic.common.treat_as_char,
        Control::Shape(shape) => shape.common().treat_as_char,
        Control::Table(table) => table.common.treat_as_char,
        Control::Equation(eq) => eq.common.treat_as_char,
        // [#6266] 양식 개체도 자기 배치를 갖는다 — 비-TAC 은 인라인이 아니다.
        Control::Form(form) => form.common.treat_as_char,
        _ => false,
    }
}

/// [#6300] 다음 줄의 머리 공백 뒤에 treat_as_char 인라인 개체가 있는지.
fn try_tac_inline_object_starts_at<S: crate::storage::Storage>(
    para: &Paragraph,
    pos: usize,
    storage: &S,
) -> Result<bool, S::Error> {
    let chars: Vec<_> = storage.collect(para.text.chars())?;
    Ok(para
        .controls
        .iter()
        .zip(para.try_control_text_positions(storage)?)
        .any(|(ctrl, ctrl_pos)| {
            is_render_inline_control(ctrl)
                && pos <= ctrl_pos
                && ctrl_pos <= chars.len()
                && chars[pos..ctrl_pos]
                    .iter()
                    .all(|ch| ch.is_whitespace() && !matches!(ch, '\n' | '\r'))
        }))
}

pub(crate) fn find_render_inline_control_positions(para: &Paragraph) -> Vec<usize> {
    match try_find_render_inline_control_positions(para, &crate::storage::Unbounded) {
        Ok(value) => value,
        Err(never) => match never {},
    }
}

fn try_find_render_inline_control_positions<S: crate::storage::Storage>(
    para: &Paragraph,
    storage: &S,
) -> Result<Vec<usize>, S::Error> {
    if para.text.is_empty() && para.char_offsets.is_empty() {
        let mut inline_seen = 0usize;
        let mut positions = storage.vec(para.controls.len())?;
        for ctrl in &para.controls {
            positions.push(inline_seen);
            if is_render_inline_control(ctrl) {
                inline_seen += 1;
            }
        }
        return Ok(positions);
    }

    para.try_control_text_positions(storage)
}

/// CharOverlap 컨트롤의 글자를 조합된 텍스트에 올바른 위치로 삽입한다.
///
/// char_offsets 갭 분석으로 각 CharOverlap의 원래 텍스트 위치를 복원하고,
/// 해당 위치의 composed line에서 기존 텍스트 런을 분할하여 CharOverlap 런을 삽입한다.
fn try_inject_char_overlap_text<S: crate::storage::Storage>(
    composed: &mut ComposedParagraph,
    para: &Paragraph,
    styles: Option<&ResolvedStyleSet>,
    storage: &S,
) -> Result<(), S::Error> {
    // CharOverlap·Ruby 컨트롤과 인덱스 수집. 덧말도 제어 문자 한 칸에 본말을 그린다.
    let char_overlap_indices: Vec<(usize, &Control)> =
        storage.collect(para.controls.iter().enumerate().filter(|(_, c)| {
            matches!(c, Control::CharOverlap(_) | Control::Ruby(_))
        }))?;

    if char_overlap_indices.is_empty() {
        return Ok(());
    }

    // 모든 컨트롤의 텍스트 위치 결정
    let control_positions = para.try_control_text_positions(storage)?;
    let stream_positions = para.control_utf16_positions_with_storage(storage)?;

    // CharOverlap별 (텍스트위치, 런) 수집
    let mut insertions: Vec<(usize, ComposedTextRun)> = storage.vec(0)?;
    for (ctrl_idx, control) in &char_overlap_indices {
        if let Control::Ruby(ruby) = control {
            let stream_pos = stream_positions.get(*ctrl_idx).copied().unwrap_or(0);
            let host_style_id = para
                .char_shapes
                .iter()
                .filter(|r| r.start_pos <= stream_pos)
                .max_by_key(|r| r.start_pos)
                .or_else(|| para.char_shapes.first())
                .map_or(0, |r| r.char_shape_id);
            let text_pos = control_positions.get(*ctrl_idx).copied().unwrap_or(0);
            storage.push(
                &mut insertions,
                (
                    text_pos,
                    ComposedTextRun {
                        text: storage.string(&ruby.main_text)?,
                        char_style_id: host_style_id,
                        lang_index: 0,
                        neutral_space_origin: None,
                        char_overlap: Some(CharOverlapInfo {
                            border_type: 0,
                            inner_char_size: 0,
                            composition: styles.and_then(|styles| {
                                super::char_overlap::Composition::from_ruby(
                                    ruby,
                                    styles,
                                    host_style_id,
                                )
                            }),
                            empty_ink: ruby.main_text.is_empty(),
                        }),
                        footnote_marker: None,
                        display_text: None,
                        supplemental_metrics_blocked: false,
                        inserted_control_text: true,
                        space_metric: crate::renderer::composer::SpaceMetric::Stored,
                        regenerated_font_metrics: text_metric_origin::regenerated_row(para, 0),
                        literal_figure_space: false,
                        literal_soft_hyphen: false,
                    },
                ),
            )?;
            continue;
        }
        let Control::CharOverlap(co) = control else {
            continue;
        };
        let text = char_overlap_flow_text(&co.chars, storage)?;
        let stored_char_style_id = co
            .char_shape_ids
            .iter()
            .find(|&&id| id != 0xFFFFFFFF)
            .copied()
            .unwrap_or(0);
        let text_pos = control_positions.get(*ctrl_idx).copied().unwrap_or(0);
        // 제어 슬롯 뒤 글자의 스타일이 아니라, 원문 슬롯 자체를 소유한 run을 찾는다.
        let stream_pos = stream_positions.get(*ctrl_idx).copied().unwrap_or(0);
        let host_style_id = para
            .char_shapes
            .iter()
            .filter(|r| r.start_pos <= stream_pos)
            .max_by_key(|r| r.start_pos)
            .or_else(|| para.char_shapes.first())
            .map_or(0, |r| r.char_shape_id);
        let composition = styles.and_then(|styles| {
            super::char_overlap::Composition::from_model(co, styles, host_style_id)
        });
        let char_style_id = if composition.is_some() {
            host_style_id
        } else {
            stored_char_style_id
        };
        storage.push(
            &mut insertions,
            (
                text_pos,
                ComposedTextRun {
                    text,
                    char_style_id,
                    lang_index: 0,
                    neutral_space_origin: None,
                    char_overlap: Some(CharOverlapInfo {
                        border_type: co.border_type,
                        inner_char_size: styles.map_or(co.inner_char_size, |s| {
                            s.char_overlap_inner_size(co.inner_char_size)
                        }),
                        composition,
                        // HWP의 프레임 문자와 HWPX의 명시적 전각 공백은 다르다.
                        empty_ink: co.border_type == 0
                            && (co.chars.is_empty()
                                || (co.hwp_wire_chars && co.chars.as_slice() == ['\u{3000}'])),
                    }),
                    footnote_marker: None,
                    display_text: None,
                    supplemental_metrics_blocked: false,
                    inserted_control_text: true,
                    space_metric: crate::renderer::composer::SpaceMetric::Stored,
                    regenerated_font_metrics: text_metric_origin::regenerated_row(para, 0),
                    literal_figure_space: false,
                    literal_soft_hyphen: false,
                },
            ),
        )?;
    }

    if insertions.is_empty() {
        return Ok(());
    }

    if composed.lines.is_empty() {
        // 빈 문단: line_segs에서 줄 정보를 가져와 새 줄 생성
        let (lh, bd, ls) = para
            .line_segs
            .first()
            .map(|s| (s.line_height, s.baseline_distance, s.line_spacing))
            .unwrap_or((400, 340, 0));
        storage.push(
            &mut composed.lines,
            ComposedLine {
                runs: storage.collect(insertions.into_iter().map(|(_, run)| run))?,
                line_height: lh,
                baseline_distance: bd,
                segment_width: 0,
                column_start: 0,
                line_spacing: ls,
                has_line_break: false,
                char_start: 0,
            },
        )?;
        return Ok(());
    }

    // 역순으로 삽입하여 이전 인덱스가 무효화되지 않도록
    storage.bytes(
        insertions
            .len()
            .saturating_mul(std::mem::size_of::<(usize, ComposedTextRun)>()),
    )?;
    insertions.sort_by_key(|(pos, _)| std::cmp::Reverse(*pos));

    for (text_pos, overlap_run) in insertions {
        try_insert_control_text_run(composed, text_pos, overlap_run, storage)?;
    }
    Ok(())
}

/// 조합된 라인들에서 text_pos 위치에 CharOverlap 런을 삽입한다.
/// 기존 텍스트 런을 필요시 분할한다.
pub(super) fn insert_control_text_run(
    composed: &mut ComposedParagraph,
    text_pos: usize,
    overlap_run: ComposedTextRun,
) {
    match try_insert_control_text_run(composed, text_pos, overlap_run, &crate::storage::Unbounded) {
        Ok(()) => (),
        Err(never) => match never {},
    }
}

fn try_insert_control_text_run<S: crate::storage::Storage>(
    composed: &mut ComposedParagraph,
    text_pos: usize,
    mut overlap_run: ComposedTextRun,
    storage: &S,
) -> Result<(), S::Error> {
    let mut char_offset = 0usize;

    for line in composed.lines.iter_mut() {
        let line_char_count: usize = line
            .runs
            .iter()
            .filter(|r| r.char_overlap.is_none() && !r.inserted_control_text)
            .map(|r| r.text.chars().count())
            .sum();

        if text_pos < char_offset + line_char_count || text_pos == char_offset {
            // 이 라인에 삽입
            let local_pos = text_pos - char_offset;
            let mut run_offset = 0usize;

            for run_idx in 0..line.runs.len() {
                // CharOverlap 런은 건너뜀 (이미 삽입된 것)
                if line.runs[run_idx].inserted_control_text {
                    continue;
                }

                let run_chars = line.runs[run_idx].text.chars().count();
                overlap_run.regenerated_font_metrics = line.runs[run_idx].regenerated_font_metrics;

                if local_pos == run_offset {
                    // 런 앞에 삽입
                    storage.reserve(&mut line.runs, 1)?;
                    line.runs.insert(run_idx, overlap_run);
                    return Ok(());
                } else if local_pos > run_offset && local_pos < run_offset + run_chars {
                    // 런 중간에 삽입: 런을 분할
                    let split_at = local_pos - run_offset;
                    let original_text = &line.runs[run_idx].text;
                    let before = storage.chars(original_text.chars().take(split_at))?;
                    let after = storage.chars(original_text.chars().skip(split_at))?;
                    storage.reserve(&mut line.runs, 2)?;

                    let style_id = line.runs[run_idx].char_style_id;
                    let lang_idx = line.runs[run_idx].lang_index;

                    // 기존 런을 before로 교체
                    line.runs[run_idx].text = before;

                    // after 런 생성
                    let after_run = ComposedTextRun {
                        text: after,
                        char_style_id: style_id,
                        lang_index: lang_idx,
                        neutral_space_origin: None,
                        char_overlap: None,
                        footnote_marker: None,
                        display_text: None,
                        supplemental_metrics_blocked: line.runs[run_idx]
                            .supplemental_metrics_blocked,
                        inserted_control_text: line.runs[run_idx].inserted_control_text,
                        space_metric: line.runs[run_idx].space_metric,
                        regenerated_font_metrics: line.runs[run_idx].regenerated_font_metrics,
                        literal_figure_space: line.runs[run_idx].literal_figure_space,
                        literal_soft_hyphen: line.runs[run_idx].literal_soft_hyphen,
                    };

                    // overlap_run과 after_run을 삽입
                    line.runs.insert(run_idx + 1, after_run);
                    line.runs.insert(run_idx + 1, overlap_run);
                    return Ok(());
                }

                run_offset += run_chars;
            }

            // 삽입한 표시도 이 물리 줄이 선택한 측정 출처를 따른다.
            if let Some(last) = line.runs.last() {
                overlap_run.regenerated_font_metrics = last.regenerated_font_metrics;
            }
            storage.push(&mut line.runs, overlap_run)?;
            return Ok(());
        }

        char_offset += line_char_count;
    }

    // 어느 라인에도 해당하지 않으면 마지막 라인에 추가
    if let Some(last_line) = composed.lines.last_mut() {
        storage.push(&mut last_line.runs, overlap_run)?;
    }
    Ok(())
}

/// ComposedLine의 폭을 언어 인식 측정으로 계산한다.
///
/// 각 run별로 해당 언어의 폰트/자간/장평을 적용하여 측정한다.
/// 진단 API에서 저장된 segment_width와 비교하는 데 사용한다.
pub fn estimate_composed_line_width(line: &ComposedLine, styles: &ResolvedStyleSet) -> f64 {
    match estimate_composed_line_width_with_storage(line, styles, &crate::storage::Unbounded) {
        Ok(value) => value,
        Err(never) => match never {},
    }
}

pub(crate) fn estimate_composed_line_width_with_storage<S: crate::storage::Storage>(
    line: &ComposedLine,
    styles: &ResolvedStyleSet,
    storage: &S,
) -> Result<f64, S::Error> {
    // Retain the original sum, stopping immediately at the first storage error.
    let mut error = None;
    let width = line
        .runs
        .iter()
        .scan(&mut error, |error, run| {
            match run.try_text_style(styles, storage) {
                Ok(ts) => Some(estimate_text_width(effective_text_for_metrics(run), &ts)),
                Err(e) => {
                    **error = Some(e);
                    None
                }
            }
        })
        .sum();
    match error {
        Some(error) => Err(error),
        None => Ok(width),
    }
}