hwpers 0.5.0

A Rust library for parsing Korean Hangul Word Processor (HWP) files with full layout rendering support
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
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
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
pub mod serializer;
pub mod style;

use crate::error::{HwpError, Result};
use crate::model::{
    border_fill::BorderFill,
    char_shape::{CharShape, FaceName},
    document::DocumentProperties,
    para_shape::ParaShape,
    paragraph::{ParaText, Paragraph, Section},
    style::Style,
    tab_def::TabDef,
    HwpDocument,
};
use crate::parser::{body_text::BodyText, doc_info::DocInfo, header::FileHeader};
use std::path::Path;

pub struct HwpWriter {
    document: HwpDocument,
    current_section_idx: usize,
    /// Instance ID counter for generating unique IDs
    next_instance_id: u32,
    /// Current list state
    current_list_type: Option<style::ListType>,
    current_list_level: u32,
    current_list_index: u32,
    list_stack: Vec<(style::ListType, u32)>,
    /// Current page layout
    page_layout: crate::model::page_layout::PageLayout,
}

/// Options for custom hyperlink styling
pub struct HyperlinkStyleOptions {
    pub text_color: u32,
    pub underline: bool,
    pub new_window: bool,
}

/// Options for custom text box styling
pub struct CustomTextBoxStyle {
    pub alignment: crate::model::text_box::TextBoxAlignment,
    pub border_style: crate::model::text_box::TextBoxBorderStyle,
    pub border_color: u32,
    pub background_color: u32,
}

/// Options for floating text box styling
pub struct FloatingTextBoxStyle {
    pub opacity: u8,
    pub rotation: i16,
}

impl HwpWriter {
    /// Create a new HWP writer with minimal default structure
    pub fn new() -> Self {
        let header = Self::create_default_header();
        let doc_info = Self::create_default_doc_info();
        let body_texts = vec![Self::create_default_body_text()];

        Self {
            document: HwpDocument {
                header,
                doc_info,
                body_texts,
                preview_text: None,
                preview_image: None,
                summary_info: None,
            },
            current_section_idx: 0,
            next_instance_id: 1,
            current_list_type: None,
            current_list_level: 0,
            current_list_index: 0,
            list_stack: Vec::new(),
            page_layout: crate::model::page_layout::PageLayout::default(),
        }
    }

    /// Add a paragraph with plain text
    pub fn add_paragraph(&mut self, text: &str) -> Result<()> {
        let para_text = ParaText {
            content: text.to_string(),
        };

        let paragraph = Paragraph {
            text: Some(para_text),
            control_mask: 0,
            para_shape_id: 0, // Use default paragraph shape
            style_id: 0,      // Use default style
            column_type: 0,
            char_shape_count: 1,
            range_tag_count: 0,
            line_align_count: 0,
            instance_id: 0,
            char_shapes: None,
            line_segments: None,
            list_header: None,
            ctrl_header: None,
            table_data: None,
            picture_data: None,
            text_box_data: None,
            hyperlinks: Vec::new(),
        };

        // Get the current section and add paragraph
        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add a paragraph with custom text style
    pub fn add_paragraph_with_style(&mut self, text: &str, style: &style::TextStyle) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};

        let para_text = ParaText {
            content: text.to_string(),
        };

        // Get or create font for the style
        let face_name_id = if let Some(font_name) = &style.font_name {
            self.ensure_font(font_name)?
        } else {
            0 // Use default font
        };

        // Create character shape from style
        let char_shape = style.to_char_shape(face_name_id);
        let char_shape_id = self.add_char_shape(char_shape)?;

        // Create character shape information for the paragraph
        let char_shapes = ParaCharShape {
            char_positions: vec![CharPositionShape {
                position: 0,
                char_shape_id,
            }],
        };

        let paragraph = Paragraph {
            text: Some(para_text),
            control_mask: 0,
            para_shape_id: 0,
            style_id: 0,
            column_type: 0,
            char_shape_count: 1,
            range_tag_count: 0,
            line_align_count: 0,
            instance_id: self.next_instance_id(),
            char_shapes: Some(char_shapes),
            line_segments: None,
            list_header: None,
            ctrl_header: None,
            table_data: None,
            picture_data: None,
            text_box_data: None,
            hyperlinks: Vec::new(),
        };

        // Get the current section and add paragraph
        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add a heading with specified level (1-6)
    pub fn add_heading(&mut self, text: &str, level: u8) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};
        use crate::model::para_shape::ParaShape;

        let heading_style = style::HeadingStyle::for_level(level);

        // Get or create font for the heading style
        let face_name_id = if let Some(font_name) = &heading_style.text_style.font_name {
            self.ensure_font(font_name)?
        } else {
            0 // Use default font
        };

        // Create character shape from heading text style
        let char_shape = heading_style.text_style.to_char_shape(face_name_id);
        let char_shape_id = self.add_char_shape(char_shape)?;

        // Create paragraph shape with heading-specific spacing
        let mut para_shape = ParaShape::new_default();
        para_shape.top_para_space = heading_style.spacing_before;
        para_shape.bottom_para_space = heading_style.spacing_after;
        let para_shape_id = self.add_para_shape(para_shape)?;

        // Create paragraph text
        let para_text = ParaText {
            content: text.to_string(),
        };

        // Create character shape information
        let char_shapes = ParaCharShape {
            char_positions: vec![CharPositionShape {
                position: 0,
                char_shape_id,
            }],
        };

        let paragraph = Paragraph {
            text: Some(para_text),
            control_mask: 0,
            para_shape_id,
            style_id: 0,
            column_type: 0,
            char_shape_count: 1,
            range_tag_count: 0,
            line_align_count: 0,
            instance_id: self.next_instance_id(),
            char_shapes: Some(char_shapes),
            line_segments: None,
            list_header: None,
            ctrl_header: None,
            table_data: None,
            picture_data: None,
            text_box_data: None,
            hyperlinks: Vec::new(),
        };

        // Add paragraph to current section
        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add a simple table from 2D string array
    pub fn add_simple_table(&mut self, data: &[Vec<&str>]) -> Result<()> {
        if data.is_empty() {
            // Empty table - do nothing
            return Ok(());
        }

        let rows = data.len() as u32;
        let cols = data[0].len() as u32;

        // Create table builder and populate with data
        let mut table_builder = style::TableBuilder::new(self, rows, cols);

        for (row_idx, row) in data.iter().enumerate() {
            for (col_idx, cell_text) in row.iter().enumerate() {
                table_builder = table_builder.set_cell(row_idx as u32, col_idx as u32, cell_text);
            }
        }

        table_builder.finish()
    }

    /// Create a table builder for advanced table creation
    pub fn add_table(&mut self, rows: u32, cols: u32) -> style::TableBuilder<'_> {
        style::TableBuilder::new(self, rows, cols)
    }

    /// Add a simple list with specified type
    pub fn add_list(&mut self, items: &[&str], list_type: style::ListType) -> Result<()> {
        self.start_list(list_type)?;
        for item in items {
            self.add_list_item(item)?;
        }
        self.end_list()
    }

    /// Start a list with specified type
    pub fn start_list(&mut self, list_type: style::ListType) -> Result<()> {
        // For now, we'll implement lists as styled paragraphs with appropriate prefixes
        // In a full implementation, this would create proper list structures
        self.current_list_type = Some(list_type);
        self.current_list_level = 0;
        self.current_list_index = 0;
        Ok(())
    }

    /// Add an item to the current list
    pub fn add_list_item(&mut self, text: &str) -> Result<()> {
        if let Some(list_type) = &self.current_list_type {
            self.current_list_index += 1;
            let prefix =
                self.get_list_prefix(list_type, self.current_list_index, self.current_list_level);
            let full_text = format!("{} {}", prefix, text);

            // Create paragraph shape with appropriate indentation for this level
            let indent_per_level = 1000; // ~3.5mm per level in HWP units
            let left_margin = 567 + (indent_per_level * self.current_list_level as i32);

            let mut para_shape = crate::model::para_shape::ParaShape::new_default();
            para_shape.left_margin = left_margin;
            para_shape.indent = 0; // No first line indent for list items
            let para_shape_id = self.add_para_shape(para_shape)?;

            // Create the paragraph with proper para_shape_id
            use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};

            let para_text = ParaText { content: full_text };

            let char_shapes = ParaCharShape {
                char_positions: vec![CharPositionShape {
                    position: 0,
                    char_shape_id: 0, // Use default character shape
                }],
            };

            let paragraph = Paragraph {
                text: Some(para_text),
                control_mask: 0,
                para_shape_id,
                style_id: 0,
                column_type: 0,
                char_shape_count: 1,
                range_tag_count: 0,
                line_align_count: 0,
                instance_id: self.next_instance_id(),
                char_shapes: Some(char_shapes),
                line_segments: None,
                list_header: None,
                ctrl_header: None,
                table_data: None,
                picture_data: None,
                text_box_data: None,
                hyperlinks: Vec::new(),
            };

            // Add paragraph to current section
            if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
                if let Some(section) = body_text.sections.get_mut(0) {
                    section.paragraphs.push(paragraph);
                }
            }
        } else {
            return Err(HwpError::InvalidInput(
                "No active list. Call start_list() first.".to_string(),
            ));
        }
        Ok(())
    }

    /// Start a nested list
    pub fn start_nested_list(&mut self, list_type: style::ListType) -> Result<()> {
        self.current_list_level += 1;
        self.list_stack.push((
            self.current_list_type
                .clone()
                .unwrap_or(style::ListType::Bullet),
            self.current_list_index,
        ));
        self.current_list_type = Some(list_type);
        self.current_list_index = 0;
        Ok(())
    }

    /// End the current list
    pub fn end_list(&mut self) -> Result<()> {
        if self.current_list_level > 0 {
            // Return to parent list
            if let Some((parent_type, parent_index)) = self.list_stack.pop() {
                self.current_list_type = Some(parent_type);
                self.current_list_index = parent_index;
                self.current_list_level -= 1;
            }
        } else {
            // End the top-level list
            self.current_list_type = None;
            self.current_list_index = 0;
        }
        Ok(())
    }

    /// Get the appropriate prefix for a list item
    fn get_list_prefix(&self, list_type: &style::ListType, index: u32, level: u32) -> String {
        // No text-based indent - we use paragraph shapes for indentation
        match list_type {
            style::ListType::Bullet => {
                let symbol = match level {
                    0 => "",
                    1 => "",
                    _ => "",
                };
                symbol.to_string()
            }
            style::ListType::Numbered => format!("{}.", index),
            style::ListType::Alphabetic => {
                let letter = ((index - 1) % 26) as u8 + b'a';
                format!("{})", letter as char)
            }
            style::ListType::Roman => {
                let roman = self.to_roman(index);
                format!("{}.", roman)
            }
            style::ListType::Korean => {
                let korean_nums = ["", "", "", "", "", "", "", "", "", ""];
                let korean = korean_nums
                    .get((index - 1) as usize % korean_nums.len())
                    .unwrap_or(&"");
                format!("{}.", korean)
            }
            style::ListType::Custom(format) => format.clone(),
        }
    }

    /// Convert number to Roman numerals
    fn to_roman(&self, mut num: u32) -> String {
        let values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
        let symbols = [
            "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I",
        ];

        let mut result = String::new();
        for (i, &value) in values.iter().enumerate() {
            while num >= value {
                result.push_str(symbols[i]);
                num -= value;
            }
        }
        result.to_lowercase()
    }

    /// Add an image from file path
    pub fn add_image<P: AsRef<std::path::Path>>(&mut self, path: P) -> Result<()> {
        let image_data = std::fs::read(path)?;
        let format = style::ImageFormat::from_bytes(&image_data).unwrap_or(style::ImageFormat::Png);
        let options = style::ImageOptions::new();
        self.add_image_with_options(&image_data, format, &options)
    }

    /// Add an image from byte data
    pub fn add_image_from_bytes(&mut self, data: &[u8], format: style::ImageFormat) -> Result<()> {
        let options = style::ImageOptions::new();
        self.add_image_with_options(data, format, &options)
    }

    /// Add an image with custom options
    pub fn add_image_with_options(
        &mut self,
        data: &[u8],
        format: style::ImageFormat,
        options: &style::ImageOptions,
    ) -> Result<()> {
        use crate::model::bin_data::BinData;
        use crate::model::control::Picture;
        use crate::model::ctrl_header::{ControlType, CtrlHeader};

        // Calculate bin_id (1-based index)
        let bin_id = (self.document.doc_info.bin_data.len() + 1) as u16;

        // Create binary data entry
        let bin_data = BinData {
            properties: 0,
            abs_name: format!("image{}.{}", bin_id, format.extension()),
            rel_name: format!("image_{}.{}", self.next_instance_id(), format.extension()),
            bin_id,
            extension: format.extension().to_string(),
            data: data.to_vec(),
        };

        // Add to document's binary data collection
        self.document.doc_info.bin_data.push(bin_data.clone());

        // Calculate dimensions (convert mm to HWPUNIT)
        let hwp_scale = 7200.0 / 25.4;
        let width = options.width.unwrap_or(50) as f32 * hwp_scale; // Default 50mm
        let height = options.height.unwrap_or(50) as f32 * hwp_scale; // Default 50mm

        // Create picture control
        let picture = Picture {
            properties: 0,
            left: 0,
            top: 0,
            right: width as i32,
            bottom: height as i32,
            z_order: 0,
            outer_margin_left: 0,
            outer_margin_right: 0,
            outer_margin_top: 0,
            outer_margin_bottom: 0,
            instance_id: self.next_instance_id(),
            bin_item_id: bin_data.bin_id,
            border_fill_id: 0,
            image_width: width as u32,
            image_height: height as u32,
        };

        // Create control header
        let ctrl_header = CtrlHeader {
            ctrl_id: ControlType::Gso as u32, // Gso is for graphics/drawing objects including images
            properties: 0,
            instance_id: self.next_instance_id(),
        };

        // Create paragraph containing the image (no text - picture control paragraph)
        let paragraph = Paragraph {
            text: None,      // Picture control paragraphs don't contain text
            control_mask: 2, // Control header present (0x02)
            para_shape_id: 0,
            style_id: 0,
            column_type: 0,
            char_shape_count: 0,
            range_tag_count: 0,
            line_align_count: 0,
            instance_id: self.next_instance_id(),
            char_shapes: None,
            line_segments: None,
            list_header: None,
            ctrl_header: Some(ctrl_header),
            table_data: None,
            picture_data: Some(picture),
            text_box_data: None,
            hyperlinks: Vec::new(),
        };

        // Add the picture control paragraph to the document
        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        // If caption is provided, add it as a separate paragraph
        if let Some(caption) = &options.caption {
            let caption_text = format!("그림: {}", caption);
            self.add_paragraph(&caption_text)?;
        }

        Ok(())
    }

    /// Add a hyperlink to URL
    pub fn add_hyperlink(&mut self, display_text: &str, url: &str) -> Result<()> {
        use crate::model::hyperlink::{Hyperlink, HyperlinkDisplay, HyperlinkType};

        let hyperlink = Hyperlink {
            hyperlink_type: HyperlinkType::Url,
            display_text: display_text.to_string(),
            target_url: url.to_string(),
            tooltip: None,
            display_mode: HyperlinkDisplay::TextOnly,
            text_color: 0x0000FF,    // Blue
            visited_color: 0x800080, // Purple
            underline: true,
            visited: false,
            open_in_new_window: false,
            start_position: 0,
            length: display_text.len() as u32,
        };

        self.add_hyperlink_with_options(hyperlink)
    }

    /// Add an email hyperlink
    pub fn add_email_link(&mut self, display_text: &str, email: &str) -> Result<()> {
        use crate::model::hyperlink::{Hyperlink, HyperlinkDisplay, HyperlinkType};

        let hyperlink = Hyperlink {
            hyperlink_type: HyperlinkType::Email,
            display_text: display_text.to_string(),
            target_url: format!("mailto:{}", email),
            tooltip: Some(format!("이메일 보내기: {}", email)),
            display_mode: HyperlinkDisplay::TextOnly,
            text_color: 0x0000FF,
            visited_color: 0x800080,
            underline: true,
            visited: false,
            open_in_new_window: false,
            start_position: 0,
            length: display_text.len() as u32,
        };

        self.add_hyperlink_with_options(hyperlink)
    }

    /// Add a file hyperlink
    pub fn add_file_link(&mut self, display_text: &str, file_path: &str) -> Result<()> {
        use crate::model::hyperlink::{Hyperlink, HyperlinkDisplay, HyperlinkType};

        let hyperlink = Hyperlink {
            hyperlink_type: HyperlinkType::File,
            display_text: display_text.to_string(),
            target_url: file_path.to_string(),
            tooltip: Some(format!("파일 열기: {}", file_path)),
            display_mode: HyperlinkDisplay::TextOnly,
            text_color: 0x008000, // Green for file links
            visited_color: 0x800080,
            underline: true,
            visited: false,
            open_in_new_window: false,
            start_position: 0,
            length: display_text.len() as u32,
        };

        self.add_hyperlink_with_options(hyperlink)
    }

    /// Add a hyperlink with custom options
    pub fn add_hyperlink_with_options(
        &mut self,
        hyperlink: crate::model::hyperlink::Hyperlink,
    ) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};

        // Create a styled paragraph for the hyperlink
        let hyperlink_style = style::TextStyle::new()
            .color(hyperlink.text_color)
            .underline();

        let para_text = ParaText {
            content: hyperlink.display_text.clone(),
        };

        // Get or create font for the hyperlink style
        let char_shape = hyperlink_style.to_char_shape(0); // Use default font
        let char_shape_id = self.add_char_shape(char_shape)?;

        // Create character shape information
        let char_shapes = ParaCharShape {
            char_positions: vec![CharPositionShape {
                position: 0,
                char_shape_id,
            }],
        };

        let paragraph = Paragraph {
            text: Some(para_text),
            control_mask: 0,
            para_shape_id: 0,
            style_id: 0,
            column_type: 0,
            char_shape_count: 1,
            range_tag_count: 0,
            line_align_count: 0,
            instance_id: self.next_instance_id(),
            char_shapes: Some(char_shapes),
            line_segments: None,
            list_header: None,
            ctrl_header: None,
            table_data: None,
            picture_data: None,
            text_box_data: None,
            hyperlinks: vec![hyperlink],
        };

        // Add the paragraph to the document
        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add a bookmark hyperlink
    pub fn add_bookmark_link(&mut self, display_text: &str, bookmark_name: &str) -> Result<()> {
        use crate::model::hyperlink::{Hyperlink, HyperlinkDisplay, HyperlinkType};

        let hyperlink = Hyperlink {
            hyperlink_type: HyperlinkType::Bookmark,
            display_text: display_text.to_string(),
            target_url: format!("#{}", bookmark_name),
            tooltip: Some(format!("이동: {}", bookmark_name)),
            display_mode: HyperlinkDisplay::TextOnly,
            text_color: 0x800080, // Purple for internal links
            visited_color: 0x800080,
            underline: true,
            visited: false,
            open_in_new_window: false,
            start_position: 0,
            length: display_text.len() as u32,
        };

        self.add_hyperlink_with_options(hyperlink)
    }

    /// Add a custom hyperlink with specific options
    pub fn add_custom_hyperlink(
        &mut self,
        display_text: &str,
        hyperlink_type: crate::model::hyperlink::HyperlinkType,
        target_url: &str,
        display_mode: crate::model::hyperlink::HyperlinkDisplay,
        style_options: HyperlinkStyleOptions,
    ) -> Result<()> {
        use crate::model::hyperlink::Hyperlink;

        let hyperlink = Hyperlink {
            hyperlink_type,
            display_text: display_text.to_string(),
            target_url: target_url.to_string(),
            tooltip: None,
            display_mode,
            text_color: style_options.text_color,
            visited_color: 0x800080,
            underline: style_options.underline,
            visited: false,
            open_in_new_window: style_options.new_window,
            start_position: 0,
            length: display_text.len() as u32,
        };

        self.add_hyperlink_with_options(hyperlink)
    }

    /// Add a paragraph with multiple hyperlinks
    pub fn add_paragraph_with_hyperlinks(
        &mut self,
        text: &str,
        hyperlinks: Vec<crate::model::hyperlink::Hyperlink>,
    ) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};

        let para_text = ParaText {
            content: text.to_string(),
        };

        // Create character shape
        let char_shape = style::TextStyle::new().to_char_shape(0);
        let char_shape_id = self.add_char_shape(char_shape)?;

        // Create character shape information
        let char_shapes = ParaCharShape {
            char_positions: vec![CharPositionShape {
                position: 0,
                char_shape_id,
            }],
        };

        // Create paragraph
        let paragraph = Paragraph {
            text: Some(para_text),
            control_mask: 0,
            para_shape_id: 0,
            style_id: 0,
            column_type: 0,
            char_shape_count: 1,
            range_tag_count: 0,
            line_align_count: 1,
            instance_id: 0,
            char_shapes: Some(char_shapes),
            line_segments: None,
            list_header: None,
            ctrl_header: None,
            table_data: None,
            picture_data: None,
            text_box_data: None,
            hyperlinks,
        };

        // Add the paragraph to the document
        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add a header to the current section
    pub fn add_header(&mut self, text: &str) {
        use crate::model::header_footer::HeaderFooter;

        // Create header footer collection if not exists
        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                if section.page_def.is_none() {
                    section.page_def = Some(crate::model::page_def::PageDef::new_default());
                }
                if let Some(page_def) = section.page_def.as_mut() {
                    let header = HeaderFooter::new_header(text);
                    page_def.header_footer.add_header(header);
                }
            }
        }
    }

    /// Add a footer with page number
    pub fn add_footer_with_page_number(
        &mut self,
        prefix: &str,
        format: crate::model::header_footer::PageNumberFormat,
    ) {
        use crate::model::header_footer::HeaderFooter;

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                if section.page_def.is_none() {
                    section.page_def = Some(crate::model::page_def::PageDef::new_default());
                }
                if let Some(page_def) = section.page_def.as_mut() {
                    let mut footer = HeaderFooter::new_footer(prefix);
                    footer = footer.with_page_number(format);
                    page_def.header_footer.add_footer(footer);
                }
            }
        }
    }

    /// Set page layout for the document
    pub fn set_page_layout(&mut self, layout: crate::model::page_layout::PageLayout) -> Result<()> {
        use crate::model::page_def::PageDef;

        // Create page definition from layout
        let page_def = PageDef::from_layout(layout);

        // Apply to current section
        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.page_def = Some(page_def);
            }
        }

        Ok(())
    }

    /// Set A4 portrait layout with default margins
    pub fn set_a4_portrait(&mut self) -> Result<()> {
        let layout = crate::model::page_layout::PageLayout::a4_portrait();
        self.set_page_layout(layout)
    }

    /// Add a paragraph with specific alignment
    pub fn add_aligned_paragraph(
        &mut self,
        text: &str,
        alignment: style::ParagraphAlignment,
    ) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};
        use crate::model::para_shape::ParaShape;

        // Create para shape with alignment
        let mut para_shape = ParaShape::new_default();
        // Set alignment in properties1 (bits 2-4)
        para_shape.properties1 = (para_shape.properties1 & !0x1C) | ((alignment as u32) << 2);
        let para_shape_id = self.add_para_shape(para_shape)?;

        let para_text = ParaText {
            content: text.to_string(),
        };

        // Create character shape
        let char_shape = style::TextStyle::new().to_char_shape(0);
        let char_shape_id = self.add_char_shape(char_shape)?;

        // Create character shape information
        let char_shapes = ParaCharShape {
            char_positions: vec![CharPositionShape {
                position: 0,
                char_shape_id,
            }],
        };

        // Create paragraph with alignment
        let paragraph = Paragraph {
            text: Some(para_text),
            control_mask: 0,
            para_shape_id,
            style_id: 0,
            column_type: 0,
            char_shape_count: 1,
            range_tag_count: 0,
            line_align_count: 1,
            instance_id: 0,
            char_shapes: Some(char_shapes),
            line_segments: None,
            list_header: None,
            ctrl_header: None,
            table_data: None,
            picture_data: None,
            text_box_data: None,
            hyperlinks: Vec::new(),
        };

        // Add the paragraph to the document
        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add a paragraph with custom spacing
    pub fn add_paragraph_with_spacing(
        &mut self,
        text: &str,
        line_spacing_percent: u32,
        before_spacing_mm: f32,
        after_spacing_mm: f32,
    ) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};
        use crate::model::para_shape::ParaShape;

        // Create para shape with spacing
        let mut para_shape = ParaShape::new_default();
        para_shape.line_space = (line_spacing_percent * 100) as i32; // Convert percent to internal units
        para_shape.top_para_space = (before_spacing_mm * 283.465) as i32; // Convert mm to HWP units
        para_shape.bottom_para_space = (after_spacing_mm * 283.465) as i32;
        let para_shape_id = self.add_para_shape(para_shape)?;

        let para_text = ParaText {
            content: text.to_string(),
        };

        // Create character shape
        let char_shape = style::TextStyle::new().to_char_shape(0);
        let char_shape_id = self.add_char_shape(char_shape)?;

        // Create character shape information
        let char_shapes = ParaCharShape {
            char_positions: vec![CharPositionShape {
                position: 0,
                char_shape_id,
            }],
        };

        // Create paragraph with spacing
        let paragraph = Paragraph {
            text: Some(para_text),
            control_mask: 0,
            para_shape_id,
            style_id: 0,
            column_type: 0,
            char_shape_count: 1,
            range_tag_count: 0,
            line_align_count: 1,
            instance_id: 0,
            char_shapes: Some(char_shapes),
            line_segments: None,
            list_header: None,
            ctrl_header: None,
            table_data: None,
            picture_data: None,
            text_box_data: None,
            hyperlinks: Vec::new(),
        };

        // Add the paragraph to the document
        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Set A4 landscape layout with default margins
    pub fn set_a4_landscape(&mut self) -> Result<()> {
        let layout = crate::model::page_layout::PageLayout::a4_landscape();
        self.set_page_layout(layout)
    }

    /// Set Letter portrait layout with default margins
    pub fn set_letter_portrait(&mut self) -> Result<()> {
        let layout = crate::model::page_layout::PageLayout::letter_portrait();
        self.set_page_layout(layout)
    }

    /// Set Letter landscape layout with default margins
    pub fn set_letter_landscape(&mut self) -> Result<()> {
        let layout = crate::model::page_layout::PageLayout::letter_landscape();
        self.set_page_layout(layout)
    }

    /// Set custom page size in millimeters
    pub fn set_custom_page_size(
        &mut self,
        width_mm: f32,
        height_mm: f32,
        orientation: crate::model::page_layout::PageOrientation,
    ) -> Result<()> {
        let layout =
            crate::model::page_layout::PageLayout::custom_mm(width_mm, height_mm, orientation);
        self.set_page_layout(layout)
    }

    /// Set page margins in millimeters
    pub fn set_page_margins_mm(&mut self, left: f32, right: f32, top: f32, bottom: f32) {
        use crate::model::page_layout::mm_to_hwp_units;
        self.page_layout.margins.left = mm_to_hwp_units(left);
        self.page_layout.margins.right = mm_to_hwp_units(right);
        self.page_layout.margins.top = mm_to_hwp_units(top);
        self.page_layout.margins.bottom = mm_to_hwp_units(bottom);
    }

    /// Set narrow margins (Office style)
    pub fn set_narrow_margins(&mut self) {
        self.page_layout.margins = crate::model::page_layout::PageMargins::narrow();
    }

    /// Set normal margins (Office style)
    pub fn set_normal_margins(&mut self) {
        self.page_layout.margins = crate::model::page_layout::PageMargins::normal();
    }

    /// Set wide margins (Office style)
    pub fn set_wide_margins(&mut self) {
        self.page_layout.margins = crate::model::page_layout::PageMargins::wide();
    }

    /// Set multiple columns
    pub fn set_columns(&mut self, columns: u16, spacing_mm: f32) {
        use crate::model::page_layout::mm_to_hwp_units;
        self.page_layout.columns = columns;
        self.page_layout.column_spacing = mm_to_hwp_units(spacing_mm);
    }

    /// Set page background color
    pub fn set_page_background_color(&mut self, color: u32) {
        self.page_layout.background_color = Some(color);
    }

    /// Set page numbering
    pub fn set_page_numbering(
        &mut self,
        start: u16,
        format: crate::model::header_footer::PageNumberFormat,
    ) -> Result<()> {
        let mut layout = crate::model::page_layout::PageLayout::default();
        layout = layout.with_page_numbering(start, format);
        self.set_page_layout(layout)
    }

    /// Convert the document to bytes
    pub fn to_bytes(&self) -> Result<Vec<u8>> {
        serializer::serialize_document(&self.document)
    }

    /// Save to file
    pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let bytes = self.to_bytes()?;
        std::fs::write(path, bytes).map_err(HwpError::Io)?;
        Ok(())
    }

    /// Create default file header
    fn create_default_header() -> FileHeader {
        FileHeader::new_default()
    }

    /// Create default document info with minimal required data
    fn create_default_doc_info() -> DocInfo {
        DocInfo {
            properties: Some(DocumentProperties::default()),
            face_names: vec![FaceName::new_default("맑은 고딕".to_string())],
            char_shapes: vec![
                CharShape::new_default(), // Default 12pt font
            ],
            para_shapes: vec![
                ParaShape::new_default(), // Default left-aligned paragraph
            ],
            styles: vec![Style::new_default()],
            border_fills: vec![BorderFill::new_default()],
            tab_defs: vec![TabDef::new_default()],
            numberings: Vec::new(),
            bullets: Vec::new(),
            bin_data: Vec::new(),
        }
    }

    /// Create default body text with one empty section
    fn create_default_body_text() -> BodyText {
        let section = Section {
            paragraphs: Vec::new(),
            section_def: None,
            page_def: None,
        };

        BodyText {
            sections: vec![section],
        }
    }

    /// Generate and return next unique instance ID
    pub fn next_instance_id(&mut self) -> u32 {
        let id = self.next_instance_id;
        self.next_instance_id += 1;
        id
    }

    /// Ensure a font exists in the document and return its ID
    pub fn ensure_font(&mut self, font_name: &str) -> Result<u16> {
        // Check if font already exists
        for (i, face_name) in self.document.doc_info.face_names.iter().enumerate() {
            if face_name.font_name == font_name {
                return Ok(i as u16);
            }
        }

        // Add new font
        let face_name = FaceName::new_default(font_name.to_string());
        self.document.doc_info.face_names.push(face_name);
        Ok((self.document.doc_info.face_names.len() - 1) as u16)
    }

    /// Add a character shape to the document and return its ID
    pub fn add_char_shape(&mut self, char_shape: CharShape) -> Result<u16> {
        self.document.doc_info.char_shapes.push(char_shape);
        Ok((self.document.doc_info.char_shapes.len() - 1) as u16)
    }

    /// Add a paragraph shape to the document and return its ID
    pub fn add_para_shape(
        &mut self,
        para_shape: crate::model::para_shape::ParaShape,
    ) -> Result<u16> {
        self.document.doc_info.para_shapes.push(para_shape);
        Ok((self.document.doc_info.para_shapes.len() - 1) as u16)
    }
}

