fop-render 0.1.1

Rendering backends for Apache FOP (PDF, SVG, etc.)
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
//! PostScript rendering backend
//!
//! Generates PostScript Level 2 documents from area trees.

use crate::image::ImageInfo;
use fop_layout::{AreaId, AreaTree, AreaType};
use fop_types::{Color, Length, Result};
use std::collections::HashMap;

/// PostScript renderer
pub struct PsRenderer {
    /// Default page width (A4)
    #[allow(dead_code)]
    page_width: Length,

    /// Default page height (A4)
    #[allow(dead_code)]
    page_height: Length,
}

impl PsRenderer {
    /// Create a new PostScript renderer
    pub fn new() -> Self {
        Self {
            page_width: Length::from_mm(210.0),
            page_height: Length::from_mm(297.0),
        }
    }

    /// Render an area tree to a PostScript document (returns PS as String)
    pub fn render_to_ps(&self, area_tree: &AreaTree) -> Result<String> {
        let mut ps_doc = PsDocument::new();

        // First pass: collect all images and convert to PS format
        let mut image_map = HashMap::new();
        self.collect_images(area_tree, &mut image_map)?;

        // Second pass: render pages
        for (id, node) in area_tree.iter() {
            if matches!(node.area.area_type, AreaType::Page) {
                let page_ps = self.render_page(area_tree, id, &image_map)?;
                ps_doc.add_page(page_ps);
            }
        }

        Ok(ps_doc.to_string())
    }

    /// Collect all images from the area tree
    fn collect_images(
        &self,
        area_tree: &AreaTree,
        image_map: &mut HashMap<AreaId, ImageData>,
    ) -> Result<()> {
        for (id, node) in area_tree.iter() {
            if matches!(node.area.area_type, AreaType::Viewport) {
                if let Some(image_data) = node.area.image_data() {
                    let image_info = ImageInfo::from_bytes(image_data)?;
                    let ps_image = ImageData {
                        width: image_info.width_px,
                        height: image_info.height_px,
                        data: image_data.to_vec(),
                    };
                    image_map.insert(id, ps_image);
                }
            }
        }
        Ok(())
    }

    /// Render a single page
    fn render_page(
        &self,
        area_tree: &AreaTree,
        page_id: AreaId,
        image_map: &HashMap<AreaId, ImageData>,
    ) -> Result<PsPage> {
        let page_node = area_tree
            .get(page_id)
            .ok_or_else(|| fop_types::FopError::Generic("Page not found".to_string()))?;

        let width = page_node.area.width();
        let height = page_node.area.height();

        let mut ps_page = PsPage::new(width, height);

        // Render all child areas recursively with absolute positioning
        render_children(
            area_tree,
            page_id,
            &mut ps_page,
            Length::ZERO,
            Length::ZERO,
            image_map,
        )?;

        Ok(ps_page)
    }
}

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

/// Image data for PostScript
#[derive(Clone)]
struct ImageData {
    width: u32,
    height: u32,
    #[allow(dead_code)]
    data: Vec<u8>,
}

/// PostScript document builder - manages multiple pages
pub struct PsDocument {
    pages: Vec<PsPage>,
}

impl PsDocument {
    /// Create a new PostScript document
    pub fn new() -> Self {
        Self { pages: Vec::new() }
    }

    /// Add a page to the document
    pub fn add_page(&mut self, page: PsPage) {
        self.pages.push(page);
    }

    /// Write DSC header comments for the document.
    ///
    /// The `%%BoundingBox` is set to the union of all page bounding boxes,
    /// which for a typical document is the bounding box of the first (or
    /// largest) page.
    fn write_dsc_header(&self, result: &mut String) {
        result.push_str("%!PS-Adobe-3.0\n");
        result.push_str("%%Creator: Apache FOP Rust\n");
        result.push_str("%%LanguageLevel: 2\n");
        result.push_str(&format!("%%Pages: {}\n", self.pages.len()));

        // Compute global bounding box (union of all pages)
        if let Some(first_page) = self.pages.first() {
            let max_w = self
                .pages
                .iter()
                .map(|p| p.width.to_pt() as i64)
                .max()
                .unwrap_or(0);
            let max_h = self
                .pages
                .iter()
                .map(|p| p.height.to_pt() as i64)
                .max()
                .unwrap_or(0);
            // Suppress unused warning: first_page is used to initialise max
            let _ = first_page;
            result.push_str(&format!("%%BoundingBox: 0 0 {} {}\n", max_w, max_h));
        } else {
            result.push_str("%%BoundingBox: (atend)\n");
        }

        result.push_str("%%DocumentNeededResources: font Helvetica Helvetica-Bold Helvetica-Oblique Helvetica-BoldOblique Times-Roman Courier\n");
        result.push_str("%%EndComments\n");
        result.push('\n');
    }

