oxidize-pdf 2.5.0

A pure Rust PDF generation and manipulation library with zero external dependencies
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
//! Advanced table builder for creating complex tables with professional styling

use super::cell_style::CellStyle;
use super::error::TableError;
use super::header_builder::HeaderBuilder;
use crate::graphics::Color;
use std::collections::HashMap;

/// Column definition for advanced tables
#[derive(Debug, Clone)]
pub struct Column {
    /// Column header text
    pub header: String,
    /// Column width in points
    pub width: f64,
    /// Default style for cells in this column
    pub default_style: Option<CellStyle>,
    /// Whether this column can resize automatically
    pub auto_resize: bool,
    /// Minimum width for auto-resizing columns
    pub min_width: Option<f64>,
    /// Maximum width for auto-resizing columns
    pub max_width: Option<f64>,
}

impl Column {
    /// Create a new column
    pub fn new<S: Into<String>>(header: S, width: f64) -> Self {
        Self {
            header: header.into(),
            width,
            default_style: None,
            auto_resize: false,
            min_width: None,
            max_width: None,
        }
    }

    /// Set default style for this column
    pub fn with_style(mut self, style: CellStyle) -> Self {
        self.default_style = Some(style);
        self
    }

    /// Enable auto-resizing for this column
    pub fn auto_resize(mut self, min_width: Option<f64>, max_width: Option<f64>) -> Self {
        self.auto_resize = true;
        self.min_width = min_width;
        self.max_width = max_width;
        self
    }
}

/// Cell data with optional styling and spanning
#[derive(Debug, Clone)]
pub struct CellData {
    /// Cell content
    pub content: String,
    /// Optional custom style for this cell
    pub style: Option<CellStyle>,
    /// Number of columns this cell spans (1 = no spanning)
    pub colspan: usize,
    /// Number of rows this cell spans (1 = no spanning)
    pub rowspan: usize,
}

impl CellData {
    /// Create a new cell with text content
    pub fn new<S: Into<String>>(content: S) -> Self {
        Self {
            content: content.into(),
            style: None,
            colspan: 1,
            rowspan: 1,
        }
    }

    /// Set custom style for this cell
    pub fn with_style(mut self, style: CellStyle) -> Self {
        self.style = Some(style);
        self
    }

    /// Set column span for this cell
    pub fn colspan(mut self, span: usize) -> Self {
        self.colspan = span.max(1);
        self
    }

    /// Set row span for this cell
    pub fn rowspan(mut self, span: usize) -> Self {
        self.rowspan = span.max(1);
        self
    }
}

/// Row data with optional styling
#[derive(Debug, Clone)]
pub struct RowData {
    /// Cells in this row
    pub cells: Vec<CellData>,
    /// Optional style for the entire row
    pub style: Option<CellStyle>,
    /// Minimum height for this row
    pub min_height: Option<f64>,
}

impl RowData {
    /// Create a new row from string content
    pub fn from_strings(content: Vec<&str>) -> Self {
        let cells = content.into_iter().map(CellData::new).collect();

        Self {
            cells,
            style: None,
            min_height: None,
        }
    }

    /// Create a new row from cell data
    pub fn from_cells(cells: Vec<CellData>) -> Self {
        Self {
            cells,
            style: None,
            min_height: None,
        }
    }

    /// Set style for the entire row
    pub fn with_style(mut self, style: CellStyle) -> Self {
        self.style = Some(style);
        self
    }

    /// Set minimum height for this row
    pub fn min_height(mut self, height: f64) -> Self {
        self.min_height = Some(height);
        self
    }
}

/// Complete advanced table configuration
#[derive(Debug, Clone)]
pub struct AdvancedTable {
    /// Table title
    pub title: Option<String>,
    /// X position on page
    pub x: f64,
    /// Y position on page
    pub y: f64,
    /// Table columns definition
    pub columns: Vec<Column>,
    /// Table rows data
    pub rows: Vec<RowData>,
    /// Header configuration
    pub header: Option<HeaderBuilder>,
    /// Whether to show the table header
    pub show_header: bool,
    /// Default cell style
    pub default_style: CellStyle,
    /// Header style
    pub header_style: CellStyle,
    /// Zebra striping configuration
    pub zebra_striping: Option<ZebraConfig>,
    /// Table-wide border style
    pub table_border: bool,
    /// Spacing between cells
    pub cell_spacing: f64,
    /// Total table width (auto-calculated if None)
    pub total_width: Option<f64>,
    /// Whether to repeat headers on page breaks
    pub repeat_headers: bool,
    /// Styles for specific cells (row, col) -> style
    pub cell_styles: HashMap<(usize, usize), CellStyle>,
}

