FormattingState.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 (auto it = nodes.find(&box); it != nodes.end())
  13. return *it->value;
  14. for (auto* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  15. if (auto it = ancestor->nodes.find(&box); it != ancestor->nodes.end()) {
  16. auto cow_node_state = adopt_own(*new NodeState(*it->value));
  17. auto* cow_node_state_ptr = cow_node_state.ptr();
  18. nodes.set(&box, move(cow_node_state));
  19. return *cow_node_state_ptr;
  20. }
  21. }
  22. return *nodes.ensure(&box, [] { return adopt_own(*new NodeState); });
  23. }
  24. FormattingState::NodeState const& FormattingState::get(NodeWithStyleAndBoxModelMetrics const& box) const
  25. {
  26. if (auto it = nodes.find(&box); it != nodes.end())
  27. return *it->value;
  28. for (auto* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  29. if (auto it = ancestor->nodes.find(&box); it != ancestor->nodes.end())
  30. return *it->value;
  31. }
  32. return *const_cast<FormattingState&>(*this).nodes.ensure(&box, [] { return adopt_own(*new NodeState); });
  33. }
  34. void FormattingState::commit()
  35. {
  36. // Only the top-level FormattingState should ever be committed.
  37. VERIFY(!m_parent);
  38. HashTable<Layout::TextNode*> text_nodes;
  39. for (auto& it : nodes) {
  40. auto& node = const_cast<Layout::NodeWithStyleAndBoxModelMetrics&>(*it.key);
  41. auto& node_state = *it.value;
  42. // Transfer box model metrics.
  43. node.box_model().offset = { node_state.offset_top, node_state.offset_right, node_state.offset_bottom, node_state.offset_left };
  44. node.box_model().padding = { node_state.padding_top, node_state.padding_right, node_state.padding_bottom, node_state.padding_left };
  45. node.box_model().border = { node_state.border_top, node_state.border_right, node_state.border_bottom, node_state.border_left };
  46. node.box_model().margin = { node_state.margin_top, node_state.margin_right, node_state.margin_bottom, node_state.margin_left };
  47. node.set_paintable(node.create_paintable());
  48. // For boxes, transfer all the state needed for painting.
  49. if (is<Layout::Box>(node)) {
  50. auto& box = static_cast<Layout::Box&>(node);
  51. auto& paint_box = const_cast<Painting::PaintableBox&>(*box.paint_box());
  52. paint_box.set_offset(node_state.offset);
  53. paint_box.set_content_size(node_state.content_width, node_state.content_height);
  54. paint_box.set_overflow_data(move(node_state.overflow_data));
  55. paint_box.set_containing_line_box_fragment(node_state.containing_line_box_fragment);
  56. if (is<Layout::BlockContainer>(box)) {
  57. for (auto& line_box : node_state.line_boxes) {
  58. for (auto& fragment : line_box.fragments()) {
  59. if (fragment.layout_node().is_text_node())
  60. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  61. }
  62. }
  63. static_cast<Painting::PaintableWithLines&>(paint_box).set_line_boxes(move(node_state.line_boxes));
  64. }
  65. }
  66. }
  67. for (auto* text_node : text_nodes)
  68. text_node->set_paintable(text_node->create_paintable());
  69. }
  70. Gfx::FloatRect margin_box_rect(Box const& box, FormattingState const& state)
  71. {
  72. auto const& box_state = state.get(box);
  73. auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width, box_state.content_height } };
  74. rect.set_x(rect.x() - box_state.margin_box_left());
  75. rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
  76. rect.set_y(rect.y() - box_state.margin_box_top());
  77. rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
  78. return rect;
  79. }
  80. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, FormattingState const& state)
  81. {
  82. auto rect = margin_box_rect(box, state);
  83. for (auto const* current = box.parent(); current; current = current->parent()) {
  84. if (current == &ancestor_box)
  85. break;
  86. if (is<Box>(*current)) {
  87. auto const& current_state = state.get(static_cast<Box const&>(*current));
  88. rect.translate_by(current_state.offset);
  89. }
  90. }
  91. return rect;
  92. }
  93. Gfx::FloatRect absolute_content_rect(Box const& box, FormattingState const& state)
  94. {
  95. auto const& box_state = state.get(box);
  96. Gfx::FloatRect rect { box_state.offset, { box_state.content_width, box_state.content_height } };
  97. for (auto* block = box.containing_block(); block; block = block->containing_block())
  98. rect.translate_by(state.get(*block).offset);
  99. return rect;
  100. }
  101. }