Resolution.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(int value, Type type);
  19. Resolution(float value, Type type);
  20. ErrorOr<String> to_string() const;
  21. float to_dots_per_pixel() const;
  22. bool operator==(Resolution const& other) const
  23. {
  24. return m_type == other.m_type && m_value == other.m_value;
  25. }
  26. int operator<=>(Resolution const& other) const
  27. {
  28. auto this_dots_per_pixel = to_dots_per_pixel();
  29. auto other_dots_per_pixel = other.to_dots_per_pixel();
  30. if (this_dots_per_pixel < other_dots_per_pixel)
  31. return -1;
  32. if (this_dots_per_pixel > other_dots_per_pixel)
  33. return 1;
  34. return 0;
  35. }
  36. private:
  37. StringView unit_name() const;
  38. Type m_type;
  39. float m_value { 0 };
  40. };
  41. }