2020-11-22 13:38:18 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
|
2020-11-22 13:38:18 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-22 13:38:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-10-10 15:56:29 +02:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-11-22 13:38:18 +01:00
|
|
|
#include <LibWeb/Forward.h>
|
2022-09-25 19:12:09 +02:00
|
|
|
#include <LibWeb/Layout/AvailableSpace.h>
|
2022-07-16 23:30:32 +02:00
|
|
|
#include <LibWeb/Layout/LayoutState.h>
|
2020-11-22 13:38:18 +01:00
|
|
|
|
|
|
|
namespace Web::Layout {
|
|
|
|
|
2024-07-24 09:17:47 +02:00
|
|
|
// NOTE: We use a custom clamping function here instead of AK::clamp(), since the AK version
|
|
|
|
// will VERIFY(max >= min) and CSS explicitly allows that (see css-values-4.)
|
|
|
|
template<typename T>
|
|
|
|
[[nodiscard]] constexpr T css_clamp(T const& value, T const& min, T const& max)
|
|
|
|
{
|
|
|
|
return ::max(min, ::min(value, max));
|
|
|
|
}
|
|
|
|
|
2020-11-22 13:38:18 +01:00
|
|
|
class FormattingContext {
|
|
|
|
public:
|
2021-10-17 18:24:07 +02:00
|
|
|
virtual ~FormattingContext();
|
|
|
|
|
2021-10-15 01:25:12 +02:00
|
|
|
enum class Type {
|
|
|
|
Block,
|
|
|
|
Inline,
|
|
|
|
Flex,
|
2023-01-23 15:19:32 +01:00
|
|
|
Grid,
|
2021-10-15 01:25:12 +02:00
|
|
|
Table,
|
|
|
|
SVG,
|
2023-05-03 10:32:23 +02:00
|
|
|
InternalReplaced, // Internal hack formatting context for replaced elements. FIXME: Get rid of this.
|
|
|
|
InternalDummy, // Internal hack formatting context for unimplemented things. FIXME: Get rid of this.
|
2021-10-15 01:25:12 +02:00
|
|
|
};
|
|
|
|
|
2024-09-11 01:03:02 +02:00
|
|
|
virtual void run(AvailableSpace const&) = 0;
|
2022-09-27 15:29:17 +02:00
|
|
|
|
|
|
|
// This function returns the automatic content height of the context's root box.
|
2023-03-19 09:57:31 +01:00
|
|
|
virtual CSSPixels automatic_content_width() const = 0;
|
2020-11-22 13:38:18 +01:00
|
|
|
|
2022-09-24 13:39:43 +02:00
|
|
|
// This function returns the automatic content height of the context's root box.
|
2022-11-23 17:46:10 +00:00
|
|
|
virtual CSSPixels automatic_content_height() const = 0;
|
2022-09-24 13:39:43 +02:00
|
|
|
|
2022-02-20 15:51:24 +01:00
|
|
|
Box const& context_box() const { return m_context_box; }
|
2020-11-22 13:38:18 +01:00
|
|
|
|
2020-11-25 20:08:52 +01:00
|
|
|
FormattingContext* parent() { return m_parent; }
|
2022-04-01 20:58:27 +03:00
|
|
|
FormattingContext const* parent() const { return m_parent; }
|
2020-11-25 20:08:52 +01:00
|
|
|
|
2021-10-15 01:25:12 +02:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
2021-09-15 12:19:42 +01:00
|
|
|
virtual bool inhibits_floating() const { return false; }
|
2020-12-05 20:10:39 +01:00
|
|
|
|
2023-05-03 10:32:23 +02:00
|
|
|
[[nodiscard]] static Optional<Type> formatting_context_type_created_by_box(Box const&);
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static bool creates_block_formatting_context(Box const&);
|
2020-12-05 20:10:39 +01:00
|
|
|
|
2024-03-04 22:11:04 +04:00
|
|
|
CSSPixels compute_table_box_width_inside_table_wrapper(Box const&, AvailableSpace const&);
|
|
|
|
CSSPixels compute_table_box_height_inside_table_wrapper(Box const&, AvailableSpace const&);
|
|
|
|
|
2023-06-08 16:47:50 +01:00
|
|
|
CSSPixels compute_width_for_replaced_element(Box const&, AvailableSpace const&) const;
|
|
|
|
CSSPixels compute_height_for_replaced_element(Box const&, AvailableSpace const&) const;
|
2020-12-11 22:27:09 +01:00
|
|
|
|
2024-09-11 01:03:02 +02:00
|
|
|
OwnPtr<FormattingContext> create_independent_formatting_context_if_needed(LayoutState&, LayoutMode, Box const& child_box);
|
2021-10-17 18:24:07 +02:00
|
|
|
|
2022-02-12 19:54:09 +01:00
|
|
|
virtual void parent_context_did_dimension_child_root_box() { }
|
|
|
|
|
2022-11-23 17:46:10 +00:00
|
|
|
CSSPixels calculate_min_content_width(Layout::Box const&) const;
|
|
|
|
CSSPixels calculate_max_content_width(Layout::Box const&) const;
|
2023-08-12 15:17:17 +02:00
|
|
|
CSSPixels calculate_min_content_height(Layout::Box const&, CSSPixels width) const;
|
|
|
|
CSSPixels calculate_max_content_height(Layout::Box const&, CSSPixels width) const;
|
2022-03-12 14:36:59 +01:00
|
|
|
|
2022-11-23 17:46:10 +00:00
|
|
|
CSSPixels calculate_fit_content_height(Layout::Box const&, AvailableSpace const&) const;
|
|
|
|
CSSPixels calculate_fit_content_width(Layout::Box const&, AvailableSpace const&) const;
|
2022-03-12 14:36:59 +01:00
|
|
|
|
2024-01-07 05:24:09 +01:00
|
|
|
CSSPixels calculate_inner_width(Layout::Box const&, AvailableSize const&, CSS::Size const& width) const;
|
2025-03-14 20:20:34 +01:00
|
|
|
[[nodiscard]] CSSPixels calculate_inner_height(Box const&, AvailableSpace const&, CSS::Size const& height) const;
|
2022-11-21 22:17:24 +03:00
|
|
|
|
2023-03-19 09:57:31 +01:00
|
|
|
virtual CSSPixels greatest_child_width(Box const&) const;
|
2022-03-18 14:44:36 +01:00
|
|
|
|
2023-05-31 10:18:34 +02:00
|
|
|
[[nodiscard]] CSSPixelRect absolute_content_rect(Box const&) const;
|
|
|
|
[[nodiscard]] CSSPixelRect margin_box_rect_in_ancestor_coordinate_space(Box const&, Box const& ancestor_box) const;
|
2024-01-17 11:24:41 +01:00
|
|
|
[[nodiscard]] CSSPixelRect margin_box_rect_in_ancestor_coordinate_space(LayoutState::UsedValues const&, Box const& ancestor_box) const;
|
2023-05-31 10:18:34 +02:00
|
|
|
[[nodiscard]] CSSPixelRect content_box_rect(Box const&) const;
|
2024-01-17 11:24:41 +01:00
|
|
|
[[nodiscard]] CSSPixelRect content_box_rect(LayoutState::UsedValues const&) const;
|
|
|
|
[[nodiscard]] CSSPixelRect content_box_rect_in_ancestor_coordinate_space(LayoutState::UsedValues const&, Box const& ancestor_box) const;
|
2023-05-31 10:18:34 +02:00
|
|
|
[[nodiscard]] CSSPixels box_baseline(Box const&) const;
|
|
|
|
[[nodiscard]] CSSPixelRect content_box_rect_in_static_position_ancestor_coordinate_space(Box const&, Box const& ancestor_box) const;
|
|
|
|
|
2023-08-15 09:30:13 +02:00
|
|
|
[[nodiscard]] CSSPixels containing_block_width_for(NodeWithStyleAndBoxModelMetrics const&) const;
|
2022-07-09 15:17:47 +02:00
|
|
|
|
2022-11-23 17:46:10 +00:00
|
|
|
[[nodiscard]] CSSPixels calculate_stretch_fit_width(Box const&, AvailableSize const&) const;
|
|
|
|
[[nodiscard]] CSSPixels calculate_stretch_fit_height(Box const&, AvailableSize const&) const;
|
2022-09-27 15:29:17 +02:00
|
|
|
|
2022-12-28 10:11:54 +01:00
|
|
|
bool can_skip_is_anonymous_text_run(Box&);
|
2022-10-01 13:48:28 +02:00
|
|
|
|
2024-11-11 15:54:21 +01:00
|
|
|
void compute_inset(NodeWithStyleAndBoxModelMetrics const&, CSSPixelSize containing_block_size);
|
2023-08-15 09:34:06 +02:00
|
|
|
|
2020-11-22 13:38:18 +01:00
|
|
|
protected:
|
2024-09-11 01:03:02 +02:00
|
|
|
FormattingContext(Type, LayoutMode, LayoutState&, Box const&, FormattingContext* parent = nullptr);
|
2020-11-22 13:38:18 +01:00
|
|
|
|
2024-11-21 17:32:02 +01:00
|
|
|
[[nodiscard]] bool should_treat_width_as_auto(Box const&, AvailableSpace const&) const;
|
|
|
|
[[nodiscard]] bool should_treat_height_as_auto(Box const&, AvailableSpace const&) const;
|
2022-11-03 18:44:40 +01:00
|
|
|
|
2023-06-16 13:46:55 +02:00
|
|
|
[[nodiscard]] bool should_treat_max_width_as_none(Box const&, AvailableSize const&) const;
|
|
|
|
[[nodiscard]] bool should_treat_max_height_as_none(Box const&, AvailableSize const&) const;
|
2023-06-14 15:33:06 +02:00
|
|
|
|
2022-09-27 15:29:17 +02:00
|
|
|
OwnPtr<FormattingContext> layout_inside(Box const&, LayoutMode, AvailableSpace const&);
|
2020-11-22 13:38:18 +01:00
|
|
|
|
2022-03-18 14:44:36 +01:00
|
|
|
struct SpaceUsedByFloats {
|
2022-11-23 17:46:10 +00:00
|
|
|
CSSPixels left { 0 };
|
|
|
|
CSSPixels right { 0 };
|
2022-03-17 12:30:30 +01:00
|
|
|
};
|
|
|
|
|
2023-06-13 13:37:15 +00:00
|
|
|
struct SpaceUsedAndContainingMarginForFloats {
|
|
|
|
// Width for left / right floats, including their own margins.
|
|
|
|
CSSPixels left_used_space;
|
|
|
|
CSSPixels right_used_space;
|
|
|
|
// Left / right total margins from the outermost containing block to the floating element.
|
|
|
|
// Each block in the containing chain adds its own margin and we store the total here.
|
|
|
|
CSSPixels left_total_containing_margin;
|
|
|
|
CSSPixels right_total_containing_margin;
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<Box const> matching_left_float_box;
|
2023-06-13 13:37:15 +00:00
|
|
|
};
|
|
|
|
|
2020-11-22 13:38:18 +01:00
|
|
|
struct ShrinkToFitResult {
|
2022-11-23 17:46:10 +00:00
|
|
|
CSSPixels preferred_width { 0 };
|
|
|
|
CSSPixels preferred_minimum_width { 0 };
|
2020-11-22 13:38:18 +01:00
|
|
|
};
|
|
|
|
|
2023-06-08 16:47:50 +01:00
|
|
|
CSSPixels tentative_width_for_replaced_element(Box const&, CSS::Size const& computed_width, AvailableSpace const&) const;
|
|
|
|
CSSPixels tentative_height_for_replaced_element(Box const&, CSS::Size const& computed_height, AvailableSpace const&) const;
|
2023-01-23 14:45:52 +01:00
|
|
|
CSSPixels compute_auto_height_for_block_formatting_context_root(Box const&) const;
|
2020-12-12 00:20:31 +01:00
|
|
|
|
2023-06-16 13:46:55 +02:00
|
|
|
[[nodiscard]] CSSPixelSize solve_replaced_size_constraint(CSSPixels input_width, CSSPixels input_height, Box const&, AvailableSpace const&) const;
|
2023-05-31 10:41:32 +02:00
|
|
|
|
2022-02-20 15:51:24 +01:00
|
|
|
ShrinkToFitResult calculate_shrink_to_fit_widths(Box const&);
|
2020-11-22 13:38:18 +01:00
|
|
|
|
2022-09-27 15:29:17 +02:00
|
|
|
void layout_absolutely_positioned_element(Box const&, AvailableSpace const&);
|
|
|
|
void compute_width_for_absolutely_positioned_element(Box const&, AvailableSpace const&);
|
|
|
|
void compute_width_for_absolutely_positioned_non_replaced_element(Box const&, AvailableSpace const&);
|
2023-06-08 16:47:50 +01:00
|
|
|
void compute_width_for_absolutely_positioned_replaced_element(Box const&, AvailableSpace const&);
|
2023-01-06 21:05:18 +01:00
|
|
|
|
|
|
|
enum class BeforeOrAfterInsideLayout {
|
|
|
|
Before,
|
|
|
|
After,
|
|
|
|
};
|
|
|
|
void compute_height_for_absolutely_positioned_element(Box const&, AvailableSpace const&, BeforeOrAfterInsideLayout);
|
|
|
|
void compute_height_for_absolutely_positioned_non_replaced_element(Box const&, AvailableSpace const&, BeforeOrAfterInsideLayout);
|
2023-06-08 16:47:50 +01:00
|
|
|
void compute_height_for_absolutely_positioned_replaced_element(Box const&, AvailableSpace const&, BeforeOrAfterInsideLayout);
|
2021-01-06 18:18:46 +01:00
|
|
|
|
2023-06-20 13:20:08 +02:00
|
|
|
[[nodiscard]] Optional<CSSPixels> compute_auto_height_for_absolutely_positioned_element(Box const&, AvailableSpace const&, BeforeOrAfterInsideLayout) const;
|
|
|
|
|
2023-07-25 10:58:11 +02:00
|
|
|
[[nodiscard]] Box const* box_child_to_derive_baseline_from(Box const&) const;
|
|
|
|
|
2021-10-15 01:25:12 +02:00
|
|
|
Type m_type {};
|
2024-09-11 01:03:02 +02:00
|
|
|
LayoutMode m_layout_mode;
|
2021-10-15 01:25:12 +02:00
|
|
|
|
2020-11-25 20:08:52 +01:00
|
|
|
FormattingContext* m_parent { nullptr };
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<Box const> m_context_box;
|
2022-02-19 20:13:47 +01:00
|
|
|
|
2022-07-16 23:30:32 +02:00
|
|
|
LayoutState& m_state;
|
2020-11-22 13:38:18 +01:00
|
|
|
};
|
|
|
|
|
2023-06-08 16:47:50 +01:00
|
|
|
bool box_is_sized_as_replaced_element(Box const&);
|
|
|
|
|
2020-11-22 13:38:18 +01:00
|
|
|
}
|