FormattingContext.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. FormattingContext const* 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(Box const&);
  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. float calculate_min_content_width(Layout::Box const&) const;
  34. float calculate_max_content_width(Layout::Box const&) const;
  35. float calculate_min_content_height(Layout::Box const&) const;
  36. float calculate_max_content_height(Layout::Box const&) const;
  37. float calculate_fit_content_height(Layout::Box const&, Optional<float> available_height) const;
  38. float calculate_fit_content_width(Layout::Box const&, Optional<float> available_width) const;
  39. virtual float greatest_child_width(Box const&);
  40. float containing_block_width_for(Box const& box) const { return containing_block_width_for(box, m_state); }
  41. float containing_block_height_for(Box const& box) const { return containing_block_height_for(box, m_state); }
  42. static float containing_block_width_for(Box const&, FormattingState const&);
  43. static float containing_block_height_for(Box const&, FormattingState const&);
  44. virtual void run_intrinsic_size_determination(Box const&);
  45. protected:
  46. FormattingContext(Type, FormattingState&, Box const&, FormattingContext* parent = nullptr);
  47. float calculate_fit_content_size(float min_content_size, float max_content_size, Optional<float> available_space) const;
  48. OwnPtr<FormattingContext> layout_inside(Box const&, LayoutMode);
  49. void compute_inset(Box const& box);
  50. struct SpaceUsedByFloats {
  51. float left { 0 };
  52. float right { 0 };
  53. };
  54. struct ShrinkToFitResult {
  55. float preferred_width { 0 };
  56. float preferred_minimum_width { 0 };
  57. };
  58. static float tentative_width_for_replaced_element(FormattingState const&, ReplacedBox const&, CSS::Length const& width);
  59. static float tentative_height_for_replaced_element(FormattingState const&, ReplacedBox const&, CSS::Length const& height);
  60. static float compute_auto_height_for_block_formatting_context_root(FormattingState const&, BlockContainer const&);
  61. static float compute_auto_height_for_block_level_element(FormattingState const&, Box const&);
  62. static float calculate_auto_height(FormattingState const& state, Box const& box);
  63. ShrinkToFitResult calculate_shrink_to_fit_widths(Box const&);
  64. void layout_absolutely_positioned_element(Box const&);
  65. void compute_width_for_absolutely_positioned_element(Box const&);
  66. void compute_width_for_absolutely_positioned_non_replaced_element(Box const&);
  67. void compute_width_for_absolutely_positioned_replaced_element(ReplacedBox const&);
  68. void compute_height_for_absolutely_positioned_element(Box const&);
  69. void compute_height_for_absolutely_positioned_non_replaced_element(Box const&);
  70. void compute_height_for_absolutely_positioned_replaced_element(ReplacedBox const&);
  71. Type m_type {};
  72. FormattingContext* m_parent { nullptr };
  73. Box const& m_context_box;
  74. FormattingState& m_state;
  75. };
  76. }