office-rs 0.1.1

A Rust library for reading and writing XML Office files
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
//! 通用数据类型定义
//!
//! 本模块定义了Office文档处理中常用的基础数据类型,包括:
//! - 颜色类型(RGB、主题色、索引色)
//! - 字体类型(字体名称、大小、样式)
//! - 尺寸类型(长度单位、边距、间距)
//! - 对齐和样式类型
//! - Office文档通用结构定义

// 注意:std::fmt 和 std::str::FromStr 在当前实现中未使用,但保留以备将来扩展

/// RGB颜色表示
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RgbColor {
    pub red: u8,
    pub green: u8,
    pub blue: u8,
}

/// RGBA颜色表示(包含透明度)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RgbaColor {
    pub red: u8,
    pub green: u8,
    pub blue: u8,
    pub alpha: u8, // 0 = 完全透明, 255 = 完全不透明
}

impl RgbColor {
    /// 创建新的RGB颜色
    pub fn new(red: u8, green: u8, blue: u8) -> Self {
        Self { red, green, blue }
    }

    /// 从十六进制字符串创建颜色(如 "FF0000" 表示红色)
    pub fn from_hex(hex: &str) -> Result<Self, String> {
        let hex = hex.trim_start_matches('#');
        if hex.len() != 6 {
            return Err("Hex color must be 6 characters long".to_string());
        }

        let red = u8
            ::from_str_radix(&hex[0..2], 16)
            .map_err(|_| "Invalid red component".to_string())?;
        let green = u8
            ::from_str_radix(&hex[2..4], 16)
            .map_err(|_| "Invalid green component".to_string())?;
        let blue = u8
            ::from_str_radix(&hex[4..6], 16)
            .map_err(|_| "Invalid blue component".to_string())?;

        Ok(Self::new(red, green, blue))
    }

    /// 转换为十六进制字符串
    pub fn to_hex(&self) -> String {
        format!("{:02X}{:02X}{:02X}", self.red, self.green, self.blue)
    }

    /// 转换为RGBA颜色(不透明)
    pub fn to_rgba(&self) -> RgbaColor {
        RgbaColor::new(self.red, self.green, self.blue, 255)
    }

    /// 常用颜色常量
    pub const BLACK: Self = Self {
        red: 0,
        green: 0,
        blue: 0,
    };
    pub const WHITE: Self = Self {
        red: 255,
        green: 255,
        blue: 255,
    };
    pub const RED: Self = Self {
        red: 255,
        green: 0,
        blue: 0,
    };
    pub const GREEN: Self = Self {
        red: 0,
        green: 255,
        blue: 0,
    };
    pub const BLUE: Self = Self {
        red: 0,
        green: 0,
        blue: 255,
    };
}

impl RgbaColor {
    /// 创建新的RGBA颜色
    pub fn new(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
        Self {
            red,
            green,
            blue,
            alpha,
        }
    }

    /// 从十六进制字符串创建颜色(如 "FF0000FF" 表示不透明红色)
    pub fn from_hex(hex: &str) -> Result<Self, String> {
        let hex = hex.trim_start_matches('#');
        if hex.len() != 8 {
            return Err("Hex RGBA color must be 8 characters long".to_string());
        }

        let red = u8
            ::from_str_radix(&hex[0..2], 16)
            .map_err(|_| "Invalid red component".to_string())?;
        let green = u8
            ::from_str_radix(&hex[2..4], 16)
            .map_err(|_| "Invalid green component".to_string())?;
        let blue = u8
            ::from_str_radix(&hex[4..6], 16)
            .map_err(|_| "Invalid blue component".to_string())?;
        let alpha = u8
            ::from_str_radix(&hex[6..8], 16)
            .map_err(|_| "Invalid alpha component".to_string())?;

        Ok(Self::new(red, green, blue, alpha))
    }

    /// 转换为十六进制字符串
    pub fn to_hex(&self) -> String {
        format!("{:02X}{:02X}{:02X}{:02X}", self.red, self.green, self.blue, self.alpha)
    }

