FormattingState.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 relative offset, size, and overflow data.
  35. if (is<Layout::Box>(node)) {
  36. auto& box = static_cast<Layout::Box&>(node);
  37. box.set_offset(node_state.offset);
  38. box.set_content_size(node_state.content_width, node_state.content_height);
  39. box.set_overflow_data(move(node_state.overflow_data));
  40. }
  41. // For block containers, transfer line boxes.
  42. if (is<Layout::BlockContainer>(node)) {
  43. auto& block_container = static_cast<Layout::BlockContainer&>(node);
  44. block_container.set_line_boxes(move(node_state.line_boxes));
  45. }
  46. }
  47. }
  48. Gfx::FloatRect margin_box_rect(Box const& box, FormattingState const& state)
  49. {
  50. auto const& box_state = state.get(box);
  51. auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width, box_state.content_height } };
  52. rect.set_x(rect.x() - box_state.margin_box_left());
  53. rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
  54. rect.set_y(rect.y() - box_state.margin_box_top());
  55. rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
  56. return rect;
  57. }
  58. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, FormattingState const& state)
  59. {
  60. auto rect = margin_box_rect(box, state);
  61. for (auto const* current = box.parent(); current; current = current->parent()) {
  62. if (current == &ancestor_box)
  63. break;
  64. if (is<Box>(*current)) {
  65. auto const& current_state = state.get(static_cast<Box const&>(*current));
  66. rect.translate_by(current_state.offset);
  67. }
  68. }
  69. return rect;
  70. }
  71. Gfx::FloatRect absolute_content_rect(Box const& box, FormattingState const& state)
  72. {
  73. auto const& box_state = state.get(box);
  74. Gfx::FloatRect rect { box_state.offset, { box_state.content_width, box_state.content_height } };
  75. for (auto* block = box.containing_block(); block; block = block->containing_block())
  76. rect.translate_by(state.get(*block).offset);
  77. return rect;
  78. }
  79. }