    /// Write the prolog section with procedure definitions and font encoding.
    fn write_prolog(&self, result: &mut String) {
        result.push_str("%%BeginProlog\n");

        // Short-hand operators
        result.push_str("/M { moveto } bind def\n");
        result.push_str("/L { lineto } bind def\n");
        result.push_str("/S { stroke } bind def\n");
        result.push_str("/F { fill } bind def\n");
        result.push_str("/NP { newpath } bind def\n");
        result.push_str("/CP { closepath } bind def\n");
        result.push_str("/RGB { setrgbcolor } bind def\n");
        result.push_str("/GRAY { setgray } bind def\n");
        result.push_str("/LW { setlinewidth } bind def\n");
        result.push_str("/SF { setfont } bind def\n");
        result.push_str("/SH { show } bind def\n");
        result.push('\n');

        // Rectangle helpers
        result.push_str("% rect: x y w h -> path\n");
        result.push_str("/rect {\n");
        result.push_str("  /h exch def /w exch def /y exch def /x exch def\n");
        result.push_str("  NP x y M x w add y L x w add y h add L x y h add L CP\n");
        result.push_str("} bind def\n");
        result.push('\n');

        result.push_str("% frect: x y w h -- filled rectangle\n");
        result.push_str("/frect {\n");
        result.push_str("  /h exch def /w exch def /y exch def /x exch def\n");
        result.push_str("  NP x y M x w add y L x w add y h add L x y h add L CP F\n");
        result.push_str("} bind def\n");
        result.push('\n');

        result.push_str("% srect: x y w h -- stroked rectangle\n");
        result.push_str("/srect {\n");
        result.push_str("  /h exch def /w exch def /y exch def /x exch def\n");
        result.push_str("  NP x y M x w add y L x w add y h add L x y h add L CP S\n");
        result.push_str("} bind def\n");
        result.push('\n');

        // Font selection helper: FN fontname fontsize -- selects and scales font
        result.push_str("% FN: /FontName size -> (select and set font)\n");
        result.push_str("/FN {\n");
        result.push_str("  exch findfont exch scalefont SF\n");
        result.push_str("} bind def\n");
        result.push('\n');

        result.push_str("%%EndProlog\n");
        result.push('\n');
    }

    /// Write the setup section (shared resources / font encoding).
    fn write_setup(&self, result: &mut String) {
        result.push_str("%%BeginSetup\n");

        // Re-encode standard fonts to ISOLatin1 for proper character support
        for font_name in &[
            "Helvetica",
            "Helvetica-Bold",
            "Helvetica-Oblique",
            "Helvetica-BoldOblique",
            "Times-Roman",
            "Times-Bold",
            "Courier",
        ] {
            result.push_str(&format!(
                "/{fname} findfont\n\
                 dup length dict begin\n\
                   {{ 1 index /FID ne {{ def }} {{ pop pop }} ifelse }} forall\n\
                   /Encoding ISOLatin1Encoding def\n\
                   currentdict\n\
                 end\n\
                 /{fname} exch definefont pop\n\n",
                fname = font_name
            ));
        }

        result.push_str("%%EndSetup\n");
        result.push('\n');
    }

    /// Write the DSC `%%Page` header for a single page.
    fn write_page_header(page_num: usize, total_pages: usize, page: &PsPage, result: &mut String) {
        result.push_str(&format!("%%Page: {} {}\n", page_num, total_pages));

        let w = page.width.to_pt() as i64;
        let h = page.height.to_pt() as i64;
        result.push_str(&format!("%%PageBoundingBox: 0 0 {} {}\n", w, h));
        result.push_str("%%PageOrientation: Portrait\n");
        result.push_str("%%BeginPageSetup\n");
        result.push_str(&format!("<< /PageSize [{} {}] >> setpagedevice\n", w, h));
        result.push_str("%%EndPageSetup\n");
    }

