Angle.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(int value, Type type);
  20. Angle(float value, Type type);
  21. static Angle make_degrees(float);
  22. Angle percentage_of(Percentage const&) const;
  23. ErrorOr<String> to_string() const;
  24. float to_degrees() const;
  25. Type type() const { return m_type; }
  26. float raw_value() const { return m_value; }
  27. bool operator==(Angle const& other) const
  28. {
  29. return m_type == other.m_type && m_value == other.m_value;
  30. }
  31. private:
  32. StringView unit_name() const;
  33. Type m_type;
  34. float m_value { 0 };
  35. };
  36. }
  37. template<>
  38. struct AK::Formatter<Web::CSS::Angle> : Formatter<StringView> {
  39. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Angle const& angle)
  40. {
  41. return Formatter<StringView>::format(builder, TRY(angle.to_string()));
  42. }
  43. };