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

LibWeb: Convert InlineFormattingContext to new pixel units

This commit is contained in:
Sam Atkins 2022-11-03 17:48:46 +00:00 committed by Linus Groh
parent 55ddfa9348
commit 056acb5ebf
3 changed files with 37 additions and 37 deletions

View file

@ -38,27 +38,27 @@ BlockFormattingContext const& InlineFormattingContext::parent() const
return static_cast<BlockFormattingContext const&>(*FormattingContext::parent()); return static_cast<BlockFormattingContext const&>(*FormattingContext::parent());
} }
float InlineFormattingContext::leftmost_x_offset_at(float y) const CSSPixels InlineFormattingContext::leftmost_x_offset_at(CSSPixels y) const
{ {
// NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC. // NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state); auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state).to_type<CSSPixels>();
float y_in_root = box_in_root_rect.y() + y; CSSPixels y_in_root = box_in_root_rect.y() + y;
auto space = parent().space_used_by_floats(y_in_root); auto space = parent().space_used_by_floats(y_in_root);
return space.left.value(); return space.left;
} }
float InlineFormattingContext::available_space_for_line(float y) const CSSPixels InlineFormattingContext::available_space_for_line(CSSPixels y) const
{ {
// NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC. // NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
auto& root_block = parent().root(); auto& root_block = parent().root();
auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), root_block, m_state); auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), root_block, m_state).to_type<CSSPixels>();
float y_in_root = box_in_root_rect.y() + y; CSSPixels y_in_root = box_in_root_rect.y() + y;
auto space = parent().space_used_by_floats(y_in_root); auto space = parent().space_used_by_floats(y_in_root);
space.left = space.left; space.left = space.left;
space.right = min(m_available_space->width.to_px() - space.right, m_available_space->width.to_px()); space.right = min(m_available_space->width.to_px() - space.right, m_available_space->width.to_px());
return (space.right - space.left).value(); return space.right - space.left;
} }
CSSPixels InlineFormattingContext::automatic_content_width() const CSSPixels InlineFormattingContext::automatic_content_width() const
@ -77,8 +77,8 @@ void InlineFormattingContext::run(Box const&, LayoutMode layout_mode, AvailableS
m_available_space = available_space; m_available_space = available_space;
generate_line_boxes(layout_mode); generate_line_boxes(layout_mode);
float max_line_width = 0; CSSPixels max_line_width = 0;
float content_height = 0; CSSPixels content_height = 0;
for (auto& line_box : m_containing_block_state.line_boxes) { for (auto& line_box : m_containing_block_state.line_boxes) {
max_line_width = max(max_line_width, line_box.width()); max_line_width = max(max_line_width, line_box.width());
@ -126,7 +126,7 @@ void InlineFormattingContext::dimension_box_on_line(Box const& box, LayoutMode l
VERIFY(!box.display().is_flow_inside()); VERIFY(!box.display().is_flow_inside());
auto const& width_value = box.computed_values().width(); auto const& width_value = box.computed_values().width();
float unconstrained_width = 0; CSSPixels unconstrained_width = 0;
if (should_treat_width_as_auto(box, *m_available_space)) { if (should_treat_width_as_auto(box, *m_available_space)) {
auto result = calculate_shrink_to_fit_widths(box); auto result = calculate_shrink_to_fit_widths(box);
@ -138,7 +138,7 @@ void InlineFormattingContext::dimension_box_on_line(Box const& box, LayoutMode l
- box_state.border_right - box_state.border_right
- box_state.margin_right; - box_state.margin_right;
unconstrained_width = min(max(result.preferred_minimum_width, available_width), result.preferred_width).value(); unconstrained_width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
} else { } else {
if (width_value.contains_percentage() && !m_available_space->width.is_definite()) { if (width_value.contains_percentage() && !m_available_space->width.is_definite()) {
// NOTE: We can't resolve percentages yet. We'll have to wait until after inner layout. // NOTE: We can't resolve percentages yet. We'll have to wait until after inner layout.
@ -148,7 +148,7 @@ void InlineFormattingContext::dimension_box_on_line(Box const& box, LayoutMode l
} }
} }
float width = unconstrained_width; CSSPixels width = unconstrained_width;
auto computed_max_width = box.computed_values().max_width(); auto computed_max_width = box.computed_values().max_width();
if (!computed_max_width.is_none()) { if (!computed_max_width.is_none()) {
auto max_width = computed_max_width.resolved(box, width_of_containing_block).to_px(box); auto max_width = computed_max_width.resolved(box, width_of_containing_block).to_px(box);
@ -161,7 +161,7 @@ void InlineFormattingContext::dimension_box_on_line(Box const& box, LayoutMode l
width = max(width, min_width); width = max(width, min_width);
} }
box_state.set_content_width(width); box_state.set_content_width(width.value());
auto independent_formatting_context = layout_inside(box, layout_mode, box_state.available_inner_space_or_constraints_from(*m_available_space)); auto independent_formatting_context = layout_inside(box, layout_mode, box_state.available_inner_space_or_constraints_from(*m_available_space));
@ -190,28 +190,28 @@ void InlineFormattingContext::apply_justification_to_fragments(CSS::TextJustify
break; break;
} }
float excess_horizontal_space = m_available_space->width.to_px().value() - line_box.width(); CSSPixels excess_horizontal_space = m_available_space->width.to_px() - line_box.width();
// Only justify the text if the excess horizontal space is less than or // Only justify the text if the excess horizontal space is less than or
// equal to 10%, or if we are not looking at the last line box. // equal to 10%, or if we are not looking at the last line box.
if (is_last_line && excess_horizontal_space / m_available_space->width.to_px().value() > text_justification_threshold) if (is_last_line && excess_horizontal_space / m_available_space->width.to_px().value() > text_justification_threshold)
return; return;
float excess_horizontal_space_including_whitespace = excess_horizontal_space; CSSPixels excess_horizontal_space_including_whitespace = excess_horizontal_space;
size_t whitespace_count = 0; size_t whitespace_count = 0;
for (auto& fragment : line_box.fragments()) { for (auto& fragment : line_box.fragments()) {
if (fragment.is_justifiable_whitespace()) { if (fragment.is_justifiable_whitespace()) {
++whitespace_count; ++whitespace_count;
excess_horizontal_space_including_whitespace += fragment.width().value(); excess_horizontal_space_including_whitespace += fragment.width();
} }
} }
float justified_space_width = whitespace_count > 0 ? (excess_horizontal_space_including_whitespace / static_cast<float>(whitespace_count)) : 0; CSSPixels justified_space_width = whitespace_count > 0 ? (excess_horizontal_space_including_whitespace / static_cast<float>(whitespace_count)) : 0;
// This is the amount that each fragment will be offset by. If a whitespace // This is the amount that each fragment will be offset by. If a whitespace
// fragment is shorter than the justified space width, it increases to push // fragment is shorter than the justified space width, it increases to push
// subsequent fragments, and decreases to pull them back otherwise. // subsequent fragments, and decreases to pull them back otherwise.
float running_diff = 0; CSSPixels running_diff = 0;
for (size_t i = 0; i < line_box.fragments().size(); ++i) { for (size_t i = 0; i < line_box.fragments().size(); ++i) {
auto& fragment = line_box.fragments()[i]; auto& fragment = line_box.fragments()[i];
@ -221,7 +221,7 @@ void InlineFormattingContext::apply_justification_to_fragments(CSS::TextJustify
if (fragment.is_justifiable_whitespace() if (fragment.is_justifiable_whitespace()
&& fragment.width() != justified_space_width) { && fragment.width() != justified_space_width) {
running_diff += justified_space_width - fragment.width().value(); running_diff += justified_space_width - fragment.width();
fragment.set_width(justified_space_width); fragment.set_width(justified_space_width);
} }
} }
@ -315,18 +315,18 @@ void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
} }
} }
bool InlineFormattingContext::any_floats_intrude_at_y(float y) const bool InlineFormattingContext::any_floats_intrude_at_y(CSSPixels y) const
{ {
auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state); auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state).to_type<CSSPixels>();
float y_in_root = box_in_root_rect.y() + y; CSSPixels y_in_root = box_in_root_rect.y() + y;
auto space = parent().space_used_by_floats(y_in_root); auto space = parent().space_used_by_floats(y_in_root);
return space.left > 0 || space.right > 0; return space.left > 0 || space.right > 0;
} }
bool InlineFormattingContext::can_fit_new_line_at_y(float y) const bool InlineFormattingContext::can_fit_new_line_at_y(CSSPixels y) const
{ {
auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state); auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state).to_type<CSSPixels>();
float y_in_root = box_in_root_rect.y() + y; CSSPixels y_in_root = box_in_root_rect.y() + y;
auto space_top = parent().space_used_by_floats(y_in_root); auto space_top = parent().space_used_by_floats(y_in_root);
auto space_bottom = parent().space_used_by_floats(y_in_root + containing_block().line_height() - 1); auto space_bottom = parent().space_used_by_floats(y_in_root + containing_block().line_height() - 1);

View file

@ -29,10 +29,10 @@ public:
void dimension_box_on_line(Box const&, LayoutMode); void dimension_box_on_line(Box const&, LayoutMode);
float leftmost_x_offset_at(float y) const; CSSPixels leftmost_x_offset_at(CSSPixels y) const;
float available_space_for_line(float y) const; CSSPixels available_space_for_line(CSSPixels y) const;
bool any_floats_intrude_at_y(float y) const; bool any_floats_intrude_at_y(CSSPixels y) const;
bool can_fit_new_line_at_y(float y) const; bool can_fit_new_line_at_y(CSSPixels y) const;
private: private:
void generate_line_boxes(LayoutMode); void generate_line_boxes(LayoutMode);
@ -42,8 +42,8 @@ private:
Optional<AvailableSpace> m_available_space; Optional<AvailableSpace> m_available_space;
float m_automatic_content_width { 0 }; CSSPixels m_automatic_content_width { 0 };
float m_automatic_content_height { 0 }; CSSPixels m_automatic_content_height { 0 };
}; };
} }