impl HwpWriter {
    /// Create a writer from an existing HwpDocument
    pub fn from_document(document: HwpDocument) -> Self {
        Self {
            document,
            current_section_idx: 0,
            next_instance_id: 1,
            current_list_type: None,
            current_list_level: 0,
            current_list_index: 0,
            list_stack: Vec::new(),
            page_layout: crate::model::page_layout::PageLayout::default(),
        }
    }

    /// Get a reference to the underlying document
    pub fn document(&self) -> &HwpDocument {
        &self.document
    }
}

impl Default for HwpWriter {
    fn default() -> Self {
        Self::new()
    }
}

// Page Layout Methods
impl HwpWriter {
    /// Get current page layout
    pub fn get_page_layout(&self) -> crate::model::page_layout::PageLayout {
        self.page_layout.clone()
    }

    /// Set paper size
    pub fn set_paper_size(&mut self, paper_size: crate::model::page_layout::PaperSize) {
        let (width, height) = paper_size.dimensions_hwp_units();
        let (final_width, final_height) = match self.page_layout.orientation {
            crate::model::page_layout::PageOrientation::Portrait => (width, height),
            crate::model::page_layout::PageOrientation::Landscape => (height, width),
        };
        self.page_layout.paper_size = paper_size;
        self.page_layout.width = final_width;
        self.page_layout.height = final_height;
    }

