strata_layout/
exclusions.rs1use crate::geometry::Rect;
7use strata_style::types::Clear;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum ExclusionKind {
12 FloatLeft,
13 FloatRight,
14}
15
16#[derive(Debug, Clone, PartialEq)]
18pub struct ExclusionRect {
19 pub bounds: Rect,
20 pub kind: ExclusionKind,
21}
22
23#[derive(Debug, Clone, Copy, PartialEq)]
25pub struct LineOpportunity {
26 pub start_x: f32,
28 pub available_width: f32,
30 pub left_float_edge: f32,
32 pub right_float_edge: f32,
34}
35
36#[derive(Debug, Clone, Default, PartialEq)]
38pub struct ExclusionSpace {
39 exclusions: Vec<ExclusionRect>,
40}
41
42impl ExclusionSpace {
43 pub fn new() -> Self {
45 Self {
46 exclusions: Vec::new(),
47 }
48 }
49
50 pub fn add_exclusion(&mut self, bounds: Rect, kind: ExclusionKind) {
52 self.exclusions.push(ExclusionRect { bounds, kind });
53 }
54
55 pub fn is_empty(&self) -> bool {
57 self.exclusions.is_empty()
58 }
59
60 pub fn exclusions(&self) -> &[ExclusionRect] {
62 &self.exclusions
63 }
64
65 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 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 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 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 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 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}