fop-layout 0.1.1

Layout engine for Apache FOP Rust implementation
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
//! Area types and traits
//!
//! Defines the different types of areas and their associated traits.

use crate::layout::{properties::OverflowBehavior, TextAlign};
use fop_types::{Color, Length, Rect};

/// Content stored in an area
#[derive(Debug, Clone)]
pub enum AreaContent {
    /// Text content
    Text(String),

    /// Binary image data (raw bytes)
    ImageData(Vec<u8>),
}

/// An area represents a rectangular region on a page
#[derive(Debug, Clone)]
pub struct Area {
    /// Area type
    pub area_type: AreaType,

    /// Position and size
    pub geometry: Rect,

    /// Rendering traits (color, background, borders, etc.)
    pub traits: TraitSet,

    /// Content (text or image data)
    pub content: Option<AreaContent>,

    /// Keep constraints for page breaking
    pub keep_constraint: Option<crate::layout::KeepConstraint>,

    /// Break-before property value
    pub break_before: Option<crate::layout::BreakValue>,

    /// Break-after property value
    pub break_after: Option<crate::layout::BreakValue>,

    /// Widows constraint - minimum lines at top of page after break
    pub widows: i32,

    /// Orphans constraint - minimum lines at bottom of page before break
    pub orphans: i32,
}

impl Area {
    /// Create a new area
    pub fn new(area_type: AreaType, geometry: Rect) -> Self {
        Self {
            area_type,
            geometry,
            traits: TraitSet::default(),
            content: None,
            keep_constraint: None,
            break_before: None,
            break_after: None,
            widows: 2,
            orphans: 2,
        }
    }

    /// Create a text area
    pub fn text(geometry: Rect, content: String) -> Self {
        Self {
            area_type: AreaType::Text,
            geometry,
            traits: TraitSet::default(),
            content: Some(AreaContent::Text(content)),
            keep_constraint: None,
            break_before: None,
            break_after: None,
            widows: 2,
            orphans: 2,
        }
    }

    /// Create a viewport area for an image
    pub fn viewport_with_image(geometry: Rect, image_data: Vec<u8>) -> Self {
        Self {
            area_type: AreaType::Viewport,
            geometry,
            traits: TraitSet::default(),
            content: Some(AreaContent::ImageData(image_data)),
            keep_constraint: None,
            break_before: None,
            break_after: None,
            widows: 2,
            orphans: 2,
        }
    }

    /// Set the area's traits
    pub fn with_traits(mut self, traits: TraitSet) -> Self {
        self.traits = traits;
        self
    }

    /// Set the area's keep constraint
    pub fn with_keep_constraint(mut self, keep_constraint: crate::layout::KeepConstraint) -> Self {
        self.keep_constraint = Some(keep_constraint);
        self
    }

    /// Set the area's break-before value
    pub fn with_break_before(mut self, break_before: crate::layout::BreakValue) -> Self {
        self.break_before = Some(break_before);
        self
    }

    /// Set the area's break-after value
    pub fn with_break_after(mut self, break_after: crate::layout::BreakValue) -> Self {
        self.break_after = Some(break_after);
        self
    }

    /// Set the area's widows constraint
    pub fn with_widows(mut self, widows: i32) -> Self {
        self.widows = widows;
        self
    }

    /// Set the area's orphans constraint
    pub fn with_orphans(mut self, orphans: i32) -> Self {
        self.orphans = orphans;
        self
    }

    /// Check if this area contains text
    pub fn has_text(&self) -> bool {
        matches!(self.content, Some(AreaContent::Text(_)))
    }

    /// Check if this area contains image data
    pub fn has_image_data(&self) -> bool {
        matches!(self.content, Some(AreaContent::ImageData(_)))
    }

    /// Get text content if this is a text area
    pub fn text_content(&self) -> Option<&str> {
        match &self.content {
            Some(AreaContent::Text(s)) => Some(s),
            _ => None,
        }
    }

    /// Get image data if this is an image area
    pub fn image_data(&self) -> Option<&[u8]> {
        match &self.content {
            Some(AreaContent::ImageData(data)) => Some(data),
            _ => None,
        }
    }

