FormattingState.cpp 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. namespace Web::Layout {
  9. FormattingState::NodeState& FormattingState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
  10. {
  11. auto state = nodes.ensure(&box, [] { return adopt_ref(*new NodeState); });
  12. // CoW if ref_count > 2 (1 for the entry in `this->nodes`, 1 for the `state` local in this function)
  13. if (state->ref_count > 2) {
  14. state = adopt_ref(*new NodeState { *state });
  15. state->ref_count = 1;
  16. nodes.set(&box, state);
  17. }
  18. return state;
  19. }
  20. FormattingState::NodeState const& FormattingState::get(NodeWithStyleAndBoxModelMetrics const& box) const
  21. {
  22. return *const_cast<FormattingState&>(*this).nodes.ensure(&box, [] { return adopt_ref(*new NodeState); });
  23. }
  24. void FormattingState::commit()
  25. {
  26. for (auto& it : nodes) {
  27. auto& node = const_cast<Layout::NodeWithStyleAndBoxModelMetrics&>(*it.key);
  28. auto& node_state = *it.value;
  29. // Transfer box model metrics.
  30. node.box_model().offset = { node_state.offset_top, node_state.offset_right, node_state.offset_bottom, node_state.offset_left };
  31. node.box_model().padding = { node_state.padding_top, node_state.padding_right, node_state.padding_bottom, node_state.padding_left };
  32. node.box_model().border = { node_state.border_top, node_state.border_right, node_state.border_bottom, node_state.border_left };
  33. node.box_model().margin = { node_state.margin_top, node_state.margin_right, node_state.margin_bottom, node_state.margin_left };
  34. // For boxes, transfer all the state needed for painting.
  35. if (is<Layout::Box>(node)) {
  36. auto& box = static_cast<Layout::Box&>(node);
  37. box.set_paint_box([](Layout::Box const& layout_box) -> OwnPtr<Painting::Box> {
  38. if (is<Layout::BlockContainer>(layout_box))
  39. return Painting::BoxWithLines::create(static_cast<Layout::BlockContainer const&>(layout_box));
  40. return Painting::Box::create(static_cast<Layout::Box const&>(layout_box));
  41. }(box));
  42. box.m_paint_box->set_offset(node_state.offset);
  43. box.m_paint_box->set_content_size(node_state.content_width, node_state.content_height);
  44. box.m_paint_box->set_overflow_data(move(node_state.overflow_data));
  45. box.m_paint_box->set_containing_line_box_fragment(node_state.containing_line_box_fragment);
  46. if (is<Layout::BlockContainer>(box))
  47. static_cast<Painting::BoxWithLines&>(*box.m_paint_box).set_line_boxes(move(node_state.line_boxes));
  48. }
  49. }
  50. }
  51. Gfx::FloatRect margin_box_rect(Box const& box, FormattingState const& state)
  52. {
  53. auto const& box_state = state.get(box);
  54. auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width, box_state.content_height } };
  55. rect.set_x(rect.x() - box_state.margin_box_left());
  56. rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
  57. rect.set_y(rect.y() - box_state.margin_box_top());
  58. rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
  59. return rect;
  60. }
  61. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, FormattingState const& state)
  62. {
  63. auto rect = margin_box_rect(box, state);
  64. for (auto const* current = box.parent(); current; current = current->parent()) {
  65. if (current == &ancestor_box)
  66. break;
  67. if (is<Box>(*current)) {
  68. auto const& current_state = state.get(static_cast<Box const&>(*current));
  69. rect.translate_by(current_state.offset);
  70. }
  71. }
  72. return rect;
  73. }
  74. Gfx::FloatRect absolute_content_rect(Box const& box, FormattingState const& state)
  75. {
  76. auto const& box_state = state.get(box);
  77. Gfx::FloatRect rect { box_state.offset, { box_state.content_width, box_state.content_height } };
  78. for (auto* block = box.containing_block(); block; block = block->containing_block())
  79. rect.translate_by(state.get(*block).offset);
  80. return rect;
  81. }
  82. }