PixelUnits.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. static constexpr i32 fractional_bits = 6;
  42. static constexpr i32 fixed_point_denominator = 1 << fractional_bits;
  43. static constexpr i32 radix_mask = fixed_point_denominator - 1;
  44. static constexpr i32 max_integer_value = NumericLimits<int>::max() >> fractional_bits;
  45. static constexpr i32 min_integer_value = NumericLimits<int>::min() >> fractional_bits;
  46. CSSPixels() = default;
  47. CSSPixels(int value);
  48. CSSPixels(unsigned int value);
  49. CSSPixels(unsigned long value);
  50. CSSPixels(float value);
  51. CSSPixels(double value);
  52. static CSSPixels from_raw(int value)
  53. {
  54. CSSPixels res;
  55. res.set_raw_value(value);
  56. return res;
  57. }
  58. float to_float() const;
  59. double to_double() const;
  60. int to_int() const;
  61. inline int raw_value() const { return m_value; }
  62. inline void set_raw_value(int value) { m_value = value; }
  63. bool might_be_saturated() const;
  64. bool operator==(CSSPixels const& other) const;
  65. explicit operator double() const { return to_double(); }
  66. CSSPixels& operator++();
  67. CSSPixels& operator--();
  68. int operator<=>(CSSPixels const& other) const;
  69. CSSPixels operator+() const;
  70. CSSPixels operator-() const;
  71. CSSPixels operator+(CSSPixels const& other) const;
  72. CSSPixels operator-(CSSPixels const& other) const;
  73. CSSPixels operator*(CSSPixels const& other) const;
  74. CSSPixels operator/(CSSPixels const& other) const;
  75. CSSPixels& operator+=(CSSPixels const& other);
  76. CSSPixels& operator-=(CSSPixels const& other);
  77. CSSPixels& operator*=(CSSPixels const& other);
  78. CSSPixels& operator/=(CSSPixels const& other);
  79. CSSPixels abs() const;
  80. private:
  81. i32 m_value { 0 };
  82. };
  83. inline bool operator==(CSSPixels left, int right) { return left == CSSPixels(right); }
  84. inline bool operator==(CSSPixels left, float right) { return left.to_float() == right; }
  85. inline bool operator==(CSSPixels left, double right) { return left.to_double() == right; }
  86. inline bool operator>(CSSPixels left, int right) { return left > CSSPixels(right); }
  87. inline bool operator>(CSSPixels left, float right) { return left.to_float() > right; }
  88. inline bool operator>(CSSPixels left, double right) { return left.to_double() > right; }
  89. inline bool operator<(CSSPixels left, int right) { return left < CSSPixels(right); }
  90. inline bool operator<(CSSPixels left, float right) { return left.to_float() < right; }
  91. inline bool operator<(CSSPixels left, double right) { return left.to_double() < right; }
  92. inline CSSPixels operator*(CSSPixels left, int right) { return left * CSSPixels(right); }
  93. inline CSSPixels operator*(CSSPixels left, unsigned long right) { return left * CSSPixels(right); }
  94. inline float operator*(CSSPixels left, float right) { return left.to_float() * right; }
  95. inline double operator*(CSSPixels left, double right) { return left.to_double() * right; }
  96. inline CSSPixels operator*(int left, CSSPixels right) { return right * CSSPixels(left); }
  97. inline CSSPixels operator*(unsigned long left, CSSPixels right) { return right * CSSPixels(left); }
  98. inline float operator*(float left, CSSPixels right) { return right.to_float() * left; }
  99. inline double operator*(double left, CSSPixels right) { return right.to_double() * left; }
  100. inline CSSPixels operator/(CSSPixels left, int right) { return left / CSSPixels(right); }
  101. inline CSSPixels operator/(CSSPixels left, unsigned long right) { return left / CSSPixels(right); }
  102. inline float operator/(CSSPixels left, float right) { return left.to_float() / right; }
  103. inline double operator/(CSSPixels left, double right) { return left.to_double() / right; }
  104. using CSSPixelLine = Gfx::Line<CSSPixels>;
  105. using CSSPixelPoint = Gfx::Point<CSSPixels>;
  106. using CSSPixelRect = Gfx::Rect<CSSPixels>;
  107. using CSSPixelSize = Gfx::Size<CSSPixels>;
  108. using DevicePixelLine = Gfx::Line<DevicePixels>;
  109. using DevicePixelPoint = Gfx::Point<DevicePixels>;
  110. using DevicePixelRect = Gfx::Rect<DevicePixels>;
  111. using DevicePixelSize = Gfx::Size<DevicePixels>;
  112. }
  113. inline Web::CSSPixels abs(Web::CSSPixels const& value)
  114. {
  115. return value.abs();
  116. }
  117. constexpr Web::CSSPixels floor(Web::CSSPixels const& value)
  118. {
  119. // FIXME: Actually floor value
  120. return value;
  121. }
  122. constexpr Web::CSSPixels ceil(Web::CSSPixels const& value)
  123. {
  124. // FIXME: Actually ceil value
  125. return value;
  126. }
  127. constexpr Web::CSSPixels round(Web::CSSPixels const& value)
  128. {
  129. // FIXME: Actually round value
  130. return value;
  131. }
  132. constexpr Web::DevicePixels abs(Web::DevicePixels const& value)
  133. {
  134. return AK::abs(value.value());
  135. }
  136. namespace AK {
  137. template<>
  138. struct Traits<Web::CSSPixels> : public GenericTraits<Web::CSSPixels> {
  139. static unsigned hash(Web::CSSPixels const& key)
  140. {
  141. VERIFY(isfinite(key.to_double()));
  142. return Traits<double>::hash(key.to_double());
  143. }
  144. static bool equals(Web::CSSPixels const& a, Web::CSSPixels const& b)
  145. {
  146. return a == b;
  147. }
  148. };
  149. template<>
  150. struct Traits<Web::DevicePixels> : public GenericTraits<Web::DevicePixels> {
  151. static unsigned hash(Web::DevicePixels const& key)
  152. {
  153. return Traits<Web::DevicePixels::Type>::hash(key.value());
  154. }
  155. static bool equals(Web::DevicePixels const& a, Web::DevicePixels const& b)
  156. {
  157. return a == b;
  158. }
  159. };
  160. template<>
  161. struct Formatter<Web::CSSPixels> : Formatter<double> {
  162. ErrorOr<void> format(FormatBuilder& builder, Web::CSSPixels const& value)
  163. {
  164. return Formatter<double>::format(builder, value.to_double());
  165. }
  166. };
  167. template<>
  168. struct Formatter<Web::DevicePixels> : Formatter<Web::DevicePixels::Type> {
  169. ErrorOr<void> format(FormatBuilder& builder, Web::DevicePixels const& value)
  170. {
  171. return Formatter<Web::DevicePixels::Type>::format(builder, value.value());
  172. }
  173. };
  174. }