    /// Set page orientation
    pub fn set_page_orientation(
        &mut self,
        orientation: crate::model::page_layout::PageOrientation,
    ) {
        if self.page_layout.orientation != orientation {
            // Swap width and height when changing orientation
            std::mem::swap(&mut self.page_layout.width, &mut self.page_layout.height);
            self.page_layout.orientation = orientation;
        }
    }

    /// Set page margins in inches
    pub fn set_page_margins_inches(&mut self, left: f32, right: f32, top: f32, bottom: f32) {
        use crate::model::page_layout::inches_to_hwp_units;
        self.page_layout.margins.left = inches_to_hwp_units(left);
        self.page_layout.margins.right = inches_to_hwp_units(right);
        self.page_layout.margins.top = inches_to_hwp_units(top);
        self.page_layout.margins.bottom = inches_to_hwp_units(bottom);
    }

    /// Set custom page size in millimeters
    pub fn set_custom_page_size_mm(&mut self, width_mm: f32, height_mm: f32) {
        use crate::model::page_layout::mm_to_hwp_units;
        self.page_layout.paper_size = crate::model::page_layout::PaperSize::Custom;
        let width = mm_to_hwp_units(width_mm);
        let height = mm_to_hwp_units(height_mm);
        let (final_width, final_height) = match self.page_layout.orientation {
            crate::model::page_layout::PageOrientation::Portrait => (width, height),
            crate::model::page_layout::PageOrientation::Landscape => (height, width),
        };
        self.page_layout.width = final_width;
        self.page_layout.height = final_height;
    }
}

