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
//! Form field appearance streams per ISO 32000-1 ยง12.7.3.3
//!
//! This module handles the visual representation of form fields,
//! including text fields, checkboxes, radio buttons, and buttons.

use crate::error::Result;
use crate::graphics::Color;
use crate::objects::{Dictionary, Object, Stream};

/// Appearance characteristics for form fields
#[derive(Debug, Clone)]
pub struct AppearanceCharacteristics {
    /// Rotation of annotation (0, 90, 180, 270)
    pub rotation: i32,
    /// Border color (RGB)
    pub border_color: Option<Color>,
    /// Background color (RGB)
    pub background_color: Option<Color>,
    /// Normal caption
    pub normal_caption: Option<String>,
    /// Rollover caption
    pub rollover_caption: Option<String>,
    /// Down (pressed) caption
    pub down_caption: Option<String>,
    /// Normal icon
    pub normal_icon: Option<Stream>,
    /// Rollover icon
    pub rollover_icon: Option<Stream>,
    /// Down icon
    pub down_icon: Option<Stream>,
    /// Icon fit parameters
    pub icon_fit: Option<IconFit>,
    /// Text position relative to icon
    pub text_position: TextPosition,
}

impl Default for AppearanceCharacteristics {
    fn default() -> Self {
        Self {
            rotation: 0,
            border_color: None,
            background_color: None,
            normal_caption: None,
            rollover_caption: None,
            down_caption: None,
            normal_icon: None,
            rollover_icon: None,
            down_icon: None,
            icon_fit: None,
            text_position: TextPosition::CaptionOnly,
        }
    }
}

/// Icon fit parameters
#[derive(Debug, Clone)]
pub struct IconFit {
    /// Scale type
    pub scale_type: IconScaleType,
    /// Scale when type
    pub scale_when: IconScaleWhen,
    /// Horizontal alignment (0.0 = left, 1.0 = right)
    pub align_x: f64,
    /// Vertical alignment (0.0 = bottom, 1.0 = top)
    pub align_y: f64,
    /// Fit to bounds ignoring aspect ratio
    pub fit_bounds: bool,
}

/// How to scale icon
#[derive(Debug, Clone, PartialEq)]
pub enum IconScaleType {
    /// Always scale
    Always,
    /// Scale only when icon is bigger
    Bigger,
    /// Scale only when icon is smaller
    Smaller,
    /// Never scale
    Never,
}

/// When to scale icon
#[derive(Debug, Clone, PartialEq)]
pub enum IconScaleWhen {
    /// Always scale
    Always,
    /// Scale only when icon is bigger than bounds
    IconBigger,
    /// Scale only when icon is smaller than bounds
    IconSmaller,
    /// Never scale
    Never,
}

/// Text position relative to icon
#[derive(Debug, Clone, PartialEq)]
pub enum TextPosition {
    /// No icon, caption only
    CaptionOnly,
    /// No caption, icon only
    IconOnly,
    /// Caption below icon
    CaptionBelowIcon,
    /// Caption above icon
    CaptionAboveIcon,
    /// Caption to the right of icon
    CaptionRightIcon,
    /// Caption to the left of icon
    CaptionLeftIcon,
    /// Caption overlaid on icon
    CaptionOverlayIcon,
}

impl AppearanceCharacteristics {
    /// Convert to PDF dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();

        if self.rotation != 0 {
            dict.set("R", Object::Integer(self.rotation as i64));
        }

        if let Some(color) = &self.border_color {
            dict.set("BC", color.to_array());
        }

        if let Some(color) = &self.background_color {
            dict.set("BG", color.to_array());
        }

        if let Some(caption) = &self.normal_caption {
            dict.set("CA", Object::String(caption.clone()));
        }

        if let Some(caption) = &self.rollover_caption {
            dict.set("RC", Object::String(caption.clone()));
        }

        if let Some(caption) = &self.down_caption {
            dict.set("AC", Object::String(caption.clone()));
        }

