1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 09:17:36 +00:00

LibWeb: Convert Layout Boxes to new pixel units

This commit is contained in:
Sam Atkins 2022-11-04 20:54:05 +00:00 committed by Linus Groh
parent c70dcaefcd
commit 65cdf89a8b
11 changed files with 29 additions and 28 deletions

View file

@ -829,7 +829,7 @@ double Element::scroll_top() const
// 9. Return the y-coordinate of the scrolling area at the alignment point with the top of the padding edge of the element.
// FIXME: Is this correct?
return block_container->scroll_offset().y();
return block_container->scroll_offset().y().value();
}
double Element::scroll_left() const
@ -871,7 +871,7 @@ double Element::scroll_left() const
// 9. Return the x-coordinate of the scrolling area at the alignment point with the left of the padding edge of the element.
// FIXME: Is this correct?
return block_container->scroll_offset().x();
return block_container->scroll_offset().x().value();
}
// https://drafts.csswg.org/cssom-view/#dom-element-scrollleft

View file

@ -27,7 +27,7 @@ bool BlockContainer::is_scrollable() const
return computed_values().overflow_y() == CSS::Overflow::Scroll;
}
void BlockContainer::set_scroll_offset(Gfx::FloatPoint offset)
void BlockContainer::set_scroll_offset(CSSPixelPoint offset)
{
// FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
if (offset.y() < 0 || m_scroll_offset == offset)

View file

@ -26,8 +26,8 @@ public:
BlockContainer const* next_sibling() const { return verify_cast<BlockContainer>(Node::next_sibling()); }
bool is_scrollable() const;
Gfx::FloatPoint scroll_offset() const { return m_scroll_offset; }
void set_scroll_offset(Gfx::FloatPoint);
CSSPixelPoint scroll_offset() const { return m_scroll_offset; }
void set_scroll_offset(CSSPixelPoint);
Painting::PaintableWithLines const* paint_box() const;
@ -36,7 +36,7 @@ public:
private:
virtual bool is_block_container() const final { return true; }
Gfx::FloatPoint m_scroll_offset;
CSSPixelPoint m_scroll_offset;
};
template<>

View file

@ -27,8 +27,8 @@ public:
bool is_body() const;
virtual Optional<float> intrinsic_width() const { return {}; }
virtual Optional<float> intrinsic_height() const { return {}; }
virtual Optional<CSSPixels> intrinsic_width() const { return {}; }
virtual Optional<CSSPixels> intrinsic_height() const { return {}; }
virtual Optional<float> intrinsic_aspect_ratio() const { return {}; }
bool has_intrinsic_width() const { return intrinsic_width().has_value(); }

View file

@ -66,7 +66,7 @@ void ImageBox::prepare_for_replaced_layout()
if (alt.is_empty())
alt = image_element.src();
float alt_text_width = 0;
CSSPixels alt_text_width = 0;
if (!m_cached_alt_text_width.has_value())
m_cached_alt_text_width = font.width(alt);
alt_text_width = m_cached_alt_text_width.value();

View file

@ -45,7 +45,7 @@ private:
ImageLoader const& m_image_loader;
Optional<float> m_cached_alt_text_width;
Optional<CSSPixels> m_cached_alt_text_width;
};
}

View file

@ -21,12 +21,12 @@ public:
const DOM::Element& dom_node() const { return verify_cast<DOM::Element>(*Node::dom_node()); }
DOM::Element& dom_node() { return verify_cast<DOM::Element>(*Node::dom_node()); }
virtual Optional<float> intrinsic_width() const final { return m_intrinsic_width; }
virtual Optional<float> intrinsic_height() const final { return m_intrinsic_height; }
virtual Optional<CSSPixels> intrinsic_width() const final { return m_intrinsic_width; }
virtual Optional<CSSPixels> intrinsic_height() const final { return m_intrinsic_height; }
virtual Optional<float> intrinsic_aspect_ratio() const final { return m_intrinsic_aspect_ratio; }
void set_intrinsic_width(Optional<float> width) { m_intrinsic_width = width; }
void set_intrinsic_height(Optional<float> height) { m_intrinsic_height = height; }
void set_intrinsic_width(Optional<CSSPixels> width) { m_intrinsic_width = width; }
void set_intrinsic_height(Optional<CSSPixels> height) { m_intrinsic_height = height; }
void set_intrinsic_aspect_ratio(Optional<float> ratio) { m_intrinsic_aspect_ratio = ratio; }
virtual void prepare_for_replaced_layout() { }
@ -36,8 +36,8 @@ public:
private:
virtual bool is_replaced_box() const final { return true; }
Optional<float> m_intrinsic_width;
Optional<float> m_intrinsic_height;
Optional<CSSPixels> m_intrinsic_width;
Optional<CSSPixels> m_intrinsic_height;
Optional<float> m_intrinsic_aspect_ratio;
};

