LayoutState.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <LibGfx/Point.h>
  9. #include <LibWeb/Layout/Box.h>
  10. #include <LibWeb/Layout/LineBox.h>
  11. #include <LibWeb/Painting/PaintableBox.h>
  12. namespace Web::Layout {
  13. enum class SizeConstraint {
  14. None,
  15. MinContent,
  16. MaxContent,
  17. };
  18. class AvailableSize;
  19. class AvailableSpace;
  20. struct LayoutState {
  21. LayoutState()
  22. : m_root(*this)
  23. {
  24. }
  25. explicit LayoutState(LayoutState const* parent);
  26. ~LayoutState();
  27. LayoutState const& find_root() const
  28. {
  29. LayoutState const* root = this;
  30. for (auto* state = m_parent; state; state = state->m_parent)
  31. root = state;
  32. return *root;
  33. }
  34. struct UsedValues {
  35. NodeWithStyle const& node() const { return *m_node; }
  36. void set_node(NodeWithStyle&, UsedValues const* containing_block_used_values);
  37. CSSPixels content_width() const { return m_content_width; }
  38. CSSPixels content_height() const { return m_content_height; }
  39. void set_content_width(CSSPixels);
  40. void set_content_height(CSSPixels);
  41. void set_indefinite_content_width();
  42. void set_indefinite_content_height();
  43. // NOTE: These are used by FlexFormattingContext to assign a temporary main size to items
  44. // early on, so that descendants have something to resolve percentages against.
  45. void set_temporary_content_width(CSSPixels);
  46. void set_temporary_content_height(CSSPixels);
  47. bool has_definite_width() const { return m_has_definite_width && width_constraint == SizeConstraint::None; }
  48. bool has_definite_height() const { return m_has_definite_height && height_constraint == SizeConstraint::None; }
  49. // Returns the available space for content inside this layout box.
  50. // If the space in an axis is indefinite, and the outer space is an intrinsic sizing constraint,
  51. // the constraint is used in that axis instead.
  52. AvailableSpace available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const;
  53. void set_content_offset(CSSPixelPoint);
  54. void set_content_x(CSSPixels);
  55. void set_content_y(CSSPixels);
  56. CSSPixelPoint offset;
  57. SizeConstraint width_constraint { SizeConstraint::None };
  58. SizeConstraint height_constraint { SizeConstraint::None };
  59. CSSPixels margin_left { 0 };
  60. CSSPixels margin_right { 0 };
  61. CSSPixels margin_top { 0 };
  62. CSSPixels margin_bottom { 0 };
  63. CSSPixels border_left { 0 };
  64. CSSPixels border_right { 0 };
  65. CSSPixels border_top { 0 };
  66. CSSPixels border_bottom { 0 };
  67. CSSPixels padding_left { 0 };
  68. CSSPixels padding_right { 0 };
  69. CSSPixels padding_top { 0 };
  70. CSSPixels padding_bottom { 0 };
  71. CSSPixels inset_left { 0 };
  72. CSSPixels inset_right { 0 };
  73. CSSPixels inset_top { 0 };
  74. CSSPixels inset_bottom { 0 };
  75. Vector<LineBox> line_boxes;
  76. CSSPixels margin_box_left() const { return margin_left + border_left_collapsed() + padding_left; }
  77. CSSPixels margin_box_right() const { return margin_right + border_right_collapsed() + padding_right; }
  78. CSSPixels margin_box_top() const { return margin_top + border_top_collapsed() + padding_top; }
  79. CSSPixels margin_box_bottom() const { return margin_bottom + border_bottom_collapsed() + padding_bottom; }
  80. CSSPixels margin_box_width() const { return margin_box_left() + content_width() + margin_box_right(); }
  81. CSSPixels margin_box_height() const { return margin_box_top() + content_height() + margin_box_bottom(); }
  82. CSSPixels border_box_left() const { return border_left_collapsed() + padding_left; }
  83. CSSPixels border_box_right() const { return border_right_collapsed() + padding_right; }
  84. CSSPixels border_box_top() const { return border_top_collapsed() + padding_top; }
  85. CSSPixels border_box_bottom() const { return border_bottom_collapsed() + padding_bottom; }
  86. CSSPixels border_box_width() const { return border_box_left() + content_width() + border_box_right(); }
  87. CSSPixels border_box_height() const { return border_box_top() + content_height() + border_box_bottom(); }
  88. Optional<LineBoxFragmentCoordinate> containing_line_box_fragment;
  89. void add_floating_descendant(Box const& box) { m_floating_descendants.set(&box); }
  90. auto const& floating_descendants() const { return m_floating_descendants; }
  91. void set_override_borders_data(Painting::PaintableBox::BordersDataWithElementKind const& override_borders_data) { m_override_borders_data = override_borders_data; }
  92. auto const& override_borders_data() const { return m_override_borders_data; }
  93. void set_table_cell_coordinates(Painting::PaintableBox::TableCellCoordinates const& table_cell_coordinates) { m_table_cell_coordinates = table_cell_coordinates; }
  94. auto const& table_cell_coordinates() const { return m_table_cell_coordinates; }
  95. private:
  96. AvailableSize available_width_inside() const;
  97. AvailableSize available_height_inside() const;
  98. bool use_collapsing_borders_model() const { return m_override_borders_data.has_value(); }
  99. // Implement the collapsing border model https://www.w3.org/TR/CSS22/tables.html#collapsing-borders.
  100. CSSPixels border_left_collapsed() const { return use_collapsing_borders_model() ? round(border_left / 2) : border_left; }
  101. CSSPixels border_right_collapsed() const { return use_collapsing_borders_model() ? round(border_right / 2) : border_right; }
  102. CSSPixels border_top_collapsed() const { return use_collapsing_borders_model() ? round(border_top / 2) : border_top; }
  103. CSSPixels border_bottom_collapsed() const { return use_collapsing_borders_model() ? round(border_bottom / 2) : border_bottom; }
  104. JS::GCPtr<Layout::NodeWithStyle> m_node { nullptr };
  105. CSSPixels m_content_width { 0 };
  106. CSSPixels m_content_height { 0 };
  107. bool m_has_definite_width { false };
  108. bool m_has_definite_height { false };
  109. HashTable<JS::GCPtr<Box const>> m_floating_descendants;
  110. Optional<Painting::PaintableBox::BordersDataWithElementKind> m_override_borders_data;
  111. Optional<Painting::PaintableBox::TableCellCoordinates> m_table_cell_coordinates;
  112. };
  113. // Commits the used values produced by layout and builds a paintable tree.
  114. void commit(Box& root);
  115. // NOTE: get_mutable() will CoW the UsedValues if it's inherited from an ancestor state;
  116. UsedValues& get_mutable(NodeWithStyle const&);
  117. // NOTE: get() will not CoW the UsedValues.
  118. UsedValues const& get(NodeWithStyle const&) const;
  119. HashMap<Layout::Node const*, NonnullOwnPtr<UsedValues>> used_values_per_layout_node;
  120. // We cache intrinsic sizes once determined, as they will not change over the course of a full layout.
  121. // This avoids computing them several times while performing flex layout.
  122. struct IntrinsicSizes {
  123. Optional<CSSPixels> min_content_width;
  124. Optional<CSSPixels> max_content_width;
  125. HashMap<CSSPixels, Optional<CSSPixels>> min_content_height;
  126. HashMap<CSSPixels, Optional<CSSPixels>> max_content_height;
  127. };
  128. HashMap<JS::GCPtr<NodeWithStyle const>, NonnullOwnPtr<IntrinsicSizes>> mutable intrinsic_sizes;
  129. LayoutState const* m_parent { nullptr };
  130. LayoutState const& m_root;
  131. private:
  132. void resolve_relative_positions(Vector<Painting::PaintableWithLines&> const&);
  133. };
  134. }