        if let Some(fit) = &self.icon_fit {
            dict.set("IF", fit.to_dict());
        }

        dict.set("TP", Object::Integer(self.text_position.to_int()));

        dict
    }
}

impl IconFit {
    /// Convert to PDF dictionary
    pub fn to_dict(&self) -> Object {
        let mut dict = Dictionary::new();

        dict.set(
            "SW",
            Object::Name(
                match self.scale_when {
                    IconScaleWhen::Always => "A",
                    IconScaleWhen::IconBigger => "B",
                    IconScaleWhen::IconSmaller => "S",
                    IconScaleWhen::Never => "N",
                }
                .to_string(),
            ),
        );

        dict.set(
            "S",
            Object::Name(
                match self.scale_type {
                    IconScaleType::Always => "A",
                    IconScaleType::Bigger => "B",
                    IconScaleType::Smaller => "S",
                    IconScaleType::Never => "N",
                }
                .to_string(),
            ),
        );

        dict.set(
            "A",
            Object::Array(vec![Object::Real(self.align_x), Object::Real(self.align_y)]),
        );

        if self.fit_bounds {
            dict.set("FB", Object::Boolean(true));
        }

        Object::Dictionary(dict)
    }
}

impl TextPosition {
    pub fn to_int(&self) -> i64 {
        match self {
            TextPosition::CaptionOnly => 0,
            TextPosition::IconOnly => 1,
            TextPosition::CaptionBelowIcon => 2,
            TextPosition::CaptionAboveIcon => 3,
            TextPosition::CaptionRightIcon => 4,
            TextPosition::CaptionLeftIcon => 5,
            TextPosition::CaptionOverlayIcon => 6,
        }
    }
}

/// Text field appearance generator
pub struct FieldAppearanceGenerator {
    /// Field value
    pub value: String,
    /// Font to use
    pub font: String,
    /// Font size
    pub font_size: f64,
    /// Text color
    pub text_color: Color,
    /// Background color
    pub background_color: Option<Color>,
    /// Border color
    pub border_color: Option<Color>,
    /// Border width
    pub border_width: f64,
    /// Field rectangle [x1, y1, x2, y2]
    pub rect: [f64; 4],
    /// Text alignment
    pub alignment: TextAlignment,
    /// Multi-line field
    pub multiline: bool,
    /// Max length (for comb fields)
    pub max_length: Option<usize>,
    /// Comb field (evenly spaced characters)
    pub comb: bool,
}

/// Text alignment in fields
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TextAlignment {
    Left,
    Center,
    Right,
}

