FormattingState.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. OwnPtr<Layout::Box::OverflowData> overflow_data;
  45. Layout::Box::OverflowData& ensure_overflow_data()
  46. {
  47. if (!overflow_data)
  48. overflow_data = make<Layout::Box::OverflowData>();
  49. return *overflow_data;
  50. }
  51. };
  52. void commit();
  53. NodeState& get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
  54. {
  55. return *nodes.ensure(&box, [] { return make<NodeState>(); });
  56. }
  57. NodeState const& get(NodeWithStyleAndBoxModelMetrics const& box) const
  58. {
  59. return const_cast<FormattingState&>(*this).get_mutable(box);
  60. }
  61. HashMap<NodeWithStyleAndBoxModelMetrics const*, NonnullOwnPtr<NodeState>> nodes;
  62. };
  63. Gfx::FloatRect absolute_content_rect(Box const&, FormattingState const&);
  64. Gfx::FloatRect margin_box_rect(Box const&, FormattingState const&);
  65. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, FormattingState const&);
  66. }