    /// Convert to PostScript string
    fn build_ps(&self) -> String {
        let mut result = String::new();
        let total = self.pages.len();

        // --- DSC Header ---
        self.write_dsc_header(&mut result);

        // --- Prolog ---
        self.write_prolog(&mut result);

        // --- Setup ---
        self.write_setup(&mut result);

        // --- Pages ---
        for (i, page) in self.pages.iter().enumerate() {
            Self::write_page_header(i + 1, total, page, &mut result);
            result.push_str(&page.to_string());
            result.push_str("showpage\n");
            result.push('\n');
        }

        // --- Trailer ---
        result.push_str("%%Trailer\n");
        result.push_str(&format!("%%Pages: {}\n", total));
        result.push_str("%%EOF\n");

        result
    }
}

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

impl std::fmt::Display for PsDocument {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.build_ps())
    }
}

/// PostScript page builder
pub struct PsPage {
    width: Length,
    height: Length,
    commands: Vec<String>,
}

impl PsPage {
    /// Create a new PostScript page
    pub fn new(width: Length, height: Length) -> Self {
        // Save graphics state at the start of every page
        let commands = vec!["gsave".to_string()];

        Self {
            width,
            height,
            commands,
        }
    }

    /// Add a filled rectangle (for backgrounds)
    #[allow(clippy::too_many_arguments)]
    pub fn add_background(
        &mut self,
        x: Length,
        y: Length,
        width: Length,
        height: Length,
        color: Color,
    ) {
        // Set fill color
        self.set_color(color);

        // Draw filled rectangle
        self.commands.push(format!(
            "{:.3} {:.3} {:.3} {:.3} frect",
            x.to_pt(),
            y.to_pt(),
            width.to_pt(),
            height.to_pt()
        ));
    }

    /// Add borders
    #[allow(clippy::too_many_arguments)]
    pub fn add_borders(
        &mut self,
        x: Length,
        y: Length,
        width: Length,
        height: Length,
        border_widths: [Length; 4],
        border_colors: [Color; 4],
        border_styles: [fop_layout::area::BorderStyle; 4],
    ) {
        use fop_layout::area::BorderStyle;

        let [top_w, right_w, bottom_w, left_w] = border_widths;
        let [top_c, right_c, bottom_c, left_c] = border_colors;
        let [top_s, right_s, bottom_s, left_s] = border_styles;

        // Top border
        if top_w.to_pt() > 0.0 && !matches!(top_s, BorderStyle::None | BorderStyle::Hidden) {
            self.set_color(top_c);
            self.commands.push(format!(
                "{:.3} {:.3} {:.3} {:.3} frect",
                x.to_pt(),
                (y + height - top_w).to_pt(),
                width.to_pt(),
                top_w.to_pt()
            ));
        }

        // Right border
        if right_w.to_pt() > 0.0 && !matches!(right_s, BorderStyle::None | BorderStyle::Hidden) {
            self.set_color(right_c);
            self.commands.push(format!(
                "{:.3} {:.3} {:.3} {:.3} frect",
                (x + width - right_w).to_pt(),
                y.to_pt(),
                right_w.to_pt(),
                height.to_pt()
            ));
        }

        // Bottom border
        if bottom_w.to_pt() > 0.0 && !matches!(bottom_s, BorderStyle::None | BorderStyle::Hidden) {
            self.set_color(bottom_c);
            self.commands.push(format!(
                "{:.3} {:.3} {:.3} {:.3} frect",
                x.to_pt(),
                y.to_pt(),
                width.to_pt(),
                bottom_w.to_pt()
            ));
        }

        // Left border
        if left_w.to_pt() > 0.0 && !matches!(left_s, BorderStyle::None | BorderStyle::Hidden) {
            self.set_color(left_c);
            self.commands.push(format!(
                "{:.3} {:.3} {:.3} {:.3} frect",
                x.to_pt(),
                y.to_pt(),
                left_w.to_pt(),
                height.to_pt()
            ));
        }
    }

