Skip to content

Commit aae356b

Browse files
committed
LibWeb: Port inline elements to the new Paintable system
This patch adds InlinePaintable which corresponds to Layout::InlineNode.
1 parent 053766d commit aae356b

File tree

7 files changed

+184
-126
lines changed

7 files changed

+184
-126
lines changed

Userland/Libraries/LibWeb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ set(SOURCES
288288
Painting/CanvasPaintable.cpp
289289
Painting/CheckBoxPaintable.cpp
290290
Painting/ImagePaintable.cpp
291+
Painting/InlinePaintable.cpp
291292
Painting/MarkerPaintable.cpp
292293
Painting/NestedBrowsingContextPaintable.cpp
293294
Painting/PaintContext.cpp

Userland/Libraries/LibWeb/Layout/InlineNode.cpp

Lines changed: 3 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
#include <LibWeb/Layout/BlockContainer.h>
1212
#include <LibWeb/Layout/InlineFormattingContext.h>
1313
#include <LibWeb/Layout/InlineNode.h>
14-
#include <LibWeb/Painting/BackgroundPainting.h>
15-
#include <LibWeb/Painting/BorderPainting.h>
16-
#include <LibWeb/Painting/ShadowPainting.h>
14+
#include <LibWeb/Painting/InlinePaintable.h>
1715

1816
namespace Web::Layout {
1917

@@ -27,118 +25,9 @@ InlineNode::~InlineNode()
2725
{
2826
}
2927

30-
void InlineNode::paint_inline(PaintContext& context, Painting::PaintPhase phase) const
28+
OwnPtr<Painting::Paintable> InlineNode::create_paintable() const
3129
{
32-
auto& painter = context.painter();
33-
34-
if (phase == Painting::PaintPhase::Background) {
35-
auto top_left_border_radius = computed_values().border_top_left_radius();
36-
auto top_right_border_radius = computed_values().border_top_right_radius();
37-
auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
38-
auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
39-
auto containing_block_position_in_absolute_coordinates = containing_block()->paint_box()->absolute_position();
40-
41-
for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
42-
Gfx::FloatRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
43-
44-
if (is_first_fragment) {
45-
float extra_start_width = box_model().padding.left;
46-
absolute_fragment_rect.translate_by(-extra_start_width, 0);
47-
absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
48-
}
49-
50-
if (is_last_fragment) {
51-
float extra_end_width = box_model().padding.right;
52-
absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
53-
}
54-
55-
auto border_radius_data = Painting::normalized_border_radius_data(*this, absolute_fragment_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
56-
Painting::paint_background(context, *this, enclosing_int_rect(absolute_fragment_rect), computed_values().background_color(), &computed_values().background_layers(), border_radius_data);
57-
58-
if (auto computed_box_shadow = computed_values().box_shadow(); !computed_box_shadow.is_empty()) {
59-
Vector<Painting::BoxShadowData> resolved_box_shadow_data;
60-
resolved_box_shadow_data.ensure_capacity(computed_box_shadow.size());
61-
for (auto const& layer : computed_box_shadow) {
62-
resolved_box_shadow_data.empend(
63-
layer.color,
64-
static_cast<int>(layer.offset_x.to_px(*this)),
65-
static_cast<int>(layer.offset_y.to_px(*this)),
66-
static_cast<int>(layer.blur_radius.to_px(*this)),
67-
static_cast<int>(layer.spread_distance.to_px(*this)),
68-
layer.placement == CSS::BoxShadowPlacement::Outer ? Painting::BoxShadowPlacement::Outer : Painting::BoxShadowPlacement::Inner);
69-
}
70-
Painting::paint_box_shadow(context, enclosing_int_rect(absolute_fragment_rect), resolved_box_shadow_data);
71-
}
72-
73-
return IterationDecision::Continue;
74-
});
75-
}
76-
77-
if (phase == Painting::PaintPhase::Border) {
78-
auto top_left_border_radius = computed_values().border_top_left_radius();
79-
auto top_right_border_radius = computed_values().border_top_right_radius();
80-
auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
81-
auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
82-
83-
auto borders_data = Painting::BordersData {
84-
.top = computed_values().border_top(),
85-
.right = computed_values().border_right(),
86-
.bottom = computed_values().border_bottom(),
87-
.left = computed_values().border_left(),
88-
};
89-
90-
auto containing_block_position_in_absolute_coordinates = containing_block()->paint_box()->absolute_position();
91-
92-
for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
93-
Gfx::FloatRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
94-
95-
if (is_first_fragment) {
96-
float extra_start_width = box_model().padding.left;
97-
absolute_fragment_rect.translate_by(-extra_start_width, 0);
98-
absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
99-
}
100-
101-
if (is_last_fragment) {
102-
float extra_end_width = box_model().padding.right;
103-
absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
104-
}
105-
106-
auto bordered_rect = absolute_fragment_rect.inflated(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
107-
auto border_radius_data = Painting::normalized_border_radius_data(*this, bordered_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
108-
109-
Painting::paint_all_borders(context, bordered_rect, border_radius_data, borders_data);
110-
111-
return IterationDecision::Continue;
112-
});
113-
}
114-
115-
// FIXME: We check for a non-null dom_node(), since pseudo-elements have a null one and were getting
116-
// highlighted incorrectly. A better solution will be needed if we want to inspect them too.
117-
if (phase == Painting::PaintPhase::Overlay && dom_node() && document().inspected_node() == dom_node()) {
118-
// FIXME: This paints a double-thick border between adjacent fragments, where ideally there
119-
// would be none. Once we implement non-rectangular outlines for the `outline` CSS
120-
// property, we can use that here instead.
121-
for_each_fragment([&](auto& fragment, bool, bool) {
122-
painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
123-
return IterationDecision::Continue;
124-
});
125-
}
126-
}
127-
128-
template<typename Callback>
129-
void InlineNode::for_each_fragment(Callback callback) const
130-
{
131-
// FIXME: This will be slow if the containing block has a lot of fragments!
132-
Vector<LineBoxFragment const&> fragments;
133-
containing_block()->paint_box()->for_each_fragment([&](auto& fragment) {
134-
if (is_inclusive_ancestor_of(fragment.layout_node()))
135-
fragments.append(fragment);
136-
return IterationDecision::Continue;
137-
});
138-
for (size_t i = 0; i < fragments.size(); ++i) {
139-
auto const& fragment = fragments[i];
140-
callback(fragment, i == 0, i == fragments.size() - 1);
141-
}
30+
return Painting::InlinePaintable::create(*this);
14231
}
14332

14433
}

