FormattingState.h 3.5 KB

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