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
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
//! Extended Graphics State Dictionary support according to ISO 32000-1 Section 8.4
//!
//! This module provides comprehensive support for PDF Extended Graphics State (ExtGState)
//! dictionary parameters as specified in ISO 32000-1:2008.

use super::soft_mask::SoftMask;
use crate::error::{PdfError, Result};
use crate::graphics::{LineCap, LineJoin};
use crate::text::Font;
use std::collections::HashMap;
use std::fmt::Write;

/// Rendering intent values according to ISO 32000-1
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RenderingIntent {
    /// Absolute colorimetric
    AbsoluteColorimetric,
    /// Relative colorimetric
    RelativeColorimetric,
    /// Saturation
    Saturation,
    /// Perceptual
    Perceptual,
}

impl RenderingIntent {
    /// Get the PDF name for this rendering intent
    pub fn pdf_name(&self) -> &'static str {
        match self {
            RenderingIntent::AbsoluteColorimetric => "AbsoluteColorimetric",
            RenderingIntent::RelativeColorimetric => "RelativeColorimetric",
            RenderingIntent::Saturation => "Saturation",
            RenderingIntent::Perceptual => "Perceptual",
        }
    }
}

/// Blend mode values for transparency
#[derive(Debug, Clone, PartialEq)]
pub enum BlendMode {
    /// Normal blend mode (default)
    Normal,
    /// Multiply blend mode
    Multiply,
    /// Screen blend mode
    Screen,
    /// Overlay blend mode
    Overlay,
    /// SoftLight blend mode
    SoftLight,
    /// HardLight blend mode
    HardLight,
    /// ColorDodge blend mode
    ColorDodge,
    /// ColorBurn blend mode
    ColorBurn,
    /// Darken blend mode
    Darken,
    /// Lighten blend mode
    Lighten,
    /// Difference blend mode
    Difference,
    /// Exclusion blend mode
    Exclusion,
    /// Hue blend mode (PDF 1.4)
    Hue,
    /// Saturation blend mode (PDF 1.4)
    Saturation,
    /// Color blend mode (PDF 1.4)
    Color,
    /// Luminosity blend mode (PDF 1.4)
    Luminosity,
}

impl BlendMode {
    /// Get the PDF name for this blend mode
    pub fn pdf_name(&self) -> &'static str {
        match self {
            BlendMode::Normal => "Normal",
            BlendMode::Multiply => "Multiply",
            BlendMode::Screen => "Screen",
            BlendMode::Overlay => "Overlay",
            BlendMode::SoftLight => "SoftLight",
            BlendMode::HardLight => "HardLight",
            BlendMode::ColorDodge => "ColorDodge",
            BlendMode::ColorBurn => "ColorBurn",
            BlendMode::Darken => "Darken",
            BlendMode::Lighten => "Lighten",
            BlendMode::Difference => "Difference",
            BlendMode::Exclusion => "Exclusion",
            BlendMode::Hue => "Hue",
            BlendMode::Saturation => "Saturation",
            BlendMode::Color => "Color",
            BlendMode::Luminosity => "Luminosity",
        }
    }
}

/// Line dash pattern specification
#[derive(Debug, Clone, PartialEq)]
pub struct LineDashPattern {
    /// Array of dash and gap lengths
    pub array: Vec<f64>,
    /// Phase offset
    pub phase: f64,
}

impl LineDashPattern {
    /// Create a new line dash pattern
    pub fn new(array: Vec<f64>, phase: f64) -> Self {
        Self { array, phase }
    }

    /// Create a solid line (no dashes)
    pub fn solid() -> Self {
        Self {
            array: Vec::new(),
            phase: 0.0,
        }
    }

    /// Create a simple dashed line
    pub fn dashed(dash_length: f64, gap_length: f64) -> Self {
        Self {
            array: vec![dash_length, gap_length],
            phase: 0.0,
        }
    }

    /// Create a dotted line
    pub fn dotted(dot_size: f64, gap_size: f64) -> Self {
        Self {
            array: vec![dot_size, gap_size],
            phase: 0.0,
        }
    }

    /// Generate PDF representation of the line dash pattern
    pub fn to_pdf_string(&self) -> String {
        if self.array.is_empty() {
            "[] 0".to_string()
        } else {
            let array_str = self
                .array
                .iter()
                .map(|&x| format!("{x:.2}"))
                .collect::<Vec<_>>()
                .join(" ");
            format!("[{array_str}] {:.2}", self.phase)
        }
    }
}

/// Font specification for ExtGState
#[derive(Debug, Clone, PartialEq)]
pub struct ExtGStateFont {
    /// Font
    pub font: Font,
    /// Font size
    pub size: f64,
}

impl ExtGStateFont {
    /// Create a new ExtGState font specification
    pub fn new(font: Font, size: f64) -> Self {
        Self { font, size }
    }
}

/// Transfer function specification according to ISO 32000-1
#[derive(Debug, Clone, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum TransferFunction {
    /// Identity transfer function (no transformation)
    Identity,
    /// Single transfer function for all components
    Single(TransferFunctionData),
    /// Separate transfer functions for each color component (C, M, Y, K or R, G, B)
    Separate {
        /// Function for first component (Cyan or Red)
        c_or_r: TransferFunctionData,
        /// Function for second component (Magenta or Green)
        m_or_g: TransferFunctionData,
        /// Function for third component (Yellow or Blue)
        y_or_b: TransferFunctionData,
        /// Function for fourth component (Black) - optional for RGB
        k: Option<TransferFunctionData>,
    },
}

/// Data for a single transfer function
#[derive(Debug, Clone, PartialEq)]
pub struct TransferFunctionData {
    /// Function type (0, 2, 3, or 4)
    pub function_type: u32,
    /// Domain of the function
    pub domain: Vec<f64>,
    /// Range of the function
    pub range: Vec<f64>,
    /// Function-specific parameters
    pub params: TransferFunctionParams,
}