Userland/Libraries/LibWeb/Layout/InlineNode.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ class InlineNode : public NodeWithStyleAndBoxModelMetrics {
1515
InlineNode(DOM::Document&, DOM::Element*, NonnullRefPtr<CSS::StyleProperties>);
1616
virtual ~InlineNode() override;
1717

18-
void paint_inline(PaintContext&, Painting::PaintPhase) const;
19-
20-
private:
21-
template<typename Callback>
22-
void for_each_fragment(Callback) const;
18+
virtual OwnPtr<Painting::Paintable> create_paintable() const override;
2319
};
2420

2521
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibGfx/AntiAliasingPainter.h>
8+
#include <LibWeb/DOM/Document.h>
9+
#include <LibWeb/Layout/BlockContainer.h>
10+
#include <LibWeb/Layout/ImageBox.h>
11+
#include <LibWeb/Painting/BackgroundPainting.h>
12+
#include <LibWeb/Painting/InlinePaintable.h>
13+
#include <LibWeb/Painting/ShadowPainting.h>
14+
15+
namespace Web::Painting {
16+
17+
NonnullOwnPtr<InlinePaintable> InlinePaintable::create(Layout::InlineNode const& layout_node)
18+
{
19+
return adopt_own(*new InlinePaintable(layout_node));
20+
}
21+
22+
InlinePaintable::InlinePaintable(Layout::InlineNode const& layout_node)
23+
: Paintable(layout_node)
24+
{
25+
}
26+
27+
Layout::InlineNode const& InlinePaintable::layout_node() const
28+
{
29+
return static_cast<Layout::InlineNode const&>(Paintable::layout_node());
30+
}
31+
32+
void InlinePaintable::paint(PaintContext& context, Painting::PaintPhase phase) const
33+
{
34+
auto& painter = context.painter();
35+
36+
if (phase == Painting::PaintPhase::Background) {
37+
auto top_left_border_radius = computed_values().border_top_left_radius();
38+
auto top_right_border_radius = computed_values().border_top_right_radius();
39+
auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
40+
auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
41+
auto containing_block_position_in_absolute_coordinates = layout_node().containing_block()->paint_box()->absolute_position();
42+
43+
for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
44+
Gfx::FloatRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
45+
46+
if (is_first_fragment) {
47+
float extra_start_width = box_model().padding.left;
48+
absolute_fragment_rect.translate_by(-extra_start_width, 0);
49+
absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
50+
}
51+
52+
if (is_last_fragment) {
53+
float extra_end_width = box_model().padding.right;
54+
absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
55+
}
56+
57+
auto border_radius_data = Painting::normalized_border_radius_data(layout_node(), absolute_fragment_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
58+
Painting::paint_background(context, layout_node(), enclosing_int_rect(absolute_fragment_rect), computed_values().background_color(), &computed_values().background_layers(), border_radius_data);
59+
60+
if (auto computed_box_shadow = computed_values().box_shadow(); !computed_box_shadow.is_empty()) {
61+
Vector<Painting::BoxShadowData> resolved_box_shadow_data;
62+
resolved_box_shadow_data.ensure_capacity(computed_box_shadow.size());
63+
for (auto const& layer : computed_box_shadow) {
64+
resolved_box_shadow_data.empend(
65+
layer.color,
66+
static_cast<int>(layer.offset_x.to_px(layout_node())),
67+
static_cast<int>(layer.offset_y.to_px(layout_node())),
68+
static_cast<int>(layer.blur_radius.to_px(layout_node())),
69+
static_cast<int>(layer.spread_distance.to_px(layout_node())),
70+
layer.placement == CSS::BoxShadowPlacement::Outer ? Painting::BoxShadowPlacement::Outer : Painting::BoxShadowPlacement::Inner);
71+
}
72+
Painting::paint_box_shadow(context, enclosing_int_rect(absolute_fragment_rect), resolved_box_shadow_data);
73+
}
74+
75+
return IterationDecision::Continue;
76+
});
77+
}
78+
79+
if (phase == Painting::PaintPhase::Border) {
80+
auto top_left_border_radius = computed_values().border_top_left_radius();
81+
auto top_right_border_radius = computed_values().border_top_right_radius();
82+
auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
83+
auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
84+
85+
auto borders_data = Painting::BordersData {
86+
.top = computed_values().border_top(),
87+
.right = computed_values().border_right(),
88+
.bottom = computed_values().border_bottom(),
89+
.left = computed_values().border_left(),
90+
};
91+
92+
auto containing_block_position_in_absolute_coordinates = layout_node().containing_block()->paint_box()->absolute_position();
93+
94+
for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
95+
Gfx::FloatRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
96+
97+
if (is_first_fragment) {
98+
float extra_start_width = box_model().padding.left;
99+
absolute_fragment_rect.translate_by(-extra_start_width, 0);
100+
absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
101+
}
102+
103+
if (is_last_fragment) {
104+
float extra_end_width = box_model().padding.right;
105+
absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
106+
}
107+
108+
auto bordered_rect = absolute_fragment_rect.inflated(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
109+
auto border_radius_data = Painting::normalized_border_radius_data(layout_node(), bordered_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
110+
111+
Painting::paint_all_borders(context, bordered_rect, border_radius_data, borders_data);
112+
113+
return IterationDecision::Continue;
114+
});
115+
}
116+
117+
// FIXME: We check for a non-null dom_node(), since pseudo-elements have a null one and were getting
118+
// highlighted incorrectly. A better solution will be needed if we want to inspect them too.
119+
if (phase == Painting::PaintPhase::Overlay && layout_node().dom_node() && layout_node().document().inspected_node() == layout_node().dom_node()) {
120+
// FIXME: This paints a double-thick border between adjacent fragments, where ideally there
121+
// would be none. Once we implement non-rectangular outlines for the `outline` CSS
122+
// property, we can use that here instead.
123+
for_each_fragment([&](auto const& fragment, bool, bool) {
124+
painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
125+
return IterationDecision::Continue;
126+
});
127+
}
128+
}
129+
130+
template<typename Callback>
131+
void InlinePaintable::for_each_fragment(Callback callback) const
132+
{
133+
// FIXME: This will be slow if the containing block has a lot of fragments!
134+
Vector<Layout::LineBoxFragment const&> fragments;
135+
layout_node().containing_block()->paint_box()->for_each_fragment([&](auto& fragment) {
136+
if (layout_node().is_inclusive_ancestor_of(fragment.layout_node()))
137+
fragments.append(fragment);
138+
return IterationDecision::Continue;
139+
});
140+
for (size_t i = 0; i < fragments.size(); ++i) {
141+
auto const& fragment = fragments[i];
142+
callback(fragment, i == 0, i == fragments.size() - 1);
143+
}
144+
}
145+
146+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibWeb/Layout/InlineNode.h>
10+
#include <LibWeb/Painting/Paintable.h>
11+
12+
namespace Web::Painting {
13+
14+
class InlinePaintable final : public Paintable {
15+
public:
16+
static NonnullOwnPtr<InlinePaintable> create(Layout::InlineNode const&);
17+
18+
virtual void paint(PaintContext&, PaintPhase) const override;
19+
20+
Layout::InlineNode const& layout_node() const;
21+
auto const& box_model() const { return layout_node().box_model(); }
22+
23+
private:
24+
InlinePaintable(Layout::InlineNode const&);
25+
26+
template<typename Callback>
27+
void for_each_fragment(Callback) const;
28+
};
29+
30+
}