    /// Add text using the improved `FN` helper.
    ///
    /// Font selection: bold/italic variants are chosen from `traits`; when no
    /// trait information is available the base Helvetica family is used.
    pub fn add_text(
        &mut self,
        text: &str,
        x: Length,
        y: Length,
        font_size: Length,
        color: Option<Color>,
    ) {
        // Set text color
        let c = color.unwrap_or(Color::BLACK);
        self.set_color(c);

        // Font selection (Helvetica family — no trait info at this call site)
        let font_name = "/Helvetica";
        self.commands
            .push(format!("{} {:.3} FN", font_name, font_size.to_pt()));

        // Escape PostScript special characters
        let escaped_text = escape_ps_string(text);

        // Position and show text
        self.commands.push(format!(
            "{:.3} {:.3} M ({}) SH",
            x.to_pt(),
            y.to_pt(),
            escaped_text
        ));
    }

    /// Add text with explicit font-weight / font-style information.
    #[allow(clippy::too_many_arguments)]
    pub fn add_text_styled(
        &mut self,
        text: &str,
        x: Length,
        y: Length,
        font_size: Length,
        color: Option<Color>,
        bold: bool,
        italic: bool,
    ) {
        let c = color.unwrap_or(Color::BLACK);
        self.set_color(c);

        let font_name = select_ps_font("Helvetica", bold, italic);
        self.commands
            .push(format!("{} {:.3} FN", font_name, font_size.to_pt()));

        let escaped_text = escape_ps_string(text);
        self.commands.push(format!(
            "{:.3} {:.3} M ({}) SH",
            x.to_pt(),
            y.to_pt(),
            escaped_text
        ));
    }

    /// Add a line (for rules, borders)
    #[allow(clippy::too_many_arguments)]
    pub fn add_line(
        &mut self,
        x1: Length,
        y1: Length,
        x2: Length,
        y2: Length,
        color: Color,
        width: Length,
        _style: &str,
    ) {
        self.set_color(color);
        self.commands.push(format!("{:.3} LW", width.to_pt()));
        self.commands.push("NP".to_string());
        self.commands
            .push(format!("{:.3} {:.3} M", x1.to_pt(), y1.to_pt()));
        self.commands
            .push(format!("{:.3} {:.3} L", x2.to_pt(), y2.to_pt()));
        self.commands.push("S".to_string());
    }

    /// Add an image
    #[allow(dead_code)]
    fn add_image(
        &mut self,
        image_data: &ImageData,
        x: Length,
        y: Length,
        width: Length,
        height: Length,
    ) {
        // Save graphics state
        self.commands.push("gsave".to_string());

        // Set up transformation matrix for the image
        self.commands
            .push(format!("{:.3} {:.3} translate", x.to_pt(), y.to_pt()));
        self.commands
            .push(format!("{:.3} {:.3} scale", width.to_pt(), height.to_pt()));

        // Image dictionary — full embedding is complex; draw a placeholder rectangle
        self.commands.push(format!(
            "% image placeholder {}x{}",
            image_data.width, image_data.height
        ));
        self.commands.push("grestore".to_string());

        // Placeholder rectangle
        self.commands.push("0.9 GRAY".to_string());
        self.commands.push(format!(
            "{:.3} {:.3} {:.3} {:.3} frect",
            x.to_pt(),
            y.to_pt(),
            width.to_pt(),
            height.to_pt()
        ));
    }

    /// Set clipping rectangle
    pub fn save_clip_state(&mut self, x: Length, y: Length, width: Length, height: Length) {
        self.commands.push("gsave".to_string());
        self.commands.push("NP".to_string());
        self.commands.push(format!(
            "{:.3} {:.3} {:.3} {:.3} rect",
            x.to_pt(),
            y.to_pt(),
            width.to_pt(),
            height.to_pt()
        ));
        self.commands.push("clip".to_string());
    }

    /// Restore graphics state
    pub fn restore_clip_state(&mut self) {
        self.commands.push("grestore".to_string());
    }

    /// Set RGB color helper
    fn set_color(&mut self, color: Color) {
        self.commands.push(format!(
            "{:.4} {:.4} {:.4} RGB",
            color.r as f64 / 255.0,
            color.g as f64 / 255.0,
            color.b as f64 / 255.0
        ));
    }

    /// Convert to PostScript string
    fn build_ps(&self) -> String {
        let mut result = String::new();

        for cmd in &self.commands {
            result.push_str(cmd);
            result.push('\n');
        }

        // Restore graphics state saved at page start
        result.push_str("grestore\n");

        result
    }
}

impl std::fmt::Display for PsPage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.build_ps())
    }
}

