2022-03-10 23:13:37 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-06-12 15:05:47 +01:00
|
|
|
#include <LibWeb/Painting/BorderPainting.h>
|
2022-07-04 20:49:38 +01:00
|
|
|
#include <LibWeb/Painting/BorderRadiusCornerClipper.h>
|
2022-03-10 23:13:37 +01:00
|
|
|
#include <LibWeb/Painting/Paintable.h>
|
2024-01-12 21:25:05 +01:00
|
|
|
#include <LibWeb/Painting/PaintableFragment.h>
|
2022-09-25 15:13:31 +01:00
|
|
|
#include <LibWeb/Painting/ShadowPainting.h>
|
2022-03-10 23:13:37 +01:00
|
|
|
|
|
|
|
namespace Web::Painting {
|
|
|
|
|
|
|
|
class PaintableBox : public Paintable {
|
2023-01-11 12:51:49 +01:00
|
|
|
JS_CELL(PaintableBox, Paintable);
|
|
|
|
|
2022-03-10 23:13:37 +01:00
|
|
|
public:
|
2023-01-11 12:51:49 +01:00
|
|
|
static JS::NonnullGCPtr<PaintableBox> create(Layout::Box const&);
|
2022-03-10 23:13:37 +01:00
|
|
|
virtual ~PaintableBox();
|
|
|
|
|
2023-08-31 17:08:45 -05:00
|
|
|
virtual void before_paint(PaintContext&, PaintPhase) const override;
|
|
|
|
virtual void after_paint(PaintContext&, PaintPhase) const override;
|
|
|
|
|
2022-03-10 23:13:37 +01:00
|
|
|
virtual void paint(PaintContext&, PaintPhase) const override;
|
|
|
|
|
2023-09-10 14:10:55 +01:00
|
|
|
virtual Optional<CSSPixelRect> get_masking_area() const { return {}; }
|
2023-10-15 04:27:48 +02:00
|
|
|
virtual Optional<Gfx::Bitmap::MaskKind> get_mask_type() const { return {}; }
|
|
|
|
virtual RefPtr<Gfx::Bitmap> calculate_mask(PaintContext&, CSSPixelRect const&) const { return {}; }
|
2023-09-10 14:10:55 +01:00
|
|
|
|
2022-03-10 23:13:37 +01:00
|
|
|
Layout::Box& layout_box() { return static_cast<Layout::Box&>(Paintable::layout_node()); }
|
|
|
|
Layout::Box const& layout_box() const { return static_cast<Layout::Box const&>(Paintable::layout_node()); }
|
|
|
|
|
|
|
|
auto const& box_model() const { return layout_box().box_model(); }
|
|
|
|
|
|
|
|
struct OverflowData {
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixelRect scrollable_overflow_rect;
|
2023-07-12 18:55:23 +02:00
|
|
|
bool has_scrollable_overflow { false };
|
|
|
|
CSSPixelPoint scroll_offset {};
|
2022-03-10 23:13:37 +01:00
|
|
|
};
|
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixelRect absolute_rect() const;
|
2023-08-15 15:42:38 +02:00
|
|
|
|
|
|
|
// Offset from the top left of the containing block's content edge.
|
|
|
|
[[nodiscard]] CSSPixelPoint offset() const;
|
2022-03-10 23:13:37 +01:00
|
|
|
|
2023-08-06 20:51:46 +02:00
|
|
|
CSSPixelPoint scroll_offset() const;
|
2023-08-06 21:10:15 +02:00
|
|
|
void set_scroll_offset(CSSPixelPoint);
|
2023-08-06 20:29:29 +02:00
|
|
|
void scroll_by(int delta_x, int delta_y);
|
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
void set_offset(CSSPixelPoint);
|
2022-03-10 23:13:37 +01:00
|
|
|
void set_offset(float x, float y) { set_offset({ x, y }); }
|
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixelSize const& content_size() const { return m_content_size; }
|
|
|
|
void set_content_size(CSSPixelSize);
|
|
|
|
void set_content_size(CSSPixels width, CSSPixels height) { set_content_size({ width, height }); }
|
2022-03-10 23:13:37 +01:00
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
void set_content_width(CSSPixels width) { set_content_size(width, content_height()); }
|
|
|
|
void set_content_height(CSSPixels height) { set_content_size(content_width(), height); }
|
|
|
|
CSSPixels content_width() const { return m_content_size.width(); }
|
|
|
|
CSSPixels content_height() const { return m_content_size.height(); }
|
2022-03-10 23:13:37 +01:00
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixelRect absolute_padding_box_rect() const
|
2022-03-10 23:13:37 +01:00
|
|
|
{
|
|
|
|
auto absolute_rect = this->absolute_rect();
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixelRect rect;
|
2022-03-10 23:13:37 +01:00
|
|
|
rect.set_x(absolute_rect.x() - box_model().padding.left);
|
|
|
|
rect.set_width(content_width() + box_model().padding.left + box_model().padding.right);
|
|
|
|
rect.set_y(absolute_rect.y() - box_model().padding.top);
|
|
|
|
rect.set_height(content_height() + box_model().padding.top + box_model().padding.bottom);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixelRect absolute_border_box_rect() const
|
2022-03-10 23:13:37 +01:00
|
|
|
{
|
|
|
|
auto padded_rect = this->absolute_padding_box_rect();
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixelRect rect;
|
2023-07-07 05:31:54 +00:00
|
|
|
auto use_collapsing_borders_model = override_borders_data().has_value();
|
|
|
|
// Implement the collapsing border model https://www.w3.org/TR/CSS22/tables.html#collapsing-borders.
|
|
|
|
auto border_top = use_collapsing_borders_model ? round(box_model().border.top / 2) : box_model().border.top;
|
|
|
|
auto border_bottom = use_collapsing_borders_model ? round(box_model().border.bottom / 2) : box_model().border.bottom;
|
|
|
|
auto border_left = use_collapsing_borders_model ? round(box_model().border.left / 2) : box_model().border.left;
|
|
|
|
auto border_right = use_collapsing_borders_model ? round(box_model().border.right / 2) : box_model().border.right;
|
|
|
|
rect.set_x(padded_rect.x() - border_left);
|
|
|
|
rect.set_width(padded_rect.width() + border_left + border_right);
|
|
|
|
rect.set_y(padded_rect.y() - border_top);
|
|
|
|
rect.set_height(padded_rect.height() + border_top + border_bottom);
|
2022-03-10 23:13:37 +01:00
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixelRect absolute_paint_rect() const;
|
2022-09-25 15:13:31 +01:00
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixels border_box_width() const
|
2022-03-10 23:13:37 +01:00
|
|
|
{
|
|
|
|
auto border_box = box_model().border_box();
|
|
|
|
return content_width() + border_box.left + border_box.right;
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixels border_box_height() const
|
2022-03-10 23:13:37 +01:00
|
|
|
{
|
|
|
|
auto border_box = box_model().border_box();
|
|
|
|
return content_height() + border_box.top + border_box.bottom;
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixels absolute_x() const { return absolute_rect().x(); }
|
|
|
|
CSSPixels absolute_y() const { return absolute_rect().y(); }
|
|
|
|
CSSPixelPoint absolute_position() const { return absolute_rect().location(); }
|
2022-03-10 23:13:37 +01:00
|
|
|
|
2023-07-12 18:55:23 +02:00
|
|
|
[[nodiscard]] bool has_scrollable_overflow() const { return m_overflow_data->has_scrollable_overflow; }
|
2022-03-10 23:13:37 +01:00
|
|
|
|
2023-07-12 18:55:23 +02:00
|
|
|
[[nodiscard]] Optional<CSSPixelRect> scrollable_overflow_rect() const
|
2022-03-10 23:13:37 +01:00
|
|
|
{
|
|
|
|
if (!m_overflow_data.has_value())
|
|
|
|
return {};
|
|
|
|
return m_overflow_data->scrollable_overflow_rect;
|
|
|
|
}
|
|
|
|
|
2023-03-19 20:28:30 +03:00
|
|
|
Optional<CSSPixelRect> calculate_overflow_clipped_rect() const;
|
2022-11-12 00:07:43 +03:00
|
|
|
|
2023-07-12 18:55:23 +02:00
|
|
|
void set_overflow_data(OverflowData data) { m_overflow_data = move(data); }
|
2022-03-10 23:13:37 +01:00
|
|
|
|
|
|
|
DOM::Node const* dom_node() const { return layout_box().dom_node(); }
|
|
|
|
DOM::Node* dom_node() { return layout_box().dom_node(); }
|
|
|
|
|
2023-12-11 16:47:40 +01:00
|
|
|
virtual void apply_scroll_offset(PaintContext&, PaintPhase) const override;
|
|
|
|
virtual void reset_scroll_offset(PaintContext&, PaintPhase) const override;
|
2023-08-08 15:02:35 +02:00
|
|
|
|
2023-01-25 04:50:14 +03:00
|
|
|
virtual void apply_clip_overflow_rect(PaintContext&, PaintPhase) const override;
|
|
|
|
virtual void clear_clip_overflow_rect(PaintContext&, PaintPhase) const override;
|
2022-03-10 23:13:37 +01:00
|
|
|
|
2022-11-02 17:35:53 +00:00
|
|
|
virtual Optional<HitTestResult> hit_test(CSSPixelPoint, HitTestType) const override;
|
2022-03-11 00:03:28 +01:00
|
|
|
|
2023-08-06 14:28:23 +02:00
|
|
|
virtual bool handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned buttons, unsigned modifiers, int wheel_delta_x, int wheel_delta_y) override;
|
|
|
|
|
2023-07-12 04:20:37 +00:00
|
|
|
enum class ConflictingElementKind {
|
|
|
|
Cell,
|
|
|
|
Row,
|
|
|
|
RowGroup,
|
|
|
|
Column,
|
|
|
|
ColumnGroup,
|
|
|
|
Table,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BorderDataWithElementKind {
|
|
|
|
CSS::BorderData border_data;
|
|
|
|
ConflictingElementKind element_kind;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BordersDataWithElementKind {
|
|
|
|
BorderDataWithElementKind top;
|
|
|
|
BorderDataWithElementKind right;
|
|
|
|
BorderDataWithElementKind bottom;
|
|
|
|
BorderDataWithElementKind left;
|
|
|
|
};
|
|
|
|
|
|
|
|
void set_override_borders_data(BordersDataWithElementKind const& override_borders_data) { m_override_borders_data = override_borders_data; }
|
|
|
|
Optional<BordersDataWithElementKind> const& override_borders_data() const { return m_override_borders_data; }
|
|
|
|
|
|
|
|
static BordersData remove_element_kind_from_borders_data(PaintableBox::BordersDataWithElementKind borders_data);
|
2023-06-27 02:13:29 +00:00
|
|
|
|
2023-07-01 02:58:11 +00:00
|
|
|
struct TableCellCoordinates {
|
|
|
|
size_t row_index;
|
|
|
|
size_t column_index;
|
|
|
|
size_t row_span;
|
|
|
|
size_t column_span;
|
|
|
|
};
|
|
|
|
|
|
|
|
void set_table_cell_coordinates(TableCellCoordinates const& table_cell_coordinates) { m_table_cell_coordinates = table_cell_coordinates; }
|
|
|
|
auto const& table_cell_coordinates() const { return m_table_cell_coordinates; }
|
|
|
|
|
2023-06-27 02:13:29 +00:00
|
|
|
enum class ShrinkRadiiForBorders {
|
|
|
|
Yes,
|
|
|
|
No
|
|
|
|
};
|
|
|
|
|
|
|
|
BorderRadiiData normalized_border_radii_data(ShrinkRadiiForBorders shrink = ShrinkRadiiForBorders::No) const;
|
2023-06-04 14:21:20 +00:00
|
|
|
|
2023-12-07 06:16:17 +01:00
|
|
|
BorderRadiiData const& border_radii_data() const { return m_border_radii_data; }
|
|
|
|
void set_border_radii_data(BorderRadiiData const& border_radii_data) { m_border_radii_data = border_radii_data; }
|
|
|
|
|
2023-12-19 19:42:00 +01:00
|
|
|
void set_box_shadow_data(Vector<ShadowData> box_shadow_data) { m_box_shadow_data = move(box_shadow_data); }
|
|
|
|
Vector<ShadowData> const& box_shadow_data() const { return m_box_shadow_data; }
|
|
|
|
|
2022-03-10 23:13:37 +01:00
|
|
|
protected:
|
|
|
|
explicit PaintableBox(Layout::Box const&);
|
|
|
|
|
2023-08-31 17:08:45 -05:00
|
|
|
Optional<CSSPixelRect> get_clip_rect() const;
|
|
|
|
|
2022-03-10 23:13:37 +01:00
|
|
|
virtual void paint_border(PaintContext&) const;
|
2022-09-15 08:31:32 +01:00
|
|
|
virtual void paint_backdrop_filter(PaintContext&) const;
|
2022-03-10 23:13:37 +01:00
|
|
|
virtual void paint_background(PaintContext&) const;
|
|
|
|
virtual void paint_box_shadow(PaintContext&) const;
|
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
virtual CSSPixelRect compute_absolute_rect() const;
|
|
|
|
virtual CSSPixelRect compute_absolute_paint_rect() const;
|
2022-03-24 16:06:43 +01:00
|
|
|
|
2022-06-15 21:27:30 +01:00
|
|
|
private:
|
2023-08-18 13:47:52 +02:00
|
|
|
[[nodiscard]] virtual bool is_paintable_box() const final { return true; }
|
|
|
|
|
2022-07-19 12:06:03 +01:00
|
|
|
Optional<OverflowData> m_overflow_data;
|
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
CSSPixelPoint m_offset;
|
|
|
|
CSSPixelSize m_content_size;
|
2022-07-19 12:06:03 +01:00
|
|
|
|
2022-10-31 19:46:55 +00:00
|
|
|
Optional<CSSPixelRect> mutable m_absolute_rect;
|
|
|
|
Optional<CSSPixelRect> mutable m_absolute_paint_rect;
|
2022-07-04 20:49:38 +01:00
|
|
|
|
2023-02-24 01:07:00 +03:00
|
|
|
Optional<CSSPixelRect> mutable m_clip_rect;
|
2022-11-12 00:07:43 +03:00
|
|
|
|
2022-07-04 20:49:38 +01:00
|
|
|
mutable bool m_clipping_overflow { false };
|
2023-12-05 22:30:32 +01:00
|
|
|
mutable Optional<u32> m_corner_clipper_id;
|
2023-06-04 14:21:20 +00:00
|
|
|
|
2023-07-12 04:20:37 +00:00
|
|
|
Optional<BordersDataWithElementKind> m_override_borders_data;
|
2023-07-01 02:58:11 +00:00
|
|
|
Optional<TableCellCoordinates> m_table_cell_coordinates;
|
2023-12-07 06:16:17 +01:00
|
|
|
|
|
|
|
BorderRadiiData m_border_radii_data;
|
2023-12-19 19:42:00 +01:00
|
|
|
Vector<ShadowData> m_box_shadow_data;
|
2022-03-10 23:13:37 +01:00
|
|
|
};
|
|
|
|
|
2023-08-18 15:52:40 +02:00
|
|
|
class PaintableWithLines : public PaintableBox {
|
2023-01-11 12:51:49 +01:00
|
|
|
JS_CELL(PaintableWithLines, PaintableBox);
|
|
|
|
|
2022-03-10 23:13:37 +01:00
|
|
|
public:
|
2023-01-11 12:51:49 +01:00
|
|
|
static JS::NonnullGCPtr<PaintableWithLines> create(Layout::BlockContainer const&);
|
2022-03-10 23:13:37 +01:00
|
|
|
virtual ~PaintableWithLines() override;
|
|
|
|
|
|
|
|
Layout::BlockContainer const& layout_box() const;
|
|
|
|
Layout::BlockContainer& layout_box();
|
|
|
|
|
2024-01-12 21:25:05 +01:00
|
|
|
Vector<PaintableFragment> const& fragments() const { return m_fragments; }
|
|
|
|
|
|
|
|
void add_fragment(Layout::LineBoxFragment const& fragment)
|
|
|
|
{
|
|
|
|
m_fragments.append(PaintableFragment { fragment });
|
|
|
|
}
|
2022-03-10 23:13:37 +01:00
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_fragment(Callback callback) const
|
|
|
|
{
|
2024-01-12 21:25:05 +01:00
|
|
|
for (auto& fragment : m_fragments) {
|
|
|
|
if (callback(fragment) == IterationDecision::Break)
|
|
|
|
return;
|
2022-03-10 23:13:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void paint(PaintContext&, PaintPhase) const override;
|
|
|
|
virtual bool wants_mouse_events() const override { return false; }
|
|
|
|
|
2022-11-02 17:35:53 +00:00
|
|
|
virtual Optional<HitTestResult> hit_test(CSSPixelPoint, HitTestType) const override;
|
2022-03-11 00:03:28 +01:00
|
|
|
|
2024-01-12 21:25:05 +01:00
|
|
|
virtual void visit_edges(Cell::Visitor& visitor) override
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
for (auto& fragment : m_fragments)
|
|
|
|
visitor.visit(JS::NonnullGCPtr { fragment.layout_node() });
|
|
|
|
}
|
|
|
|
|
2022-03-12 14:13:25 +01:00
|
|
|
protected:
|
2022-03-10 23:13:37 +01:00
|
|
|
PaintableWithLines(Layout::BlockContainer const&);
|
|
|
|
|
2022-03-12 14:13:25 +01:00
|
|
|
private:
|
2023-08-18 13:47:52 +02:00
|
|
|
[[nodiscard]] virtual bool is_paintable_with_lines() const final { return true; }
|
|
|
|
|
2024-01-12 21:25:05 +01:00
|
|
|
Vector<PaintableFragment> m_fragments;
|
2022-03-10 23:13:37 +01:00
|
|
|
};
|
|
|
|
|
2024-01-12 21:25:05 +01:00
|
|
|
void paint_text_decoration(PaintContext& context, Layout::Node const& text_node, PaintableFragment const& fragment);
|
|
|
|
void paint_cursor_if_needed(PaintContext& context, Layout::TextNode const& text_node, PaintableFragment const& fragment);
|
|
|
|
void paint_text_fragment(PaintContext& context, Layout::TextNode const& text_node, PaintableFragment const& fragment, PaintPhase phase);
|
2024-01-03 19:00:04 +01:00
|
|
|
|
2022-03-10 23:13:37 +01:00
|
|
|
}
|