LayoutState.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. struct LayoutState {
  19. LayoutState()
  20. : m_root(*this)
  21. {
  22. }
  23. explicit LayoutState(LayoutState const* parent)
  24. : m_parent(parent)
  25. , m_root(find_root())
  26. {
  27. used_values_per_layout_node.resize(m_root.used_values_per_layout_node.size());
  28. }
  29. LayoutState const& find_root() const
  30. {
  31. LayoutState const* root = this;
  32. for (auto* state = m_parent; state; state = state->m_parent)
  33. root = state;
  34. return *root;
  35. }
  36. struct UsedValues {
  37. Layout::NodeWithStyleAndBoxModelMetrics* node { nullptr };
  38. float content_width() const { return m_content_width; }
  39. float content_height() const { return m_content_height; }
  40. void set_content_width(float);
  41. void set_content_height(float);
  42. Gfx::FloatPoint offset;
  43. SizeConstraint width_constraint { SizeConstraint::None };
  44. SizeConstraint height_constraint { SizeConstraint::None };
  45. float margin_left { 0 };
  46. float margin_right { 0 };
  47. float margin_top { 0 };
  48. float margin_bottom { 0 };
  49. float border_left { 0 };
  50. float border_right { 0 };
  51. float border_top { 0 };
  52. float border_bottom { 0 };
  53. float padding_left { 0 };
  54. float padding_right { 0 };
  55. float padding_top { 0 };
  56. float padding_bottom { 0 };
  57. float inset_left { 0 };
  58. float inset_right { 0 };
  59. float inset_top { 0 };
  60. float inset_bottom { 0 };
  61. Vector<LineBox> line_boxes;
  62. float margin_box_left() const { return margin_left + border_left + padding_left; }
  63. float margin_box_right() const { return margin_right + border_right + padding_right; }
  64. float margin_box_top() const { return margin_top + border_top + padding_top; }
  65. float margin_box_bottom() const { return margin_bottom + border_bottom + padding_bottom; }
  66. float margin_box_width() const { return margin_box_left() + content_width() + margin_box_right(); }
  67. float margin_box_height() const { return margin_box_top() + content_height() + margin_box_bottom(); }
  68. float border_box_left() const { return border_left + padding_left; }
  69. float border_box_right() const { return border_right + padding_right; }
  70. float border_box_top() const { return border_top + padding_top; }
  71. float border_box_bottom() const { return border_bottom + padding_bottom; }
  72. float border_box_width() const { return border_box_left() + content_width() + border_box_right(); }
  73. float border_box_height() const { return border_box_top() + content_height() + border_box_bottom(); }
  74. Optional<Painting::PaintableBox::OverflowData> overflow_data;
  75. Painting::PaintableBox::OverflowData& ensure_overflow_data()
  76. {
  77. if (!overflow_data.has_value())
  78. overflow_data = Painting::PaintableBox::OverflowData {};
  79. return *overflow_data;
  80. }
  81. Optional<LineBoxFragmentCoordinate> containing_line_box_fragment;
  82. private:
  83. float m_content_width { 0 };
  84. float m_content_height { 0 };
  85. };
  86. void commit();
  87. // NOTE: get_mutable() will CoW the UsedValues if it's inherited from an ancestor state;
  88. UsedValues& get_mutable(NodeWithStyleAndBoxModelMetrics const&);
  89. // NOTE: get() will not CoW the UsedValues.
  90. UsedValues const& get(NodeWithStyleAndBoxModelMetrics const&) const;
  91. Vector<OwnPtr<UsedValues>> used_values_per_layout_node;
  92. // We cache intrinsic sizes once determined, as they will not change over the course of a full layout.
  93. // This avoids computing them several times while performing flex layout.
  94. struct IntrinsicSizes {
  95. Optional<float> min_content_width;
  96. Optional<float> max_content_width;
  97. Optional<float> min_content_height;
  98. Optional<float> max_content_height;
  99. };
  100. HashMap<NodeWithStyleAndBoxModelMetrics const*, NonnullOwnPtr<IntrinsicSizes>> mutable intrinsic_sizes;
  101. HashMap<Box const*, float> mutable flex_item_size_cache;
  102. LayoutState const* m_parent { nullptr };
  103. LayoutState const& m_root;
  104. };
  105. Gfx::FloatRect absolute_content_rect(Box const&, LayoutState const&);
  106. Gfx::FloatRect margin_box_rect(Box const&, LayoutState const&);
  107. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const&);
  108. }