mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 10:27:35 +00:00
LibWeb: Linear interpolation between table width sizing guesses
Make used widths of the columns a linear combination of two consecutive sizing-guesses when the assignable table width is less than or equal to the max-content sizing-guess, as the specification describes.
This commit is contained in:
parent
030b1a197b
commit
0314e26ef4
1 changed files with 57 additions and 53 deletions
|
@ -479,7 +479,10 @@ void TableFormattingContext::distribute_width_to_columns()
|
||||||
// This is the width that we will be able to allocate to the columns.
|
// This is the width that we will be able to allocate to the columns.
|
||||||
CSSPixels available_width = m_state.get(table_box()).content_width() - total_horizontal_border_spacing;
|
CSSPixels available_width = m_state.get(table_box()).content_width() - total_horizontal_border_spacing;
|
||||||
|
|
||||||
auto columns_total_used_width = [&]() {
|
Vector<CSSPixels> candidate_widths;
|
||||||
|
candidate_widths.resize(m_columns.size());
|
||||||
|
|
||||||
|
auto compute_columns_total_used_width = [&]() {
|
||||||
CSSPixels total_used_width = 0;
|
CSSPixels total_used_width = 0;
|
||||||
for (auto& column : m_columns) {
|
for (auto& column : m_columns) {
|
||||||
total_used_width += column.used_width;
|
total_used_width += column.used_width;
|
||||||
|
@ -487,54 +490,38 @@ void TableFormattingContext::distribute_width_to_columns()
|
||||||
return total_used_width;
|
return total_used_width;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto column_preferred_width = [&](Column& column) {
|
auto compute_columns_total_candidate_width = [&]() {
|
||||||
switch (column.type) {
|
CSSPixels total_candidate_width = 0;
|
||||||
case SizeType::Percent: {
|
for (auto width : candidate_widths) {
|
||||||
return max(column.min_size, column.percentage_width / 100 * available_width);
|
total_candidate_width += width;
|
||||||
}
|
|
||||||
case SizeType::Pixel:
|
|
||||||
case SizeType::Auto: {
|
|
||||||
return column.max_size;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
}
|
}
|
||||||
|
return total_candidate_width;
|
||||||
|
};
|
||||||
|
|
||||||
|
auto commit_candidate_widths = [&]() {
|
||||||
|
for (size_t i = 0; i < m_columns.size(); ++i) {
|
||||||
|
auto& column = m_columns[i];
|
||||||
|
column.used_width = candidate_widths[i];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
auto expand_columns_to_fill_available_width = [&](SizeType column_type) {
|
auto compute_linear_combination = [&](CSSPixels columns_total_candidate_width) {
|
||||||
CSSPixels remaining_available_width = available_width;
|
auto columns_total_used_width = compute_columns_total_used_width();
|
||||||
CSSPixels total_preferred_width_increment = 0;
|
if (columns_total_candidate_width == columns_total_used_width) {
|
||||||
for (auto& column : m_columns) {
|
|
||||||
remaining_available_width -= column.used_width;
|
|
||||||
if (column.type == column_type) {
|
|
||||||
total_preferred_width_increment += column_preferred_width(column) - column.min_size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (total_preferred_width_increment == 0)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (auto& column : m_columns) {
|
|
||||||
if (column.type == column_type) {
|
|
||||||
CSSPixels preferred_width_increment = column_preferred_width(column) - column.min_size;
|
|
||||||
column.used_width += preferred_width_increment * remaining_available_width / total_preferred_width_increment;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
auto candidate_weight = ((available_width - columns_total_used_width) / (columns_total_candidate_width - columns_total_used_width)).to_double();
|
||||||
|
for (size_t i = 0; i < m_columns.size(); ++i) {
|
||||||
auto shrink_columns_to_fit_available_width = [&](SizeType column_type) {
|
auto& column = m_columns[i];
|
||||||
for (auto& column : m_columns) {
|
column.used_width = candidate_weight * candidate_widths[i] + (1 - candidate_weight) * column.used_width;
|
||||||
if (column.type == column_type)
|
|
||||||
column.used_width = column.min_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
expand_columns_to_fill_available_width(column_type);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 1. The min-content sizing-guess is the set of column width assignments where each column is assigned its min-content width.
|
// 1. The min-content sizing-guess is the set of column width assignments where each column is assigned its min-content width.
|
||||||
for (auto& column : m_columns) {
|
for (size_t i = 0; i < m_columns.size(); ++i) {
|
||||||
|
auto& column = m_columns[i];
|
||||||
column.used_width = column.min_size;
|
column.used_width = column.min_size;
|
||||||
|
candidate_widths[i] = column.min_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. The min-content-percentage sizing-guess is the set of column width assignments where:
|
// 2. The min-content-percentage sizing-guess is the set of column width assignments where:
|
||||||
|
@ -542,15 +529,21 @@ void TableFormattingContext::distribute_width_to_columns()
|
||||||
// - its intrinsic percentage width times the assignable width and
|
// - its intrinsic percentage width times the assignable width and
|
||||||
// - its min-content width.
|
// - its min-content width.
|
||||||
// - all other columns are assigned their min-content width.
|
// - all other columns are assigned their min-content width.
|
||||||
for (auto& column : m_columns) {
|
for (size_t i = 0; i < m_columns.size(); ++i) {
|
||||||
|
auto& column = m_columns[i];
|
||||||
if (column.type == SizeType::Percent) {
|
if (column.type == SizeType::Percent) {
|
||||||
column.used_width = max(column.min_size, column.percentage_width / 100 * available_width);
|
candidate_widths[i] = max(column.min_size, column.percentage_width / 100 * available_width);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (columns_total_used_width() > available_width) {
|
auto columns_total_candidate_width = compute_columns_total_candidate_width();
|
||||||
shrink_columns_to_fit_available_width(SizeType::Percent);
|
// If the assignable table width is less than or equal to the max-content sizing-guess, the used widths of the columns must be the
|
||||||
|
// linear combination (with weights adding to 1) of the two consecutive sizing-guesses whose width sums bound the available width.
|
||||||
|
if (available_width < columns_total_candidate_width) {
|
||||||
|
compute_linear_combination(columns_total_candidate_width);
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
commit_candidate_widths();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. The min-content-specified sizing-guess is the set of column width assignments where:
|
// 3. The min-content-specified sizing-guess is the set of column width assignments where:
|
||||||
|
@ -559,15 +552,19 @@ void TableFormattingContext::distribute_width_to_columns()
|
||||||
// - its min-content width
|
// - its min-content width
|
||||||
// - any other column that is constrained is assigned its max-content width
|
// - any other column that is constrained is assigned its max-content width
|
||||||
// - all other columns are assigned their min-content width.
|
// - all other columns are assigned their min-content width.
|
||||||
for (auto& column : m_columns) {
|
for (size_t i = 0; i < m_columns.size(); ++i) {
|
||||||
if (column.type == SizeType::Pixel) {
|
auto& column = m_columns[i];
|
||||||
column.used_width = column.max_size;
|
if (column.is_constrained) {
|
||||||
|
candidate_widths[i] = column.max_size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (columns_total_used_width() > available_width) {
|
columns_total_candidate_width = compute_columns_total_candidate_width();
|
||||||
shrink_columns_to_fit_available_width(SizeType::Pixel);
|
if (available_width < columns_total_candidate_width) {
|
||||||
|
compute_linear_combination(columns_total_candidate_width);
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
commit_candidate_widths();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. The max-content sizing-guess is the set of column width assignments where:
|
// 4. The max-content sizing-guess is the set of column width assignments where:
|
||||||
|
@ -575,21 +572,28 @@ void TableFormattingContext::distribute_width_to_columns()
|
||||||
// - its intrinsic percentage width times the assignable width and
|
// - its intrinsic percentage width times the assignable width and
|
||||||
// - its min-content width
|
// - its min-content width
|
||||||
// - all other columns are assigned their max-content width.
|
// - all other columns are assigned their max-content width.
|
||||||
for (auto& column : m_columns) {
|
for (size_t i = 0; i < m_columns.size(); ++i) {
|
||||||
|
auto& column = m_columns[i];
|
||||||
if (column.type != SizeType::Percent) {
|
if (column.type != SizeType::Percent) {
|
||||||
column.used_width = column.max_size;
|
candidate_widths[i] = column.max_size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the assignable table width is less than or equal to the max-content sizing-guess, the used widths of the columns must be the
|
columns_total_candidate_width = compute_columns_total_candidate_width();
|
||||||
// linear combination (with weights adding to 1) of the two consecutive sizing-guesses whose width sums bound the available width.
|
if (available_width < columns_total_candidate_width) {
|
||||||
|
compute_linear_combination(columns_total_candidate_width);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
commit_candidate_widths();
|
||||||
|
}
|
||||||
|
|
||||||
// Otherwise, the used widths of the columns are the result of starting from the max-content sizing-guess and distributing
|
// Otherwise, the used widths of the columns are the result of starting from the max-content sizing-guess and distributing
|
||||||
// the excess width to the columns of the table according to the rules for distributing excess width to columns (for used width).
|
// the excess width to the columns of the table according to the rules for distributing excess width to columns (for used width).
|
||||||
|
|
||||||
// Implements steps 1 and 2 of https://www.w3.org/TR/css-tables-3/#distributing-width-to-columns
|
// Implements steps 1 and 2 of https://www.w3.org/TR/css-tables-3/#distributing-width-to-columns
|
||||||
// FIXME: Implement steps 3-6 as well, which distribute excess width to constrained columns.
|
// FIXME: Implement steps 3-6 as well, which distribute excess width to constrained columns.
|
||||||
if (columns_total_used_width() < available_width) {
|
auto columns_total_used_width = compute_columns_total_used_width();
|
||||||
|
if (columns_total_used_width < available_width) {
|
||||||
// NOTE: if all columns got their max width and there is still width to distribute left
|
// NOTE: if all columns got their max width and there is still width to distribute left
|
||||||
// it should be assigned to columns proportionally to their max width
|
// it should be assigned to columns proportionally to their max width
|
||||||
CSSPixels grid_max = 0.0f;
|
CSSPixels grid_max = 0.0f;
|
||||||
|
@ -602,7 +606,7 @@ void TableFormattingContext::distribute_width_to_columns()
|
||||||
++unconstrained_column_count;
|
++unconstrained_column_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto width_to_distribute = available_width - columns_total_used_width();
|
auto width_to_distribute = available_width - columns_total_used_width;
|
||||||
if (grid_max == 0) {
|
if (grid_max == 0) {
|
||||||
// If total max width of columns is zero then divide distributable width equally among them
|
// If total max width of columns is zero then divide distributable width equally among them
|
||||||
auto column_width = width_to_distribute / unconstrained_column_count;
|
auto column_width = width_to_distribute / unconstrained_column_count;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue