PixelUnits.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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<FloatingPoint F>
  81. static CSSPixels floored_value_for(F value)
  82. {
  83. i32 raw_value = 0;
  84. if (!isnan(value))
  85. raw_value = AK::clamp_to<int>(floor(value * fixed_point_denominator));
  86. return from_raw(raw_value);
  87. }
  88. template<Unsigned U>
  89. constexpr CSSPixels(U value)
  90. {
  91. if (value > max_integer_value) [[unlikely]]
  92. m_value = NumericLimits<int>::max();
  93. else
  94. m_value = static_cast<int>(value) << fractional_bits;
  95. }
  96. static constexpr CSSPixels from_raw(int value)
  97. {
  98. CSSPixels res;
  99. res.set_raw_value(value);
  100. return res;
  101. }
  102. static constexpr CSSPixels min()
  103. {
  104. return from_raw(NumericLimits<int>::min());
  105. }
  106. static constexpr CSSPixels max()
  107. {
  108. return from_raw(NumericLimits<int>::max());
  109. }
  110. static constexpr CSSPixels smallest_positive_value()
  111. {
  112. return from_raw(1);
  113. }
  114. float to_float() const;
  115. double to_double() const;
  116. int to_int() const;
  117. constexpr int raw_value() const { return m_value; }
  118. constexpr void set_raw_value(int value) { m_value = value; }
  119. constexpr bool might_be_saturated() const { return raw_value() == NumericLimits<i32>::max() || raw_value() == NumericLimits<i32>::min(); }
  120. constexpr bool operator==(CSSPixels const& other) const = default;
  121. explicit operator double() const { return to_double(); }
  122. explicit operator float() const { return to_float(); }
  123. explicit operator int() const { return to_int(); }
  124. constexpr CSSPixels& operator++()
  125. {
  126. m_value = Checked<int>::saturating_add(m_value, fixed_point_denominator);
  127. return *this;
  128. }
  129. constexpr CSSPixels& operator--()
  130. {
  131. m_value = Checked<int>::saturating_sub(m_value, fixed_point_denominator);
  132. return *this;
  133. }
  134. constexpr int operator<=>(CSSPixels const& other) const
  135. {
  136. return raw_value() > other.raw_value()
  137. ? 1
  138. : raw_value() < other.raw_value()
  139. ? -1
  140. : 0;
  141. }
  142. constexpr CSSPixels operator+() const { return from_raw(+raw_value()); }
  143. constexpr CSSPixels operator-() const { return from_raw(-raw_value()); }
  144. constexpr CSSPixels operator+(CSSPixels const& other) const
  145. {
  146. return from_raw(Checked<int>::saturating_add(raw_value(), other.raw_value()));
  147. }
  148. constexpr CSSPixels operator-(CSSPixels const& other) const
  149. {
  150. return from_raw(Checked<int>::saturating_sub(raw_value(), other.raw_value()));
  151. }
  152. constexpr CSSPixels operator*(CSSPixels const& other) const
  153. {
  154. i64 value = raw_value();
  155. value *= other.raw_value();
  156. int int_value = AK::clamp_to<int>(value >> fractional_bits);
  157. // Rounding:
  158. // If last bit cut off was 1:
  159. if (value & (1u << (fractional_bits - 1))) {
  160. // If any bit after was 1 as well
  161. if (value & (radix_mask >> 1u)) {
  162. // We need to round away from 0
  163. int_value = Checked<int>::saturating_add(int_value, 1);
  164. } else {
  165. // Otherwise we round to the next even value
  166. // Which means we add the least significant bit of the raw integer value
  167. int_value = Checked<int>::saturating_add(int_value, int_value & 1);
  168. }
  169. }
  170. return from_raw(int_value);
  171. }
  172. constexpr CSSPixels operator*(CSSPixelFraction const& other) const;
  173. constexpr CSSPixelFraction operator/(CSSPixels const& other) const;
  174. constexpr CSSPixels operator/(CSSPixelFraction const& other) const;
  175. constexpr CSSPixels& operator+=(CSSPixels const& other)
  176. {
  177. *this = *this + other;
  178. return *this;
  179. }
  180. constexpr CSSPixels& operator-=(CSSPixels const& other)
  181. {
  182. *this = *this - other;
  183. return *this;
  184. }
  185. constexpr CSSPixels& operator*=(CSSPixels const& other)
  186. {
  187. *this = *this * other;
  188. return *this;
  189. }
  190. constexpr CSSPixels& operator*=(CSSPixelFraction const& other)
  191. {
  192. *this = *this * other;
  193. return *this;
  194. }
  195. constexpr CSSPixels& operator/=(CSSPixels const& other)
  196. {
  197. *this = *this * other;
  198. return *this;
  199. }
  200. constexpr CSSPixels abs() const { return from_raw(::abs(m_value)); }
  201. CSSPixels& scale_by(float value)
  202. {
  203. *this = CSSPixels(to_float() * value);
  204. return *this;
  205. }
  206. CSSPixels& scale_by(double value)
  207. {
  208. *this = CSSPixels(to_double() * value);
  209. return *this;
  210. }
  211. CSSPixels scaled(float value) const
  212. {
  213. auto result = *this;
  214. result.scale_by(value);
  215. return result;
  216. }
  217. CSSPixels scaled(double value) const
  218. {
  219. auto result = *this;
  220. result.scale_by(value);
  221. return result;
  222. }
  223. private:
  224. i32 m_value { 0 };
  225. };
  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 bool operator>(CSSPixels left, T right) { return left > CSSPixels(right); }
  232. inline bool operator>(CSSPixels left, float right) { return left.to_float() > right; }
  233. inline bool operator>(CSSPixels left, double right) { return left.to_double() > right; }
  234. template<Integral T>
  235. constexpr bool operator<(CSSPixels left, T right) { return left < CSSPixels(right); }
  236. inline bool operator<(CSSPixels left, float right) { return left.to_float() < right; }
  237. inline bool operator<(CSSPixels left, double right) { return left.to_double() < right; }
  238. template<Integral T>
  239. constexpr CSSPixels operator*(CSSPixels left, T right) { return left * CSSPixels(right); }
  240. inline float operator*(CSSPixels left, float right) { return left.to_float() * right; }
  241. inline double operator*(CSSPixels left, double right) { return left.to_double() * right; }
  242. template<Integral T>
  243. constexpr CSSPixels operator*(T left, CSSPixels right) { return CSSPixels(left) * right; }
  244. inline float operator*(float left, CSSPixels right) { return right.to_float() * left; }
  245. inline double operator*(double left, CSSPixels right) { return right.to_double() * left; }
  246. class CSSPixelFraction {
  247. public:
  248. constexpr CSSPixelFraction(CSSPixels numerator, CSSPixels denominator)
  249. : m_numerator(numerator)
  250. , m_denominator(denominator)
  251. {
  252. VERIFY(denominator != 0);
  253. }
  254. explicit constexpr CSSPixelFraction(CSSPixels value)
  255. : m_numerator(value)
  256. , m_denominator(1)
  257. {
  258. }
  259. template<Signed I>
  260. constexpr CSSPixelFraction(I numerator, I denominator = 1)
  261. : m_numerator(numerator)
  262. , m_denominator(denominator)
  263. {
  264. VERIFY(denominator != 0);
  265. }
  266. constexpr operator CSSPixels() const
  267. {
  268. i64 wide_value = m_numerator.raw_value();
  269. wide_value <<= CSSPixels::fractional_bits;
  270. wide_value /= m_denominator.raw_value();
  271. return CSSPixels::from_raw(AK::clamp_to<int>(wide_value));
  272. }
  273. constexpr CSSPixels operator-(CSSPixels const& other) const
  274. {
  275. return CSSPixels(*this) - other;
  276. }
  277. constexpr CSSPixels operator+(CSSPixels const& other) const
  278. {
  279. return CSSPixels(*this) + other;
  280. }
  281. constexpr CSSPixelFraction operator-() const
  282. {
  283. return CSSPixelFraction(-numerator(), denominator());
  284. }
  285. constexpr int operator<=>(CSSPixelFraction const& other) const
  286. {
  287. auto left = static_cast<i64>(m_numerator.raw_value()) * other.m_denominator.raw_value();
  288. auto right = static_cast<i64>(other.m_numerator.raw_value()) * m_denominator.raw_value();
  289. if (left > right)
  290. return 1;
  291. if (left < right)
  292. return -1;
  293. return 0;
  294. }
  295. template<Signed I>
  296. constexpr int operator<=>(I const& other) const
  297. {
  298. return *this <=> CSSPixelFraction(other);
  299. }
  300. constexpr CSSPixels numerator() const { return m_numerator; }
  301. constexpr CSSPixels denominator() const { return m_denominator; }
  302. float to_float() const { return CSSPixels(*this).to_float(); }
  303. double to_double() const { return CSSPixels(*this).to_double(); }
  304. int to_int() const { return CSSPixels(*this).to_int(); }
  305. bool might_be_saturated() const { return CSSPixels(*this).might_be_saturated(); }
  306. private:
  307. CSSPixels m_numerator;
  308. CSSPixels m_denominator;
  309. };
  310. constexpr CSSPixels CSSPixels::operator*(CSSPixelFraction const& other) const
  311. {
  312. i64 wide_value = raw_value();
  313. wide_value *= other.numerator().raw_value();
  314. wide_value /= other.denominator().raw_value();
  315. return CSSPixels::from_raw(AK::clamp_to<int>(wide_value));
  316. }
  317. constexpr CSSPixelFraction CSSPixels::operator/(CSSPixels const& other) const
  318. {
  319. return CSSPixelFraction(*this, other);
  320. }
  321. constexpr CSSPixels CSSPixels::operator/(CSSPixelFraction const& other) const
  322. {
  323. i64 wide_value = raw_value();
  324. wide_value *= other.denominator().raw_value();
  325. wide_value /= other.numerator().raw_value();
  326. return CSSPixels::from_raw(AK::clamp_to<int>(wide_value));
  327. }
  328. template<Integral T>
  329. constexpr CSSPixelFraction operator/(CSSPixels left, T right) { return left / CSSPixels(right); }
  330. inline float operator/(CSSPixels left, float right) { return left.to_float() / right; }
  331. inline double operator/(CSSPixels left, double right) { return left.to_double() / right; }
  332. using CSSPixelLine = Gfx::Line<CSSPixels>;
  333. using CSSPixelPoint = Gfx::Point<CSSPixels>;
  334. using CSSPixelRect = Gfx::Rect<CSSPixels>;
  335. using CSSPixelSize = Gfx::Size<CSSPixels>;
  336. using DevicePixelLine = Gfx::Line<DevicePixels>;
  337. using DevicePixelPoint = Gfx::Point<DevicePixels>;
  338. using DevicePixelRect = Gfx::Rect<DevicePixels>;
  339. using DevicePixelSize = Gfx::Size<DevicePixels>;
  340. }
  341. constexpr Web::CSSPixels abs(Web::CSSPixels const& value)
  342. {
  343. return value.abs();
  344. }
  345. constexpr Web::CSSPixels floor(Web::CSSPixels const& value)
  346. {
  347. return Web::CSSPixels::from_raw(value.raw_value() & ~Web::CSSPixels::radix_mask);
  348. }
  349. constexpr Web::CSSPixels ceil(Web::CSSPixels const& value)
  350. {
  351. auto floor_value = value.raw_value() & ~Web::CSSPixels::radix_mask;
  352. auto ceil_value = floor_value + (value.raw_value() & Web::CSSPixels::radix_mask ? Web::CSSPixels::fixed_point_denominator : 0);
  353. return Web::CSSPixels::from_raw(ceil_value);
  354. }
  355. constexpr Web::CSSPixels round(Web::CSSPixels const& value)
  356. {
  357. // FIXME: Maybe do this with bit-fiddling instead
  358. if (value > 0)
  359. return floor(value + Web::CSSPixels::from_raw(Web::CSSPixels::fixed_point_denominator >> 1 /* 0.5 */));
  360. return ceil(value - Web::CSSPixels::from_raw(Web::CSSPixels::fixed_point_denominator >> 1 /* 0.5 */));
  361. }
  362. inline Web::CSSPixels sqrt(Web::CSSPixels const& value)
  363. {
  364. return Web::CSSPixels::nearest_value_for(AK::sqrt(value.to_float()));
  365. }
  366. constexpr Web::DevicePixels abs(Web::DevicePixels const& value)
  367. {
  368. return AK::abs(value.value());
  369. }
  370. constexpr Web::CSSPixels square_distance_between(Web::CSSPixelPoint const& a, Web::CSSPixelPoint const& b)
  371. {
  372. auto delta_x = abs(a.x() - b.x());
  373. auto delta_y = abs(a.y() - b.y());
  374. return delta_x * delta_x + delta_y * delta_y;
  375. }
  376. template<>
  377. template<>
  378. [[nodiscard]] ALWAYS_INLINE Web::CSSPixelRect Web::CSSPixelRect::to_rounded<Web::CSSPixels>() const
  379. {
  380. return {
  381. round(x()),
  382. round(y()),
  383. round(width()),
  384. round(height()),
  385. };
  386. }
  387. namespace AK {
  388. template<>
  389. struct Traits<Web::CSSPixels> : public DefaultTraits<Web::CSSPixels> {
  390. static unsigned hash(Web::CSSPixels const& key)
  391. {
  392. return Traits<int>::hash(key.raw_value());
  393. }
  394. static bool equals(Web::CSSPixels const& a, Web::CSSPixels const& b)
  395. {
  396. return a == b;
  397. }
  398. };
  399. template<>
  400. struct Traits<Web::DevicePixels> : public DefaultTraits<Web::DevicePixels> {
  401. static unsigned hash(Web::DevicePixels const& key)
  402. {
  403. return Traits<Web::DevicePixels::Type>::hash(key.value());
  404. }
  405. static bool equals(Web::DevicePixels const& a, Web::DevicePixels const& b)
  406. {
  407. return a == b;
  408. }
  409. };
  410. template<>
  411. struct Formatter<Web::CSSPixels> : Formatter<double> {
  412. ErrorOr<void> format(FormatBuilder& builder, Web::CSSPixels const& value)
  413. {
  414. return Formatter<double>::format(builder, value.to_double());
  415. }
  416. };
  417. template<>
  418. struct Formatter<Web::DevicePixels> : Formatter<Web::DevicePixels::Type> {
  419. ErrorOr<void> format(FormatBuilder& builder, Web::DevicePixels const& value)
  420. {
  421. return Formatter<Web::DevicePixels::Type>::format(builder, value.value());
  422. }
  423. };
  424. }
  425. namespace IPC {
  426. template<>
  427. ErrorOr<void> encode(Encoder& encoder, Web::DevicePixels const& value);
  428. template<>
  429. ErrorOr<Web::DevicePixels> decode(Decoder& decoder);
  430. template<>
  431. ErrorOr<void> encode(Encoder& encoder, Web::DevicePixelPoint const& value);
  432. template<>
  433. ErrorOr<Web::DevicePixelPoint> decode(Decoder& decoder);
  434. template<>
  435. ErrorOr<void> encode(Encoder& encoder, Web::DevicePixelSize const& value);
  436. template<>
  437. ErrorOr<Web::DevicePixelSize> decode(Decoder& decoder);
  438. template<>
  439. ErrorOr<void> encode(Encoder& encoder, Web::DevicePixelRect const& value);
  440. template<>
  441. ErrorOr<Web::DevicePixelRect> decode(Decoder& decoder);
  442. }