/// Parameters for different transfer function types
#[derive(Debug, Clone, PartialEq)]
pub enum TransferFunctionParams {
    /// Type 0: Sampled function
    Sampled {
        /// Sample values
        samples: Vec<f64>,
        /// Number of samples in each dimension
        size: Vec<u32>,
        /// Bits per sample
        bits_per_sample: u32,
    },
    /// Type 2: Exponential interpolation
    Exponential {
        /// C0 values
        c0: Vec<f64>,
        /// C1 values
        c1: Vec<f64>,
        /// Exponent
        n: f64,
    },
    /// Type 3: Stitching function
    Stitching {
        /// Functions to stitch together
        functions: Vec<TransferFunctionData>,
        /// Bounds for stitching
        bounds: Vec<f64>,
        /// Encode values
        encode: Vec<f64>,
    },
    /// Type 4: PostScript calculator function
    PostScript {
        /// PostScript code
        code: String,
    },
}

impl TransferFunction {
    /// Create an identity transfer function
    pub fn identity() -> Self {
        TransferFunction::Identity
    }

    /// Create a gamma correction transfer function
    pub fn gamma(gamma_value: f64) -> Self {
        TransferFunction::Single(TransferFunctionData {
            function_type: 2,
            domain: vec![0.0, 1.0],
            range: vec![0.0, 1.0],
            params: TransferFunctionParams::Exponential {
                c0: vec![0.0],
                c1: vec![1.0],
                n: gamma_value,
            },
        })
    }

    /// Create a linear transfer function with slope and intercept
    pub fn linear(slope: f64, intercept: f64) -> Self {
        TransferFunction::Single(TransferFunctionData {
            function_type: 2,
            domain: vec![0.0, 1.0],
            range: vec![0.0, 1.0],
            params: TransferFunctionParams::Exponential {
                c0: vec![intercept],
                c1: vec![slope + intercept],
                n: 1.0,
            },
        })
    }

    /// Convert transfer function to PDF representation
    pub fn to_pdf_string(&self) -> String {
        match self {
            TransferFunction::Identity => "/Identity".to_string(),
            TransferFunction::Single(data) => data.to_pdf_string(),
            TransferFunction::Separate {
                c_or_r,
                m_or_g,
                y_or_b,
                k,
            } => {
                let mut result = String::from("[");
                result.push_str(&c_or_r.to_pdf_string());
                result.push(' ');
                result.push_str(&m_or_g.to_pdf_string());
                result.push(' ');
                result.push_str(&y_or_b.to_pdf_string());
                if let Some(k_func) = k {
                    result.push(' ');
                    result.push_str(&k_func.to_pdf_string());
                }
                result.push(']');
                result
            }
        }
    }
}

impl TransferFunctionData {
    /// Convert transfer function data to PDF representation
    pub fn to_pdf_string(&self) -> String {
        let mut dict = String::from("<<");

        // Function type
        dict.push_str(&format!(" /FunctionType {}", self.function_type));

        // Domain
        dict.push_str(" /Domain [");
        for (i, val) in self.domain.iter().enumerate() {
            if i > 0 {
                dict.push(' ');
            }
            dict.push_str(&format!("{:.3}", val));
        }
        dict.push(']');

        // Range
        dict.push_str(" /Range [");
        for (i, val) in self.range.iter().enumerate() {
            if i > 0 {
                dict.push(' ');
            }
            dict.push_str(&format!("{:.3}", val));
        }
        dict.push(']');

        // Function-specific parameters
        match &self.params {
            TransferFunctionParams::Exponential { c0, c1, n } => {
                // Type 2: Exponential interpolation function
                dict.push_str(" /C0 [");
                for (i, val) in c0.iter().enumerate() {
                    if i > 0 {
                        dict.push(' ');
                    }
                    dict.push_str(&format!("{:.3}", val));
                }
                dict.push_str("] /C1 [");
                for (i, val) in c1.iter().enumerate() {
                    if i > 0 {
                        dict.push(' ');
                    }
                    dict.push_str(&format!("{:.3}", val));
                }
                dict.push_str(&format!("] /N {:.3}", n));
            }
            TransferFunctionParams::Sampled {
                size,
                bits_per_sample,
                samples,
                ..
            } => {
                // Type 0: Sampled function
                dict.push_str(" /Size [");
                for (i, val) in size.iter().enumerate() {
                    if i > 0 {
                        dict.push(' ');
                    }
                    dict.push_str(&format!("{}", val));
                }
                dict.push_str(&format!("] /BitsPerSample {}", bits_per_sample));
                // Samples would be encoded as a stream
                dict.push_str(" /Length ");
                dict.push_str(&format!("{}", samples.len()));
            }
            TransferFunctionParams::Stitching {
                bounds,
                encode,
                functions,
            } => {
                // Type 3: Stitching function
                dict.push_str(" /Bounds [");
                for (i, val) in bounds.iter().enumerate() {
                    if i > 0 {
                        dict.push(' ');
                    }
                    dict.push_str(&format!("{:.3}", val));
                }
                dict.push_str("] /Encode [");
                for (i, val) in encode.iter().enumerate() {
                    if i > 0 {
                        dict.push(' ');
                    }
                    dict.push_str(&format!("{:.3}", val));
                }
                dict.push_str("] /Functions [");
                for (i, func) in functions.iter().enumerate() {
                    if i > 0 {
                        dict.push(' ');
                    }
                    dict.push_str(&func.to_pdf_string());
                }
                dict.push(']');
            }
            TransferFunctionParams::PostScript { code } => {
                // Type 4: PostScript calculator function
                dict.push_str(&format!(
                    " /Length {} stream\n{}\nendstream",
                    code.len(),
                    code
                ));
            }
        }

        dict.push_str(" >>");
        dict
    }
}

/// Halftone specification according to ISO 32000-1
#[derive(Debug, Clone, PartialEq)]
pub enum Halftone {
    /// Default halftone
    Default,
    /// Type 1: Simple halftone
    Type1 {
        /// Halftone frequency
        frequency: f64,
        /// Halftone angle in degrees
        angle: f64,
        /// Spot function name
        spot_function: SpotFunction,
    },
    /// Type 5: Halftone with multiple colorants
    Type5 {
        /// Halftone for each colorant
        colorants: HashMap<String, HalftoneColorant>,
        /// Default halftone
        default: Box<Halftone>,
    },
    /// Type 6: Threshold array
    Type6 {
        /// Width of threshold array
        width: u32,
        /// Height of threshold array
        height: u32,
        /// Threshold values
        thresholds: Vec<u8>,
    },
    /// Type 10: Stochastic (FM) screening
    Type10 {
        /// Halftone frequency
        frequency: f64,
    },
    /// Type 16: Multiple threshold arrays
    Type16 {
        /// Width of threshold arrays
        width: u32,
        /// Height of threshold arrays  
        height: u32,
        /// Multiple threshold arrays
        thresholds: Vec<Vec<u8>>,
    },
}

