Skip to main content

strata_layout/
exclusions.rs

1//! W3C CSS Floats & Exclusion Space engine (Blink LayoutNG parity).
2//!
3//! Tracks floating boxes in a Block Formatting Context (BFC) and solves
4//! line opportunities for Inline Formatting Context (IFC) line boxes.
5
6use crate::geometry::Rect;
7use strata_style::types::Clear;
8
9/// Directional orientation of a spatial layout exclusion.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum ExclusionKind {
12    FloatLeft,
13    FloatRight,
14}
15
16/// An individual rectangular obstacle registered in the formatting context.
17#[derive(Debug, Clone, PartialEq)]
18pub struct ExclusionRect {
19    pub bounds: Rect,
20    pub kind: ExclusionKind,
21}
22
23/// Usable inline opportunity for a line box at a given vertical position.
24#[derive(Debug, Clone, Copy, PartialEq)]
25pub struct LineOpportunity {
26    /// Horizontal start offset relative to content container origin.
27    pub start_x: f32,
28    /// Width available for inline phrasing content without colliding with floats.
29    pub available_width: f32,
30    /// Absolute right edge of the leftmost float obstruction (or 0.0).
31    pub left_float_edge: f32,
32    /// Absolute left edge of the rightmost float obstruction (or container width).
33    pub right_float_edge: f32,
34}
35
36/// Space manager tracking active float exclusions within a Block Formatting Context.
37#[derive(Debug, Clone, Default, PartialEq)]
38pub struct ExclusionSpace {
39    exclusions: Vec<ExclusionRect>,
40}
41
42impl ExclusionSpace {
43    /// Constructs an empty exclusion space.
44    pub fn new() -> Self {
45        Self {
46            exclusions: Vec::new(),
47        }
48    }
49
50    /// Adds a float exclusion rectangle in container coordinates.
51    pub fn add_exclusion(&mut self, bounds: Rect, kind: ExclusionKind) {
52        self.exclusions.push(ExclusionRect { bounds, kind });
53    }
54
55    /// Returns whether any exclusions are active.
56    pub fn is_empty(&self) -> bool {
57        self.exclusions.is_empty()
58    }
59
60    /// Returns a slice of all active exclusions.
61    pub fn exclusions(&self) -> &[ExclusionRect] {
62        &self.exclusions
63    }
64
65    /// Computes the usable horizontal line opportunity for a line box spanning `[y, y + height]`.
66    pub fn line_opportunity(&self, y: f32, height: f32, container_width: f32) -> LineOpportunity {
67        if self.exclusions.is_empty() {
68            return LineOpportunity {
69                start_x: 0.0,
70                available_width: container_width.max(0.0),
71                left_float_edge: 0.0,
72                right_float_edge: container_width,
73            };
74        }
75
76        let line_top = y;
77        let line_bottom = y + height.max(0.001);
78
79        let mut left_edge: f32 = 0.0;
80        let mut right_edge: f32 = container_width;
81
82        for e in &self.exclusions {
83            let float_top = e.bounds.y;
84            let float_bottom = e.bounds.y + e.bounds.height;
85
86            // Check if vertical intervals intersect
87            if float_top < line_bottom && float_bottom > line_top {
88                match e.kind {
89                    ExclusionKind::FloatLeft => {
90                        let r = e.bounds.x + e.bounds.width;
91                        if r > left_edge {
92                            left_edge = r;
93                        }
94                    }
95                    ExclusionKind::FloatRight => {
96                        let l = e.bounds.x;
97                        if l < right_edge {
98                            right_edge = l;
99                        }
100                    }
101                }
102            }
103        }
104
105        let avail = if right_edge > left_edge {
106            right_edge - left_edge
107        } else {
108            0.0
109        };
110
111        LineOpportunity {
112            start_x: left_edge,
113            available_width: avail,
114            left_float_edge: left_edge,
115            right_float_edge: right_edge,
116        }
117    }
118
119    /// Calculates clearance offset advancing past the bottom edge of relevant floats.
120    pub fn clearance_offset(&self, clear: Clear, current_y: f32) -> f32 {
121        if self.exclusions.is_empty() || clear == Clear::None {
122            return current_y;
123        }
124
125        let mut cleared_y = current_y;
126
127        for e in &self.exclusions {
128            let matches = match clear {
129                Clear::None => false,
130                Clear::Left | Clear::InlineStart => e.kind == ExclusionKind::FloatLeft,
131                Clear::Right | Clear::InlineEnd => e.kind == ExclusionKind::FloatRight,
132                Clear::Both => true,
133            };
134
135            if matches {
136                let bottom = e.bounds.y + e.bounds.height;
137                if bottom > cleared_y {
138                    cleared_y = bottom;
139                }
140            }
141        }
142
143        cleared_y
144    }
145
146    /// Returns the maximum bottom coordinate among all registered floats.
147    pub fn max_float_bottom(&self) -> f32 {
148        self.exclusions
149            .iter()
150            .map(|e| e.bounds.y + e.bounds.height)
151            .fold(0.0, f32::max)
152    }
153
154    /// Finds the nearest bottom edge of an active float that occurs strictly after `y`.
155    pub fn next_float_bottom_after(&self, y: f32) -> Option<f32> {
156        let mut best: Option<f32> = None;
157        for e in &self.exclusions {
158            let bottom = e.bounds.y + e.bounds.height;
159            if bottom > y + 0.01 {
160                match best {
161                    Some(b) => {
162                        if bottom < b {
163                            best = Some(bottom);
164                        }
165                    }
166                    None => best = Some(bottom),
167                }
168            }
169        }
170        best
171    }
172
173    /// Translates all exclusion boundaries by a spatial delta `(dx, dy)`.
174    pub fn translate(&mut self, dx: f32, dy: f32) {
175        for e in &mut self.exclusions {
176            e.bounds.x += dx;
177            e.bounds.y += dy;
178        }
179    }
180}