taffy/compute/grid/
mod.rs

1//! This module is a partial implementation of the CSS Grid Level 1 specification
2//! <https://www.w3.org/TR/css-grid-1>
3use crate::geometry::{AbsoluteAxis, AbstractAxis, InBothAbsAxis};
4use crate::geometry::{Line, Point, Rect, Size};
5use crate::style::{AlignItems, AlignSelf, AvailableSpace, Overflow, Position};
6use crate::tree::{Layout, LayoutInput, LayoutOutput, LayoutPartialTreeExt, NodeId, RunMode, SizingMode};
7use crate::util::debug::debug_log;
8use crate::util::sys::{f32_max, f32_min, GridTrackVec, Vec};
9use crate::util::MaybeMath;
10use crate::util::{MaybeResolve, ResolveOrZero};
11use crate::{
12    style_helpers::*, AlignContent, BoxGenerationMode, BoxSizing, CoreStyle, Direction, GridContainerStyle,
13    GridItemStyle, JustifyContent, LayoutGridContainer,
14};
15use alignment::{align_and_position_item, align_tracks};
16use explicit_grid::{compute_explicit_grid_size_in_axis, initialize_grid_tracks, AutoRepeatStrategy};
17use implicit_grid::compute_grid_size_estimate;
18use placement::place_grid_items;
19use track_sizing::{
20    determine_if_item_crosses_flexible_or_intrinsic_tracks, resolve_item_track_indexes, track_sizing_algorithm,
21};
22use types::{CellOccupancyMatrix, GridTrack, NamedLineResolver, TrackCounts};
23
24#[cfg(feature = "detailed_layout_info")]
25use types::{GridItem, GridTrackKind};
26
27pub(crate) use types::{GridCoordinate, GridLine, OriginZeroLine};
28
29mod alignment;
30mod explicit_grid;
31mod implicit_grid;
32mod placement;
33mod track_sizing;
34mod types;
35mod util;
36
37/// Grid layout algorithm
38/// This consists of a few phases:
39///   - Resolving the explicit grid
40///   - Placing items (which also resolves the implicit grid)
41///   - Track (row/column) sizing
42///   - Alignment & Final item placement
43pub fn compute_grid_layout<Tree: LayoutGridContainer>(
44    tree: &mut Tree,
45    node: NodeId,
46    inputs: LayoutInput,
47) -> LayoutOutput {
48    let LayoutInput { known_dimensions, parent_size, available_space, run_mode, .. } = inputs;
49
50    let style = tree.get_grid_container_style(node);
51    let direction = style.direction();
52
53    // 1. Compute "available grid space"
54    // https://www.w3.org/TR/css-grid-1/#available-grid-space
55    let aspect_ratio = style.aspect_ratio();
56    let padding = style.padding().resolve_or_zero(parent_size.width, |val, basis| tree.calc(val, basis));
57    let border = style.border().resolve_or_zero(parent_size.width, |val, basis| tree.calc(val, basis));
58    let padding_border = padding + border;
59    let padding_border_size = padding_border.sum_axes();
60    let box_sizing_adjustment =
61        if style.box_sizing() == BoxSizing::ContentBox { padding_border_size } else { Size::ZERO };
62
63    let min_size = style
64        .min_size()
65        .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
66        .maybe_apply_aspect_ratio(aspect_ratio)
67        .maybe_add(box_sizing_adjustment);
68    let max_size = style
69        .max_size()
70        .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
71        .maybe_apply_aspect_ratio(aspect_ratio)
72        .maybe_add(box_sizing_adjustment);
73    let preferred_size = if inputs.sizing_mode == SizingMode::InherentSize {
74        style
75            .size()
76            .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
77            .maybe_apply_aspect_ratio(style.aspect_ratio())
78            .maybe_add(box_sizing_adjustment)
79    } else {
80        Size::NONE
81    };
82
83    // Scrollbar gutters are reserved when the `overflow` property is set to `Overflow::Scroll`.
84    // However, the axis are switched (transposed) because a node that scrolls vertically needs
85    // *horizontal* space to be reserved for a scrollbar
86    let scrollbar_gutter = style.overflow().transpose().map(|overflow| match overflow {
87        Overflow::Scroll => style.scrollbar_width(),
88        _ => 0.0,
89    });
90    let mut content_box_inset = padding_border;
91    content_box_inset.bottom += scrollbar_gutter.y;
92
93    match direction {
94        Direction::Ltr => content_box_inset.right += scrollbar_gutter.x,
95        Direction::Rtl => content_box_inset.left += scrollbar_gutter.x,
96    };
97
98    let align_content = style.align_content().unwrap_or(AlignContent::Stretch);
99    let justify_content = style.justify_content().unwrap_or(JustifyContent::Stretch);
100    let align_items = style.align_items();
101    let justify_items = style.justify_items();
102
103    // Note: we avoid accessing the grid rows/columns methods more than once as this can
104    // cause an expensive-ish computation
105    let grid_template_columns = style.grid_template_columns();
106    let grid_template_rows = style.grid_template_rows();
107    let grid_auto_columns = style.grid_auto_columns();
108    let grid_auto_rows = style.grid_auto_rows();
109
110    let constrained_available_space = known_dimensions
111        .or(preferred_size)
112        .map(|size| size.map(AvailableSpace::Definite))
113        .unwrap_or(available_space)
114        .maybe_clamp(min_size, max_size)
115        .maybe_max(padding_border_size);
116
117    let available_grid_space = Size {
118        width: constrained_available_space
119            .width
120            .map_definite_value(|space| space - content_box_inset.horizontal_axis_sum()),
121        height: constrained_available_space
122            .height
123            .map_definite_value(|space| space - content_box_inset.vertical_axis_sum()),
124    };
125
126    let outer_node_size =
127        known_dimensions.or(preferred_size).maybe_clamp(min_size, max_size).maybe_max(padding_border_size);
128    let mut inner_node_size = Size {
129        width: outer_node_size.width.map(|space| space - content_box_inset.horizontal_axis_sum()),
130        height: outer_node_size.height.map(|space| space - content_box_inset.vertical_axis_sum()),
131    };
132
133    debug_log!("parent_size", dbg:parent_size);
134    debug_log!("outer_node_size", dbg:outer_node_size);
135    debug_log!("inner_node_size", dbg:inner_node_size);
136
137    if let (RunMode::ComputeSize, Some(width), Some(height)) = (run_mode, outer_node_size.width, outer_node_size.height)
138    {
139        return LayoutOutput::from_outer_size(Size { width, height });
140    }
141
142    let get_child_styles_iter =
143        |node| tree.child_ids(node).map(|child_node: NodeId| tree.get_grid_child_style(child_node));
144    let child_styles_iter = get_child_styles_iter(node);
145
146    // 2. Resolve the explicit grid
147
148    // This is very similar to the inner_node_size except if the inner_node_size is not definite but the node
149    // has a min- or max- size style then that will be used in it's place.
150    let auto_fit_container_size = outer_node_size
151        .or(max_size)
152        .or(min_size)
153        .maybe_clamp(min_size, max_size)
154        .maybe_max(padding_border_size)
155        .maybe_sub(content_box_inset.sum_axes());
156
157    // If the grid container has a definite size or max size in the relevant axis:
158    //   - then the number of repetitions is the largest possible positive integer that does not cause the grid to overflow the content
159    //     box of its grid container.
160    // Otherwise, if the grid container has a definite min size in the relevant axis:
161    //   - then the number of repetitions is the smallest possible positive integer that fulfills that minimum requirement
162    // Otherwise, the specified track list repeats only once.
163    let auto_repeat_fit_strategy = outer_node_size.or(max_size).map(|val| match val {
164        Some(_) => AutoRepeatStrategy::MaxRepetitionsThatDoNotOverflow,
165        None => AutoRepeatStrategy::MinRepetitionsThatDoOverflow,
166    });
167
168    // Compute the number of rows and columns in the explicit grid *template*
169    // (explicit tracks from grid_areas are computed separately below)
170    let (col_auto_repetition_count, grid_template_col_count) = compute_explicit_grid_size_in_axis(
171        &style,
172        auto_fit_container_size.width,
173        auto_repeat_fit_strategy.width,
174        |val, basis| tree.calc(val, basis),
175        AbsoluteAxis::Horizontal,
176    );
177    let (row_auto_repetition_count, grid_template_row_count) = compute_explicit_grid_size_in_axis(
178        &style,
179        auto_fit_container_size.height,
180        auto_repeat_fit_strategy.height,
181        |val, basis| tree.calc(val, basis),
182        AbsoluteAxis::Vertical,
183    );
184
185    // type CustomIdent<'a> = <<Tree as LayoutPartialTree>::CoreContainerStyle<'_> as CoreStyle>::CustomIdent;
186    let mut name_resolver = NamedLineResolver::new(&style, col_auto_repetition_count, row_auto_repetition_count);
187
188    let explicit_col_count = grid_template_col_count.max(name_resolver.area_column_count());
189    let explicit_row_count = grid_template_row_count.max(name_resolver.area_row_count());
190
191    name_resolver.set_explicit_column_count(explicit_col_count);
192    name_resolver.set_explicit_row_count(explicit_row_count);
193
194    // 3. Implicit Grid: Estimate Track Counts
195    // Estimate the number of rows and columns in the implicit grid (= the entire grid)
196    // This is necessary as part of placement. Doing it early here is a perf optimisation to reduce allocations.
197    let (est_col_counts, est_row_counts) =
198        compute_grid_size_estimate(explicit_col_count, explicit_row_count, direction, child_styles_iter);
199
200    // 4. Grid Item Placement
201    // Match items (children) to a definite grid position (row start/end and column start/end position)
202    let mut items = Vec::with_capacity(tree.child_count(node));
203    let mut cell_occupancy_matrix = CellOccupancyMatrix::with_track_counts(est_col_counts, est_row_counts);
204    let in_flow_children_iter = || {
205        tree.child_ids(node)
206            .enumerate()
207            .map(|(index, child_node)| (index, child_node, tree.get_grid_child_style(child_node)))
208            .filter(|(_, _, style)| {
209                style.box_generation_mode() != BoxGenerationMode::None && style.position() != Position::Absolute
210            })
211    };
212    place_grid_items(
213        &mut cell_occupancy_matrix,
214        &mut items,
215        in_flow_children_iter,
216        direction,
217        style.grid_auto_flow(),
218        align_items.unwrap_or(AlignItems::Stretch),
219        justify_items.unwrap_or(AlignItems::Stretch),
220        &name_resolver,
221    );
222
223    // Extract track counts from previous step (auto-placement can expand the number of tracks)
224    let final_col_counts = *cell_occupancy_matrix.track_counts(AbsoluteAxis::Horizontal);
225    let final_row_counts = *cell_occupancy_matrix.track_counts(AbsoluteAxis::Vertical);
226
227    // 5. Initialize Tracks
228    // Initialize (explicit and implicit) grid tracks (and gutters)
229    // This resolves the min and max track sizing functions for all tracks and gutters
230    let mut columns = GridTrackVec::new();
231    let mut rows = GridTrackVec::new();
232    let mut column_track_counts_for_init = final_col_counts;
233    if direction.is_rtl() && final_col_counts.explicit <= 1 {
234        column_track_counts_for_init.negative_implicit = final_col_counts.positive_implicit;
235        column_track_counts_for_init.positive_implicit = final_col_counts.negative_implicit;
236    }
237    initialize_grid_tracks(
238        &mut columns,
239        column_track_counts_for_init,
240        &style,
241        AbsoluteAxis::Horizontal,
242        |column_index| {
243            let occupancy_index = if direction.is_rtl() {
244                rtl_column_occupancy_index_for_initialization(column_index, final_col_counts)
245            } else {
246                column_index
247            };
248            cell_occupancy_matrix.column_is_occupied(occupancy_index)
249        },
250    );
251    initialize_grid_tracks(&mut rows, final_row_counts, &style, AbsoluteAxis::Vertical, |row_index| {
252        cell_occupancy_matrix.row_is_occupied(row_index)
253    });
254    if direction.is_rtl() {
255        reverse_non_gutter_tracks(&mut columns, final_col_counts);
256    }
257
258    drop(grid_template_rows);
259    drop(grid_template_columns);
260    drop(grid_auto_rows);
261    drop(grid_auto_columns);
262    drop(style);
263
264    // 6. Track Sizing
265
266    // Convert grid placements in origin-zero coordinates to indexes into the GridTrack (rows and columns) vectors
267    // This computation is relatively trivial, but it requires the final number of negative (implicit) tracks in
268    // each axis, and doing it up-front here means we don't have to keep repeating that calculation
269    resolve_item_track_indexes(&mut items, final_col_counts, final_row_counts);
270    // For each item, and in each axis, determine whether the item crosses any flexible (fr) tracks
271    // Record this as a boolean (per-axis) on each item for later use in the track-sizing algorithm
272    determine_if_item_crosses_flexible_or_intrinsic_tracks(&mut items, &columns, &rows);
273
274    // Determine if the grid has any baseline aligned items
275    let has_baseline_aligned_item = items.iter().any(|item| item.align_self == AlignSelf::Baseline);
276
277    // Run track sizing algorithm for Inline axis
278    track_sizing_algorithm(
279        tree,
280        AbstractAxis::Inline,
281        min_size.get(AbstractAxis::Inline),
282        max_size.get(AbstractAxis::Inline),
283        justify_content,
284        align_content,
285        available_grid_space,
286        inner_node_size,
287        &mut columns,
288        &mut rows,
289        &mut items,
290        |track: &GridTrack, parent_size: Option<f32>, tree: &Tree| {
291            track.max_track_sizing_function.definite_value(parent_size, |val, basis| tree.calc(val, basis))
292        },
293        has_baseline_aligned_item,
294    );
295    let initial_column_sum = columns.iter().map(|track| track.base_size).sum::<f32>();
296    inner_node_size.width = inner_node_size.width.or_else(|| initial_column_sum.into());
297
298    items.iter_mut().for_each(|item| item.available_space_cache = None);
299
300    // Run track sizing algorithm for Block axis
301    track_sizing_algorithm(
302        tree,
303        AbstractAxis::Block,
304        min_size.get(AbstractAxis::Block),
305        max_size.get(AbstractAxis::Block),
306        align_content,
307        justify_content,
308        available_grid_space,
309        inner_node_size,
310        &mut rows,
311        &mut columns,
312        &mut items,
313        |track: &GridTrack, _, _| Some(track.base_size),
314        false, // TODO: Support baseline alignment in the vertical axis
315    );
316    let initial_row_sum = rows.iter().map(|track| track.base_size).sum::<f32>();
317    inner_node_size.height = inner_node_size.height.or_else(|| initial_row_sum.into());
318
319    debug_log!("initial_column_sum", dbg:initial_column_sum);
320    debug_log!(dbg: columns.iter().map(|track| track.base_size).collect::<Vec<_>>());
321    debug_log!("initial_row_sum", dbg:initial_row_sum);
322    debug_log!(dbg: rows.iter().map(|track| track.base_size).collect::<Vec<_>>());
323
324    // 6. Compute container size
325    let resolved_style_size = known_dimensions.or(preferred_size);
326    let container_border_box = Size {
327        width: resolved_style_size
328            .get(AbstractAxis::Inline)
329            .unwrap_or_else(|| initial_column_sum + content_box_inset.horizontal_axis_sum())
330            .maybe_clamp(min_size.width, max_size.width)
331            .max(padding_border_size.width),
332        height: resolved_style_size
333            .get(AbstractAxis::Block)
334            .unwrap_or_else(|| initial_row_sum + content_box_inset.vertical_axis_sum())
335            .maybe_clamp(min_size.height, max_size.height)
336            .max(padding_border_size.height),
337    };
338    let container_content_box = Size {
339        width: f32_max(0.0, container_border_box.width - content_box_inset.horizontal_axis_sum()),
340        height: f32_max(0.0, container_border_box.height - content_box_inset.vertical_axis_sum()),
341    };
342
343    // If only the container's size has been requested
344    if run_mode == RunMode::ComputeSize {
345        return LayoutOutput::from_outer_size(container_border_box);
346    }
347
348    // 7. Resolve percentage track base sizes
349    // In the case of an indefinitely sized container these resolve to zero during the "Initialise Tracks" step
350    // and therefore need to be re-resolved here based on the content-sized content box of the container
351    if !available_grid_space.width.is_definite() {
352        for column in &mut columns {
353            let min: Option<f32> = column
354                .min_track_sizing_function
355                .resolved_percentage_size(container_content_box.width, |val, basis| tree.calc(val, basis));
356            let max: Option<f32> = column
357                .max_track_sizing_function
358                .resolved_percentage_size(container_content_box.width, |val, basis| tree.calc(val, basis));
359            column.base_size = column.base_size.maybe_clamp(min, max);
360        }
361    }
362    if !available_grid_space.height.is_definite() {
363        for row in &mut rows {
364            let min: Option<f32> = row
365                .min_track_sizing_function
366                .resolved_percentage_size(container_content_box.height, |val, basis| tree.calc(val, basis));
367            let max: Option<f32> = row
368                .max_track_sizing_function
369                .resolved_percentage_size(container_content_box.height, |val, basis| tree.calc(val, basis));
370            row.base_size = row.base_size.maybe_clamp(min, max);
371        }
372    }
373
374    // Column sizing must be re-run (once) if:
375    //   - The grid container's width was initially indefinite and there are any columns with percentage track sizing functions
376    //   - Any grid item crossing an intrinsically sized track's min content contribution width has changed
377    // TODO: Only rerun sizing for tracks that actually require it rather than for all tracks if any need it.
378    let mut rerun_column_sizing;
379
380    let has_percentage_column = columns.iter().any(|track| track.uses_percentage());
381    let parent_width_indefinite = !available_space.width.is_definite();
382    rerun_column_sizing = parent_width_indefinite && has_percentage_column;
383
384    if !rerun_column_sizing {
385        let min_content_contribution_changed =
386            items.iter_mut().filter(|item| item.crosses_intrinsic_column).any(|item| {
387                let available_space = item.available_space(
388                    AbstractAxis::Inline,
389                    &rows,
390                    inner_node_size.height,
391                    |track: &GridTrack, _| Some(track.base_size),
392                );
393                let new_min_content_contribution =
394                    item.min_content_contribution(AbstractAxis::Inline, tree, available_space, inner_node_size);
395
396                let has_changed = Some(new_min_content_contribution) != item.min_content_contribution_cache.width;
397
398                item.available_space_cache = Some(available_space);
399                item.min_content_contribution_cache.width = Some(new_min_content_contribution);
400                item.max_content_contribution_cache.width = None;
401                item.minimum_contribution_cache.width = None;
402
403                has_changed
404            });
405        rerun_column_sizing = min_content_contribution_changed;
406    } else {
407        // Clear intrinsic width caches
408        items.iter_mut().for_each(|item| {
409            item.available_space_cache = None;
410            item.min_content_contribution_cache.width = None;
411            item.max_content_contribution_cache.width = None;
412            item.minimum_contribution_cache.width = None;
413        });
414    }
415
416    if rerun_column_sizing {
417        // Re-run track sizing algorithm for Inline axis
418        track_sizing_algorithm(
419            tree,
420            AbstractAxis::Inline,
421            min_size.get(AbstractAxis::Inline),
422            max_size.get(AbstractAxis::Inline),
423            justify_content,
424            align_content,
425            available_grid_space,
426            inner_node_size,
427            &mut columns,
428            &mut rows,
429            &mut items,
430            |track: &GridTrack, _, _| Some(track.base_size),
431            has_baseline_aligned_item,
432        );
433
434        // Row sizing must be re-run (once) if:
435        //   - The grid container's height was initially indefinite and there are any rows with percentage track sizing functions
436        //   - Any grid item crossing an intrinsically sized track's min content contribution height has changed
437        // TODO: Only rerun sizing for tracks that actually require it rather than for all tracks if any need it.
438        let mut rerun_row_sizing;
439
440        let has_percentage_row = rows.iter().any(|track| track.uses_percentage());
441        let parent_height_indefinite = !available_space.height.is_definite();
442        rerun_row_sizing = parent_height_indefinite && has_percentage_row;
443
444        if !rerun_row_sizing {
445            let min_content_contribution_changed =
446                items.iter_mut().filter(|item| item.crosses_intrinsic_column).any(|item| {
447                    let available_space = item.available_space(
448                        AbstractAxis::Block,
449                        &columns,
450                        inner_node_size.width,
451                        |track: &GridTrack, _| Some(track.base_size),
452                    );
453                    let new_min_content_contribution =
454                        item.min_content_contribution(AbstractAxis::Block, tree, available_space, inner_node_size);
455
456                    let has_changed = Some(new_min_content_contribution) != item.min_content_contribution_cache.height;
457
458                    item.available_space_cache = Some(available_space);
459                    item.min_content_contribution_cache.height = Some(new_min_content_contribution);
460                    item.max_content_contribution_cache.height = None;
461                    item.minimum_contribution_cache.height = None;
462
463                    has_changed
464                });
465            rerun_row_sizing = min_content_contribution_changed;
466        } else {
467            items.iter_mut().for_each(|item| {
468                // Clear intrinsic height caches
469                item.available_space_cache = None;
470                item.min_content_contribution_cache.height = None;
471                item.max_content_contribution_cache.height = None;
472                item.minimum_contribution_cache.height = None;
473            });
474        }
475
476        if rerun_row_sizing {
477            // Re-run track sizing algorithm for Block axis
478            track_sizing_algorithm(
479                tree,
480                AbstractAxis::Block,
481                min_size.get(AbstractAxis::Block),
482                max_size.get(AbstractAxis::Block),
483                align_content,
484                justify_content,
485                available_grid_space,
486                inner_node_size,
487                &mut rows,
488                &mut columns,
489                &mut items,
490                |track: &GridTrack, _, _| Some(track.base_size),
491                false, // TODO: Support baseline alignment in the vertical axis
492            );
493        }
494    }
495
496    // 8. Track Alignment
497
498    // Align columns
499    let inline_size_without_scrollbar = f32_max(container_border_box.width - padding_border_size.width, 0.0);
500    let inline_scrollbar_gutter_for_alignment = f32_min(scrollbar_gutter.x, inline_size_without_scrollbar);
501    align_tracks(
502        container_content_box.get(AbstractAxis::Inline),
503        Line {
504            start: padding.left + if direction.is_rtl() { inline_scrollbar_gutter_for_alignment } else { 0.0 },
505            end: padding.right + if direction.is_rtl() { 0.0 } else { inline_scrollbar_gutter_for_alignment },
506        },
507        Line { start: border.left, end: border.right },
508        &mut columns,
509        justify_content,
510        direction.is_rtl(),
511    );
512    // Align rows
513    align_tracks(
514        container_content_box.get(AbstractAxis::Block),
515        Line { start: padding.top, end: padding.bottom },
516        Line { start: border.top, end: border.bottom },
517        &mut rows,
518        align_content,
519        false,
520    );
521
522    // 9. Size, Align, and Position Grid Items
523
524    #[cfg_attr(not(feature = "content_size"), allow(unused_mut))]
525    let mut item_content_size_contribution = Size::ZERO;
526
527    // Sort items back into original order to allow them to be matched up with styles
528    items.sort_by_key(|item| item.source_order);
529
530    let container_alignment_styles = InBothAbsAxis { horizontal: justify_items, vertical: align_items };
531
532    // Position in-flow children (stored in items vector)
533    for (index, item) in items.iter_mut().enumerate() {
534        let grid_area = Rect {
535            top: rows[item.row_indexes.start as usize + 1].offset,
536            bottom: rows[item.row_indexes.end as usize].offset,
537            left: columns[item.column_indexes.start as usize + 1].offset,
538            right: columns[item.column_indexes.end as usize].offset,
539        };
540        #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
541        let (content_size_contribution, y_position, height) = align_and_position_item(
542            tree,
543            item.node,
544            index as u32,
545            grid_area,
546            container_alignment_styles,
547            item.baseline_shim,
548            direction,
549        );
550        item.y_position = y_position;
551        item.height = height;
552
553        #[cfg(feature = "content_size")]
554        {
555            item_content_size_contribution = item_content_size_contribution.f32_max(content_size_contribution);
556        }
557    }
558
559    // Position hidden and absolutely positioned children
560    let mut order = items.len() as u32;
561    (0..tree.child_count(node)).for_each(|index| {
562        let child = tree.get_child_id(node, index);
563        let child_style = tree.get_grid_child_style(child);
564
565        // Position hidden child
566        if child_style.box_generation_mode() == BoxGenerationMode::None {
567            drop(child_style);
568            tree.set_unrounded_layout(child, &Layout::with_order(order));
569            tree.perform_child_layout(
570                child,
571                Size::NONE,
572                Size::NONE,
573                Size::MAX_CONTENT,
574                SizingMode::InherentSize,
575                Line::FALSE,
576            );
577            order += 1;
578            return;
579        }
580
581        // Position absolutely positioned child
582        if child_style.position() == Position::Absolute {
583            // Convert grid-col-{start/end} into Option's of indexes into the columns vector
584            // The Option is None if the style property is Auto and an unresolvable Span
585            let maybe_col_indexes = name_resolver
586                .resolve_column_names(&child_style.grid_column())
587                .into_origin_zero(final_col_counts.explicit)
588                .resolve_absolutely_positioned_grid_tracks()
589                .map(|maybe_grid_line| {
590                    maybe_grid_line
591                        .map(|line: OriginZeroLine| {
592                            if direction.is_rtl() {
593                                OriginZeroLine(final_col_counts.explicit as i16 - line.0)
594                            } else {
595                                line
596                            }
597                        })
598                        .and_then(|line| line.try_into_track_vec_index(final_col_counts))
599                });
600            let maybe_col_indexes = if direction.is_rtl() {
601                Line { start: maybe_col_indexes.end, end: maybe_col_indexes.start }
602            } else {
603                maybe_col_indexes
604            };
605            // Convert grid-row-{start/end} into Option's of indexes into the row vector
606            // The Option is None if the style property is Auto and an unresolvable Span
607            let maybe_row_indexes = name_resolver
608                .resolve_row_names(&child_style.grid_row())
609                .into_origin_zero(final_row_counts.explicit)
610                .resolve_absolutely_positioned_grid_tracks()
611                .map(|maybe_grid_line| {
612                    maybe_grid_line.and_then(|line: OriginZeroLine| line.try_into_track_vec_index(final_row_counts))
613                });
614
615            let grid_area = Rect {
616                top: maybe_row_indexes.start.map(|index| rows[index].offset).unwrap_or(border.top),
617                bottom: maybe_row_indexes
618                    .end
619                    .map(|index| rows[index].offset)
620                    .unwrap_or(container_border_box.height - border.bottom - scrollbar_gutter.y),
621                left: maybe_col_indexes.start.map(|index| columns[index].offset).unwrap_or_else(|| {
622                    if direction.is_rtl() {
623                        border.left + scrollbar_gutter.x
624                    } else {
625                        border.left
626                    }
627                }),
628                right: maybe_col_indexes.end.map(|index| columns[index].offset).unwrap_or_else(|| {
629                    if direction.is_rtl() {
630                        container_border_box.width - border.right
631                    } else {
632                        container_border_box.width - border.right - scrollbar_gutter.x
633                    }
634                }),
635            };
636            drop(child_style);
637
638            // TODO: Baseline alignment support for absolutely positioned items (should check if is actually specified)
639            #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
640            let (content_size_contribution, _, _) =
641                align_and_position_item(tree, child, order, grid_area, container_alignment_styles, 0.0, direction);
642            #[cfg(feature = "content_size")]
643            {
644                item_content_size_contribution = item_content_size_contribution.f32_max(content_size_contribution);
645            }
646
647            order += 1;
648        }
649    });
650
651    // Set detailed grid information
652    #[cfg(feature = "detailed_layout_info")]
653    tree.set_detailed_grid_info(
654        node,
655        DetailedGridInfo {
656            rows: DetailedGridTracksInfo::from_grid_tracks_and_track_count(final_row_counts, rows),
657            columns: DetailedGridTracksInfo::from_grid_tracks_and_track_count(final_col_counts, columns),
658            items: items.iter().map(DetailedGridItemsInfo::from_grid_item).collect(),
659        },
660    );
661
662    // If there are not items then return just the container size (no baseline)
663    if items.is_empty() {
664        return LayoutOutput::from_outer_size(container_border_box);
665    }
666
667    // Determine the grid container baseline(s) (currently we only compute the first baseline)
668    let grid_container_baseline: f32 = {
669        // Sort items by row start position so that we can iterate items in groups which are in the same row
670        items.sort_by_key(|item| item.row_indexes.start);
671
672        // Get the row index of the first row containing items
673        let first_row = items[0].row_indexes.start;
674
675        // Create a slice of all of the items start in this row (taking advantage of the fact that we have just sorted the array)
676        let first_row_items = &items[0..].split(|item| item.row_indexes.start != first_row).next().unwrap();
677
678        // Check if any items in *this row* are baseline aligned
679        let row_has_baseline_item = first_row_items.iter().any(|item| item.align_self == AlignSelf::Baseline);
680
681        let item = if row_has_baseline_item {
682            first_row_items.iter().find(|item| item.align_self == AlignSelf::Baseline).unwrap()
683        } else {
684            &first_row_items[0]
685        };
686
687        item.y_position + item.baseline.unwrap_or(item.height)
688    };
689
690    LayoutOutput::from_sizes_and_baselines(
691        container_border_box,
692        item_content_size_contribution,
693        Point { x: None, y: Some(grid_container_baseline) },
694    )
695}
696
697/// Reverses only non-gutter column tracks in-place while preserving line/gutter slots.
698fn reverse_non_gutter_tracks(tracks: &mut [GridTrack], track_counts: TrackCounts) {
699    // When the explicit grid has 0/1 tracks, visual RTL mirroring is entirely determined by implicit tracks.
700    // Reverse all non-gutter tracks in that case.
701    if track_counts.explicit <= 1 {
702        const MIN_TRACK_VEC_LEN_TO_REVERSE_COLUMNS: usize = 5;
703        if tracks.len() < MIN_TRACK_VEC_LEN_TO_REVERSE_COLUMNS {
704            return;
705        }
706        let mut left = 1;
707        let mut right = tracks.len() - 2;
708        while left < right {
709            tracks.swap(left, right);
710            left += 2;
711            right = right.saturating_sub(2);
712        }
713        return;
714    }
715
716    let explicit_track_count = track_counts.explicit as usize;
717    if explicit_track_count < 2 {
718        return;
719    }
720
721    let mut left = track_counts.negative_implicit as usize;
722    let mut right = left + explicit_track_count - 1;
723    while left < right {
724        tracks.swap((2 * left) + 1, (2 * right) + 1);
725        left += 1;
726        right = right.saturating_sub(1);
727    }
728}
729
730/// Maps initialized column indexes to occupancy-matrix indexes for auto-fit collapsing in RTL.
731fn rtl_column_occupancy_index_for_initialization(column_index: usize, track_counts: TrackCounts) -> usize {
732    if track_counts.explicit <= 1 {
733        return track_counts.len() - column_index - 1;
734    }
735
736    let explicit_start = track_counts.negative_implicit as usize;
737    let explicit_end = explicit_start + track_counts.explicit as usize;
738    if (explicit_start..explicit_end).contains(&column_index) {
739        explicit_start + (explicit_end - column_index - 1)
740    } else {
741        column_index
742    }
743}
744
745/// Information from the computation of grid
746#[derive(Debug, Clone, PartialEq)]
747#[cfg(feature = "detailed_layout_info")]
748pub struct DetailedGridInfo {
749    /// <https://drafts.csswg.org/css-grid-1/#grid-row>
750    pub rows: DetailedGridTracksInfo,
751    /// <https://drafts.csswg.org/css-grid-1/#grid-column>
752    pub columns: DetailedGridTracksInfo,
753    /// <https://drafts.csswg.org/css-grid-1/#grid-items>
754    pub items: Vec<DetailedGridItemsInfo>,
755}
756
757/// Information from the computation of grids tracks
758#[derive(Debug, Clone, PartialEq)]
759#[cfg(feature = "detailed_layout_info")]
760pub struct DetailedGridTracksInfo {
761    /// Number of leading implicit grid tracks
762    pub negative_implicit_tracks: u16,
763    /// Number of explicit grid tracks
764    pub explicit_tracks: u16,
765    /// Number of trailing implicit grid tracks
766    pub positive_implicit_tracks: u16,
767
768    /// Gutters between tracks
769    pub gutters: Vec<f32>,
770    /// The used size of the tracks
771    pub sizes: Vec<f32>,
772}
773
774#[cfg(feature = "detailed_layout_info")]
775impl DetailedGridTracksInfo {
776    /// Get the base_size of [`GridTrack`] with a kind [`types::GridTrackKind`]
777    #[inline(always)]
778    fn grid_track_base_size_of_kind(grid_tracks: &[GridTrack], kind: GridTrackKind) -> Vec<f32> {
779        grid_tracks
780            .iter()
781            .filter_map(|track| match track.kind == kind {
782                true => Some(track.base_size),
783                false => None,
784            })
785            .collect()
786    }
787
788    /// Get the sizes of the gutters
789    fn gutters_from_grid_track_layout(grid_tracks: &[GridTrack]) -> Vec<f32> {
790        DetailedGridTracksInfo::grid_track_base_size_of_kind(grid_tracks, GridTrackKind::Gutter)
791    }
792
793    /// Get the sizes of the tracks
794    fn sizes_from_grid_track_layout(grid_tracks: &[GridTrack]) -> Vec<f32> {
795        DetailedGridTracksInfo::grid_track_base_size_of_kind(grid_tracks, GridTrackKind::Track)
796    }
797
798    /// Construct DetailedGridTracksInfo from TrackCounts and GridTracks
799    fn from_grid_tracks_and_track_count(track_count: TrackCounts, grid_tracks: Vec<GridTrack>) -> Self {
800        DetailedGridTracksInfo {
801            negative_implicit_tracks: track_count.negative_implicit,
802            explicit_tracks: track_count.explicit,
803            positive_implicit_tracks: track_count.positive_implicit,
804            gutters: DetailedGridTracksInfo::gutters_from_grid_track_layout(&grid_tracks),
805            sizes: DetailedGridTracksInfo::sizes_from_grid_track_layout(&grid_tracks),
806        }
807    }
808}
809
810/// Grid area information from the placement algorithm
811///
812/// The values is 1-indexed grid line numbers bounding the area.
813/// This matches the Chrome and Firefox's format as of 2nd Jan 2024.
814#[derive(Debug, Clone, PartialEq)]
815#[cfg(feature = "detailed_layout_info")]
816pub struct DetailedGridItemsInfo {
817    /// row-start with 1-indexed grid line numbers
818    pub row_start: u16,
819    /// row-end with 1-indexed grid line numbers
820    pub row_end: u16,
821    /// column-start with 1-indexed grid line numbers
822    pub column_start: u16,
823    /// column-end with 1-indexed grid line numbers
824    pub column_end: u16,
825}
826
827/// Grid area information from the placement algorithm
828#[cfg(feature = "detailed_layout_info")]
829impl DetailedGridItemsInfo {
830    /// Construct from GridItems
831    #[inline(always)]
832    fn from_grid_item(grid_item: &GridItem) -> Self {
833        /// Conversion from the indexes of Vec<GridTrack> into 1-indexed grid line numbers. See [`GridItem::row_indexes`] or [`GridItem::column_indexes`]
834        #[inline(always)]
835        fn to_one_indexed_grid_line(grid_track_index: u16) -> u16 {
836            grid_track_index / 2 + 1
837        }
838
839        DetailedGridItemsInfo {
840            row_start: to_one_indexed_grid_line(grid_item.row_indexes.start),
841            row_end: to_one_indexed_grid_line(grid_item.row_indexes.end),
842            column_start: to_one_indexed_grid_line(grid_item.column_indexes.start),
843            column_end: to_one_indexed_grid_line(grid_item.column_indexes.end),
844        }
845    }
846}