/// Select the PostScript font name for the Helvetica family based on weight/style.
fn select_ps_font(family: &str, bold: bool, italic: bool) -> String {
    // Normalise family name to its PS base name
    let base = match family.to_lowercase().as_str() {
        "times" | "times new roman" | "times-roman" => "Times",
        "courier" | "courier new" => "Courier",
        _ => "Helvetica",
    };

    match (base, bold, italic) {
        ("Times", false, false) => "/Times-Roman".to_string(),
        ("Times", true, false) => "/Times-Bold".to_string(),
        ("Times", false, true) => "/Times-Italic".to_string(),
        ("Times", true, true) => "/Times-BoldItalic".to_string(),
        ("Courier", false, false) => "/Courier".to_string(),
        ("Courier", true, false) => "/Courier-Bold".to_string(),
        ("Courier", false, true) => "/Courier-Oblique".to_string(),
        ("Courier", true, true) => "/Courier-BoldOblique".to_string(),
        // Helvetica (default)
        (_, false, false) => "/Helvetica".to_string(),
        (_, true, false) => "/Helvetica-Bold".to_string(),
        (_, false, true) => "/Helvetica-Oblique".to_string(),
        (_, true, true) => "/Helvetica-BoldOblique".to_string(),
    }
}

/// Render child areas recursively
#[allow(clippy::too_many_arguments)]
fn render_children(
    area_tree: &AreaTree,
    parent_id: AreaId,
    ps_page: &mut PsPage,
    offset_x: Length,
    offset_y: Length,
    image_map: &HashMap<AreaId, ImageData>,
) -> Result<()> {
    let children = area_tree.children(parent_id);

    for child_id in children {
        if let Some(child_node) = area_tree.get(child_id) {
            // Calculate absolute position
            let abs_x = offset_x + child_node.area.geometry.x;
            let abs_y = offset_y + child_node.area.geometry.y;

            // Check if clipping is needed
            let needs_clipping = child_node
                .area
                .traits
                .overflow
                .map(|o| o.clips_content())
                .unwrap_or(false);

            if needs_clipping {
                ps_page.save_clip_state(
                    abs_x,
                    abs_y,
                    child_node.area.width(),
                    child_node.area.height(),
                );
            }

            // Render background color if present
            if let Some(bg_color) = child_node.area.traits.background_color {
                ps_page.add_background(
                    abs_x,
                    abs_y,
                    child_node.area.width(),
                    child_node.area.height(),
                    bg_color,
                );
            }

            // Render borders if present
            if let (Some(border_widths), Some(border_colors), Some(border_styles)) = (
                child_node.area.traits.border_width,
                child_node.area.traits.border_color,
                child_node.area.traits.border_style,
            ) {
                ps_page.add_borders(
                    abs_x,
                    abs_y,
                    child_node.area.width(),
                    child_node.area.height(),
                    border_widths,
                    border_colors,
                    border_styles,
                );
            }

            match child_node.area.area_type {
                AreaType::Text => {
                    // Check if this is a leader area
                    if let Some(leader_pattern) = &child_node.area.traits.is_leader {
                        render_leader(
                            ps_page,
                            leader_pattern,
                            abs_x,
                            abs_y,
                            child_node.area.width(),
                            child_node.area.height(),
                            &child_node.area.traits,
                        );
                    } else if let Some(text_content) = child_node.area.text_content() {
                        let font_size = child_node
                            .area
                            .traits
                            .font_size
                            .unwrap_or(Length::from_pt(12.0));

                        // Determine bold/italic from traits
                        let bold = child_node
                            .area
                            .traits
                            .font_weight
                            .map(|w| w >= 700)
                            .unwrap_or(false);
                        let italic = child_node
                            .area
                            .traits
                            .font_style
                            .map(|s| {
                                matches!(
                                    s,
                                    fop_layout::area::FontStyle::Italic
                                        | fop_layout::area::FontStyle::Oblique
                                )
                            })
                            .unwrap_or(false);

                        ps_page.add_text_styled(
                            text_content,
                            abs_x,
                            abs_y,
                            font_size,
                            child_node.area.traits.color,
                            bold,
                            italic,
                        );
                    }
                }
                AreaType::Inline => {
                    if let Some(leader_pattern) = &child_node.area.traits.is_leader {
                        render_leader(
                            ps_page,
                            leader_pattern,
                            abs_x,
                            abs_y,
                            child_node.area.width(),
                            child_node.area.height(),
                            &child_node.area.traits,
                        );
                    } else {
                        render_children(area_tree, child_id, ps_page, abs_x, abs_y, image_map)?;
                    }
                }
                AreaType::Viewport => {
                    // Render image if available
                    if let Some(img_data) = image_map.get(&child_id) {
                        ps_page.add_image(
                            img_data,
                            abs_x,
                            abs_y,
                            child_node.area.width(),
                            child_node.area.height(),
                        );
                    }
                    // Still recurse for child areas
                    render_children(area_tree, child_id, ps_page, abs_x, abs_y, image_map)?;
                }
                _ => {
                    // Recursively render other areas
                    render_children(area_tree, child_id, ps_page, abs_x, abs_y, image_map)?;
                }
            }

            if needs_clipping {
                ps_page.restore_clip_state();
            }
        }
    }

    Ok(())
}

