PixelUnits.h 16 KB

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