// Styled Text Methods
impl HwpWriter {
    /// Helper to convert TextStyle to CharShape
    fn text_style_to_char_shape(&self, style: &style::TextStyle) -> CharShape {
        let mut char_shape = CharShape::new_default();

        // Set properties
        let mut properties = 0u32;
        if style.bold {
            properties |= 0x1; // Bit 0 for bold
        }
        if style.italic {
            properties |= 0x2; // Bit 1 for italic
        }
        if style.underline {
            properties |= 0x1 << 2; // Bits 2-4 for underline (type 1)
        }
        if style.strikethrough {
            properties |= 0x1 << 5; // Bits 5-7 for strikethrough (type 1)
        }
        char_shape.properties = properties;

        // Set colors
        char_shape.text_color = style.color;
        if let Some(bg_color) = style.background_color {
            char_shape.shade_color = bg_color;
        }

        // Set font size if specified
        if let Some(size) = style.font_size {
            char_shape.base_size = (size * 100) as i32; // Convert pt to HWP units
        }

        char_shape
    }

    /// Add a styled paragraph
    pub fn add_styled_paragraph(&mut self, styled_text: &style::StyledText) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};
        use crate::model::paragraph::{ParaText, Paragraph};

        let text = &styled_text.text;
        let ranges = &styled_text.ranges;

        // Create CharShape for each unique style and build position-shape pairs
        let mut char_positions = Vec::new();
        let default_char_shape_id = 0u16; // Use default shape for unstyled text

        for range in ranges {
            let char_shape = self.text_style_to_char_shape(&range.style);
            let char_shape_id = self.add_char_shape(char_shape)?;

            char_positions.push(CharPositionShape {
                position: range.start as u32,
                char_shape_id,
            });
        }

        // Sort by position
        char_positions.sort_by_key(|p| p.position);

        let para_char_shape = if char_positions.is_empty() {
            ParaCharShape::new_single_shape(default_char_shape_id)
        } else {
            ParaCharShape { char_positions }
        };

        let paragraph = Paragraph {
            text: Some(ParaText {
                content: text.clone(),
            }),
            char_shapes: Some(para_char_shape.clone()),
            char_shape_count: para_char_shape.char_positions.len() as u16,
            ..Default::default()
        };

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add paragraph with bold ranges
    pub fn add_paragraph_with_bold(
        &mut self,
        text: &str,
        bold_ranges: Vec<(usize, usize)>,
    ) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};
        use crate::model::paragraph::{ParaText, Paragraph};

        let mut char_positions = Vec::new();

        for (start, _end) in bold_ranges {
            let mut char_shape = CharShape::new_default();
            char_shape.properties = 0x1; // Bold
            let char_shape_id = self.add_char_shape(char_shape)?;

            char_positions.push(CharPositionShape {
                position: start as u32,
                char_shape_id,
            });
        }

        char_positions.sort_by_key(|p| p.position);

        let para_char_shape = ParaCharShape { char_positions };

        let paragraph = Paragraph {
            text: Some(ParaText {
                content: text.to_string(),
            }),
            char_shapes: Some(para_char_shape.clone()),
            char_shape_count: para_char_shape.char_positions.len() as u16,
            ..Default::default()
        };

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add paragraph with colors
    pub fn add_paragraph_with_colors(
        &mut self,
        text: &str,
        color_ranges: Vec<(usize, usize, u32)>,
    ) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};
        use crate::model::paragraph::{ParaText, Paragraph};

        let mut char_positions = Vec::new();

        for (start, _end, color) in color_ranges {
            let mut char_shape = CharShape::new_default();
            char_shape.text_color = color;
            let char_shape_id = self.add_char_shape(char_shape)?;

            char_positions.push(CharPositionShape {
                position: start as u32,
                char_shape_id,
            });
        }

        char_positions.sort_by_key(|p| p.position);

        let para_char_shape = ParaCharShape { char_positions };

        let paragraph = Paragraph {
            text: Some(ParaText {
                content: text.to_string(),
            }),
            char_shapes: Some(para_char_shape.clone()),
            char_shape_count: para_char_shape.char_positions.len() as u16,
            ..Default::default()
        };

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add paragraph with highlight
    pub fn add_paragraph_with_highlight(
        &mut self,
        text: &str,
        highlight_ranges: Vec<(usize, usize, u32)>,
    ) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};
        use crate::model::paragraph::{ParaText, Paragraph};

        let mut char_positions = Vec::new();

        for (start, _end, color) in highlight_ranges {
            let mut char_shape = CharShape::new_default();
            char_shape.shade_color = color; // Use shade_color for background/highlight
            let char_shape_id = self.add_char_shape(char_shape)?;

            char_positions.push(CharPositionShape {
                position: start as u32,
                char_shape_id,
            });
        }

        char_positions.sort_by_key(|p| p.position);

        let para_char_shape = ParaCharShape { char_positions };

        let paragraph = Paragraph {
            text: Some(ParaText {
                content: text.to_string(),
            }),
            char_shapes: Some(para_char_shape.clone()),
            char_shape_count: para_char_shape.char_positions.len() as u16,
            ..Default::default()
        };

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add mixed text with multiple styles
    pub fn add_mixed_text(
        &mut self,
        text: &str,
        style_ranges: Vec<(usize, usize, style::TextStyle)>,
    ) -> Result<()> {
        use crate::model::para_char_shape::{CharPositionShape, ParaCharShape};
        use crate::model::paragraph::{ParaText, Paragraph};

        let mut char_positions = Vec::new();

        for (start, _end, text_style) in style_ranges {
            let char_shape = self.text_style_to_char_shape(&text_style);
            let char_shape_id = self.add_char_shape(char_shape)?;

            char_positions.push(CharPositionShape {
                position: start as u32,
                char_shape_id,
            });
        }

        char_positions.sort_by_key(|p| p.position);

        let para_char_shape = ParaCharShape { char_positions };

        let paragraph = Paragraph {
            text: Some(ParaText {
                content: text.to_string(),
            }),
            char_shapes: Some(para_char_shape.clone()),
            char_shape_count: para_char_shape.char_positions.len() as u16,
            ..Default::default()
        };

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }
}