/// Zebra striping configuration
#[derive(Debug, Clone)]
pub struct ZebraConfig {
    /// Background color for odd rows
    pub odd_color: Option<Color>,
    /// Background color for even rows
    pub even_color: Option<Color>,
    /// Start with odd or even row
    pub start_with_odd: bool,
}

impl ZebraConfig {
    /// Create zebra striping with alternating colors
    pub fn new(odd_color: Option<Color>, even_color: Option<Color>) -> Self {
        Self {
            odd_color,
            even_color,
            start_with_odd: true,
        }
    }

    /// Simple zebra striping with one alternating color
    pub fn simple(color: Color) -> Self {
        Self::new(Some(color), None)
    }

    /// Get color for a specific row
    pub fn get_color_for_row(&self, row_index: usize) -> Option<Color> {
        let is_odd = (row_index % 2) == (if self.start_with_odd { 1 } else { 0 });
        if is_odd {
            self.odd_color
        } else {
            self.even_color
        }
    }
}

/// Builder for creating advanced tables with fluent API
pub struct AdvancedTableBuilder {
    table: AdvancedTable,
}

impl AdvancedTableBuilder {
    /// Create a new table builder
    pub fn new() -> Self {
        Self {
            table: AdvancedTable {
                title: None,
                x: 0.0,
                y: 0.0,
                columns: Vec::new(),
                rows: Vec::new(),
                header: None,
                show_header: true,
                default_style: CellStyle::data(),
                header_style: CellStyle::header(),
                zebra_striping: None,
                table_border: true,
                cell_spacing: 0.0,
                total_width: None,
                repeat_headers: false,
                cell_styles: HashMap::new(),
            },
        }
    }

    /// Add a column to the table
    pub fn add_column<S: Into<String>>(mut self, header: S, width: f64) -> Self {
        self.table.columns.push(Column::new(header, width));
        self
    }

    /// Add a column with custom styling
    pub fn add_styled_column<S: Into<String>>(
        mut self,
        header: S,
        width: f64,
        style: CellStyle,
    ) -> Self {
        self.table
            .columns
            .push(Column::new(header, width).with_style(style));
        self
    }

    /// Set columns from a list of headers with equal widths
    pub fn columns_equal_width(mut self, headers: Vec<&str>, total_width: f64) -> Self {
        let column_width = total_width / headers.len() as f64;
        self.table.columns = headers
            .into_iter()
            .map(|header| Column::new(header, column_width))
            .collect();
        self.table.total_width = Some(total_width);
        self
    }

    /// Add a simple row from string content
    pub fn add_row(mut self, content: Vec<&str>) -> Self {
        self.table.rows.push(RowData::from_strings(content));
        self
    }

    pub fn add_row_with_min_height(mut self, content: Vec<&str>, min_height: f64) -> Self {
        self.table
            .rows
            .push(RowData::from_strings(content).min_height(min_height));
        self
    }

    /// Add a row with cell data
    pub fn add_row_cells(mut self, cells: Vec<CellData>) -> Self {
        self.table.rows.push(RowData::from_cells(cells));
        self
    }

    /// Add a styled row
    pub fn add_styled_row(mut self, content: Vec<&str>, style: CellStyle) -> Self {
        self.table
            .rows
            .push(RowData::from_strings(content).with_style(style));
        self
    }

    /// Set default cell style
    pub fn default_style(mut self, style: CellStyle) -> Self {
        self.table.default_style = style;
        self
    }

    /// Set data cell style (alias for default_style)
    pub fn data_style(mut self, style: CellStyle) -> Self {
        self.table.default_style = style;
        self
    }

    /// Set header style
    pub fn header_style(mut self, style: CellStyle) -> Self {
        self.table.header_style = style;
        self
    }

    /// Control header visibility
    pub fn show_header(mut self, show: bool) -> Self {
        self.table.show_header = show;
        self
    }

    /// Set table title
    pub fn title<S: Into<String>>(mut self, title: S) -> Self {
        self.table.title = Some(title.into());
        self
    }

    /// Set table columns from (header, width) tuples
    pub fn columns(mut self, column_specs: Vec<(&str, f64)>) -> Self {
        self.table.columns = column_specs
            .into_iter()
            .map(|(header, width)| Column::new(header, width))
            .collect();
        self
    }

    /// Set table position on page
    pub fn position(mut self, x: f64, y: f64) -> Self {
        self.table.x = x;
        self.table.y = y;
        self
    }

