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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
//! 각주 내용 편집 관련 native 메서드

use super::super::helpers::{
    build_tab_def_from_json, json_has_border_keys, json_has_tab_keys, parse_json_i16_array,
    parse_para_shape_mods,
};
use super::formatting::{para_shape_mods_affect_text_flow, restore_para_meta};
use crate::document_core::DocumentCore;
use crate::error::HwpError;
use crate::model::control::Control;
use crate::model::event::DocumentEvent;
use crate::model::paragraph::{ParaMeta, Paragraph};
use crate::renderer::composer::{reflow_line_segs, restamp_indentation, ParagraphBox};

impl DocumentCore {
    /// 구역의 각주 번호를 문서 순서대로 1부터 다시 매긴다.
    fn renumber_footnotes_in_section(&mut self, section_idx: usize) {
        let section = &mut self.document.sections[section_idx];
        let mut footnote_number = 1u16;
        for para in &mut section.paragraphs {
            for ctrl in &mut para.controls {
                match ctrl {
                    Control::Footnote(footnote) => {
                        footnote.number = footnote_number;
                        footnote_number = footnote_number.saturating_add(1);
                    }
                    Control::Table(table) => {
                        for cell in &mut table.cells {
                            for cell_para in &mut cell.paragraphs {
                                for cell_ctrl in &mut cell_para.controls {
                                    if let Control::Footnote(footnote) = cell_ctrl {
                                        footnote.number = footnote_number;
                                        footnote_number = footnote_number.saturating_add(1);
                                    }
                                }
                            }
                        }
                    }
                    Control::Shape(shape) => {
                        if let Some(text_box) =
                            shape.drawing_mut().and_then(|d| d.text_box.as_mut())
                        {
                            for text_para in &mut text_box.paragraphs {
                                for text_ctrl in &mut text_para.controls {
                                    if let Control::Footnote(footnote) = text_ctrl {
                                        footnote.number = footnote_number;
                                        footnote_number = footnote_number.saturating_add(1);
                                    }
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }
        }
    }

    /// 본문 커서 위치의 각주 마커를 조회한다.
    ///
    /// direction:
    /// - "backward": 커서 바로 앞 마커(Backspace)
    /// - "forward": 커서 바로 뒤 마커(Delete)
    pub fn get_footnote_at_cursor_native(
        &self,
        section_idx: usize,
        para_idx: usize,
        char_offset: usize,
        direction: &str,
    ) -> Result<String, HwpError> {
        if direction != "backward" && direction != "forward" {
            return Err(HwpError::RenderError(format!(
                "지원하지 않는 각주 조회 방향입니다: {}",
                direction
            )));
        }

        let section = self.document.sections.get(section_idx).ok_or_else(|| {
            HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
        })?;
        let para = section
            .paragraphs
            .get(para_idx)
            .ok_or_else(|| HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", para_idx)))?;

        let positions = crate::document_core::helpers::find_control_text_positions(para);
        for (control_idx, ctrl) in para.controls.iter().enumerate() {
            let number = match ctrl {
                Control::Footnote(footnote) => footnote.number,
                Control::Endnote(endnote) => endnote.number,
                _ => continue,
            };
            let Some(marker_pos) = positions.get(control_idx).copied() else {
                continue;
            };
            let matches_cursor = match direction {
                "backward" => char_offset == marker_pos + 1,
                "forward" => char_offset == marker_pos,
                _ => false,
            };
            if matches_cursor {
                return Ok(format!(
                    "{{\"hit\":true,\"sectionIndex\":{},\"paragraphIndex\":{},\"controlIndex\":{},\"charOffset\":{},\"footnoteNumber\":{}}}",
                    section_idx,
                    para_idx,
                    control_idx,
                    marker_pos,
                    number,
                ));
            }
        }

        Ok("{\"hit\":false}".to_string())
    }

    /// 본문 각주 컨트롤을 삭제한다.
    ///
    /// 각주 내부 내용과 본문 마커를 함께 제거하고, 남은 각주 번호를 문서 순서대로 재계산한다.
    pub fn delete_footnote_native(
        &mut self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
    ) -> Result<String, HwpError> {
        let (marker_pos, deleted_number) = {
            let section = self.document.sections.get(section_idx).ok_or_else(|| {
                HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
            })?;
            let para = section.paragraphs.get(para_idx).ok_or_else(|| {
                HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", para_idx))
            })?;
            let ctrl = para.controls.get(control_idx).ok_or_else(|| {
                HwpError::RenderError(format!("컨트롤 인덱스 {} 범위 초과", control_idx))
            })?;
            let number = match ctrl {
                Control::Footnote(footnote) => footnote.number,
                Control::Endnote(endnote) => endnote.number,
                _ => {
                    return Err(HwpError::RenderError(format!(
                        "컨트롤 {}은 각주/미주가 아닙니다",
                        control_idx
                    )));
                }
            };
            let positions = crate::document_core::helpers::find_control_text_positions(para);
            let marker_pos = positions.get(control_idx).copied().ok_or_else(|| {
                HwpError::RenderError(format!(
                    "각주 컨트롤 {}의 본문 위치를 찾을 수 없습니다",
                    control_idx
                ))
            })?;
            (marker_pos, number)
        };

        {
            let section = &mut self.document.sections[section_idx];
            let para = &mut section.paragraphs[para_idx];

            let marker_utf16_pos = para.char_offsets.get(marker_pos).copied().unwrap_or(0);
            for offset in para.char_offsets.iter_mut().skip(marker_pos) {
                if *offset >= 8 {
                    *offset -= 8;
                }
            }
            // char_shapes/range_tags 도 char_offsets 와 함께 되돌린다 — 삽입 경로
            // (shift_for_inline_control_insert)와 대칭되는 삭제측 시프트가 없으면
            // 삭제 지점 이후 글자모양 run·range_tag 경계가 텍스트와 어긋난다.
            for cs in &mut para.char_shapes {
                if cs.start_pos > marker_utf16_pos {
                    cs.start_pos = cs.start_pos.saturating_sub(8);
                }
            }
            for rt in &mut para.range_tags {
                if rt.start >= marker_utf16_pos {
                    rt.start = rt.start.saturating_sub(8);
                }
                if rt.end >= marker_utf16_pos {
                    rt.end = rt.end.saturating_sub(8);
                }
            }

            para.controls.remove(control_idx);
            if control_idx < para.ctrl_data_records.len() {
                para.ctrl_data_records.remove(control_idx);
            }
            if para.char_count >= 8 {
                para.char_count -= 8;
            }
            if !para
                .controls
                .iter()
                .any(|c| matches!(c, Control::Footnote(_) | Control::Endnote(_)))
            {
                para.control_mask &= !(1u32 << 0x0011);
            }
            para.forget_para_text_if_emptied();

            Self::reflow_paragraph_line_segs_after_control_delete(para, &self.styles, self.dpi);
            section.raw_stream = None;
        }

        self.renumber_footnotes_in_section(section_idx);
        // 미주 번호는 구역마다 미주 모양의 번호 매기기를 따른다(뒤 구역까지).
        self.refresh_endnote_numbers(section_idx);
        self.mark_section_dirty(section_idx);
        self.recompose_section(section_idx);
        self.paginate_if_needed();
        // 각주 번호는 구역의 각주 모양(시작 번호·이어 세기·쪽마다 새로 시작)을 따른다.
        self.refresh_footnote_numbers(section_idx);
        self.invalidate_page_tree_cache();

        self.event_log.push(DocumentEvent::FootnoteDeleted {
            section: section_idx,
            para: para_idx,
            ctrl: control_idx,
        });

        Ok(format!(
            "{{\"ok\":true,\"sectionIndex\":{},\"paragraphIndex\":{},\"controlIndex\":{},\"charOffset\":{},\"deletedNumber\":{}}}",
            section_idx,
            para_idx,
            control_idx,
            marker_pos,
            deleted_number,
        ))
    }

    /// 각주 컨트롤 내부 문단의 가변 참조를 얻는다.
    pub(crate) fn get_footnote_paragraph_mut(
        &mut self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
        fn_para_idx: usize,
    ) -> Result<&mut Paragraph, HwpError> {
        let section = self.document.sections.get_mut(section_idx).ok_or_else(|| {
            HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
        })?;
        let para = section
            .paragraphs
            .get_mut(para_idx)
            .ok_or_else(|| HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", para_idx)))?;
        let ctrl = para.controls.get_mut(control_idx).ok_or_else(|| {
            HwpError::RenderError(format!("컨트롤 인덱스 {} 범위 초과", control_idx))
        })?;
        match ctrl {
            Control::Footnote(f) => {
                let len = f.paragraphs.len();
                if fn_para_idx >= len {
                    return Err(HwpError::RenderError(format!(
                        "각주 문단 인덱스 {} 범위 초과 (총 {}개)",
                        fn_para_idx, len
                    )));
                }
                Ok(&mut f.paragraphs[fn_para_idx])
            }
            Control::Endnote(e) => {
                let len = e.paragraphs.len();
                if fn_para_idx >= len {
                    return Err(HwpError::RenderError(format!(
                        "미주 문단 인덱스 {} 범위 초과 (총 {}개)",
                        fn_para_idx, len
                    )));
                }
                Ok(&mut e.paragraphs[fn_para_idx])
            }
            _ => Err(HwpError::RenderError(format!(
                "컨트롤 {}은 각주/미주가 아닙니다",
                control_idx
            ))),
        }
    }

    /// 각주 컨트롤 내부 문단의 불변 참조를 얻는다.
    pub(crate) fn get_footnote_paragraph_ref(
        &self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
        fn_para_idx: usize,
    ) -> Option<&Paragraph> {
        let section = self.document.sections.get(section_idx)?;
        let para = section.paragraphs.get(para_idx)?;
        let ctrl = para.controls.get(control_idx)?;
        match ctrl {
            Control::Footnote(f) => f.paragraphs.get(fn_para_idx),
            Control::Endnote(e) => e.paragraphs.get(fn_para_idx),
            _ => None,
        }
    }

    /// 각주 문단 리플로우
    pub(crate) fn reflow_footnote_paragraph(
        &mut self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
        fn_para_idx: usize,
    ) {
        use crate::renderer::hwpunit_to_px;

        // 각주 영역 폭 = 페이지 텍스트 영역 폭
        let available_width = {
            let section = &self.document.sections[section_idx];
            let page_def = &section.section_def.page_def;
            let text_width =
                page_def.width as i32 - page_def.margin_left as i32 - page_def.margin_right as i32;
            hwpunit_to_px(text_width, self.dpi)
        };

        // 문단 모양 변경 직후에는 새 id가 이전 rebuild의 styles에 없을 수 있다.
        // 머리말/꼬리말과 셀의 재조판처럼 현재 doc_info에서 해석한 같은 스타일로
        // 줄 폭과 들여쓰기 플래그를 만든다.
        let styles = self.resolve_render_styles();

        // 문단 여백 적용
        let para_shape_id = match self.get_footnote_paragraph_ref(
            section_idx,
            para_idx,
            control_idx,
            fn_para_idx,
        ) {
            Some(p) => p.para_shape_id,
            None => return,
        };
        let para_style = styles.para_styles.get(para_shape_id as usize);
        let margin_left = para_style.map(|s| s.margin_left).unwrap_or(0.0);
        let margin_right = para_style.map(|s| s.margin_right).unwrap_or(0.0);
        let final_width = (available_width - margin_left - margin_right).max(0.0);

        // 가변 참조로 리플로우 실행
        let section = &mut self.document.sections[section_idx];
        let ctrl = &mut section.paragraphs[para_idx].controls[control_idx];
        match ctrl {
            Control::Footnote(f) => {
                if let Some(para) = f.paragraphs.get_mut(fn_para_idx) {
                    // 각주 본문 상자 — 각주 영역은 자기 열이 아니므로 미스냅.
                    reflow_line_segs(
                        para,
                        ParagraphBox::content_width_px(final_width, self.dpi),
                        &styles,
                        self.dpi,
                    );
                }
            }
            Control::Endnote(e) => {
                if let Some(para) = e.paragraphs.get_mut(fn_para_idx) {
                    // 미주 본문 상자 — 각주와 같은 이유로 미스냅.
                    reflow_line_segs(
                        para,
                        ParagraphBox::content_width_px(final_width, self.dpi),
                        &styles,
                        self.dpi,
                    );
                }
            }
            _ => {}
        }
    }

    /// 각주/미주 내부 문단 속성 조회.
    pub fn get_para_properties_in_footnote_native(
        &self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
        fn_para_idx: usize,
    ) -> Result<String, HwpError> {
        let para = self
            .get_footnote_paragraph_ref(section_idx, para_idx, control_idx, fn_para_idx)
            .ok_or_else(|| {
                HwpError::RenderError(format!(
                    "각주/미주 문단을 찾을 수 없습니다: sec={} para={} ctrl={} fn_para={}",
                    section_idx, para_idx, control_idx, fn_para_idx
                ))
            })?;
        Ok(self.build_para_properties_json(para.para_shape_id, section_idx))
    }

    /// 각주/미주 내부 문단 속성 적용.
    pub fn apply_para_format_in_footnote_native(
        &mut self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
        fn_para_idx: usize,
        props_json: &str,
    ) -> Result<String, HwpError> {
        let mut mods = parse_para_shape_mods(props_json);

        if json_has_tab_keys(props_json) {
            let para = self
                .get_footnote_paragraph_ref(section_idx, para_idx, control_idx, fn_para_idx)
                .ok_or_else(|| {
                    HwpError::RenderError("각주/미주 문단을 찾을 수 없음".to_string())
                })?;
            let base_tab_def_id = self
                .document
                .doc_info
                .para_shapes
                .get(para.para_shape_id as usize)
                .map(|ps| ps.tab_def_id)
                .unwrap_or(0);
            let new_td = build_tab_def_from_json(
                props_json,
                base_tab_def_id,
                &self.document.doc_info.tab_defs,
            );
            let new_tab_id = self.document.find_or_create_tab_def(new_td);
            mods.tab_def_id = Some(new_tab_id);
        }

        if json_has_border_keys(props_json) {
            let bf_id = self.create_border_fill_from_json(props_json);
            mods.border_fill_id = Some(bf_id);
        }
        if let Some(arr) = parse_json_i16_array(props_json, "borderSpacing", 4) {
            mods.border_spacing = Some([arr[0], arr[1], arr[2], arr[3]]);
        }

        let base_id = self
            .get_footnote_paragraph_ref(section_idx, para_idx, control_idx, fn_para_idx)
            .ok_or_else(|| HwpError::RenderError("각주/미주 문단을 찾을 수 없음".to_string()))?
            .para_shape_id;
        let new_id = self.document.find_or_create_para_shape(base_id, &mods);
        let (old_indent, new_indent) = self.para_shape_indents(base_id, new_id);
        {
            let fn_para =
                self.get_footnote_paragraph_mut(section_idx, para_idx, control_idx, fn_para_idx)?;
            fn_para.para_shape_id = new_id;
            restamp_indentation(&mut fn_para.line_segs, old_indent, new_indent);
        }

        if para_shape_mods_affect_text_flow(&mods) {
            self.reflow_footnote_paragraph(section_idx, para_idx, control_idx, fn_para_idx);
        }

        self.document.sections[section_idx].raw_stream = None;
        self.rebuild_section(section_idx);
        self.event_log.push(DocumentEvent::ParaFormatChanged {
            section: section_idx,
            para: para_idx,
        });
        Ok("{\"ok\":true}".to_string())
    }

    /// 각주 문단 정보를 반환한다.
    /// JSON: `{"ok":true,"paraCount":N,"textLen":N,"text":"..."}`
    pub fn get_footnote_info_native(
        &self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
    ) -> Result<String, HwpError> {
        let section = self.document.sections.get(section_idx).ok_or_else(|| {
            HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
        })?;
        let para = section
            .paragraphs
            .get(para_idx)
            .ok_or_else(|| HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", para_idx)))?;
        let ctrl = para.controls.get(control_idx).ok_or_else(|| {
            HwpError::RenderError(format!("컨트롤 인덱스 {} 범위 초과", control_idx))
        })?;
        match ctrl {
            Control::Footnote(f) => {
                let para_count = f.paragraphs.len();
                let texts: Vec<String> = f
                    .paragraphs
                    .iter()
                    .map(|p| p.text.replace('\\', "\\\\").replace('"', "\\\""))
                    .collect();
                let total_len: usize = f.paragraphs.iter().map(|p| p.text.chars().count()).sum();
                Ok(format!(
                    "{{\"ok\":true,\"paraCount\":{},\"totalTextLen\":{},\"number\":{},\"texts\":[{}]}}",
                    para_count,
                    total_len,
                    f.number,
                    texts.iter().map(|t| format!("\"{}\"", t)).collect::<Vec<_>>().join(","),
                ))
            }
            Control::Endnote(e) => {
                let para_count = e.paragraphs.len();
                let texts: Vec<String> = e
                    .paragraphs
                    .iter()
                    .map(|p| p.text.replace('\\', "\\\\").replace('"', "\\\""))
                    .collect();
                let total_len: usize = e.paragraphs.iter().map(|p| p.text.chars().count()).sum();
                Ok(format!(
                    "{{\"ok\":true,\"paraCount\":{},\"totalTextLen\":{},\"number\":{},\"texts\":[{}]}}",
                    para_count,
                    total_len,
                    e.number,
                    texts.iter().map(|t| format!("\"{}\"", t)).collect::<Vec<_>>().join(","),
                ))
            }
            _ => Err(HwpError::RenderError(format!(
                "컨트롤 {}은 각주/미주가 아닙니다",
                control_idx
            ))),
        }
    }

    /// 각주 내 텍스트 삽입
    pub fn insert_text_in_footnote_native(
        &mut self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
        fn_para_idx: usize,
        char_offset: usize,
        text: &str,
    ) -> Result<String, HwpError> {
        let new_chars_count = text.chars().count();
        let fn_para =
            self.get_footnote_paragraph_mut(section_idx, para_idx, control_idx, fn_para_idx)?;
        fn_para.insert_text_at(char_offset, text);

        self.reflow_footnote_paragraph(section_idx, para_idx, control_idx, fn_para_idx);

        self.document.sections[section_idx].raw_stream = None;
        self.mark_section_dirty(section_idx);
        self.paginate_if_needed();

        let new_offset = char_offset + new_chars_count;
        self.event_log.push(DocumentEvent::TextInserted {
            section: section_idx,
            para: para_idx,
            offset: char_offset,
            len: new_chars_count,
        });
        Ok(super::super::helpers::json_ok_with(&format!(
            "\"charOffset\":{}",
            new_offset
        )))
    }

    /// 각주 내 텍스트 삭제
    pub fn delete_text_in_footnote_native(
        &mut self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
        fn_para_idx: usize,
        char_offset: usize,
        count: usize,
    ) -> Result<String, HwpError> {
        let fn_para =
            self.get_footnote_paragraph_mut(section_idx, para_idx, control_idx, fn_para_idx)?;
        // [Task #2337] undo 재삽입용 삭제 텍스트 확보 (HF 와 동일 방식 — char 슬라이스로
        // delete_text_at 클램핑과 동일 범위). 역연산 삭제 커맨드가 사용한다.
        let deleted_text: String = fn_para.text.chars().skip(char_offset).take(count).collect();
        fn_para.delete_text_at(char_offset, count);

        self.reflow_footnote_paragraph(section_idx, para_idx, control_idx, fn_para_idx);

        self.document.sections[section_idx].raw_stream = None;
        self.mark_section_dirty(section_idx);
        self.paginate_if_needed();

        self.event_log.push(DocumentEvent::TextDeleted {
            section: section_idx,
            para: para_idx,
            offset: char_offset,
            count,
        });
        Ok(super::super::helpers::json_ok_with(&format!(
            "\"charOffset\":{},\"deletedText\":\"{}\"",
            char_offset,
            super::super::helpers::json_escape(&deleted_text)
        )))
    }

    /// 각주 내 문단 분할 (Enter 키)
    pub fn split_paragraph_in_footnote_native(
        &mut self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
        fn_para_idx: usize,
        char_offset: usize,
        restore_meta: Option<ParaMeta>,
    ) -> Result<String, HwpError> {
        // 문단 분할
        let mut new_para = {
            let section = self.document.sections.get_mut(section_idx).ok_or_else(|| {
                HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
            })?;
            let para = section.paragraphs.get_mut(para_idx).ok_or_else(|| {
                HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", para_idx))
            })?;
            let ctrl = para.controls.get_mut(control_idx).ok_or_else(|| {
                HwpError::RenderError(format!("컨트롤 인덱스 {} 범위 초과", control_idx))
            })?;
            match ctrl {
                Control::Footnote(f) => {
                    if fn_para_idx >= f.paragraphs.len() {
                        return Err(HwpError::RenderError(format!(
                            "각주 문단 인덱스 {} 범위 초과",
                            fn_para_idx
                        )));
                    }
                    f.paragraphs[fn_para_idx].split_at(char_offset)
                }
                Control::Endnote(e) => {
                    if fn_para_idx >= e.paragraphs.len() {
                        return Err(HwpError::RenderError(format!(
                            "미주 문단 인덱스 {} 범위 초과",
                            fn_para_idx
                        )));
                    }
                    e.paragraphs[fn_para_idx].split_at(char_offset)
                }
                _ => {
                    return Err(HwpError::RenderError(
                        "컨트롤이 각주/미주가 아닙니다".to_string(),
                    ))
                }
            }
        };
        if let Some(meta) = restore_meta {
            restore_para_meta(&mut new_para, meta, &self.document.doc_info.para_shapes);
        }

        // 새 문단 삽입
        let new_para_idx = fn_para_idx + 1;
        {
            let ctrl =
                &mut self.document.sections[section_idx].paragraphs[para_idx].controls[control_idx];
            match ctrl {
                Control::Footnote(f) => f.paragraphs.insert(new_para_idx, new_para),
                Control::Endnote(e) => e.paragraphs.insert(new_para_idx, new_para),
                _ => {}
            }
        }

        // 리플로우
        self.reflow_footnote_paragraph(section_idx, para_idx, control_idx, fn_para_idx);
        self.reflow_footnote_paragraph(section_idx, para_idx, control_idx, new_para_idx);

        self.document.sections[section_idx].raw_stream = None;
        self.mark_section_dirty(section_idx);
        self.paginate_if_needed();

        self.event_log.push(DocumentEvent::ParagraphSplit {
            section: section_idx,
            para: para_idx,
            offset: char_offset,
        });
        Ok(super::super::helpers::json_ok_with(&format!(
            "\"fnParaIndex\":{},\"charOffset\":0",
            new_para_idx
        )))
    }

    /// 각주 내 문단 병합 (Backspace at start)
    pub fn merge_paragraph_in_footnote_native(
        &mut self,
        section_idx: usize,
        para_idx: usize,
        control_idx: usize,
        fn_para_idx: usize,
    ) -> Result<String, HwpError> {
        if fn_para_idx == 0 {
            return Err(HwpError::RenderError(
                "첫 번째 문단은 이전 문단과 병합할 수 없습니다".to_string(),
            ));
        }

        let merge_offset;
        let removed_meta;
        {
            let section = self.document.sections.get_mut(section_idx).ok_or_else(|| {
                HwpError::RenderError(format!("구역 인덱스 {} 범위 초과", section_idx))
            })?;
            let para = section.paragraphs.get_mut(para_idx).ok_or_else(|| {
                HwpError::RenderError(format!("문단 인덱스 {} 범위 초과", para_idx))
            })?;
            let ctrl = para.controls.get_mut(control_idx).ok_or_else(|| {
                HwpError::RenderError(format!("컨트롤 인덱스 {} 범위 초과", control_idx))
            })?;
            match ctrl {
                Control::Footnote(f) => {
                    if fn_para_idx >= f.paragraphs.len() {
                        return Err(HwpError::RenderError(format!(
                            "각주 문단 인덱스 {} 범위 초과",
                            fn_para_idx
                        )));
                    }
                    merge_offset = f.paragraphs[fn_para_idx - 1].text.chars().count();
                    let removed = f.paragraphs.remove(fn_para_idx);
                    removed_meta =
                        super::super::helpers::removed_para_meta_field(&removed.capture_meta());
                    f.paragraphs[fn_para_idx - 1].merge_from(&removed);
                }
                Control::Endnote(e) => {
                    if fn_para_idx >= e.paragraphs.len() {
                        return Err(HwpError::RenderError(format!(
                            "미주 문단 인덱스 {} 범위 초과",
                            fn_para_idx
                        )));
                    }
                    merge_offset = e.paragraphs[fn_para_idx - 1].text.chars().count();
                    let removed = e.paragraphs.remove(fn_para_idx);
                    removed_meta =
                        super::super::helpers::removed_para_meta_field(&removed.capture_meta());
                    e.paragraphs[fn_para_idx - 1].merge_from(&removed);
                }
                _ => {
                    return Err(HwpError::RenderError(
                        "컨트롤이 각주/미주가 아닙니다".to_string(),
                    ))
                }
            }
        }

        let prev_idx = fn_para_idx - 1;
        self.reflow_footnote_paragraph(section_idx, para_idx, control_idx, prev_idx);

        self.document.sections[section_idx].raw_stream = None;
        self.mark_section_dirty(section_idx);
        self.paginate_if_needed();

        self.event_log.push(DocumentEvent::ParagraphMerged {
            section: section_idx,
            para: para_idx,
        });
        Ok(super::super::helpers::json_ok_with(&format!(
            "\"fnParaIndex\":{},\"charOffset\":{}{}",
            prev_idx, merge_offset, removed_meta
        )))
    }
}

#[cfg(test)]
mod tests {
    use crate::document_core::DocumentCore;
    use crate::model::control::Control;

    /// 본문 최상위(표/글상자 밖) 미주는 renumber_footnotes_in_section 의 바깥쪽 match 에
    /// Control::Endnote 분기가 없어 각주 삭제 후에도 번호가 갱신되지 않던 결함의 회귀 테스트.
    #[test]
    fn delete_footnote_renumbers_top_level_endnote_after_it() {
        let mut core = DocumentCore::new_empty();
        core.create_blank_document_native().unwrap();
        core.insert_text_native(0, 0, 0, "ab").unwrap();

        // 순서: 각주(offset 0) -> 미주(offset 1, 각주 삽입으로 +8 시프트됨)
        core.insert_footnote_native(0, 0, 0).unwrap();
        let endnote_offset = core.document.sections[0].paragraphs[0]
            .char_offsets
            .get(1)
            .copied()
            .unwrap() as usize;
        core.insert_endnote_native(0, 0, endnote_offset).unwrap();

        let footnote_ctrl_idx = core.document.sections[0].paragraphs[0]
            .controls
            .iter()
            .position(|c| matches!(c, Control::Footnote(_)))
            .expect("본문에 각주 컨트롤이 있어야 함");
        core.delete_footnote_native(0, 0, footnote_ctrl_idx)
            .unwrap();

        let remaining_endnote_number = core.document.sections[0].paragraphs[0]
            .controls
            .iter()
            .find_map(|c| match c {
                Control::Endnote(e) => Some(e.number),
                _ => None,
            })
            .expect("본문에 미주 컨트롤이 남아 있어야 함");

        assert_eq!(
            remaining_endnote_number, 1,
            "각주 삭제 후 본문 최상위 미주 번호가 1로 재계산되어야 함"
        );
    }

    /// 각주 문단 병합의 undo 가 사라진 문단의 스코프 메타데이터를 되돌리는지 (Task #2342).
    #[test]
    fn merge_paragraph_in_footnote_undo_restores_removed_paragraph_meta() {
        use crate::document_core::helpers::removed_para_meta_of;
        use crate::model::paragraph::NumberingRestart;

        let mut core = DocumentCore::new_empty();
        core.create_blank_document_native().unwrap();
        core.insert_text_native(0, 0, 0, "ab").unwrap();
        core.insert_footnote_native(0, 0, 0).unwrap();
        let ctrl_idx = core.document.sections[0].paragraphs[0]
            .controls
            .iter()
            .position(|c| matches!(c, Control::Footnote(_)))
            .expect("각주 컨트롤");

        core.insert_text_in_footnote_native(0, 0, ctrl_idx, 0, 0, "HelloWorld")
            .unwrap();
        core.split_paragraph_in_footnote_native(0, 0, ctrl_idx, 0, 5, None)
            .unwrap();

        let second = core.get_footnote_paragraph_mut(0, 0, ctrl_idx, 1).unwrap();
        second.para_shape_id = 20;
        second.style_id = 5;
        second.numbering_restart = Some(NumberingRestart::NewStart(3));
        second.raw_header_extra = vec![0, 0, 0, 0, 0, 0, 0xBB, 0xBB, 0xBB, 0xBB];
        let text_before_merge = second.text.clone();

        let merged = core
            .merge_paragraph_in_footnote_native(0, 0, ctrl_idx, 1)
            .unwrap();
        let meta = removed_para_meta_of(&merged);
        core.split_paragraph_in_footnote_native(0, 0, ctrl_idx, 0, 5, Some(meta))
            .unwrap();

        let restored = core.get_footnote_paragraph_mut(0, 0, ctrl_idx, 1).unwrap();
        assert_eq!(restored.text, text_before_merge);
        assert_eq!(restored.para_shape_id, 20);
        assert_eq!(restored.style_id, 5);
        assert_eq!(
            restored.numbering_restart,
            Some(NumberingRestart::NewStart(3))
        );
        assert_eq!(
            restored.raw_header_extra,
            vec![0, 0, 0, 0, 0, 0, 0xBB, 0xBB, 0xBB, 0xBB]
        );
    }
}

#[cfg(test)]
mod delete_footnote_char_shape_tests {
    use crate::document_core::DocumentCore;
    use crate::model::paragraph::CharShapeRef;

    /// [reference-integrity 회귀] 각주 삭제 후 char_offsets 만 -8 시프트하고
    /// char_shapes.start_pos 는 그대로 두면, 삭제 지점 이후 글자모양 run 경계가
    /// 텍스트와 어긋난다(삽입측 shift_for_inline_control_insert 와 비대칭).
    #[test]
    fn delete_footnote_shifts_char_shapes_back_with_char_offsets() {
        let mut core = DocumentCore::new_empty();
        core.create_blank_document_native().unwrap();
        core.insert_text_native(0, 0, 0, "abcd").unwrap();
        core.insert_footnote_native(0, 0, 2).unwrap();

        // 각주 삽입 후 'c'(char idx 뒤쪽, 각주 마커 이후)의 글자모양을 별도 run(id=9)으로 지정.
        let (control_idx, marker_pos) = {
            let para = &core.document.sections[0].paragraphs[0];
            let positions = crate::document_core::helpers::find_control_text_positions(para);
            let ci = para
                .controls
                .iter()
                .position(|c| matches!(c, crate::model::control::Control::Footnote(_)))
                .expect("각주 컨트롤");
            (ci, positions[ci])
        };
        {
            let para = &mut core.document.sections[0].paragraphs[0];
            para.char_shapes.push(CharShapeRef {
                start_pos: marker_pos as u32 + 8,
                char_shape_id: 9,
            });
        }

        core.delete_footnote_native(0, 0, control_idx).unwrap();

        let para = &core.document.sections[0].paragraphs[0];
        assert_eq!(
            para.char_shape_id_at(2),
            Some(9),
            "각주 삭제 후 char_shapes.start_pos 가 되돌아가지 않아 글자모양 run 이 텍스트와 어긋남"
        );
    }
}