// Text Box Methods
impl HwpWriter {
    /// Add a simple text box
    pub fn add_text_box(&mut self, text: &str) -> Result<()> {
        use crate::model::ctrl_header::CtrlHeader;
        use crate::model::paragraph::ParaText;
        use crate::model::text_box::TextBox;

        let text_box = TextBox::new(text);

        let ctrl_header = CtrlHeader {
            ctrl_id: 0x7874, // TextBox control ID
            properties: 0,
            instance_id: 0,
        };

        let paragraph = Paragraph {
            text: Some(ParaText {
                content: String::new(),
            }),
            control_mask: 0x02, // Control header present
            para_shape_id: 0,
            style_id: 0,
            column_type: 0,
            char_shape_count: 0,
            range_tag_count: 0,
            line_align_count: 0,
            instance_id: 0,
            char_shapes: None,
            line_segments: None,
            list_header: None,
            ctrl_header: Some(ctrl_header),
            table_data: None,
            picture_data: None,
            text_box_data: Some(text_box),
            hyperlinks: Vec::new(),
        };

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add text box at specific position
    pub fn add_text_box_at_position(
        &mut self,
        text: &str,
        x: u32,
        y: u32,
        width: u32,
        height: u32,
    ) -> Result<()> {
        use crate::model::ctrl_header::CtrlHeader;
        use crate::model::paragraph::ParaText;
        use crate::model::text_box::TextBox;

        let text_box = TextBox::new(text)
            .with_position_mm(x as i32, y as i32)
            .with_size_mm(width, height);

        let ctrl_header = CtrlHeader {
            ctrl_id: 0x7874, // TextBox control ID
            properties: 0,
            instance_id: 0,
        };

        let paragraph = Paragraph {
            text: Some(ParaText {
                content: String::new(),
            }),
            control_mask: 0x02,
            para_shape_id: 0,
            style_id: 0,
            column_type: 0,
            char_shape_count: 0,
            range_tag_count: 0,
            line_align_count: 0,
            instance_id: 0,
            char_shapes: None,
            line_segments: None,
            list_header: None,
            ctrl_header: Some(ctrl_header),
            table_data: None,
            picture_data: None,
            text_box_data: Some(text_box),
            hyperlinks: Vec::new(),
        };

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add custom text box
    pub fn add_custom_text_box(
        &mut self,
        text: &str,
        x: u32,
        y: u32,
        width: u32,
        height: u32,
        style: CustomTextBoxStyle,
    ) -> Result<()> {
        use crate::model::ctrl_header::CtrlHeader;
        use crate::model::paragraph::ParaText;
        use crate::model::text_box::TextBox;

        let text_box = TextBox::new(text)
            .with_position_mm(x as i32, y as i32)
            .with_size_mm(width, height)
            .with_alignment(style.alignment)
            .with_border(style.border_style, 1, style.border_color)
            .with_background(style.background_color);

        let ctrl_header = CtrlHeader {
            ctrl_id: 0x7874, // TextBox control ID
            properties: 0,
            instance_id: 0,
        };

        let paragraph = Paragraph {
            text: Some(ParaText {
                content: String::new(),
            }),
            control_mask: 0x02,
            para_shape_id: 0,
            style_id: 0,
            column_type: 0,
            char_shape_count: 0,
            range_tag_count: 0,
            line_align_count: 0,
            instance_id: 0,
            char_shapes: None,
            line_segments: None,
            list_header: None,
            ctrl_header: Some(ctrl_header),
            table_data: None,
            picture_data: None,
            text_box_data: Some(text_box),
            hyperlinks: Vec::new(),
        };

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add styled text box
    pub fn add_styled_text_box(&mut self, text: &str, style: &str) -> Result<()> {
        use crate::model::ctrl_header::CtrlHeader;
        use crate::model::paragraph::ParaText;
        use crate::model::text_box::TextBox;

        let text_box = match style {
            "basic" => TextBox::basic(text),
            "highlight" => TextBox::highlight(text),
            "warning" => TextBox::warning(text),
            "info" => TextBox::info(text),
            "transparent" => TextBox::transparent(text),
            "bubble" => TextBox::bubble(text),
            _ => TextBox::basic(text),
        };

        let ctrl_header = CtrlHeader {
            ctrl_id: 0x7874, // TextBox control ID
            properties: 0,
            instance_id: 0,
        };

        let paragraph = Paragraph {
            text: Some(ParaText {
                content: String::new(),
            }),
            control_mask: 0x02,
            para_shape_id: 0,
            style_id: 0,
            column_type: 0,
            char_shape_count: 0,
            range_tag_count: 0,
            line_align_count: 0,
            instance_id: 0,
            char_shapes: None,
            line_segments: None,
            list_header: None,
            ctrl_header: Some(ctrl_header),
            table_data: None,
            picture_data: None,
            text_box_data: Some(text_box),
            hyperlinks: Vec::new(),
        };

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }

    /// Add floating text box
    pub fn add_floating_text_box(
        &mut self,
        text: &str,
        x: u32,
        y: u32,
        width: u32,
        height: u32,
        style: FloatingTextBoxStyle,
    ) -> Result<()> {
        use crate::model::ctrl_header::CtrlHeader;
        use crate::model::paragraph::ParaText;
        use crate::model::text_box::{TextBox, TextBoxAlignment};

        let text_box = TextBox::new(text)
            .with_position_mm(x as i32, y as i32)
            .with_size_mm(width, height)
            .with_alignment(TextBoxAlignment::Absolute)
            .with_transparent_background()
            .with_opacity(style.opacity)
            .with_rotation(style.rotation);

        let ctrl_header = CtrlHeader {
            ctrl_id: 0x7874, // TextBox control ID
            properties: 0,
            instance_id: 0,
        };

        let paragraph = Paragraph {
            text: Some(ParaText {
                content: String::new(),
            }),
            control_mask: 0x02,
            para_shape_id: 0,
            style_id: 0,
            column_type: 0,
            char_shape_count: 0,
            range_tag_count: 0,
            line_align_count: 0,
            instance_id: 0,
            char_shapes: None,
            line_segments: None,
            list_header: None,
            ctrl_header: Some(ctrl_header),
            table_data: None,
            picture_data: None,
            text_box_data: Some(text_box),
            hyperlinks: Vec::new(),
        };

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                section.paragraphs.push(paragraph);
            }
        }

        Ok(())
    }
}

// Header/Footer Methods
impl HwpWriter {
    /// Add header with options
    pub fn add_header_with_options(
        &mut self,
        text: &str,
        page_type: crate::model::header_footer::PageApplyType,
        alignment: crate::model::header_footer::HeaderFooterAlignment,
    ) {
        use crate::model::header_footer::HeaderFooter;

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                if section.page_def.is_none() {
                    section.page_def = Some(crate::model::page_def::PageDef::new_default());
                }
                if let Some(page_def) = section.page_def.as_mut() {
                    let header = HeaderFooter::new_header(text)
                        .with_apply_type(page_type)
                        .with_alignment(alignment);
                    page_def.header_footer.add_header(header);
                }
            }
        }
    }

    /// Add header with page number
    pub fn add_header_with_page_number(
        &mut self,
        text: &str,
        format: crate::model::header_footer::PageNumberFormat,
    ) {
        use crate::model::header_footer::{HeaderFooter, HeaderFooterAlignment};

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                if section.page_def.is_none() {
                    section.page_def = Some(crate::model::page_def::PageDef::new_default());
                }
                if let Some(page_def) = section.page_def.as_mut() {
                    let header = HeaderFooter::new_header(text)
                        .with_page_number(format)
                        .with_alignment(HeaderFooterAlignment::Center); // Default center alignment for page numbers
                    page_def.header_footer.add_header(header);
                }
            }
        }
    }

    /// Add footer
    pub fn add_footer(&mut self, text: &str) {
        use crate::model::header_footer::HeaderFooter;

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                if section.page_def.is_none() {
                    section.page_def = Some(crate::model::page_def::PageDef::new_default());
                }
                if let Some(page_def) = section.page_def.as_mut() {
                    let footer = HeaderFooter::new_footer(text);
                    page_def.header_footer.add_footer(footer);
                }
            }
        }
    }

    /// Add footer with options
    pub fn add_footer_with_options(
        &mut self,
        text: &str,
        page_type: crate::model::header_footer::PageApplyType,
        alignment: crate::model::header_footer::HeaderFooterAlignment,
    ) {
        use crate::model::header_footer::HeaderFooter;

        if let Some(body_text) = self.document.body_texts.get_mut(self.current_section_idx) {
            if let Some(section) = body_text.sections.get_mut(0) {
                if section.page_def.is_none() {
                    section.page_def = Some(crate::model::page_def::PageDef::new_default());
                }
                if let Some(page_def) = section.page_def.as_mut() {
                    let footer = HeaderFooter::new_footer(text)
                        .with_apply_type(page_type)
                        .with_alignment(alignment);
                    page_def.header_footer.add_footer(footer);
                }
            }
        }
    }
}

