PixelUnits.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/Concepts.h>
  8. #include <AK/DistinctNumeric.h>
  9. #include <AK/Traits.h>
  10. #include <LibGfx/Forward.h>
  11. #include <math.h>
  12. namespace Web {
  13. /// DevicePixels: A position or length on the physical display.
  14. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, DevicePixels, Arithmetic, CastToUnderlying, Comparison, Increment);
  15. template<Integral T>
  16. constexpr bool operator==(DevicePixels left, T right) { return left.value() == right; }
  17. template<Integral T>
  18. constexpr bool operator!=(DevicePixels left, T right) { return left.value() != right; }
  19. template<Integral T>
  20. constexpr bool operator>(DevicePixels left, T right) { return left.value() > right; }
  21. template<Integral T>
  22. constexpr bool operator<(DevicePixels left, T right) { return left.value() < right; }
  23. template<Integral T>
  24. constexpr bool operator>=(DevicePixels left, T right) { return left.value() >= right; }
  25. template<Integral T>
  26. constexpr bool operator<=(DevicePixels left, T right) { return left.value() <= right; }
  27. template<Integral T>
  28. constexpr DevicePixels operator*(DevicePixels left, T right) { return left.value() * right; }
  29. template<Integral T>
  30. constexpr DevicePixels operator*(T left, DevicePixels right) { return right * left; }
  31. template<Integral T>
  32. constexpr DevicePixels operator/(DevicePixels left, T right) { return left.value() / right; }
  33. template<Integral T>
  34. constexpr DevicePixels operator%(DevicePixels left, T right) { return left.value() % right; }
  35. /// CSSPixels: A position or length in CSS "reference pixels", independent of zoom or screen DPI.
  36. /// See https://www.w3.org/TR/css-values-3/#reference-pixel
  37. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(float, CSSPixels, Arithmetic, CastToUnderlying, Comparison, Increment);
  38. template<Arithmetic T>
  39. constexpr bool operator==(CSSPixels left, T right) { return left.value() == right; }
  40. template<Arithmetic T>
  41. constexpr bool operator!=(CSSPixels left, T right) { return left.value() != right; }
  42. template<Arithmetic T>
  43. constexpr bool operator>(CSSPixels left, T right) { return left.value() > right; }
  44. template<Arithmetic T>
  45. constexpr bool operator<(CSSPixels left, T right) { return left.value() < right; }
  46. template<Arithmetic T>
  47. constexpr bool operator>=(CSSPixels left, T right) { return left.value() >= right; }
  48. template<Arithmetic T>
  49. constexpr bool operator<=(CSSPixels left, T right) { return left.value() <= right; }
  50. template<Arithmetic T>
  51. constexpr CSSPixels operator*(CSSPixels left, T right) { return left.value() * right; }
  52. template<Arithmetic T>
  53. constexpr CSSPixels operator*(T left, CSSPixels right) { return right * left; }
  54. template<Arithmetic T>
  55. constexpr CSSPixels operator/(CSSPixels left, T right) { return left.value() / right; }
  56. template<Arithmetic T>
  57. constexpr CSSPixels operator%(CSSPixels left, T right) { return left.value() % right; }
  58. using CSSPixelLine = Gfx::Line<CSSPixels>;
  59. using CSSPixelPoint = Gfx::Point<CSSPixels>;
  60. using CSSPixelRect = Gfx::Rect<CSSPixels>;
  61. using CSSPixelSize = Gfx::Size<CSSPixels>;
  62. using DevicePixelLine = Gfx::Line<DevicePixels>;
  63. using DevicePixelPoint = Gfx::Point<DevicePixels>;
  64. using DevicePixelRect = Gfx::Rect<DevicePixels>;
  65. using DevicePixelSize = Gfx::Size<DevicePixels>;
  66. }
  67. constexpr Web::CSSPixels floor(Web::CSSPixels const& value)
  68. {
  69. return ::floorf(value.value());
  70. }
  71. constexpr Web::CSSPixels ceil(Web::CSSPixels const& value)
  72. {
  73. return ::ceilf(value.value());
  74. }
  75. constexpr Web::CSSPixels round(Web::CSSPixels const& value)
  76. {
  77. return ::roundf(value.value());
  78. }
  79. constexpr Web::CSSPixels fmod(Web::CSSPixels const& x, Web::CSSPixels const& y)
  80. {
  81. return ::fmodf(x.value(), y.value());
  82. }
  83. constexpr Web::CSSPixels abs(Web::CSSPixels const& value)
  84. {
  85. return AK::abs(value.value());
  86. }
  87. constexpr Web::DevicePixels abs(Web::DevicePixels const& value)
  88. {
  89. return AK::abs(value.value());
  90. }
  91. namespace AK {
  92. template<>
  93. struct Traits<Web::CSSPixels> : public GenericTraits<Web::CSSPixels> {
  94. static unsigned hash(Web::CSSPixels const& key)
  95. {
  96. return Traits<Web::CSSPixels::Type>::hash(key.value());
  97. }
  98. static bool equals(Web::CSSPixels const& a, Web::CSSPixels const& b)
  99. {
  100. return a == b;
  101. }
  102. };
  103. template<>
  104. struct Traits<Web::DevicePixels> : public GenericTraits<Web::DevicePixels> {
  105. static unsigned hash(Web::DevicePixels const& key)
  106. {
  107. return Traits<Web::DevicePixels::Type>::hash(key.value());
  108. }
  109. static bool equals(Web::DevicePixels const& a, Web::DevicePixels const& b)
  110. {
  111. return a == b;
  112. }
  113. };
  114. template<>
  115. struct Formatter<Web::CSSPixels> : Formatter<Web::CSSPixels::Type> {
  116. ErrorOr<void> format(FormatBuilder& builder, Web::CSSPixels const& value)
  117. {
  118. return Formatter<Web::CSSPixels::Type>::format(builder, value.value());
  119. }
  120. };
  121. template<>
  122. struct Formatter<Web::DevicePixels> : Formatter<Web::DevicePixels::Type> {
  123. ErrorOr<void> format(FormatBuilder& builder, Web::DevicePixels const& value)
  124. {
  125. return Formatter<Web::DevicePixels::Type>::format(builder, value.value());
  126. }
  127. };
  128. }