PixelUnits.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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/Debug.h>
  11. #include <AK/DistinctNumeric.h>
  12. #include <AK/Math.h>
  13. #include <AK/Traits.h>
  14. #include <LibGfx/Forward.h>
  15. #include <LibGfx/Rect.h>
  16. #include <math.h>
  17. namespace Web {
  18. /// DevicePixels: A position or length on the physical display.
  19. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, DevicePixels, Arithmetic, CastToUnderlying, Comparison, Increment);
  20. template<Integral T>
  21. constexpr bool operator==(DevicePixels left, T right) { return left.value() == right; }
  22. template<Integral T>
  23. constexpr bool operator!=(DevicePixels left, T right) { return left.value() != right; }
  24. template<Integral T>
  25. constexpr bool operator>(DevicePixels left, T right) { return left.value() > right; }
  26. template<Integral T>
  27. constexpr bool operator<(DevicePixels left, T right) { return left.value() < right; }
  28. template<Integral T>
  29. constexpr bool operator>=(DevicePixels left, T right) { return left.value() >= right; }
  30. template<Integral T>
  31. constexpr bool operator<=(DevicePixels left, T right) { return left.value() <= right; }
  32. template<Integral T>
  33. constexpr DevicePixels operator*(DevicePixels left, T right) { return left.value() * right; }
  34. template<Integral T>
  35. constexpr DevicePixels operator*(T left, DevicePixels right) { return right * left; }
  36. template<Integral T>
  37. constexpr DevicePixels operator/(DevicePixels left, T right) { return left.value() / right; }
  38. template<Integral T>
  39. constexpr DevicePixels operator%(DevicePixels left, T right) { return left.value() % right; }
  40. class CSSPixelFraction;
  41. /// CSSPixels: A position or length in CSS "reference pixels", independent of zoom or screen DPI.
  42. /// See https://www.w3.org/TR/css-values-3/#reference-pixel
  43. class CSSPixels {
  44. public:
  45. static constexpr i32 fractional_bits = 6;
  46. static constexpr i32 fixed_point_denominator = 1 << fractional_bits;
  47. static constexpr i32 radix_mask = fixed_point_denominator - 1;
  48. static constexpr i32 max_integer_value = NumericLimits<int>::max() >> fractional_bits;
  49. static constexpr i32 min_integer_value = NumericLimits<int>::min() >> fractional_bits;
  50. constexpr CSSPixels() = default;
  51. template<Signed I>
  52. constexpr CSSPixels(I value)
  53. {
  54. if (value > max_integer_value) [[unlikely]]
  55. m_value = NumericLimits<int>::max();
  56. else if (value < min_integer_value) [[unlikely]]
  57. m_value = NumericLimits<int>::min();
  58. else
  59. m_value = static_cast<int>(value) << fractional_bits;
  60. }
  61. template<FloatingPoint F>
  62. explicit CSSPixels(F value)
  63. {
  64. *this = nearest_value_for(value);
  65. }
  66. template<FloatingPoint F>
  67. static CSSPixels nearest_value_for(F value)
  68. {
  69. i32 raw_value = 0;
  70. if (!isnan(value))
  71. raw_value = AK::clamp_to_int(value * fixed_point_denominator);
  72. // Note: The resolution of CSSPixels is 0.015625, so care must be taken when converting
  73. // floats/doubles to CSSPixels as small values (such as scale factors) can underflow to zero,
  74. // or otherwise produce inaccurate results (when scaled back up).
  75. if (raw_value == 0 && value != 0)
  76. dbgln_if(LIBWEB_CSS_DEBUG, "CSSPixels: Conversion from float or double underflowed to zero");
  77. return from_raw(raw_value);
  78. }
  79. template<Unsigned U>
  80. constexpr CSSPixels(U value)
  81. {
  82. if (value > max_integer_value) [[unlikely]]
  83. m_value = NumericLimits<int>::max();
  84. else
  85. m_value = static_cast<int>(value) << fractional_bits;
  86. }
  87. static constexpr CSSPixels from_raw(int value)
  88. {
  89. CSSPixels res;
  90. res.set_raw_value(value);
  91. return res;
  92. }
  93. static constexpr CSSPixels min()
  94. {
  95. return from_raw(NumericLimits<int>::min());
  96. }
  97. static constexpr CSSPixels max()
  98. {
  99. return from_raw(NumericLimits<int>::max());
  100. }
  101. static constexpr CSSPixels smallest_positive_value()
  102. {
  103. return from_raw(1);
  104. }
  105. float to_float() const;
  106. double to_double() const;
  107. int to_int() const;
  108. constexpr int raw_value() const { return m_value; }
  109. constexpr void set_raw_value(int value) { m_value = value; }
  110. constexpr bool might_be_saturated() const { return raw_value() == NumericLimits<i32>::max() || raw_value() == NumericLimits<i32>::min(); }
  111. constexpr bool operator==(CSSPixels const& other) const = default;
  112. explicit operator double() const { return to_double(); }
  113. explicit operator float() const { return to_float(); }
  114. explicit operator int() const { return to_int(); }
  115. constexpr CSSPixels& operator++()
  116. {
  117. m_value = Checked<int>::saturating_add(m_value, fixed_point_denominator);
  118. return *this;
  119. }
  120. constexpr CSSPixels& operator--()
  121. {
  122. m_value = Checked<int>::saturating_sub(m_value, fixed_point_denominator);
  123. return *this;
  124. }
  125. constexpr int operator<=>(CSSPixels const& other) const
  126. {
  127. return raw_value() > other.raw_value()
  128. ? 1
  129. : raw_value() < other.raw_value()
  130. ? -1
  131. : 0;
  132. }
  133. constexpr CSSPixels operator+() const { return from_raw(+raw_value()); }
  134. constexpr CSSPixels operator-() const { return from_raw(-raw_value()); }
  135. constexpr CSSPixels operator+(CSSPixels const& other) const
  136. {
  137. return from_raw(Checked<int>::saturating_add(raw_value(), other.raw_value()));
  138. }
  139. constexpr CSSPixels operator-(CSSPixels const& other) const
  140. {
  141. return from_raw(Checked<int>::saturating_sub(raw_value(), other.raw_value()));
  142. }
  143. constexpr CSSPixels operator*(CSSPixels const& other) const
  144. {
  145. i64 value = raw_value();
  146. value *= other.raw_value();
  147. int int_value = AK::clamp_to_int(value >> fractional_bits);
  148. // Rounding:
  149. // If last bit cut off was 1:
  150. if (value & (1u << (fractional_bits - 1))) {
  151. // If any bit after was 1 as well
  152. if (value & (radix_mask >> 1u)) {
  153. // We need to round away from 0
  154. int_value = Checked<int>::saturating_add(int_value, 1);
  155. } else {
  156. // Otherwise we round to the next even value
  157. // Which means we add the least significant bit of the raw integer value
  158. int_value = Checked<int>::saturating_add(int_value, int_value & 1);
  159. }
  160. }
  161. return from_raw(int_value);
  162. }
  163. constexpr CSSPixels operator*(CSSPixelFraction const& other) const;
  164. constexpr CSSPixelFraction operator/(CSSPixels const& other) const;
  165. constexpr CSSPixels operator/(CSSPixelFraction const& other) const;
  166. constexpr CSSPixels& operator+=(CSSPixels const& other)
  167. {
  168. *this = *this + other;
  169. return *this;
  170. }
  171. constexpr CSSPixels& operator-=(CSSPixels const& other)
  172. {
  173. *this = *this - other;
  174. return *this;
  175. }
  176. constexpr CSSPixels& operator*=(CSSPixels const& other)
  177. {
  178. *this = *this * other;
  179. return *this;
  180. }
  181. constexpr CSSPixels& operator*=(CSSPixelFraction const& other)
  182. {
  183. *this = *this * other;
  184. return *this;
  185. }
  186. constexpr CSSPixels& operator/=(CSSPixels const& other)
  187. {
  188. *this = *this * other;
  189. return *this;
  190. }
  191. constexpr CSSPixels abs() const { return from_raw(::abs(m_value)); }
  192. CSSPixels& scale_by(float value)
  193. {
  194. *this = CSSPixels(to_float() * value);
  195. return *this;
  196. }
  197. CSSPixels& scale_by(double value)
  198. {
  199. *this = CSSPixels(to_double() * value);
  200. return *this;
  201. }
  202. CSSPixels scaled(float value) const
  203. {
  204. auto result = *this;
  205. result.scale_by(value);
  206. return result;
  207. }
  208. CSSPixels scaled(double value) const
  209. {
  210. auto result = *this;
  211. result.scale_by(value);
  212. return result;
  213. }
  214. private:
  215. i32 m_value { 0 };
  216. };
  217. template<Integral T>
  218. constexpr bool operator==(CSSPixels left, T right) { return left == CSSPixels(right); }
  219. inline bool operator==(CSSPixels left, float right) { return left.to_float() == right; }
  220. inline bool operator==(CSSPixels left, double right) { return left.to_double() == right; }
  221. template<Integral T>
  222. constexpr bool operator>(CSSPixels left, T right) { return left > CSSPixels(right); }
  223. inline bool operator>(CSSPixels left, float right) { return left.to_float() > right; }
  224. inline bool operator>(CSSPixels left, double right) { return left.to_double() > right; }
  225. template<Integral T>
  226. constexpr bool operator<(CSSPixels left, T right) { return left < CSSPixels(right); }
  227. inline bool operator<(CSSPixels left, float right) { return left.to_float() < right; }
  228. inline bool operator<(CSSPixels left, double right) { return left.to_double() < right; }
  229. template<Integral T>
  230. constexpr CSSPixels operator*(CSSPixels left, T right) { return left * CSSPixels(right); }
  231. inline float operator*(CSSPixels left, float right) { return left.to_float() * right; }
  232. inline double operator*(CSSPixels left, double right) { return left.to_double() * right; }
  233. template<Integral T>
  234. constexpr CSSPixels operator*(T left, CSSPixels right) { return CSSPixels(left) * right; }
  235. inline float operator*(float left, CSSPixels right) { return right.to_float() * left; }
  236. inline double operator*(double left, CSSPixels right) { return right.to_double() * left; }
  237. template<Integral T>
  238. constexpr static T rounding_divide(T dividend, T divisor)
  239. {
  240. if ((dividend < 0) == (divisor < 0))
  241. return (dividend + (divisor / 2)) / divisor;
  242. return (dividend - (divisor / 2)) / divisor;
  243. }
  244. class CSSPixelFraction {
  245. public:
  246. constexpr CSSPixelFraction(CSSPixels numerator, CSSPixels denominator)
  247. : m_numerator(numerator)
  248. , m_denominator(denominator)
  249. {
  250. }
  251. explicit constexpr CSSPixelFraction(CSSPixels value)
  252. : m_numerator(value)
  253. , m_denominator(1)
  254. {
  255. }
  256. template<Signed I>
  257. constexpr CSSPixelFraction(I numerator, I denominator = 1)
  258. : m_numerator(numerator)
  259. , m_denominator(denominator)
  260. {
  261. }
  262. constexpr operator CSSPixels() const
  263. {
  264. i64 wide_value = m_numerator.raw_value();
  265. wide_value <<= CSSPixels::fractional_bits;
  266. wide_value = rounding_divide<i64>(wide_value, m_denominator.raw_value());
  267. return CSSPixels::from_raw(AK::clamp_to_int(wide_value));
  268. }
  269. constexpr CSSPixels operator-(CSSPixels const& other) const
  270. {
  271. return CSSPixels(*this) - other;
  272. }
  273. constexpr CSSPixels operator+(CSSPixels const& other) const
  274. {
  275. return CSSPixels(*this) + other;
  276. }
  277. constexpr CSSPixelFraction operator-() const
  278. {
  279. return CSSPixelFraction(-numerator(), denominator());
  280. }
  281. constexpr int operator<=>(CSSPixelFraction const& other) const
  282. {
  283. auto left = static_cast<i64>(m_numerator.raw_value()) * other.m_denominator.raw_value();
  284. auto right = static_cast<i64>(other.m_numerator.raw_value()) * m_denominator.raw_value();
  285. if (left > right)
  286. return 1;
  287. if (left < right)
  288. return -1;
  289. return 0;
  290. }
  291. template<Signed I>
  292. constexpr int operator<=>(I const& other) const
  293. {
  294. return *this <=> CSSPixelFraction(other);
  295. }
  296. constexpr CSSPixels numerator() const { return m_numerator; }
  297. constexpr CSSPixels denominator() const { return m_denominator; }
  298. float to_float() const { return CSSPixels(*this).to_float(); }
  299. double to_double() const { return CSSPixels(*this).to_double(); }
  300. int to_int() const { return CSSPixels(*this).to_int(); }
  301. bool might_be_saturated() const { return CSSPixels(*this).might_be_saturated(); }
  302. private:
  303. CSSPixels m_numerator;
  304. CSSPixels m_denominator;
  305. };
  306. constexpr CSSPixels CSSPixels::operator*(CSSPixelFraction const& other) const
  307. {
  308. i64 wide_value = raw_value();
  309. wide_value *= other.numerator().raw_value();
  310. wide_value = rounding_divide<i64>(wide_value, other.denominator().raw_value());
  311. return CSSPixels::from_raw(AK::clamp_to_int(wide_value));
  312. }
  313. constexpr CSSPixelFraction CSSPixels::operator/(CSSPixels const& other) const
  314. {
  315. return CSSPixelFraction(*this, other);
  316. }
  317. constexpr CSSPixels CSSPixels::operator/(CSSPixelFraction const& other) const
  318. {
  319. i64 wide_value = raw_value();
  320. wide_value *= other.denominator().raw_value();
  321. wide_value = rounding_divide<i64>(wide_value, other.numerator().raw_value());
  322. return CSSPixels::from_raw(AK::clamp_to_int(wide_value));
  323. }
  324. template<Integral T>
  325. constexpr CSSPixelFraction operator/(CSSPixels left, T right) { return left / CSSPixels(right); }
  326. inline float operator/(CSSPixels left, float right) { return left.to_float() / right; }
  327. inline double operator/(CSSPixels left, double right) { return left.to_double() / right; }
  328. using CSSPixelLine = Gfx::Line<CSSPixels>;
  329. using CSSPixelPoint = Gfx::Point<CSSPixels>;
  330. using CSSPixelRect = Gfx::Rect<CSSPixels>;
  331. using CSSPixelSize = Gfx::Size<CSSPixels>;
  332. using DevicePixelLine = Gfx::Line<DevicePixels>;
  333. using DevicePixelPoint = Gfx::Point<DevicePixels>;
  334. using DevicePixelRect = Gfx::Rect<DevicePixels>;
  335. using DevicePixelSize = Gfx::Size<DevicePixels>;
  336. }
  337. constexpr Web::CSSPixels abs(Web::CSSPixels const& value)
  338. {
  339. return value.abs();
  340. }
  341. constexpr Web::CSSPixels floor(Web::CSSPixels const& value)
  342. {
  343. return Web::CSSPixels::from_raw(value.raw_value() & ~Web::CSSPixels::radix_mask);
  344. }
  345. constexpr Web::CSSPixels ceil(Web::CSSPixels const& value)
  346. {
  347. auto floor_value = value.raw_value() & ~Web::CSSPixels::radix_mask;
  348. auto ceil_value = floor_value + (value.raw_value() & Web::CSSPixels::radix_mask ? Web::CSSPixels::fixed_point_denominator : 0);
  349. return Web::CSSPixels::from_raw(ceil_value);
  350. }
  351. constexpr Web::CSSPixels round(Web::CSSPixels const& value)
  352. {
  353. // FIXME: Maybe do this with bit-fiddling instead
  354. if (value > 0)
  355. return floor(value + Web::CSSPixels::from_raw(Web::CSSPixels::fixed_point_denominator >> 1 /* 0.5 */));
  356. return ceil(value - Web::CSSPixels::from_raw(Web::CSSPixels::fixed_point_denominator >> 1 /* 0.5 */));
  357. }
  358. inline Web::CSSPixels sqrt(Web::CSSPixels const& value)
  359. {
  360. return Web::CSSPixels::nearest_value_for(AK::sqrt(value.to_float()));
  361. }
  362. constexpr Web::DevicePixels abs(Web::DevicePixels const& value)
  363. {
  364. return AK::abs(value.value());
  365. }
  366. constexpr Web::CSSPixels square_distance_between(Web::CSSPixelPoint const& a, Web::CSSPixelPoint const& b)
  367. {
  368. auto delta_x = abs(a.x() - b.x());
  369. auto delta_y = abs(a.y() - b.y());
  370. return delta_x * delta_x + delta_y * delta_y;
  371. }
  372. template<>
  373. template<>
  374. [[nodiscard]] ALWAYS_INLINE Web::CSSPixelRect Web::CSSPixelRect::to_rounded<Web::CSSPixels>() const
  375. {
  376. return {
  377. round(x()),
  378. round(y()),
  379. round(width()),
  380. round(height()),
  381. };
  382. }
  383. namespace AK {
  384. template<>
  385. struct Traits<Web::CSSPixels> : public GenericTraits<Web::CSSPixels> {
  386. static unsigned hash(Web::CSSPixels const& key)
  387. {
  388. return Traits<int>::hash(key.raw_value());
  389. }
  390. static bool equals(Web::CSSPixels const& a, Web::CSSPixels const& b)
  391. {
  392. return a == b;
  393. }
  394. };
  395. template<>
  396. struct Traits<Web::DevicePixels> : public GenericTraits<Web::DevicePixels> {
  397. static unsigned hash(Web::DevicePixels const& key)
  398. {
  399. return Traits<Web::DevicePixels::Type>::hash(key.value());
  400. }
  401. static bool equals(Web::DevicePixels const& a, Web::DevicePixels const& b)
  402. {
  403. return a == b;
  404. }
  405. };
  406. template<>
  407. struct Formatter<Web::CSSPixels> : Formatter<double> {
  408. ErrorOr<void> format(FormatBuilder& builder, Web::CSSPixels const& value)
  409. {
  410. return Formatter<double>::format(builder, value.to_double());
  411. }
  412. };
  413. template<>
  414. struct Formatter<Web::DevicePixels> : Formatter<Web::DevicePixels::Type> {
  415. ErrorOr<void> format(FormatBuilder& builder, Web::DevicePixels const& value)
  416. {
  417. return Formatter<Web::DevicePixels::Type>::format(builder, value.value());
  418. }
  419. };
  420. }