impl FieldAppearanceGenerator {
    /// Generate appearance stream for text field
    pub fn generate_text_field(&self) -> Result<Stream> {
        let mut ops = Vec::new();
        let width = self.rect[2] - self.rect[0];
        let height = self.rect[3] - self.rect[1];

        // Save graphics state
        ops.push("q".to_string());

        // Draw background if specified
        if let Some(bg_color) = &self.background_color {
            ops.push(format!(
                "{} {} {} rg",
                bg_color.r(),
                bg_color.g(),
                bg_color.b()
            ));
            ops.push(format!("0 0 {} {} re", width, height));
            ops.push("f".to_string());
        }

        // Draw border if specified
        if let Some(border_color) = &self.border_color {
            if self.border_width > 0.0 {
                ops.push(format!("{} w", self.border_width));
                ops.push(format!(
                    "{} {} {} RG",
                    border_color.r(),
                    border_color.g(),
                    border_color.b()
                ));
                ops.push(format!(
                    "{} {} {} {} re",
                    self.border_width / 2.0,
                    self.border_width / 2.0,
                    width - self.border_width,
                    height - self.border_width
                ));
                ops.push("S".to_string());
            }
        }

        // Begin text
        ops.push("BT".to_string());

        // Set font and size
        ops.push(format!("/{} {} Tf", self.font, self.font_size));

        // Set text color
        ops.push(format!(
            "{} {} {} rg",
            self.text_color.r(),
            self.text_color.g(),
            self.text_color.b()
        ));

        // Calculate text position
        let padding = 2.0;
        let text_y = height / 2.0 - self.font_size / 2.0;

        if self.comb {
            if let Some(max_len) = self.max_length {
                // Comb field - evenly space characters
                let char_width = (width - 2.0 * padding) / max_len as f64;

                for (i, ch) in self.value.chars().take(max_len).enumerate() {
                    let x = padding + (i as f64 + 0.5) * char_width;
                    ops.push(format!("{} {} Td", x, text_y));
                    ops.push(format!("({}) Tj", escape_string(&ch.to_string())));
                    if i < self.value.len() - 1 {
                        ops.push(format!("{} 0 Td", -x));
                    }
                }
            }
        } else if self.multiline {
            // Multi-line text field
            let lines = self.value.lines();
            let line_height = self.font_size * 1.2;
            let mut y = height - padding - self.font_size;

            for line in lines {
                let x = match self.alignment {
                    TextAlignment::Left => padding,
                    TextAlignment::Center => width / 2.0,
                    TextAlignment::Right => width - padding,
                };

                ops.push(format!("{} {} Td", x, y));
                ops.push(format!("({}) Tj", escape_string(line)));

                y -= line_height;
                if y < padding {
                    break;
                }
            }
        } else {
            // Single line text field
            let x = match self.alignment {
                TextAlignment::Left => padding,
                TextAlignment::Center => width / 2.0,
                TextAlignment::Right => width - padding,
            };

            ops.push(format!("{} {} Td", x, text_y));
            ops.push(format!("({}) Tj", escape_string(&self.value)));
        }

        // End text
        ops.push("ET".to_string());

        // Restore graphics state
        ops.push("Q".to_string());

        let content = ops.join("\n");

        let mut stream = Stream::new(content.into_bytes());
        stream
            .dictionary_mut()
            .set("Type", Object::Name("XObject".to_string()));
        stream
            .dictionary_mut()
            .set("Subtype", Object::Name("Form".to_string()));
        stream.dictionary_mut().set(
            "BBox",
            Object::Array(vec![
                Object::Real(0.0),
                Object::Real(0.0),
                Object::Real(width),
                Object::Real(height),
            ]),
        );

        Ok(stream)
    }
}

/// Checkbox/Radio button appearance generator
pub struct ButtonAppearanceGenerator {
    /// Button style
    pub style: ButtonStyle,
    /// Size of the button
    pub size: f64,
    /// Border color
    pub border_color: Color,
    /// Background color
    pub background_color: Color,
    /// Check/dot color
    pub check_color: Color,
    /// Border width
    pub border_width: f64,
}

/// Button visual style
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ButtonStyle {
    /// Checkbox with checkmark
    Check,
    /// Checkbox with cross
    Cross,
    /// Checkbox with diamond
    Diamond,
    /// Checkbox with circle
    Circle,
    /// Checkbox with star
    Star,
    /// Checkbox with square
    Square,
    /// Radio button (circle with dot)
    Radio,
}