    /// 获取透明度百分比(0.0-1.0)
    pub fn alpha_percent(&self) -> f32 {
        (self.alpha as f32) / 255.0
    }

    /// 从透明度百分比设置alpha值
    pub fn with_alpha_percent(mut self, alpha: f32) -> Self {
        self.alpha = (alpha.clamp(0.0, 1.0) * 255.0) as u8;
        self
    }

    /// 转换为RGB颜色(丢弃透明度)
    pub fn to_rgb(&self) -> RgbColor {
        RgbColor::new(self.red, self.green, self.blue)
    }

    /// 常用RGBA颜色常量
    pub const TRANSPARENT: Self = Self {
        red: 0,
        green: 0,
        blue: 0,
        alpha: 0,
    };
    pub const BLACK: Self = Self {
        red: 0,
        green: 0,
        blue: 0,
        alpha: 255,
    };
    pub const WHITE: Self = Self {
        red: 255,
        green: 255,
        blue: 255,
        alpha: 255,
    };
    pub const RED: Self = Self {
        red: 255,
        green: 0,
        blue: 0,
        alpha: 255,
    };
    pub const GREEN: Self = Self {
        red: 0,
        green: 255,
        blue: 0,
        alpha: 255,
    };
    pub const BLUE: Self = Self {
        red: 0,
        green: 0,
        blue: 255,
        alpha: 255,
    };
}

/// 主题颜色类型
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ThemeColor {
    /// 深色1(通常是黑色)
    Dark1,
    /// 浅色1(通常是白色)
    Light1,
    /// 深色2
    Dark2,
    /// 浅色2
    Light2,
    /// 强调色1-6
    Accent1,
    Accent2,
    Accent3,
    Accent4,
    Accent5,
    Accent6,
    /// 超链接颜色
    Hyperlink,
    /// 已访问超链接颜色
    FollowedHyperlink,
}

/// 颜色类型枚举
#[derive(Debug, Clone, PartialEq)]
pub enum Color {
    /// RGB颜色
    Rgb(RgbColor),
    /// RGBA颜色(包含透明度)
    Rgba(RgbaColor),
    /// 主题颜色
    Theme(ThemeColor),
    /// 索引颜色(用于兼容旧格式)
    Indexed(u8),
    /// 自动颜色
    Auto,
}

/// 字体大小(以磅为单位)
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct FontSize(pub f32);

impl FontSize {
    /// 创建字体大小
    pub fn new(size: f32) -> Self {
        Self(size.max(0.0))
    }

    /// 获取大小值
    pub fn value(&self) -> f32 {
        self.0
    }

    /// 常用字体大小
    pub const SMALL: Self = Self(8.0);
    pub const NORMAL: Self = Self(11.0);
    pub const MEDIUM: Self = Self(12.0);
    pub const LARGE: Self = Self(14.0);
    pub const EXTRA_LARGE: Self = Self(18.0);
}

/// 下划线样式
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UnderlineStyle {
    None,
    Single,
    Double,
    Thick,
    Dotted,
    DottedHeavy,
    Dashed,
    DashedHeavy,
    DashLong,
    DashLongHeavy,
    DotDash,
    DotDashHeavy,
    DotDotDash,
    DotDotDashHeavy,
    Wave,
    WaveHeavy,
    WaveDouble,
}

/// 字体样式
#[derive(Debug, Clone, PartialEq)]
pub struct FontStyle {
    pub bold: bool,
    pub italic: bool,
    pub underline: UnderlineStyle,
    pub strikethrough: bool,
    pub superscript: bool,
    pub subscript: bool,
    pub small_caps: bool,
    pub all_caps: bool,
}

impl Default for FontStyle {
    fn default() -> Self {
        Self {
            bold: false,
            italic: false,
            underline: UnderlineStyle::None,
            strikethrough: false,
            superscript: false,
            subscript: false,
            small_caps: false,
            all_caps: false,
        }
    }
}