    /// Get the area's width
    pub fn width(&self) -> Length {
        self.geometry.width
    }

    /// Get the area's height
    pub fn height(&self) -> Length {
        self.geometry.height
    }
}

/// Types of areas in the area tree
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AreaType {
    /// Page area - represents a physical page
    Page,

    /// Region area - represents a page region (body, before, after, start, end)
    Region,

    /// Header area - static content at top of page
    Header,

    /// Footer area - static content at bottom of page
    Footer,

    /// Block area - block-level formatting context
    Block,

    /// Line area - contains inline areas
    Line,

    /// Inline area - inline-level content
    Inline,

    /// Text area - actual text content
    Text,

    /// Space area - whitespace
    Space,

    /// Viewport area - for images, SVG, etc.
    Viewport,

    /// Footnote area - footnote content
    Footnote,

    /// Footnote separator area - line above footnotes
    FootnoteSeparator,

    /// Column area - represents a column in multi-column layout
    Column,

    /// Float area - floating element (like CSS floats)
    FloatArea,

    /// Sidebar start area - static content on the start (left) side of the page
    SidebarStart,

    /// Sidebar end area - static content on the end (right) side of the page
    SidebarEnd,
}

/// Rendering traits for an area
///
/// These are the properties that affect rendering (color, background, borders, etc.)
#[derive(Debug, Clone, Default)]
pub struct TraitSet {
    /// Text color
    pub color: Option<Color>,

    /// Background color
    pub background_color: Option<Color>,

    /// Font family
    pub font_family: Option<String>,

    /// Font size
    pub font_size: Option<Length>,

    /// Font weight (100-900, 400 = normal, 700 = bold)
    pub font_weight: Option<u16>,

    /// Font style (normal, italic, oblique)
    pub font_style: Option<FontStyle>,

    /// Text decoration
    pub text_decoration: Option<TextDecoration>,

    /// Border widths (top, right, bottom, left)
    pub border_width: Option<[Length; 4]>,

    /// Border colors (top, right, bottom, left)
    pub border_color: Option<[Color; 4]>,

    /// Border styles (top, right, bottom, left)
    pub border_style: Option<[BorderStyle; 4]>,

    /// Padding (top, right, bottom, left)
    pub padding: Option<[Length; 4]>,

    /// Text alignment
    pub text_align: Option<TextAlign>,

    /// Link destination (for hyperlinks)
    pub link_destination: Option<String>,

    /// Leader pattern (dots, rule, space, use-content)
    pub is_leader: Option<String>,

    /// Rule thickness (for rule leaders)
    pub rule_thickness: Option<Length>,

    /// Rule style (for rule leaders: solid, dashed, dotted)
    pub rule_style: Option<String>,

    /// Line height
    pub line_height: Option<Length>,

    /// Letter spacing (extra space between characters)
    pub letter_spacing: Option<Length>,

    /// Word spacing (extra space between words)
    pub word_spacing: Option<Length>,

    /// Border radius for rounded corners (top-left, top-right, bottom-right, bottom-left)
    /// Each corner can have independent radius values
    pub border_radius: Option<[Length; 4]>,

    /// Overflow behavior - controls clipping of content
    pub overflow: Option<OverflowBehavior>,

    /// Opacity - transparency level (0.0 = transparent, 1.0 = opaque)
    pub opacity: Option<f64>,

    /// Text transformation
    pub text_transform: Option<TextTransform>,

    /// Font variant
    pub font_variant: Option<FontVariant>,

    /// Display alignment (vertical alignment)
    pub display_align: Option<DisplayAlign>,

    /// Baseline shift for inline positioning (positive = up, negative = down, as fraction of font-size)
    pub baseline_shift: Option<f64>,

    /// Whether hyphenation is enabled
    pub hyphenate: Option<bool>,

    /// Minimum word length before hyphenation (hyphenation-minimum-word-count)
    pub hyphenation_min_word_chars: Option<u32>,

    /// Characters before hyphen (hyphenation-push-character-count)
    pub hyphenation_push_chars: Option<u32>,

    /// Characters after hyphen (hyphenation-remain-character-count)
    pub hyphenation_remain_chars: Option<u32>,

    /// Font stretch (condensed/expanded)
    pub font_stretch: Option<FontStretch>,