impl ButtonAppearanceGenerator {
    /// Generate appearance stream for checked state
    pub fn generate_checked(&self) -> Result<Stream> {
        let mut ops = Vec::new();

        // Save graphics state
        ops.push("q".to_string());

        // Draw background
        ops.push(format!(
            "{} {} {} rg",
            self.background_color.r(),
            self.background_color.g(),
            self.background_color.b()
        ));

        match self.style {
            ButtonStyle::Radio => {
                // Circle background
                self.draw_circle(&mut ops, self.size / 2.0, self.size / 2.0, self.size / 2.0);
                ops.push("f".to_string());

                // Draw border
                if self.border_width > 0.0 {
                    ops.push(format!("{} w", self.border_width));
                    ops.push(format!(
                        "{} {} {} RG",
                        self.border_color.r(),
                        self.border_color.g(),
                        self.border_color.b()
                    ));
                    ops.push("s".to_string());
                }

                // Draw dot
                let dot_size = self.size * 0.3;
                ops.push(format!(
                    "{} {} {} rg",
                    self.check_color.r(),
                    self.check_color.g(),
                    self.check_color.b()
                ));
                self.draw_circle(&mut ops, self.size / 2.0, self.size / 2.0, dot_size);
                ops.push("f".to_string());
            }
            _ => {
                // Rectangle background
                ops.push(format!("0 0 {} {} re", self.size, self.size));
                ops.push("f".to_string());

                // Draw border
                if self.border_width > 0.0 {
                    ops.push(format!("{} w", self.border_width));
                    ops.push(format!(
                        "{} {} {} RG",
                        self.border_color.r(),
                        self.border_color.g(),
                        self.border_color.b()
                    ));
                    ops.push(format!(
                        "{} {} {} {} re",
                        self.border_width / 2.0,
                        self.border_width / 2.0,
                        self.size - self.border_width,
                        self.size - self.border_width
                    ));
                    ops.push("S".to_string());
                }

                // Draw check mark based on style
                ops.push(format!(
                    "{} {} {} rg",
                    self.check_color.r(),
                    self.check_color.g(),
                    self.check_color.b()
                ));

                self.draw_check_style(&mut ops);
            }
        }

        // Restore graphics state
        ops.push("Q".to_string());

        let content = ops.join("\n");

        let mut stream = Stream::new(content.into_bytes());
        stream
            .dictionary_mut()
            .set("Type", Object::Name("XObject".to_string()));
        stream
            .dictionary_mut()
            .set("Subtype", Object::Name("Form".to_string()));
        stream.dictionary_mut().set(
            "BBox",
            Object::Array(vec![
                Object::Real(0.0),
                Object::Real(0.0),
                Object::Real(self.size),
                Object::Real(self.size),
            ]),
        );

        Ok(stream)
    }

    /// Generate appearance stream for unchecked state
    pub fn generate_unchecked(&self) -> Result<Stream> {
        let mut ops = Vec::new();

        // Save graphics state
        ops.push("q".to_string());

        // Draw background
        ops.push(format!(
            "{} {} {} rg",
            self.background_color.r(),
            self.background_color.g(),
            self.background_color.b()
        ));

        if self.style == ButtonStyle::Radio {
            // Circle background
            self.draw_circle(&mut ops, self.size / 2.0, self.size / 2.0, self.size / 2.0);
            ops.push("f".to_string());

            // Draw border
            if self.border_width > 0.0 {
                ops.push(format!("{} w", self.border_width));
                ops.push(format!(
                    "{} {} {} RG",
                    self.border_color.r(),
                    self.border_color.g(),
                    self.border_color.b()
                ));
                ops.push("s".to_string());
            }
        } else {
            // Rectangle background
            ops.push(format!("0 0 {} {} re", self.size, self.size));
            ops.push("f".to_string());

            // Draw border
            if self.border_width > 0.0 {
                ops.push(format!("{} w", self.border_width));
                ops.push(format!(
                    "{} {} {} RG",
                    self.border_color.r(),
                    self.border_color.g(),
                    self.border_color.b()
                ));
                ops.push(format!(
                    "{} {} {} {} re",
                    self.border_width / 2.0,
                    self.border_width / 2.0,
                    self.size - self.border_width,
                    self.size - self.border_width
                ));
                ops.push("S".to_string());
            }
        }

        // Restore graphics state
        ops.push("Q".to_string());

        let content = ops.join("\n");

        let mut stream = Stream::new(content.into_bytes());
        stream
            .dictionary_mut()
            .set("Type", Object::Name("XObject".to_string()));
        stream
            .dictionary_mut()
            .set("Subtype", Object::Name("Form".to_string()));
        stream.dictionary_mut().set(
            "BBox",
            Object::Array(vec![
                Object::Real(0.0),
                Object::Real(0.0),
                Object::Real(self.size),
                Object::Real(self.size),
            ]),
        );

        Ok(stream)
    }