/// Spot function for halftone screening
#[derive(Debug, Clone, PartialEq)]
pub enum SpotFunction {
    /// Simple dot
    SimpleDot,
    /// Inverted simple dot
    InvertedSimpleDot,
    /// Round dot
    Round,
    /// Inverted round dot
    InvertedRound,
    /// Ellipse
    Ellipse,
    /// Square
    Square,
    /// Cross
    Cross,
    /// Diamond
    Diamond,
    /// Line
    Line,
    /// Custom spot function
    Custom(String),
}

impl SpotFunction {
    /// Get the PDF name for this spot function
    pub fn pdf_name(&self) -> String {
        match self {
            SpotFunction::SimpleDot => "SimpleDot".to_string(),
            SpotFunction::InvertedSimpleDot => "InvertedSimpleDot".to_string(),
            SpotFunction::Round => "Round".to_string(),
            SpotFunction::InvertedRound => "InvertedRound".to_string(),
            SpotFunction::Ellipse => "Ellipse".to_string(),
            SpotFunction::Square => "Square".to_string(),
            SpotFunction::Cross => "Cross".to_string(),
            SpotFunction::Diamond => "Diamond".to_string(),
            SpotFunction::Line => "Line".to_string(),
            SpotFunction::Custom(name) => name.clone(),
        }
    }
}

/// Halftone specification for a single colorant
#[derive(Debug, Clone, PartialEq)]
pub struct HalftoneColorant {
    /// Halftone frequency
    pub frequency: f64,
    /// Halftone angle in degrees
    pub angle: f64,
    /// Spot function
    pub spot_function: SpotFunction,
}

/// Extended Graphics State Dictionary according to ISO 32000-1 Section 8.4
#[derive(Debug, Clone)]
pub struct ExtGState {
    // Line parameters
    /// Line width (LW)
    pub line_width: Option<f64>,
    /// Line cap style (LC)
    pub line_cap: Option<LineCap>,
    /// Line join style (LJ)
    pub line_join: Option<LineJoin>,
    /// Miter limit (ML)
    pub miter_limit: Option<f64>,
    /// Line dash pattern (D)
    pub dash_pattern: Option<LineDashPattern>,

    // Rendering intent
    /// Rendering intent (RI)
    pub rendering_intent: Option<RenderingIntent>,

    // Overprint control
    /// Overprint for stroking operations (OP)
    pub overprint_stroke: Option<bool>,
    /// Overprint for non-stroking operations (op)
    pub overprint_fill: Option<bool>,
    /// Overprint mode (OPM)
    pub overprint_mode: Option<u8>,

    // Font
    /// Font and size (Font)
    pub font: Option<ExtGStateFont>,

    // Color functions (simplified for basic implementation)
    /// Black generation function (BG)
    pub black_generation: Option<TransferFunction>,
    /// Black generation function alternative (BG2)
    pub black_generation_2: Option<TransferFunction>,
    /// Undercolor removal function (UCR)
    pub undercolor_removal: Option<TransferFunction>,
    /// Undercolor removal function alternative (UCR2)
    pub undercolor_removal_2: Option<TransferFunction>,
    /// Transfer function (TR)
    pub transfer_function: Option<TransferFunction>,
    /// Transfer function alternative (TR2)
    pub transfer_function_2: Option<TransferFunction>,

    // Halftone
    /// Halftone dictionary (HT)
    pub halftone: Option<Halftone>,

    // Flatness and smoothness
    /// Flatness tolerance (FL)
    pub flatness: Option<f64>,
    /// Smoothness tolerance (SM)
    pub smoothness: Option<f64>,

    // Additional parameters
    /// Automatic stroke adjustment (SA)
    pub stroke_adjustment: Option<bool>,

    // Transparency parameters (PDF 1.4+)
    /// Blend mode (BM)
    pub blend_mode: Option<BlendMode>,
    /// Soft mask (SMask)
    pub soft_mask: Option<SoftMask>,
    /// Alpha constant for stroking (CA)
    pub alpha_stroke: Option<f64>,
    /// Alpha constant for non-stroking (ca)
    pub alpha_fill: Option<f64>,
    /// Alpha source flag (AIS)
    pub alpha_is_shape: Option<bool>,
    /// Text knockout flag (TK)
    pub text_knockout: Option<bool>,

    // PDF 2.0 additions
    /// Black point compensation (UseBlackPtComp)
    pub use_black_point_compensation: Option<bool>,
}

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

impl ExtGState {
    /// Create a new empty ExtGState dictionary
    pub fn new() -> Self {
        Self {
            line_width: None,
            line_cap: None,
            line_join: None,
            miter_limit: None,
            dash_pattern: None,
            rendering_intent: None,
            overprint_stroke: None,
            overprint_fill: None,
            overprint_mode: None,
            font: None,
            black_generation: None,
            black_generation_2: None,
            undercolor_removal: None,
            undercolor_removal_2: None,
            transfer_function: None,
            transfer_function_2: None,
            halftone: None,
            flatness: None,
            smoothness: None,
            stroke_adjustment: None,
            blend_mode: None,
            soft_mask: None,
            alpha_stroke: None,
            alpha_fill: None,
            alpha_is_shape: None,
            text_knockout: None,
            use_black_point_compensation: None,
        }
    }

    // Line parameter setters
    /// Set line width
    pub fn with_line_width(mut self, width: f64) -> Self {
        self.line_width = Some(width.max(0.0));
        self
    }

    /// Set line cap style
    pub fn with_line_cap(mut self, cap: LineCap) -> Self {
        self.line_cap = Some(cap);
        self
    }

    /// Set line join style
    pub fn with_line_join(mut self, join: LineJoin) -> Self {
        self.line_join = Some(join);
        self
    }

