1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 07:07:34 +00:00

LibWeb: Convert GridFormattingContext to new pixel units

This commit is contained in:
Sam Atkins 2022-11-04 20:18:48 +00:00 committed by Linus Groh
parent f2c977251b
commit 55ddfa9348
2 changed files with 47 additions and 47 deletions

View file

@ -17,7 +17,7 @@ GridFormattingContext::GridFormattingContext(LayoutState& state, BlockContainer
GridFormattingContext::~GridFormattingContext() = default; GridFormattingContext::~GridFormattingContext() = default;
float GridFormattingContext::resolve_definite_track_size(CSS::GridSize const& grid_size, AvailableSpace const& available_space, Box const& box) CSSPixels GridFormattingContext::resolve_definite_track_size(CSS::GridSize const& grid_size, AvailableSpace const& available_space, Box const& box)
{ {
VERIFY(grid_size.is_definite()); VERIFY(grid_size.is_definite());
switch (grid_size.type()) { switch (grid_size.type()) {
@ -55,7 +55,7 @@ size_t GridFormattingContext::count_of_gap_rows()
return count; return count;
} }
float GridFormattingContext::resolve_size(CSS::Size const& size, AvailableSize const& available_size, Box const& box) CSSPixels GridFormattingContext::resolve_size(CSS::Size const& size, AvailableSize const& available_size, Box const& box)
{ {
if (size.is_length() && size.length().is_calculated()) { if (size.is_length() && size.length().is_calculated()) {
if (size.length().calculated_style_value()->contains_percentage()) { if (size.length().calculated_style_value()->contains_percentage()) {
@ -72,7 +72,7 @@ float GridFormattingContext::resolve_size(CSS::Size const& size, AvailableSize c
if (size.is_percentage()) { if (size.is_percentage()) {
if (!available_size.is_definite()) if (!available_size.is_definite())
return 0; return 0;
return available_size.to_px().value() * size.percentage().as_fraction(); return available_size.to_px() * size.percentage().as_fraction();
} }
return 0; return 0;
} }
@ -109,7 +109,7 @@ int GridFormattingContext::count_of_repeated_auto_fill_or_fit_tracks(Vector<CSS:
// repetitions is the largest possible positive integer that does not cause the grid to overflow the // repetitions is the largest possible positive integer that does not cause the grid to overflow the
// content box of its grid container // content box of its grid container
auto sum_of_grid_track_sizes = 0; CSSPixels sum_of_grid_track_sizes = 0;
// (treating each track as its max track sizing function if that is definite or its minimum track sizing // (treating each track as its max track sizing function if that is definite or its minimum track sizing
// function otherwise, flooring the max track sizing function by the min track sizing function if both // function otherwise, flooring the max track sizing function by the min track sizing function if both
// are definite, and taking gap into account) // are definite, and taking gap into account)
@ -127,7 +127,7 @@ int GridFormattingContext::count_of_repeated_auto_fill_or_fit_tracks(Vector<CSS:
sum_of_grid_track_sizes += min(resolve_definite_track_size(track_sizing_function.grid_size(), available_space, box), resolve_definite_track_size(track_sizing_function.grid_size(), available_space, box)); sum_of_grid_track_sizes += min(resolve_definite_track_size(track_sizing_function.grid_size(), available_space, box), resolve_definite_track_size(track_sizing_function.grid_size(), available_space, box));
} }
} }
return max(1, static_cast<int>(get_free_space_x(available_space) / sum_of_grid_track_sizes)); return max(1, static_cast<int>((get_free_space_x(available_space) / sum_of_grid_track_sizes).value()));
// For the purpose of finding the number of auto-repeated tracks in a standalone axis, the UA must // For the purpose of finding the number of auto-repeated tracks in a standalone axis, the UA must
// floor the track size to a UA-specified value to avoid division by zero. It is suggested that this // floor the track size to a UA-specified value to avoid division by zero. It is suggested that this
@ -781,7 +781,7 @@ void GridFormattingContext::calculate_sizes_of_columns(Box const& box, Available
// result from assuming the items used minimum size as its preferred size; else the items minimum // result from assuming the items used minimum size as its preferred size; else the items minimum
// contribution is its min-content contribution. Because the minimum contribution often depends on // contribution is its min-content contribution. Because the minimum contribution often depends on
// the size of the items content, it is considered a type of intrinsic size contribution. // the size of the items content, it is considered a type of intrinsic size contribution.
float grid_column_width = 0; CSSPixels grid_column_width = 0;
for (auto& box_of_column : boxes_of_column) for (auto& box_of_column : boxes_of_column)
grid_column_width = max(grid_column_width, calculate_min_content_width(box_of_column).value()); grid_column_width = max(grid_column_width, calculate_min_content_width(box_of_column).value());
grid_column.base_size = grid_column_width; grid_column.base_size = grid_column_width;
@ -887,7 +887,7 @@ void GridFormattingContext::calculate_sizes_of_columns(Box const& box, Available
// 12.5.1. Distributing Extra Space Across Spanned Tracks // 12.5.1. Distributing Extra Space Across Spanned Tracks
// To distribute extra space by increasing the affected sizes of a set of tracks as required by a // To distribute extra space by increasing the affected sizes of a set of tracks as required by a
// set of intrinsic size contributions, // set of intrinsic size contributions,
float sum_of_track_sizes = 0; CSSPixels sum_of_track_sizes = 0;
for (auto& it : m_grid_columns) for (auto& it : m_grid_columns)
sum_of_track_sizes += it.base_size; sum_of_track_sizes += it.base_size;
@ -909,10 +909,10 @@ void GridFormattingContext::calculate_sizes_of_columns(Box const& box, Available
for (auto& grid_column : m_grid_columns) { for (auto& grid_column : m_grid_columns) {
if (grid_column.is_gap) if (grid_column.is_gap)
continue; continue;
grid_column.space_to_distribute = max(0, (grid_column.growth_limit == -1 ? grid_column.base_size : grid_column.growth_limit) - grid_column.base_size); grid_column.space_to_distribute = max(CSSPixels(0), (grid_column.growth_limit == -1 ? grid_column.base_size : grid_column.growth_limit) - grid_column.base_size);
} }
auto remaining_free_space = available_space.width.is_definite() ? available_space.width.to_px().value() - sum_of_track_sizes : 0; auto remaining_free_space = available_space.width.is_definite() ? available_space.width.to_px() - sum_of_track_sizes : 0;
// 2.2. Distribute space up to limits: Find the item-incurred increase for each spanned track with an // 2.2. Distribute space up to limits: Find the item-incurred increase for each spanned track with an
// affected size by: distributing the space equally among such tracks, freezing a tracks // affected size by: distributing the space equally among such tracks, freezing a tracks
// item-incurred increase as its affected size + item-incurred increase reaches its limit (and // item-incurred increase as its affected size + item-incurred increase reaches its limit (and
@ -1024,13 +1024,13 @@ void GridFormattingContext::calculate_sizes_of_columns(Box const& box, Available
column_flex_factor_sum = 1; column_flex_factor_sum = 1;
// See 12.7.1. // See 12.7.1.
float sized_column_widths = 0; CSSPixels sized_column_widths = 0;
for (auto& grid_column : m_grid_columns) { for (auto& grid_column : m_grid_columns) {
if (!grid_column.min_track_sizing_function.is_flexible_length()) if (!grid_column.min_track_sizing_function.is_flexible_length())
sized_column_widths += grid_column.base_size; sized_column_widths += grid_column.base_size;
} }
// Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
double free_horizontal_space = available_space.width.is_definite() ? available_space.width.to_px().value() - sized_column_widths : 0; CSSPixels free_horizontal_space = available_space.width.is_definite() ? available_space.width.to_px() - sized_column_widths : 0;
// If the free space is zero or if sizing the grid container under a min-content constraint: // If the free space is zero or if sizing the grid container under a min-content constraint:
// The used flex fraction is zero. // The used flex fraction is zero.
@ -1044,7 +1044,7 @@ void GridFormattingContext::calculate_sizes_of_columns(Box const& box, Available
if (grid_column.min_track_sizing_function.is_flexible_length()) { if (grid_column.min_track_sizing_function.is_flexible_length()) {
// See 12.7.1. // See 12.7.1.
// Let the hypothetical fr size be the leftover space divided by the flex factor sum. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
auto hypothetical_fr_size = static_cast<double>(1.0 / column_flex_factor_sum) * free_horizontal_space; auto hypothetical_fr_size = free_horizontal_space / column_flex_factor_sum;
// For each flexible track, if the product of the used flex fraction and the tracks flex factor is // For each flexible track, if the product of the used flex fraction and the tracks flex factor is
// greater than the tracks base size, set its base size to that product. // greater than the tracks base size, set its base size to that product.
grid_column.base_size = max(grid_column.base_size, hypothetical_fr_size); grid_column.base_size = max(grid_column.base_size, hypothetical_fr_size);
@ -1097,13 +1097,13 @@ void GridFormattingContext::calculate_sizes_of_columns(Box const& box, Available
// positive, definite free space equally amongst them. If the free space is indefinite, but the grid // positive, definite free space equally amongst them. If the free space is indefinite, but the grid
// container has a definite min-width/height, use that size to calculate the free space for this // container has a definite min-width/height, use that size to calculate the free space for this
// step instead. // step instead.
float used_horizontal_space = 0; CSSPixels used_horizontal_space = 0;
for (auto& grid_column : m_grid_columns) { for (auto& grid_column : m_grid_columns) {
if (!(grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())) if (!(grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto()))
used_horizontal_space += grid_column.base_size; used_horizontal_space += grid_column.base_size;
} }
float remaining_horizontal_space = available_space.width.is_definite() ? available_space.width.to_px().value() - used_horizontal_space : 0; CSSPixels remaining_horizontal_space = available_space.width.is_definite() ? available_space.width.to_px() - used_horizontal_space : 0;
auto count_of_auto_max_column_tracks = 0; auto count_of_auto_max_column_tracks = 0;
for (auto& grid_column : m_grid_columns) { for (auto& grid_column : m_grid_columns) {
if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto()) if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
@ -1239,9 +1239,9 @@ void GridFormattingContext::calculate_sizes_of_rows(Box const& box)
// result from assuming the items used minimum size as its preferred size; else the items minimum // result from assuming the items used minimum size as its preferred size; else the items minimum
// contribution is its min-content contribution. Because the minimum contribution often depends on // contribution is its min-content contribution. Because the minimum contribution often depends on
// the size of the items content, it is considered a type of intrinsic size contribution. // the size of the items content, it is considered a type of intrinsic size contribution.
float grid_row_height = 0; CSSPixels grid_row_height = 0;
for (auto& positioned_box : positioned_boxes_of_row) for (auto& positioned_box : positioned_boxes_of_row)
grid_row_height = max(grid_row_height, calculate_min_content_height(positioned_box.box, AvailableSize::make_definite(m_grid_columns[positioned_box.column].base_size)).value()); grid_row_height = max(grid_row_height, calculate_min_content_height(positioned_box.box, AvailableSize::make_definite(m_grid_columns[positioned_box.column].base_size)));
grid_row.base_size = grid_row_height; grid_row.base_size = grid_row_height;
// - For min-content maximums: // - For min-content maximums:
@ -1425,13 +1425,13 @@ void GridFormattingContext::calculate_sizes_of_rows(Box const& box)
row_flex_factor_sum = 1; row_flex_factor_sum = 1;
// See 12.7.1. // See 12.7.1.
float sized_row_heights = 0; CSSPixels sized_row_heights = 0;
for (auto& grid_row : m_grid_rows) { for (auto& grid_row : m_grid_rows) {
if (!grid_row.min_track_sizing_function.is_flexible_length()) if (!grid_row.min_track_sizing_function.is_flexible_length())
sized_row_heights += grid_row.base_size; sized_row_heights += grid_row.base_size;
} }
// Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
double free_vertical_space = box_state.content_height() - sized_row_heights; CSSPixels free_vertical_space = CSSPixels(box_state.content_height()) - sized_row_heights;
// If the free space is zero or if sizing the grid container under a min-content constraint: // If the free space is zero or if sizing the grid container under a min-content constraint:
// The used flex fraction is zero. // The used flex fraction is zero.
@ -1445,7 +1445,7 @@ void GridFormattingContext::calculate_sizes_of_rows(Box const& box)
if (grid_row.min_track_sizing_function.is_flexible_length()) { if (grid_row.min_track_sizing_function.is_flexible_length()) {
// See 12.7.1. // See 12.7.1.
// Let the hypothetical fr size be the leftover space divided by the flex factor sum. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
auto hypothetical_fr_size = static_cast<double>(1.0 / row_flex_factor_sum) * free_vertical_space; auto hypothetical_fr_size = free_vertical_space / row_flex_factor_sum;
// For each flexible track, if the product of the used flex fraction and the tracks flex factor is // For each flexible track, if the product of the used flex fraction and the tracks flex factor is
// greater than the tracks base size, set its base size to that product. // greater than the tracks base size, set its base size to that product.
grid_row.base_size = max(grid_row.base_size, hypothetical_fr_size); grid_row.base_size = max(grid_row.base_size, hypothetical_fr_size);
@ -1498,13 +1498,13 @@ void GridFormattingContext::calculate_sizes_of_rows(Box const& box)
// positive, definite free space equally amongst them. If the free space is indefinite, but the grid // positive, definite free space equally amongst them. If the free space is indefinite, but the grid
// container has a definite min-width/height, use that size to calculate the free space for this // container has a definite min-width/height, use that size to calculate the free space for this
// step instead. // step instead.
float used_vertical_space = 0; CSSPixels used_vertical_space = 0;
for (auto& grid_row : m_grid_rows) { for (auto& grid_row : m_grid_rows) {
if (!(grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())) if (!(grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto()))
used_vertical_space += grid_row.base_size; used_vertical_space += grid_row.base_size;
} }
float remaining_vertical_space = box_state.content_height() - used_vertical_space; CSSPixels remaining_vertical_space = CSSPixels(box_state.content_height()) - used_vertical_space;
auto count_of_auto_max_row_tracks = 0; auto count_of_auto_max_row_tracks = 0;
for (auto& grid_row : m_grid_rows) { for (auto& grid_row : m_grid_rows) {
if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto()) if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
@ -1720,10 +1720,10 @@ void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const
auto layout_box = [&](int row_start, int row_end, int column_start, int column_end, Box const& child_box) -> void { auto layout_box = [&](int row_start, int row_end, int column_start, int column_end, Box const& child_box) -> void {
auto& child_box_state = m_state.get_mutable(child_box); auto& child_box_state = m_state.get_mutable(child_box);
float x_start = 0; CSSPixels x_start = 0;
float x_end = 0; CSSPixels x_end = 0;
float y_start = 0; CSSPixels y_start = 0;
float y_end = 0; CSSPixels y_end = 0;
for (int i = 0; i < column_start; i++) for (int i = 0; i < column_start; i++)
x_start += m_grid_columns[i].base_size; x_start += m_grid_columns[i].base_size;
for (int i = 0; i < column_end; i++) for (int i = 0; i < column_end; i++)
@ -1732,8 +1732,8 @@ void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const
y_start += m_grid_rows[i].base_size; y_start += m_grid_rows[i].base_size;
for (int i = 0; i < row_end; i++) for (int i = 0; i < row_end; i++)
y_end += m_grid_rows[i].base_size; y_end += m_grid_rows[i].base_size;
child_box_state.set_content_width(x_end - x_start); child_box_state.set_content_width((x_end - x_start).value());
child_box_state.set_content_height(y_end - y_start); child_box_state.set_content_height((y_end - y_start).value());
child_box_state.offset = { x_start, y_start }; child_box_state.offset = { x_start, y_start };
auto available_space_for_children = AvailableSpace(AvailableSize::make_definite(child_box_state.content_width()), AvailableSize::make_definite(child_box_state.content_height())); auto available_space_for_children = AvailableSpace(AvailableSize::make_definite(child_box_state.content_width()), AvailableSize::make_definite(child_box_state.content_height()));
@ -1764,7 +1764,7 @@ void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const
positioned_box.box); positioned_box.box);
} }
float total_y = 0; CSSPixels total_y = 0;
for (auto& grid_row : m_grid_rows) for (auto& grid_row : m_grid_rows)
total_y += grid_row.base_size; total_y += grid_row.base_size;
m_automatic_content_height = total_y; m_automatic_content_height = total_y;
@ -1790,7 +1790,7 @@ bool GridFormattingContext::is_auto_positioned_track(CSS::GridTrackPlacement con
return grid_track_start.is_auto_positioned() && grid_track_end.is_auto_positioned(); return grid_track_start.is_auto_positioned() && grid_track_end.is_auto_positioned();
} }
float GridFormattingContext::get_free_space_x(AvailableSpace const& available_space) CSSPixels GridFormattingContext::get_free_space_x(AvailableSpace const& available_space)
{ {
// https://www.w3.org/TR/css-grid-2/#algo-terms // https://www.w3.org/TR/css-grid-2/#algo-terms
// free space: Equal to the available grid space minus the sum of the base sizes of all the grid // free space: Equal to the available grid space minus the sum of the base sizes of all the grid
@ -1799,24 +1799,24 @@ float GridFormattingContext::get_free_space_x(AvailableSpace const& available_sp
// FIXME: do indefinite space // FIXME: do indefinite space
if (!available_space.width.is_definite()) if (!available_space.width.is_definite())
return 0; return 0;
auto sum_base_sizes = 0; CSSPixels sum_base_sizes = 0;
for (auto& grid_column : m_grid_columns) for (auto& grid_column : m_grid_columns)
sum_base_sizes += grid_column.base_size; sum_base_sizes += grid_column.base_size;
return max(0, available_space.width.to_px().value() - sum_base_sizes); return max(CSSPixels(0), available_space.width.to_px() - sum_base_sizes);
} }
float GridFormattingContext::get_free_space_y(Box const& box) CSSPixels GridFormattingContext::get_free_space_y(Box const& box)
{ {
// https://www.w3.org/TR/css-grid-2/#algo-terms // https://www.w3.org/TR/css-grid-2/#algo-terms
// free space: Equal to the available grid space minus the sum of the base sizes of all the grid // free space: Equal to the available grid space minus the sum of the base sizes of all the grid
// tracks (including gutters), floored at zero. If available grid space is indefinite, the free // tracks (including gutters), floored at zero. If available grid space is indefinite, the free
// space is indefinite as well. // space is indefinite as well.
auto sum_base_sizes = 0; CSSPixels sum_base_sizes = 0;
for (auto& grid_row : m_grid_rows) for (auto& grid_row : m_grid_rows)
sum_base_sizes += grid_row.base_size; sum_base_sizes += grid_row.base_size;
auto& box_state = m_state.get_mutable(box); auto& box_state = m_state.get_mutable(box);
if (box_state.has_definite_height()) if (box_state.has_definite_height())
return max(0, absolute_content_rect(box, m_state).height() - sum_base_sizes); return max(CSSPixels(0), CSSPixels(absolute_content_rect(box, m_state).height()) - sum_base_sizes);
return -1; return -1;
} }

View file

@ -40,7 +40,7 @@ public:
virtual CSSPixels automatic_content_height() const override; virtual CSSPixels automatic_content_height() const override;
private: private:
float m_automatic_content_height { 0 }; CSSPixels m_automatic_content_height { 0 };
bool is_auto_positioned_row(CSS::GridTrackPlacement const&, CSS::GridTrackPlacement const&) const; bool is_auto_positioned_row(CSS::GridTrackPlacement const&, CSS::GridTrackPlacement const&) const;
bool is_auto_positioned_column(CSS::GridTrackPlacement const&, CSS::GridTrackPlacement const&) const; bool is_auto_positioned_column(CSS::GridTrackPlacement const&, CSS::GridTrackPlacement const&) const;
bool is_auto_positioned_track(CSS::GridTrackPlacement const&, CSS::GridTrackPlacement const&) const; bool is_auto_positioned_track(CSS::GridTrackPlacement const&, CSS::GridTrackPlacement const&) const;
@ -56,10 +56,10 @@ private:
struct TemporaryTrack { struct TemporaryTrack {
CSS::GridSize min_track_sizing_function; CSS::GridSize min_track_sizing_function;
CSS::GridSize max_track_sizing_function; CSS::GridSize max_track_sizing_function;
float base_size { 0 }; CSSPixels base_size { 0 };
float growth_limit { 0 }; CSSPixels growth_limit { 0 };
float space_to_distribute { 0 }; CSSPixels space_to_distribute { 0 };
float planned_increase { 0 }; CSSPixels planned_increase { 0 };
bool is_gap { false }; bool is_gap { false };
TemporaryTrack(CSS::GridSize min_track_sizing_function, CSS::GridSize max_track_sizing_function) TemporaryTrack(CSS::GridSize min_track_sizing_function, CSS::GridSize max_track_sizing_function)
@ -74,10 +74,10 @@ private:
{ {
} }
TemporaryTrack(float size_in_px, bool is_gap) TemporaryTrack(CSSPixels size, bool is_gap)
: min_track_sizing_function(CSS::GridSize(CSS::Length(size_in_px, CSS::Length::Type::Px))) : min_track_sizing_function(CSS::GridSize(CSS::Length::make_px(size)))
, max_track_sizing_function(CSS::GridSize(CSS::Length(size_in_px, CSS::Length::Type::Px))) , max_track_sizing_function(CSS::GridSize(CSS::Length::make_px(size)))
, base_size(size_in_px) , base_size(size)
, is_gap(is_gap) , is_gap(is_gap)
{ {
} }
@ -96,14 +96,14 @@ private:
Vector<PositionedBox> m_positioned_boxes; Vector<PositionedBox> m_positioned_boxes;
Vector<Box const&> m_boxes_to_place; Vector<Box const&> m_boxes_to_place;
float get_free_space_x(AvailableSpace const& available_space); CSSPixels get_free_space_x(AvailableSpace const& available_space);
float get_free_space_y(Box const&); CSSPixels get_free_space_y(Box const&);
int get_line_index_by_line_name(DeprecatedString const& line_name, CSS::GridTrackSizeList); int get_line_index_by_line_name(DeprecatedString const& line_name, CSS::GridTrackSizeList);
float resolve_definite_track_size(CSS::GridSize const&, AvailableSpace const&, Box const&); CSSPixels resolve_definite_track_size(CSS::GridSize const&, AvailableSpace const&, Box const&);
size_t count_of_gap_columns(); size_t count_of_gap_columns();
size_t count_of_gap_rows(); size_t count_of_gap_rows();
float resolve_size(CSS::Size const&, AvailableSize const&, Box const&); CSSPixels resolve_size(CSS::Size const&, AvailableSize const&, Box const&);
int count_of_repeated_auto_fill_or_fit_tracks(Vector<CSS::ExplicitGridTrack> const& track_list, AvailableSpace const&, Box const&); int count_of_repeated_auto_fill_or_fit_tracks(Vector<CSS::ExplicitGridTrack> const& track_list, AvailableSpace const&, Box const&);
int get_count_of_tracks(Vector<CSS::ExplicitGridTrack> const&, AvailableSpace const&, Box const&); int get_count_of_tracks(Vector<CSS::ExplicitGridTrack> const&, AvailableSpace const&, Box const&);