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

LibWeb: Turn BoxModelMetrics into a simple struct

Using getters for this thing was just cumbersome and didn't achieve
anything of value so just turn it into a plain struct.
This commit is contained in:
Andreas Kling 2020-06-24 11:22:34 +02:00
parent 5744dd43c5
commit f742b245b7
5 changed files with 115 additions and 136 deletions

View file

@ -123,23 +123,23 @@ void dump_tree(const LayoutNode& layout_node)
// Dump the horizontal box properties
dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
layout_box.box_model().margin().left.to_px(layout_box),
layout_box.box_model().border().left.to_px(layout_box),
layout_box.box_model().padding().left.to_px(layout_box),
layout_box.box_model().margin.left.to_px(layout_box),
layout_box.box_model().border.left.to_px(layout_box),
layout_box.box_model().padding.left.to_px(layout_box),
layout_box.width(),
layout_box.box_model().padding().right.to_px(layout_box),
layout_box.box_model().border().right.to_px(layout_box),
layout_box.box_model().margin().right.to_px(layout_box));
layout_box.box_model().padding.right.to_px(layout_box),
layout_box.box_model().border.right.to_px(layout_box),
layout_box.box_model().margin.right.to_px(layout_box));
// And the vertical box properties
dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
layout_box.box_model().margin().top.to_px(layout_box),
layout_box.box_model().border().top.to_px(layout_box),
layout_box.box_model().padding().top.to_px(layout_box),
layout_box.box_model().margin.top.to_px(layout_box),
layout_box.box_model().border.top.to_px(layout_box),
layout_box.box_model().padding.top.to_px(layout_box),
layout_box.height(),
layout_box.box_model().padding().bottom.to_px(layout_box),
layout_box.box_model().border().bottom.to_px(layout_box),
layout_box.box_model().margin().bottom.to_px(layout_box));
layout_box.box_model().padding.bottom.to_px(layout_box),
layout_box.box_model().border.bottom.to_px(layout_box),
layout_box.box_model().margin.bottom.to_px(layout_box));
dbgprintf("\n");
}

View file