    /// Add a complex header using HeaderBuilder
    pub fn complex_header(mut self, header: HeaderBuilder) -> Self {
        // Auto-generate columns from header if table has no columns
        if self.table.columns.is_empty() {
            let column_count = header.total_columns;
            for i in 0..column_count {
                self.table.columns.push(Column::new(
                    format!("Column {}", i + 1),
                    100.0, // Default width
                ));
            }
        }
        self.table.header = Some(header);
        self
    }

    /// Enable zebra stripes
    pub fn zebra_stripes(mut self, enabled: bool, color: Color) -> Self {
        if enabled {
            self.table.zebra_striping = Some(ZebraConfig::simple(color));
        } else {
            self.table.zebra_striping = None;
        }
        self
    }

    /// Add row with custom style
    pub fn add_row_with_style(mut self, content: Vec<&str>, style: CellStyle) -> Self {
        let mut row = RowData::from_strings(content);
        row = row.with_style(style);
        self.table.rows.push(row);
        self
    }

    /// Add row with mixed cell styles
    pub fn add_row_with_mixed_styles(mut self, cells: Vec<(CellStyle, &str)>) -> Self {
        let cell_data: Vec<CellData> = cells
            .into_iter()
            .map(|(style, content)| CellData::new(content.to_string()).with_style(style))
            .collect();
        self.table.rows.push(RowData::from_cells(cell_data));
        self
    }

    /// Build with error handling (for compatibility with tests)
    pub fn build(self) -> Result<AdvancedTable, TableError> {
        if self.table.columns.is_empty() {
            return Err(TableError::NoColumns);
        }
        Ok(self.table)
    }

    /// Enable zebra striping
    pub fn zebra_striping(mut self, color: Color) -> Self {
        self.table.zebra_striping = Some(ZebraConfig::simple(color));
        self
    }

    /// Enable custom zebra striping
    pub fn zebra_striping_custom(mut self, config: ZebraConfig) -> Self {
        self.table.zebra_striping = Some(config);
        self
    }

    /// Enable or disable table border
    pub fn table_border(mut self, enabled: bool) -> Self {
        self.table.table_border = enabled;
        self
    }

    /// Set cell spacing
    pub fn cell_spacing(mut self, spacing: f64) -> Self {
        self.table.cell_spacing = spacing;
        self
    }

    /// Set total table width
    pub fn total_width(mut self, width: f64) -> Self {
        self.table.total_width = Some(width);
        self
    }

    /// Enable header repetition on page breaks
    pub fn repeat_headers(mut self, repeat: bool) -> Self {
        self.table.repeat_headers = repeat;
        self
    }

    /// Set style for a specific cell
    pub fn set_cell_style(mut self, row: usize, col: usize, style: CellStyle) -> Self {
        self.table.cell_styles.insert((row, col), style);
        self
    }

    /// Add bulk data from a 2D vector
    pub fn add_data(mut self, data: Vec<Vec<&str>>) -> Self {
        for row in data {
            self = self.add_row(row);
        }
        self
    }

    /// Create a financial table with common styling
    pub fn financial_table(self) -> Self {
        self.header_style(
            CellStyle::header()
                .background_color(Color::rgb(0.2, 0.4, 0.8))
                .text_color(Color::white()),
        )
        .default_style(CellStyle::data())
        .zebra_striping(Color::rgb(0.97, 0.97, 0.97))
        .table_border(true)
    }

    /// Create a data table with minimal styling
    pub fn minimal_table(self) -> Self {
        self.header_style(
            CellStyle::new()
                .font_size(12.0)
                .background_color(Color::rgb(0.95, 0.95, 0.95)),
        )
        .default_style(CellStyle::data())
        .table_border(false)
        .cell_spacing(2.0)
    }
}

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

impl AdvancedTable {
    /// Get the total calculated width of the table
    pub fn calculate_width(&self) -> f64 {
        if let Some(width) = self.total_width {
            width
        } else {
            self.columns.iter().map(|col| col.width).sum()
        }
    }

    /// Get the number of rows (excluding headers)
    pub fn row_count(&self) -> usize {
        self.rows.len()
    }

    /// Get the number of columns
    pub fn column_count(&self) -> usize {
        self.columns.len()
    }

