LayoutState.cpp 5.7 KB

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