PixelUnits.h 7.0 KB

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