    /// Text alignment for last line (used with justify)
    pub text_align_last: Option<TextAlign>,

    /// Change bar color (for margin rule rendering)
    pub change_bar_color: Option<fop_types::Color>,

    /// Span property (none or all columns)
    pub span: Span,

    /// Role attribute for accessibility tagging (PDF/UA)
    pub role: Option<String>,

    /// Language attribute (xml:lang) for language tagging
    pub xml_lang: Option<String>,

    /// Writing mode (lr-tb, rl-tb, tb-rl, tb-lr)
    pub writing_mode: WritingMode,

    /// Text direction (ltr, rtl)
    pub direction: Direction,
}

/// Writing mode values for XSL-FO
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WritingMode {
    /// Left-to-right, top-to-bottom (default Western text)
    #[default]
    LrTb,
    /// Right-to-left, top-to-bottom (Arabic/Hebrew)
    RlTb,
    /// Top-to-bottom, right-to-left (Traditional CJK vertical)
    TbRl,
    /// Top-to-bottom, left-to-right (Modern CJK vertical)
    TbLr,
}

/// Text direction values
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Direction {
    /// Left-to-right (default)
    #[default]
    Ltr,
    /// Right-to-left (Arabic/Hebrew)
    Rtl,
}

/// Span values for fo:block column spanning
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Span {
    /// Block stays in current column (default)
    #[default]
    None,
    /// Block spans all columns
    All,
}

/// Font style values
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FontStyle {
    Normal,
    Italic,
    Oblique,
}

/// Border style values (XSL-FO and CSS compatible)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BorderStyle {
    /// No border
    None,
    /// Solid line
    Solid,
    /// Dashed line
    Dashed,
    /// Dotted line
    Dotted,
    /// Double line
    Double,
    /// 3D grooved border
    Groove,
    /// 3D ridged border
    Ridge,
    /// 3D inset border
    Inset,
    /// 3D outset border
    Outset,
    /// Hidden border (same as none, but for border conflict resolution)
    Hidden,
}

/// Text decoration values
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextDecoration {
    pub underline: bool,
    pub overline: bool,
    pub line_through: bool,
}

impl TextDecoration {
    pub const NONE: Self = Self {
        underline: false,
        overline: false,
        line_through: false,
    };

    pub const UNDERLINE: Self = Self {
        underline: true,
        overline: false,
        line_through: false,
    };
}

/// Text transformation mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TextTransform {
    #[default]
    None,
    Uppercase,
    Lowercase,
    Capitalize,
}

/// Font variant
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FontVariant {
    #[default]
    Normal,
    SmallCaps,
}

/// Font stretch values
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FontStretch {
    UltraCondensed,
    ExtraCondensed,
    Condensed,
    SemiCondensed,
    #[default]
    Normal,
    SemiExpanded,
    Expanded,
    ExtraExpanded,
    UltraExpanded,
}

/// Display alignment (vertical alignment within a block area)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DisplayAlign {
    #[default]
    Before,
    Center,
    After,
}

#[cfg(test)]
mod tests {
    use super::*;
    use fop_types::{Length, Point, Size};

    #[test]
    fn test_area_creation() {
        let rect = Rect::from_point_size(
            Point::new(Length::from_pt(10.0), Length::from_pt(20.0)),
            Size::new(Length::from_pt(100.0), Length::from_pt(50.0)),
        );

        let area = Area::new(AreaType::Block, rect);

        assert_eq!(area.area_type, AreaType::Block);
        assert_eq!(area.width(), Length::from_pt(100.0));
        assert_eq!(area.height(), Length::from_pt(50.0));
        assert!(!area.has_text());
    }

    #[test]
    fn test_text_area() {
        let rect = Rect::from_point_size(
            Point::ZERO,
            Size::new(Length::from_pt(50.0), Length::from_pt(12.0)),
        );

        let area = Area::text(rect, "Hello".to_string());

        assert_eq!(area.area_type, AreaType::Text);
        assert!(area.has_text());
        assert_eq!(area.text_content().expect("test: should succeed"), "Hello");
    }

