Angle.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/String.h>
  8. #include <LibWeb/Forward.h>
  9. namespace Web::CSS {
  10. class Angle {
  11. public:
  12. enum class Type {
  13. Deg,
  14. Grad,
  15. Rad,
  16. Turn,
  17. };
  18. static Optional<Type> unit_from_name(StringView);
  19. Angle(double value, Type type);
  20. static Angle make_degrees(double);
  21. Angle percentage_of(Percentage const&) const;
  22. String to_string() const;
  23. double to_degrees() const;
  24. double to_radians() const;
  25. Type type() const { return m_type; }
  26. double raw_value() const { return m_value; }
  27. StringView unit_name() const;
  28. bool operator==(Angle const& other) const
  29. {
  30. return m_type == other.m_type && m_value == other.m_value;
  31. }
  32. int operator<=>(Angle const& other) const
  33. {
  34. auto this_degrees = to_degrees();
  35. auto other_degrees = other.to_degrees();
  36. if (this_degrees < other_degrees)
  37. return -1;
  38. if (this_degrees > other_degrees)
  39. return 1;
  40. return 0;
  41. }
  42. static Angle resolve_calculated(NonnullRefPtr<CSSMathValue> const&, Layout::Node const&, Angle const& reference_value);
  43. private:
  44. Type m_type;
  45. double m_value { 0 };
  46. };
  47. }
  48. template<>
  49. struct AK::Formatter<Web::CSS::Angle> : Formatter<StringView> {
  50. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Angle const& angle)
  51. {
  52. return Formatter<StringView>::format(builder, angle.to_string());
  53. }
  54. };