2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-09-19 13:16:00 -03:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-12-03 15:21:33 -03:00
|
|
|
#include <LibGfx/Painter.h>
|
2021-08-19 08:20:43 -03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/DOM/Element.h>
|
2021-10-06 15:02:41 -03:00
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
2020-12-05 16:10:39 -03:00
|
|
|
#include <LibWeb/Layout/InlineFormattingContext.h>
|
2020-11-22 11:53:01 -03:00
|
|
|
#include <LibWeb/Layout/InlineNode.h>
|
2021-09-19 13:16:00 -03:00
|
|
|
#include <LibWeb/Painting/BackgroundPainting.h>
|
|
|
|
|
#include <LibWeb/Painting/BorderPainting.h>
|
2021-09-19 15:49:00 -03:00
|
|
|
#include <LibWeb/Painting/ShadowPainting.h>
|
2019-10-17 18:09:41 -03:00
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
namespace Web::Layout {
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
InlineNode::InlineNode(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
|
|
|
|
|
: Layout::NodeWithStyleAndBoxModelMetrics(document, &element, move(style))
|
2019-10-17 18:09:41 -03:00
|
|
|
{
|
2020-11-22 11:53:01 -03:00
|
|
|
set_inline(true);
|
2019-10-17 18:09:41 -03:00
|
|
|
}
|
|
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
InlineNode::~InlineNode()
|
2019-10-17 18:09:41 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 17:31:08 -03:00
|
|
|
void InlineNode::paint(PaintContext& context, PaintPhase phase)
|
2020-12-03 15:21:33 -03:00
|
|
|
{
|
|
|
|
|
auto& painter = context.painter();
|
|
|
|
|
|
|
|
|
|
if (phase == PaintPhase::Background) {
|
2021-09-19 13:16:00 -03:00
|
|
|
auto top_left_border_radius = computed_values().border_top_left_radius();
|
|
|
|
|
auto top_right_border_radius = computed_values().border_top_right_radius();
|
|
|
|
|
auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
|
|
|
|
|
auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
|
|
|
|
|
|
|
|
|
|
for_each_fragment([&](auto& fragment) {
|
|
|
|
|
// FIXME: This recalculates our (InlineNode's) absolute_rect() for every single fragment!
|
|
|
|
|
auto rect = fragment.absolute_rect();
|
|
|
|
|
auto border_radius_data = Painting::normalized_border_radius_data(*this, rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
|
2021-11-12 17:12:56 -03:00
|
|
|
Painting::paint_background(context, *this, enclosing_int_rect(rect), computed_values().background_color(), &computed_values().background_layers(), border_radius_data);
|
2021-09-19 15:49:00 -03:00
|
|
|
|
|
|
|
|
if (auto computed_box_shadow = computed_values().box_shadow(); computed_box_shadow.has_value()) {
|
|
|
|
|
auto box_shadow_data = Painting::BoxShadowData {
|
2022-01-19 14:00:50 -03:00
|
|
|
.offset_x = (int)computed_box_shadow->offset_x.resolved_or_zero(*this).to_px(*this),
|
|
|
|
|
.offset_y = (int)computed_box_shadow->offset_y.resolved_or_zero(*this).to_px(*this),
|
|
|
|
|
.blur_radius = (int)computed_box_shadow->blur_radius.resolved_or_zero(*this).to_px(*this),
|
2021-09-19 15:49:00 -03:00
|
|
|
.color = computed_box_shadow->color
|
|
|
|
|
};
|
|
|
|
|
Painting::paint_box_shadow(context, enclosing_int_rect(rect), box_shadow_data);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 17:31:08 -03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2020-12-03 15:21:33 -03:00
|
|
|
}
|
2021-08-19 08:20:43 -03:00
|
|
|
|
2021-09-19 16:49:23 -03:00
|
|
|
if (phase == PaintPhase::Border) {
|
|
|
|
|
auto top_left_border_radius = computed_values().border_top_left_radius();
|
|
|
|
|
auto top_right_border_radius = computed_values().border_top_right_radius();
|
|
|
|
|
auto bottom_right_border_radius = computed_values().border_bottom_right_radius();
|
|
|
|
|
auto bottom_left_border_radius = computed_values().border_bottom_left_radius();
|
|
|
|
|
|
|
|
|
|
auto borders_data = Painting::BordersData {
|
|
|
|
|
.top = computed_values().border_top(),
|
|
|
|
|
.right = computed_values().border_right(),
|
|
|
|
|
.bottom = computed_values().border_bottom(),
|
|
|
|
|
.left = computed_values().border_left(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for_each_fragment([&](auto& fragment) {
|
|
|
|
|
// FIXME: This recalculates our (InlineNode's) absolute_rect() for every single fragment!
|
|
|
|
|
auto bordered_rect = fragment.absolute_rect();
|
|
|
|
|
bordered_rect.inflate(borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
Painting::paint_all_borders(context, bordered_rect, border_radius_data, borders_data);
|
|
|
|
|
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 08:20:43 -03:00
|
|
|
if (phase == PaintPhase::Foreground && document().inspected_node() == dom_node()) {
|
|
|
|
|
// FIXME: This paints a double-thick border between adjacent fragments, where ideally there
|
|
|
|
|
// would be none. Once we implement non-rectangular outlines for the `outline` CSS
|
|
|
|
|
// property, we can use that here instead.
|
2021-09-19 13:16:00 -03:00
|
|
|
for_each_fragment([&](auto& fragment) {
|
|
|
|
|
painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
|
2021-08-19 08:20:43 -03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 13:16:00 -03:00
|
|
|
template<typename Callback>
|
|
|
|
|
void InlineNode::for_each_fragment(Callback callback)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: This will be slow if the containing block has a lot of fragments!
|
|
|
|
|
containing_block()->for_each_fragment([&](auto& fragment) {
|
|
|
|
|
if (!is_inclusive_ancestor_of(fragment.layout_node()))
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
// FIXME: This skips the 0-width fragments at the start and end of the InlineNode.
|
|
|
|
|
// A better solution would be to not generate them in the first place.
|
|
|
|
|
if (fragment.width() == 0 || fragment.height() == 0)
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
return callback(fragment);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|