    /// Set miter limit
    pub fn with_miter_limit(mut self, limit: f64) -> Self {
        self.miter_limit = Some(limit.max(1.0));
        self
    }

    /// Set line dash pattern
    pub fn with_dash_pattern(mut self, pattern: LineDashPattern) -> Self {
        self.dash_pattern = Some(pattern);
        self
    }

    // Rendering intent setter
    /// Set rendering intent
    pub fn with_rendering_intent(mut self, intent: RenderingIntent) -> Self {
        self.rendering_intent = Some(intent);
        self
    }

    // Overprint setters
    /// Set overprint for stroking operations
    pub fn with_overprint_stroke(mut self, overprint: bool) -> Self {
        self.overprint_stroke = Some(overprint);
        self
    }

    /// Set overprint for non-stroking operations
    pub fn with_overprint_fill(mut self, overprint: bool) -> Self {
        self.overprint_fill = Some(overprint);
        self
    }

    /// Set overprint mode
    pub fn with_overprint_mode(mut self, mode: u8) -> Self {
        self.overprint_mode = Some(mode);
        self
    }

    // Font setter
    /// Set font and size
    pub fn with_font(mut self, font: Font, size: f64) -> Self {
        self.font = Some(ExtGStateFont::new(font, size.max(0.0)));
        self
    }

    // Flatness and smoothness setters
    /// Set flatness tolerance
    pub fn with_flatness(mut self, flatness: f64) -> Self {
        self.flatness = Some(flatness.clamp(0.0, 100.0));
        self
    }

    /// Set smoothness tolerance
    pub fn with_smoothness(mut self, smoothness: f64) -> Self {
        self.smoothness = Some(smoothness.clamp(0.0, 1.0));
        self
    }

    /// Set automatic stroke adjustment
    pub fn with_stroke_adjustment(mut self, adjustment: bool) -> Self {
        self.stroke_adjustment = Some(adjustment);
        self
    }

    // Transparency setters
    /// Set blend mode
    pub fn with_blend_mode(mut self, mode: BlendMode) -> Self {
        self.blend_mode = Some(mode);
        self
    }

    /// Set alpha constant for stroking operations
    pub fn with_alpha_stroke(mut self, alpha: f64) -> Self {
        self.alpha_stroke = Some(alpha.clamp(0.0, 1.0));
        self
    }

    /// Set alpha constant for non-stroking operations
    pub fn with_alpha_fill(mut self, alpha: f64) -> Self {
        self.alpha_fill = Some(alpha.clamp(0.0, 1.0));
        self
    }

    /// Set alpha constant for both stroking and non-stroking operations
    pub fn with_alpha(mut self, alpha: f64) -> Self {
        let clamped = alpha.clamp(0.0, 1.0);
        self.alpha_stroke = Some(clamped);
        self.alpha_fill = Some(clamped);
        self
    }

    /// Set alpha source flag
    pub fn with_alpha_is_shape(mut self, is_shape: bool) -> Self {
        self.alpha_is_shape = Some(is_shape);
        self
    }

    /// Set text knockout flag
    pub fn with_text_knockout(mut self, knockout: bool) -> Self {
        self.text_knockout = Some(knockout);
        self
    }

    /// Set soft mask for transparency
    pub fn set_soft_mask(&mut self, mask: SoftMask) {
        self.soft_mask = Some(mask);
    }

    /// Set soft mask with a named XObject
    pub fn set_soft_mask_name(&mut self, name: String) {
        self.soft_mask = Some(SoftMask::luminosity(name));
    }

    /// Remove soft mask (set to None)
    pub fn set_soft_mask_none(&mut self) {
        self.soft_mask = Some(SoftMask::none());
    }

    /// Set black point compensation (PDF 2.0)
    pub fn with_black_point_compensation(mut self, use_compensation: bool) -> Self {
        self.use_black_point_compensation = Some(use_compensation);
        self
    }

    // Transfer function setters
    /// Set transfer function for output device gamma correction
    pub fn with_transfer_function(mut self, func: TransferFunction) -> Self {
        self.transfer_function = Some(func);
        self
    }

    /// Set gamma correction transfer function
    pub fn with_gamma_correction(mut self, gamma: f64) -> Self {
        self.transfer_function = Some(TransferFunction::gamma(gamma));
        self
    }

    /// Set linear transfer function with slope and intercept
    pub fn with_linear_transfer(mut self, slope: f64, intercept: f64) -> Self {
        self.transfer_function = Some(TransferFunction::linear(slope, intercept));
        self
    }

    /// Set alternative transfer function (TR2)
    pub fn with_transfer_function_2(mut self, func: TransferFunction) -> Self {
        self.transfer_function_2 = Some(func);
        self
    }

    /// Set black generation function
    pub fn with_black_generation(mut self, func: TransferFunction) -> Self {
        self.black_generation = Some(func);
        self
    }

    /// Set undercolor removal function
    pub fn with_undercolor_removal(mut self, func: TransferFunction) -> Self {
        self.undercolor_removal = Some(func);
        self
    }

    /// Check if any transparency parameters are set
    pub fn uses_transparency(&self) -> bool {
        self.alpha_stroke.is_some_and(|a| a < 1.0)
            || self.alpha_fill.is_some_and(|a| a < 1.0)
            || self.blend_mode.is_some()
            || self.soft_mask.is_some()
    }