Userland/Libraries/LibWeb/Painting/Paintable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Paintable {
2525
virtual void after_children_paint(PaintContext&, PaintPhase) const { }
2626

2727
Layout::Node const& layout_node() const { return m_layout_node; }
28+
auto const& computed_values() const { return m_layout_node.computed_values(); }
2829

2930
protected:
3031
explicit Paintable(Layout::Node const& layout_node)
@@ -48,7 +49,6 @@ class PaintableBox : public Paintable {
4849
Layout::Box const& layout_box() const { return static_cast<Layout::Box const&>(Paintable::layout_node()); }
4950

5051
auto const& box_model() const { return layout_box().box_model(); }
51-
auto const& computed_values() const { return layout_box().computed_values(); }
5252

5353
struct OverflowData {
5454
Gfx::FloatRect scrollable_overflow_rect;

Userland/Libraries/LibWeb/Painting/StackingContext.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <LibGfx/Painter.h>
1010
#include <LibWeb/Layout/Box.h>
1111
#include <LibWeb/Layout/InitialContainingBlock.h>
12-
#include <LibWeb/Layout/InlineNode.h>
1312
#include <LibWeb/Layout/ReplacedBox.h>
1413
#include <LibWeb/Painting/Paintable.h>
1514
#include <LibWeb/Painting/StackingContext.h>
@@ -18,11 +17,8 @@ namespace Web::Painting {
1817

1918
static void paint_node(Layout::Node const& layout_node, PaintContext& context, PaintPhase phase)
2019
{
21-
// FIXME: This whole thing is hairy. Find a nicer solution for painting InlineNode.
22-
if (layout_node.is_box())
23-
static_cast<Layout::Box const&>(layout_node).paint_box()->paint(context, phase);
24-
else if (is<Layout::InlineNode>(layout_node))
25-
static_cast<Layout::InlineNode const&>(layout_node).paint_inline(context, phase);
20+
if (auto const* paintable = layout_node.paintable())
21+
paintable->paint(context, phase);
2622
}
2723

2824
StackingContext::StackingContext(Layout::Box& box, StackingContext* parent)

0 commit comments

Comments
 (0)