PixelUnits.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. template<FloatingPoint F>
  267. constexpr CSSPixelFraction(F numerator, F denominator = 1)
  268. {
  269. if (CSSPixels::nearest_value_for(denominator) == 0) {
  270. numerator = numerator / denominator;
  271. denominator = 1;
  272. }
  273. m_numerator = CSSPixels(numerator);
  274. m_denominator = CSSPixels(denominator);
  275. VERIFY(denominator != 0);
  276. }
  277. constexpr operator CSSPixels() const
  278. {
  279. i64 wide_value = m_numerator.raw_value();
  280. wide_value <<= CSSPixels::fractional_bits;
  281. wide_value /= m_denominator.raw_value();
  282. return CSSPixels::from_raw(AK::clamp_to<int>(wide_value));
  283. }
  284. constexpr CSSPixels operator-(CSSPixels const& other) const
  285. {
  286. return CSSPixels(*this) - other;
  287. }
  288. constexpr CSSPixels operator+(CSSPixels const& other) const
  289. {
  290. return CSSPixels(*this) + other;
  291. }
  292. constexpr CSSPixelFraction operator-() const
  293. {
  294. return CSSPixelFraction(-numerator(), denominator());
  295. }
  296. constexpr int operator<=>(CSSPixelFraction const& other) const
  297. {
  298. auto left = static_cast<i64>(m_numerator.raw_value()) * other.m_denominator.raw_value();
  299. auto right = static_cast<i64>(other.m_numerator.raw_value()) * m_denominator.raw_value();
  300. if (left > right)
  301. return 1;
  302. if (left < right)
  303. return -1;
  304. return 0;
  305. }
  306. template<Signed I>
  307. constexpr int operator<=>(I const& other) const
  308. {
  309. return *this <=> CSSPixelFraction(other);
  310. }
  311. constexpr CSSPixels numerator() const { return m_numerator; }
  312. constexpr CSSPixels denominator() const { return m_denominator; }
  313. float to_float() const { return CSSPixels(*this).to_float(); }
  314. double to_double() const { return CSSPixels(*this).to_double(); }
  315. int to_int() const { return CSSPixels(*this).to_int(); }
  316. bool might_be_saturated() const { return CSSPixels(*this).might_be_saturated(); }
  317. private:
  318. CSSPixels m_numerator;
  319. CSSPixels m_denominator;
  320. };
  321. constexpr CSSPixels CSSPixels::operator*(CSSPixelFraction const& other) const
  322. {
  323. i64 wide_value = raw_value();
  324. wide_value *= other.numerator().raw_value();
  325. wide_value /= other.denominator().raw_value();
  326. return CSSPixels::from_raw(AK::clamp_to<int>(wide_value));
  327. }
  328. constexpr CSSPixelFraction CSSPixels::operator/(CSSPixels const& other) const
  329. {
  330. return CSSPixelFraction(*this, other);
  331. }
  332. constexpr CSSPixels CSSPixels::operator/(CSSPixelFraction const& other) const
  333. {
  334. i64 wide_value = raw_value();
  335. wide_value *= other.denominator().raw_value();
  336. wide_value /= other.numerator().raw_value();
  337. return CSSPixels::from_raw(AK::clamp_to<int>(wide_value));
  338. }
  339. template<Integral T>
  340. constexpr CSSPixelFraction operator/(CSSPixels left, T right) { return left / CSSPixels(right); }
  341. inline float operator/(CSSPixels left, float right) { return left.to_float() / right; }
  342. inline double operator/(CSSPixels left, double right) { return left.to_double() / right; }
  343. using CSSPixelLine = Gfx::Line<CSSPixels>;
  344. using CSSPixelPoint = Gfx::Point<CSSPixels>;
  345. using CSSPixelRect = Gfx::Rect<CSSPixels>;
  346. using CSSPixelSize = Gfx::Size<CSSPixels>;
  347. using DevicePixelLine = Gfx::Line<DevicePixels>;
  348. using DevicePixelPoint = Gfx::Point<DevicePixels>;
  349. using DevicePixelRect = Gfx::Rect<DevicePixels>;
  350. using DevicePixelSize = Gfx::Size<DevicePixels>;
  351. }
  352. constexpr Web::CSSPixels abs(Web::CSSPixels const& value)
  353. {
  354. return value.abs();
  355. }
  356. constexpr Web::CSSPixels floor(Web::CSSPixels const& value)
  357. {
  358. return Web::CSSPixels::from_raw(value.raw_value() & ~Web::CSSPixels::radix_mask);
  359. }
  360. constexpr Web::CSSPixels ceil(Web::CSSPixels const& value)
  361. {
  362. auto floor_value = value.raw_value() & ~Web::CSSPixels::radix_mask;
  363. auto ceil_value = floor_value + (value.raw_value() & Web::CSSPixels::radix_mask ? Web::CSSPixels::fixed_point_denominator : 0);
  364. return Web::CSSPixels::from_raw(ceil_value);
  365. }
  366. constexpr Web::CSSPixels round(Web::CSSPixels const& value)
  367. {
  368. // FIXME: Maybe do this with bit-fiddling instead
  369. if (value > 0)
  370. return floor(value + Web::CSSPixels::from_raw(Web::CSSPixels::fixed_point_denominator >> 1 /* 0.5 */));
  371. return ceil(value - Web::CSSPixels::from_raw(Web::CSSPixels::fixed_point_denominator >> 1 /* 0.5 */));
  372. }
  373. inline Web::CSSPixels sqrt(Web::CSSPixels const& value)
  374. {
  375. return Web::CSSPixels::nearest_value_for(AK::sqrt(value.to_float()));
  376. }
  377. constexpr Web::DevicePixels abs(Web::DevicePixels const& value)
  378. {
  379. return AK::abs(value.value());
  380. }
  381. constexpr Web::CSSPixels square_distance_between(Web::CSSPixelPoint const& a, Web::CSSPixelPoint const& b)
  382. {
  383. auto delta_x = abs(a.x() - b.x());
  384. auto delta_y = abs(a.y() - b.y());
  385. return delta_x * delta_x + delta_y * delta_y;
  386. }
  387. template<>
  388. template<>
  389. [[nodiscard]] ALWAYS_INLINE Web::CSSPixelRect Web::CSSPixelRect::to_rounded<Web::CSSPixels>() const
  390. {
  391. return {
  392. round(x()),
  393. round(y()),
  394. round(width()),
  395. round(height()),
  396. };
  397. }
  398. namespace AK {
  399. template<>
  400. struct Traits<Web::CSSPixels> : public DefaultTraits<Web::CSSPixels> {
  401. static unsigned hash(Web::CSSPixels const& key)
  402. {
  403. return Traits<int>::hash(key.raw_value());
  404. }
  405. static bool equals(Web::CSSPixels const& a, Web::CSSPixels const& b)
  406. {
  407. return a == b;
  408. }
  409. };
  410. template<>
  411. struct Traits<Web::DevicePixels> : public DefaultTraits<Web::DevicePixels> {
  412. static unsigned hash(Web::DevicePixels const& key)
  413. {
  414. return Traits<Web::DevicePixels::Type>::hash(key.value());
  415. }
  416. static bool equals(Web::DevicePixels const& a, Web::DevicePixels const& b)
  417. {
  418. return a == b;
  419. }
  420. };
  421. template<>
  422. struct Formatter<Web::CSSPixels> : Formatter<double> {
  423. ErrorOr<void> format(FormatBuilder& builder, Web::CSSPixels const& value)
  424. {
  425. return Formatter<double>::format(builder, value.to_double());
  426. }
  427. };
  428. template<>
  429. struct Formatter<Web::DevicePixels> : Formatter<Web::DevicePixels::Type> {
  430. ErrorOr<void> format(FormatBuilder& builder, Web::DevicePixels const& value)
  431. {
  432. return Formatter<Web::DevicePixels::Type>::format(builder, value.value());
  433. }
  434. };
  435. }
  436. namespace IPC {
  437. template<>
  438. ErrorOr<void> encode(Encoder& encoder, Web::DevicePixels const& value);
  439. template<>
  440. ErrorOr<Web::DevicePixels> decode(Decoder& decoder);
  441. template<>
  442. ErrorOr<void> encode(Encoder& encoder, Web::DevicePixelPoint const& value);
  443. template<>
  444. ErrorOr<Web::DevicePixelPoint> decode(Decoder& decoder);
  445. template<>
  446. ErrorOr<void> encode(Encoder& encoder, Web::DevicePixelSize const& value);
  447. template<>
  448. ErrorOr<Web::DevicePixelSize> decode(Decoder& decoder);
  449. template<>
  450. ErrorOr<void> encode(Encoder& encoder, Web::DevicePixelRect const& value);
  451. template<>
  452. ErrorOr<Web::DevicePixelRect> decode(Decoder& decoder);
  453. }