View file

@ -110,8 +110,8 @@ float LineBuilder::y_for_float_to_be_inserted_here(Box const& box)
// FIXME: This is super dumb, we move 1px downwards per iteration and stop // FIXME: This is super dumb, we move 1px downwards per iteration and stop
// when we find an Y value where we don't collide with other floats. // when we find an Y value where we don't collide with other floats.
while (true) { while (true) {
auto space_at_y_top = m_context.available_space_for_line(candidate_y); auto space_at_y_top = m_context.available_space_for_line(candidate_y).value();
auto space_at_y_bottom = m_context.available_space_for_line(candidate_y + height); auto space_at_y_bottom = m_context.available_space_for_line(candidate_y + height).value();
if (width > space_at_y_top || width > space_at_y_bottom) { if (width > space_at_y_top || width > space_at_y_bottom) {
if (!m_context.any_floats_intrude_at_y(candidate_y) && !m_context.any_floats_intrude_at_y(candidate_y + height)) { if (!m_context.any_floats_intrude_at_y(candidate_y) && !m_context.any_floats_intrude_at_y(candidate_y + height)) {
return candidate_y; return candidate_y;
@ -154,8 +154,8 @@ void LineBuilder::update_last_line()
auto text_align = m_context.containing_block().computed_values().text_align(); auto text_align = m_context.containing_block().computed_values().text_align();
auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().line_height()); auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().line_height());
float x_offset_top = m_context.leftmost_x_offset_at(m_current_y); float x_offset_top = m_context.leftmost_x_offset_at(m_current_y).value();
float x_offset_bottom = m_context.leftmost_x_offset_at(m_current_y + current_line_height - 1); float x_offset_bottom = m_context.leftmost_x_offset_at(m_current_y + current_line_height - 1).value();
float x_offset = max(x_offset_top, x_offset_bottom); float x_offset = max(x_offset_top, x_offset_bottom);
float excess_horizontal_space = m_available_width_for_current_line - line_box.width(); float excess_horizontal_space = m_available_width_for_current_line - line_box.width();
@ -306,7 +306,7 @@ void LineBuilder::recalculate_available_space()
auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().line_height()); auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().line_height());
auto available_at_top_of_line_box = m_context.available_space_for_line(m_current_y); auto available_at_top_of_line_box = m_context.available_space_for_line(m_current_y);
auto available_at_bottom_of_line_box = m_context.available_space_for_line(m_current_y + current_line_height - 1); auto available_at_bottom_of_line_box = m_context.available_space_for_line(m_current_y + current_line_height - 1);
m_available_width_for_current_line = min(available_at_bottom_of_line_box, available_at_top_of_line_box); m_available_width_for_current_line = min(available_at_bottom_of_line_box, available_at_top_of_line_box).value();
} }
} }