    #[test]
    fn test_traits() {
        let traits = TraitSet {
            color: Some(Color::RED),
            font_size: Some(Length::from_pt(12.0)),
            ..Default::default()
        };

        assert_eq!(traits.color, Some(Color::RED));
        assert_eq!(traits.font_size, Some(Length::from_pt(12.0)));
    }
}

#[cfg(test)]
mod extended_tests {
    use super::*;
    use fop_types::{Length, Point, Rect, Size};

    // ---- Area builder method tests ----

    #[test]
    fn test_area_with_traits() {
        let rect = Rect::from_point_size(
            Point::ZERO,
            Size::new(Length::from_pt(100.0), Length::from_pt(50.0)),
        );
        let traits = TraitSet {
            font_size: Some(Length::from_pt(14.0)),
            ..Default::default()
        };
        let area = Area::new(AreaType::Block, rect).with_traits(traits);
        assert_eq!(area.traits.font_size, Some(Length::from_pt(14.0)));
    }

    #[test]
    fn test_area_widows_orphans_defaults() {
        let rect = Rect::from_point_size(
            Point::ZERO,
            Size::new(Length::from_pt(100.0), Length::from_pt(50.0)),
        );
        let area = Area::new(AreaType::Block, rect);
        assert_eq!(area.widows, 2);
        assert_eq!(area.orphans, 2);
    }

    #[test]
    fn test_area_with_widows_and_orphans() {
        let rect = Rect::from_point_size(
            Point::ZERO,
            Size::new(Length::from_pt(100.0), Length::from_pt(50.0)),
        );
        let area = Area::new(AreaType::Block, rect)
            .with_widows(4)
            .with_orphans(3);
        assert_eq!(area.widows, 4);
        assert_eq!(area.orphans, 3);
    }

    #[test]
    fn test_area_break_before_none_by_default() {
        let rect = Rect::from_point_size(
            Point::ZERO,
            Size::new(Length::from_pt(100.0), Length::from_pt(50.0)),
        );
        let area = Area::new(AreaType::Block, rect);
        assert!(area.break_before.is_none());
        assert!(area.break_after.is_none());
    }

    #[test]
    fn test_area_with_break_before() {
        use crate::layout::BreakValue;
        let rect = Rect::from_point_size(
            Point::ZERO,
            Size::new(Length::from_pt(100.0), Length::from_pt(50.0)),
        );
        let area = Area::new(AreaType::Block, rect).with_break_before(BreakValue::Page);
        assert!(area.break_before.is_some());
    }

    #[test]
    fn test_area_with_break_after() {
        use crate::layout::BreakValue;
        let rect = Rect::from_point_size(
            Point::ZERO,
            Size::new(Length::from_pt(100.0), Length::from_pt(50.0)),
        );
        let area = Area::new(AreaType::Block, rect).with_break_after(BreakValue::Page);
        assert!(area.break_after.is_some());
    }

    #[test]
    fn test_area_viewport_with_image() {
        let rect = Rect::from_point_size(
            Point::ZERO,
            Size::new(Length::from_pt(50.0), Length::from_pt(50.0)),
        );
        let image_data = vec![0u8, 1, 2, 3, 4];
        let area = Area::viewport_with_image(rect, image_data.clone());
        assert_eq!(area.area_type, AreaType::Viewport);
        assert!(area.has_image_data());
        assert_eq!(
            area.image_data().expect("test: should succeed"),
            image_data.as_slice()
        );
    }

    #[test]
    fn test_area_text_content_none_for_non_text() {
        let rect = Rect::from_point_size(
            Point::ZERO,
            Size::new(Length::from_pt(100.0), Length::from_pt(50.0)),
        );
        let area = Area::new(AreaType::Block, rect);
        assert!(area.text_content().is_none());
        assert!(!area.has_text());
        assert!(!area.has_image_data());
    }

    #[test]
    fn test_area_image_data_none_for_text_area() {
        let rect = Rect::from_point_size(
            Point::ZERO,
            Size::new(Length::from_pt(50.0), Length::from_pt(12.0)),
        );
        let area = Area::text(rect, "Test".to_string());
        assert!(area.image_data().is_none());
    }

    // ---- TraitSet field tests ----

