FormattingState.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 and size.
  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. }
  25. // For block containers, transfer line boxes.
  26. if (is<Layout::BlockContainer>(node)) {
  27. auto& block_container = static_cast<Layout::BlockContainer&>(node);
  28. block_container.set_line_boxes(move(node_state.line_boxes));
  29. }
  30. }
  31. }
  32. Gfx::FloatRect margin_box_rect(Box const& box, FormattingState const& state)
  33. {
  34. auto const& box_state = state.get(box);
  35. auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width, 0 } };
  36. rect.set_x(rect.x() - box_state.margin_box_left());
  37. rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
  38. rect.set_y(rect.y() - box_state.margin_box_top());
  39. rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
  40. return rect;
  41. }
  42. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, FormattingState const& state)
  43. {
  44. auto rect = margin_box_rect(box, state);
  45. for (auto const* current = box.parent(); current; current = current->parent()) {
  46. if (current == &ancestor_box)
  47. break;
  48. if (is<Box>(*current)) {
  49. auto const& current_state = state.get(static_cast<Box const&>(*current));
  50. rect.translate_by(current_state.offset);
  51. }
  52. }
  53. return rect;
  54. }
  55. Gfx::FloatRect absolute_content_rect(Box const& box, FormattingState const& state)
  56. {
  57. auto const& box_state = state.get(box);
  58. Gfx::FloatRect rect { box_state.offset, { box_state.content_width, box_state.content_height } };
  59. for (auto* block = box.containing_block(); block; block = block->containing_block())
  60. rect.translate_by(state.get(*block).offset);
  61. return rect;
  62. }
  63. }