BlockFormattingContext.h 3.6 KB

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