Angle.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefPtr.h>
  8. #include <AK/String.h>
  9. #include <LibWeb/Forward.h>
  10. namespace Web::CSS {
  11. class Angle {
  12. public:
  13. enum class Type {
  14. Calculated,
  15. Deg,
  16. Grad,
  17. Rad,
  18. Turn,
  19. };
  20. static Optional<Type> unit_from_name(StringView);
  21. Angle(int value, Type type);
  22. Angle(float value, Type type);
  23. static Angle make_calculated(NonnullRefPtr<CalculatedStyleValue>);
  24. static Angle make_degrees(float);
  25. Angle percentage_of(Percentage const&) const;
  26. bool is_calculated() const { return m_type == Type::Calculated; }
  27. String to_string() const;
  28. float to_degrees() const;
  29. bool operator==(Angle const& other) const
  30. {
  31. if (is_calculated())
  32. return m_calculated_style == other.m_calculated_style;
  33. return m_type == other.m_type && m_value == other.m_value;
  34. }
  35. bool operator!=(Angle const& other) const
  36. {
  37. return !(*this == other);
  38. }
  39. private:
  40. StringView unit_name() const;
  41. Type m_type;
  42. float m_value { 0 };
  43. RefPtr<CalculatedStyleValue> m_calculated_style;
  44. };
  45. }
  46. template<>
  47. struct AK::Formatter<Web::CSS::Angle> : Formatter<StringView> {
  48. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Angle const& angle)
  49. {
  50. return Formatter<StringView>::format(builder, angle.to_string());
  51. }
  52. };