strata-layout 0.1.2

Chromium-parity Box Model, Flexbox (L1), and CSS Grid (L2) layout engine in pure Rust
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
//! Geometry structures for CSS Box Model and subpixel layout snapping.

use strata_primitives::geom::{EdgeInsets, Point as PrimPoint, Rect as PrimRect, Size as PrimSize};
use strata_primitives::LayoutUnit;

/// 2D Cartesian Coordinate Point.
pub type Point = PrimPoint<f32>;

/// 2D Size Dimensions.
pub type Size = PrimSize<f32>;

/// 2D Rectangle in floating point pixels.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Rect {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl Rect {
    #[inline(always)]
    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    #[inline(always)]
    pub const fn from_xywh(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    #[inline(always)]
    pub const fn zero() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            width: 0.0,
            height: 0.0,
        }
    }

    #[inline(always)]
    pub fn min_x(self) -> f32 {
        self.x
    }
    #[inline(always)]
    pub fn max_x(self) -> f32 {
        self.x + self.width
    }
    #[inline(always)]
    pub fn min_y(self) -> f32 {
        self.y
    }
    #[inline(always)]
    pub fn max_y(self) -> f32 {
        self.y + self.height
    }

    /// Computes intersection with another rectangle if overlapping.
    #[inline]
    pub fn intersect(self, other: Self) -> Option<Self> {
        let x0 = self.min_x().max(other.min_x());
        let y0 = self.min_y().max(other.min_y());
        let x1 = self.max_x().min(other.max_x());
        let y1 = self.max_y().min(other.max_y());

        if x1 > x0 && y1 > y0 {
            Some(Self::new(x0, y0, x1 - x0, y1 - y0))
        } else {
            None
        }
    }

    /// Returns true if this rectangle contains the given point (px, py).
    #[inline]
    pub fn contains_point(self, px: f32, py: f32) -> bool {
        px >= self.min_x() && px <= self.max_x() && py >= self.min_y() && py <= self.max_y()
    }

    /// Returns true if this rectangle completely encloses `other`.
    #[inline]
    pub fn contains_rect(self, other: Self) -> bool {
        if other.width <= 0.0 || other.height <= 0.0 {
            return false;
        }
        other.min_x() >= self.min_x()
            && other.max_x() <= self.max_x()
            && other.min_y() >= self.min_y()
            && other.max_y() <= self.max_y()
    }

    /// Chromium subpixel layout boundary snapping via `LayoutUnit` (1/64th px fixed point).
    ///
    /// Snaps bounding edges against global screen space, deriving width and height
    /// as `snapped_max - snapped_min`. This guarantees adjacent sibling boxes and
    /// deeply nested descendants snap seamlessly with zero hairline cracks or rounding drift.
    pub fn snap_to_pixel(self) -> Self {
        let x = if self.x.is_finite() {
            self.x.clamp(-1e7, 1e7)
        } else {
            0.0
        };
        let y = if self.y.is_finite() {
            self.y.clamp(-1e7, 1e7)
        } else {
            0.0
        };
        let w = if self.width.is_finite() {
            self.width.clamp(0.0, 1e7)
        } else {
            0.0
        };
        let h = if self.height.is_finite() {
            self.height.clamp(0.0, 1e7)
        } else {
            0.0
        };

        let snapped_x = LayoutUnit::from_f32(x).to_f32();
        let snapped_y = LayoutUnit::from_f32(y).to_f32();
        let snapped_max_x = LayoutUnit::from_f32(x + w).to_f32();
        let snapped_max_y = LayoutUnit::from_f32(y + h).to_f32();

        Self {
            x: snapped_x,
            y: snapped_y,
            width: (snapped_max_x - snapped_x).max(0.0),
            height: (snapped_max_y - snapped_y).max(0.0),
        }
    }

    /// Snaps bounding edges against a device scale factor (e.g. 1.0 for 1x, 2.0 for Retina).
    pub fn snap_to_scale(self, scale: f32) -> Self {
        let s = if scale > 0.0 && scale.is_finite() {
            scale
        } else {
            1.0
        };
        let x = if self.x.is_finite() {
            self.x.clamp(-1e7, 1e7)
        } else {
            0.0
        };
        let y = if self.y.is_finite() {
            self.y.clamp(-1e7, 1e7)
        } else {
            0.0
        };
        let w = if self.width.is_finite() {
            self.width.clamp(0.0, 1e7)
        } else {
            0.0
        };
        let h = if self.height.is_finite() {
            self.height.clamp(0.0, 1e7)
        } else {
            0.0
        };

        let snapped_x = (x * s).round() / s;
        let snapped_y = (y * s).round() / s;
        let snapped_max_x = ((x + w) * s).round() / s;
        let snapped_max_y = ((y + h) * s).round() / s;

        Self {
            x: snapped_x,
            y: snapped_y,
            width: (snapped_max_x - snapped_x).max(0.0),
            height: (snapped_max_y - snapped_y).max(0.0),
        }
    }

    /// Returns true if all components of the rectangle are finite numbers.
    #[inline(always)]
    pub fn is_finite(&self) -> bool {
        self.x.is_finite()
            && self.y.is_finite()
            && self.width.is_finite()
            && self.height.is_finite()
    }

    /// Sanitizes all coordinates ensuring they are finite and clamped to valid layout limits.
    pub fn sanitize(&mut self) {
        if !self.x.is_finite() {
            self.x = 0.0;
        } else {
            self.x = self.x.clamp(-1e7, 1e7);
        }
        if !self.y.is_finite() {
            self.y = 0.0;
        } else {
            self.y = self.y.clamp(-1e7, 1e7);
        }
        if !self.width.is_finite() {
            self.width = 0.0;
        } else {
            self.width = self.width.clamp(0.0, 1e7);
        }
        if !self.height.is_finite() {
            self.height = 0.0;
        } else {
            self.height = self.height.clamp(0.0, 1e7);
        }
    }
}

