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
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
//! 직렬화 컨텍스트 — 1-pass 스캔으로 ID 풀을 구성하고 2-pass 쓰기에서 참조 정합성을 단언.
//!
//! ## 배경
//!
//! HWPX 직렬화에서 가장 큰 함정은 **한 파일(section.xml)에서 쓴 ID가 다른 파일(header.xml)에
//! 등록되지 않은** 상태로 출력되는 경우다. 예: `<hp:run charPrIDRef="3">` 를 썼는데
//! header의 `<hh:charPr id="3">` 가 누락되면 한컴2020이 조용히 스타일을 엉키게 렌더링한다.
//!
//! `SerializeContext`는 이를 구조적으로 방지한다:
//! 1. **1-pass**: Document IR을 훑어 모든 ID를 `registered`에 등록
//! 2. **2-pass**: 각 writer가 ID를 사용할 때 `reference`에 기록
//! 3. **단언**: `assert_all_refs_resolved()` 가 `referenced - registered` 가 공집합임을 확인
//!
//! Stage 0 에서는 뼈대 구조만 둔다. 실제 스캔 로직은 Stage 1~4에서 writer가 추가될 때 함께 확장한다.
#![allow(dead_code)]
use std::collections::{HashMap, HashSet};
use crate::model::control::Control;
use crate::model::document::Document;
use crate::serializer::content_loss::{ContentLossReport, SerializedFormat};
use crate::serializer::SerializeError;
/// 양방향 ID 풀 — 등록된 ID와 참조된 ID를 추적한다.
#[derive(Debug, Default)]
pub struct IdPool<T: Copy + Eq + std::hash::Hash> {
pub(super) registered: HashSet<T>,
referenced: HashSet<T>,
reservation: Option<std::sync::Arc<dyn crate::storage::Reservation>>,
storage_failure: Option<String>,
}
impl<T: Copy + Eq + std::hash::Hash> IdPool<T> {
pub fn new() -> Self {
Self {
registered: HashSet::new(),
referenced: HashSet::new(),
reservation: None,
storage_failure: None,
}
}
/// header/DocInfo에서 정의되는 ID를 등록.
pub fn register(&mut self, id: T) {
if self.storage_failure.is_some() {
return;
}
if let Err(error) = insert_id(&self.reservation, &mut self.registered, id) {
self.storage_failure = Some(error);
}
}
/// section/기타 writer가 ID를 참조할 때 호출.
pub fn reference(&mut self, id: T) {
if self.storage_failure.is_some() {
return;
}
if let Err(error) = insert_id(&self.reservation, &mut self.referenced, id) {
self.storage_failure = Some(error);
}
}
pub fn is_registered(&self, id: &T) -> bool {
self.registered.contains(id)
}
/// `referenced - registered`: 참조됐으나 등록되지 않은 ID.
pub fn unresolved(&self) -> Vec<T> {
self.referenced
.difference(&self.registered)
.copied()
.collect()
}
pub fn registered_count(&self) -> usize {
self.registered.len()
}
}
/// HWPX manifest + ZIP entry용 BinData 엔트리.
#[derive(Debug, Clone)]
pub struct BinDataEntry {
/// content.hpf 의 `opf:item id` (예: "image1")
pub manifest_id: String,
/// ZIP 엔트리 경로 (예: "BinData/image1.png") 또는 외부 참조 원본 경로
/// (`is_embedded=false`, 예: `D:\다운로드\...`)
pub href: String,
/// MIME 타입 (예: "image/png")
pub media_type: String,
/// IR 상의 bin_data_id (storage_id) — 매핑 역추적용
pub bin_data_id: u16,
/// content.hpf `isEmbeded` — false 면 ZIP 엔트리가 없는 항목이다:
/// 외부 파일 참조(#1891) 또는 스트림 부재로 콘텐츠가 없는 항목(#3526).
pub is_embedded: bool,
}
/// [#3546] OOXML 차트 파트 — BinData 매니페스트 대상이 아니라
/// `Chart/chartN.xml` 원형 경로로 방출되는 항목.
#[derive(Debug, Clone)]
pub struct ChartPartEntry {
/// ZIP 엔트리 경로 (예: "Chart/chart1.xml")
pub href: String,
/// IR 상의 bin_data_id (= 60000 + N, HWPX 파서 주입 규약)
pub bin_data_id: u16,
}
/// 1-pass 스캔으로 구축되는 직렬화 컨텍스트.
#[derive(Debug)]
pub struct SerializeContext {
pub char_shape_ids: IdPool<u32>,
pub para_shape_ids: IdPool<u16>,
pub border_fill_ids: IdPool<u16>,
pub tab_pr_ids: IdPool<u16>,
pub numbering_ids: IdPool<u16>,
pub style_ids: IdPool<u16>,
/// `bin_data_id` (IR) → manifest 엔트리 매핑
pub bin_data_map: HashMap<u16, BinDataEntry>,
/// HWP5 BIN_DATA 레코드 순번(1-based) → storage_id 사상.
///
/// `ImageAttr.bin_data_id`는 storage stream 이름이 아니라 DocInfo 레코드
/// 순번이다. storage_id와 같은 숫자가 있어도 순번 사상이 우선해야 한다.
bin_seq_to_storage: HashMap<u16, u16>,
/// [#3546] OOXML 차트 파트 — Chart/chartN.xml 원형 방출 목록.
/// 원본 content.hpf 는 Chart 파트를 나열하지 않으므로 manifest·3-way
/// 단언 대상 밖이다.
pub chart_entries: Vec<ChartPartEntry>,
/// [#6869] 이 문단에서 `hp:pageNum` 을 이미 냈는가.
///
/// HWP5 는 같은 문단에 쪽번호 위치(`pngp`) 컨트롤을 여러 개 담을 수 있고 rhwp 파서는
/// 그것을 그대로 보존한다(156532689 문단 179 는 **9개**). 그런데 HWPX 로 그 9개를
/// 그대로 내면 **한글이 그 문서에서 멈춘다**(정답지 실측: 20분 타임아웃, 이슈는
/// 44쪽→12쪽으로 관측). 한컴 자신이 같은 문서를 HWPX 로 저장하면 문단당 하나로
/// 접는다(문서 전체 23 → 5).
///
/// 접은 슬롯은 **축에서도 빠져야 한다** — 컨트롤만 지우고 `textpos` 를 그대로 두면
/// 오히려 축이 더 어긋나 여전히 멈춘다(실측). `render_control_slot_tracked` 가
/// "아무것도 방출하지 않은 슬롯" 으로 세어 `#5943` 의 축 보정이 그대로 걸리도록,
/// 접을 때는 **XML 을 한 글자도 내지 않는다**.
/// `render_runs`의 호출 스코프에서만 사용하며, 중첩 문단 종료 시 부모 상태를 복원한다.
pub(crate) para_page_num_pos_emitted: bool,
/// 문서 전역 문단 ID 카운터 — `<hp:p id="...">` 에 발급한다.
para_id_counter: u32,
/// HWP3 전용 Hyperlink control을 HWPX `fieldBegin`으로 승격할 때 쓰는 ID.
/// HWP3 원본에는 HWPX field ID가 없으므로, 기존 Field ID와 충돌 가능성이 낮은
/// 상위 범위를 문서 내에서 단조 감소시켜 링크마다 다른 값을 발급한다.
generated_hyperlink_id: u32,
/// subList(셀·글상자) 직렬화 중첩 깊이 (#1379 3단계).
///
/// 본문 경로는 colPr 를 섹션 템플릿 첫 run 에서 처리하므로 인라인 미방출이
/// 정합이지만, 셀·글상자 subList 의 colPr 는 원본 XML 에 인라인으로 존재한다.
/// `render_control_slot` 의 ColumnDef 방출을 subList 경로(depth > 0)로 한정한다.
pub sub_list_depth: u32,
/// 본문 첫 문단의 첫 ColumnDef(섹션 템플릿 colPr 앵커가 흡수하는 단 정의)의
/// **인라인 XML 방출만** 1회 억제하기 위한 consume-once 플래그 (#1584).
///
/// ColumnDef 는 char-offset 슬롯(8유닛)을 점유하므로 `slots` 에는 그대로 남겨
/// 위치 정합을 보존하되, 첫 ColumnDef 의 `<hp:colPr>` XML 은 템플릿이 이미
/// 방출했으므로 중복 방지를 위해 건너뛴다. `write_section` 이 첫 문단 렌더 직전
/// true 로 설정하고, 첫 본문 ColumnDef 방출 시 `render_control_slot` 이 소거한다.
pub body_coldef_template_pending: bool,
/// [#5943] 저장 lineseg 의 `textpos` 가 이미 HWPX 슬롯 축인지 여부.
///
/// `LineSeg::text_start` 는 파서가 파일 값을 그대로 담으므로 **출처마다 축이 다르다**
/// — HWP5·HWP3·HML 출처는 확장 제어 하나당 8유닛을 주는 HWP5 축이고, HWPX 출처는
/// `hp:secPr`·`hp:colPr` 을 세지 않는 HWPX 축이다. 이 값이 참이면 재기준화하지
/// 않는다(이중으로 빼면 aift.hwpx 왕복이 `textpos 24 → 8` 로 깨진다).
/// 판정은 "이 lineseg 가 HWPX 컨테이너에서 나왔는가" — 출처 포맷이 HWPX 이거나
/// rhwp HWPX→HWP5 변환본(`hwpx_lineage`)이다.
///
/// [#7526] rhwp 원본 마커(`META-INF/rhwp-hwp5-origin`·`rhwp-hwp3-origin`)를 싣는 문서는
/// 제외한다. 산출물이 rhwp 축 계약으로 다시 읽히므로, 그 파일에서 읽은 문단만 날값을
/// 두고(`hwpx_axis_shift` 0 아님 — 다시 내리면 02502 재수출이 32 → 16 으로 깨진다)
/// 편집으로 다시 조판한 문단은 HWP5 출처처럼 내린다.
pub line_segs_on_hwpx_axis: bool,
/// 새 HWP5 계보 산출물은 실제 방출된 문단의 제어 슬롯을 함께 센다.
pub line_segs_on_paragraph_axis: bool,
/// 이번 HWPX 산출물에서 발생한 사용자 내용 손실 (#4430).
///
/// ID 풀과 마찬가지로 한 번의 직렬화 생명주기에만 속하며, 완료 시 바이트와 함께
/// `SerializedDocument`로 이동한다.
pub content_loss: ContentLossReport,
pub(crate) table_instance_ids: IdPool<u32>,
pub(crate) table_row_grids: crate::model::table_row_grid::Writer,
}
impl Default for SerializeContext {
fn default() -> Self {
Self {
char_shape_ids: IdPool::default(),
line_segs_on_hwpx_axis: false,
line_segs_on_paragraph_axis: false,
para_shape_ids: IdPool::default(),
border_fill_ids: IdPool::default(),
tab_pr_ids: IdPool::default(),
numbering_ids: IdPool::default(),
style_ids: IdPool::default(),
bin_data_map: HashMap::new(),
bin_seq_to_storage: HashMap::new(),
chart_entries: Vec::new(),
para_page_num_pos_emitted: false,
para_id_counter: 0,
generated_hyperlink_id: u32::MAX,
sub_list_depth: 0,
body_coldef_template_pending: false,
content_loss: ContentLossReport::new(SerializedFormat::Hwpx),
table_row_grids: Default::default(),
table_instance_ids: Default::default(),
}
}
}
impl SerializeContext {
/// HWP3 `Control::Hyperlink`용 HWPX field ID를 하나 발급한다.
pub fn next_generated_hyperlink_id(&mut self) -> u32 {
let id = self.generated_hyperlink_id;
self.generated_hyperlink_id = self.generated_hyperlink_id.saturating_sub(1);
id
}
/// Document IR 전체를 1-pass 스캔하여 ID 풀을 채운다.
///
/// Stage 0에서는 최소 등록(header.xml 리소스만)만 수행한다. Stage 1~4에서
/// 각 writer가 추가되면서 `reference()` 호출과 스캔 범위가 확장된다.
pub fn collect_from_document(doc: &Document) -> Self {
Self::try_collect_from_document(doc, &crate::storage::Unbounded, None)
.expect("unbounded HWPX context")
}
pub(crate) fn try_collect_from_document<S: crate::storage::Storage>(
doc: &Document,
storage: &S,
reservation: Option<std::sync::Arc<dyn crate::storage::Reservation>>,
) -> Result<Self, SerializeError>
where
S::Error: std::fmt::Display,
{
let mut ctx = Self::default();
ctx.char_shape_ids.reservation = reservation.clone();
ctx.para_shape_ids.reservation = reservation.clone();
ctx.border_fill_ids.reservation = reservation.clone();
ctx.tab_pr_ids.reservation = reservation.clone();
ctx.numbering_ids.reservation = reservation.clone();
ctx.table_instance_ids.reservation = reservation.clone();
ctx.style_ids.reservation = reservation;
// [#8303] HWP3 원본 산출물도 문단 축으로 낸다 — 한컴 2020·2024 가 같은 HWP3 을
// 저장한 HWPX 의 `textpos` 축이다. 표지 엔트리는 `serialize_hwpx` 가 함께 쓴다.
ctx.line_segs_on_paragraph_axis = doc
.hwpx_aux_entry(crate::model::document::HWP5_ORIGIN_HWPX_MARKER_PATH)
== Some(crate::model::document::HWP5_ORIGIN_HWPX_PARAGRAPH_AXIS)
|| doc
.hwpx_aux_entry(crate::model::document::HWP3_ORIGIN_HWPX_MARKER_PATH)
.is_some();
// [#7526] rhwp 원본 마커를 싣는 산출물은 rhwp 축 계약을 따른다. 그 파일에서 읽은
// 문단은 이미 그 축이고(`hwpx_axis_shift` 0 아님), 편집으로 다시 조판한 문단만
// HWP5 축이라 문단마다 가른다(`render_paragraph_parts`).
let rhwp_origin_marker = [
crate::model::document::HWP5_ORIGIN_HWPX_MARKER_PATH,
crate::model::document::HWP3_ORIGIN_HWPX_MARKER_PATH,
]
.iter()
.any(|path| doc.hwpx_aux_entry(path).is_some());
ctx.line_segs_on_hwpx_axis = (doc.provenance.format
== crate::model::provenance::SourceFormat::Hwpx
|| doc.provenance.hwpx_lineage)
&& !rhwp_origin_marker;
// CharShape, ParaShape, BorderFill, TabDef, Numbering, Style, Font
// 목록은 배열 인덱스가 곧 HWPX `id` 속성이 된다.
for (idx, _) in doc.doc_info.char_shapes.iter().enumerate() {
ctx.char_shape_ids.register(idx as u32);
ctx.check_storage()?;
}
for (idx, _) in doc.doc_info.para_shapes.iter().enumerate() {
ctx.para_shape_ids.register(idx as u16);
ctx.check_storage()?;
}
// [#1384] borderFill id 는 1-based 방출(header.rs write_border_fill: idx+1)
// 이고 borderFillIDRef 도 1-based 참조이므로, 등록도 1-based 로 맞춘다.
// 종전 `idx`(0-based) 등록이라 마지막 id(예: exam_social 31)가 등록 범위
// (0~30) 밖으로 빠져 SERIALIZE_FAIL(미등록 borderFillIDRef)을 유발했다.
// 인라인 등록(표/셀, 아래)은 IR 값(1-based) 그대로라 본래 정합 — 이로써 통일.
for (idx, _) in doc.doc_info.border_fills.iter().enumerate() {
ctx.border_fill_ids.register((idx + 1) as u16);
ctx.check_storage()?;
}
for (idx, _) in doc.doc_info.tab_defs.iter().enumerate() {
ctx.tab_pr_ids.register(idx as u16);
ctx.check_storage()?;
}
// [#1409] numbering id 는 1-based 방출(header.rs write_numbering: id+1)이고
// 실물도 1-based 이므로 등록도 1-based 로 맞춘다 (#1384 borderFill 동형).
// numbering 은 reference 검사가 없어 현재 미표면화이나, 등록 축 일관성 +
// HWP5 변환·미래 검사 활성화 대비.
for (idx, _) in doc.doc_info.numberings.iter().enumerate() {
ctx.numbering_ids.register((idx + 1) as u16);
ctx.check_storage()?;
}
for (idx, _) in doc.doc_info.styles.iter().enumerate() {
ctx.style_ids.register(idx as u16);
ctx.check_storage()?;
}
// [#1933 보강] style 0 은 `effective_style_id`/`write_styles` 계약상 "항상
// 등록됨" 취급이다 — `<hh:styles>` 블록 자체가 없는(styles 비어있는) 정상
// HWPX 도 파라그래프가 암묵적 기본 style 0을 참조할 수 있고, `write_styles`
// 는 그 경우 블록을 그대로 생략한다(header.rs). 종전에는 `doc_info.styles`
// 가 비어 있으면 0도 등록되지 않아, 있는 그대로 되돌려 쓰는 정상적인
// round-trip이 `assert_all_refs_resolved` 에서 "미등록 styleIDRef: [0]" 로
// 하드 실패했다(예: styles 블록이 없는 샘플의 HWPX 자기 왕복).
ctx.style_ids.register(0);
ctx.check_storage()?;
// 인라인 컨트롤(표/그림 등)의 borderFillIDRef를 사전 등록하여
// assert_all_refs_resolved 검증 시 누락 방지. 중첩 표(셀 안의 표)와
// 글상자·머리말/꼬리말·각주/미주 안의 표까지 재귀한다(아래
// `register_border_fills_in_paragraphs` 참조, `table_extract::collect_from_paragraph`
// 와 같은 위상의 재귀).
for sec in &doc.sections {
register_border_fills_in_paragraphs(&mut ctx, &sec.paragraphs, 0)?;
for master in &sec.section_def.master_pages {
register_border_fills_in_paragraphs(&mut ctx, &master.paragraphs, 0)?;
}
}
// BinData: bin_data_content의 storage_id → manifest 엔트리 생성.
// manifest id 는 반드시 `image{bin_data_id}` — HWPX 파서(section.rs)가
// binaryItemIDRef 의 숫자를 그대로 bin_data_id 로 파싱하므로(숫자 불변식),
// 순번(i+1) 명명은 링크 항목으로 id 에 구멍이 있는 문서(#1891 73504)에서
// 이름과 id 가 어긋나 재파스 그림 참조가 엉킨다.
for (index, bd) in doc.bin_data_content.iter().enumerate() {
// HWP5 연결 레코드 자리 항목은 BinData 가 아니다 — 아래 bin_data_list 루프가
// 외부 참조로 처리한다.
if crate::model::bin_data::is_hwp5_link_slot(bd, doc.doc_info.bin_data_list.get(index))
{
continue;
}
// [#3546] OOXML 차트 파트(HWPX 파서가 60000+N 으로 주입)는 BinData 가
// 아니다 — 원본은 Chart/chartN.xml 이고 content.hpf 도 나열하지 않는다.
// manifest 등록 없이 원형 경로로 별도 방출한다.
if bd.extension == "ooxml_chart" {
if let Some(n) = bd.id.checked_sub(60000).filter(|n| *n >= 1) {
let href = storage
.display(&format_args!("Chart/chart{}.xml", n))
.map_err(context_error)?;
storage
.push(
&mut ctx.chart_entries,
ChartPartEntry {
href,
bin_data_id: bd.id,
},
)
.map_err(context_error)?;
continue;
}
}
// 빈 확장자는 원본과 동일하게 확장자 없이(`image{id}.`) 재직렬화한다.
// 예전엔 `.bin` 기본값을 붙였으나(#1981), 원본이 확장자 없는 BinData
// (`BinData/image13.` 등, OLE·미상 임베드)를 담은 경우 라운드트립 확장자
// 멀티셋이 `bin` vs `""` 로 어긋나 PKG_FAIL 이 났다. 원본 형태를 보존한다.
let manifest_id = storage
.display(&format_args!("image{}", bd.id))
.map_err(context_error)?;
let ext = bd.extension.as_str();
let href = storage
.display(&format_args!("BinData/{}.{}", manifest_id, ext))
.map_err(context_error)?;
let media_type = mime_from_ext(ext);
let additional = usize::from(!ctx.bin_data_map.contains_key(&bd.id));
storage
.reserve_map(&mut ctx.bin_data_map, additional)
.map_err(context_error)?;
ctx.bin_data_map.insert(
bd.id,
BinDataEntry {
manifest_id,
href,
media_type: storage.string(media_type).map_err(context_error)?,
bin_data_id: bd.id,
is_embedded: true,
},
);
}
// 콘텐츠 없는 BinData: 바이트가 없어도 manifest 항목과 참조는 보존해야
// 한다 (미등록이면 `write_img` 가 Err 를 반환해(picture.rs) 해당 <hp:pic>
// 이 통째로 드롭되고 레이아웃 앵커까지 사라져 렌더가 갈라진다).
//
// [#1891] 은 외부 참조(Link)만 이 구멍을 막았으나, Embedding/Storage 도
// 스트림이 없으면(parser/mod.rs 가 "BinData 스트림 없음" 경고 후 skip)
// bin_data_content 가 비어 위 루프에 걸리지 않는다. 그 결과 두 루프를
// 모두 빠져나가 같은 드롭이 재현됐다(#3526 hwpspec.hwp bin_data_id=37).
// 따라서 data_type 을 가리지 않고 "아직 등록되지 않은 모든 항목"으로 넓힌다.
//
// ZIP 엔트리는 만들지 않고 content.hpf 에 isEmbeded="0" + href(외부 경로,
// 없으면 빈 문자열)로만 방출한다 — mod.rs 가 ZIP 쓰기와 3-way 단언에서,
// package_check 가 엔트리 실재 검사에서 각각 제외하므로 패키지는 정합하다.
// 명명은 위와 같은 숫자 불변식(`image{storage_id}`)을 따른다.
for bd in &doc.doc_info.bin_data_list {
// storage_id=0 은 "참조 없는 placeholder pic" 센티널(#1567)과 겹치므로
// 등록하지 않는다 (HWP5 Link 항목은 storage_id 미부여일 수 있음).
if bd.storage_id == 0 || ctx.bin_data_map.contains_key(&bd.storage_id) {
continue;
}
let ext = bd.extension.as_deref().unwrap_or("");
let additional = usize::from(!ctx.bin_data_map.contains_key(&bd.storage_id));
storage
.reserve_map(&mut ctx.bin_data_map, additional)
.map_err(context_error)?;
ctx.bin_data_map.insert(
bd.storage_id,
BinDataEntry {
manifest_id: storage
.display(&format_args!("image{}", bd.storage_id))
.map_err(context_error)?,
href: storage
.string(bd.abs_path.as_deref().unwrap_or_default())
.map_err(context_error)?,
media_type: storage.string(mime_from_ext(ext)).map_err(context_error)?,
bin_data_id: bd.storage_id,
is_embedded: false,
},
);
}
// [#3893/#4049] HWP5 본문 개체의 BIN_DATA 순번 축. `storage_id`와
// 우연히 같은 숫자가 있어도 별개 의미이므로, 실제 데이터를 가진 모든
// 레코드의 사상을 등록한다.
for (i, bd) in doc.doc_info.bin_data_list.iter().enumerate() {
let seq = (i + 1) as u16;
if bd.storage_id != 0 && ctx.bin_data_map.contains_key(&bd.storage_id) {
storage
.reserve_map(&mut ctx.bin_seq_to_storage, 1)
.map_err(context_error)?;
ctx.bin_seq_to_storage.insert(seq, bd.storage_id);
}
}
// [#5168] 외부 연결(Link) BinData 는 `storage_id` 가 없어(0) 위 등록 루프들에서
// 모두 빠진다. 그러면 Link 를 참조하는 그림(`bin_data_id` = BIN_DATA 레코드 순번)이
// `resolve_bin_id` 의 직접 조회 폴백으로 떨어져, 순번이 내장 이미지의 `storage_id`
// 범위 안이면 **엉뚱한 내장 이미지로 바뀌고**(image{순번}=image{storage_id} 충돌),
// 범위를 넘으면 미등록으로 **드롭**됐다(07605: LINK 7·EMBED 5 → gso 12 → pic 10,
// image1~5 가 각 2회 참조). Link 마다 기존 키와 겹치지 않는 새 id 를 배정해 외부
// 참조 엔트리(`is_embedded=false`, href=연결 경로)로 등록하고 그 순번을 사상한다.
// 외부 참조라 ZIP 엔트리·3-way 단언에서는 제외된다(#1891 경로와 동일).
let mut next_link_id = ctx.bin_data_map.keys().copied().max().unwrap_or(0) + 1;
for (i, bd) in doc.doc_info.bin_data_list.iter().enumerate() {
let seq = (i + 1) as u16;
if bd.data_type != crate::model::bin_data::BinDataType::Link
|| ctx.bin_seq_to_storage.contains_key(&seq)
{
continue;
}
let link_id = next_link_id;
next_link_id += 1;
let href = storage
.string(
bd.abs_path
.as_deref()
.or(bd.rel_path.as_deref())
.unwrap_or_default(),
)
.map_err(context_error)?;
let ext = bd.extension.as_deref().unwrap_or("");
let additional = usize::from(!ctx.bin_data_map.contains_key(&link_id));
storage
.reserve_map(&mut ctx.bin_data_map, additional)
.map_err(context_error)?;
ctx.bin_data_map.insert(
link_id,
BinDataEntry {
manifest_id: storage
.display(&format_args!("image{}", link_id))
.map_err(context_error)?,
href,
media_type: storage.string(mime_from_ext(ext)).map_err(context_error)?,
bin_data_id: link_id,
is_embedded: false,
},
);
storage
.reserve_map(&mut ctx.bin_seq_to_storage, 1)
.map_err(context_error)?;
ctx.bin_seq_to_storage.insert(seq, link_id);
}
ctx.check_storage()?;
Ok(ctx)
}
/// manifest·content.hpf 출력용 엔트리 목록 (삽입 순서 보존을 위해 `bin_data_id` 정렬).
pub fn bin_data_entries(&self) -> Vec<BinDataEntry> {
self.try_bin_data_entries(&crate::storage::Unbounded)
.expect("unbounded HWPX entries")
}
pub(crate) fn try_bin_data_entries<S: crate::storage::Storage>(
&self,
storage: &S,
) -> Result<Vec<BinDataEntry>, SerializeError>
where
S::Error: std::fmt::Display,
{
self.check_storage()?;
let mut entries = storage
.vec(self.bin_data_map.len())
.map_err(context_error)?;
for value in self.bin_data_map.values() {
entries.push(BinDataEntry {
manifest_id: storage.string(&value.manifest_id).map_err(context_error)?,
href: storage.string(&value.href).map_err(context_error)?,
media_type: storage.string(&value.media_type).map_err(context_error)?,
bin_data_id: value.bin_data_id,
is_embedded: value.is_embedded,
});
}
// Stable sort may allocate replacement scratch. Preserve equal-key
// ordering even for a caller-populated public map.
storage
.bytes(
entries
.len()
.saturating_mul(std::mem::size_of::<BinDataEntry>()),
)
.map_err(context_error)?;
entries.sort_by_key(|entry| entry.bin_data_id);
Ok(entries)
}
pub(crate) fn check_storage(&self) -> Result<(), SerializeError> {
if let Some(error) = [
&self.char_shape_ids.storage_failure,
&self.para_shape_ids.storage_failure,
&self.border_fill_ids.storage_failure,
&self.tab_pr_ids.storage_failure,
&self.numbering_ids.storage_failure,
&self.style_ids.storage_failure,
&self.table_instance_ids.storage_failure,
]
.into_iter()
.find_map(Option::as_deref)
{
return Err(context_error(error));
}
Ok(())
}
/// `bin_data_id` → manifest id 조회 (Stage 4의 `<hc:img binaryItemIDRef="...">` 용).
///
/// HWP5 본문 개체의 `bin_data_id`는 BIN_DATA 레코드 순번이므로, 등록된
/// 순번 사상을 storage ID 직접 조회보다 먼저 적용한다. 사상이 없는 sparse
/// ID(차트)와 수동 구성 문서는 기존 직접 조회를 사용한다.
pub fn resolve_bin_id(&self, bin_data_id: u16) -> Option<&str> {
self.bin_seq_to_storage
.get(&bin_data_id)
.and_then(|storage| self.bin_data_map.get(storage))
.map(|e| e.manifest_id.as_str())
.or_else(|| {
self.bin_data_map
.get(&bin_data_id)
.map(|e| e.manifest_id.as_str())
})
}
/// 모든 참조가 해소되었는지 단언. 해소되지 않은 ID가 있으면 `SerializeError::XmlError` 반환.
pub fn assert_all_refs_resolved(&self) -> Result<(), SerializeError> {
self.try_assert_all_refs_resolved(&crate::storage::Unbounded)
}
pub(crate) fn try_assert_all_refs_resolved<S: crate::storage::Storage>(
&self,
storage: &S,
) -> Result<(), SerializeError>
where
S::Error: std::fmt::Display,
{
self.check_storage()?;
let mut missing: Vec<String> = Vec::new();
let cs = storage
.collect(
self.char_shape_ids
.referenced
.difference(&self.char_shape_ids.registered)
.copied(),
)
.map_err(context_error)?;
if !cs.is_empty() {
storage
.push(
&mut missing,
storage
.display(&format_args!("charPrIDRef: {:?}", cs))
.map_err(context_error)?,
)
.map_err(context_error)?;
}
let ps = storage
.collect(
self.para_shape_ids
.referenced
.difference(&self.para_shape_ids.registered)
.copied(),
)
.map_err(context_error)?;
if !ps.is_empty() {
storage
.push(
&mut missing,
storage
.display(&format_args!("paraPrIDRef: {:?}", ps))
.map_err(context_error)?,
)
.map_err(context_error)?;
}
let bf = storage
.collect(
self.border_fill_ids
.referenced
.difference(&self.border_fill_ids.registered)
.copied(),
)
.map_err(context_error)?;
if !bf.is_empty() {
storage
.push(
&mut missing,
storage
.display(&format_args!("borderFillIDRef: {:?}", bf))
.map_err(context_error)?,
)
.map_err(context_error)?;
}
let tp = storage
.collect(
self.tab_pr_ids
.referenced
.difference(&self.tab_pr_ids.registered)
.copied(),
)
.map_err(context_error)?;
if !tp.is_empty() {
storage
.push(
&mut missing,
storage
.display(&format_args!("tabPrIDRef: {:?}", tp))
.map_err(context_error)?,
)
.map_err(context_error)?;
}
let nm = storage
.collect(
self.numbering_ids
.referenced
.difference(&self.numbering_ids.registered)
.copied(),
)
.map_err(context_error)?;
if !nm.is_empty() {
storage
.push(
&mut missing,
storage
.display(&format_args!("numberingIDRef: {:?}", nm))
.map_err(context_error)?,
)
.map_err(context_error)?;
}
let st = storage
.collect(
self.style_ids
.referenced
.difference(&self.style_ids.registered)
.copied(),
)
.map_err(context_error)?;
if !st.is_empty() {
storage
.push(
&mut missing,
storage
.display(&format_args!("styleIDRef: {:?}", st))
.map_err(context_error)?,
)
.map_err(context_error)?;
}
if missing.is_empty() {
Ok(())
} else {
let joined = storage.join(&missing, "; ").map_err(context_error)?;
Err(SerializeError::XmlError(
storage
.display(&format_args!("미등록 ID 참조 발견: {}", joined))
.map_err(context_error)?,
))
}
}
/// 문서 전역 문단 ID를 하나 발급하고 카운터를 증가시킨다.
pub fn next_para_id(&mut self) -> u32 {
let id = self.para_id_counter;
self.para_id_counter += 1;
id
}
/// [Issue #1933] 스타일 목록 밖 styleIDRef 를 기본 스타일(0)로 강등한다.
///
/// 일부 생성기 산출물(보도자료 계열)은 header 스타일 목록에 없는 style_id 를
/// 문단이 참조한다(파일 자기모순). 종전에는 `assert_all_refs_resolved` 가
/// 하드 실패해 "열리는데 저장 불가" 상태가 됐다. 한글은 이런 문서를 기본
/// 스타일로 폴백해 열고 저장하므로, 미등록 참조는 0(항상 등록됨)으로 강등한다.
/// 등록된 참조는 그대로 반환한다.
pub fn effective_style_id(&self, raw: u8) -> u8 {
if self.style_ids.is_registered(&(raw as u16)) {
raw
} else {
0
}
}
}
/// 재귀 스캔 최대 깊이 — `table_extract::MAX_NEST_DEPTH` 와 같은 값.
/// 순환 참조가 없는 정상 IR에서는 닿지 않고, 적대적/손상 입력에서 무한 재귀를 막는다.
const MAX_BORDER_FILL_SCAN_DEPTH: usize = 8;
/// 표 하나의 `border_fill_id`(표/영역/셀)를 등록한다.
fn register_table_border_fills(
ctx: &mut SerializeContext,
table: &crate::model::table::Table,
) -> Result<(), SerializeError> {
ctx.border_fill_ids.register(table.border_fill_id);
ctx.check_storage()?;
for zone in &table.zones {
ctx.border_fill_ids.register(zone.border_fill_id);
ctx.check_storage()?;
}
for cell in &table.cells {
ctx.border_fill_ids.register(cell.border_fill_id);
ctx.check_storage()?;
}
Ok(())
}
/// 문단 목록을 재귀하며 표(및 중첩 표)의 `border_fill_id`를 사전 등록한다.
///
/// `table_extract::collect_from_paragraph`/`nested_tables`(#3719 계열 표 추출)와 같은
/// 위상의 재귀 — 표 셀 안의 표(중첩 표), 글상자, 머리말/꼬리말, 각주/미주까지 내려간다.
/// 종전에는 `doc.sections[].paragraphs[]`의 최상위 `Control::Table`만 훑어 셀 안에 중첩된
/// 표나 글상자·머리말/꼬리말·각주/미주 안의 표의 `border_fill_id`가 등록에서 빠졌다.
/// 그 표가 실제 직렬화(`table.rs`의 `reference()` 호출)에서 참조되면
/// `assert_all_refs_resolved`가 "미등록 ID 참조 발견"으로 하드 실패해 문서 전체의
/// `export-hwpx`가 산출물 없이 실패했다(실측: 정부 보고서 표 107개 중 중첩 표 1개를 가진
/// 문서에서 `borderFillIDRef: [0]` 미등록으로 재현).
///
/// [gestell 리뷰] 첫 수정은 6개 소유자(표 셀·글상자·머리말·꼬리말·각주·미주)만 돌았다.
/// OWPML 은 문단 리스트를 8개 자리에 둘 수 있다 — 나머지 2개(캡션, 필드 메모)와 함께
/// `Control::Picture`(자체 캡션을 갖는 별도 컨트롤)·`Control::HiddenComment` 도 이 재귀 밖에
/// 있었다. `#2736`(전수 조사: 순회 × 컨테이너 행렬)이 이미 지적한 "공유 방문자 부재로 반복되는
/// 재귀 누락"과 같은 계열 — 이번 세션의 `#4321`/PR #4365(`injection_scan.rs`)가 캡션·그림·필드
/// 메모 누락을 고친 것과 동형이다.
fn register_border_fills_in_paragraphs(
ctx: &mut SerializeContext,
paragraphs: &[crate::model::paragraph::Paragraph],
depth: usize,
) -> Result<(), SerializeError> {
if depth >= MAX_BORDER_FILL_SCAN_DEPTH {
return Ok(());
}
for para in paragraphs {
for ctrl in ¶.controls {
register_border_fills_in_control(ctx, ctrl, depth)?;
}
}
Ok(())
}
fn register_border_fills_in_control(
ctx: &mut SerializeContext,
ctrl: &Control,
depth: usize,
) -> Result<(), SerializeError> {
match ctrl {
Control::Table(tbl) => {
ctx.table_instance_ids.register(tbl.common.instance_id);
ctx.check_storage()?;
register_table_border_fills(ctx, tbl)?;
for cell in &tbl.cells {
register_border_fills_in_paragraphs(ctx, &cell.paragraphs, depth + 1)?;
}
if let Some(caption) = &tbl.caption {
register_border_fills_in_paragraphs(ctx, &caption.paragraphs, depth + 1)?;
}
}
Control::Shape(shape) => {
register_border_fills_in_shape(ctx, shape, depth)?;
}
// 독립 그림 컨트롤(글상자/묶음 밖) — 자체 `caption` 을 갖는다(`src/model/image.rs`).
Control::Picture(pic) => {
if let Some(caption) = &pic.caption {
register_border_fills_in_paragraphs(ctx, &caption.paragraphs, depth + 1)?;
}
}
Control::Header(h) => {
register_border_fills_in_paragraphs(ctx, &h.paragraphs, depth + 1)?;
}
Control::Footer(f) => {
register_border_fills_in_paragraphs(ctx, &f.paragraphs, depth + 1)?;
}
Control::Footnote(f) => {
register_border_fills_in_paragraphs(ctx, &f.paragraphs, depth + 1)?;
}
Control::Endnote(e) => {
register_border_fills_in_paragraphs(ctx, &e.paragraphs, depth + 1)?;
}
// 숨은 설명(메모) — 화면에 안 보여도 파일에는 문단 리스트로 존재한다.
Control::HiddenComment(hc) => {
register_border_fills_in_paragraphs(ctx, &hc.paragraphs, depth + 1)?;
}
// 필드(누름틀 등) 메모 — `Field.memo_paragraphs`.
Control::Field(f) => {
register_border_fills_in_paragraphs(ctx, &f.memo_paragraphs, depth + 1)?;
}
_ => {}
}
Ok(())
}
/// `ShapeObject`(그리기 개체) 하나의 글상자·캡션과, 묶음이면 자식 개체까지 재귀한다.
///
/// 캡션 자리는 변형마다 다르다(#4319 가 렌더 쪽에서 이미 지적한 비대칭과 같은 축):
/// - 기본 6종(Line/Rectangle/Ellipse/Arc/Polygon/Curve): `drawing.caption`
/// - `Group`/`Picture`(묶음 내 자식으로서의 그림): 자기 struct 의 `caption`
/// - `Chart`/`Ole`: 자기 struct 의 `caption` — 파서(`src/parser/control/shape.rs:213,222`)가
/// `drawing.caption` 을 `.take()` 로 옮기므로 `drawing.caption` 은 항상 `None` 이다. 그래도
/// 방어적으로 두 자리를 모두 확인한다(렌더의 `shape_caption_for_layout` 폴백과 동형).
fn register_border_fills_in_shape(
ctx: &mut SerializeContext,
shape: &crate::model::shape::ShapeObject,
depth: usize,
) -> Result<(), SerializeError> {
if depth >= MAX_BORDER_FILL_SCAN_DEPTH {
return Ok(());
}
use crate::model::shape::ShapeObject;
if let Some(tb) = shape.drawing().and_then(|d| d.text_box.as_ref()) {
register_border_fills_in_paragraphs(ctx, &tb.paragraphs, depth + 1)?;
}
match shape {
ShapeObject::Group(g) => {
if let Some(caption) = &g.caption {
register_border_fills_in_paragraphs(ctx, &caption.paragraphs, depth + 1)?;
}
for child in &g.children {
register_border_fills_in_shape(ctx, child, depth + 1)?;
}
}
ShapeObject::Picture(p) => {
if let Some(caption) = &p.caption {
register_border_fills_in_paragraphs(ctx, &caption.paragraphs, depth + 1)?;
}
}
ShapeObject::Chart(c) => {
if let Some(caption) = c.caption.as_ref().or(c.drawing.caption.as_ref()) {
register_border_fills_in_paragraphs(ctx, &caption.paragraphs, depth + 1)?;
}
}
ShapeObject::Ole(o) => {
if let Some(caption) = o.caption.as_ref().or(o.drawing.caption.as_ref()) {
register_border_fills_in_paragraphs(ctx, &caption.paragraphs, depth + 1)?;
}
}
ShapeObject::Line(_)
| ShapeObject::Rectangle(_)
| ShapeObject::Ellipse(_)
| ShapeObject::Arc(_)
| ShapeObject::Polygon(_)
| ShapeObject::Curve(_) => {
if let Some(caption) = shape.drawing().and_then(|d| d.caption.as_ref()) {
register_border_fills_in_paragraphs(ctx, &caption.paragraphs, depth + 1)?;
}
}
}
Ok(())
}
fn mime_from_ext(ext: &str) -> &'static str {
match ext.to_ascii_lowercase().as_str() {
"png" => "image/png",
"jpg" | "jpeg" => "image/jpeg",
"gif" => "image/gif",
"bmp" => "image/bmp",
"tif" | "tiff" => "image/tiff",
"svg" => "image/svg+xml",
_ => "application/octet-stream",
}
}
pub(super) fn context_error(error: impl std::fmt::Display) -> SerializeError {
SerializeError::XmlError(format!("HWPX context: {error}"))
}
fn insert_id<T: Copy + Eq + std::hash::Hash>(
reservation: &Option<std::sync::Arc<dyn crate::storage::Reservation>>,
values: &mut HashSet<T>,
id: T,
) -> Result<(), String> {
if values.contains(&id) {
return Ok(());
}
if let Some(reservation) = reservation {
if values.len() == values.capacity() {
let capacity = values
.len()
.saturating_add(1)
.max(values.capacity().saturating_mul(2))
.max(8);
reservation.hash_payload(std::mem::size_of::<T>(), capacity)?;
values
.try_reserve(capacity - values.len())
.map_err(|_| reservation.reject("HWPX context ID set allocation"))?;
}
}
values.insert(id);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_doc_has_no_registered_ids() {
let doc = Document::default();
let ctx = SerializeContext::collect_from_document(&doc);
assert_eq!(ctx.char_shape_ids.registered_count(), 0);
assert_eq!(ctx.para_shape_ids.registered_count(), 0);
assert!(ctx.bin_data_map.is_empty());
}
#[test]
fn empty_doc_passes_ref_resolution() {
let doc = Document::default();
let ctx = SerializeContext::collect_from_document(&doc);
ctx.assert_all_refs_resolved().expect("empty doc must pass");
}
/// [Issue #4395] `doc_info.styles` 가 비어 있어도(=`<hh:styles>` 블록 없음) style id 0 은
/// 항상 등록된 것으로 취급해야 한다 — `effective_style_id`(아래)의 기존 계약이자,
/// `write_styles`(header.rs)가 빈 목록일 때 블록 자체를 생략하는 정상 동작과 짝을 이룬다.
/// 수정 전에는 `doc.doc_info.styles` 순회로만 등록해서 목록이 비면 0 도 미등록으로 남아,
/// styleIDRef=0 을 참조하는 문단이 있는 문서(예: `samples/task2156/width_ladder.hwpx`)의
/// 저장 자체가 "미등록 ID 참조 발견: styleIDRef: [0]" 로 하드 실패했다.
#[test]
fn style_zero_always_registered_without_explicit_style_list() {
let doc = Document::default();
assert!(doc.doc_info.styles.is_empty());
let mut ctx = SerializeContext::collect_from_document(&doc);
ctx.style_ids.reference(0);
ctx.assert_all_refs_resolved()
.expect("style 0 must always be registered, even with an empty style list (#4395)");
}
#[test]
fn task1384_border_fill_registered_one_based() {
// borderFill 은 1-based(방출 id=idx+1, borderFillIDRef 1-based)이므로
// N 개 적재 시 마지막 참조 N 이 resolved 되어야 한다 (#1384 — 종전 0-based
// 등록이라 N 이 미등록으로 SERIALIZE_FAIL 했다).
use crate::model::style::BorderFill;
let mut doc = Document::default();
doc.doc_info.border_fills = vec![BorderFill::default(); 31];
let mut ctx = SerializeContext::collect_from_document(&doc);
// exam_social 패턴: charPr 가 borderFillIDRef=31(마지막) 참조.
ctx.border_fill_ids.reference(31);
ctx.assert_all_refs_resolved()
.expect("1-based 등록이면 borderFillIDRef=31 resolved");
// 0 은 1-based 축에 없음(미등록) — 회귀 가드 의미 명시.
assert!(!ctx.border_fill_ids.is_registered(&0));
assert!(ctx.border_fill_ids.is_registered(&31));
}
#[test]
fn task1409_numbering_registered_one_based() {
// numbering 도 1-based(방출 id=idx+1, 실물 1-based) — borderFill 동형(#1384).
// N 개 적재 시 마지막 id=N 이 등록되고 0 은 미등록이어야 한다.
use crate::model::style::Numbering;
let mut doc = Document::default();
doc.doc_info.numberings = vec![Numbering::default(); 8];
let ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.numbering_ids.is_registered(&8),
"1-based 등록이면 마지막 numbering id=8 등록"
);
assert!(
!ctx.numbering_ids.is_registered(&0),
"0 은 1-based 축에 없음 (회귀 가드)"
);
}
#[test]
fn nested_table_border_fill_id_registered() {
// 실측(정부 보고서, 표 107개 중 1개가 셀 안에 표를 담은 문서)에서 재현: 종전에는
// `doc.sections[].paragraphs[]`의 최상위 `Control::Table`만 훑어 셀 안에 중첩된
// 표의 `border_fill_id`가 등록에서 빠졌다. 그 표가 실제 직렬화에서 참조되면
// `assert_all_refs_resolved`가 "미등록 ID 참조 발견"으로 하드 실패해 문서 전체의
// `export-hwpx`가 산출물 없이 실패했다 — 이 테스트는 그 등록 누락의 회귀 가드다.
use crate::model::paragraph::Paragraph;
use crate::model::table::{Cell, Table};
const OUTER_BORDER_FILL_ID: u16 = 1;
const INNER_BORDER_FILL_ID: u16 = 2; // outer와 달라 별도 등록이 필요함을 보장.
let mut inner_table = Table::default();
inner_table.border_fill_id = INNER_BORDER_FILL_ID;
inner_table.row_count = 1;
inner_table.col_count = 1;
inner_table.cells = vec![Cell::new_empty(0, 0, 1000, 1000, INNER_BORDER_FILL_ID)];
let mut outer_cell = Cell::new_empty(0, 0, 5000, 5000, OUTER_BORDER_FILL_ID);
outer_cell.paragraphs = vec![{
let mut p = Paragraph::new_empty();
p.controls.push(Control::Table(Box::new(inner_table)));
p
}];
let mut outer_table = Table::default();
outer_table.border_fill_id = OUTER_BORDER_FILL_ID;
outer_table.row_count = 1;
outer_table.col_count = 1;
outer_table.cells = vec![outer_cell];
let mut doc = Document::default();
let mut para = Paragraph::new_empty();
para.controls.push(Control::Table(Box::new(outer_table)));
doc.sections = vec![crate::model::document::Section {
paragraphs: vec![para],
..Default::default()
}];
let mut ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.border_fill_ids.is_registered(&INNER_BORDER_FILL_ID),
"중첩 표(셀 안의 표)의 border_fill_id도 사전 등록되어야 한다"
);
// table.rs 의 실제 직렬화 경로와 동형: 중첩 표가 참조하는 시점을 재현해도
// assert_all_refs_resolved 가 통과해야 한다(수정 전에는 여기서 실패했다).
ctx.border_fill_ids.reference(OUTER_BORDER_FILL_ID);
ctx.border_fill_ids.reference(INNER_BORDER_FILL_ID);
ctx.assert_all_refs_resolved()
.expect("중첩 표 border_fill_id 참조가 미등록으로 남으면 안 된다");
}
// ── [gestell 리뷰] 문단 리스트 8개 소유자 전수 — 각 자리에 표를 하나씩 심어
// border_fill_id 사전 등록을 개별로 확인한다. 표 셀은 위 테스트가 이미 덮는다.
/// 표 하나(1×1)를 만들어 스스로 border_fill_id 를 갖게 한다 — 소유자 테스트 공용 헬퍼.
fn table_with_border_fill(id: u16) -> crate::model::table::Table {
use crate::model::table::{Cell, Table};
let mut t = Table::default();
t.border_fill_id = id;
t.row_count = 1;
t.col_count = 1;
t.cells = vec![Cell::new_empty(0, 0, 1000, 1000, id)];
t
}
/// 문단 하나에 표 컨트롤 하나를 심은 문단 리스트 — 캡션/메모/숨은설명 등
/// `Vec<Paragraph>` 필드에 그대로 대입해 쓴다.
fn paragraphs_with_table(id: u16) -> Vec<crate::model::paragraph::Paragraph> {
use crate::model::paragraph::Paragraph;
let mut p = Paragraph::new_empty();
p.controls
.push(Control::Table(Box::new(table_with_border_fill(id))));
vec![p]
}
/// 컨트롤 하나를 문서 최상위 문단에 심는다 — 소유자 테스트 공용 헬퍼.
fn doc_with_top_control(ctrl: Control) -> Document {
use crate::model::paragraph::Paragraph;
use crate::model::style::{CharShape, ParaShape, Style};
let mut doc = Document::default();
// 실제 문서는 항상 기본 char_shape/para_shape/style 을 최소 1개씩 갖는다 —
// 완전히 빈 doc_info 는 para_shape_id=0/style_id=0(필드 기본값) 참조조차
// 미등록으로 만들어, border_fill_id 축과 무관한 잡음으로 end-to-end 직렬화가
// 실패한다. 소유자별 회귀 테스트가 실제로 검사하려는 축(border_fill_id)만
// 남기기 위해 기본 항목을 채운다.
doc.doc_info.char_shapes = vec![CharShape::default()];
doc.doc_info.para_shapes = vec![ParaShape::default()];
doc.doc_info.styles = vec![Style::default()];
let mut para = Paragraph::new_empty();
para.controls.push(ctrl);
doc.sections = vec![crate::model::document::Section {
paragraphs: vec![para],
..Default::default()
}];
doc
}
#[test]
fn table_caption_border_fill_id_registered() {
// 표 캡션(Table.caption) 안의 표 — 캡션은 8개 문단 리스트 소유자 중 하나다.
use crate::model::shape::Caption;
const ID: u16 = 21;
let mut outer = table_with_border_fill(1);
outer.caption = Some(Caption {
paragraphs: paragraphs_with_table(ID),
..Default::default()
});
let doc = doc_with_top_control(Control::Table(Box::new(outer)));
let ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.border_fill_ids.is_registered(&ID),
"표 캡션 안의 표 border_fill_id도 사전 등록되어야 한다"
);
}
/// [gestell 리뷰 요청] 실제 export-hwpx 진입점(`serialize_hwpx`)까지 물려서 확인한다 —
/// `SerializeContext` 등록만 보는 위 테스트보다 실측(#4408 재현)에 가까운 형태. 표
/// 캡션 안에 표를 담은 실제 코퍼스 문서를 찾지 못해(§보고 — 재현 문서 없음, 최소 IR로
/// 구성) `serialize_hwpx` 를 직접 호출해 전체 파이프라인이 크래시 없이 끝나는지 본다.
#[test]
fn table_in_caption_serializes_end_to_end_without_crash() {
use crate::model::shape::Caption;
const ID: u16 = 121;
let mut outer = table_with_border_fill(1);
outer.row_count = 1;
outer.col_count = 1;
outer.caption = Some(Caption {
paragraphs: paragraphs_with_table(ID),
..Default::default()
});
let doc = doc_with_top_control(Control::Table(Box::new(outer)));
let result = crate::serializer::hwpx::serialize_hwpx(&doc);
assert!(
result.is_ok(),
"표 캡션 안에 표가 있으면 export-hwpx 가 산출물 없이 실패해서는 안 된다: {:?}",
result.err()
);
}
#[test]
fn shape_default_caption_border_fill_id_registered() {
// 기본 6종 도형(Line 등)의 캡션은 `drawing.caption` 에 있다.
use crate::model::shape::{Caption, LineShape, ShapeObject};
const ID: u16 = 22;
let mut line = LineShape::default();
line.drawing.caption = Some(Caption {
paragraphs: paragraphs_with_table(ID),
..Default::default()
});
let doc = doc_with_top_control(Control::Shape(Box::new(ShapeObject::Line(line))));
let ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.border_fill_ids.is_registered(&ID),
"기본 도형 drawing.caption 안의 표 border_fill_id도 사전 등록되어야 한다"
);
}
#[test]
fn shape_group_caption_and_child_border_fill_id_registered() {
// Group 은 자기 struct 의 `caption` 을 쓰고(drawing 이 없음), 자식 개체도 재귀해야 한다.
use crate::model::shape::{Caption, GroupShape, LineShape, ShapeObject};
const GROUP_CAPTION_ID: u16 = 23;
const CHILD_CAPTION_ID: u16 = 24;
let mut child_line = LineShape::default();
child_line.drawing.caption = Some(Caption {
paragraphs: paragraphs_with_table(CHILD_CAPTION_ID),
..Default::default()
});
let mut group = GroupShape::default();
group.caption = Some(Caption {
paragraphs: paragraphs_with_table(GROUP_CAPTION_ID),
..Default::default()
});
group.children = vec![ShapeObject::Line(child_line)];
let doc = doc_with_top_control(Control::Shape(Box::new(ShapeObject::Group(group))));
let ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.border_fill_ids.is_registered(&GROUP_CAPTION_ID),
"Group.caption 안의 표 border_fill_id도 사전 등록되어야 한다"
);
assert!(
ctx.border_fill_ids.is_registered(&CHILD_CAPTION_ID),
"Group 자식 개체(재귀)의 캡션 안 표 border_fill_id도 사전 등록되어야 한다"
);
}
#[test]
fn shape_picture_variant_caption_border_fill_id_registered() {
// 묶음 내 자식으로서의 그림(ShapeObject::Picture) — 자기 struct 의 `caption`.
use crate::model::image::Picture;
use crate::model::shape::{Caption, ShapeObject};
const ID: u16 = 25;
let mut pic = Picture::default();
pic.caption = Some(Caption {
paragraphs: paragraphs_with_table(ID),
..Default::default()
});
let doc = doc_with_top_control(Control::Shape(Box::new(ShapeObject::Picture(Box::new(
pic,
)))));
let ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.border_fill_ids.is_registered(&ID),
"ShapeObject::Picture.caption 안의 표 border_fill_id도 사전 등록되어야 한다"
);
}
#[test]
fn shape_chart_caption_border_fill_id_registered() {
// Chart — 파서가 drawing.caption 을 자기 struct 의 caption 으로 옮긴다(#4319 계열).
// 두 자리 다 방어적으로 확인하므로 own-field 경로를 실측한다.
use crate::model::shape::{Caption, ChartShape, ShapeObject};
const ID: u16 = 26;
let mut chart = ChartShape::default();
chart.caption = Some(Caption {
paragraphs: paragraphs_with_table(ID),
..Default::default()
});
let doc = doc_with_top_control(Control::Shape(Box::new(ShapeObject::Chart(Box::new(
chart,
)))));
let ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.border_fill_ids.is_registered(&ID),
"Chart.caption 안의 표 border_fill_id도 사전 등록되어야 한다"
);
}
#[test]
fn shape_ole_caption_border_fill_id_registered() {
// Ole — Chart 와 같은 축(own-field caption).
use crate::model::shape::{Caption, OleShape, ShapeObject};
const ID: u16 = 27;
let mut ole = OleShape::default();
ole.caption = Some(Caption {
paragraphs: paragraphs_with_table(ID),
..Default::default()
});
let doc = doc_with_top_control(Control::Shape(Box::new(ShapeObject::Ole(Box::new(ole)))));
let ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.border_fill_ids.is_registered(&ID),
"Ole.caption 안의 표 border_fill_id도 사전 등록되어야 한다"
);
}
#[test]
fn control_picture_caption_border_fill_id_registered() {
// 독립 그림 컨트롤(Control::Picture) — ShapeObject::Picture 와 별개 축.
// 종전 코드는 Control::Picture 자체를 재귀 match 에서 다루지 않았다.
use crate::model::image::Picture;
use crate::model::shape::Caption;
const ID: u16 = 28;
let mut pic = Picture::default();
pic.caption = Some(Caption {
paragraphs: paragraphs_with_table(ID),
..Default::default()
});
let doc = doc_with_top_control(Control::Picture(Box::new(pic)));
let ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.border_fill_ids.is_registered(&ID),
"Control::Picture.caption 안의 표 border_fill_id도 사전 등록되어야 한다"
);
}
#[test]
fn hidden_comment_border_fill_id_registered() {
// 숨은 설명(HiddenComment) — 화면에 안 보여도 파일에는 문단 리스트로 존재한다.
use crate::model::control::HiddenComment;
const ID: u16 = 29;
let hc = HiddenComment {
paragraphs: paragraphs_with_table(ID),
};
let doc = doc_with_top_control(Control::HiddenComment(Box::new(hc)));
let ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.border_fill_ids.is_registered(&ID),
"HiddenComment 안의 표 border_fill_id도 사전 등록되어야 한다"
);
}
#[test]
fn field_memo_paragraphs_border_fill_id_registered() {
// 필드(누름틀 등) 메모 — `Field.memo_paragraphs`.
use crate::model::control::Field;
const ID: u16 = 30;
let field = Field {
memo_paragraphs: paragraphs_with_table(ID),
..Default::default()
};
let doc = doc_with_top_control(Control::Field(field));
let ctx = SerializeContext::collect_from_document(&doc);
assert!(
ctx.border_fill_ids.is_registered(&ID),
"Field.memo_paragraphs 안의 표 border_fill_id도 사전 등록되어야 한다"
);
}
#[test]
fn issue1981_empty_extension_bindata_keeps_no_ext() {
// 빈 확장자 BinData 는 `.bin` 을 붙이지 않고 원본 형태(`image{id}.`)로
// 재직렬화해야 한다 — 라운드트립 확장자 멀티셋 보존(#1981).
use crate::model::bin_data::BinDataContent;
let mut doc = Document::default();
doc.bin_data_content.push(BinDataContent {
id: 6,
data: vec![0, 1, 2].into(),
extension: String::new(),
});
doc.bin_data_content.push(BinDataContent {
id: 7,
data: vec![3, 4, 5].into(),
extension: "bmp".to_string(),
});
let ctx = SerializeContext::collect_from_document(&doc);
let e6 = &ctx.bin_data_map[&6];
assert_eq!(e6.href, "BinData/image6.", "빈 확장자는 .bin 금지");
assert_eq!(e6.media_type, "application/octet-stream");
let e7 = &ctx.bin_data_map[&7];
assert_eq!(e7.href, "BinData/image7.bmp");
}
#[test]
fn issue3526_contentless_embedding_bindata_is_registered() {
// [#3526] 스트림이 없어 `bin_data_content` 가 비는 Embedding/Storage 항목도
// manifest 에 등록돼야 한다. 미등록이면 picture.rs `write_img` 가 Err 를
// 반환해 <hp:pic> 이 통째로 드롭되고 앵커·레이아웃까지 사라진다
// (hwpspec.hwp bin_data_id=37). [#1891] 은 Link 만 막아서 Embedding/Storage
// 는 두 등록 루프를 모두 빠져나갔다.
use crate::model::bin_data::{BinData, BinDataContent, BinDataType};
let mut doc = Document::default();
// 정상 항목(콘텐츠 보유) — 넓힌 루프가 이걸 덮어쓰면 안 된다.
doc.bin_data_content.push(BinDataContent {
id: 1,
data: vec![0, 1, 2].into(),
extension: "png".to_string(),
});
doc.doc_info.bin_data_list.push(BinData {
data_type: BinDataType::Embedding,
storage_id: 1,
extension: Some("png".to_string()),
..Default::default()
});
// 스트림 부재 재현: 목록에는 있으나 콘텐츠가 없는 Embedding / Storage.
doc.doc_info.bin_data_list.push(BinData {
data_type: BinDataType::Embedding,
storage_id: 37,
extension: Some("jpg".to_string()),
..Default::default()
});
doc.doc_info.bin_data_list.push(BinData {
data_type: BinDataType::Storage,
storage_id: 38,
extension: Some("OLE".to_string()),
..Default::default()
});
let ctx = SerializeContext::collect_from_document(&doc);
// `write_img`(picture.rs) 의 분기 조건 그 자체 — Some 이어야 pic 이 산다.
assert_eq!(
ctx.resolve_bin_id(37),
Some("image37"),
"콘텐츠 없는 Embedding 도 등록돼야 <hp:pic> 이 드롭되지 않는다"
);
assert_eq!(
ctx.resolve_bin_id(38),
Some("image38"),
"콘텐츠 없는 Storage 도 동일하게 등록"
);
let e37 = &ctx.bin_data_map[&37];
assert!(
!e37.is_embedded,
"ZIP 엔트리가 없으므로 isEmbeded=0 (mod.rs 3-way 단언 제외 대상)"
);
assert_eq!(e37.media_type, "image/jpeg");
// abs_path 없는 Embedding 은 빈 href — populate_link_image_paths 가 빈
// 경로를 걸러내므로(parser/mod.rs) 허위 external_path 가 생기지 않는다.
assert_eq!(e37.href, "", "존재하지 않는 ZIP 경로를 가리키면 안 된다");
// 콘텐츠 보유 항목은 그대로 임베드로 남아야 한다(덮어쓰기 회귀 가드).
let e1 = &ctx.bin_data_map[&1];
assert!(e1.is_embedded, "콘텐츠 보유 항목은 isEmbeded=1 유지");
assert_eq!(e1.href, "BinData/image1.png");
}
#[test]
fn hwp5_bin_sequence_precedes_colliding_storage_id() {
// HWP5 Picture의 bin_data_id는 storage stream 번호가 아니라 BIN_DATA
// 레코드 순번이다. Worldcup 계열처럼 storage id가 재배열된 경우에도
// 순번 1은 첫 레코드(storage 3)를 가리켜야 한다.
use crate::model::bin_data::{BinData, BinDataContent, BinDataType};
let mut doc = Document::default();
for storage_id in [3u16, 1, 2] {
doc.doc_info.bin_data_list.push(BinData {
data_type: BinDataType::Embedding,
storage_id,
extension: Some("png".to_string()),
..Default::default()
});
doc.bin_data_content.push(BinDataContent {
id: storage_id,
data: vec![storage_id as u8].into(),
extension: "png".to_string(),
});
}
let ctx = SerializeContext::collect_from_document(&doc);
assert_eq!(ctx.resolve_bin_id(1), Some("image3"));
assert_eq!(ctx.resolve_bin_id(2), Some("image1"));
assert_eq!(ctx.resolve_bin_id(3), Some("image2"));
}
#[test]
fn unresolved_char_pr_fails() {
let doc = Document::default();
let mut ctx = SerializeContext::collect_from_document(&doc);
ctx.char_shape_ids.reference(42); // 등록되지 않은 ID 참조
let err = ctx.assert_all_refs_resolved().unwrap_err();
let msg = format!("{}", err);
assert!(
msg.contains("charPrIDRef"),
"error message should name charPrIDRef: {}",
msg
);
assert!(
msg.contains("42"),
"error message should include id 42: {}",
msg
);
}
#[test]
fn id_pool_register_reference_roundtrip() {
let mut pool: IdPool<u32> = IdPool::new();
pool.register(1);
pool.register(2);
pool.reference(1);
pool.reference(3); // 미등록
assert!(pool.is_registered(&1));
assert!(!pool.is_registered(&3));
assert_eq!(pool.unresolved(), vec![3]);
}
#[test]
fn mime_from_ext_covers_common_formats() {
assert_eq!(mime_from_ext("png"), "image/png");
assert_eq!(mime_from_ext("PNG"), "image/png");
assert_eq!(mime_from_ext("jpg"), "image/jpeg");
assert_eq!(mime_from_ext("unknown"), "application/octet-stream");
}
}