    fn draw_circle(&self, ops: &mut Vec<String>, cx: f64, cy: f64, r: f64) {
        // Draw circle using Bezier curves
        let k = 0.552284749831; // Magic constant for circle approximation

        ops.push(format!("{} {} m", cx + r, cy));
        ops.push(format!(
            "{} {} {} {} {} {} c",
            cx + r,
            cy + r * k,
            cx + r * k,
            cy + r,
            cx,
            cy + r
        ));
        ops.push(format!(
            "{} {} {} {} {} {} c",
            cx - r * k,
            cy + r,
            cx - r,
            cy + r * k,
            cx - r,
            cy
        ));
        ops.push(format!(
            "{} {} {} {} {} {} c",
            cx - r,
            cy - r * k,
            cx - r * k,
            cy - r,
            cx,
            cy - r
        ));
        ops.push(format!(
            "{} {} {} {} {} {} c",
            cx + r * k,
            cy - r,
            cx + r,
            cy - r * k,
            cx + r,
            cy
        ));
    }

    fn draw_check_style(&self, ops: &mut Vec<String>) {
        match self.style {
            ButtonStyle::Check => {
                // Draw checkmark
                ops.push(format!("{} w", self.size * 0.1));
                ops.push(format!("{} {} m", self.size * 0.2, self.size * 0.5));
                ops.push(format!("{} {} l", self.size * 0.4, self.size * 0.3));
                ops.push(format!("{} {} l", self.size * 0.8, self.size * 0.7));
                ops.push("S".to_string());
            }
            ButtonStyle::Cross => {
                // Draw X
                ops.push(format!("{} w", self.size * 0.1));
                ops.push(format!("{} {} m", self.size * 0.2, self.size * 0.2));
                ops.push(format!("{} {} l", self.size * 0.8, self.size * 0.8));
                ops.push(format!("{} {} m", self.size * 0.2, self.size * 0.8));
                ops.push(format!("{} {} l", self.size * 0.8, self.size * 0.2));
                ops.push("S".to_string());
            }
            ButtonStyle::Diamond => {
                // Draw diamond
                ops.push(format!("{} {} m", self.size * 0.5, self.size * 0.8));
                ops.push(format!("{} {} l", self.size * 0.8, self.size * 0.5));
                ops.push(format!("{} {} l", self.size * 0.5, self.size * 0.2));
                ops.push(format!("{} {} l", self.size * 0.2, self.size * 0.5));
                ops.push("f".to_string());
            }
            ButtonStyle::Circle => {
                // Draw filled circle
                self.draw_circle(ops, self.size / 2.0, self.size / 2.0, self.size * 0.3);
                ops.push("f".to_string());
            }
            ButtonStyle::Star => {
                // Draw star
                let cx = self.size / 2.0;
                let cy = self.size / 2.0;
                let r = self.size * 0.4;

                ops.push(format!("{} {} m", cx, cy + r));
                for i in 1..10 {
                    let angle = i as f64 * 36.0 * std::f64::consts::PI / 180.0;
                    let radius = if i % 2 == 0 { r } else { r * 0.5 };
                    let x = cx + radius * angle.sin();
                    let y = cy + radius * angle.cos();
                    ops.push(format!("{} {} l", x, y));
                }
                ops.push("f".to_string());
            }
            ButtonStyle::Square => {
                // Draw filled square
                let inset = self.size * 0.25;
                ops.push(format!(
                    "{} {} {} {} re",
                    inset,
                    inset,
                    self.size - 2.0 * inset,
                    self.size - 2.0 * inset
                ));
                ops.push("f".to_string());
            }
            _ => {}
        }
    }
}