impl From<PrimRect<f32>> for Rect {
    fn from(r: PrimRect<f32>) -> Self {
        Self {
            x: r.origin.x,
            y: r.origin.y,
            width: r.size.width,
            height: r.size.height,
        }
    }
}

impl From<Rect> for PrimRect<f32> {
    fn from(r: Rect) -> Self {
        PrimRect::from_xywh(r.x, r.y, r.width, r.height)
    }
}

/// The concentric boxes and scroll metrics forming the CSS Box Model for an element.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct LayoutGeometry {
    /// Content box (where inner text and child elements reside).
    pub content_box: Rect,
    /// Padding box (content box expanded by padding insets).
    pub padding_box: Rect,
    /// Border box (padding box expanded by border widths).
    /// This is the primary rectangle used for element rendering and layout bounds.
    pub border_box: Rect,
    /// Margin box (border box expanded by margin insets).
    pub margin_box: Rect,
    /// Total scrollable content extent (width, height) enclosing all overflowing children.
    pub scroll_bounds: Size,
    /// Current scroll offset (scroll_x, scroll_y).
    pub scroll_offset: Point,
    /// Whether element has scrollable overflow along (horizontal, vertical) axes.
    pub has_scroll: (bool, bool),
}

impl LayoutGeometry {
    /// Returns true if all concentric boxes contain strictly finite coordinates.
    #[inline(always)]
    pub fn is_finite(&self) -> bool {
        self.content_box.is_finite()
            && self.padding_box.is_finite()
            && self.border_box.is_finite()
            && self.margin_box.is_finite()
    }

    /// Sanitizes all concentric rectangles to strictly finite values.
    pub fn sanitize(&mut self) {
        self.content_box.sanitize();
        self.padding_box.sanitize();
        self.border_box.sanitize();
        self.margin_box.sanitize();
    }

    /// Returns the maximum allowable scroll offset `(max_x, max_y)`.
    #[inline(always)]
    pub fn max_scroll(&self) -> Point {
        Point::new(
            (self.scroll_bounds.width - self.padding_box.width).max(0.0),
            (self.scroll_bounds.height - self.padding_box.height).max(0.0),
        )
    }

    /// Returns true if this element is an active scroll container.
    #[inline(always)]
    pub fn is_scroll_container(&self) -> bool {
        self.has_scroll.0 || self.has_scroll.1
    }

    /// Clamps a candidate scroll offset to valid bounds `[0, max_scroll]`.
    #[inline(always)]
    pub fn clamp_scroll_offset(&self, offset: Point) -> Point {
        let max = self.max_scroll();
        Point::new(offset.x.clamp(0.0, max.x), offset.y.clamp(0.0, max.y))
    }