/// Render a leader
#[allow(clippy::too_many_arguments)]
fn render_leader(
    ps_page: &mut PsPage,
    leader_pattern: &str,
    x: Length,
    y: Length,
    width: Length,
    height: Length,
    traits: &fop_layout::area::TraitSet,
) {
    match leader_pattern {
        "rule" => {
            let thickness = traits.rule_thickness.unwrap_or(Length::from_pt(0.5));
            let style = traits.rule_style.as_deref().unwrap_or("solid");
            let color = traits.color.unwrap_or(Color::BLACK);

            // Centre the rule vertically within the leader area
            let half_diff = Length::from_millipoints((height - thickness).millipoints() / 2);
            let rule_y = y + half_diff;

            let half_thickness = Length::from_millipoints(thickness.millipoints() / 2);
            ps_page.add_line(
                x,
                rule_y + half_thickness,
                x + width,
                rule_y + half_thickness,
                color,
                thickness,
                style,
            );
        }
        "dots" | "space" => {
            // These are handled by text rendering or produce nothing
        }
        _ => {}
    }
}

/// Escape PostScript special characters in strings
fn escape_ps_string(text: &str) -> String {
    text.replace('\\', "\\\\")
        .replace('(', "\\(")
        .replace(')', "\\)")
        .replace('\r', "\\r")
        .replace('\n', "\\n")
        .replace('\t', "\\t")
}

#[cfg(test)]
mod tests {
    use super::*;
    use fop_layout::area::BorderStyle;
    use fop_types::{Color, Length};

    // ── PsRenderer ───────────────────────────────────────────────────────────

    #[test]
    fn test_ps_renderer_new() {
        let renderer = PsRenderer::new();
        let _ = renderer;
    }

    #[test]
    fn test_ps_renderer_default() {
        let _r1 = PsRenderer::new();
        let _r2 = PsRenderer::default();
    }

    // ── PsDocument ───────────────────────────────────────────────────────────

    #[test]
    fn test_ps_document_empty_has_ps_header() {
        let doc = PsDocument::new();
        let output = doc.to_string();
        assert!(
            output.starts_with("%!PS-Adobe-3.0"),
            "PostScript document must begin with %!PS-Adobe-3.0"
        );
    }

    #[test]
    fn test_ps_document_empty_has_trailer() {
        let doc = PsDocument::new();
        let output = doc.to_string();
        assert!(
            output.contains("%%Trailer"),
            "PS document must have %%Trailer"
        );
        assert!(output.contains("%%EOF"), "PS document must end with %%EOF");
    }

    #[test]
    fn test_ps_document_default_equals_new() {
        let d1 = PsDocument::new().to_string();
        let d2 = PsDocument::default().to_string();
        assert_eq!(d1, d2, "default() and new() must produce identical output");
    }

    #[test]
    fn test_ps_document_empty_page_count() {
        let doc = PsDocument::new();
        let output = doc.to_string();
        assert!(
            output.contains("%%Pages: 0"),
            "empty document must declare 0 pages"
        );
    }

    #[test]
    fn test_ps_document_add_page_increments_count() {
        let mut doc = PsDocument::new();
        doc.add_page(PsPage::new(Length::from_mm(210.0), Length::from_mm(297.0)));
        let output = doc.to_string();
        // %%Pages appears both in header and trailer
        assert!(
            output.contains("%%Pages: 1"),
            "single-page doc must declare 1 page"
        );
    }

