BlockFormattingContext.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 float automatic_content_height() const override;
  20. bool is_initial() const;
  21. auto const& left_side_floats() const { return m_left_floats; }
  22. auto const& right_side_floats() const { return m_right_floats; }
  23. void compute_width(Box const&, AvailableSpace const&, LayoutMode = LayoutMode::Normal);
  24. // https://www.w3.org/TR/css-display/#block-formatting-context-root
  25. BlockContainer const& root() const { return static_cast<BlockContainer const&>(context_box()); }
  26. virtual void parent_context_did_dimension_child_root_box() override;
  27. void compute_height(Box const&, AvailableSpace const&);
  28. void add_absolutely_positioned_box(Box const& box) { m_absolutely_positioned_boxes.append(box); }
  29. SpaceUsedByFloats space_used_by_floats(float y) const;
  30. virtual float greatest_child_width(Box const&) override;
  31. void layout_floating_box(Box const& child, BlockContainer const& containing_block, LayoutMode, AvailableSpace const&, LineBuilder* = nullptr);
  32. void layout_block_level_box(Box const&, BlockContainer const&, LayoutMode, float& bottom_of_lowest_margin_box, AvailableSpace const&);
  33. static bool should_treat_width_as_auto(Box const&, AvailableSpace const&);
  34. static bool should_treat_height_as_auto(Box const&, AvailableSpace const&);
  35. virtual bool can_determine_size_of_child() const override { return true; }
  36. virtual void determine_width_of_child(Box const&, AvailableSpace const&) override;
  37. virtual void determine_height_of_child(Box const&, AvailableSpace const&) override;
  38. private:
  39. virtual bool is_block_formatting_context() const final { return true; }
  40. void compute_width_for_floating_box(Box const&, AvailableSpace const&);
  41. void compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox const&, AvailableSpace const&);
  42. void layout_initial_containing_block(LayoutMode, AvailableSpace const&);
  43. void layout_block_level_children(BlockContainer const&, LayoutMode, AvailableSpace const&);
  44. void layout_inline_children(BlockContainer const&, LayoutMode, AvailableSpace const&);
  45. static void resolve_vertical_box_model_metrics(Box const& box, AvailableSpace const&, LayoutState&);
  46. void place_block_level_element_in_normal_flow_horizontally(Box const& child_box, AvailableSpace const&);
  47. void place_block_level_element_in_normal_flow_vertically(Box const& child_box, AvailableSpace const&);
  48. void layout_list_item_marker(ListItemBox const&);
  49. enum class FloatSide {
  50. Left,
  51. Right,
  52. };
  53. struct FloatingBox {
  54. Box const& box;
  55. // Offset from left/right edge to the left content edge of `box`.
  56. float offset_from_edge { 0 };
  57. // Top margin edge of `box`.
  58. float top_margin_edge { 0 };
  59. // Bottom margin edge of `box`.
  60. float bottom_margin_edge { 0 };
  61. };
  62. struct FloatSideData {
  63. // Floating boxes currently accumulating on this side.
  64. Vector<FloatingBox&> current_boxes;
  65. // Combined width of boxes currently accumulating on this side.
  66. // This is the innermost margin of the innermost floating box.
  67. float current_width { 0 };
  68. // Highest value of `m_current_width` we've seen.
  69. float max_width { 0 };
  70. // All floating boxes encountered thus far within this BFC.
  71. Vector<NonnullOwnPtr<FloatingBox>> all_boxes;
  72. // Current Y offset from BFC root top.
  73. float y_offset { 0 };
  74. void clear()
  75. {
  76. current_boxes.clear();
  77. current_width = 0;
  78. }
  79. };
  80. FloatSideData m_left_floats;
  81. FloatSideData m_right_floats;
  82. Vector<Box const&> m_absolutely_positioned_boxes;
  83. bool m_was_notified_after_parent_dimensioned_my_root_box { false };
  84. };
  85. }