@ -28,41 +28,33 @@
namespace Web {
BoxModelMetrics::BoxModelMetrics()
{
}
BoxModelMetrics::~BoxModelMetrics()
{
}
BoxModelMetrics::PixelBox BoxModelMetrics::margin_box(const LayoutNode& layout_node) const
PixelBox BoxModelMetrics::margin_box(const LayoutNode& layout_node) const
{
return {
m_margin.top.to_px(layout_node) + m_border.top.to_px(layout_node) + m_padding.top.to_px(layout_node),
m_margin.right.to_px(layout_node) + m_border.right.to_px(layout_node) + m_padding.right.to_px(layout_node),
m_margin.bottom.to_px(layout_node) + m_border.bottom.to_px(layout_node) + m_padding.bottom.to_px(layout_node),
m_margin.left.to_px(layout_node) + m_border.left.to_px(layout_node) + m_padding.left.to_px(layout_node),
margin.top.to_px(layout_node) + border.top.to_px(layout_node) + padding.top.to_px(layout_node),
margin.right.to_px(layout_node) + border.right.to_px(layout_node) + padding.right.to_px(layout_node),
margin.bottom.to_px(layout_node) + border.bottom.to_px(layout_node) + padding.bottom.to_px(layout_node),
margin.left.to_px(layout_node) + border.left.to_px(layout_node) + padding.left.to_px(layout_node),
};
}
BoxModelMetrics::PixelBox BoxModelMetrics::padding_box(const LayoutNode& layout_node) const
PixelBox BoxModelMetrics::padding_box(const LayoutNode& layout_node) const
{
return {
m_padding.top.to_px(layout_node),
m_padding.right.to_px(layout_node),
m_padding.bottom.to_px(layout_node),
m_padding.left.to_px(layout_node),
padding.top.to_px(layout_node),
padding.right.to_px(layout_node),
padding.bottom.to_px(layout_node),
padding.left.to_px(layout_node),
};
}
BoxModelMetrics::PixelBox BoxModelMetrics::border_box(const LayoutNode& layout_node) const
PixelBox BoxModelMetrics::border_box(const LayoutNode& layout_node) const
{
return {
m_border.top.to_px(layout_node) + m_padding.top.to_px(layout_node),
m_border.right.to_px(layout_node) + m_padding.right.to_px(layout_node),
m_border.bottom.to_px(layout_node) + m_padding.bottom.to_px(layout_node),
m_border.left.to_px(layout_node) + m_padding.left.to_px(layout_node),
border.top.to_px(layout_node) + padding.top.to_px(layout_node),
border.right.to_px(layout_node) + padding.right.to_px(layout_node),
border.bottom.to_px(layout_node) + padding.bottom.to_px(layout_node),
border.left.to_px(layout_node) + padding.left.to_px(layout_node),
};
}

View file

@ -31,37 +31,24 @@
namespace Web {
class BoxModelMetrics {
public:
BoxModelMetrics();
~BoxModelMetrics();
LengthBox& margin() { return m_margin; }
LengthBox& padding() { return m_padding; }
LengthBox& border() { return m_border; }
LengthBox& offset() { return m_offset; }
const LengthBox& margin() const { return m_margin; }
const LengthBox& padding() const { return m_padding; }
const LengthBox& border() const { return m_border; }
const LengthBox& offset() const { return m_offset; }
struct PixelBox {
struct PixelBox {
float top;
float right;
float bottom;
float left;
};
};
struct BoxModelMetrics {
public:
LengthBox margin;
LengthBox padding;
LengthBox border;
LengthBox offset;
PixelBox margin_box(const LayoutNode&) const;
PixelBox padding_box(const LayoutNode&) const;
PixelBox border_box(const LayoutNode&) const;
private:
LengthBox m_margin;
LengthBox m_padding;
LengthBox m_border;
LengthBox m_offset;
};
}

View file

@ -74,53 +74,53 @@ void LayoutBlock::layout_absolutely_positioned_descendant(LayoutBox& box)
auto specified_width = style.length_or_fallback(CSS::PropertyID::Width, Length::make_auto(), width());
box_model.margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, Length::make_auto(), height());
box_model.margin().right = style.length_or_fallback(CSS::PropertyID::MarginRight, Length::make_auto(), width());
box_model.margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, Length::make_auto(), height());
box_model.margin().left = style.length_or_fallback(CSS::PropertyID::MarginLeft, Length::make_auto(), width());
box_model.margin.top = style.length_or_fallback(CSS::PropertyID::MarginTop, Length::make_auto(), height());
box_model.margin.right = style.length_or_fallback(CSS::PropertyID::MarginRight, Length::make_auto(), width());
box_model.margin.bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, Length::make_auto(), height());
box_model.margin.left = style.length_or_fallback(CSS::PropertyID::MarginLeft, Length::make_auto(), width());
box_model.offset().top = style.length_or_fallback(CSS::PropertyID::Top, Length::make_auto(), height());
box_model.offset().right = style.length_or_fallback(CSS::PropertyID::Right, Length::make_auto(), width());
box_model.offset().bottom = style.length_or_fallback(CSS::PropertyID::Bottom, Length::make_auto(), height());
box_model.offset().left = style.length_or_fallback(CSS::PropertyID::Left, Length::make_auto(), width());
box_model.offset.top = style.length_or_fallback(CSS::PropertyID::Top, Length::make_auto(), height());
box_model.offset.right = style.length_or_fallback(CSS::PropertyID::Right, Length::make_auto(), width());
box_model.offset.bottom = style.length_or_fallback(CSS::PropertyID::Bottom, Length::make_auto(), height());
box_model.offset.left = style.length_or_fallback(CSS::PropertyID::Left, Length::make_auto(), width());
if (box_model.offset().left.is_auto() && specified_width.is_auto() && box_model.offset().right.is_auto()) {
if (box_model.margin().left.is_auto())
box_model.margin().left = zero_value;
if (box_model.margin().right.is_auto())
box_model.margin().right = zero_value;
if (box_model.offset.left.is_auto() && specified_width.is_auto() && box_model.offset.right.is_auto()) {
if (box_model.margin.left.is_auto())
box_model.margin.left = zero_value;
if (box_model.margin.right.is_auto())
box_model.margin.right = zero_value;
}
Gfx::FloatPoint used_offset;
float x_offset = box_model.offset().left.to_px(box)
float x_offset = box_model.offset.left.to_px(box)
+ box_model.border_box(box).left
- box_model.offset().right.to_px(box)
- box_model.offset.right.to_px(box)
- box_model.border_box(box).right;
float y_offset = box_model.offset().top.to_px(box)
float y_offset = box_model.offset.top.to_px(box)
+ box_model.border_box(box).top
- box_model.offset().bottom.to_px(box)
- box_model.offset.bottom.to_px(box)
- box_model.border_box(box).bottom;
bool has_left_side_constraints = !box_model.offset().left.is_auto() || !box_model.margin().left.is_auto();
bool has_right_side_constraints = !box_model.offset().right.is_auto() || !box_model.margin().right.is_auto();
bool has_left_side_constraints = !box_model.offset.left.is_auto() || !box_model.margin.left.is_auto();
bool has_right_side_constraints = !box_model.offset.right.is_auto() || !box_model.margin.right.is_auto();
if (has_left_side_constraints && has_right_side_constraints) {
// If both 'left' and 'right' are set, we will have stretched the width to accomodate both.
x_offset += box_model.offset().right.to_px(box);
x_offset += box_model.offset.right.to_px(box);
}
if (has_left_side_constraints) {
used_offset.set_x(x_offset + box_model.margin().left.to_px(box));
used_offset.set_x(x_offset + box_model.margin.left.to_px(box));
} else if (has_right_side_constraints) {
used_offset.set_x(width() + x_offset - box.width() - box_model.margin().right.to_px(box));
used_offset.set_x(width() + x_offset - box.width() - box_model.margin.right.to_px(box));
}
if (!box_model.offset().top.is_auto()) {
used_offset.set_y(y_offset + box_model.margin().top.to_px(box));
} else if (!box_model.offset().bottom.is_auto()) {
used_offset.set_y(height() + y_offset - box.height() - box_model.margin().bottom.to_px(box));
if (!box_model.offset.top.is_auto()) {
used_offset.set_y(y_offset + box_model.margin.top.to_px(box));
} else if (!box_model.offset.bottom.is_auto()) {
used_offset.set_y(height() + y_offset - box.height() - box_model.margin.bottom.to_px(box));
}
box.set_offset(used_offset);
@ -411,12 +411,12 @@ void LayoutBlock::compute_width_for_absolutely_positioned_block()
set_width(used_width.to_px(*this));
box_model().margin().left = margin_left;
box_model().margin().right = margin_right;
box_model().border().left = border_left;
box_model().border().right = border_right;
box_model().padding().left = padding_left;
box_model().padding().right = padding_right;
box_model().margin.left = margin_left;
box_model().margin.right = margin_right;
box_model().border.left = border_left;
box_model().border.right = border_right;
box_model().padding.left = padding_left;
box_model().padding.right = padding_right;
}
void LayoutBlock::compute_width()
@ -550,12 +550,12 @@ void LayoutBlock::compute_width()
}
set_width(used_width.to_px(*this));
box_model().margin().left = margin_left;
box_model().margin().right = margin_right;
box_model().border().left = border_left;
box_model().border().right = border_right;
box_model().padding().left = padding_left;
box_model().padding().right = padding_right;
box_model().margin.left = margin_left;
box_model().margin.right = margin_right;
box_model().border.left = border_left;
box_model().border.right = border_right;
box_model().padding.left = padding_left;
box_model().padding.right = padding_right;
}
void LayoutBlock::place_block_level_replaced_element_in_normal_flow(LayoutReplaced& box)
@ -566,19 +566,19 @@ void LayoutBlock::place_block_level_replaced_element_in_normal_flow(LayoutReplac
auto& containing_block = *this;
auto& replaced_element_box_model = box.box_model();
replaced_element_box_model.margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value, containing_block.width());
replaced_element_box_model.margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value, containing_block.width());
replaced_element_box_model.border().top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
replaced_element_box_model.border().bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
replaced_element_box_model.padding().top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value, containing_block.width());
replaced_element_box_model.padding().bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value, containing_block.width());
replaced_element_box_model.margin.top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value, containing_block.width());
replaced_element_box_model.margin.bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value, containing_block.width());
replaced_element_box_model.border.top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
replaced_element_box_model.border.bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
replaced_element_box_model.padding.top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value, containing_block.width());
replaced_element_box_model.padding.bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value, containing_block.width());
float x = replaced_element_box_model.margin().left.to_px(*this)
+ replaced_element_box_model.border().left.to_px(*this)
+ replaced_element_box_model.padding().left.to_px(*this)
+ replaced_element_box_model.offset().left.to_px(*this);
float x = replaced_element_box_model.margin.left.to_px(*this)
+ replaced_element_box_model.border.left.to_px(*this)
+ replaced_element_box_model.padding.left.to_px(*this)
+ replaced_element_box_model.offset.left.to_px(*this);
float y = replaced_element_box_model.margin_box(*this).top + box_model().offset().top.to_px(*this);
float y = replaced_element_box_model.margin_box(*this).top + box_model().offset.top.to_px(*this);
box.set_offset(x, y);
}
@ -621,24 +621,24 @@ void LayoutBlock::place_block_level_non_replaced_element_in_normal_flow(LayoutBl
auto& containing_block = *this;
auto& box = block.box_model();
box.margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value, containing_block.width());
box.margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value, containing_block.width());
box.border().top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
box.border().bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
box.padding().top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value, containing_block.width());
box.padding().bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value, containing_block.width());
box.margin.top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value, containing_block.width());
box.margin.bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value, containing_block.width());
box.border.top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
box.border.bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
box.padding.top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value, containing_block.width());
box.padding.bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value, containing_block.width());
float x = box.margin().left.to_px(*this)
+ box.border().left.to_px(*this)
+ box.padding().left.to_px(*this)
+ box.offset().left.to_px(*this);
float x = box.margin.left.to_px(*this)
+ box.border.left.to_px(*this)
+ box.padding.left.to_px(*this)
+ box.offset.left.to_px(*this);
if (text_align() == CSS::TextAlign::VendorSpecificCenter) {
x = (containing_block.width() / 2) - block.width() / 2;
}
float y = box.margin_box(*this).top
+ box.offset().top.to_px(*this);
+ box.offset.top.to_px(*this);
auto* relevant_sibling = block.previous_sibling();
while (relevant_sibling != nullptr) {
@ -652,8 +652,8 @@ void LayoutBlock::place_block_level_non_replaced_element_in_normal_flow(LayoutBl
y += relevant_sibling->effective_offset().y() + relevant_sibling->height();
// Collapse top margin with bottom margin of previous sibling if necessary
float previous_sibling_margin_bottom = sibling_box.margin().bottom.to_px(*relevant_sibling);
float my_margin_top = box.margin().top.to_px(*this);
float previous_sibling_margin_bottom = sibling_box.margin.bottom.to_px(*relevant_sibling);
float my_margin_top = box.margin.top.to_px(*this);
if (my_margin_top < 0 || previous_sibling_margin_bottom < 0) {
// Negative margins present.
@ -679,12 +679,12 @@ void LayoutBlock::compute_height()
auto& containing_block = *this->containing_block();
box_model().margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, Length::make_px(0), containing_block.width());
box_model().margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, Length::make_px(0), containing_block.width());
box_model().border().top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, Length::make_px(0));
box_model().border().bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, Length::make_px(0));
box_model().padding().top = style.length_or_fallback(CSS::PropertyID::PaddingTop, Length::make_px(0), containing_block.width());
box_model().padding().bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, Length::make_px(0), containing_block.width());
box_model().margin.top = style.length_or_fallback(CSS::PropertyID::MarginTop, Length::make_px(0), containing_block.width());
box_model().margin.bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, Length::make_px(0), containing_block.width());
box_model().border.top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, Length::make_px(0));
box_model().border.bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, Length::make_px(0));
box_model().padding.top = style.length_or_fallback(CSS::PropertyID::PaddingTop, Length::make_px(0), containing_block.width());
box_model().padding.bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, Length::make_px(0), containing_block.width());
if (!specified_height.is_auto()) {
float used_height = specified_height.to_px(*this);

View file

@ -196,10 +196,10 @@ void LayoutBox::paint(PaintContext& context, PaintPhase phase)
context.painter().translate(context.scroll_offset());
Gfx::FloatRect padded_rect;
padded_rect.set_x(absolute_x() - box_model().padding().left.to_px(*this));
padded_rect.set_width(width() + box_model().padding().left.to_px(*this) + box_model().padding().right.to_px(*this));
padded_rect.set_y(absolute_y() - box_model().padding().top.to_px(*this));
padded_rect.set_height(height() + box_model().padding().top.to_px(*this) + box_model().padding().bottom.to_px(*this));
padded_rect.set_x(absolute_x() - box_model().padding.left.to_px(*this));
padded_rect.set_width(width() + box_model().padding.left.to_px(*this) + box_model().padding.right.to_px(*this));
padded_rect.set_y(absolute_y() - box_model().padding.top.to_px(*this));
padded_rect.set_height(height() + box_model().padding.top.to_px(*this) + box_model().padding.bottom.to_px(*this));
if (phase == PaintPhase::Background && !is_body()) {
// FIXME: We should paint the body here too, but that currently happens at the view layer.
@ -219,10 +219,10 @@ void LayoutBox::paint(PaintContext& context, PaintPhase phase)
if (phase == PaintPhase::Border) {
Gfx::FloatRect bordered_rect;
bordered_rect.set_x(padded_rect.x() - box_model().border().left.to_px(*this));
bordered_rect.set_width(padded_rect.width() + box_model().border().left.to_px(*this) + box_model().border().right.to_px(*this));
bordered_rect.set_y(padded_rect.y() - box_model().border().top.to_px(*this));
bordered_rect.set_height(padded_rect.height() + box_model().border().top.to_px(*this) + box_model().border().bottom.to_px(*this));
bordered_rect.set_x(padded_rect.x() - box_model().border.left.to_px(*this));
bordered_rect.set_width(padded_rect.width() + box_model().border.left.to_px(*this) + box_model().border.right.to_px(*this));
bordered_rect.set_y(padded_rect.y() - box_model().border.top.to_px(*this));
bordered_rect.set_height(padded_rect.height() + box_model().border.top.to_px(*this) + box_model().border.bottom.to_px(*this));
paint_border(context, Edge::Left, bordered_rect, CSS::PropertyID::BorderLeftStyle, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftWidth);
paint_border(context, Edge::Right, bordered_rect, CSS::PropertyID::BorderRightStyle, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightWidth);