From d13526e1e7346a95b924c45f2054e66ffe3d145e Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Sat, 15 May 2021 20:54:01 +0200 Subject: [PATCH] LibWeb: Handle background-painting of Box in seperate function --- Userland/Libraries/LibWeb/Layout/Box.cpp | 66 +++++++++++++----------- Userland/Libraries/LibWeb/Layout/Box.h | 1 + 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/Userland/Libraries/LibWeb/Layout/Box.cpp b/Userland/Libraries/LibWeb/Layout/Box.cpp index 79c26947d3..146774e918 100644 --- a/Userland/Libraries/LibWeb/Layout/Box.cpp +++ b/Userland/Libraries/LibWeb/Layout/Box.cpp @@ -27,36 +27,8 @@ void Box::paint(PaintContext& context, PaintPhase phase) auto padded_rect = this->padded_rect(); if (phase == PaintPhase::Background) { - // If the body's background properties were propagated to the root element, do no re-paint the body's background. - if (is_body() && document().html_element()->should_use_body_background_properties()) - return; - Gfx::IntRect background_rect; - - Color background_color = computed_values().background_color(); - const Gfx::Bitmap* background_image = this->background_image() ? this->background_image()->bitmap() : nullptr; - CSS::Repeat background_repeat_x = computed_values().background_repeat_x(); - CSS::Repeat background_repeat_y = computed_values().background_repeat_y(); - - if (is_root_element()) { - // CSS 2.1 Appendix E.2: If the element is a root element, paint the background over the entire canvas. - background_rect = context.viewport_rect(); - - // Section 2.11.2: If the computed value of background-image on the root element is none and its background-color is transparent, - // user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY child element. - if (document().html_element()->should_use_body_background_properties()) { - background_color = document().background_color(context.palette()); - background_image = document().background_image(); - background_repeat_x = document().background_repeat_x(); - background_repeat_y = document().background_repeat_y(); - } - } else { - background_rect = enclosing_int_rect(padded_rect); - } - - context.painter().fill_rect(background_rect, move(background_color)); - if (background_image) - paint_background_image(context, *background_image, background_repeat_x, background_repeat_y, move(background_rect)); + paint_background(context); } if (phase == PaintPhase::Border) { @@ -92,6 +64,41 @@ void Box::paint_border(PaintContext& context) Painting::paint_border(context, Painting::BorderEdge::Bottom, bordered_rect, computed_values()); } +void Box::paint_background(PaintContext& context) +{ + auto padded_rect = this->padded_rect(); + // If the body's background properties were propagated to the root element, do no re-paint the body's background. + if (is_body() && document().html_element()->should_use_body_background_properties()) + return; + + Gfx::IntRect background_rect; + + Color background_color = computed_values().background_color(); + const Gfx::Bitmap* background_image = this->background_image() ? this->background_image()->bitmap() : nullptr; + CSS::Repeat background_repeat_x = computed_values().background_repeat_x(); + CSS::Repeat background_repeat_y = computed_values().background_repeat_y(); + + if (is_root_element()) { + // CSS 2.1 Appendix E.2: If the element is a root element, paint the background over the entire canvas. + background_rect = context.viewport_rect(); + + // Section 2.11.2: If the computed value of background-image on the root element is none and its background-color is transparent, + // user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY child element. + if (document().html_element()->should_use_body_background_properties()) { + background_color = document().background_color(context.palette()); + background_image = document().background_image(); + background_repeat_x = document().background_repeat_x(); + background_repeat_y = document().background_repeat_y(); + } + } else { + background_rect = enclosing_int_rect(padded_rect); + } + + context.painter().fill_rect(background_rect, move(background_color)); + if (background_image) + paint_background_image(context, *background_image, background_repeat_x, background_repeat_y, move(background_rect)); +} + void Box::paint_background_image( PaintContext& context, const Gfx::Bitmap& background_image, @@ -226,5 +233,4 @@ float Box::width_of_logical_containing_block() const VERIFY(containing_block); return containing_block->width(); } - } diff --git a/Userland/Libraries/LibWeb/Layout/Box.h b/Userland/Libraries/LibWeb/Layout/Box.h index 4275ec3823..d502ae53ab 100644 --- a/Userland/Libraries/LibWeb/Layout/Box.h +++ b/Userland/Libraries/LibWeb/Layout/Box.h @@ -111,6 +111,7 @@ public: virtual void paint(PaintContext&, PaintPhase) override; virtual void paint_border(PaintContext& context); + virtual void paint_background(PaintContext& context); Vector& line_boxes() { return m_line_boxes; } const Vector& line_boxes() const { return m_line_boxes; }