2020-11-22 09:38:18 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
|
2020-11-22 09:38:18 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-22 09:38:18 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-10-10 10:56:29 -03:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-11-22 09:38:18 -03:00
|
|
|
#include <LibWeb/Forward.h>
|
2022-09-25 14:12:09 -03:00
|
|
|
#include <LibWeb/Layout/AvailableSpace.h>
|
2022-07-16 18:30:32 -03:00
|
|
|
#include <LibWeb/Layout/LayoutState.h>
|
2020-11-22 09:38:18 -03:00
|
|
|
|
|
|
|
|
namespace Web::Layout {
|
|
|
|
|
|
2024-07-24 04:17:47 -03: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 09:38:18 -03:00
|
|
|
class FormattingContext {
|
|
|
|
|
public:
|
2021-10-17 13:24:07 -03:00
|
|
|
virtual ~FormattingContext();
|
|
|
|
|
|
2021-10-14 20:25:12 -03:00
|
|
|
enum class Type {
|
|
|
|
|
Block,
|
|
|
|
|
Inline,
|
|
|
|
|
Flex,
|
2023-01-23 11:19:32 -03:00
|
|
|
Grid,
|
2021-10-14 20:25:12 -03:00
|
|
|
Table,
|
|
|
|
|
SVG,
|
2023-05-03 05:32:23 -03: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-14 20:25:12 -03:00
|
|
|
};
|
|
|
|
|
|
2024-09-10 20:03:02 -03:00
|
|
|
virtual void run(AvailableSpace const&) = 0;
|
2022-09-27 10:29:17 -03:00
|
|
|
|
|
|
|
|
// This function returns the automatic content height of the context's root box.
|
2023-03-19 05:57:31 -03:00
|
|
|
virtual CSSPixels automatic_content_width() const = 0;
|
2020-11-22 09:38:18 -03:00
|
|
|
|
2022-09-24 08:39:43 -03:00
|
|
|
// This function returns the automatic content height of the context's root box.
|
2022-11-23 14:46:10 -03:00
|
|
|
virtual CSSPixels automatic_content_height() const = 0;
|
2022-09-24 08:39:43 -03:00
|
|
|
|
2022-02-20 11:51:24 -03:00
|
|
|
Box const& context_box() const { return m_context_box; }
|
2020-11-22 09:38:18 -03:00
|
|
|
|
2020-11-25 16:08:52 -03:00
|
|
|
FormattingContext* parent() { return m_parent; }
|
2022-04-01 14:58:27 -03:00
|
|
|
FormattingContext const* parent() const { return m_parent; }
|
2020-11-25 16:08:52 -03:00
|
|
|
|
2021-10-14 20:25:12 -03:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
|
2021-09-15 08:19:42 -03:00
|
|
|
virtual bool inhibits_floating() const { return false; }
|
2020-12-05 16:10:39 -03:00
|
|
|
|
2023-05-03 05:32:23 -03:00
|
|
|
[[nodiscard]] static Optional<Type> formatting_context_type_created_by_box(Box const&);
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
static bool creates_block_formatting_context(Box const&);
|
2020-12-05 16:10:39 -03:00
|
|
|
|
2024-03-04 15:11:04 -03: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 12:47:50 -03: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 18:27:09 -03:00
|
|
|
|
2024-09-10 20:03:02 -03:00
|
|
|
OwnPtr<FormattingContext> create_independent_formatting_context_if_needed(LayoutState&, LayoutMode, Box const& child_box);
|
2021-10-17 13:24:07 -03:00
|
|
|
|
2022-02-12 15:54:09 -03:00
|
|
|
virtual void parent_context_did_dimension_child_root_box() { }
|
|
|
|
|
|
2022-11-23 14:46:10 -03:00
|
|
|
CSSPixels calculate_min_content_width(Layout::Box const&) const;
|
|
|
|
|
CSSPixels calculate_max_content_width(Layout::Box const&) const;
|
2023-08-12 10:17:17 -03: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 10:36:59 -03:00
|
|
|
|
2022-11-23 14:46:10 -03: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 10:36:59 -03:00
|
|
|
|
2024-01-07 01:24:09 -03:00
|
|
|
CSSPixels calculate_inner_width(Layout::Box const&, AvailableSize const&, CSS::Size const& width) const;
|
2025-03-14 16:20:34 -03:00
|
|
|
[[nodiscard]] CSSPixels calculate_inner_height(Box const&, AvailableSpace const&, CSS::Size const& height) const;
|
2022-11-21 16:17:24 -03:00
|
|
|
|
2023-03-19 05:57:31 -03:00
|
|
|
virtual CSSPixels greatest_child_width(Box const&) const;
|
2022-03-18 10:44:36 -03:00
|
|
|
|
2023-05-31 05:18:34 -03: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 07:24:41 -03:00
|
|
|
[[nodiscard]] CSSPixelRect margin_box_rect_in_ancestor_coordinate_space(LayoutState::UsedValues const&, Box const& ancestor_box) const;
|
2023-05-31 05:18:34 -03:00
|
|
|
[[nodiscard]] CSSPixelRect content_box_rect(Box const&) const;
|
2024-01-17 07:24:41 -03: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 05:18:34 -03: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 04:30:13 -03:00
|
|
|
[[nodiscard]] CSSPixels containing_block_width_for(NodeWithStyleAndBoxModelMetrics const&) const;
|
2022-07-09 10:17:47 -03:00
|
|
|
|
2022-11-23 14:46:10 -03: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 10:29:17 -03:00
|
|
|
|
2022-12-28 06:11:54 -03:00
|
|
|
bool can_skip_is_anonymous_text_run(Box&);
|
2022-10-01 08:48:28 -03:00
|
|
|
|
2024-11-11 11:54:21 -03:00
|
|
|
void compute_inset(NodeWithStyleAndBoxModelMetrics const&, CSSPixelSize containing_block_size);
|
2023-08-15 04:34:06 -03:00
|
|
|
|
2020-11-22 09:38:18 -03:00
|
|
|
protected:
|
2024-09-10 20:03:02 -03:00
|
|
|
FormattingContext(Type, LayoutMode, LayoutState&, Box const&, FormattingContext* parent = nullptr);
|
2020-11-22 09:38:18 -03:00
|
|
|
|
2024-11-21 13:32:02 -03: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 14:44:40 -03:00
|
|
|
|
2023-06-16 08:46:55 -03: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 10:33:06 -03:00
|
|
|
|
2025-08-04 10:55:19 -03:00
|
|
|
[[nodiscard]] bool box_is_sized_as_replaced_element(Box const&, AvailableSpace const&) const;
|
|
|
|
|
|
2022-09-27 10:29:17 -03:00
|
|
|
OwnPtr<FormattingContext> layout_inside(Box const&, LayoutMode, AvailableSpace const&);
|
2020-11-22 09:38:18 -03:00
|
|
|
|
2022-03-18 10:44:36 -03:00
|
|
|
struct SpaceUsedByFloats {
|
2022-11-23 14:46:10 -03:00
|
|
|
CSSPixels left { 0 };
|
|
|
|
|
CSSPixels right { 0 };
|
2022-03-17 08:30:30 -03:00
|
|
|
};
|
|
|
|
|
|
2023-06-13 10:37:15 -03: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-14 12:01:23 -03:00
|
|
|
GC::Ptr<Box const> matching_left_float_box;
|
2025-05-24 06:46:01 -03:00
|
|
|
GC::Ptr<Box const> matching_right_float_box;
|
2023-06-13 10:37:15 -03:00
|
|
|
};
|
|
|
|
|
|
2020-11-22 09:38:18 -03:00
|
|
|
struct ShrinkToFitResult {
|
2022-11-23 14:46:10 -03:00
|
|
|
CSSPixels preferred_width { 0 };
|
|
|
|
|
CSSPixels preferred_minimum_width { 0 };
|
2020-11-22 09:38:18 -03:00
|
|
|
};
|
|
|
|
|
|
2023-06-08 12:47:50 -03: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 10:45:52 -03:00
|
|
|
CSSPixels compute_auto_height_for_block_formatting_context_root(Box const&) const;
|
2020-12-11 20:20:31 -03:00
|
|
|
|
2023-06-16 08:46:55 -03:00
|
|
|
[[nodiscard]] CSSPixelSize solve_replaced_size_constraint(CSSPixels input_width, CSSPixels input_height, Box const&, AvailableSpace const&) const;
|
2023-05-31 05:41:32 -03:00
|
|
|
|
2022-02-20 11:51:24 -03:00
|
|
|
ShrinkToFitResult calculate_shrink_to_fit_widths(Box const&);
|
2020-11-22 09:38:18 -03:00
|
|
|
|
2022-09-27 10:29:17 -03: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 12:47:50 -03:00
|
|
|
void compute_width_for_absolutely_positioned_replaced_element(Box const&, AvailableSpace const&);
|
2023-01-06 17:05:18 -03: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 12:47:50 -03:00
|
|
|
void compute_height_for_absolutely_positioned_replaced_element(Box const&, AvailableSpace const&, BeforeOrAfterInsideLayout);
|
2021-01-06 14:18:46 -03:00
|
|
|
|
2023-06-20 08:20:08 -03:00
|
|
|
[[nodiscard]] Optional<CSSPixels> compute_auto_height_for_absolutely_positioned_element(Box const&, AvailableSpace const&, BeforeOrAfterInsideLayout) const;
|
|
|
|
|
|
2023-07-25 05:58:11 -03:00
|
|
|
[[nodiscard]] Box const* box_child_to_derive_baseline_from(Box const&) const;
|
|
|
|
|
|
2021-10-14 20:25:12 -03:00
|
|
|
Type m_type {};
|
2024-09-10 20:03:02 -03:00
|
|
|
LayoutMode m_layout_mode;
|
2021-10-14 20:25:12 -03:00
|
|
|
|
2020-11-25 16:08:52 -03:00
|
|
|
FormattingContext* m_parent { nullptr };
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<Box const> m_context_box;
|
2022-02-19 16:13:47 -03:00
|
|
|
|
2022-07-16 18:30:32 -03:00
|
|
|
LayoutState& m_state;
|
2020-11-22 09:38:18 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|