    /// Generate PDF dictionary representation
    pub fn to_pdf_dictionary(&self) -> Result<String> {
        let mut dict = String::from("<< /Type /ExtGState");

        // Line parameters
        if let Some(width) = self.line_width {
            write!(&mut dict, " /LW {width:.3}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write line width".to_string())
            })?;
        }

        if let Some(cap) = self.line_cap {
            write!(&mut dict, " /LC {}", cap as u8)
                .map_err(|_| PdfError::InvalidStructure("Failed to write line cap".to_string()))?;
        }

        if let Some(join) = self.line_join {
            write!(&mut dict, " /LJ {}", join as u8)
                .map_err(|_| PdfError::InvalidStructure("Failed to write line join".to_string()))?;
        }

        if let Some(limit) = self.miter_limit {
            write!(&mut dict, " /ML {limit:.3}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write miter limit".to_string())
            })?;
        }

        if let Some(ref pattern) = self.dash_pattern {
            write!(&mut dict, " /D {}", pattern.to_pdf_string()).map_err(|_| {
                PdfError::InvalidStructure("Failed to write dash pattern".to_string())
            })?;
        }

        // Rendering intent
        if let Some(intent) = self.rendering_intent {
            write!(&mut dict, " /RI /{}", intent.pdf_name()).map_err(|_| {
                PdfError::InvalidStructure("Failed to write rendering intent".to_string())
            })?;
        }

        // Overprint control
        if let Some(op) = self.overprint_stroke {
            write!(&mut dict, " /OP {op}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write overprint stroke".to_string())
            })?;
        }

        if let Some(op) = self.overprint_fill {
            write!(&mut dict, " /op {op}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write overprint fill".to_string())
            })?;
        }

        if let Some(mode) = self.overprint_mode {
            write!(&mut dict, " /OPM {mode}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write overprint mode".to_string())
            })?;
        }

        // Font
        if let Some(ref font) = self.font {
            write!(
                &mut dict,
                " /Font [/{} {:.3}]",
                font.font.pdf_name(),
                font.size
            )
            .map_err(|_| PdfError::InvalidStructure("Failed to write font".to_string()))?;
        }

        // Flatness and smoothness
        if let Some(flatness) = self.flatness {
            write!(&mut dict, " /FL {flatness:.3}")
                .map_err(|_| PdfError::InvalidStructure("Failed to write flatness".to_string()))?;
        }

        if let Some(smoothness) = self.smoothness {
            write!(&mut dict, " /SM {smoothness:.3}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write smoothness".to_string())
            })?;
        }

        // Stroke adjustment
        if let Some(sa) = self.stroke_adjustment {
            write!(&mut dict, " /SA {sa}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write stroke adjustment".to_string())
            })?;
        }

        // Transparency parameters
        if let Some(ref mode) = self.blend_mode {
            write!(&mut dict, " /BM /{}", mode.pdf_name()).map_err(|_| {
                PdfError::InvalidStructure("Failed to write blend mode".to_string())
            })?;
        }

        if let Some(ref mask) = self.soft_mask {
            if mask.is_none() {
                write!(&mut dict, " /SMask /None").map_err(|_| {
                    PdfError::InvalidStructure("Failed to write soft mask".to_string())
                })?;
            } else {
                // In a full implementation, this would write the soft mask dictionary
                // For now, we write a reference
                write!(&mut dict, " /SMask {}", mask.to_pdf_string()).map_err(|_| {
                    PdfError::InvalidStructure("Failed to write soft mask".to_string())
                })?;
            }
        }

        if let Some(alpha) = self.alpha_stroke {
            write!(&mut dict, " /CA {alpha:.3}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write stroke alpha".to_string())
            })?;
        }

        if let Some(alpha) = self.alpha_fill {
            write!(&mut dict, " /ca {alpha:.3}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write fill alpha".to_string())
            })?;
        }

        if let Some(ais) = self.alpha_is_shape {
            write!(&mut dict, " /AIS {ais}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write alpha is shape".to_string())
            })?;
        }

        if let Some(tk) = self.text_knockout {
            write!(&mut dict, " /TK {tk}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write text knockout".to_string())
            })?;
        }

        // Transfer functions
        if let Some(ref tf) = self.transfer_function {
            write!(&mut dict, " /TR {}", tf.to_pdf_string()).map_err(|_| {
                PdfError::InvalidStructure("Failed to write transfer function".to_string())
            })?;
        }

        if let Some(ref tf) = self.transfer_function_2 {
            write!(&mut dict, " /TR2 {}", tf.to_pdf_string()).map_err(|_| {
                PdfError::InvalidStructure("Failed to write transfer function 2".to_string())
            })?;
        }

        if let Some(ref bg) = self.black_generation {
            write!(&mut dict, " /BG {}", bg.to_pdf_string()).map_err(|_| {
                PdfError::InvalidStructure("Failed to write black generation".to_string())
            })?;
        }

        if let Some(ref bg) = self.black_generation_2 {
            write!(&mut dict, " /BG2 {}", bg.to_pdf_string()).map_err(|_| {
                PdfError::InvalidStructure("Failed to write black generation 2".to_string())
            })?;
        }

        if let Some(ref ucr) = self.undercolor_removal {
            write!(&mut dict, " /UCR {}", ucr.to_pdf_string()).map_err(|_| {
                PdfError::InvalidStructure("Failed to write undercolor removal".to_string())
            })?;
        }

        if let Some(ref ucr) = self.undercolor_removal_2 {
            write!(&mut dict, " /UCR2 {}", ucr.to_pdf_string()).map_err(|_| {
                PdfError::InvalidStructure("Failed to write undercolor removal 2".to_string())
            })?;
        }

        // PDF 2.0 parameters
        if let Some(use_comp) = self.use_black_point_compensation {
            write!(&mut dict, " /UseBlackPtComp {use_comp}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write black point compensation".to_string())
            })?;
        }

        dict.push_str(" >>");
        Ok(dict)
    }

    /// Check if the ExtGState is empty (no parameters set)
    pub fn is_empty(&self) -> bool {
        self.line_width.is_none()
            && self.line_cap.is_none()
            && self.line_join.is_none()
            && self.miter_limit.is_none()
            && self.dash_pattern.is_none()
            && self.rendering_intent.is_none()
            && self.overprint_stroke.is_none()
            && self.overprint_fill.is_none()
            && self.overprint_mode.is_none()
            && self.font.is_none()
            && self.flatness.is_none()
            && self.smoothness.is_none()
            && self.stroke_adjustment.is_none()
            && self.blend_mode.is_none()
            && self.soft_mask.is_none()
            && self.alpha_stroke.is_none()
            && self.alpha_fill.is_none()
            && self.alpha_is_shape.is_none()
            && self.text_knockout.is_none()
            && self.transfer_function.is_none()
            && self.transfer_function_2.is_none()
            && self.black_generation.is_none()
            && self.black_generation_2.is_none()
            && self.undercolor_removal.is_none()
            && self.undercolor_removal_2.is_none()
            && self.use_black_point_compensation.is_none()
    }

    /// Convert to Dictionary object for PDF writer
    pub fn to_dict(&self) -> crate::objects::Dictionary {
        use crate::objects::{Dictionary, Object};

        let mut dict = Dictionary::new();
        dict.set("Type", Object::Name("ExtGState".to_string()));

        // Line parameters
        if let Some(width) = self.line_width {
            dict.set("LW", Object::Real(width));
        }

        if let Some(cap) = self.line_cap {
            dict.set("LC", Object::Integer(cap as i64));
        }

        if let Some(join) = self.line_join {
            dict.set("LJ", Object::Integer(join as i64));
        }

        if let Some(limit) = self.miter_limit {
            dict.set("ML", Object::Real(limit));
        }

        // Transparency parameters
        if let Some(mode) = &self.blend_mode {
            dict.set("BM", Object::Name(mode.pdf_name().to_string()));
        }

        if let Some(alpha) = self.alpha_stroke {
            dict.set("CA", Object::Real(alpha));
        }

        if let Some(alpha) = self.alpha_fill {
            dict.set("ca", Object::Real(alpha));
        }

        if let Some(ais) = self.alpha_is_shape {
            dict.set("AIS", Object::Boolean(ais));
        }

        if let Some(tk) = self.text_knockout {
            dict.set("TK", Object::Boolean(tk));
        }

        // Other parameters
        if let Some(intent) = &self.rendering_intent {
            dict.set("RI", Object::Name(intent.pdf_name().to_string()));
        }

        if let Some(op) = self.overprint_stroke {
            dict.set("OP", Object::Boolean(op));
        }

        if let Some(op) = self.overprint_fill {
            dict.set("op", Object::Boolean(op));
        }

        if let Some(mode) = self.overprint_mode {
            dict.set("OPM", Object::Integer(mode as i64));
        }

        if let Some(flatness) = self.flatness {
            dict.set("FL", Object::Real(flatness));
        }

        if let Some(smoothness) = self.smoothness {
            dict.set("SM", Object::Real(smoothness));
        }

        if let Some(sa) = self.stroke_adjustment {
            dict.set("SA", Object::Boolean(sa));
        }

        dict
    }
}

