Size.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibWeb/CSS/Length.h>
  9. #include <LibWeb/CSS/PercentageOr.h>
  10. namespace Web::CSS {
  11. class Size {
  12. public:
  13. enum class Type {
  14. Auto,
  15. Calculated,
  16. Length,
  17. Percentage,
  18. MinContent,
  19. MaxContent,
  20. FitContent,
  21. None, // NOTE: This is only valid for max-width and max-height.
  22. };
  23. static Size make_auto();
  24. static Size make_px(CSSPixels);
  25. static Size make_length(Length);
  26. static Size make_percentage(Percentage);
  27. static Size make_calculated(NonnullRefPtr<CalculatedStyleValue>);
  28. static Size make_min_content();
  29. static Size make_max_content();
  30. static Size make_fit_content(Length available_space);
  31. static Size make_fit_content();
  32. static Size make_none();
  33. bool is_auto() const { return m_type == Type::Auto; }
  34. bool is_calculated() const { return m_type == Type::Calculated; }
  35. bool is_length() const { return m_type == Type::Length; }
  36. bool is_percentage() const { return m_type == Type::Percentage; }
  37. bool is_min_content() const { return m_type == Type::MinContent; }
  38. bool is_max_content() const { return m_type == Type::MaxContent; }
  39. bool is_fit_content() const { return m_type == Type::FitContent; }
  40. bool is_none() const { return m_type == Type::None; }
  41. // FIXME: This is a stopgap API that will go away once all layout code is aware of CSS::Size.
  42. CSS::Length resolved(Layout::Node const&, CSSPixels reference_value) const;
  43. [[nodiscard]] CSSPixels to_px(Layout::Node const&, CSSPixels reference_value) const;
  44. bool contains_percentage() const;
  45. CalculatedStyleValue const& calculated() const
  46. {
  47. VERIFY(is_calculated());
  48. return m_length_percentage.calculated();
  49. }
  50. CSS::Length const& length() const
  51. {
  52. VERIFY(is_length());
  53. return m_length_percentage.length();
  54. }
  55. CSS::Percentage const& percentage() const
  56. {
  57. VERIFY(is_percentage());
  58. return m_length_percentage.percentage();
  59. }
  60. CSS::Length const& fit_content_available_space() const
  61. {
  62. VERIFY(is_fit_content());
  63. return m_length_percentage.length();
  64. }
  65. String to_string() const;
  66. private:
  67. Size(Type type, LengthPercentage);
  68. Type m_type {};
  69. CSS::LengthPercentage m_length_percentage;
  70. };
  71. }
  72. template<>
  73. struct AK::Formatter<Web::CSS::Size> : Formatter<StringView> {
  74. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Size const& size)
  75. {
  76. return Formatter<StringView>::format(builder, size.to_string());
  77. }
  78. };