// Document Metadata and Statistics Methods
impl HwpWriter {
    /// Set document title
    pub fn set_document_title(&mut self, title: &str) -> &mut Self {
        if let Some(props) = self.document.doc_info.properties.as_mut() {
            props.document_title = Some(title.to_string());
        }
        self
    }

    /// Set document author
    pub fn set_document_author(&mut self, author: &str) -> &mut Self {
        if let Some(props) = self.document.doc_info.properties.as_mut() {
            props.document_author = Some(author.to_string());
        }
        self
    }

    /// Set document subject
    pub fn set_document_subject(&mut self, subject: &str) -> &mut Self {
        if let Some(props) = self.document.doc_info.properties.as_mut() {
            props.document_subject = Some(subject.to_string());
        }
        self
    }

    /// Set document keywords
    pub fn set_document_keywords(&mut self, keywords: &str) -> &mut Self {
        if let Some(props) = self.document.doc_info.properties.as_mut() {
            props.document_keywords = Some(keywords.to_string());
        }
        self
    }

    /// Set document company
    pub fn set_document_company(&mut self, company: &str) -> &mut Self {
        if let Some(props) = self.document.doc_info.properties.as_mut() {
            props.document_company = Some(company.to_string());
        }
        self
    }

    /// Update document statistics (character count, word count, etc.)
    pub fn update_document_statistics(&mut self) {
        // Extract text first (immutable borrow)
        let text = self.document.extract_text();

        // Count sections
        let section_count = self
            .document
            .body_texts
            .iter()
            .map(|bt| bt.sections.len())
            .sum::<usize>() as u16;

        // Count total characters (excluding spaces)
        let chars: Vec<char> = text.chars().collect();
        let total_character_count = chars.iter().filter(|c| !c.is_whitespace()).count() as u32;
        let space_character_count = chars.iter().filter(|c| c.is_whitespace()).count() as u32;

        // Count different character types
        let hangul_character_count = chars.iter().filter(|c| is_hangul(**c)).count() as u32;
        let english_character_count =
            chars.iter().filter(|c| c.is_ascii_alphabetic()).count() as u32;
        let hanja_character_count = chars.iter().filter(|c| is_hanja(**c)).count() as u32;
        let japanese_character_count = chars.iter().filter(|c| is_japanese(**c)).count() as u32;

        // Count symbols and other
        let symbol_character_count =
            chars.iter().filter(|c| c.is_ascii_punctuation()).count() as u32;
        let other_character_count = (total_character_count as usize
            - hangul_character_count as usize
            - english_character_count as usize
            - hanja_character_count as usize
            - japanese_character_count as usize
            - symbol_character_count as usize) as u32;

        // Count words (split by whitespace)
        let total_word_count = text.split_whitespace().count() as u32;

        // Rough line count estimation (average 10 words per line)
        let line_count = if total_word_count > 0 {
            (total_word_count as f32 / 10.0).ceil() as u32
        } else {
            0
        };

        // Page count estimation (average 500 words per page)
        let total_page_count = if total_word_count > 0 {
            (total_word_count as f32 / 500.0).ceil() as u32
        } else {
            1
        };

        // Now update properties (mutable borrow)
        if let Some(props) = self.document.doc_info.properties.as_mut() {
            props.section_count = section_count;
            props.total_character_count = total_character_count;
            props.space_character_count = space_character_count;
            props.hangul_character_count = hangul_character_count;
            props.english_character_count = english_character_count;
            props.hanja_character_count = hanja_character_count;
            props.japanese_character_count = japanese_character_count;
            props.symbol_character_count = symbol_character_count;
            props.other_character_count = other_character_count;
            props.total_word_count = total_word_count;
            props.line_count = line_count;
            props.total_page_count = total_page_count;
        }
    }

