2020-11-22 13:38:18 +01:00
|
|
|
/*
|
2022-02-19 20:13:47 +01:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.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 {
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2022-09-27 15:29:17 +02:00
|
|
|
virtual void run(Box const&, LayoutMode, AvailableSpace const&) = 0;
|
|
|
|
|
|
|
|
// 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; }
|
|
|
|
bool is_block_formatting_context() const { return type() == Type::Block; }
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2022-07-16 23:30:32 +02:00
|
|
|
OwnPtr<FormattingContext> create_independent_formatting_context_if_needed(LayoutState&, 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;
|
|
|
|
CSSPixels calculate_min_content_height(Layout::Box const&, AvailableSize const& available_width) const;
|
|
|
|
CSSPixels calculate_max_content_height(Layout::Box const&, AvailableSize const& available_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
|
|
|
|
2022-11-21 22:17:24 +03:00
|
|
|
CSS::Length calculate_inner_width(Layout::Box const&, AvailableSize const&, CSS::Size const& width) const;
|
|
|
|
CSS::Length calculate_inner_height(Layout::Box const&, AvailableSize const&, CSS::Size const& height) const;
|
|
|
|
|
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(Box const&) const;
|
|
|
|
[[nodiscard]] CSSPixelRect margin_box_rect_in_ancestor_coordinate_space(Box const&, Box const& ancestor_box) const;
|
|
|
|
[[nodiscard]] CSSPixelRect border_box_rect(Box const&) const;
|
|
|
|
[[nodiscard]] CSSPixelRect border_box_rect_in_ancestor_coordinate_space(Box const&, Box const& ancestor_box) const;
|
|
|
|
[[nodiscard]] CSSPixelRect content_box_rect(Box const&) const;
|
|
|
|
[[nodiscard]] CSSPixelRect content_box_rect_in_ancestor_coordinate_space(Box const&, Box const& ancestor_box) const;
|
|
|
|
[[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-05-31 09:28:03 +02:00
|
|
|
[[nodiscard]] CSSPixels containing_block_width_for(Box const&) const;
|
|
|
|
[[nodiscard]] CSSPixels containing_block_height_for(Box 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
|
|
|
|
|
|
|
virtual bool can_determine_size_of_child() const { return false; }
|
|
|
|
virtual void determine_width_of_child(Box const&, AvailableSpace const&) { }
|
|
|
|
virtual void determine_height_of_child(Box const&, AvailableSpace const&) { }
|
2022-09-26 15:41:22 +02:00
|
|
|
|
2022-11-23 17:46:10 +00:00
|
|
|
virtual CSSPixelPoint calculate_static_position(Box const&) const;
|
2022-12-28 10:11:54 +01:00
|
|
|
bool can_skip_is_anonymous_text_run(Box&);
|
2022-10-01 13:48:28 +02:00
|
|
|
|
2020-11-22 13:38:18 +01:00
|
|
|
protected:
|
2022-07-16 23:30:32 +02:00
|
|
|
FormattingContext(Type, LayoutState&, Box const&, FormattingContext* parent = nullptr);
|
2020-11-22 13:38:18 +01:00
|
|
|
|
2022-11-03 18:44:40 +01:00
|
|
|
static bool should_treat_width_as_auto(Box const&, AvailableSpace const&);
|
|
|
|
static bool should_treat_height_as_auto(Box const&, AvailableSpace const&);
|
|
|
|
|
2022-09-27 15:29:17 +02:00
|
|
|
OwnPtr<FormattingContext> layout_inside(Box const&, LayoutMode, AvailableSpace const&);
|
2022-03-27 15:35:28 +02:00
|
|
|
void compute_inset(Box const& box);
|
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;
|
|
|
|
};
|
|
|
|
|
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-08 16:47:50 +01:00
|
|
|
[[nodiscard]] CSSPixelSize solve_replaced_size_constraint(CSSPixels input_width, CSSPixels input_height, Box 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
|
|
|
|
2021-10-15 01:25:12 +02:00
|
|
|
Type m_type {};
|
|
|
|
|
2020-11-25 20:08:52 +01:00
|
|
|
FormattingContext* m_parent { nullptr };
|
2023-02-26 16:09:02 -07:00
|
|
|
JS::NonnullGCPtr<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
|
|
|
}
|