View file

@ -37,7 +37,8 @@ float SVGGeometryBox::viewbox_scaling() const
return min(scale_width, scale_height);
}
Gfx::FloatPoint SVGGeometryBox::viewbox_origin() const
CSSPixelPoint SVGGeometryBox::viewbox_origin() const
{
auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
if (!svg_box || !svg_box->view_box().has_value())

View file

@ -22,7 +22,7 @@ public:
SVG::SVGGeometryElement const& dom_node() const { return verify_cast<SVG::SVGGeometryElement>(SVGGraphicsBox::dom_node()); }
float viewbox_scaling() const;
Gfx::FloatPoint viewbox_origin() const;
CSSPixelPoint viewbox_origin() const;
virtual RefPtr<Painting::Paintable> create_paintable() const override;

View file

@ -25,8 +25,8 @@ RefPtr<Painting::Paintable> SVGSVGBox::create_paintable() const
void SVGSVGBox::prepare_for_replaced_layout()
{
if (dom_node().has_attribute(HTML::AttributeNames::width) && dom_node().has_attribute(HTML::AttributeNames::height)) {
Optional<float> w;
Optional<float> h;
Optional<CSSPixels> w;
Optional<CSSPixels> h;
if (auto width = HTML::parse_dimension_value(dom_node().attribute(HTML::AttributeNames::width))) {
if (width->has_length())
w = width->to_length().to_px(*this);
@ -38,14 +38,14 @@ void SVGSVGBox::prepare_for_replaced_layout()
if (w.has_value() && h.has_value()) {
set_intrinsic_width(*w);
set_intrinsic_height(*h);
set_intrinsic_aspect_ratio(*w / *h);
set_intrinsic_aspect_ratio(w->value() / h->value());
return;
}
}
Optional<Gfx::FloatRect> united_rect;
Optional<CSSPixelRect> united_rect;
auto add_to_united_rect = [&](Gfx::FloatRect const& rect) {
auto add_to_united_rect = [&](CSSPixelRect const& rect) {
if (united_rect.has_value())
united_rect = united_rect->united(rect);
else
@ -55,7 +55,7 @@ void SVGSVGBox::prepare_for_replaced_layout()
for_each_in_subtree_of_type<SVGGeometryBox>([&](SVGGeometryBox const& geometry_box) {
auto& dom_node = const_cast<SVG::SVGGeometryElement&>(geometry_box.dom_node());
if (dom_node.has_attribute(HTML::AttributeNames::width) && dom_node.has_attribute(HTML::AttributeNames::height)) {
Gfx::FloatRect rect;
CSSPixelRect rect;
// FIXME: Allow for relative lengths here
rect.set_width(computed_values().width().resolved(*this, CSS::Length::make_px(0)).to_px(*this));
rect.set_height(computed_values().height().resolved(*this, CSS::Length::make_px(0)).to_px(*this));
@ -64,7 +64,7 @@ void SVGSVGBox::prepare_for_replaced_layout()
}
auto& path = dom_node.get_path();
auto path_bounding_box = path.bounding_box();
auto path_bounding_box = path.bounding_box().to_type<CSSPixels>();
// Stroke increases the path's size by stroke_width/2 per side.
auto stroke_width = geometry_box.dom_node().stroke_width().value_or(0);
@ -74,7 +74,7 @@ void SVGSVGBox::prepare_for_replaced_layout()
if (maybe_view_box.has_value()) {
auto view_box = maybe_view_box.value();
Gfx::FloatRect rect(view_box.min_x, view_box.min_y, view_box.width, view_box.height);
CSSPixelRect rect(view_box.min_x, view_box.min_y, view_box.width, view_box.height);
add_to_united_rect(rect);
return IterationDecision::Continue;
}
@ -86,7 +86,7 @@ void SVGSVGBox::prepare_for_replaced_layout()
if (united_rect.has_value()) {
set_intrinsic_width(united_rect->width());
set_intrinsic_height(united_rect->height());
set_intrinsic_aspect_ratio(united_rect->width() / united_rect->height());
set_intrinsic_aspect_ratio(united_rect->width().value() / united_rect->height().value());
}
}

View file

@ -551,7 +551,7 @@ void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
// FIXME: Handle overflow-x and overflow-y being different values.
auto clip_box = context.rounded_device_rect(absolute_padding_box_rect());
context.painter().add_clip_rect(clip_box.to_type<int>());
auto scroll_offset = static_cast<Layout::BlockContainer const&>(layout_box()).scroll_offset();
auto scroll_offset = context.rounded_device_point(static_cast<Layout::BlockContainer const&>(layout_box()).scroll_offset());
context.painter().translate(-scroll_offset.to_type<int>());
auto border_radii = normalized_border_radii_data(ShrinkRadiiForBorders::Yes);