FormattingContext.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/OwnPtr.h>
  8. #include <LibWeb/Forward.h>
  9. #include <LibWeb/Layout/FormattingState.h>
  10. namespace Web::Layout {
  11. class FormattingContext {
  12. public:
  13. virtual ~FormattingContext();
  14. enum class Type {
  15. Block,
  16. Inline,
  17. Flex,
  18. Table,
  19. SVG,
  20. };
  21. virtual void run(Box const&, LayoutMode) = 0;
  22. Box const& context_box() const { return m_context_box; }
  23. FormattingContext* parent() { return m_parent; }
  24. const FormattingContext* parent() const { return m_parent; }
  25. Type type() const { return m_type; }
  26. bool is_block_formatting_context() const { return type() == Type::Block; }
  27. virtual bool inhibits_floating() const { return false; }
  28. static bool creates_block_formatting_context(const Box&);
  29. static float compute_width_for_replaced_element(FormattingState const&, ReplacedBox const&);
  30. static float compute_height_for_replaced_element(FormattingState const&, ReplacedBox const&);
  31. OwnPtr<FormattingContext> create_independent_formatting_context_if_needed(FormattingState&, Box const& child_box);
  32. virtual void parent_context_did_dimension_child_root_box() { }
  33. struct MinAndMaxContentSize {
  34. float min_content_size { 0 };
  35. float max_content_size { 0 };
  36. };
  37. MinAndMaxContentSize calculate_min_and_max_content_width(Layout::Box const&) const;
  38. MinAndMaxContentSize calculate_min_and_max_content_height(Layout::Box const&) const;
  39. float calculate_fit_content_height(Layout::Box const&, Optional<float> available_height) const;
  40. float calculate_fit_content_width(Layout::Box const&, Optional<float> available_width) const;
  41. virtual float greatest_child_width(Box const&);
  42. protected:
  43. FormattingContext(Type, FormattingState&, Box const&, FormattingContext* parent = nullptr);
  44. float calculate_fit_content_size(float min_content_size, float max_content_size, Optional<float> available_space) const;
  45. FormattingState::IntrinsicSizes calculate_intrinsic_sizes(Layout::Box const&) const;
  46. OwnPtr<FormattingContext> layout_inside(Box const&, LayoutMode);
  47. void compute_position(Box const&);
  48. struct SpaceUsedByFloats {
  49. float left { 0 };
  50. float right { 0 };
  51. };
  52. struct ShrinkToFitResult {
  53. float preferred_width { 0 };
  54. float preferred_minimum_width { 0 };
  55. };
  56. static float tentative_width_for_replaced_element(FormattingState const&, ReplacedBox const&, CSS::Length const& width);
  57. static float tentative_height_for_replaced_element(FormattingState const&, ReplacedBox const&, CSS::Length const& height);
  58. static float compute_auto_height_for_block_formatting_context_root(FormattingState const&, BlockContainer const&);
  59. static float compute_auto_height_for_block_level_element(FormattingState const&, Box const&);
  60. ShrinkToFitResult calculate_shrink_to_fit_widths(Box const&);
  61. void layout_absolutely_positioned_element(Box const&);
  62. void compute_width_for_absolutely_positioned_element(Box const&);
  63. void compute_width_for_absolutely_positioned_non_replaced_element(Box const&);
  64. void compute_width_for_absolutely_positioned_replaced_element(ReplacedBox const&);
  65. void compute_height_for_absolutely_positioned_element(Box const&);
  66. void compute_height_for_absolutely_positioned_non_replaced_element(Box const&);
  67. void compute_height_for_absolutely_positioned_replaced_element(ReplacedBox const&);
  68. Type m_type {};
  69. FormattingContext* m_parent { nullptr };
  70. Box const& m_context_box;
  71. FormattingState& m_state;
  72. };
  73. }