FormattingState.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. void FormattingState::commit()
  10. {
  11. for (auto& it : nodes) {
  12. auto& node = const_cast<Layout::NodeWithStyleAndBoxModelMetrics&>(*it.key);
  13. auto& node_state = *it.value;
  14. // Transfer box model metrics.
  15. node.box_model().offset = { node_state.offset_top, node_state.offset_right, node_state.offset_bottom, node_state.offset_left };
  16. node.box_model().padding = { node_state.padding_top, node_state.padding_right, node_state.padding_bottom, node_state.padding_left };
  17. node.box_model().border = { node_state.border_top, node_state.border_right, node_state.border_bottom, node_state.border_left };
  18. node.box_model().margin = { node_state.margin_top, node_state.margin_right, node_state.margin_bottom, node_state.margin_left };
  19. // For boxes, transfer relative offset, size, and overflow data.
  20. if (is<Layout::Box>(node)) {
  21. auto& box = static_cast<Layout::Box&>(node);
  22. box.set_offset(node_state.offset);
  23. box.set_content_size(node_state.content_width, node_state.content_height);
  24. box.set_overflow_data(move(node_state.overflow_data));
  25. }
  26. // For block containers, transfer line boxes.
  27. if (is<Layout::BlockContainer>(node)) {
  28. auto& block_container = static_cast<Layout::BlockContainer&>(node);
  29. block_container.set_line_boxes(move(node_state.line_boxes));
  30. }
  31. }
  32. }
  33. Gfx::FloatRect margin_box_rect(Box const& box, FormattingState const& state)
  34. {
  35. auto const& box_state = state.get(box);
  36. auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width, 0 } };
  37. rect.set_x(rect.x() - box_state.margin_box_left());
  38. rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
  39. rect.set_y(rect.y() - box_state.margin_box_top());
  40. rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
  41. return rect;
  42. }
  43. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, FormattingState const& state)
  44. {
  45. auto rect = margin_box_rect(box, state);
  46. for (auto const* current = box.parent(); current; current = current->parent()) {
  47. if (current == &ancestor_box)
  48. break;
  49. if (is<Box>(*current)) {
  50. auto const& current_state = state.get(static_cast<Box const&>(*current));
  51. rect.translate_by(current_state.offset);
  52. }
  53. }
  54. return rect;
  55. }
  56. Gfx::FloatRect absolute_content_rect(Box const& box, FormattingState const& state)
  57. {
  58. auto const& box_state = state.get(box);
  59. Gfx::FloatRect rect { box_state.offset, { box_state.content_width, box_state.content_height } };
  60. for (auto* block = box.containing_block(); block; block = block->containing_block())
  61. rect.translate_by(state.get(*block).offset);
  62. return rect;
  63. }
  64. }