impl FontStyle {
    /// 创建粗体样式
    pub fn bold() -> Self {
        Self {
            bold: true,
            italic: false,
            underline: UnderlineStyle::None,
            strikethrough: false,
            superscript: false,
            subscript: false,
            small_caps: false,
            all_caps: false,
        }
    }

    /// 创建斜体样式
    pub fn italic() -> Self {
        Self {
            bold: false,
            italic: true,
            underline: UnderlineStyle::None,
            strikethrough: false,
            superscript: false,
            subscript: false,
            small_caps: false,
            all_caps: false,
        }
    }
}

/// 字体定义
#[derive(Debug, Clone, PartialEq)]
pub struct Font {
    pub name: String,
    pub size: FontSize,
    pub style: FontStyle,
    pub color: Color,
}

impl Default for Font {
    fn default() -> Self {
        Self {
            name: "Calibri".to_string(),
            size: FontSize::NORMAL,
            style: FontStyle::default(),
            color: Color::Auto,
        }
    }
}

/// 长度单位
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LengthUnit {
    /// 磅(1/72英寸)
    Points(f32),
    /// 像素
    Pixels(f32),
    /// 英寸
    Inches(f32),
    /// 厘米
    Centimeters(f32),
    /// 毫米
    Millimeters(f32),
    /// EMU(English Metric Unit,1/914400英寸)
    Emu(i32),
}

impl LengthUnit {
    /// 转换为磅
    pub fn to_points(&self) -> f32 {
        match self {
            LengthUnit::Points(p) => *p,
            LengthUnit::Pixels(px) => *px * 0.75, // 假设96 DPI
            LengthUnit::Inches(inch) => *inch * 72.0,
            LengthUnit::Centimeters(cm) => *cm * 28.35,
            LengthUnit::Millimeters(mm) => *mm * 2.835,
            LengthUnit::Emu(emu) => (*emu as f32) / 12700.0,
        }
    }

    /// 转换为EMU
    pub fn to_emu(&self) -> i32 {
        (self.to_points() * 12700.0) as i32
    }
}

/// 边距定义
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Margin {
    pub top: LengthUnit,
    pub right: LengthUnit,
    pub bottom: LengthUnit,
    pub left: LengthUnit,
}

impl Margin {
    /// 创建统一边距
    pub fn uniform(margin: LengthUnit) -> Self {
        Self {
            top: margin,
            right: margin,
            bottom: margin,
            left: margin,
        }
    }

    /// 创建零边距
    pub fn zero() -> Self {
        Self::uniform(LengthUnit::Points(0.0))
    }
}

/// 水平对齐方式
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HorizontalAlignment {
    Left,
    Center,
    Right,
    Justify,
    Distributed,
}

/// 垂直对齐方式
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VerticalAlignment {
    Top,
    Middle,
    Bottom,
    Justify,
    Distributed,
}

/// 边框样式
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BorderStyle {
    None,
    Thin,
    Medium,
    Thick,
    Double,
    Dotted,
    Dashed,
    DashDot,
    DashDotDot,
}

/// 边框定义
#[derive(Debug, Clone, PartialEq)]
pub struct Border {
    pub style: BorderStyle,
    pub color: Color,
    pub width: LengthUnit,
}

impl Default for Border {
    fn default() -> Self {
        Self {
            style: BorderStyle::None,
            color: Color::Auto,
            width: LengthUnit::Points(0.0),
        }
    }
}

/// 边框集合
#[derive(Debug, Clone, PartialEq)]
pub struct Borders {
    pub top: Border,
    pub right: Border,
    pub bottom: Border,
    pub left: Border,
}

impl Default for Borders {
    fn default() -> Self {
        Self {
            top: Border::default(),
            right: Border::default(),
            bottom: Border::default(),
            left: Border::default(),
        }
    }
}

