FormattingState.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/BlockContainer.h>
  7. #include <LibWeb/Layout/FormattingState.h>
  8. #include <LibWeb/Layout/TextNode.h>
  9. namespace Web::Layout {
  10. FormattingState::NodeState& FormattingState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
  11. {
  12. if (m_lookup_cache.box == &box && m_lookup_cache.is_mutable)
  13. return *m_lookup_cache.state;
  14. auto& node_state = [&]() -> NodeState& {
  15. if (auto it = nodes.find(&box); it != nodes.end())
  16. return *it->value;
  17. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  18. if (auto it = ancestor->nodes.find(&box); it != ancestor->nodes.end()) {
  19. auto cow_node_state = adopt_own(*new NodeState(*it->value));
  20. auto* cow_node_state_ptr = cow_node_state.ptr();
  21. nodes.set(&box, move(cow_node_state));
  22. return *cow_node_state_ptr;
  23. }
  24. }
  25. return *nodes.ensure(&box, [] { return adopt_own(*new NodeState); });
  26. }();
  27. m_lookup_cache = LookupCache { .box = &box, .state = &node_state, .is_mutable = true };
  28. return node_state;
  29. }
  30. FormattingState::NodeState const& FormattingState::get(NodeWithStyleAndBoxModelMetrics const& box) const
  31. {
  32. if (m_lookup_cache.box == &box)
  33. return *m_lookup_cache.state;
  34. auto& node_state = [&]() -> NodeState const& {
  35. if (auto it = nodes.find(&box); it != nodes.end())
  36. return *it->value;
  37. for (auto* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  38. if (auto it = ancestor->nodes.find(&box); it != ancestor->nodes.end())
  39. return *it->value;
  40. }
  41. return *const_cast<FormattingState&>(*this).nodes.ensure(&box, [] { return adopt_own(*new NodeState); });
  42. }();
  43. const_cast<FormattingState*>(this)->m_lookup_cache = LookupCache { .box = &box, .state = const_cast<NodeState*>(&node_state), .is_mutable = false };
  44. return node_state;
  45. }
  46. void FormattingState::commit()
  47. {
  48. // Only the top-level FormattingState should ever be committed.
  49. VERIFY(!m_parent);
  50. HashTable<Layout::TextNode*> text_nodes;
  51. for (auto& it : nodes) {
  52. auto& node = const_cast<Layout::NodeWithStyleAndBoxModelMetrics&>(*it.key);
  53. auto& node_state = *it.value;
  54. // Transfer box model metrics.
  55. node.box_model().inset = { node_state.inset_top, node_state.inset_right, node_state.inset_bottom, node_state.inset_left };
  56. node.box_model().padding = { node_state.padding_top, node_state.padding_right, node_state.padding_bottom, node_state.padding_left };
  57. node.box_model().border = { node_state.border_top, node_state.border_right, node_state.border_bottom, node_state.border_left };
  58. node.box_model().margin = { node_state.margin_top, node_state.margin_right, node_state.margin_bottom, node_state.margin_left };
  59. node.set_paintable(node.create_paintable());
  60. // For boxes, transfer all the state needed for painting.
  61. if (is<Layout::Box>(node)) {
  62. auto& box = static_cast<Layout::Box&>(node);
  63. auto& paint_box = const_cast<Painting::PaintableBox&>(*box.paint_box());
  64. paint_box.set_offset(node_state.offset);
  65. paint_box.set_content_size(node_state.content_width, node_state.content_height);
  66. paint_box.set_overflow_data(move(node_state.overflow_data));
  67. paint_box.set_containing_line_box_fragment(node_state.containing_line_box_fragment);
  68. if (is<Layout::BlockContainer>(box)) {
  69. for (auto& line_box : node_state.line_boxes) {
  70. for (auto& fragment : line_box.fragments()) {
  71. if (fragment.layout_node().is_text_node())
  72. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  73. }
  74. }
  75. static_cast<Painting::PaintableWithLines&>(paint_box).set_line_boxes(move(node_state.line_boxes));
  76. }
  77. }
  78. }
  79. for (auto* text_node : text_nodes)
  80. text_node->set_paintable(text_node->create_paintable());
  81. }
  82. Gfx::FloatRect margin_box_rect(Box const& box, FormattingState const& state)
  83. {
  84. auto const& box_state = state.get(box);
  85. auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width, box_state.content_height } };
  86. rect.set_x(rect.x() - box_state.margin_box_left());
  87. rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
  88. rect.set_y(rect.y() - box_state.margin_box_top());
  89. rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
  90. return rect;
  91. }
  92. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, FormattingState const& state)
  93. {
  94. auto rect = margin_box_rect(box, state);
  95. for (auto const* current = box.parent(); current; current = current->parent()) {
  96. if (current == &ancestor_box)
  97. break;
  98. if (is<Box>(*current)) {
  99. auto const& current_state = state.get(static_cast<Box const&>(*current));
  100. rect.translate_by(current_state.offset);
  101. }
  102. }
  103. return rect;
  104. }
  105. Gfx::FloatRect absolute_content_rect(Box const& box, FormattingState const& state)
  106. {
  107. auto const& box_state = state.get(box);
  108. Gfx::FloatRect rect { box_state.offset, { box_state.content_width, box_state.content_height } };
  109. for (auto* block = box.containing_block(); block; block = block->containing_block())
  110. rect.translate_by(state.get(*block).offset);
  111. return rect;
  112. }
  113. }