Frequency.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Frequency {
  12. public:
  13. enum class Type {
  14. Calculated,
  15. Hz,
  16. kHz
  17. };
  18. static Optional<Type> unit_from_name(StringView);
  19. Frequency(int value, Type type);
  20. Frequency(float value, Type type);
  21. static Frequency make_calculated(NonnullRefPtr<CalculatedStyleValue>);
  22. static Frequency make_hertz(float);
  23. Frequency percentage_of(Percentage const&) const;
  24. bool is_calculated() const { return m_type == Type::Calculated; }
  25. NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const;
  26. String to_string() const;
  27. float to_hertz() const;
  28. bool operator==(Frequency const& other) const
  29. {
  30. if (is_calculated())
  31. return m_calculated_style == other.m_calculated_style;
  32. return m_type == other.m_type && m_value == other.m_value;
  33. }
  34. bool operator!=(Frequency const& other) const
  35. {
  36. return !(*this == other);
  37. }
  38. private:
  39. StringView unit_name() const;
  40. Type m_type;
  41. float m_value { 0 };
  42. RefPtr<CalculatedStyleValue> m_calculated_style;
  43. };
  44. }
  45. template<>
  46. struct AK::Formatter<Web::CSS::Frequency> : Formatter<StringView> {
  47. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Frequency const& frequency)
  48. {
  49. return Formatter<StringView>::format(builder, frequency.to_string());
  50. }
  51. };