2022-02-19 20:13:47 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-02-20 15:51:24 +01:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <LibGfx/Point.h>
|
2022-02-21 11:22:08 +01:00
|
|
|
#include <LibWeb/Layout/Box.h>
|
2022-02-20 15:51:24 +01:00
|
|
|
#include <LibWeb/Layout/LineBox.h>
|
2022-03-10 23:13:37 +01:00
|
|
|
#include <LibWeb/Painting/PaintableBox.h>
|
2022-02-20 15:51:24 +01:00
|
|
|
|
2022-02-19 20:13:47 +01:00
|
|
|
namespace Web::Layout {
|
|
|
|
|
2022-07-09 15:17:47 +02:00
|
|
|
enum class SizeConstraint {
|
|
|
|
None,
|
|
|
|
MinContent,
|
|
|
|
MaxContent,
|
|
|
|
};
|
|
|
|
|
2022-07-16 23:30:32 +02:00
|
|
|
struct LayoutState {
|
|
|
|
LayoutState()
|
2022-03-18 12:31:26 +01:00
|
|
|
: m_root(*this)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-07-16 23:30:32 +02:00
|
|
|
explicit LayoutState(LayoutState const* parent)
|
2022-03-12 16:33:11 +01:00
|
|
|
: m_parent(parent)
|
2022-03-18 12:31:26 +01:00
|
|
|
, m_root(find_root())
|
|
|
|
{
|
2022-07-16 23:43:48 +02:00
|
|
|
used_values_per_layout_node.resize(m_root.used_values_per_layout_node.size());
|
2022-03-18 12:31:26 +01:00
|
|
|
}
|
|
|
|
|
2022-07-16 23:30:32 +02:00
|
|
|
LayoutState const& find_root() const
|
2022-03-12 16:33:11 +01:00
|
|
|
{
|
2022-07-16 23:30:32 +02:00
|
|
|
LayoutState const* root = this;
|
2022-03-18 12:31:26 +01:00
|
|
|
for (auto* state = m_parent; state; state = state->m_parent)
|
|
|
|
root = state;
|
|
|
|
return *root;
|
2022-03-12 16:33:11 +01:00
|
|
|
}
|
|
|
|
|
2022-07-16 23:43:48 +02:00
|
|
|
struct UsedValues {
|
2022-07-17 18:46:38 +02:00
|
|
|
NodeWithStyleAndBoxModelMetrics const& node() const { return *m_node; }
|
|
|
|
void set_node(NodeWithStyleAndBoxModelMetrics&, UsedValues const* containing_block_used_values);
|
2022-07-11 17:12:58 +02:00
|
|
|
|
2022-07-17 17:59:02 +02:00
|
|
|
float content_width() const { return m_content_width; }
|
|
|
|
float content_height() const { return m_content_height; }
|
|
|
|
void set_content_width(float);
|
|
|
|
void set_content_height(float);
|
|
|
|
|
2022-07-17 18:46:38 +02:00
|
|
|
bool has_definite_width() const { return m_has_definite_width && width_constraint == SizeConstraint::None; }
|
|
|
|
bool has_definite_height() const { return m_has_definite_height && height_constraint == SizeConstraint::None; }
|
|
|
|
void set_has_definite_width(bool value) { m_has_definite_width = value; }
|
|
|
|
void set_has_definite_height(bool value) { m_has_definite_height = value; }
|
|
|
|
|
2022-02-20 15:51:24 +01:00
|
|
|
Gfx::FloatPoint offset;
|
|
|
|
|
2022-07-09 15:17:47 +02:00
|
|
|
SizeConstraint width_constraint { SizeConstraint::None };
|
|
|
|
SizeConstraint height_constraint { SizeConstraint::None };
|
|
|
|
|
2022-02-20 15:51:24 +01:00
|
|
|
float margin_left { 0 };
|
|
|
|
float margin_right { 0 };
|
|
|
|
float margin_top { 0 };
|
|
|
|
float margin_bottom { 0 };
|
|
|
|
|
|
|
|
float border_left { 0 };
|
|
|
|
float border_right { 0 };
|
|
|
|
float border_top { 0 };
|
|
|
|
float border_bottom { 0 };
|
|
|
|
|
|
|
|
float padding_left { 0 };
|
|
|
|
float padding_right { 0 };
|
|
|
|
float padding_top { 0 };
|
|
|
|
float padding_bottom { 0 };
|
|
|
|
|
2022-03-26 13:26:42 +01:00
|
|
|
float inset_left { 0 };
|
|
|
|
float inset_right { 0 };
|
|
|
|
float inset_top { 0 };
|
|
|
|
float inset_bottom { 0 };
|
2022-02-20 15:51:24 +01:00
|
|
|
|
|
|
|
Vector<LineBox> line_boxes;
|
|
|
|
|
|
|
|
float margin_box_left() const { return margin_left + border_left + padding_left; }
|
|
|
|
float margin_box_right() const { return margin_right + border_right + padding_right; }
|
|
|
|
float margin_box_top() const { return margin_top + border_top + padding_top; }
|
|
|
|
float margin_box_bottom() const { return margin_bottom + border_bottom + padding_bottom; }
|
|
|
|
|
2022-07-17 17:59:02 +02:00
|
|
|
float margin_box_width() const { return margin_box_left() + content_width() + margin_box_right(); }
|
|
|
|
float margin_box_height() const { return margin_box_top() + content_height() + margin_box_bottom(); }
|
2022-03-24 18:06:45 +01:00
|
|
|
|
2022-02-20 15:51:24 +01:00
|
|
|
float border_box_left() const { return border_left + padding_left; }
|
|
|
|
float border_box_right() const { return border_right + padding_right; }
|
|
|
|
float border_box_top() const { return border_top + padding_top; }
|
|
|
|
float border_box_bottom() const { return border_bottom + padding_bottom; }
|
|
|
|
|
2022-07-17 17:59:02 +02:00
|
|
|
float border_box_width() const { return border_box_left() + content_width() + border_box_right(); }
|
|
|
|
float border_box_height() const { return border_box_top() + content_height() + border_box_bottom(); }
|
2022-02-21 11:22:08 +01:00
|
|
|
|
2022-03-10 15:50:57 +01:00
|
|
|
Optional<Painting::PaintableBox::OverflowData> overflow_data;
|
2022-02-21 11:22:08 +01:00
|
|
|
|
2022-03-10 15:50:57 +01:00
|
|
|
Painting::PaintableBox::OverflowData& ensure_overflow_data()
|
2022-02-21 11:22:08 +01:00
|
|
|
{
|
2022-02-27 10:55:39 +01:00
|
|
|
if (!overflow_data.has_value())
|
2022-03-10 15:50:57 +01:00
|
|
|
overflow_data = Painting::PaintableBox::OverflowData {};
|
2022-02-21 11:22:08 +01:00
|
|
|
return *overflow_data;
|
|
|
|
}
|
2022-02-28 12:32:23 +01:00
|
|
|
|
2022-02-28 12:41:23 +01:00
|
|
|
Optional<LineBoxFragmentCoordinate> containing_line_box_fragment;
|
2022-07-17 17:59:02 +02:00
|
|
|
|
2022-09-09 23:56:55 +02:00
|
|
|
void add_floating_descendant(Box const& box) { m_floating_descendants.set(&box); }
|
|
|
|
auto const& floating_descendants() const { return m_floating_descendants; }
|
|
|
|
|
2022-07-17 17:59:02 +02:00
|
|
|
private:
|
2022-07-17 18:46:38 +02:00
|
|
|
Layout::NodeWithStyleAndBoxModelMetrics* m_node { nullptr };
|
|
|
|
|
2022-07-17 17:59:02 +02:00
|
|
|
float m_content_width { 0 };
|
|
|
|
float m_content_height { 0 };
|
2022-07-17 18:46:38 +02:00
|
|
|
|
|
|
|
bool m_has_definite_width { false };
|
|
|
|
bool m_has_definite_height { false };
|
2022-09-09 23:56:55 +02:00
|
|
|
|
|
|
|
HashTable<Box const*> m_floating_descendants;
|
2022-02-20 15:51:24 +01:00
|
|
|
};
|
|
|
|
|
2022-09-14 11:43:57 +02:00
|
|
|
float resolved_definite_width(Box const&) const;
|
|
|
|
float resolved_definite_height(Box const&) const;
|
|
|
|
|
2022-02-20 15:51:24 +01:00
|
|
|
void commit();
|
|
|
|
|
2022-07-16 23:43:48 +02:00
|
|
|
// NOTE: get_mutable() will CoW the UsedValues if it's inherited from an ancestor state;
|
|
|
|
UsedValues& get_mutable(NodeWithStyleAndBoxModelMetrics const&);
|
2022-02-20 15:51:24 +01:00
|
|
|
|
2022-07-16 23:43:48 +02:00
|
|
|
// NOTE: get() will not CoW the UsedValues.
|
|
|
|
UsedValues const& get(NodeWithStyleAndBoxModelMetrics const&) const;
|
2022-02-20 15:51:24 +01:00
|
|
|
|
2022-07-16 23:43:48 +02:00
|
|
|
Vector<OwnPtr<UsedValues>> used_values_per_layout_node;
|
2022-03-12 14:36:59 +01:00
|
|
|
|
|
|
|
// We cache intrinsic sizes once determined, as they will not change over the course of a full layout.
|
|
|
|
// This avoids computing them several times while performing flex layout.
|
|
|
|
struct IntrinsicSizes {
|
2022-07-08 12:44:49 +02:00
|
|
|
Optional<float> min_content_width;
|
|
|
|
Optional<float> max_content_width;
|
|
|
|
Optional<float> min_content_height;
|
|
|
|
Optional<float> max_content_height;
|
2022-03-12 14:36:59 +01:00
|
|
|
};
|
2022-07-08 12:44:49 +02:00
|
|
|
|
|
|
|
HashMap<NodeWithStyleAndBoxModelMetrics const*, NonnullOwnPtr<IntrinsicSizes>> mutable intrinsic_sizes;
|
2022-03-12 16:33:11 +01:00
|
|
|
|
2022-07-16 23:30:32 +02:00
|
|
|
LayoutState const* m_parent { nullptr };
|
|
|
|
LayoutState const& m_root;
|
2022-02-19 20:13:47 +01:00
|
|
|
};
|
|
|
|
|
2022-07-16 23:30:32 +02:00
|
|
|
Gfx::FloatRect absolute_content_rect(Box const&, LayoutState const&);
|
|
|
|
Gfx::FloatRect margin_box_rect(Box const&, LayoutState const&);
|
|
|
|
Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const&);
|
2022-09-07 13:29:58 +02:00
|
|
|
Gfx::FloatRect border_box_rect(Box const&, LayoutState const&);
|
|
|
|
Gfx::FloatRect border_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const&);
|
2022-09-08 13:21:57 +02:00
|
|
|
Gfx::FloatRect content_box_rect(Box const&, LayoutState const&);
|
|
|
|
Gfx::FloatRect content_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const&);
|
2022-02-20 15:51:24 +01:00
|
|
|
|
2022-02-19 20:13:47 +01:00
|
|
|
}
|