/// 填充模式
#[derive(Debug, Clone, PartialEq)]
pub enum FillPattern {
    /// 无填充
    None,
    /// 纯色填充
    Solid(Color),
    /// 渐变填充
    Gradient {
        start_color: Color,
        end_color: Color,
        angle: f32, // 角度(度)
    },
    /// 图案填充
    Pattern {
        pattern_type: PatternType,
        foreground_color: Color,
        background_color: Color,
    },
}

/// 图案类型
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PatternType {
    Solid,
    Gray75,
    Gray50,
    Gray25,
    Gray125,
    Gray0625,
    HorizontalStripe,
    VerticalStripe,
    ReverseDiagonalStripe,
    DiagonalStripe,
    DiagonalCrosshatch,
    ThickDiagonalCrosshatch,
}

/// 页面方向
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PageOrientation {
    Portrait,
    Landscape,
}

/// 页面大小
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PageSize {
    pub width: LengthUnit,
    pub height: LengthUnit,
    pub orientation: PageOrientation,
}

impl PageSize {
    /// A4纸张大小
    pub fn a4() -> Self {
        Self {
            width: LengthUnit::Millimeters(210.0),
            height: LengthUnit::Millimeters(297.0),
            orientation: PageOrientation::Portrait,
        }
    }

    /// Letter纸张大小
    pub fn letter() -> Self {
        Self {
            width: LengthUnit::Inches(8.5),
            height: LengthUnit::Inches(11.0),
            orientation: PageOrientation::Portrait,
        }
    }
}

/// 文档属性
#[derive(Debug, Clone, PartialEq)]
pub struct DocumentProperties {
    pub title: Option<String>,
    pub author: Option<String>,
    pub subject: Option<String>,
    pub keywords: Option<String>,
    pub description: Option<String>,
    pub category: Option<String>,
    pub created: Option<chrono::DateTime<chrono::Utc>>,
    pub modified: Option<chrono::DateTime<chrono::Utc>>,
}

impl Default for DocumentProperties {
    fn default() -> Self {
        Self {
            title: None,
            author: None,
            subject: None,
            keywords: None,
            description: None,
            category: None,
            created: None,
            modified: None,
        }
    }
}

/// 坐标点
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point {
    pub x: LengthUnit,
    pub y: LengthUnit,
}

/// 尺寸
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Size {
    pub width: LengthUnit,
    pub height: LengthUnit,
}

/// 矩形区域
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rectangle {
    pub position: Point,
    pub size: Size,
}

/// 列表类型
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ListType {
    /// 无序列表
    Bullet,
    /// 有序列表(数字)
    Number,
    /// 字母列表(小写)
    LowerAlpha,
    /// 字母列表(大写)
    UpperAlpha,
    /// 罗马数字(小写)
    LowerRoman,
    /// 罗马数字(大写)
    UpperRoman,
}

/// 列表样式
#[derive(Debug, Clone, PartialEq)]
pub struct ListStyle {
    pub list_type: ListType,
    pub level: u8,
    pub indent: LengthUnit,
    pub bullet_char: Option<char>,
    pub number_format: Option<String>,
}

impl Default for ListStyle {
    fn default() -> Self {
        Self {
            list_type: ListType::Bullet,
            level: 0,
            indent: LengthUnit::Points(18.0),
            bullet_char: Some(''),
            number_format: None,
        }
    }
}

/// 表格边框样式
#[derive(Debug, Clone, PartialEq)]
pub struct TableBorderStyle {
    pub outer: Borders,
    pub inner_horizontal: Border,
    pub inner_vertical: Border,
}

impl Default for TableBorderStyle {
    fn default() -> Self {
        Self {
            outer: Borders::default(),
            inner_horizontal: Border::default(),
            inner_vertical: Border::default(),
        }
    }
}

/// 表格样式
#[derive(Debug, Clone, PartialEq)]
pub struct TableStyle {
    pub borders: TableBorderStyle,
    pub cell_padding: Margin,
    pub cell_spacing: LengthUnit,
    pub background_color: Option<Color>,
    pub stripe_rows: bool,
    pub stripe_columns: bool,
}