    /// Get style for a specific cell, considering row/column defaults and zebra striping
    pub fn get_cell_style(&self, row: usize, col: usize) -> CellStyle {
        // Priority: specific cell style > row style > column style > zebra striping > default

        // Check specific cell style
        if let Some(cell_style) = self.cell_styles.get(&(row, col)) {
            return cell_style.clone();
        }

        // Check row style
        if let Some(row_data) = self.rows.get(row) {
            if let Some(row_style) = &row_data.style {
                return row_style.clone();
            }
        }

        // Check column style
        if let Some(column) = self.columns.get(col) {
            if let Some(column_style) = &column.default_style {
                let mut style = column_style.clone();

                // Apply zebra striping if configured
                if let Some(zebra) = &self.zebra_striping {
                    if let Some(color) = zebra.get_color_for_row(row) {
                        style.background_color = Some(color);
                    }
                }

                return style;
            }
        }

        // Apply zebra striping to default style
        let mut style = self.default_style.clone();
        if let Some(zebra) = &self.zebra_striping {
            if let Some(color) = zebra.get_color_for_row(row) {
                style.background_color = Some(color);
            }
        }

        style
    }

    /// Get a reference to the style for a specific cell, considering row/column defaults.
    ///
    /// Returns a reference for the common cases (cell-specific, row, or column style).
    /// When zebra striping would modify the style, falls back to the base column or default style
    /// without applying the zebra background. Use [`get_cell_style`] when zebra colors are needed.
    pub fn get_cell_style_ref(&self, row: usize, col: usize) -> &CellStyle {
        // Priority: specific cell style > row style > column style > default

        if let Some(cell_style) = self.cell_styles.get(&(row, col)) {
            return cell_style;
        }

        if let Some(row_data) = self.rows.get(row) {
            if let Some(row_style) = &row_data.style {
                return row_style;
            }
        }

        if let Some(column) = self.columns.get(col) {
            if let Some(column_style) = &column.default_style {
                // Return the base column style without zebra mutation.
                // Callers needing zebra background should use `get_cell_style`.
                return column_style;
            }
        }

        &self.default_style
    }

