FormattingState.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/LineBox.h>
  10. #include <LibWeb/Layout/Node.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. };
  45. void commit();
  46. NodeState& ensure(NodeWithStyleAndBoxModelMetrics const& box)
  47. {
  48. return *nodes.ensure(&box, [] { return make<NodeState>(); });
  49. }
  50. NodeState const& get(NodeWithStyleAndBoxModelMetrics const& box) const
  51. {
  52. if (!nodes.contains(&box))
  53. return const_cast<FormattingState&>(*this).ensure(box);
  54. return *nodes.get(&box).value();
  55. }
  56. HashMap<NodeWithStyleAndBoxModelMetrics const*, NonnullOwnPtr<NodeState>> nodes;
  57. };
  58. Gfx::FloatRect absolute_content_rect(Box const&, FormattingState const&);
  59. Gfx::FloatRect margin_box_rect(Box const&, FormattingState const&);
  60. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, FormattingState const&);
  61. }