|
| 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 | +} |
0 commit comments