    #[test]
    fn test_ps_document_two_pages() {
        let mut doc = PsDocument::new();
        doc.add_page(PsPage::new(Length::from_mm(210.0), Length::from_mm(297.0)));
        doc.add_page(PsPage::new(Length::from_mm(210.0), Length::from_mm(297.0)));
        let output = doc.to_string();
        assert!(
            output.contains("%%Pages: 2"),
            "two-page doc must declare 2 pages"
        );
        assert!(
            output.contains("%%Page: 1 2"),
            "first page label must be '1 2'"
        );
        assert!(
            output.contains("%%Page: 2 2"),
            "second page label must be '2 2'"
        );
    }

    #[test]
    fn test_ps_document_prolog_contains_helpers() {
        let doc = PsDocument::new();
        let output = doc.to_string();
        assert!(
            output.contains("/frect"),
            "prolog must define /frect helper"
        );
        assert!(output.contains("/FN"), "prolog must define /FN font helper");
        assert!(output.contains("/SH"), "prolog must define /SH show helper");
    }

    #[test]
    fn test_ps_document_page_has_showpage() {
        let mut doc = PsDocument::new();
        doc.add_page(PsPage::new(Length::from_mm(210.0), Length::from_mm(297.0)));
        let output = doc.to_string();
        assert!(
            output.contains("showpage"),
            "each page must end with showpage"
        );
    }

    // ── PsPage ───────────────────────────────────────────────────────────────

    fn make_page() -> PsPage {
        PsPage::new(Length::from_mm(210.0), Length::from_mm(297.0))
    }

    #[test]
    fn test_ps_page_new_contains_gsave() {
        let page = make_page();
        let output = page.to_string();
        assert!(output.contains("gsave"), "new page must begin with gsave");
    }

    #[test]
    fn test_ps_page_add_background() {
        let mut page = make_page();
        page.add_background(
            Length::from_pt(10.0),
            Length::from_pt(20.0),
            Length::from_pt(100.0),
            Length::from_pt(50.0),
            Color::RED,
        );
        let output = page.to_string();
        assert!(output.contains("frect"), "background must use frect");
        // RGB values for red (1 0 0)
        assert!(output.contains("RGB"), "background must set RGB color");
    }

    #[test]
    fn test_ps_page_add_text_basic() {
        let mut page = make_page();
        page.add_text(
            "Hello PS",
            Length::from_pt(72.0),
            Length::from_pt(720.0),
            Length::from_pt(12.0),
            None,
        );
        let output = page.to_string();
        assert!(
            output.contains("Hello PS"),
            "text content must appear in output"
        );
        assert!(output.contains("SH"), "text must end with SH (show)");
        assert!(output.contains("FN"), "text must select a font with FN");
    }

    #[test]
    fn test_ps_page_add_text_styled_bold() {
        let mut page = make_page();
        page.add_text_styled(
            "Bold Text",
            Length::from_pt(50.0),
            Length::from_pt(700.0),
            Length::from_pt(14.0),
            Some(Color::BLACK),
            true,
            false,
        );
        let output = page.to_string();
        assert!(
            output.contains("Bold Text"),
            "bold text content must appear"
        );
        assert!(
            output.contains("Helvetica-Bold"),
            "bold must select Helvetica-Bold"
        );
    }

    #[test]
    fn test_ps_page_add_text_styled_italic() {
        let mut page = make_page();
        page.add_text_styled(
            "Italic",
            Length::from_pt(50.0),
            Length::from_pt(700.0),
            Length::from_pt(12.0),
            None,
            false,
            true,
        );
        let output = page.to_string();
        assert!(
            output.contains("Helvetica-Oblique"),
            "italic must select Helvetica-Oblique"
        );
    }

    #[test]
    fn test_ps_page_add_text_styled_bold_italic() {
        let mut page = make_page();
        page.add_text_styled(
            "BI",
            Length::from_pt(10.0),
            Length::from_pt(10.0),
            Length::from_pt(10.0),
            None,
            true,
            true,
        );
        let output = page.to_string();
        assert!(
            output.contains("Helvetica-BoldOblique"),
            "bold+italic must select Helvetica-BoldOblique"
        );
    }