impl Default for TableStyle {
    fn default() -> Self {
        Self {
            borders: TableBorderStyle::default(),
            cell_padding: Margin::uniform(LengthUnit::Points(2.0)),
            cell_spacing: LengthUnit::Points(0.0),
            background_color: None,
            stripe_rows: false,
            stripe_columns: false,
        }
    }
}

/// 文本装饰
#[derive(Debug, Clone, PartialEq)]
pub struct TextDecoration {
    pub shadow: bool,
    pub emboss: bool,
    pub imprint: bool,
    pub outline: bool,
    pub glow: Option<Color>,
    pub reflection: bool,
}

impl Default for TextDecoration {
    fn default() -> Self {
        Self {
            shadow: false,
            emboss: false,
            imprint: false,
            outline: false,
            glow: None,
            reflection: false,
        }
    }
}

/// 段落样式
#[derive(Debug, Clone, PartialEq)]
pub struct ParagraphStyle {
    pub alignment: HorizontalAlignment,
    pub line_spacing: LineSpacing,
    pub space_before: LengthUnit,
    pub space_after: LengthUnit,
    pub first_line_indent: LengthUnit,
    pub left_indent: LengthUnit,
    pub right_indent: LengthUnit,
    pub keep_together: bool,
    pub keep_with_next: bool,
    pub page_break_before: bool,
}

impl Default for ParagraphStyle {
    fn default() -> Self {
        Self {
            alignment: HorizontalAlignment::Left,
            line_spacing: LineSpacing::Single,
            space_before: LengthUnit::Points(0.0),
            space_after: LengthUnit::Points(0.0),
            first_line_indent: LengthUnit::Points(0.0),
            left_indent: LengthUnit::Points(0.0),
            right_indent: LengthUnit::Points(0.0),
            keep_together: false,
            keep_with_next: false,
            page_break_before: false,
        }
    }
}

