BlockFormattingContext.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Vector.h>
  8. #include <LibWeb/Forward.h>
  9. #include <LibWeb/Layout/BlockContainer.h>
  10. #include <LibWeb/Layout/FormattingContext.h>
  11. namespace Web::Layout {
  12. class LineBuilder;
  13. // https://www.w3.org/TR/css-display/#block-formatting-context
  14. class BlockFormattingContext : public FormattingContext {
  15. public:
  16. explicit BlockFormattingContext(LayoutState&, BlockContainer const&, FormattingContext* parent);
  17. ~BlockFormattingContext();
  18. virtual void run(Box const&, LayoutMode, AvailableSpace const&) override;
  19. virtual CSSPixels automatic_content_height() const override;
  20. auto const& left_side_floats() const { return m_left_floats; }
  21. auto const& right_side_floats() const { return m_right_floats; }
  22. void compute_width(Box const&, AvailableSpace const&, LayoutMode = LayoutMode::Normal);
  23. // https://www.w3.org/TR/css-display/#block-formatting-context-root
  24. BlockContainer const& root() const { return static_cast<BlockContainer const&>(context_box()); }
  25. virtual void parent_context_did_dimension_child_root_box() override;
  26. void compute_height(Box const&, AvailableSpace const&);
  27. void add_absolutely_positioned_box(Box const& box) { m_absolutely_positioned_boxes.append(box); }
  28. SpaceUsedByFloats space_used_by_floats(CSSPixels y) const;
  29. virtual CSSPixels greatest_child_width(Box const&) override;
  30. void layout_floating_box(Box const& child, BlockContainer const& containing_block, LayoutMode, AvailableSpace const&, CSSPixels y, LineBuilder* = nullptr);
  31. void layout_block_level_box(Box const&, BlockContainer const&, LayoutMode, CSSPixels& bottom_of_lowest_margin_box, AvailableSpace const&);
  32. virtual bool can_determine_size_of_child() const override { return true; }
  33. virtual void determine_width_of_child(Box const&, AvailableSpace const&) override;
  34. virtual void determine_height_of_child(Box const&, AvailableSpace const&) override;
  35. private:
  36. CSSPixels compute_auto_height_for_block_level_element(Box const&, AvailableSpace const&);
  37. void compute_width_for_floating_box(Box const&, AvailableSpace const&);
  38. void compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox const&, AvailableSpace const&);
  39. CSSPixels compute_width_for_table_wrapper(Box const&, AvailableSpace const&);
  40. void layout_viewport(LayoutMode, AvailableSpace const&);
  41. void layout_block_level_children(BlockContainer const&, LayoutMode, AvailableSpace const&);
  42. void layout_inline_children(BlockContainer const&, LayoutMode, AvailableSpace const&);
  43. static void resolve_vertical_box_model_metrics(Box const& box, LayoutState&);
  44. void place_block_level_element_in_normal_flow_horizontally(Box const& child_box, AvailableSpace const&);
  45. void place_block_level_element_in_normal_flow_vertically(Box const&, CSSPixels y);
  46. void layout_list_item_marker(ListItemBox const&);
  47. enum class FloatSide {
  48. Left,
  49. Right,
  50. };
  51. struct FloatingBox {
  52. JS::NonnullGCPtr<Box const> box;
  53. // Offset from left/right edge to the left content edge of `box`.
  54. CSSPixels offset_from_edge { 0 };
  55. // Top margin edge of `box`.
  56. CSSPixels top_margin_edge { 0 };
  57. // Bottom margin edge of `box`.
  58. CSSPixels bottom_margin_edge { 0 };
  59. };
  60. struct FloatSideData {
  61. // Floating boxes currently accumulating on this side.
  62. Vector<FloatingBox&> current_boxes;
  63. // Combined width of boxes currently accumulating on this side.
  64. // This is the innermost margin of the innermost floating box.
  65. CSSPixels current_width { 0 };
  66. // Highest value of `m_current_width` we've seen.
  67. CSSPixels max_width { 0 };
  68. // All floating boxes encountered thus far within this BFC.
  69. Vector<NonnullOwnPtr<FloatingBox>> all_boxes;
  70. // Current Y offset from BFC root top.
  71. CSSPixels y_offset { 0 };
  72. void clear()
  73. {
  74. current_boxes.clear();
  75. current_width = 0;
  76. }
  77. };
  78. struct BlockMarginState {
  79. Vector<CSSPixels> current_collapsible_margins;
  80. Function<void(CSSPixels)> block_container_y_position_update_callback;
  81. bool box_last_in_flow_child_margin_bottom_collapsed { false };
  82. void add_margin(CSSPixels margin)
  83. {
  84. current_collapsible_margins.append(margin);
  85. }
  86. void register_block_container_y_position_update_callback(Function<void(CSSPixels)> callback)
  87. {
  88. block_container_y_position_update_callback = move(callback);
  89. }
  90. CSSPixels current_collapsed_margin() const;
  91. bool has_block_container_waiting_for_final_y_position() const
  92. {
  93. return static_cast<bool>(block_container_y_position_update_callback);
  94. }
  95. void update_block_waiting_for_final_y_position() const
  96. {
  97. if (block_container_y_position_update_callback) {
  98. CSSPixels collapsed_margin = current_collapsed_margin();
  99. block_container_y_position_update_callback(collapsed_margin);
  100. }
  101. }
  102. void reset()
  103. {
  104. block_container_y_position_update_callback = {};
  105. current_collapsible_margins.clear();
  106. }
  107. };
  108. Optional<CSSPixels> m_y_offset_of_current_block_container;
  109. BlockMarginState m_margin_state;
  110. FloatSideData m_left_floats;
  111. FloatSideData m_right_floats;
  112. Vector<JS::NonnullGCPtr<Box const>> m_absolutely_positioned_boxes;
  113. bool m_was_notified_after_parent_dimensioned_my_root_box { false };
  114. };
  115. }