/// Escape special characters in PDF strings
fn escape_string(s: &str) -> String {
    s.chars()
        .map(|c| match c {
            '(' => "\\(".to_string(),
            ')' => "\\)".to_string(),
            '\\' => "\\\\".to_string(),
            '\n' => "\\n".to_string(),
            '\r' => "\\r".to_string(),
            '\t' => "\\t".to_string(),
            c => c.to_string(),
        })
        .collect()
}

/// Push button appearance generator
pub struct PushButtonAppearanceGenerator {
    /// Button caption
    pub caption: String,
    /// Font to use
    pub font: String,
    /// Font size
    pub font_size: f64,
    /// Text color
    pub text_color: Color,
    /// Background color
    pub background_color: Color,
    /// Border color
    pub border_color: Color,
    /// Border width
    pub border_width: f64,
    /// Button rectangle [width, height]
    pub size: [f64; 2],
    /// Border style
    pub border_style: ButtonBorderStyle,
}

/// Border style for buttons
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ButtonBorderStyle {
    /// Solid border
    Solid,
    /// Dashed border
    Dashed,
    /// Beveled (3D raised)
    Beveled,
    /// Inset (3D pressed)
    Inset,
    /// Underline only
    Underline,
}

impl PushButtonAppearanceGenerator {
    /// Generate normal appearance
    pub fn generate_normal(&self) -> Result<Stream> {
        self.generate_appearance(false)
    }

    /// Generate rollover appearance
    pub fn generate_rollover(&self) -> Result<Stream> {
        // Make slightly lighter for rollover
        let mut appearance = self.clone();
        appearance.background_color = appearance.background_color.lighten(0.1);
        appearance.generate_appearance(false)
    }

    /// Generate down (pressed) appearance
    pub fn generate_down(&self) -> Result<Stream> {
        self.generate_appearance(true)
    }

    fn generate_appearance(&self, pressed: bool) -> Result<Stream> {
        let mut ops = Vec::new();
        let [width, height] = self.size;

        // Save graphics state
        ops.push("q".to_string());

        // Draw background
        let bg_color = if pressed {
            self.background_color.darken(0.1)
        } else {
            self.background_color
        };

        ops.push(format!(
            "{} {} {} rg",
            bg_color.r(),
            bg_color.g(),
            bg_color.b()
        ));
        ops.push(format!("0 0 {} {} re", width, height));
        ops.push("f".to_string());

        // Draw border based on style
        self.draw_border(&mut ops, width, height, pressed);

        // Draw caption text
        if !self.caption.is_empty() {
            ops.push("BT".to_string());
            ops.push(format!("/{} {} Tf", self.font, self.font_size));
            ops.push(format!(
                "{} {} {} rg",
                self.text_color.r(),
                self.text_color.g(),
                self.text_color.b()
            ));

            // Center text
            let text_x = width / 2.0;
            let text_y = height / 2.0 - self.font_size / 2.0;

            ops.push(format!("{} {} Td", text_x, text_y));
            ops.push(format!("({}) Tj", escape_string(&self.caption)));
            ops.push("ET".to_string());
        }

        // Restore graphics state
        ops.push("Q".to_string());

        let content = ops.join("\n");

        let mut stream = Stream::new(content.into_bytes());
        stream
            .dictionary_mut()
            .set("Type", Object::Name("XObject".to_string()));
        stream
            .dictionary_mut()
            .set("Subtype", Object::Name("Form".to_string()));
        stream.dictionary_mut().set(
            "BBox",
            Object::Array(vec![
                Object::Real(0.0),
                Object::Real(0.0),
                Object::Real(width),
                Object::Real(height),
            ]),
        );

        Ok(stream)
    }

