From 50ade23779b91e01cf5e483d526742d01c7b0a69 Mon Sep 17 00:00:00 2001 From: Kamil Jarosz Date: Wed, 19 Jun 2024 17:57:07 +0200 Subject: [PATCH] text: Implement Layout.bounds --- core/src/html/layout.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core/src/html/layout.rs b/core/src/html/layout.rs index 4bc26868f..d06361bc8 100644 --- a/core/src/html/layout.rs +++ b/core/src/html/layout.rs @@ -78,6 +78,13 @@ pub struct LayoutContext<'a, 'gc> { /// A growing list of layout boxes that form the line currently being laid out. boxes: Vec>, + /// The bounds of all laid-out text, excluding margins. + /// + /// None indicates that no bounds have yet to be calculated. If the layout + /// ends without a line having been generated, the default bounding box + /// should be used. + bounds: Option>, + /// The exterior bounds of all laid-out text, including left and right /// margins. /// @@ -115,6 +122,7 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> { lines: Vec::new(), current_line_index: 0, boxes: Vec::new(), + bounds: None, exterior_bounds: None, is_first_line: true, has_line_break: false, @@ -357,6 +365,12 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> { boxes, }); self.current_line_index += 1; + + if let Some(lb) = &mut self.bounds { + *lb += bounds; + } else { + self.bounds = Some(bounds); + } } } @@ -690,6 +704,7 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> { ); Layout { + bounds: self.bounds.unwrap_or_default(), exterior_bounds: self.exterior_bounds.unwrap_or_default(), lines: self.lines, } @@ -705,6 +720,9 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> { #[derive(Clone, Debug, Collect)] #[collect(no_drop)] pub struct Layout<'gc> { + #[collect(require_static)] + bounds: BoxBounds, + #[collect(require_static)] exterior_bounds: BoxBounds, @@ -712,6 +730,11 @@ pub struct Layout<'gc> { } impl<'gc> Layout<'gc> { + /// Bounds of this layout, i.e. a union of bounds of all layout boxes. + pub fn bounds(&self) -> BoxBounds { + self.bounds + } + /// Exterior bounds of this layout. /// /// The returned bounds will include the text bounds itself,