PixelUnits.h 7.2 KB

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