/// ExtGState manager for handling multiple graphics states
#[derive(Debug, Clone)]
pub struct ExtGStateManager {
    states: HashMap<String, ExtGState>,
    next_id: usize,
}

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

impl ExtGStateManager {
    /// Create a new ExtGState manager
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
            next_id: 1,
        }
    }

    /// Add an ExtGState and return its name
    pub fn add_state(&mut self, state: ExtGState) -> Result<String> {
        if state.is_empty() {
            return Err(PdfError::InvalidStructure(
                "ExtGState cannot be empty".to_string(),
            ));
        }

        let name = format!("GS{}", self.next_id);
        self.states.insert(name.clone(), state);
        self.next_id += 1;
        Ok(name)
    }

    /// Get an ExtGState by name
    pub fn get_state(&self, name: &str) -> Option<&ExtGState> {
        self.states.get(name)
    }

    /// Get all states
    pub fn states(&self) -> &HashMap<String, ExtGState> {
        &self.states
    }

    /// Generate ExtGState resource dictionary
    pub fn to_resource_dictionary(&self) -> Result<String> {
        if self.states.is_empty() {
            return Ok(String::new());
        }

        let mut dict = String::from("/ExtGState <<");

        for (name, state) in &self.states {
            let state_dict = state.to_pdf_dictionary()?;
            write!(&mut dict, " /{name} {state_dict}").map_err(|_| {
                PdfError::InvalidStructure("Failed to write ExtGState resource".to_string())
            })?;
        }

        dict.push_str(" >>");
        Ok(dict)
    }

    /// Clear all states
    pub fn clear(&mut self) {
        self.states.clear();
        self.next_id = 1;
    }

    /// Count of registered states
    pub fn count(&self) -> usize {
        self.states.len()
    }
}

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

    #[test]
    fn test_rendering_intent_pdf_names() {
        assert_eq!(
            RenderingIntent::AbsoluteColorimetric.pdf_name(),
            "AbsoluteColorimetric"
        );
        assert_eq!(
            RenderingIntent::RelativeColorimetric.pdf_name(),
            "RelativeColorimetric"
        );
        assert_eq!(RenderingIntent::Saturation.pdf_name(), "Saturation");
        assert_eq!(RenderingIntent::Perceptual.pdf_name(), "Perceptual");
    }

    #[test]
    fn test_blend_mode_pdf_names() {
        assert_eq!(BlendMode::Normal.pdf_name(), "Normal");
        assert_eq!(BlendMode::Multiply.pdf_name(), "Multiply");
        assert_eq!(BlendMode::Screen.pdf_name(), "Screen");
        assert_eq!(BlendMode::Overlay.pdf_name(), "Overlay");
    }

    #[test]
    fn test_line_dash_pattern_creation() {
        let solid = LineDashPattern::solid();
        assert!(solid.array.is_empty());
        assert_eq!(solid.phase, 0.0);

        let dashed = LineDashPattern::dashed(5.0, 3.0);
        assert_eq!(dashed.array, vec![5.0, 3.0]);
        assert_eq!(dashed.phase, 0.0);

        let dotted = LineDashPattern::dotted(1.0, 2.0);
        assert_eq!(dotted.array, vec![1.0, 2.0]);
    }

    #[test]
    fn test_line_dash_pattern_pdf_string() {
        let solid = LineDashPattern::solid();
        assert_eq!(solid.to_pdf_string(), "[] 0");

        let dashed = LineDashPattern::dashed(5.0, 3.0);
        assert_eq!(dashed.to_pdf_string(), "[5.00 3.00] 0.00");

        let custom = LineDashPattern::new(vec![10.0, 5.0, 2.0, 5.0], 2.5);
        assert_eq!(custom.to_pdf_string(), "[10.00 5.00 2.00 5.00] 2.50");
    }

    #[test]
    fn test_extgstate_font() {
        let font = ExtGStateFont::new(Font::Helvetica, 12.0);
        assert_eq!(font.font, Font::Helvetica);
        assert_eq!(font.size, 12.0);
    }

    #[test]
    fn test_extgstate_creation() {
        let state = ExtGState::new();
        assert!(state.is_empty());
        assert!(!state.uses_transparency());
    }

    #[test]
    fn test_extgstate_line_parameters() {
        let state = ExtGState::new()
            .with_line_width(2.5)
            .with_line_cap(LineCap::Round)
            .with_line_join(LineJoin::Bevel)
            .with_miter_limit(4.0);

        assert_eq!(state.line_width, Some(2.5));
        assert_eq!(state.line_cap, Some(LineCap::Round));
        assert_eq!(state.line_join, Some(LineJoin::Bevel));
        assert_eq!(state.miter_limit, Some(4.0));
        assert!(!state.is_empty());
    }

    #[test]
    fn test_extgstate_transparency() {
        let state = ExtGState::new()
            .with_alpha_stroke(0.8)
            .with_alpha_fill(0.6)
            .with_blend_mode(BlendMode::Multiply);

        assert_eq!(state.alpha_stroke, Some(0.8));
        assert_eq!(state.alpha_fill, Some(0.6));
        assert_eq!(state.blend_mode, Some(BlendMode::Multiply));
        assert!(state.uses_transparency());
    }

    #[test]
    fn test_extgstate_alpha_clamping() {
        let state = ExtGState::new()
            .with_alpha_stroke(1.5) // Should clamp to 1.0
            .with_alpha_fill(-0.1); // Should clamp to 0.0

        assert_eq!(state.alpha_stroke, Some(1.0));
        assert_eq!(state.alpha_fill, Some(0.0));
    }

    #[test]
    fn test_extgstate_combined_alpha() {
        let state = ExtGState::new().with_alpha(0.5);

        assert_eq!(state.alpha_stroke, Some(0.5));
        assert_eq!(state.alpha_fill, Some(0.5));
    }

    #[test]
    fn test_extgstate_rendering_intent() {
        let state = ExtGState::new().with_rendering_intent(RenderingIntent::Perceptual);

        assert_eq!(state.rendering_intent, Some(RenderingIntent::Perceptual));
    }

    #[test]
    fn test_extgstate_overprint() {
        let state = ExtGState::new()
            .with_overprint_stroke(true)
            .with_overprint_fill(false)
            .with_overprint_mode(1);

        assert_eq!(state.overprint_stroke, Some(true));
        assert_eq!(state.overprint_fill, Some(false));
        assert_eq!(state.overprint_mode, Some(1));
    }

    #[test]
    fn test_extgstate_font_setting() {
        let state = ExtGState::new().with_font(Font::HelveticaBold, 14.0);

        assert!(state.font.is_some());
        let font = state.font.unwrap();
        assert_eq!(font.font, Font::HelveticaBold);
        assert_eq!(font.size, 14.0);
    }

    #[test]
    fn test_extgstate_tolerance_parameters() {
        let state = ExtGState::new()
            .with_flatness(1.5)
            .with_smoothness(0.8)
            .with_stroke_adjustment(true);

        assert_eq!(state.flatness, Some(1.5));
        assert_eq!(state.smoothness, Some(0.8));
        assert_eq!(state.stroke_adjustment, Some(true));
    }

    #[test]
    fn test_extgstate_pdf_dictionary_generation() {
        let state = ExtGState::new()
            .with_line_width(2.0)
            .with_line_cap(LineCap::Round)
            .with_alpha(0.5)
            .with_blend_mode(BlendMode::Multiply);

        let dict = state.to_pdf_dictionary().unwrap();
        assert!(dict.contains("/Type /ExtGState"));
        assert!(dict.contains("/LW 2.000"));
        assert!(dict.contains("/LC 1"));
        assert!(dict.contains("/CA 0.500"));
        assert!(dict.contains("/ca 0.500"));
        assert!(dict.contains("/BM /Multiply"));
    }

    #[test]
    fn test_extgstate_manager_creation() {
        let manager = ExtGStateManager::new();
        assert_eq!(manager.count(), 0);
        assert!(manager.states().is_empty());
    }

    #[test]
    fn test_extgstate_manager_add_state() {
        let mut manager = ExtGStateManager::new();
        let state = ExtGState::new().with_line_width(2.0);

        let name = manager.add_state(state).unwrap();
        assert_eq!(name, "GS1");
        assert_eq!(manager.count(), 1);

        let retrieved = manager.get_state(&name).unwrap();
        assert_eq!(retrieved.line_width, Some(2.0));
    }

    #[test]
    fn test_extgstate_manager_empty_state_rejection() {
        let mut manager = ExtGStateManager::new();
        let empty_state = ExtGState::new();

        let result = manager.add_state(empty_state);
        assert!(result.is_err());
        assert_eq!(manager.count(), 0);
    }

    #[test]
    fn test_extgstate_manager_multiple_states() {
        let mut manager = ExtGStateManager::new();

        let state1 = ExtGState::new().with_line_width(1.0);
        let state2 = ExtGState::new().with_alpha(0.5);

        let name1 = manager.add_state(state1).unwrap();
        let name2 = manager.add_state(state2).unwrap();

        assert_eq!(name1, "GS1");
        assert_eq!(name2, "GS2");
        assert_eq!(manager.count(), 2);
    }

    #[test]
    fn test_extgstate_manager_resource_dictionary() {
        let mut manager = ExtGStateManager::new();

        let state = ExtGState::new().with_line_width(2.0);
        manager.add_state(state).unwrap();

        let dict = manager.to_resource_dictionary().unwrap();
        assert!(dict.contains("/ExtGState"));
        assert!(dict.contains("/GS1"));
        assert!(dict.contains("/LW 2.000"));
    }

    #[test]
    fn test_extgstate_manager_clear() {
        let mut manager = ExtGStateManager::new();

        let state = ExtGState::new().with_line_width(1.0);
        manager.add_state(state).unwrap();
        assert_eq!(manager.count(), 1);

        manager.clear();
        assert_eq!(manager.count(), 0);
        assert!(manager.states().is_empty());
    }

    #[test]
    fn test_extgstate_value_validation() {
        // Test line width validation (non-negative)
        let state = ExtGState::new().with_line_width(-1.0);
        assert_eq!(state.line_width, Some(0.0));

        // Test miter limit validation (>= 1.0)
        let state = ExtGState::new().with_miter_limit(0.5);
        assert_eq!(state.miter_limit, Some(1.0));

        // Test flatness validation (0-100)
        let state = ExtGState::new().with_flatness(150.0);
        assert_eq!(state.flatness, Some(100.0));

        // Test smoothness validation (0-1)
        let state = ExtGState::new().with_smoothness(1.5);
        assert_eq!(state.smoothness, Some(1.0));

        // Test font size validation (non-negative)
        let state = ExtGState::new().with_font(Font::Helvetica, -5.0);
        assert_eq!(state.font.unwrap().size, 0.0);
    }

    #[test]
    fn test_line_dash_patterns() {
        let state = ExtGState::new().with_dash_pattern(LineDashPattern::dashed(10.0, 5.0));

        let dict = state.to_pdf_dictionary().unwrap();
        assert!(dict.contains("/D [10.00 5.00] 0.00"));
    }

    #[test]
    fn test_complex_extgstate() {
        let dash_pattern = LineDashPattern::new(vec![3.0, 2.0, 1.0, 2.0], 1.0);

        let state = ExtGState::new()
            .with_line_width(1.5)
            .with_line_cap(LineCap::Square)
            .with_line_join(LineJoin::Round)
            .with_miter_limit(10.0)
            .with_dash_pattern(dash_pattern)
            .with_rendering_intent(RenderingIntent::Saturation)
            .with_overprint_stroke(true)
            .with_overprint_fill(false)
            .with_font(Font::TimesBold, 18.0)
            .with_flatness(0.5)
            .with_smoothness(0.1)
            .with_stroke_adjustment(false)
            .with_blend_mode(BlendMode::SoftLight)
            .with_alpha_stroke(0.8)
            .with_alpha_fill(0.6)
            .with_alpha_is_shape(true)
            .with_text_knockout(false);

        assert!(!state.is_empty());
        assert!(state.uses_transparency());

        let dict = state.to_pdf_dictionary().unwrap();
        assert!(dict.contains("/Type /ExtGState"));
        assert!(dict.contains("/LW 1.500"));
        assert!(dict.contains("/LC 2"));
        assert!(dict.contains("/LJ 1"));
        assert!(dict.contains("/ML 10.000"));
        assert!(dict.contains("/D [3.00 2.00 1.00 2.00] 1.00"));
        assert!(dict.contains("/RI /Saturation"));
        assert!(dict.contains("/OP true"));
        assert!(dict.contains("/op false"));
        assert!(dict.contains("/Font [/Times-Bold 18.000]"));
        assert!(dict.contains("/FL 0.500"));
        assert!(dict.contains("/SM 0.100"));
        assert!(dict.contains("/SA false"));
        assert!(dict.contains("/BM /SoftLight"));
        assert!(dict.contains("/CA 0.800"));
        assert!(dict.contains("/ca 0.600"));
        assert!(dict.contains("/AIS true"));
        assert!(dict.contains("/TK false"));
    }

    #[test]
    fn test_transfer_function_identity() {
        let tf = TransferFunction::identity();
        assert_eq!(tf.to_pdf_string(), "/Identity");
    }

    #[test]
    fn test_transfer_function_gamma() {
        let tf = TransferFunction::gamma(2.2);
        let pdf = tf.to_pdf_string();
        assert!(pdf.contains("/FunctionType 2"));
        assert!(pdf.contains("/N 2.200"));
        assert!(pdf.contains("/Domain [0.000 1.000]"));
        assert!(pdf.contains("/Range [0.000 1.000]"));
        assert!(pdf.contains("/C0 [0.000]"));
        assert!(pdf.contains("/C1 [1.000]"));
    }

    #[test]
    fn test_transfer_function_linear() {
        let tf = TransferFunction::linear(0.8, 0.1);
        let pdf = tf.to_pdf_string();
        assert!(pdf.contains("/FunctionType 2"));
        assert!(pdf.contains("/N 1.000"));
        assert!(pdf.contains("/C0 [0.100]")); // intercept
        assert!(pdf.contains("/C1 [0.900]")); // slope + intercept
    }

    #[test]
    fn test_extgstate_with_transfer_functions() {
        let state = ExtGState::new()
            .with_gamma_correction(1.8)
            .with_transfer_function_2(TransferFunction::identity())
            .with_black_generation(TransferFunction::linear(1.0, 0.0))
            .with_undercolor_removal(TransferFunction::gamma(2.2));

        assert!(!state.is_empty());

        let dict = state.to_pdf_dictionary().unwrap();
        assert!(dict.contains("/TR"));
        assert!(dict.contains("/TR2 /Identity"));
        assert!(dict.contains("/BG"));
        assert!(dict.contains("/UCR"));
        assert!(dict.contains("/N 1.800")); // gamma value for TR
        assert!(dict.contains("/N 2.200")); // gamma value for UCR
    }

    #[test]
    fn test_transfer_function_separate() {
        let c_func = TransferFunctionData {
            function_type: 2,
            domain: vec![0.0, 1.0],
            range: vec![0.0, 1.0],
            params: TransferFunctionParams::Exponential {
                c0: vec![0.0],
                c1: vec![1.0],
                n: 1.5,
            },
        };

        let m_func = c_func.clone();
        let y_func = c_func.clone();
        let k_func = Some(TransferFunctionData {
            function_type: 2,
            domain: vec![0.0, 1.0],
            range: vec![0.0, 1.0],
            params: TransferFunctionParams::Exponential {
                c0: vec![0.1],
                c1: vec![0.9],
                n: 2.0,
            },
        });

        let tf = TransferFunction::Separate {
            c_or_r: c_func,
            m_or_g: m_func,
            y_or_b: y_func,
            k: k_func,
        };

        let pdf = tf.to_pdf_string();
        assert!(pdf.starts_with('['));
        assert!(pdf.ends_with(']'));
        assert!(pdf.contains("/FunctionType 2"));
        // Should have 4 functions for CMYK
        assert_eq!(pdf.matches("/FunctionType 2").count(), 4);
    }
}