    /// Get document statistics
    pub fn get_document_statistics(&self) -> Option<&crate::model::DocumentProperties> {
        self.document.doc_info.properties.as_ref()
    }

    /// Get mutable document statistics
    pub fn get_document_statistics_mut(&mut self) -> Option<&mut crate::model::DocumentProperties> {
        self.document.doc_info.properties.as_mut()
    }
}

// Helper functions for character type detection
fn is_hangul(c: char) -> bool {
    matches!(c, '\u{AC00}'..='\u{D7AF}' | '\u{1100}'..='\u{11FF}' | '\u{3130}'..='\u{318F}' | '\u{A960}'..='\u{A97F}')
}

fn is_hanja(c: char) -> bool {
    matches!(c, '\u{4E00}'..='\u{9FFF}' | '\u{3400}'..='\u{4DBF}')
}

fn is_japanese(c: char) -> bool {
    matches!(c, '\u{3040}'..='\u{309F}' | '\u{30A0}'..='\u{30FF}')
}

// ============================================================================
// TODO: Unimplemented Features
// ============================================================================
//
// The following features are part of the HWP format specification but are not
// yet implemented in this library. Example usage can be found in:
// examples/shape_document.rs.disabled
//
// ## Shape Drawing (도형 그리기)
// - [ ] add_rectangle(x, y, width, height) - Draw rectangles
// - [ ] add_circle(x, y, radius) - Draw circles
// - [ ] add_ellipse(x, y, width, height) - Draw ellipses
// - [ ] add_line(x1, y1, x2, y2) - Draw lines
// - [ ] add_dashed_line(x1, y1, x2, y2) - Draw dashed lines
// - [ ] add_arrow(x1, y1, x2, y2) - Draw arrows
// - [ ] add_polygon(points) - Draw custom polygons
// - [ ] add_custom_shape(shape_type, ...) - Draw custom shapes with styling
// - [ ] add_shape_with_text(shape, text, alignment) - Shapes with text content
// - [ ] add_shape_group(shapes) - Group multiple shapes together
//
// ## Other Potential Features
// - [ ] Advanced table features (cell spanning, nested tables, etc.)
// - [ ] Chart/graph insertion
// - [ ] Mathematical equations (MathML)
// - [ ] Video/audio embedding
// - [ ] Forms and input fields
// - [ ] Comments and annotations
// - [ ] Track changes/revision history
// - [ ] Mail merge fields
//
// If you need any of these features, please open an issue at:
// https://github.com/yourusername/hwpers/issues
// ============================================================================