FormattingState.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. struct FormattingState {
  14. FormattingState()
  15. : m_root(*this)
  16. {
  17. }
  18. explicit FormattingState(FormattingState const* parent)
  19. : m_parent(parent)
  20. , m_root(find_root())
  21. {
  22. }
  23. FormattingState const& find_root() const
  24. {
  25. FormattingState const* root = this;
  26. for (auto* state = m_parent; state; state = state->m_parent)
  27. root = state;
  28. return *root;
  29. }
  30. struct NodeState {
  31. float content_width { 0 };
  32. float content_height { 0 };
  33. Gfx::FloatPoint offset;
  34. float margin_left { 0 };
  35. float margin_right { 0 };
  36. float margin_top { 0 };
  37. float margin_bottom { 0 };
  38. float border_left { 0 };
  39. float border_right { 0 };
  40. float border_top { 0 };
  41. float border_bottom { 0 };
  42. float padding_left { 0 };
  43. float padding_right { 0 };
  44. float padding_top { 0 };
  45. float padding_bottom { 0 };
  46. float offset_left { 0 };
  47. float offset_right { 0 };
  48. float offset_top { 0 };
  49. float offset_bottom { 0 };
  50. Vector<LineBox> line_boxes;
  51. float margin_box_left() const { return margin_left + border_left + padding_left; }
  52. float margin_box_right() const { return margin_right + border_right + padding_right; }
  53. float margin_box_top() const { return margin_top + border_top + padding_top; }
  54. float margin_box_bottom() const { return margin_bottom + border_bottom + padding_bottom; }
  55. float border_box_left() const { return border_left + padding_left; }
  56. float border_box_right() const { return border_right + padding_right; }
  57. float border_box_top() const { return border_top + padding_top; }
  58. float border_box_bottom() const { return border_bottom + padding_bottom; }
  59. float border_box_width() const { return border_box_left() + content_width + border_box_right(); }
  60. float border_box_height() const { return border_box_top() + content_height + border_box_bottom(); }
  61. Optional<Painting::PaintableBox::OverflowData> overflow_data;
  62. Painting::PaintableBox::OverflowData& ensure_overflow_data()
  63. {
  64. if (!overflow_data.has_value())
  65. overflow_data = Painting::PaintableBox::OverflowData {};
  66. return *overflow_data;
  67. }
  68. Optional<LineBoxFragmentCoordinate> containing_line_box_fragment;
  69. };
  70. void commit();
  71. // NOTE: get_mutable() will CoW the NodeState if it's inherited from an ancestor state;
  72. NodeState& get_mutable(NodeWithStyleAndBoxModelMetrics const&);
  73. // NOTE: get() will not CoW the NodeState.
  74. NodeState const& get(NodeWithStyleAndBoxModelMetrics const&) const;
  75. HashMap<NodeWithStyleAndBoxModelMetrics const*, NonnullOwnPtr<NodeState>> nodes;
  76. // We cache intrinsic sizes once determined, as they will not change over the course of a full layout.
  77. // This avoids computing them several times while performing flex layout.
  78. struct IntrinsicSizes {
  79. Gfx::FloatSize min_content_size;
  80. Gfx::FloatSize max_content_size;
  81. };
  82. HashMap<NodeWithStyleAndBoxModelMetrics const*, IntrinsicSizes> mutable intrinsic_sizes;
  83. FormattingState const* m_parent { nullptr };
  84. FormattingState const& m_root;
  85. };
  86. Gfx::FloatRect absolute_content_rect(Box const&, FormattingState const&);
  87. Gfx::FloatRect margin_box_rect(Box const&, FormattingState const&);
  88. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, FormattingState const&);
  89. }