LayoutState.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. : m_parent(parent)
  27. , m_root(find_root())
  28. {
  29. used_values_per_layout_node.resize(m_root.used_values_per_layout_node.size());
  30. }
  31. LayoutState const& find_root() const
  32. {
  33. LayoutState const* root = this;
  34. for (auto* state = m_parent; state; state = state->m_parent)
  35. root = state;
  36. return *root;
  37. }
  38. struct UsedValues {
  39. NodeWithStyleAndBoxModelMetrics const& node() const { return *m_node; }
  40. void set_node(NodeWithStyleAndBoxModelMetrics&, UsedValues const* containing_block_used_values);
  41. CSSPixels content_width() const { return m_content_width; }
  42. CSSPixels content_height() const { return m_content_height; }
  43. void set_content_width(CSSPixels);
  44. void set_content_height(CSSPixels);
  45. void set_indefinite_content_width();
  46. void set_indefinite_content_height();
  47. // NOTE: These are used by FlexFormattingContext to assign a temporary main size to items
  48. // early on, so that descendants have something to resolve percentages against.
  49. void set_temporary_content_width(CSSPixels);
  50. void set_temporary_content_height(CSSPixels);
  51. bool has_definite_width() const { return m_has_definite_width && width_constraint == SizeConstraint::None; }
  52. bool has_definite_height() const { return m_has_definite_height && height_constraint == SizeConstraint::None; }
  53. // Returns the available space for content inside this layout box.
  54. // If the space in an axis is indefinite, and the outer space is an intrinsic sizing constraint,
  55. // the constraint is used in that axis instead.
  56. AvailableSpace available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const;
  57. void set_content_offset(CSSPixelPoint);
  58. void set_content_x(CSSPixels);
  59. void set_content_y(CSSPixels);
  60. CSSPixelPoint offset;
  61. SizeConstraint width_constraint { SizeConstraint::None };
  62. SizeConstraint height_constraint { SizeConstraint::None };
  63. CSSPixels margin_left { 0 };
  64. CSSPixels margin_right { 0 };
  65. CSSPixels margin_top { 0 };
  66. CSSPixels margin_bottom { 0 };
  67. CSSPixels border_left { 0 };
  68. CSSPixels border_right { 0 };
  69. CSSPixels border_top { 0 };
  70. CSSPixels border_bottom { 0 };
  71. CSSPixels padding_left { 0 };
  72. CSSPixels padding_right { 0 };
  73. CSSPixels padding_top { 0 };
  74. CSSPixels padding_bottom { 0 };
  75. CSSPixels inset_left { 0 };
  76. CSSPixels inset_right { 0 };
  77. CSSPixels inset_top { 0 };
  78. CSSPixels inset_bottom { 0 };
  79. Vector<LineBox> line_boxes;
  80. CSSPixels margin_box_left() const { return margin_left + border_left + padding_left; }
  81. CSSPixels margin_box_right() const { return margin_right + border_right + padding_right; }
  82. CSSPixels margin_box_top() const { return margin_top + border_top + padding_top; }
  83. CSSPixels margin_box_bottom() const { return margin_bottom + border_bottom + padding_bottom; }
  84. CSSPixels margin_box_width() const { return margin_box_left() + content_width() + margin_box_right(); }
  85. CSSPixels margin_box_height() const { return margin_box_top() + content_height() + margin_box_bottom(); }
  86. CSSPixels border_box_left() const { return border_left + padding_left; }
  87. CSSPixels border_box_right() const { return border_right + padding_right; }
  88. CSSPixels border_box_top() const { return border_top + padding_top; }
  89. CSSPixels border_box_bottom() const { return border_bottom + padding_bottom; }
  90. CSSPixels border_box_width() const { return border_box_left() + content_width() + border_box_right(); }
  91. CSSPixels border_box_height() const { return border_box_top() + content_height() + border_box_bottom(); }
  92. Optional<Painting::PaintableBox::OverflowData> overflow_data;
  93. Painting::PaintableBox::OverflowData& ensure_overflow_data()
  94. {
  95. if (!overflow_data.has_value())
  96. overflow_data = Painting::PaintableBox::OverflowData {};
  97. return *overflow_data;
  98. }
  99. Optional<LineBoxFragmentCoordinate> containing_line_box_fragment;
  100. void add_floating_descendant(Box const& box) { m_floating_descendants.set(&box); }
  101. auto const& floating_descendants() const { return m_floating_descendants; }
  102. private:
  103. AvailableSize available_width_inside() const;
  104. AvailableSize available_height_inside() const;
  105. JS::GCPtr<Layout::NodeWithStyleAndBoxModelMetrics> m_node { nullptr };
  106. CSSPixels m_content_width { 0 };
  107. CSSPixels m_content_height { 0 };
  108. bool m_has_definite_width { false };
  109. bool m_has_definite_height { false };
  110. HashTable<JS::GCPtr<Box const>> m_floating_descendants;
  111. };
  112. CSSPixels resolved_definite_width(Box const&) const;
  113. CSSPixels resolved_definite_height(Box const&) const;
  114. void commit();
  115. // NOTE: get_mutable() will CoW the UsedValues if it's inherited from an ancestor state;
  116. UsedValues& get_mutable(NodeWithStyleAndBoxModelMetrics const&);
  117. // NOTE: get() will not CoW the UsedValues.
  118. UsedValues const& get(NodeWithStyleAndBoxModelMetrics const&) const;
  119. Vector<OwnPtr<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. // NOTE: Since intrinsic heights depend on the amount of available width, we have to cache
  126. // three separate kinds of results, depending on the available width at the time of calculation.
  127. HashMap<CSSPixels, Optional<CSSPixels>> min_content_height_with_definite_available_width;
  128. HashMap<CSSPixels, Optional<CSSPixels>> max_content_height_with_definite_available_width;
  129. Optional<CSSPixels> min_content_height_with_min_content_available_width;
  130. Optional<CSSPixels> max_content_height_with_min_content_available_width;
  131. Optional<CSSPixels> min_content_height_with_max_content_available_width;
  132. Optional<CSSPixels> max_content_height_with_max_content_available_width;
  133. };
  134. HashMap<JS::GCPtr<NodeWithStyleAndBoxModelMetrics const>, NonnullOwnPtr<IntrinsicSizes>> mutable intrinsic_sizes;
  135. LayoutState const* m_parent { nullptr };
  136. LayoutState const& m_root;
  137. };
  138. CSSPixelRect absolute_content_rect(Box const&, LayoutState const&);
  139. CSSPixelRect margin_box_rect(Box const&, LayoutState const&);
  140. CSSPixelRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const&);
  141. CSSPixelRect border_box_rect(Box const&, LayoutState const&);
  142. CSSPixelRect border_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const&);
  143. CSSPixelRect content_box_rect(Box const&, LayoutState const&);
  144. CSSPixelRect content_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const&);
  145. CSSPixels box_baseline(LayoutState const& state, Box const& box);
  146. }