LayoutState.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. float content_width() const { return m_content_width; }
  42. float content_height() const { return m_content_height; }
  43. void set_content_width(float);
  44. void set_content_height(float);
  45. bool has_definite_width() const { return m_has_definite_width && width_constraint == SizeConstraint::None; }
  46. bool has_definite_height() const { return m_has_definite_height && height_constraint == SizeConstraint::None; }
  47. // Returns the available space for content inside this layout box.
  48. // If the space in an axis is indefinite, and the outer space is an intrinsic sizing constraint,
  49. // the constraint is used in that axis instead.
  50. AvailableSpace available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const;
  51. void set_content_offset(Gfx::FloatPoint);
  52. void set_content_x(float);
  53. void set_content_y(float);
  54. Gfx::FloatPoint offset;
  55. SizeConstraint width_constraint { SizeConstraint::None };
  56. SizeConstraint height_constraint { SizeConstraint::None };
  57. float margin_left { 0 };
  58. float margin_right { 0 };
  59. float margin_top { 0 };
  60. float margin_bottom { 0 };
  61. float border_left { 0 };
  62. float border_right { 0 };
  63. float border_top { 0 };
  64. float border_bottom { 0 };
  65. float padding_left { 0 };
  66. float padding_right { 0 };
  67. float padding_top { 0 };
  68. float padding_bottom { 0 };
  69. float inset_left { 0 };
  70. float inset_right { 0 };
  71. float inset_top { 0 };
  72. float inset_bottom { 0 };
  73. Vector<LineBox> line_boxes;
  74. float margin_box_left() const { return margin_left + border_left + padding_left; }
  75. float margin_box_right() const { return margin_right + border_right + padding_right; }
  76. float margin_box_top() const { return margin_top + border_top + padding_top; }
  77. float margin_box_bottom() const { return margin_bottom + border_bottom + padding_bottom; }
  78. float margin_box_width() const { return margin_box_left() + content_width() + margin_box_right(); }
  79. float margin_box_height() const { return margin_box_top() + content_height() + margin_box_bottom(); }
  80. float border_box_left() const { return border_left + padding_left; }
  81. float border_box_right() const { return border_right + padding_right; }
  82. float border_box_top() const { return border_top + padding_top; }
  83. float border_box_bottom() const { return border_bottom + padding_bottom; }
  84. float border_box_width() const { return border_box_left() + content_width() + border_box_right(); }
  85. float border_box_height() const { return border_box_top() + content_height() + border_box_bottom(); }
  86. Optional<Painting::PaintableBox::OverflowData> overflow_data;
  87. Painting::PaintableBox::OverflowData& ensure_overflow_data()
  88. {
  89. if (!overflow_data.has_value())
  90. overflow_data = Painting::PaintableBox::OverflowData {};
  91. return *overflow_data;
  92. }
  93. Optional<LineBoxFragmentCoordinate> containing_line_box_fragment;
  94. void add_floating_descendant(Box const& box) { m_floating_descendants.set(&box); }
  95. auto const& floating_descendants() const { return m_floating_descendants; }
  96. private:
  97. AvailableSize available_width_inside() const;
  98. AvailableSize available_height_inside() const;
  99. Layout::NodeWithStyleAndBoxModelMetrics* m_node { nullptr };
  100. float m_content_width { 0 };
  101. float m_content_height { 0 };
  102. bool m_has_definite_width { false };
  103. bool m_has_definite_height { false };
  104. HashTable<Box const*> m_floating_descendants;
  105. };
  106. float resolved_definite_width(Box const&) const;
  107. float resolved_definite_height(Box const&) const;
  108. void commit();
  109. // NOTE: get_mutable() will CoW the UsedValues if it's inherited from an ancestor state;
  110. UsedValues& get_mutable(NodeWithStyleAndBoxModelMetrics const&);
  111. // NOTE: get() will not CoW the UsedValues.
  112. UsedValues const& get(NodeWithStyleAndBoxModelMetrics const&) const;
  113. Vector<OwnPtr<UsedValues>> used_values_per_layout_node;
  114. // We cache intrinsic sizes once determined, as they will not change over the course of a full layout.
  115. // This avoids computing them several times while performing flex layout.
  116. struct IntrinsicSizes {
  117. Optional<float> min_content_width;
  118. Optional<float> max_content_width;
  119. Optional<float> min_content_height;
  120. Optional<float> max_content_height;
  121. };
  122. HashMap<NodeWithStyleAndBoxModelMetrics const*, NonnullOwnPtr<IntrinsicSizes>> mutable intrinsic_sizes;
  123. LayoutState const* m_parent { nullptr };
  124. LayoutState const& m_root;
  125. };
  126. Gfx::FloatRect absolute_content_rect(Box const&, LayoutState const&);
  127. Gfx::FloatRect margin_box_rect(Box const&, LayoutState const&);
  128. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const&);
  129. Gfx::FloatRect border_box_rect(Box const&, LayoutState const&);
  130. Gfx::FloatRect border_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const&);
  131. Gfx::FloatRect content_box_rect(Box const&, LayoutState const&);
  132. Gfx::FloatRect content_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const&);
  133. }