    #[test]
    fn test_ps_page_add_line() {
        let mut page = make_page();
        page.add_line(
            Length::from_pt(0.0),
            Length::from_pt(0.0),
            Length::from_pt(200.0),
            Length::from_pt(0.0),
            Color::BLACK,
            Length::from_pt(1.0),
            "solid",
        );
        let output = page.to_string();
        assert!(output.contains("M"), "line must have moveto (M)");
        assert!(
            output.contains(
                " L
"
            ) && output.contains(
                "
S
"
            ),
            "line must have separate L and S commands"
        );
        assert!(output.contains("LW"), "line must set line width (LW)");
    }

    #[test]
    fn test_ps_page_add_borders_top_only() {
        let mut page = make_page();
        page.add_borders(
            Length::from_pt(10.0),
            Length::from_pt(10.0),
            Length::from_pt(100.0),
            Length::from_pt(50.0),
            [
                Length::from_pt(2.0), // top
                Length::ZERO,         // right
                Length::ZERO,         // bottom
                Length::ZERO,         // left
            ],
            [Color::BLACK; 4],
            [BorderStyle::Solid; 4],
        );
        let output = page.to_string();
        assert!(
            output.contains("frect"),
            "borders must use frect for filled rectangles"
        );
    }

    #[test]
    fn test_ps_page_add_borders_none_style_skipped() {
        let mut page = make_page();
        let initial_len = page.commands.len();
        page.add_borders(
            Length::from_pt(0.0),
            Length::from_pt(0.0),
            Length::from_pt(100.0),
            Length::from_pt(50.0),
            [Length::from_pt(1.0); 4],
            [Color::BLACK; 4],
            [BorderStyle::None; 4], // all none — should not draw anything
        );
        // No new frect commands should have been added
        assert_eq!(
            page.commands.len(),
            initial_len,
            "BorderStyle::None must not produce drawing commands"
        );
    }

    #[test]
    fn test_ps_page_save_restore_clip() {
        let mut page = make_page();
        page.save_clip_state(
            Length::from_pt(10.0),
            Length::from_pt(10.0),
            Length::from_pt(80.0),
            Length::from_pt(60.0),
        );
        page.restore_clip_state();
        let output = page.to_string();
        // save_clip_state uses gsave + clip; restore uses grestore
        assert!(
            output.contains("gsave"),
            "clip state save must include gsave"
        );
        assert!(
            output.contains("grestore"),
            "clip state restore must include grestore"
        );
    }

    // ── select_ps_font helper (tested indirectly via add_text_styled) ─────────

    #[test]
    fn test_ps_font_selection_times() {
        let mut page = make_page();
        // Directly call the public add_text_styled with Times via a modified
        // wrapper isn't possible (select_ps_font is private), but we can
        // verify the Helvetica family path is exhaustive via the styled API.
        page.add_text_styled(
            "plain",
            Length::from_pt(10.0),
            Length::from_pt(10.0),
            Length::from_pt(10.0),
            None,
            false,
            false,
        );
        let output = page.to_string();
        assert!(
            output.contains("Helvetica"),
            "plain text must use Helvetica"
        );
    }

    // ── PS output structural checks ───────────────────────────────────────────

    #[test]
    fn test_ps_document_has_bounding_box_with_pages() {
        let mut doc = PsDocument::new();
        doc.add_page(PsPage::new(Length::from_mm(210.0), Length::from_mm(297.0)));
        let output = doc.to_string();
        assert!(
            output.contains("%%BoundingBox:"),
            "document with pages must have %%BoundingBox"
        );
    }

    #[test]
    fn test_ps_document_language_level_2() {
        let doc = PsDocument::new();
        let output = doc.to_string();
        assert!(
            output.contains("%%LanguageLevel: 2"),
            "must declare PostScript language level 2"
        );
    }

    #[test]
    fn test_ps_document_creator_comment() {
        let doc = PsDocument::new();
        let output = doc.to_string();
        assert!(
            output.contains("%%Creator: Apache FOP Rust"),
            "must include Creator DSC comment"
        );
    }

    #[test]
    fn test_ps_page_output_ends_with_grestore() {
        let page = make_page();
        let output = page.to_string();
        // The page build_ps method ends with grestore
        assert!(
            output.trim_end().ends_with("grestore"),
            "page PS must end with grestore"
        );
    }
}