    fn draw_border(&self, ops: &mut Vec<String>, width: f64, height: f64, pressed: bool) {
        match self.border_style {
            ButtonBorderStyle::Solid => {
                if self.border_width > 0.0 {
                    ops.push(format!("{} w", self.border_width));
                    ops.push(format!(
                        "{} {} {} RG",
                        self.border_color.r(),
                        self.border_color.g(),
                        self.border_color.b()
                    ));
                    ops.push(format!(
                        "{} {} {} {} re",
                        self.border_width / 2.0,
                        self.border_width / 2.0,
                        width - self.border_width,
                        height - self.border_width
                    ));
                    ops.push("S".to_string());
                }
            }
            ButtonBorderStyle::Dashed => {
                if self.border_width > 0.0 {
                    ops.push(format!("{} w", self.border_width));
                    ops.push("[3 3] 0 d".to_string()); // Dash pattern
                    ops.push(format!(
                        "{} {} {} RG",
                        self.border_color.r(),
                        self.border_color.g(),
                        self.border_color.b()
                    ));
                    ops.push(format!(
                        "{} {} {} {} re",
                        self.border_width / 2.0,
                        self.border_width / 2.0,
                        width - self.border_width,
                        height - self.border_width
                    ));
                    ops.push("S".to_string());
                }
            }
            ButtonBorderStyle::Beveled | ButtonBorderStyle::Inset => {
                let is_inset = self.border_style == ButtonBorderStyle::Inset || pressed;
                let light_color = if is_inset {
                    self.border_color.darken(0.3)
                } else {
                    self.border_color.lighten(0.3)
                };
                let dark_color = if is_inset {
                    self.border_color.lighten(0.3)
                } else {
                    self.border_color.darken(0.3)
                };

                // Top and left edges (light)
                ops.push(format!("{} w", self.border_width));
                ops.push(format!(
                    "{} {} {} RG",
                    light_color.r(),
                    light_color.g(),
                    light_color.b()
                ));
                ops.push(format!("{} {} m", 0.0, 0.0));
                ops.push(format!("{} {} l", 0.0, height));
                ops.push(format!("{} {} l", width, height));
                ops.push("S".to_string());

                // Bottom and right edges (dark)
                ops.push(format!(
                    "{} {} {} RG",
                    dark_color.r(),
                    dark_color.g(),
                    dark_color.b()
                ));
                ops.push(format!("{} {} m", width, height));
                ops.push(format!("{} {} l", width, 0.0));
                ops.push(format!("{} {} l", 0.0, 0.0));
                ops.push("S".to_string());
            }
            ButtonBorderStyle::Underline => {
                if self.border_width > 0.0 {
                    ops.push(format!("{} w", self.border_width));
                    ops.push(format!(
                        "{} {} {} RG",
                        self.border_color.r(),
                        self.border_color.g(),
                        self.border_color.b()
                    ));
                    ops.push(format!("{} {} m", 0.0, self.border_width / 2.0));
                    ops.push(format!("{} {} l", width, self.border_width / 2.0));
                    ops.push("S".to_string());
                }
            }
        }
    }
}

impl Clone for PushButtonAppearanceGenerator {
    fn clone(&self) -> Self {
        Self {
            caption: self.caption.clone(),
            font: self.font.clone(),
            font_size: self.font_size,
            text_color: self.text_color,
            background_color: self.background_color,
            border_color: self.border_color,
            border_width: self.border_width,
            size: self.size,
            border_style: self.border_style,
        }
    }
}

impl Color {
    pub fn lighten(&self, amount: f64) -> Color {
        Color::rgb(
            (self.r() + amount).min(1.0),
            (self.g() + amount).min(1.0),
            (self.b() + amount).min(1.0),
        )
    }

    pub fn darken(&self, amount: f64) -> Color {
        Color::rgb(
            (self.r() - amount).max(0.0),
            (self.g() - amount).max(0.0),
            (self.b() - amount).max(0.0),
        )
    }

    pub fn to_array(&self) -> Object {
        Object::Array(vec![
            Object::Real(self.r()),
            Object::Real(self.g()),
            Object::Real(self.b()),
        ])
    }
}