/// 行间距类型
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LineSpacing {
    /// 单倍行距
    Single,
    /// 1.5倍行距
    OneAndHalf,
    /// 双倍行距
    Double,
    /// 多倍行距
    Multiple(f32),
    /// 固定行距(磅)
    Exact(f32),
    /// 最小行距(磅)
    AtLeast(f32),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rgb_color() {
        let color = RgbColor::new(255, 128, 64);
        assert_eq!(color.to_hex(), "FF8040");

        let parsed = RgbColor::from_hex("FF8040").unwrap();
        assert_eq!(parsed, color);

        let parsed_with_hash = RgbColor::from_hex("#FF8040").unwrap();
        assert_eq!(parsed_with_hash, color);
    }

    #[test]
    fn test_font_size() {
        let size = FontSize::new(12.0);
        assert_eq!(size.value(), 12.0);

        // 负数应该被转换为0
        let negative_size = FontSize::new(-5.0);
        assert_eq!(negative_size.value(), 0.0);
    }

    #[test]
    fn test_length_unit_conversion() {
        let points = LengthUnit::Points(72.0);
        assert_eq!(points.to_points(), 72.0);

        let inches = LengthUnit::Inches(1.0);
        assert_eq!(inches.to_points(), 72.0);

        let cm = LengthUnit::Centimeters(2.54);
        assert!((cm.to_points() - 72.009).abs() < 0.1);
    }

    #[test]
    fn test_margin() {
        let uniform = Margin::uniform(LengthUnit::Points(10.0));
        assert_eq!(uniform.top.to_points(), 10.0);
        assert_eq!(uniform.right.to_points(), 10.0);
        assert_eq!(uniform.bottom.to_points(), 10.0);
        assert_eq!(uniform.left.to_points(), 10.0);

        let zero = Margin::zero();
        assert_eq!(zero.top.to_points(), 0.0);
    }

    #[test]
    fn test_page_size() {
        let a4 = PageSize::a4();
        assert_eq!(a4.orientation, PageOrientation::Portrait);
        // 210mm = 210 * 2.835 = 595.35 points (允许小的浮点误差)
        assert!((a4.width.to_points() - 595.35).abs() < 0.1);

        let letter = PageSize::letter();
        assert_eq!(letter.width.to_points(), 612.0); // 8.5 inches in points
    }

    #[test]
    fn test_font_style() {
        let default_style = FontStyle::default();
        assert!(!default_style.bold);
        assert!(!default_style.italic);
        assert_eq!(default_style.underline, UnderlineStyle::None);
        assert!(!default_style.strikethrough);
        assert!(!default_style.superscript);
        assert!(!default_style.subscript);

        let bold_style = FontStyle::bold();
        assert!(bold_style.bold);
        assert!(!bold_style.italic);

        let italic_style = FontStyle::italic();
        assert!(!italic_style.bold);
        assert!(italic_style.italic);
    }

    #[test]
    fn test_rgba_color() {
        let rgba = RgbaColor::new(255, 0, 0, 128);
        assert_eq!(rgba.red, 255);
        assert_eq!(rgba.green, 0);
        assert_eq!(rgba.blue, 0);
        assert_eq!(rgba.alpha, 128);

        // 测试透明度百分比
        assert_eq!(rgba.alpha_percent(), 0.5019608); // 128/255

        // 测试十六进制转换
        assert_eq!(rgba.to_hex(), "FF000080");

        // 测试从十六进制创建
        let rgba_from_hex = RgbaColor::from_hex("FF000080").unwrap();
        assert_eq!(rgba_from_hex, rgba);

        // 测试RGB转换
        let rgb = rgba.to_rgb();
        assert_eq!(rgb.red, 255);
        assert_eq!(rgb.green, 0);
        assert_eq!(rgb.blue, 0);

        // 测试透明度百分比设置
        let semi_transparent = RgbaColor::RED.with_alpha_percent(0.5);
        assert_eq!(semi_transparent.alpha, 127); // 0.5 * 255 = 127.5 -> 127
    }

    #[test]
    fn test_color_enum() {
        let rgb_color = Color::Rgb(RgbColor::RED);
        let rgba_color = Color::Rgba(RgbaColor::RED);
        let theme_color = Color::Theme(ThemeColor::Accent1);
        let auto_color = Color::Auto;

        // 确保不同类型的颜色不相等
        assert_ne!(rgb_color, rgba_color);
        assert_ne!(rgb_color, theme_color);
        assert_ne!(rgb_color, auto_color);
    }

    #[test]
    fn test_underline_style() {
        let mut style = FontStyle::default();
        assert_eq!(style.underline, UnderlineStyle::None);

        style.underline = UnderlineStyle::Single;
        assert_eq!(style.underline, UnderlineStyle::Single);

        style.underline = UnderlineStyle::Double;
        assert_eq!(style.underline, UnderlineStyle::Double);
    }

    #[test]
    fn test_list_style() {
        let default_list = ListStyle::default();
        assert_eq!(default_list.list_type, ListType::Bullet);
        assert_eq!(default_list.level, 0);
        assert_eq!(default_list.bullet_char, Some(''));

        let numbered_list = ListStyle {
            list_type: ListType::Number,
            level: 1,
            indent: LengthUnit::Points(36.0),
            bullet_char: None,
            number_format: Some("1.".to_string()),
        };
        assert_eq!(numbered_list.list_type, ListType::Number);
        assert_eq!(numbered_list.level, 1);
    }

    #[test]
    fn test_line_spacing() {
        let single = LineSpacing::Single;
        let double = LineSpacing::Double;
        let multiple = LineSpacing::Multiple(1.5);
        let exact = LineSpacing::Exact(12.0);

        assert_ne!(single, double);

        if let LineSpacing::Multiple(factor) = multiple {
            assert_eq!(factor, 1.5);
        } else {
            panic!("Expected Multiple variant");
        }

        if let LineSpacing::Exact(points) = exact {
            assert_eq!(points, 12.0);
        } else {
            panic!("Expected Exact variant");
        }
    }
}