    /// Validate table structure (e.g., consistent column counts with colspan/rowspan)
    pub fn validate(&self) -> Result<(), TableError> {
        let expected_cols = self.columns.len();
        // Track the last row each column is occupied through (exclusive upper bound)
        let mut rowspan_end: Vec<usize> = vec![0; expected_cols];

        for (row_idx, row) in self.rows.iter().enumerate() {
            // Count columns occupied by rowspan from previous rows
            let occupied_by_rowspan: usize =
                rowspan_end.iter().filter(|&&end| end > row_idx).count();

            // Sum of cell colspans + occupied columns must equal total columns
            let total_colspan: usize = row.cells.iter().map(|c| c.colspan).sum();
            if total_colspan + occupied_by_rowspan != expected_cols {
                return Err(TableError::ColumnMismatch {
                    row: row_idx,
                    found: total_colspan + occupied_by_rowspan,
                    expected: expected_cols,
                });
            }

            // Record new rowspans from this row's cells
            let mut actual_col = 0;
            for cell in &row.cells {
                // Skip columns occupied by rowspan
                while actual_col < expected_cols && rowspan_end[actual_col] > row_idx {
                    actual_col += 1;
                }
                if cell.rowspan > 1 {
                    for c in actual_col..(actual_col + cell.colspan).min(expected_cols) {
                        rowspan_end[c] = row_idx + cell.rowspan;
                    }
                }
                actual_col += cell.colspan;
            }
        }

        Ok(())
    }
}

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

    // =============================================================================
    // Column tests
    // =============================================================================

    #[test]
    fn test_column_new() {
        let col = Column::new("Header", 100.0);
        assert_eq!(col.header, "Header");
        assert_eq!(col.width, 100.0);
        assert!(col.default_style.is_none());
        assert!(!col.auto_resize);
        assert!(col.min_width.is_none());
        assert!(col.max_width.is_none());
    }

    #[test]
    fn test_column_with_style() {
        let style = CellStyle::data();
        let col = Column::new("Header", 100.0).with_style(style.clone());
        assert!(col.default_style.is_some());
        // font_size is Option<f64>
        assert_eq!(col.default_style.unwrap().font_size, style.font_size);
    }

    #[test]
    fn test_column_auto_resize() {
        let col = Column::new("Header", 100.0).auto_resize(Some(50.0), Some(200.0));
        assert!(col.auto_resize);
        assert_eq!(col.min_width, Some(50.0));
        assert_eq!(col.max_width, Some(200.0));
    }

    #[test]
    fn test_column_auto_resize_no_limits() {
        let col = Column::new("Header", 100.0).auto_resize(None, None);
        assert!(col.auto_resize);
        assert!(col.min_width.is_none());
        assert!(col.max_width.is_none());
    }

    // =============================================================================
    // CellData tests
    // =============================================================================

    #[test]
    fn test_cell_data_new() {
        let cell = CellData::new("Content");
        assert_eq!(cell.content, "Content");
        assert!(cell.style.is_none());
        assert_eq!(cell.colspan, 1);
        assert_eq!(cell.rowspan, 1);
    }

    #[test]
    fn test_cell_data_with_style() {
        let style = CellStyle::header();
        let cell = CellData::new("Content").with_style(style);
        assert!(cell.style.is_some());
    }

    #[test]
    fn test_cell_data_colspan() {
        let cell = CellData::new("Content").colspan(3);
        assert_eq!(cell.colspan, 3);
    }

    #[test]
    fn test_cell_data_colspan_min_is_one() {
        // colspan(0) should be clamped to 1
        let cell = CellData::new("Content").colspan(0);
        assert_eq!(cell.colspan, 1);
    }

    #[test]
    fn test_cell_data_rowspan() {
        let cell = CellData::new("Content").rowspan(2);
        assert_eq!(cell.rowspan, 2);
    }

    #[test]
    fn test_cell_data_rowspan_min_is_one() {
        // rowspan(0) should be clamped to 1
        let cell = CellData::new("Content").rowspan(0);
        assert_eq!(cell.rowspan, 1);
    }

    #[test]
    fn test_cell_data_combined_span() {
        let cell = CellData::new("Merged").colspan(2).rowspan(3);
        assert_eq!(cell.colspan, 2);
        assert_eq!(cell.rowspan, 3);
    }

    // =============================================================================
    // RowData tests
    // =============================================================================

    #[test]
    fn test_row_data_from_strings() {
        let row = RowData::from_strings(vec!["A", "B", "C"]);
        assert_eq!(row.cells.len(), 3);
        assert_eq!(row.cells[0].content, "A");
        assert_eq!(row.cells[1].content, "B");
        assert_eq!(row.cells[2].content, "C");
        assert!(row.style.is_none());
        assert!(row.min_height.is_none());
    }

    #[test]
    fn test_row_data_from_cells() {
        let cells = vec![CellData::new("Cell1"), CellData::new("Cell2").colspan(2)];
        let row = RowData::from_cells(cells);
        assert_eq!(row.cells.len(), 2);
        assert_eq!(row.cells[1].colspan, 2);
    }

    #[test]
    fn test_row_data_with_style() {
        let style = CellStyle::header();
        let row = RowData::from_strings(vec!["A"]).with_style(style);
        assert!(row.style.is_some());
    }

    #[test]
    fn test_row_data_min_height() {
        let row = RowData::from_strings(vec!["A"]).min_height(50.0);
        assert_eq!(row.min_height, Some(50.0));
    }

    // =============================================================================
    // ZebraConfig tests
    // =============================================================================

    #[test]
    fn test_zebra_config_new() {
        let odd = Color::rgb(0.9, 0.9, 0.9);
        let even = Color::rgb(1.0, 1.0, 1.0);
        let config = ZebraConfig::new(Some(odd), Some(even));
        assert!(config.odd_color.is_some());
        assert!(config.even_color.is_some());
        assert!(config.start_with_odd);
    }

    #[test]
    fn test_zebra_config_simple() {
        let color = Color::rgb(0.95, 0.95, 0.95);
        let config = ZebraConfig::simple(color);
        assert!(config.odd_color.is_some());
        assert!(config.even_color.is_none());
    }

    #[test]
    fn test_zebra_config_get_color_for_row() {
        let odd_color = Color::rgb(0.9, 0.9, 0.9);
        let config = ZebraConfig::simple(odd_color);

        // Row 0 is even (start_with_odd=true means odd rows get color)
        assert!(config.get_color_for_row(0).is_none()); // even row
        assert!(config.get_color_for_row(1).is_some()); // odd row
        assert!(config.get_color_for_row(2).is_none()); // even row
        assert!(config.get_color_for_row(3).is_some()); // odd row
    }

    #[test]
    fn test_zebra_config_alternating() {
        let odd = Color::rgb(0.9, 0.9, 0.9);
        let even = Color::rgb(0.95, 0.95, 0.95);
        let config = ZebraConfig::new(Some(odd), Some(even));

        // Both even and odd rows should have colors
        assert!(config.get_color_for_row(0).is_some()); // even row
        assert!(config.get_color_for_row(1).is_some()); // odd row
    }

    // =============================================================================
    // AdvancedTableBuilder tests
    // =============================================================================

    #[test]
    fn test_builder_new() {
        let builder = AdvancedTableBuilder::new();
        let table = builder.add_column("Col1", 100.0).build().unwrap();
        assert_eq!(table.columns.len(), 1);
        assert!(table.rows.is_empty());
    }

    #[test]
    fn test_builder_default() {
        let builder = AdvancedTableBuilder::default();
        assert!(builder.table.columns.is_empty());
    }

    #[test]
    fn test_builder_add_column() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_column("B", 75.0)
            .build()
            .unwrap();
        assert_eq!(table.columns.len(), 2);
        assert_eq!(table.columns[0].width, 50.0);
        assert_eq!(table.columns[1].width, 75.0);
    }

    #[test]
    fn test_builder_add_styled_column() {
        let style = CellStyle::header();
        let table = AdvancedTableBuilder::new()
            .add_styled_column("Header", 100.0, style)
            .build()
            .unwrap();
        assert!(table.columns[0].default_style.is_some());
    }

    #[test]
    fn test_builder_columns_equal_width() {
        let table = AdvancedTableBuilder::new()
            .columns_equal_width(vec!["A", "B", "C", "D"], 400.0)
            .build()
            .unwrap();
        assert_eq!(table.columns.len(), 4);
        assert_eq!(table.columns[0].width, 100.0);
        assert_eq!(table.total_width, Some(400.0));
    }

    #[test]
    fn test_builder_add_row() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_row(vec!["Value"])
            .build()
            .unwrap();
        assert_eq!(table.rows.len(), 1);
        assert_eq!(table.rows[0].cells[0].content, "Value");
    }

    #[test]
    fn test_builder_add_row_with_min_height() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_row_with_min_height(vec!["Value"], 30.0)
            .build()
            .unwrap();
        assert_eq!(table.rows[0].min_height, Some(30.0));
    }

    #[test]
    fn test_builder_add_row_cells() {
        let cells = vec![CellData::new("Cell1").colspan(2), CellData::new("Cell2")];
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_column("B", 50.0)
            .add_column("C", 50.0)
            .add_row_cells(cells)
            .build()
            .unwrap();
        assert_eq!(table.rows[0].cells[0].colspan, 2);
    }

    #[test]
    fn test_builder_add_styled_row() {
        let style = CellStyle::header();
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_styled_row(vec!["Value"], style)
            .build()
            .unwrap();
        assert!(table.rows[0].style.is_some());
    }

    #[test]
    fn test_builder_default_style() {
        let style = CellStyle::new().font_size(14.0);
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .default_style(style.clone())
            .build()
            .unwrap();
        assert_eq!(table.default_style.font_size, Some(14.0));
    }

    #[test]
    fn test_builder_data_style() {
        let style = CellStyle::new().font_size(16.0);
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .data_style(style)
            .build()
            .unwrap();
        assert_eq!(table.default_style.font_size, Some(16.0));
    }

    #[test]
    fn test_builder_header_style() {
        let style = CellStyle::new().font_size(18.0);
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .header_style(style)
            .build()
            .unwrap();
        assert_eq!(table.header_style.font_size, Some(18.0));
    }

    #[test]
    fn test_builder_show_header() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .show_header(false)
            .build()
            .unwrap();
        assert!(!table.show_header);
    }

    #[test]
    fn test_builder_title() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .title("My Table")
            .build()
            .unwrap();
        assert_eq!(table.title, Some("My Table".to_string()));
    }

    #[test]
    fn test_builder_columns() {
        let table = AdvancedTableBuilder::new()
            .columns(vec![("X", 30.0), ("Y", 40.0)])
            .build()
            .unwrap();
        assert_eq!(table.columns.len(), 2);
        assert_eq!(table.columns[0].header, "X");
        assert_eq!(table.columns[1].header, "Y");
    }

    #[test]
    fn test_builder_position() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .position(100.0, 200.0)
            .build()
            .unwrap();
        assert_eq!(table.x, 100.0);
        assert_eq!(table.y, 200.0);
    }

    #[test]
    fn test_builder_zebra_stripes() {
        let color = Color::rgb(0.95, 0.95, 0.95);
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .zebra_stripes(true, color)
            .build()
            .unwrap();
        assert!(table.zebra_striping.is_some());
        // Verify the ZebraConfig was set with the correct color
        let zebra = table.zebra_striping.as_ref().unwrap();
        assert_eq!(zebra.odd_color, Some(color));
    }

    #[test]
    fn test_builder_zebra_stripes_disabled() {
        let color = Color::rgb(0.95, 0.95, 0.95);
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .zebra_stripes(false, color)
            .build()
            .unwrap();
        assert!(table.zebra_striping.is_none());
    }

    #[test]
    fn test_builder_zebra_striping() {
        let color = Color::rgb(0.9, 0.9, 0.9);
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .zebra_striping(color)
            .build()
            .unwrap();
        assert!(table.zebra_striping.is_some());
    }

    #[test]
    fn test_builder_zebra_striping_custom() {
        let config = ZebraConfig::new(
            Some(Color::rgb(0.9, 0.9, 0.9)),
            Some(Color::rgb(1.0, 1.0, 1.0)),
        );
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .zebra_striping_custom(config)
            .build()
            .unwrap();
        assert!(table.zebra_striping.is_some());
    }

    #[test]
    fn test_builder_add_row_with_style() {
        let style = CellStyle::data();
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_row_with_style(vec!["Value"], style)
            .build()
            .unwrap();
        assert!(table.rows[0].style.is_some());
    }

    #[test]
    fn test_builder_add_row_with_mixed_styles() {
        let style1 = CellStyle::header();
        let style2 = CellStyle::data();
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_column("B", 50.0)
            .add_row_with_mixed_styles(vec![(style1, "Header"), (style2, "Data")])
            .build()
            .unwrap();
        assert!(table.rows[0].cells[0].style.is_some());
        assert!(table.rows[0].cells[1].style.is_some());
    }

    #[test]
    fn test_builder_table_border() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .table_border(false)
            .build()
            .unwrap();
        assert!(!table.table_border);
    }

    #[test]
    fn test_builder_cell_spacing() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .cell_spacing(5.0)
            .build()
            .unwrap();
        assert_eq!(table.cell_spacing, 5.0);
    }

    #[test]
    fn test_builder_total_width() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .total_width(500.0)
            .build()
            .unwrap();
        assert_eq!(table.total_width, Some(500.0));
    }

    #[test]
    fn test_builder_repeat_headers() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .repeat_headers(true)
            .build()
            .unwrap();
        assert!(table.repeat_headers);
    }

    #[test]
    fn test_builder_set_cell_style() {
        let style = CellStyle::header();
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_row(vec!["Value"])
            .set_cell_style(0, 0, style)
            .build()
            .unwrap();
        assert!(table.cell_styles.contains_key(&(0, 0)));
    }

    #[test]
    fn test_builder_add_data() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_column("B", 50.0)
            .add_data(vec![vec!["A1", "B1"], vec!["A2", "B2"], vec!["A3", "B3"]])
            .build()
            .unwrap();
        assert_eq!(table.rows.len(), 3);
    }

    #[test]
    fn test_builder_financial_table() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .financial_table()
            .build()
            .unwrap();
        // Financial table sets zebra striping
        assert!(table.zebra_striping.is_some());
        assert!(table.table_border);
    }

    #[test]
    fn test_builder_minimal_table() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .minimal_table()
            .build()
            .unwrap();
        assert!(!table.table_border);
        assert_eq!(table.cell_spacing, 2.0);
    }

    #[test]
    fn test_builder_build_fails_without_columns() {
        let result = AdvancedTableBuilder::new().build();
        assert!(result.is_err());
        match result {
            Err(TableError::NoColumns) => {}
            _ => panic!("Expected NoColumns error"),
        }
    }

    // =============================================================================
    // AdvancedTable tests
    // =============================================================================

    #[test]
    fn test_table_calculate_width_explicit() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_column("B", 75.0)
            .total_width(300.0)
            .build()
            .unwrap();
        assert_eq!(table.calculate_width(), 300.0);
    }

    #[test]
    fn test_table_calculate_width_from_columns() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_column("B", 75.0)
            .build()
            .unwrap();
        assert_eq!(table.calculate_width(), 125.0);
    }

    #[test]
    fn test_table_row_count() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_row(vec!["1"])
            .add_row(vec!["2"])
            .add_row(vec!["3"])
            .build()
            .unwrap();
        assert_eq!(table.row_count(), 3);
    }

    #[test]
    fn test_table_column_count() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_column("B", 50.0)
            .build()
            .unwrap();
        assert_eq!(table.column_count(), 2);
    }

    #[test]
    fn test_table_get_cell_style_specific() {
        let specific_style = CellStyle::header();
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_row(vec!["Value"])
            .set_cell_style(0, 0, specific_style.clone())
            .build()
            .unwrap();
        let style = table.get_cell_style(0, 0);
        assert_eq!(style.font_size, specific_style.font_size);
    }

    #[test]
    fn test_table_get_cell_style_row() {
        let row_style = CellStyle::header();
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_styled_row(vec!["Value"], row_style.clone())
            .build()
            .unwrap();
        let style = table.get_cell_style(0, 0);
        assert_eq!(style.font_size, row_style.font_size);
    }

    #[test]
    fn test_table_get_cell_style_column() {
        let col_style = CellStyle::new().font_size(20.0);
        let table = AdvancedTableBuilder::new()
            .add_styled_column("A", 50.0, col_style.clone())
            .add_row(vec!["Value"])
            .build()
            .unwrap();
        let style = table.get_cell_style(0, 0);
        assert_eq!(style.font_size, Some(20.0));
    }

    #[test]
    fn test_table_get_cell_style_zebra() {
        let zebra_color = Color::rgb(0.9, 0.9, 0.9);
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_row(vec!["Row0"])
            .add_row(vec!["Row1"])
            .zebra_striping(zebra_color)
            .build()
            .unwrap();

        // Zebra applies to odd rows (row 1)
        let style_row1 = table.get_cell_style(1, 0);
        assert!(style_row1.background_color.is_some());
    }

    #[test]
    fn test_table_get_cell_style_column_with_zebra() {
        let col_style = CellStyle::new().font_size(20.0);
        let zebra_color = Color::rgb(0.9, 0.9, 0.9);
        let table = AdvancedTableBuilder::new()
            .add_styled_column("A", 50.0, col_style)
            .add_row(vec!["Row0"])
            .add_row(vec!["Row1"])
            .zebra_striping(zebra_color)
            .build()
            .unwrap();

        // Row 1 (odd) should have column style with zebra background
        let style = table.get_cell_style(1, 0);
        assert_eq!(style.font_size, Some(20.0));
        assert!(style.background_color.is_some());
    }

    #[test]
    fn test_table_validate_success() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_column("B", 50.0)
            .add_row(vec!["1", "2"])
            .add_row(vec!["3", "4"])
            .build()
            .unwrap();
        assert!(table.validate().is_ok());
    }

    #[test]
    fn test_table_validate_column_mismatch() {
        let mut table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_column("B", 50.0)
            .build()
            .unwrap();

        // Manually add a row with wrong number of cells
        table.rows.push(RowData::from_strings(vec!["1", "2", "3"]));

        let result = table.validate();
        assert!(result.is_err());
        match result {
            Err(TableError::ColumnMismatch {
                row,
                found,
                expected,
            }) => {
                assert_eq!(row, 0);
                assert_eq!(found, 3);
                assert_eq!(expected, 2);
            }
            _ => panic!("Expected ColumnMismatch error"),
        }
    }

    #[test]
    fn test_table_get_cell_style_default() {
        let default_style = CellStyle::new().font_size(12.0);
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_row(vec!["Value"])
            .default_style(default_style.clone())
            .build()
            .unwrap();

        let style = table.get_cell_style(0, 0);
        assert_eq!(style.font_size, Some(12.0));
    }

    #[test]
    fn test_table_get_cell_style_invalid_row() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_row(vec!["Value"])
            .build()
            .unwrap();

        // Getting style for non-existent row should return default
        let style = table.get_cell_style(100, 0);
        assert_eq!(style.font_size, table.default_style.font_size);
    }

    #[test]
    fn test_table_get_cell_style_invalid_column() {
        let table = AdvancedTableBuilder::new()
            .add_column("A", 50.0)
            .add_row(vec!["Value"])
            .build()
            .unwrap();

        // Getting style for non-existent column should return default
        let style = table.get_cell_style(0, 100);
        assert_eq!(style.font_size, table.default_style.font_size);
    }

    #[test]
    fn test_get_cell_style_ref_returns_correct_style() {
        use crate::graphics::Color;

        // Case 1: cell-specific style returned by reference
        let cell_style = CellStyle::new().font_size(18.0).text_color(Color::red());
        let table = AdvancedTableBuilder::new()
            .add_column("A", 100.0)
            .add_row(vec!["Value"])
            .set_cell_style(0, 0, cell_style.clone())
            .build()
            .unwrap();

        let style_ref = table.get_cell_style_ref(0, 0);
        assert!(
            (style_ref.font_size.unwrap_or(12.0) - 18.0).abs() < f64::EPSILON,
            "Cell-specific style should have font_size 18.0"
        );

        // Case 2: returns default_style when no cell/row/column style is set
        let table_default = AdvancedTableBuilder::new()
            .add_column("A", 100.0)
            .add_row(vec!["Value"])
            .build()
            .unwrap();

        let style_ref_default = table_default.get_cell_style_ref(0, 0);
        assert_eq!(
            style_ref_default.font_size, table_default.default_style.font_size,
            "Should fall back to default_style when no overrides are set"
        );

        // Case 3: reference is the same font_size as what get_cell_style returns for the non-zebra path
        let style_owned = table.get_cell_style(0, 0);
        let style_ref2 = table.get_cell_style_ref(0, 0);
        assert_eq!(
            style_owned.font_size, style_ref2.font_size,
            "get_cell_style and get_cell_style_ref should agree when no zebra striping"
        );
    }
}