FormattingContext.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. virtual void parent_context_did_dimension_child_root_box() { }
  33. protected:
  34. FormattingContext(Type, Box&, FormattingContext* parent = nullptr);
  35. OwnPtr<FormattingContext> layout_inside(Box&, LayoutMode);
  36. struct ShrinkToFitResult {
  37. float preferred_width { 0 };
  38. float preferred_minimum_width { 0 };
  39. };
  40. static float tentative_width_for_replaced_element(const ReplacedBox&, const CSS::Length& width);
  41. static float tentative_height_for_replaced_element(const ReplacedBox&, const CSS::Length& height);
  42. enum ConsiderFloats {
  43. Yes,
  44. No,
  45. };
  46. static float compute_auto_height_for_block_level_element(Box const&, ConsiderFloats consider_floats = ConsiderFloats::Yes);
  47. ShrinkToFitResult calculate_shrink_to_fit_widths(Box&);
  48. void layout_absolutely_positioned_element(Box&);
  49. void compute_width_for_absolutely_positioned_element(Box&);
  50. void compute_width_for_absolutely_positioned_non_replaced_element(Box&);
  51. void compute_width_for_absolutely_positioned_replaced_element(ReplacedBox&);
  52. void compute_height_for_absolutely_positioned_element(Box&);
  53. void compute_height_for_absolutely_positioned_non_replaced_element(Box&);
  54. void compute_height_for_absolutely_positioned_replaced_element(ReplacedBox&);
  55. Type m_type {};
  56. FormattingContext* m_parent { nullptr };
  57. Box& m_context_box;
  58. };
  59. }