1use app_units::Au;
6use euclid::Rect;
7use euclid::default::Size2D as UntypedSize2D;
8use layout_api::{AxesOverflow, LayoutElement, LayoutNode};
9use malloc_size_of_derive::MallocSizeOf;
10use paint_api::display_list::AxesScrollSensitivity;
11use script::layout_dom::ServoLayoutNode;
12use servo_arc::Arc as ServoArc;
13use style::context::SharedStyleContext;
14use style::logical_geometry::WritingMode;
15use style::properties::ComputedValues;
16use style::values::computed::{Display as StyleDisplay, Overflow};
17use style_traits::CSSPixel;
18
19use crate::PropagatedBoxTreeData;
20use crate::cell::ArcRefCell;
21use crate::containing_block::DefiniteContainingBlock;
22use crate::context::LayoutContext;
23use crate::dom::{LayoutBox, NodeExt};
24use crate::dom_traversal::{Contents, NodeAndStyleInfo};
25use crate::flow::float::FloatBox;
26use crate::flow::{BlockContainer, BlockFormattingContext, BlockLevelBox};
27use crate::formatting_contexts::IndependentFormattingContext;
28use crate::fragment_tree::{FragmentFlags, FragmentTree};
29use crate::geom::{LogicalVec2, PhysicalSize};
30use crate::positioned::{AbsolutelyPositionedBox, PositioningContext};
31use crate::style_ext::Display;
32
33#[derive(MallocSizeOf)]
34pub struct BoxTree {
35 root: BlockFormattingContext,
38 pub(crate) viewport_overflow: AxesOverflow,
40 pub(crate) initial_containing_block_style: ServoArc<ComputedValues>,
42}
43
44impl BoxTree {
45 #[servo_tracing::instrument(name = "Box Tree Construction", skip_all)]
46 pub(crate) fn construct(context: &LayoutContext, root_element: ServoLayoutNode<'_>) -> Self {
47 let style_for_writing_mode =
48 Self::writing_mode_source_style(&context.style_context, root_element);
49 let boxes = construct_for_root_element(context, root_element, &style_for_writing_mode);
50
51 assert!(boxes.len() <= 1);
53
54 let default_style = context
55 .style_context
56 .stylist
57 .device()
58 .default_computed_values_arc();
59 let initial_containing_block_style = BoxTree::style_with_writing_mode_source_style(
60 default_style.clone(),
61 &style_for_writing_mode,
62 );
63
64 let viewport_overflow = Self::viewport_overflow(root_element, boxes.first());
65 let contents = BlockContainer::BlockLevelBoxes(boxes);
66 Self {
67 root: BlockFormattingContext::from_block_container(
68 contents, false, None,
69 ),
70 viewport_overflow: viewport_overflow.to_scrollable(),
74 initial_containing_block_style,
75 }
76 }
77
78 pub(crate) fn writing_mode_source_style(
94 context: &SharedStyleContext,
95 root_element: ServoLayoutNode<'_>,
96 ) -> ServoArc<ComputedValues> {
97 let root_element_style = root_element.style(context);
100
101 debug_assert_ne!(*root_element_style.get_display(), StyleDisplay::Contents);
104
105 if *root_element_style.get_display() == StyleDisplay::None {
106 return root_element_style;
107 }
108
109 let Some(body) = root_element.flat_tree_children().find(|child| {
113 child
114 .as_element()
115 .is_some_and(|element| element.is_body_element_of_html_element_root())
116 }) else {
117 return root_element_style;
118 };
119
120 let body_style = body.style(context);
125 let body_display = body_style.get_display();
126 if *body_display == StyleDisplay::Contents || *body_display == StyleDisplay::None {
127 return root_element.style(context);
128 }
129 body_style.clone()
130 }
131
132 pub(crate) fn style_with_writing_mode_source_style(
133 base_style: ServoArc<ComputedValues>,
134 writing_mode_source_style: &ServoArc<ComputedValues>,
135 ) -> ServoArc<ComputedValues> {
136 if base_style.writing_mode == writing_mode_source_style.writing_mode &&
137 base_style.get_direction() == writing_mode_source_style.get_direction() &&
138 base_style.get_text_orientation() == writing_mode_source_style.get_text_orientation()
139 {
140 return base_style;
141 }
142
143 let mut new_style = (*base_style).clone();
144 let inherited_box = new_style.mutate_inherited_box();
145 inherited_box.writing_mode = writing_mode_source_style.get_inherited_box().writing_mode;
146 inherited_box.direction = writing_mode_source_style.get_inherited_box().direction;
147 inherited_box.text_orientation = writing_mode_source_style
148 .get_inherited_box()
149 .text_orientation;
150 new_style.writing_mode = writing_mode_source_style.writing_mode;
151 ServoArc::new(new_style)
152 }
153
154 pub(crate) fn writing_mode(&self) -> WritingMode {
155 self.initial_containing_block_style.writing_mode
156 }
157
158 fn viewport_overflow(
159 root_element: ServoLayoutNode<'_>,
160 root_box: Option<&ArcRefCell<BlockLevelBox>>,
161 ) -> AxesOverflow {
162 let Some(root_box) = root_box else {
175 return AxesOverflow::default();
176 };
177
178 let propagate_from_body = || {
179 let body = root_element.flat_tree_children().find(|child| {
183 child
184 .as_element()
185 .is_some_and(|element| element.is_body_element_of_html_element_root())
186 })?;
187
188 let body_layout_data = body.inner_layout_data()?;
192 let mut body_box = body_layout_data.self_box.borrow_mut();
193 body_box.as_mut()?.with_base_mut(|base| {
194 base.base_fragment_info
195 .flags
196 .insert(FragmentFlags::PROPAGATED_OVERFLOW_TO_VIEWPORT);
197 AxesOverflow::from(&*base.style)
198 })
199 };
200
201 root_box.borrow_mut().with_base_mut(|base| {
202 let root_overflow = AxesOverflow::from(&*base.style);
203 if root_overflow.x == Overflow::Visible &&
204 root_overflow.y == Overflow::Visible &&
205 let Some(body_overflow) = propagate_from_body()
206 {
207 return body_overflow;
208 }
209 base.base_fragment_info
210 .flags
211 .insert(FragmentFlags::PROPAGATED_OVERFLOW_TO_VIEWPORT);
212 root_overflow
213 })
214 }
215}
216
217fn construct_for_root_element(
218 context: &LayoutContext,
219 root_element: ServoLayoutNode<'_>,
220 style_for_writing_mode: &ServoArc<ComputedValues>,
221) -> Vec<ArcRefCell<BlockLevelBox>> {
222 let root_style = BoxTree::style_with_writing_mode_source_style(
223 root_element.style(&context.style_context),
224 style_for_writing_mode,
225 );
226 let info = NodeAndStyleInfo::new(root_element, root_style);
227 let box_style = info.style.get_box();
228
229 let display_inside = match Display::from(box_style.display) {
230 Display::None => return Vec::new(),
231 Display::Contents => {
232 unreachable!()
236 },
237 Display::GeneratingBox(display_generating_box) => display_generating_box.display_inside(),
239 };
240
241 let contents = Contents::for_element(root_element, context);
242
243 let propagated_data = PropagatedBoxTreeData::default();
244 let root_box = if box_style.position.is_absolutely_positioned() {
245 BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(ArcRefCell::new(
246 AbsolutelyPositionedBox::construct(context, &info, display_inside, contents),
247 ))
248 } else if box_style.float.is_floating() {
249 BlockLevelBox::OutOfFlowFloatBox(FloatBox::construct(
250 context,
251 &info,
252 display_inside,
253 contents,
254 propagated_data,
255 ))
256 } else {
257 BlockLevelBox::Independent(IndependentFormattingContext::construct(
258 context,
259 &info,
260 display_inside,
261 contents,
262 propagated_data,
263 ))
264 };
265
266 let root_box = ArcRefCell::new(root_box);
267 root_element
268 .box_slot()
269 .set(LayoutBox::BlockLevel(root_box.clone()));
270 vec![root_box]
271}
272
273impl BoxTree {
274 #[servo_tracing::instrument(name = "Fragment Tree Construction", skip_all)]
275 pub(crate) fn layout(
276 &self,
277 layout_context: &LayoutContext,
278 viewport: UntypedSize2D<Au>,
279 ) -> FragmentTree {
280 let physical_containing_block: Rect<Au, CSSPixel> =
281 PhysicalSize::from_untyped(viewport).into();
282
283 let initial_containing_block_size = LogicalVec2::from_physical_size(
284 &physical_containing_block.size,
285 self.initial_containing_block_style.writing_mode,
286 );
287 let initial_containing_block = DefiniteContainingBlock {
288 size: initial_containing_block_size,
289 style: &self.initial_containing_block_style,
290 };
291
292 let mut positioning_context = PositioningContext::default();
293 let independent_layout = self.root.layout(
294 layout_context,
295 &mut positioning_context,
296 &(&initial_containing_block).into(),
297 &initial_containing_block.size.block.into(),
298 None,
299 );
300
301 let root_fragments = independent_layout.fragments;
302 let mut root_fragments = root_fragments.into_physical_fragments(
303 self.initial_containing_block_style.writing_mode,
304 initial_containing_block_size,
305 );
306
307 assert!(root_fragments.len() <= 1);
309
310 positioning_context.layout_initial_containing_block_children(
314 layout_context,
315 &initial_containing_block,
316 &mut root_fragments,
317 );
318
319 let viewport_scroll_sensitivity = AxesScrollSensitivity {
320 x: self.viewport_overflow.x.into(),
321 y: self.viewport_overflow.y.into(),
322 };
323
324 FragmentTree::new(
325 root_fragments,
326 physical_containing_block,
327 viewport_scroll_sensitivity,
328 )
329 }
330}