FormattingContext.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2020, 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. namespace Web::Layout {
  10. class FormattingContext {
  11. public:
  12. virtual ~FormattingContext();
  13. enum class Type {
  14. Block,
  15. Inline,
  16. Flex,
  17. Table,
  18. SVG,
  19. };
  20. virtual void run(Box&, LayoutMode) = 0;
  21. Box& context_box() { return m_context_box; }
  22. const Box& 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(const ReplacedBox&);
  30. static float compute_height_for_replaced_element(const ReplacedBox&);
  31. OwnPtr<FormattingContext> create_independent_formatting_context_if_needed(Box& child_box);
  32. protected:
  33. FormattingContext(Type, Box&, FormattingContext* parent = nullptr);
  34. OwnPtr<FormattingContext> layout_inside(Box&, LayoutMode);
  35. struct ShrinkToFitResult {
  36. float preferred_width { 0 };
  37. float preferred_minimum_width { 0 };
  38. };
  39. static float tentative_width_for_replaced_element(const ReplacedBox&, const CSS::Length& width);
  40. static float tentative_height_for_replaced_element(const ReplacedBox&, const CSS::Length& height);
  41. enum ConsiderFloats {
  42. Yes,
  43. No,
  44. };
  45. static float compute_auto_height_for_block_level_element(Box const&, ConsiderFloats consider_floats = ConsiderFloats::Yes);
  46. ShrinkToFitResult calculate_shrink_to_fit_widths(Box&);
  47. void layout_absolutely_positioned_element(Box&);
  48. void compute_width_for_absolutely_positioned_element(Box&);
  49. void compute_width_for_absolutely_positioned_non_replaced_element(Box&);
  50. void compute_width_for_absolutely_positioned_replaced_element(ReplacedBox&);
  51. void compute_height_for_absolutely_positioned_element(Box&);
  52. void compute_height_for_absolutely_positioned_non_replaced_element(Box&);
  53. void compute_height_for_absolutely_positioned_replaced_element(ReplacedBox&);
  54. Type m_type {};
  55. FormattingContext* m_parent { nullptr };
  56. Box& m_context_box;
  57. };
  58. }