Resolution.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 Resolution {
  11. public:
  12. enum class Type {
  13. Dpi,
  14. Dpcm,
  15. Dppx,
  16. };
  17. static Optional<Type> unit_from_name(StringView);
  18. Resolution(double value, Type type);
  19. static Resolution make_dots_per_pixel(double);
  20. String to_string() const;
  21. double to_dots_per_pixel() const;
  22. Type type() const { return m_type; }
  23. double raw_value() const { return m_value; }
  24. StringView unit_name() const;
  25. bool operator==(Resolution const& other) const
  26. {
  27. return m_type == other.m_type && m_value == other.m_value;
  28. }
  29. int operator<=>(Resolution const& other) const
  30. {
  31. auto this_dots_per_pixel = to_dots_per_pixel();
  32. auto other_dots_per_pixel = other.to_dots_per_pixel();
  33. if (this_dots_per_pixel < other_dots_per_pixel)
  34. return -1;
  35. if (this_dots_per_pixel > other_dots_per_pixel)
  36. return 1;
  37. return 0;
  38. }
  39. private:
  40. Type m_type;
  41. double m_value { 0 };
  42. };
  43. }