    /// Computes concentric boxes from a given border_box and padding/border/margin insets.
    pub fn from_border_box(
        border_box: Rect,
        padding: EdgeInsets<f32>,
        border: EdgeInsets<f32>,
        margin: EdgeInsets<f32>,
    ) -> Self {
        let sanitize_edge = |e: EdgeInsets<f32>| -> EdgeInsets<f32> {
            EdgeInsets {
                top: if e.top.is_finite() {
                    e.top.clamp(-1e7, 1e7)
                } else {
                    0.0
                },
                right: if e.right.is_finite() {
                    e.right.clamp(-1e7, 1e7)
                } else {
                    0.0
                },
                bottom: if e.bottom.is_finite() {
                    e.bottom.clamp(-1e7, 1e7)
                } else {
                    0.0
                },
                left: if e.left.is_finite() {
                    e.left.clamp(-1e7, 1e7)
                } else {
                    0.0
                },
            }
        };

        let padding = sanitize_edge(padding);
        let border = sanitize_edge(border);
        let margin = sanitize_edge(margin);

        let bx = if border_box.x.is_finite() {
            border_box.x.clamp(-1e7, 1e7)
        } else {
            0.0
        };
        let by = if border_box.y.is_finite() {
            border_box.y.clamp(-1e7, 1e7)
        } else {
            0.0
        };
        let bw = if border_box.width.is_finite() {
            border_box.width.clamp(0.0, 1e7)
        } else {
            0.0
        };
        let bh = if border_box.height.is_finite() {
            border_box.height.clamp(0.0, 1e7)
        } else {
            0.0
        };
        let border_box = Rect::new(bx, by, bw, bh);

        let content_box = Rect::new(
            border_box.x + border.left + padding.left,
            border_box.y + border.top + padding.top,
            (border_box.width - border.left - border.right - padding.left - padding.right)
                .clamp(0.0, 1e7),
            (border_box.height - border.top - border.bottom - padding.top - padding.bottom)
                .clamp(0.0, 1e7),
        );

        let padding_box = Rect::new(
            border_box.x + border.left,
            border_box.y + border.top,
            (border_box.width - border.left - border.right).clamp(0.0, 1e7),
            (border_box.height - border.top - border.bottom).clamp(0.0, 1e7),
        );

        let margin_box = Rect::new(
            border_box.x - margin.left,
            border_box.y - margin.top,
            (border_box.width + margin.left + margin.right).clamp(0.0, 1e7),
            (border_box.height + margin.top + margin.bottom).clamp(0.0, 1e7),
        );

        let scroll_bounds = Size::new(content_box.width, content_box.height);
        let scroll_offset = Point::ZERO;
        let has_scroll = (false, false);

        Self {
            content_box,
            padding_box,
            border_box,
            margin_box,
            scroll_bounds,
            scroll_offset,
            has_scroll,
        }
    }

    /// Computes concentric boxes while preserving the specified scroll offset.
    pub fn from_border_box_with_scroll(
        border_box: Rect,
        padding: EdgeInsets<f32>,
        border: EdgeInsets<f32>,
        margin: EdgeInsets<f32>,
        scroll_offset: Point,
    ) -> Self {
        let mut geom = Self::from_border_box(border_box, padding, border, margin);
        geom.scroll_offset = scroll_offset;
        geom
    }

    /// Translates all concentric rectangles by (dx, dy).
    pub fn translate(&mut self, dx: f32, dy: f32) {
        self.content_box.x += dx;
        self.content_box.y += dy;
        self.padding_box.x += dx;
        self.padding_box.y += dy;
        self.border_box.x += dx;
        self.border_box.y += dy;
        self.margin_box.x += dx;
        self.margin_box.y += dy;
    }

    /// Snaps all concentric rectangles to Chromium subpixel coordinates.
    pub fn snap_to_pixel(&mut self) {
        let _prof = crate::prof::Scope::new(crate::prof::Slot::Geometry);
        self.content_box = self.content_box.snap_to_pixel();
        self.padding_box = self.padding_box.snap_to_pixel();
        self.border_box = self.border_box.snap_to_pixel();
        self.margin_box = self.margin_box.snap_to_pixel();
    }

    /// Snaps all concentric rectangles against a device scale factor.
    pub fn snap_to_scale(&mut self, scale: f32) {
        let _prof = crate::prof::Scope::new(crate::prof::Slot::Geometry);
        self.content_box = self.content_box.snap_to_scale(scale);
        self.padding_box = self.padding_box.snap_to_scale(scale);
        self.border_box = self.border_box.snap_to_scale(scale);
        self.margin_box = self.margin_box.snap_to_scale(scale);
    }
}