PixelUnits.h 16 KB

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