Size.h 2.2 KB

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