Angle.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. ErrorOr<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. private:
  37. StringView unit_name() const;
  38. Type m_type;
  39. float m_value { 0 };
  40. RefPtr<CalculatedStyleValue> m_calculated_style;
  41. };
  42. }
  43. template<>
  44. struct AK::Formatter<Web::CSS::Angle> : Formatter<StringView> {
  45. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Angle const& angle)
  46. {
  47. return Formatter<StringView>::format(builder, TRY(angle.to_string()));
  48. }
  49. };