Angle.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const;
  28. String to_string() const;
  29. float to_degrees() const;
  30. bool operator==(Angle const& other) const
  31. {
  32. if (is_calculated())
  33. return m_calculated_style == other.m_calculated_style;
  34. return m_type == other.m_type && m_value == other.m_value;
  35. }
  36. bool operator!=(Angle const& other) const
  37. {
  38. return !(*this == other);
  39. }
  40. private:
  41. StringView unit_name() const;
  42. Type m_type;
  43. float m_value { 0 };
  44. RefPtr<CalculatedStyleValue> m_calculated_style;
  45. };
  46. }
  47. template<>
  48. struct AK::Formatter<Web::CSS::Angle> : Formatter<StringView> {
  49. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Angle const& angle)
  50. {
  51. return Formatter<StringView>::format(builder, angle.to_string());
  52. }
  53. };