FormattingState.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/Paintable.h>
  12. namespace Web::Layout {
  13. struct FormattingState {
  14. struct NodeState {
  15. float content_width { 0 };
  16. float content_height { 0 };
  17. Gfx::FloatPoint offset;
  18. float margin_left { 0 };
  19. float margin_right { 0 };
  20. float margin_top { 0 };
  21. float margin_bottom { 0 };
  22. float border_left { 0 };
  23. float border_right { 0 };
  24. float border_top { 0 };
  25. float border_bottom { 0 };
  26. float padding_left { 0 };
  27. float padding_right { 0 };
  28. float padding_top { 0 };
  29. float padding_bottom { 0 };
  30. float offset_left { 0 };
  31. float offset_right { 0 };
  32. float offset_top { 0 };
  33. float offset_bottom { 0 };
  34. Vector<LineBox> line_boxes;
  35. float margin_box_left() const { return margin_left + border_left + padding_left; }
  36. float margin_box_right() const { return margin_right + border_right + padding_right; }
  37. float margin_box_top() const { return margin_top + border_top + padding_top; }
  38. float margin_box_bottom() const { return margin_bottom + border_bottom + padding_bottom; }
  39. float border_box_left() const { return border_left + padding_left; }
  40. float border_box_right() const { return border_right + padding_right; }
  41. float border_box_top() const { return border_top + padding_top; }
  42. float border_box_bottom() const { return border_bottom + padding_bottom; }
  43. float border_box_width() const { return border_box_left() + content_width + border_box_right(); }
  44. float border_box_height() const { return border_box_top() + content_height + border_box_bottom(); }
  45. Optional<Painting::Paintable::OverflowData> overflow_data;
  46. Painting::Paintable::OverflowData& ensure_overflow_data()
  47. {
  48. if (!overflow_data.has_value())
  49. overflow_data = Painting::Paintable::OverflowData {};
  50. return *overflow_data;
  51. }
  52. Optional<LineBoxFragmentCoordinate> containing_line_box_fragment;
  53. // NOTE: NodeState is ref-counted and accessed via copy-on-write helpers below.
  54. size_t ref_count { 1 };
  55. void ref()
  56. {
  57. VERIFY(ref_count);
  58. ++ref_count;
  59. }
  60. void unref()
  61. {
  62. VERIFY(ref_count);
  63. if (!--ref_count)
  64. delete this;
  65. }
  66. };
  67. void commit();
  68. // NOTE: get_mutable() will CoW the NodeState if it's shared with another FormattingContext.
  69. NodeState& get_mutable(NodeWithStyleAndBoxModelMetrics const&);
  70. // NOTE: get() will not CoW the NodeState.
  71. NodeState const& get(NodeWithStyleAndBoxModelMetrics const&) const;
  72. HashMap<NodeWithStyleAndBoxModelMetrics const*, NonnullRefPtr<NodeState>> nodes;
  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. }