    #[test]
    fn test_traitset_default_all_none() {
        let traits = TraitSet::default();
        assert!(traits.color.is_none());
        assert!(traits.background_color.is_none());
        assert!(traits.font_family.is_none());
        assert!(traits.font_size.is_none());
        assert!(traits.font_weight.is_none());
        assert!(traits.font_style.is_none());
        assert!(traits.text_decoration.is_none());
        assert!(traits.border_width.is_none());
        assert!(traits.padding.is_none());
        assert!(traits.text_align.is_none());
        assert!(traits.line_height.is_none());
        assert!(traits.letter_spacing.is_none());
        assert!(traits.word_spacing.is_none());
    }

    #[test]
    fn test_traitset_writing_mode_default_lr_tb() {
        let traits = TraitSet::default();
        assert_eq!(traits.writing_mode, WritingMode::LrTb);
    }

    #[test]
    fn test_traitset_direction_default_ltr() {
        let traits = TraitSet::default();
        assert_eq!(traits.direction, Direction::Ltr);
    }

    #[test]
    fn test_traitset_span_default_none() {
        let traits = TraitSet::default();
        assert_eq!(traits.span, Span::None);
    }

    #[test]
    fn test_traitset_display_align_default_before() {
        let traits = TraitSet::default();
        // display_align is Option<DisplayAlign>, defaults to None
        assert_eq!(traits.display_align, None);
    }

    #[test]
    fn test_traitset_font_variant_default_normal() {
        let traits = TraitSet::default();
        // font_variant is Option<FontVariant>, defaults to None
        assert_eq!(traits.font_variant, None);
    }

    #[test]
    fn test_traitset_text_transform_default_none() {
        let traits = TraitSet::default();
        // text_transform is Option<TextTransform>, defaults to None
        assert_eq!(traits.text_transform, None);
    }

    #[test]
    fn test_traitset_font_stretch_default_normal() {
        let traits = TraitSet::default();
        // font_stretch is Option<FontStretch>, defaults to None
        assert_eq!(traits.font_stretch, None);
    }

    // ---- AreaType coverage tests ----

    #[test]
    fn test_area_types_are_distinct() {
        assert_ne!(AreaType::Page, AreaType::Region);
        assert_ne!(AreaType::Block, AreaType::Line);
        assert_ne!(AreaType::Inline, AreaType::Text);
        assert_ne!(AreaType::Header, AreaType::Footer);
        assert_ne!(AreaType::Column, AreaType::Footnote);
    }

    #[test]
    fn test_area_width_and_height() {
        let rect = Rect::from_point_size(
            Point::new(Length::from_pt(5.0), Length::from_pt(10.0)),
            Size::new(Length::from_pt(200.0), Length::from_pt(100.0)),
        );
        let area = Area::new(AreaType::Page, rect);
        assert_eq!(area.width(), Length::from_pt(200.0));
        assert_eq!(area.height(), Length::from_pt(100.0));
    }

    // ---- TextDecoration tests ----

    #[test]
    fn test_text_decoration_none() {
        let td = TextDecoration::NONE;
        assert!(!td.underline);
        assert!(!td.overline);
        assert!(!td.line_through);
    }

    #[test]
    fn test_text_decoration_underline() {
        let td = TextDecoration::UNDERLINE;
        assert!(td.underline);
        assert!(!td.overline);
        assert!(!td.line_through);
    }

    #[test]
    fn test_text_decoration_custom() {
        let td = TextDecoration {
            underline: false,
            overline: true,
            line_through: true,
        };
        assert!(!td.underline);
        assert!(td.overline);
        assert!(td.line_through);
    }

    // ---- FontStyle / BorderStyle enum tests ----

    #[test]
    fn test_font_style_variants() {
        assert_ne!(FontStyle::Normal, FontStyle::Italic);
        assert_ne!(FontStyle::Italic, FontStyle::Oblique);
        assert_ne!(FontStyle::Normal, FontStyle::Oblique);
    }

    #[test]
    fn test_border_style_variants() {
        assert_ne!(BorderStyle::None, BorderStyle::Solid);
        assert_ne!(BorderStyle::Dashed, BorderStyle::Dotted);
        assert_ne!(BorderStyle::Hidden, BorderStyle::None);
    }
}