FormattingState.h 3.1 KB

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