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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
//! 문서 트리 DFS 기반 캐럿 네비게이션
//!
//! HWP 문서의 계층 구조(Section → Paragraph → Control → 내부 Paragraph → ...)를
//! DFS로 순회하여 다음/이전 편집 가능 위치를 찾는다.
use crate::document_core::helpers::{
find_logical_control_positions, get_textbox_from_shape, is_treat_as_char_object_control,
navigable_text_len,
};
use crate::document_core::DocumentCore;
use crate::model::control::Control;
use crate::model::document::Section;
use crate::model::paragraph::Paragraph;
use crate::model::shape::ShapeObject;
/// 글상자 오버플로우 연결 정보 (소스 → 타겟)
#[derive(Debug, Clone)]
pub struct OverflowLink {
/// 소스 글상자가 속한 문단 인덱스 (섹션 내)
pub source_parent_para: usize,
/// 소스 컨트롤 인덱스
pub source_ctrl_idx: usize,
/// 소스에서 오버플로우 시작 문단 인덱스
pub overflow_start: usize,
/// 타겟 글상자가 속한 문단 인덱스
pub target_parent_para: usize,
/// 타겟 컨트롤 인덱스
pub target_ctrl_idx: usize,
}
/// 문서 트리 내 현재 위치를 나타내는 컨텍스트 스택의 한 단계
#[derive(Debug, Clone)]
pub struct NavContextEntry {
/// 이 컨트롤이 속한 문단 인덱스 (부모 컨텍스트 기준)
pub parent_para: usize,
/// 부모 문단 내 컨트롤 인덱스
pub ctrl_idx: usize,
/// 부모 문단 텍스트에서 이 컨트롤의 charOffset 위치
pub ctrl_text_pos: usize,
/// Table: 셀 인덱스 / TextBox: 0
pub cell_idx: usize,
/// true=Shape+TextBox, false=Table
pub is_textbox: bool,
}
/// DFS 순회 결과
#[derive(Debug)]
pub enum NavResult {
/// 편집 가능한 텍스트 위치
Text {
sec: usize,
para: usize,
char_offset: usize,
context: Vec<NavContextEntry>,
},
/// 문서 경계 (이동 불가)
Boundary,
}
/// 컨트롤이 편집 가능(네비게이션 가능)한지 판별.
/// Some(true)=TextBox, Some(false)=Table/인라인 개체/각주 마커, None=건너뜀
///
/// 텍스트가 전혀 없는 빈 글상자(장식용 프레임 등)는 건너뛴다.
/// CharOverlap(글자겹침)은 1글자 단위로 건너뛴다 (표와 동일 취급).
fn classify_navigable(ctrl: &Control) -> Option<bool> {
match ctrl {
Control::Shape(s) => {
if let Some(tb) = get_textbox_from_shape(s.as_ref()) {
// TextBox가 있고 텍스트가 비어있지 않으면 글상자 진입
if !tb.paragraphs.iter().all(|p| p.text.is_empty()) {
return Some(true);
}
}
// TextBox 없거나 비어있는 TAC 도형만 표처럼 1칸 건너뛰기
s.common().treat_as_char.then_some(false)
}
Control::Table(_) | Control::Picture(_) | Control::Equation(_) | Control::Form(_)
if is_treat_as_char_object_control(ctrl) =>
{
Some(false)
}
Control::Footnote(_) => Some(false),
Control::Endnote(_) => Some(false),
// CharOverlap은 layout에서 char_count=1로 처리되므로
// 별도의 건너뛰기 없이 일반 문자처럼 1칸 이동
Control::CharOverlap(_) => None,
_ => None,
}
}
fn is_equation_only_navigation_paragraph(para: &Paragraph) -> bool {
let mut has_equation = false;
for ctrl in ¶.controls {
match ctrl {
Control::Equation(_) if is_treat_as_char_object_control(ctrl) => has_equation = true,
_ if classify_navigable(ctrl).is_some() => return false,
_ => {}
}
}
has_equation && para.text.chars().all(|ch| ch == '\u{fffc}')
}
fn equation_only_virtual_last_offset(para: &Paragraph) -> Option<usize> {
if !is_equation_only_navigation_paragraph(para) {
return None;
}
let equation_count = para
.controls
.iter()
.filter(|ctrl| {
matches!(ctrl, Control::Equation(_)) && is_treat_as_char_object_control(ctrl)
})
.count();
if equation_count == 0 {
None
} else {
Some(equation_count * 2 - 1)
}
}
fn equation_only_next_offset(para: &Paragraph, char_offset: usize, forward: bool) -> Option<usize> {
let last_offset = equation_only_virtual_last_offset(para)?;
if forward {
if char_offset >= last_offset {
return None;
}
let next = if char_offset.is_multiple_of(2) {
char_offset + 1
} else {
char_offset + 2
};
Some(next.min(last_offset))
} else {
if char_offset == 0 {
return None;
}
let prev = if char_offset % 2 == 1 {
char_offset - 1
} else {
char_offset.saturating_sub(2)
};
Some(prev)
}
}
fn equation_control_at_position(
para: &Paragraph,
ctrl_positions: &[usize],
logical_pos: usize,
) -> bool {
para.controls.iter().enumerate().any(|(ci, ctrl)| {
matches!(ctrl, Control::Equation(_))
&& is_treat_as_char_object_control(ctrl)
&& ctrl_positions.get(ci).copied() == Some(logical_pos)
})
}
fn should_skip_equation_only_duplicate_boundary(
para: &Paragraph,
ctrl_positions: &[usize],
char_offset: usize,
next_offset: usize,
text_len: usize,
) -> bool {
if char_offset == 0
|| next_offset + 1 > text_len
|| !is_equation_only_navigation_paragraph(para)
{
return false;
}
let previous_equation_ends_here = para.controls.iter().enumerate().any(|(ci, ctrl)| {
matches!(ctrl, Control::Equation(_))
&& is_treat_as_char_object_control(ctrl)
&& ctrl_positions
.get(ci)
.copied()
.is_some_and(|pos| pos + 1 == char_offset)
});
let current_equation_starts_here =
equation_control_at_position(para, ctrl_positions, char_offset);
let next_equation_starts_here = equation_control_at_position(para, ctrl_positions, next_offset);
previous_equation_ends_here && (current_equation_starts_here || next_equation_starts_here)
}
/// context 스택을 따라 현재 컨테이너의 paragraphs를 반환한다.
/// 오버플로우 타겟 글상자의 경우 소스의 오버플로우 문단 슬라이스를 반환한다.
fn resolve_paragraphs<'a>(
sections: &'a [Section],
sec: usize,
context: &[NavContextEntry],
overflow_links: &[OverflowLink],
) -> Option<&'a [Paragraph]> {
let section_paras: &[Paragraph] = §ions.get(sec)?.paragraphs;
let mut paragraphs: &[Paragraph] = section_paras;
for (depth, entry) in context.iter().enumerate() {
let para = paragraphs.get(entry.parent_para)?;
let ctrl = para.controls.get(entry.ctrl_idx)?;
if entry.is_textbox {
if let Control::Shape(s) = ctrl {
let tb = super::super::helpers::get_textbox_at(s.as_ref(), entry.cell_idx)?;
// 마지막 컨텍스트 entry가 빈 글상자이면 오버플로우 타겟인지 확인
if depth == context.len() - 1 && tb.paragraphs.iter().all(|p| p.text.is_empty()) {
if let Some(link) = overflow_links.iter().find(|l| {
l.target_parent_para == entry.parent_para
&& l.target_ctrl_idx == entry.ctrl_idx
}) {
// 소스 글상자의 오버플로우 문단 반환
let src_para = section_paras.get(link.source_parent_para)?;
let src_ctrl = src_para.controls.get(link.source_ctrl_idx)?;
if let Control::Shape(src_s) = src_ctrl {
let src_tb = get_textbox_from_shape(src_s.as_ref())?;
if link.overflow_start < src_tb.paragraphs.len() {
return Some(&src_tb.paragraphs[link.overflow_start..]);
}
}
}
}
paragraphs = &tb.paragraphs;
} else if let Control::Picture(pic) = ctrl {
// Picture 캡션 내부 텍스트 편집
let cap = pic.caption.as_ref()?;
paragraphs = &cap.paragraphs;
} else {
return None;
}
} else {
if let Control::Table(t) = ctrl {
let cell = t.cells.get(entry.cell_idx)?;
paragraphs = &cell.paragraphs;
} else {
return None;
}
}
}
Some(paragraphs)
}
/// Table의 reading order로 다음 셀 인덱스를 반환한다.
/// 행 우선 순서 (row 0 col 0, row 0 col 1, ..., row 1 col 0, ...)
fn next_cell_index(
table: &crate::model::table::Table,
current_cell_idx: usize,
forward: bool,
) -> Option<usize> {
if table.cells.is_empty() {
return None;
}
if forward {
let next = current_cell_idx + 1;
if next < table.cells.len() {
Some(next)
} else {
None
}
} else {
if current_cell_idx > 0 {
Some(current_cell_idx - 1)
} else {
None
}
}
}
impl DocumentCore {
fn nav_paragraph_ref<'a>(
&'a self,
sec: usize,
para: usize,
context: &[NavContextEntry],
overflow_links: &[OverflowLink],
) -> Option<&'a Paragraph> {
if context.is_empty() {
self.get_render_paragraph_ref(sec, para).ok()
} else {
resolve_paragraphs(&self.document.sections, sec, context, overflow_links)?.get(para)
}
}
fn nav_paragraph_count(
&self,
sec: usize,
context: &[NavContextEntry],
overflow_links: &[OverflowLink],
) -> Option<usize> {
if context.is_empty() {
let body_count = self.document.sections.get(sec)?.paragraphs.len();
let endnote_count = self
.pagination
.get(sec)
.map(|pagination| pagination.endnote_paragraphs.len())
.unwrap_or(0);
Some(body_count + endnote_count)
} else {
Some(resolve_paragraphs(&self.document.sections, sec, context, overflow_links)?.len())
}
}
/// 외부에서 전달받은 context의 ctrl_text_pos를 실제 문서 데이터로 복원한다.
/// TypeScript 측에서 ctrl_text_pos를 모르는 경우 0으로 전달하므로,
/// Rust가 편집용 logical control position 으로 올바른 값을 채운다.
pub(crate) fn fix_context_text_positions(
sections: &[Section],
sec: usize,
context: &[NavContextEntry],
) -> Vec<NavContextEntry> {
if context.is_empty() {
return Vec::new();
}
let mut fixed = Vec::with_capacity(context.len());
let mut paragraphs: &[Paragraph] = match sections.get(sec) {
Some(s) => &s.paragraphs,
None => return context.to_vec(),
};
for entry in context {
let ctrl_text_pos = if let Some(para) = paragraphs.get(entry.parent_para) {
let positions = find_logical_control_positions(para);
positions
.get(entry.ctrl_idx)
.copied()
.unwrap_or(entry.ctrl_text_pos)
} else {
entry.ctrl_text_pos
};
fixed.push(NavContextEntry {
parent_para: entry.parent_para,
ctrl_idx: entry.ctrl_idx,
ctrl_text_pos,
cell_idx: entry.cell_idx,
is_textbox: entry.is_textbox,
});
// 다음 depth를 위해 현재 컨트롤 내부 paragraphs로 이동
if let Some(para) = paragraphs.get(entry.parent_para) {
if let Some(ctrl) = para.controls.get(entry.ctrl_idx) {
if entry.is_textbox {
if let Control::Shape(s) = ctrl {
if let Some(tb) =
super::super::helpers::get_textbox_at(s.as_ref(), entry.cell_idx)
{
paragraphs = &tb.paragraphs;
}
}
} else {
if let Control::Table(t) = ctrl {
if let Some(cell) = t.cells.get(entry.cell_idx) {
paragraphs = &cell.paragraphs;
}
}
}
}
}
}
fixed
}
/// 문서 트리 DFS 기반 다음/이전 편집 가능 위치를 반환한다.
///
/// - `sec`: 현재 섹션 인덱스
/// - `para`: 현재 컨텍스트 내 문단 인덱스
/// - `char_offset`: 현재 문자 오프셋
/// - `delta`: +1(forward) 또는 -1(backward)
/// - `context`: 현재 컨텍스트 스택 (빈 배열 = body)
/// - `max_para`: 현재 컨테이너에서 렌더링된 마지막 문단 인덱스 (None이면 제한 없음)
/// - `overflow_links`: 글상자 오버플로우 연결 정보
pub(crate) fn navigate_next_editable(
&self,
sec: usize,
para: usize,
char_offset: usize,
delta: i32,
context: &[NavContextEntry],
max_para: Option<usize>,
overflow_links: &[OverflowLink],
) -> NavResult {
let sections = &self.document.sections;
let forward = delta > 0;
let para_count = match self.nav_paragraph_count(sec, context, overflow_links) {
Some(count) if count > 0 => count,
_ => return NavResult::Boundary,
};
// Step 1: 현재 문단 내 탐색
if let Some(current_para) = self.nav_paragraph_ref(sec, para, context, overflow_links) {
let text_len = navigable_text_len(current_para);
let ctrl_positions = find_logical_control_positions(current_para);
if forward {
// Forward: char_offset 이후의 컨트롤 또는 텍스트 끝 확인
let next_offset = char_offset + 1;
if let Some(next) = equation_only_next_offset(current_para, char_offset, true) {
return NavResult::Text {
sec,
para,
char_offset: next,
context: context.to_vec(),
};
}
if should_skip_equation_only_duplicate_boundary(
current_para,
&ctrl_positions,
char_offset,
next_offset,
text_len,
) {
return NavResult::Text {
sec,
para,
char_offset: next_offset + 1,
context: context.to_vec(),
};
}
// char_offset 위치 또는 그 이후에 있는 편집 가능 컨트롤 탐색
for (ci, ctrl) in current_para.controls.iter().enumerate() {
let cpos = ctrl_positions.get(ci).copied().unwrap_or(text_len);
if cpos < next_offset {
continue; // 이미 지나친 컨트롤
}
if cpos == char_offset {
// 현재 위치에 컨트롤이 있음
if let Some(is_tb) = classify_navigable(ctrl) {
if is_tb {
// 글상자: 진입
return self.enter_control_forward(
sec,
para,
ci,
cpos,
is_tb,
context,
overflow_links,
);
}
// 도형/표/그림/수식: 컨트롤 다음 위치로 이동 (글자처럼 1칸)
let next = cpos + 1;
if next <= text_len {
return NavResult::Text {
sec,
para,
char_offset: next,
context: context.to_vec(),
};
}
// 문단 끝 넘어감 → Step 2로
break;
}
// 편집 불가 컨트롤 → 건너뜀 (다음 offset으로 이동)
// 계속 다음 컨트롤 탐색
}
if cpos > char_offset && cpos < next_offset.min(text_len) {
// char_offset과 next_offset 사이에 컨트롤이 있음
if let Some(is_tb) = classify_navigable(ctrl) {
if is_tb {
return self.enter_control_forward(
sec,
para,
ci,
cpos,
is_tb,
context,
overflow_links,
);
}
// 표: 건너뛰기
let skip = cpos + 1;
if skip <= text_len {
return NavResult::Text {
sec,
para,
char_offset: skip,
context: context.to_vec(),
};
}
break;
}
}
}
for (ci, ctrl) in current_para.controls.iter().enumerate() {
let cpos = ctrl_positions.get(ci).copied().unwrap_or(text_len);
if cpos == next_offset {
if let Some(is_tb) = classify_navigable(ctrl) {
if is_tb {
return self.enter_control_forward(
sec,
para,
ci,
cpos,
is_tb,
context,
overflow_links,
);
}
// 도형/표: next_offset을 반환 (getCursorRect에서 적절히 처리)
return NavResult::Text {
sec,
para,
char_offset: next_offset,
context: context.to_vec(),
};
}
}
}
// 텍스트가 남아있으면 다음 charOffset 반환
if next_offset <= text_len {
return NavResult::Text {
sec,
para,
char_offset: next_offset,
context: context.to_vec(),
};
}
// 문단 끝 도달 → Step 2
} else {
// Backward: char_offset 이전의 컨트롤 또는 텍스트 시작 확인
if let Some(prev) = equation_only_next_offset(current_para, char_offset, false) {
return NavResult::Text {
sec,
para,
char_offset: prev,
context: context.to_vec(),
};
}
if char_offset == 0 {
// 문단 시작 도달 → Step 2 (이전 문단/컨테이너)
} else {
let prev_offset = char_offset - 1;
// prev_offset 위치에 컨트롤이 있는지 역순 탐색
for (ci, ctrl) in current_para.controls.iter().enumerate().rev() {
let cpos = ctrl_positions.get(ci).copied().unwrap_or(text_len);
if cpos == char_offset {
// 현재 위치에 컨트롤 → 건너뛰기 (이미 이 위치에 있으므로)
if let Some(is_tb) = classify_navigable(ctrl) {
if is_tb {
return self.enter_control_backward(
sec,
para,
ci,
cpos,
is_tb,
context,
overflow_links,
);
}
// 도형/표/그림: 컨트롤 앞으로 건너뛰기
if cpos > 0 {
return NavResult::Text {
sec,
para,
char_offset: cpos - 1,
context: context.to_vec(),
};
}
break;
}
}
if cpos == prev_offset {
// 이전 위치에 컨트롤 → 이 위치에서 멈춤
if let Some(is_tb) = classify_navigable(ctrl) {
if is_tb {
return self.enter_control_backward(
sec,
para,
ci,
cpos,
is_tb,
context,
overflow_links,
);
}
return NavResult::Text {
sec,
para,
char_offset: cpos,
context: context.to_vec(),
};
}
}
}
// prev_offset 위치 또는 그 이전 컨트롤 탐색 (사이에 있는 경우)
for (ci, ctrl) in current_para.controls.iter().enumerate().rev() {
let cpos = ctrl_positions.get(ci).copied().unwrap_or(text_len);
if cpos > prev_offset && cpos < char_offset {
if let Some(is_tb) = classify_navigable(ctrl) {
if is_tb {
return self.enter_control_backward(
sec,
para,
ci,
cpos,
is_tb,
context,
overflow_links,
);
}
// 표: 건너뛰기
if cpos > 0 {
return NavResult::Text {
sec,
para,
char_offset: cpos - 1,
context: context.to_vec(),
};
}
break;
}
}
}
// 텍스트 이동
return NavResult::Text {
sec,
para,
char_offset: prev_offset,
context: context.to_vec(),
};
}
}
}
// Step 2: 같은 컨테이너의 다음/이전 문단
// max_para가 설정된 경우, 렌더링된 범위를 넘어서는 문단으로 이동하지 않는다.
// 렌더 범위 상한: max_para가 있으면 그 이후 문단은 진입하지 않음
let effective_para_limit = max_para.map_or(para_count, |mp| (mp + 1).min(para_count));
if forward {
if para + 1 < effective_para_limit {
return self.navigate_to_para_start(sec, para + 1, context, overflow_links);
}
// 렌더 범위 끝 도달 → 오버플로우 링크 확인
// 현재 소스 글상자의 오버플로우 타겟이 있으면 타겟으로 이동
if !context.is_empty() && max_para.is_some() {
let last = &context[context.len() - 1];
if last.is_textbox {
if let Some(link) = overflow_links.iter().find(|l| {
l.source_parent_para == last.parent_para
&& l.source_ctrl_idx == last.ctrl_idx
}) {
// 타겟 글상자 컨텍스트로 전환 (para 0부터 시작)
let mut target_ctx = context[..context.len() - 1].to_vec();
let ctrl_text_pos = last.ctrl_text_pos; // 같은 텍스트 위치 유지
target_ctx.push(NavContextEntry {
parent_para: link.target_parent_para,
ctrl_idx: link.target_ctrl_idx,
ctrl_text_pos,
cell_idx: 0,
is_textbox: true,
});
return self.navigate_to_para_start(sec, 0, &target_ctx, overflow_links);
}
}
}
// 렌더 범위 끝 → Step 3 (컨테이너 탈출)으로 진행
} else {
if para > 0 {
return self.navigate_to_para_end(sec, para - 1, context, overflow_links);
}
// 문단 시작 → 오버플로우 타겟에서 backward 시 소스로 복귀
if !context.is_empty() {
let last = &context[context.len() - 1];
if last.is_textbox {
if let Some(link) = overflow_links.iter().find(|l| {
l.target_parent_para == last.parent_para
&& l.target_ctrl_idx == last.ctrl_idx
}) {
// 소스 글상자의 마지막 렌더 문단 끝으로 복귀
let mut source_ctx = context[..context.len() - 1].to_vec();
let ctrl_text_pos = last.ctrl_text_pos;
source_ctx.push(NavContextEntry {
parent_para: link.source_parent_para,
ctrl_idx: link.source_ctrl_idx,
ctrl_text_pos,
cell_idx: 0,
is_textbox: true,
});
// overflow_start - 1 = 소스에서 마지막으로 렌더링된 문단
let last_rendered = link.overflow_start.saturating_sub(1);
return self.navigate_to_para_end(
sec,
last_rendered,
&source_ctx,
overflow_links,
);
}
}
}
// 문단 시작 도달 → Step 3 (컨테이너 탈출)으로 진행
}
// Step 3: 컨테이너 탈출
if !context.is_empty() {
let mut parent_ctx = context.to_vec();
let last = parent_ctx.pop().unwrap();
if last.is_textbox {
// TextBox 탈출: 컨트롤 위치를 기준으로 바로 다음/이전 위치 반환
// (navigate_next_editable 재귀 호출하면 같은 컨트롤 재진입 위험)
return self.exit_control(sec, &last, forward, &parent_ctx, overflow_links);
} else {
// Table 탈출: 다음/이전 셀 확인
let parent_para = match self.nav_paragraph_ref(
sec,
last.parent_para,
&parent_ctx,
overflow_links,
) {
Some(p) => p,
None => return NavResult::Boundary,
};
if let Some(Control::Table(table)) = parent_para.controls.get(last.ctrl_idx) {
if let Some(next_cell) = next_cell_index(table, last.cell_idx, forward) {
let mut new_ctx = parent_ctx.clone();
new_ctx.push(NavContextEntry {
parent_para: last.parent_para,
ctrl_idx: last.ctrl_idx,
ctrl_text_pos: last.ctrl_text_pos,
cell_idx: next_cell,
is_textbox: false,
});
if forward {
return self.navigate_to_para_start(sec, 0, &new_ctx, overflow_links);
} else {
let cell_para_count = table
.cells
.get(next_cell)
.map(|c| c.paragraphs.len())
.unwrap_or(1);
return self.navigate_to_para_end(
sec,
cell_para_count.saturating_sub(1),
&new_ctx,
overflow_links,
);
}
}
// 모든 셀 소진 → 표 탈출 (컨트롤 재진입 방지)
return self.exit_control(sec, &last, forward, &parent_ctx, overflow_links);
}
return NavResult::Boundary;
}
}
// Step 4: Body 수준 — 다음/이전 섹션
if forward {
if sec + 1 < sections.len() {
return self.navigate_to_para_start(sec + 1, 0, &[], overflow_links);
}
} else {
if sec > 0 {
let prev_sec = sec - 1;
let prev_para_count = self
.nav_paragraph_count(prev_sec, &[], overflow_links)
.unwrap_or(0);
return self.navigate_to_para_end(
prev_sec,
prev_para_count.saturating_sub(1),
&[],
overflow_links,
);
}
}
NavResult::Boundary
}
/// 문단 시작으로 이동 (forward 방향으로 진입 시 사용)
/// 문단 시작(offset=0)에 컨트롤이 있으면 자동 진입
fn navigate_to_para_start(
&self,
sec: usize,
para: usize,
context: &[NavContextEntry],
overflow_links: &[OverflowLink],
) -> NavResult {
if let Some(current_para) = self.nav_paragraph_ref(sec, para, context, overflow_links) {
let text_len = navigable_text_len(current_para);
let ctrl_positions = find_logical_control_positions(current_para);
// offset 0에 컨트롤이 있는지 확인
for (ci, ctrl) in current_para.controls.iter().enumerate() {
let cpos = ctrl_positions.get(ci).copied().unwrap_or(text_len);
if cpos == 0 {
if let Some(is_tb) = classify_navigable(ctrl) {
if is_tb {
// 글상자: 진입
return self.enter_control_forward(
sec,
para,
ci,
0,
is_tb,
context,
overflow_links,
);
}
// 문단 경계에서 새 문단으로 진입할 때는 시작 위치에 멈춘다.
// 수식/그림/표 같은 inline control 은 다음 오른쪽 이동에서 1글자처럼 소비된다.
return NavResult::Text {
sec,
para,
char_offset: 0,
context: context.to_vec(),
};
}
}
if cpos > 0 {
break; // 위치 0 이후 → 중단
}
}
}
NavResult::Text {
sec,
para,
char_offset: 0,
context: context.to_vec(),
}
}
/// 문단 끝으로 이동 (backward 방향으로 진입 시 사용)
/// 문단 끝에 컨트롤이 있으면 자동 진입
fn navigate_to_para_end(
&self,
sec: usize,
para: usize,
context: &[NavContextEntry],
overflow_links: &[OverflowLink],
) -> NavResult {
if let Some(current_para) = self.nav_paragraph_ref(sec, para, context, overflow_links) {
let text_len = navigable_text_len(current_para);
let ctrl_positions = find_logical_control_positions(current_para);
// 문단 끝에 컨트롤이 있는지 역순 확인
for (ci, ctrl) in current_para.controls.iter().enumerate().rev() {
let cpos = ctrl_positions.get(ci).copied().unwrap_or(text_len);
if cpos == text_len {
if let Some(is_tb) = classify_navigable(ctrl) {
if is_tb {
// 글상자: 진입
return self.enter_control_backward(
sec,
para,
ci,
cpos,
is_tb,
context,
overflow_links,
);
}
// 표: 건너뛰기 → 표 앞 위치로
// (text_len 위치에 표가 있으므로 text_len은 표 FFFC 뒤 = 실제 텍스트 끝)
// backward 진입이므로 text_len 위치에서 멈춤 (표 뒤)
}
}
if cpos < text_len {
break;
}
}
return NavResult::Text {
sec,
para,
char_offset: text_len,
context: context.to_vec(),
};
}
NavResult::Text {
sec,
para,
char_offset: 0,
context: context.to_vec(),
}
}
/// 컨트롤에 forward 방향으로 진입 (내부 첫 위치)
fn enter_control_forward(
&self,
sec: usize,
para: usize,
ctrl_idx: usize,
ctrl_text_pos: usize,
is_textbox: bool,
context: &[NavContextEntry],
overflow_links: &[OverflowLink],
) -> NavResult {
let mut new_ctx = context.to_vec();
if is_textbox {
new_ctx.push(NavContextEntry {
parent_para: para,
ctrl_idx,
ctrl_text_pos,
cell_idx: 0,
is_textbox: true,
});
self.navigate_to_para_start(sec, 0, &new_ctx, overflow_links)
} else {
// Table: 첫 셀로 진입
new_ctx.push(NavContextEntry {
parent_para: para,
ctrl_idx,
ctrl_text_pos,
cell_idx: 0,
is_textbox: false,
});
self.navigate_to_para_start(sec, 0, &new_ctx, overflow_links)
}
}
/// 컨트롤에 backward 방향으로 진입 (내부 마지막 위치)
fn enter_control_backward(
&self,
sec: usize,
para: usize,
ctrl_idx: usize,
ctrl_text_pos: usize,
is_textbox: bool,
context: &[NavContextEntry],
overflow_links: &[OverflowLink],
) -> NavResult {
let sections = &self.document.sections;
if is_textbox {
let mut new_ctx = context.to_vec();
new_ctx.push(NavContextEntry {
parent_para: para,
ctrl_idx,
ctrl_text_pos,
cell_idx: 0,
is_textbox: true,
});
// 이 글상자가 오버플로우 소스이면, 타겟의 마지막 문단 끝으로 진입
if let Some(link) = overflow_links
.iter()
.find(|l| l.source_parent_para == para && l.source_ctrl_idx == ctrl_idx)
{
let mut target_ctx = context.to_vec();
target_ctx.push(NavContextEntry {
parent_para: link.target_parent_para,
ctrl_idx: link.target_ctrl_idx,
ctrl_text_pos,
cell_idx: 0,
is_textbox: true,
});
let target_paras =
match resolve_paragraphs(sections, sec, &target_ctx, overflow_links) {
Some(p) => p,
None => return NavResult::Boundary,
};
let last_para = target_paras.len().saturating_sub(1);
return self.navigate_to_para_end(sec, last_para, &target_ctx, overflow_links);
}
// TextBox 내부의 마지막 문단 끝으로
let inner_paras = match resolve_paragraphs(sections, sec, &new_ctx, overflow_links) {
Some(p) => p,
None => return NavResult::Boundary,
};
let last_para = inner_paras.len().saturating_sub(1);
self.navigate_to_para_end(sec, last_para, &new_ctx, overflow_links)
} else {
// Table: 마지막 셀로 진입
let parent_para = match self.nav_paragraph_ref(sec, para, context, overflow_links) {
Some(p) => p,
None => return NavResult::Boundary,
};
if let Some(Control::Table(table)) = parent_para.controls.get(ctrl_idx) {
let last_cell = table.cells.len().saturating_sub(1);
let mut new_ctx = context.to_vec();
new_ctx.push(NavContextEntry {
parent_para: para,
ctrl_idx,
ctrl_text_pos,
cell_idx: last_cell,
is_textbox: false,
});
let cell_para_count = table
.cells
.get(last_cell)
.map(|c| c.paragraphs.len())
.unwrap_or(1);
return self.navigate_to_para_end(
sec,
cell_para_count.saturating_sub(1),
&new_ctx,
overflow_links,
);
}
NavResult::Boundary
}
}
/// 컨트롤 탈출 시 부모 문단에서 다음/이전 위치를 직접 반환한다.
/// navigate_next_editable 재귀 호출을 피하여 같은 컨트롤 재진입을 방지한다.
///
/// - Forward 탈출: ctrl_text_pos 위치 반환 (컨트롤 직후의 텍스트 문자)
/// - Backward 탈출: ctrl_text_pos - 1 위치 반환 (컨트롤 직전의 텍스트 문자)
///
/// 오버플로우 타겟 탈출 시:
/// - Forward: 소스 컨트롤을 건너뛰고 다음 편집 가능 컨트롤 진입
/// - Backward: (Step 2에서 처리 — 여기에 도달하지 않음)
fn exit_control(
&self,
sec: usize,
exited: &NavContextEntry,
forward: bool,
parent_ctx: &[NavContextEntry],
overflow_links: &[OverflowLink],
) -> NavResult {
let sections = &self.document.sections;
// 오버플로우 타겟 탈출인지 확인
let target_link = overflow_links.iter().find(|l| {
l.target_parent_para == exited.parent_para && l.target_ctrl_idx == exited.ctrl_idx
});
if forward {
// 부모 문단의 텍스트 길이 확인
let parent_p =
match self.nav_paragraph_ref(sec, exited.parent_para, parent_ctx, overflow_links) {
Some(p) => p,
None => return NavResult::Boundary,
};
let text_len = navigable_text_len(parent_p);
if exited.ctrl_text_pos <= text_len {
// 컨트롤 다음 위치에 다른 편집 가능 컨트롤이 있는지 확인
let ctrl_positions = find_logical_control_positions(parent_p);
// 오버플로우 타겟 탈출 시 소스 컨트롤 건너뛰기
let skip_ctrl_idx = target_link.map(|l| l.source_ctrl_idx);
for (ci, ctrl) in parent_p.controls.iter().enumerate() {
if ci <= exited.ctrl_idx {
continue; // 같은 컨트롤이거나 이미 지나친 것
}
// 소스 컨트롤 건너뛰기
if Some(ci) == skip_ctrl_idx {
continue;
}
let cpos = ctrl_positions.get(ci).copied().unwrap_or(text_len);
if cpos == exited.ctrl_text_pos {
// 같은 위치에 다른 편집 가능 컨트롤 → 진입
if let Some(is_tb) = classify_navigable(ctrl) {
return self.enter_control_forward(
sec,
exited.parent_para,
ci,
cpos,
is_tb,
parent_ctx,
overflow_links,
);
}
}
if cpos > exited.ctrl_text_pos {
break; // 더 뒤 위치는 나중에 도달
}
}
// ctrl_text_pos가 문단 끝 미만이면 해당 위치로 이동
// (문단 끝이면 다음 문단/섹션으로 진행)
if exited.ctrl_text_pos < text_len {
return NavResult::Text {
sec,
para: exited.parent_para,
char_offset: exited.ctrl_text_pos,
context: parent_ctx.to_vec(),
};
}
}
// ctrl_text_pos >= text_len → 다음 문단으로
let para_count = self
.nav_paragraph_count(sec, parent_ctx, overflow_links)
.unwrap_or(0);
if exited.parent_para + 1 < para_count {
return self.navigate_to_para_start(
sec,
exited.parent_para + 1,
parent_ctx,
overflow_links,
);
}
// 부모 컨테이너도 끝 → 더 상위로 탈출
if !parent_ctx.is_empty() {
let mut grandparent_ctx = parent_ctx.to_vec();
let grandparent = grandparent_ctx.pop().unwrap();
return self.exit_control(
sec,
&grandparent,
true,
&grandparent_ctx,
overflow_links,
);
}
// Body 수준 → 다음 섹션
if sec + 1 < sections.len() {
return self.navigate_to_para_start(sec + 1, 0, &[], overflow_links);
}
NavResult::Boundary
} else {
// Backward: 부모 문단 정보 조회
let parent_p =
match self.nav_paragraph_ref(sec, exited.parent_para, parent_ctx, overflow_links) {
Some(p) => p,
None => return NavResult::Boundary,
};
// 같은 텍스트 위치에 이전 편집 가능 컨트롤이 있으면 backward 진입
// (모든 컨트롤이 ctrl_text_pos=0인 문단에서 역방향 탐색에 필요)
let ctrl_positions = find_logical_control_positions(parent_p);
for (ci, ctrl) in parent_p.controls.iter().enumerate().rev() {
if ci >= exited.ctrl_idx {
continue;
}
let cpos = ctrl_positions
.get(ci)
.copied()
.unwrap_or(navigable_text_len(parent_p));
if cpos == exited.ctrl_text_pos {
if let Some(is_tb) = classify_navigable(ctrl) {
return self.enter_control_backward(
sec,
exited.parent_para,
ci,
cpos,
is_tb,
parent_ctx,
overflow_links,
);
}
}
if cpos < exited.ctrl_text_pos {
break;
}
}
// ctrl_text_pos > 0: 컨트롤 직전 텍스트 위치 (ctrl_text_pos - 1)
if exited.ctrl_text_pos > 0 {
let prev_pos = exited.ctrl_text_pos - 1;
// prev_pos에 다른 편집 가능 컨트롤이 있는지 확인
let ctrl_positions = find_logical_control_positions(parent_p);
for (ci, ctrl) in parent_p.controls.iter().enumerate().rev() {
if ci >= exited.ctrl_idx {
continue;
}
let cpos = ctrl_positions
.get(ci)
.copied()
.unwrap_or(navigable_text_len(parent_p));
if cpos == prev_pos {
if let Some(is_tb) = classify_navigable(ctrl) {
return self.enter_control_backward(
sec,
exited.parent_para,
ci,
cpos,
is_tb,
parent_ctx,
overflow_links,
);
}
}
if cpos < prev_pos {
break;
}
}
return NavResult::Text {
sec,
para: exited.parent_para,
char_offset: prev_pos,
context: parent_ctx.to_vec(),
};
}
// ctrl_text_pos == 0: 문단 시작 → 이전 문단
if exited.parent_para > 0 {
return self.navigate_to_para_end(
sec,
exited.parent_para - 1,
parent_ctx,
overflow_links,
);
}
// 부모 컨테이너도 시작 → 더 상위로 탈출
if !parent_ctx.is_empty() {
let mut grandparent_ctx = parent_ctx.to_vec();
let grandparent = grandparent_ctx.pop().unwrap();
return self.exit_control(
sec,
&grandparent,
false,
&grandparent_ctx,
overflow_links,
);
}
// Body 수준 → 이전 섹션
if sec > 0 {
let prev_sec = sec - 1;
let prev_para_count = self
.nav_paragraph_count(prev_sec, &[], overflow_links)
.unwrap_or(0);
return self.navigate_to_para_end(
prev_sec,
prev_para_count.saturating_sub(1),
&[],
overflow_links,
);
}
NavResult::Boundary
}
}
/// NavResult를 JSON 문자열로 직렬화한다.
pub(crate) fn nav_result_to_json(result: &NavResult) -> String {
match result {
NavResult::Text {
sec,
para,
char_offset,
context,
} => {
let ctx_json: Vec<String> = context.iter().map(|e| {
format!(
"{{\"parentPara\":{},\"ctrlIdx\":{},\"ctrlTextPos\":{},\"cellIdx\":{},\"isTextBox\":{}}}",
e.parent_para, e.ctrl_idx, e.ctrl_text_pos, e.cell_idx, e.is_textbox
)
}).collect();
format!(
"{{\"type\":\"text\",\"sec\":{},\"para\":{},\"charOffset\":{},\"context\":[{}]}}",
sec, para, char_offset, ctx_json.join(",")
)
}
NavResult::Boundary => "{\"type\":\"boundary\"}".to_string(),
}
}
/// JSON 문자열에서 NavContextEntry 배열을 파싱한다.
pub(crate) fn parse_nav_context(json: &str) -> Vec<NavContextEntry> {
let json = json.trim();
if json == "[]" || json.is_empty() {
return Vec::new();
}
let mut result = Vec::new();
// 간단한 JSON 배열 파서
let inner = json.trim_start_matches('[').trim_end_matches(']');
if inner.trim().is_empty() {
return result;
}
// {...},{...} 형태를 분리
let mut depth = 0;
let mut start = 0;
let bytes = inner.as_bytes();
for i in 0..bytes.len() {
match bytes[i] {
b'{' => depth += 1,
b'}' => {
depth -= 1;
if depth == 0 {
let obj = &inner[start..=i].trim();
if let Some(entry) = Self::parse_nav_entry(obj) {
result.push(entry);
}
start = i + 1;
}
}
b',' if depth == 0 => {
start = i + 1;
}
_ => {}
}
}
result
}
fn parse_nav_entry(json: &str) -> Option<NavContextEntry> {
use crate::document_core::helpers::{json_bool, json_i32};
let parent_para = json_i32(json, "parentPara")? as usize;
let ctrl_idx = json_i32(json, "ctrlIdx")? as usize;
let ctrl_text_pos = json_i32(json, "ctrlTextPos").unwrap_or(0) as usize;
let cell_idx = json_i32(json, "cellIdx").unwrap_or(0) as usize;
let is_textbox = json_bool(json, "isTextBox").unwrap_or(true);
Some(NavContextEntry {
parent_para,
ctrl_idx,
ctrl_text_pos,
cell_idx,
is_textbox,
})
}
/// 섹션의 글상자 오버플로우 연결 정보를 계산(캐시)하여 반환한다.
pub(crate) fn get_overflow_links(&self, sec_idx: usize) -> Vec<OverflowLink> {
{
let cache = self.overflow_links_cache.borrow();
if let Some(links) = cache.get(&sec_idx) {
return links.clone();
}
}
let links = self.compute_overflow_links(sec_idx);
self.overflow_links_cache
.borrow_mut()
.insert(sec_idx, links.clone());
links
}
/// 섹션 내 글상자 오버플로우 연결 정보를 계산한다.
///
/// scan_textbox_overflow()와 동일한 알고리즘:
/// 1. Rectangle Shape + TextBox가 있는 컨트롤 수집
/// 2. 소스 감지: segment_width 변화 + vpos 리셋 → overflow_start 결정
/// 3. 타겟 감지: 모든 문단의 텍스트가 비어있는 글상자
/// 4. segment_width 매칭으로 소스→타겟 매핑
fn compute_overflow_links(&self, sec_idx: usize) -> Vec<OverflowLink> {
let section = match self.document.sections.get(sec_idx) {
Some(s) => s,
None => return Vec::new(),
};
// 오버플로우 소스 수집: (target_sw, para_idx, ctrl_idx, overflow_start)
let mut overflow_sources: Vec<(i32, usize, usize, usize)> = Vec::new();
// 빈 타겟 글상자 수집: (para_idx, ctrl_idx, inner_sw)
let mut empty_targets: Vec<(usize, usize, i32)> = Vec::new();
for (pi, para) in section.paragraphs.iter().enumerate() {
for (ci, ctrl) in para.controls.iter().enumerate() {
let drawing = match ctrl {
Control::Shape(s) => match s.as_ref() {
ShapeObject::Rectangle(r) => &r.drawing,
_ => continue,
},
_ => continue,
};
let tb = match &drawing.text_box {
Some(tb) => tb,
None => continue,
};
let has_text = tb.paragraphs.iter().any(|p| !p.text.is_empty());
if !has_text {
// 빈 글상자 → 타겟 후보
let inner_sw = tb
.paragraphs
.first()
.and_then(|p| p.line_segs.first())
.map(|ls| ls.segment_width)
.unwrap_or(0);
empty_targets.push((pi, ci, inner_sw));
continue;
}
// 오버플로우 감지 (scan_textbox_overflow와 동일)
let first_sw = tb
.paragraphs
.first()
.and_then(|p| p.line_segs.first())
.map(|ls| ls.segment_width)
.unwrap_or(0);
let mut max_vpos_end: i32 = 0;
let mut overflow_idx: Option<usize> = None;
for (tpi, tp) in tb.paragraphs.iter().enumerate() {
if let Some(first_ls) = tp.line_segs.first() {
if tpi > 0
&& first_ls.segment_width != first_sw
&& first_ls.vertical_pos < max_vpos_end
{
overflow_idx = Some(tpi);
break;
}
if let Some(last_ls) = tp.line_segs.last() {
let end = last_ls.vertical_pos + last_ls.line_height;
if end > max_vpos_end {
max_vpos_end = end;
}
}
}
}
if let Some(oi) = overflow_idx {
let target_sw = tb.paragraphs[oi]
.line_segs
.first()
.map(|ls| ls.segment_width)
.unwrap_or(0);
overflow_sources.push((target_sw, pi, ci, oi));
}
}
}
// 소스→타겟 매핑 (segment_width 가장 가까운 빈 글상자)
let mut links = Vec::new();
for (target_sw, src_pi, src_ci, oi) in overflow_sources {
let best = empty_targets
.iter()
.enumerate()
.min_by_key(|(_, (_, _, esw))| (target_sw - *esw).abs());
if let Some((idx, &(tgt_pi, tgt_ci, _))) = best {
links.push(OverflowLink {
source_parent_para: src_pi,
source_ctrl_idx: src_ci,
overflow_start: oi,
target_parent_para: tgt_pi,
target_ctrl_idx: tgt_ci,
});
empty_targets.remove(idx);
}
}
links
}
}