Size.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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&, Length const& reference_value) const;
  43. CSS::Length resolved(Layout::Node const&, CSSPixels reference_value) const;
  44. [[nodiscard]] CSSPixels to_px(Layout::Node const&, CSSPixels reference_value) const;
  45. bool contains_percentage() const;
  46. CalculatedStyleValue const& calculated() const
  47. {
  48. VERIFY(is_calculated());
  49. return m_length_percentage.calculated();
  50. }
  51. CSS::Length const& length() const
  52. {
  53. VERIFY(is_length());
  54. return m_length_percentage.length();
  55. }
  56. CSS::Percentage const& percentage() const
  57. {
  58. VERIFY(is_percentage());
  59. return m_length_percentage.percentage();
  60. }
  61. CSS::Length const& fit_content_available_space() const
  62. {
  63. VERIFY(is_fit_content());
  64. return m_length_percentage.length();
  65. }
  66. String to_string() const;
  67. private:
  68. Size(Type type, LengthPercentage);
  69. Type m_type {};
  70. CSS::LengthPercentage m_length_percentage;
  71. };
  72. }
  73. template<>
  74. struct AK::Formatter<Web::CSS::Size> : Formatter<StringView> {
  75. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Size const& size)
  76. {
  77. return Formatter<StringView>::format(builder, size.to_string());
  78. }
  79. };