PixelUnits.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. * Copyright (c) 2012-2023, Apple Inc. All rights reserved.
  4. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Concepts.h>
  10. #include <AK/DistinctNumeric.h>
  11. #include <AK/Traits.h>
  12. #include <LibGfx/Forward.h>
  13. #include <math.h>
  14. namespace Web {
  15. /// DevicePixels: A position or length on the physical display.
  16. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, DevicePixels, Arithmetic, CastToUnderlying, Comparison, Increment);
  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 bool operator<=(DevicePixels left, T right) { return left.value() <= right; }
  29. template<Integral T>
  30. constexpr DevicePixels operator*(DevicePixels left, T right) { return left.value() * right; }
  31. template<Integral T>
  32. constexpr DevicePixels operator*(T left, DevicePixels right) { return right * left; }
  33. template<Integral T>
  34. constexpr DevicePixels operator/(DevicePixels left, T right) { return left.value() / right; }
  35. template<Integral T>
  36. constexpr DevicePixels operator%(DevicePixels left, T right) { return left.value() % right; }
  37. /// CSSPixels: A position or length in CSS "reference pixels", independent of zoom or screen DPI.
  38. /// See https://www.w3.org/TR/css-values-3/#reference-pixel
  39. class CSSPixels {
  40. public:
  41. CSSPixels() = default;
  42. CSSPixels(int value);
  43. CSSPixels(unsigned int value);
  44. CSSPixels(unsigned long value);
  45. CSSPixels(float value);
  46. CSSPixels(double value);
  47. static CSSPixels from_raw(int value)
  48. {
  49. CSSPixels res;
  50. res.set_raw_value(value);
  51. return res;
  52. }
  53. float to_float() const;
  54. double to_double() const;
  55. int to_int() const;
  56. inline int raw_value() const { return m_value; }
  57. inline void set_raw_value(int value) { m_value = value; }
  58. bool might_be_saturated() const;
  59. bool operator==(CSSPixels const& other) const;
  60. explicit operator double() const { return to_double(); }
  61. CSSPixels& operator++();
  62. CSSPixels& operator--();
  63. int operator<=>(CSSPixels const& other) const;
  64. CSSPixels operator+() const;
  65. CSSPixels operator-() const;
  66. CSSPixels operator+(CSSPixels const& other) const;
  67. CSSPixels operator-(CSSPixels const& other) const;
  68. CSSPixels operator*(CSSPixels const& other) const;
  69. CSSPixels operator/(CSSPixels const& other) const;
  70. CSSPixels& operator+=(CSSPixels const& other);
  71. CSSPixels& operator-=(CSSPixels const& other);
  72. CSSPixels& operator*=(CSSPixels const& other);
  73. CSSPixels& operator/=(CSSPixels const& other);
  74. CSSPixels abs() const;
  75. private:
  76. i32 m_value { 0 };
  77. };
  78. inline bool operator==(CSSPixels left, int right) { return left == CSSPixels(right); }
  79. inline bool operator==(CSSPixels left, float right) { return left.to_float() == right; }
  80. inline bool operator==(CSSPixels left, double right) { return left.to_double() == right; }
  81. inline bool operator>(CSSPixels left, int right) { return left > CSSPixels(right); }
  82. inline bool operator>(CSSPixels left, float right) { return left.to_float() > right; }
  83. inline bool operator>(CSSPixels left, double right) { return left.to_double() > right; }
  84. inline bool operator<(CSSPixels left, int right) { return left < CSSPixels(right); }
  85. inline bool operator<(CSSPixels left, float right) { return left.to_float() < right; }
  86. inline bool operator<(CSSPixels left, double right) { return left.to_double() < right; }
  87. inline CSSPixels operator*(CSSPixels left, int right) { return left * CSSPixels(right); }
  88. inline CSSPixels operator*(CSSPixels left, unsigned long right) { return left * CSSPixels(right); }
  89. inline float operator*(CSSPixels left, float right) { return left.to_float() * right; }
  90. inline double operator*(CSSPixels left, double right) { return left.to_double() * right; }
  91. inline CSSPixels operator*(int left, CSSPixels right) { return right * CSSPixels(left); }
  92. inline CSSPixels operator*(unsigned long left, CSSPixels right) { return right * CSSPixels(left); }
  93. inline float operator*(float left, CSSPixels right) { return right.to_float() * left; }
  94. inline double operator*(double left, CSSPixels right) { return right.to_double() * left; }
  95. inline CSSPixels operator/(CSSPixels left, int right) { return left / CSSPixels(right); }
  96. inline CSSPixels operator/(CSSPixels left, unsigned long right) { return left / CSSPixels(right); }
  97. inline float operator/(CSSPixels left, float right) { return left.to_float() / right; }
  98. inline double operator/(CSSPixels left, double right) { return left.to_double() / right; }
  99. using CSSPixelLine = Gfx::Line<CSSPixels>;
  100. using CSSPixelPoint = Gfx::Point<CSSPixels>;
  101. using CSSPixelRect = Gfx::Rect<CSSPixels>;
  102. using CSSPixelSize = Gfx::Size<CSSPixels>;
  103. using DevicePixelLine = Gfx::Line<DevicePixels>;
  104. using DevicePixelPoint = Gfx::Point<DevicePixels>;
  105. using DevicePixelRect = Gfx::Rect<DevicePixels>;
  106. using DevicePixelSize = Gfx::Size<DevicePixels>;
  107. }
  108. inline Web::CSSPixels abs(Web::CSSPixels const& value)
  109. {
  110. return value.abs();
  111. }
  112. constexpr Web::CSSPixels floor(Web::CSSPixels const& value)
  113. {
  114. // FIXME: Actually floor value
  115. return value;
  116. }
  117. constexpr Web::CSSPixels ceil(Web::CSSPixels const& value)
  118. {
  119. // FIXME: Actually ceil value
  120. return value;
  121. }
  122. constexpr Web::CSSPixels round(Web::CSSPixels const& value)
  123. {
  124. // FIXME: Actually round value
  125. return value;
  126. }
  127. constexpr Web::DevicePixels abs(Web::DevicePixels const& value)
  128. {
  129. return AK::abs(value.value());
  130. }
  131. namespace AK {
  132. template<>
  133. struct Traits<Web::CSSPixels> : public GenericTraits<Web::CSSPixels> {
  134. static unsigned hash(Web::CSSPixels const& key)
  135. {
  136. VERIFY(isfinite(key.to_double()));
  137. return Traits<double>::hash(key.to_double());
  138. }
  139. static bool equals(Web::CSSPixels const& a, Web::CSSPixels const& b)
  140. {
  141. return a == b;
  142. }
  143. };
  144. template<>
  145. struct Traits<Web::DevicePixels> : public GenericTraits<Web::DevicePixels> {
  146. static unsigned hash(Web::DevicePixels const& key)
  147. {
  148. return Traits<Web::DevicePixels::Type>::hash(key.value());
  149. }
  150. static bool equals(Web::DevicePixels const& a, Web::DevicePixels const& b)
  151. {
  152. return a == b;
  153. }
  154. };
  155. template<>
  156. struct Formatter<Web::CSSPixels> : Formatter<double> {
  157. ErrorOr<void> format(FormatBuilder& builder, Web::CSSPixels const& value)
  158. {
  159. return Formatter<double>::format(builder, value.to_double());
  160. }
  161. };
  162. template<>
  163. struct Formatter<Web::DevicePixels> : Formatter<Web::DevicePixels::Type> {
  164. ErrorOr<void> format(FormatBuilder& builder, Web::DevicePixels const& value)
  165. {
  166. return Formatter<Web::DevicePixels::Type>::format(builder, value.value());
  167. }
  168. };
  169. }