Dimension.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/Flex.h>
  10. #include <LibWeb/CSS/Frequency.h>
  11. #include <LibWeb/CSS/Length.h>
  12. #include <LibWeb/CSS/Percentage.h>
  13. #include <LibWeb/CSS/PercentageOr.h>
  14. #include <LibWeb/CSS/Resolution.h>
  15. #include <LibWeb/CSS/Time.h>
  16. namespace Web::CSS::Parser {
  17. class Dimension {
  18. public:
  19. Dimension(Angle&& value)
  20. : m_value(move(value))
  21. {
  22. }
  23. Dimension(Flex&& value)
  24. : m_value(move(value))
  25. {
  26. }
  27. Dimension(Frequency&& value)
  28. : m_value(move(value))
  29. {
  30. }
  31. Dimension(Length&& value)
  32. : m_value(move(value))
  33. {
  34. }
  35. Dimension(Percentage&& value)
  36. : m_value(move(value))
  37. {
  38. }
  39. Dimension(Resolution&& value)
  40. : m_value(move(value))
  41. {
  42. }
  43. Dimension(Time&& value)
  44. : m_value(move(value))
  45. {
  46. }
  47. bool is_angle() const { return m_value.has<Angle>(); }
  48. Angle angle() const { return m_value.get<Angle>(); }
  49. bool is_angle_percentage() const { return is_angle() || is_percentage(); }
  50. AnglePercentage angle_percentage() const
  51. {
  52. if (is_angle())
  53. return angle();
  54. return percentage();
  55. }
  56. bool is_flex() const { return m_value.has<Flex>(); }
  57. Flex flex() const { return m_value.get<Flex>(); }
  58. bool is_frequency() const { return m_value.has<Frequency>(); }
  59. Frequency frequency() const { return m_value.get<Frequency>(); }
  60. bool is_frequency_percentage() const { return is_frequency() || is_percentage(); }
  61. FrequencyPercentage frequency_percentage() const
  62. {
  63. if (is_frequency())
  64. return frequency();
  65. return percentage();
  66. }
  67. bool is_length() const { return m_value.has<Length>(); }
  68. Length length() const { return m_value.get<Length>(); }
  69. bool is_length_percentage() const { return is_length() || is_percentage(); }
  70. LengthPercentage length_percentage() const
  71. {
  72. if (is_length())
  73. return length();
  74. return percentage();
  75. }
  76. bool is_percentage() const { return m_value.has<Percentage>(); }
  77. Percentage percentage() const { return m_value.get<Percentage>(); }
  78. bool is_resolution() const { return m_value.has<Resolution>(); }
  79. Resolution resolution() const { return m_value.get<Resolution>(); }
  80. bool is_time() const { return m_value.has<Time>(); }
  81. Time time() const { return m_value.get<Time>(); }
  82. bool is_time_percentage() const { return is_time() || is_percentage(); }
  83. TimePercentage time_percentage() const
  84. {
  85. if (is_time())
  86. return time();
  87. return percentage();
  88. }
  89. private:
  90. Variant<Angle, Flex, Frequency, Length, Percentage, Resolution, Time> m_value;
  91. };
  92. }