Dimension.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Variant.h>
  8. #include <LibWeb/CSS/Angle.h>
  9. #include <LibWeb/CSS/Frequency.h>
  10. #include <LibWeb/CSS/Length.h>
  11. #include <LibWeb/CSS/Percentage.h>
  12. #include <LibWeb/CSS/PercentageOr.h>
  13. #include <LibWeb/CSS/Resolution.h>
  14. #include <LibWeb/CSS/Time.h>
  15. namespace Web::CSS::Parser {
  16. class Dimension {
  17. public:
  18. Dimension(Angle&& value)
  19. : m_value(move(value))
  20. {
  21. }
  22. Dimension(Frequency&& value)
  23. : m_value(move(value))
  24. {
  25. }
  26. Dimension(Length&& value)
  27. : m_value(move(value))
  28. {
  29. }
  30. Dimension(Percentage&& value)
  31. : m_value(move(value))
  32. {
  33. }
  34. Dimension(Resolution&& value)
  35. : m_value(move(value))
  36. {
  37. }
  38. Dimension(Time&& value)
  39. : m_value(move(value))
  40. {
  41. }
  42. bool is_angle() const { return m_value.has<Angle>(); }
  43. Angle angle() const { return m_value.get<Angle>(); }
  44. bool is_angle_percentage() const { return is_angle() || is_percentage(); }
  45. AnglePercentage angle_percentage() const
  46. {
  47. if (is_angle())
  48. return angle();
  49. return percentage();
  50. }
  51. bool is_frequency() const { return m_value.has<Frequency>(); }
  52. Frequency frequency() const { return m_value.get<Frequency>(); }
  53. bool is_frequency_percentage() const { return is_frequency() || is_percentage(); }
  54. FrequencyPercentage frequency_percentage() const
  55. {
  56. if (is_frequency())
  57. return frequency();
  58. return percentage();
  59. }
  60. bool is_length() const { return m_value.has<Length>(); }
  61. Length length() const { return m_value.get<Length>(); }
  62. bool is_length_percentage() const { return is_length() || is_percentage(); }
  63. LengthPercentage length_percentage() const
  64. {
  65. if (is_length())
  66. return length();
  67. return percentage();
  68. }
  69. bool is_percentage() const { return m_value.has<Percentage>(); }
  70. Percentage percentage() const { return m_value.get<Percentage>(); }
  71. bool is_resolution() const { return m_value.has<Resolution>(); }
  72. Resolution resolution() const { return m_value.get<Resolution>(); }
  73. bool is_time() const { return m_value.has<Time>(); }
  74. Time time() const { return m_value.get<Time>(); }
  75. bool is_time_percentage() const { return is_time() || is_percentage(); }
  76. TimePercentage time_percentage() const
  77. {
  78. if (is_time())
  79. return time();
  80. return percentage();
  81. }
  82. private:
  83. Variant<Angle, Frequency, Length, Percentage, Resolution, Time> m_value;
  84. };
  85. }