PixelUnits.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Math.h>
  7. #include <LibWeb/PixelUnits.h>
  8. namespace Web {
  9. static i32 const fractional_bits = 6;
  10. static constexpr i32 fixed_point_denominator = 1 << fractional_bits;
  11. CSSPixels::CSSPixels(int value)
  12. {
  13. m_value = value * fixed_point_denominator;
  14. }
  15. CSSPixels::CSSPixels(unsigned int value)
  16. {
  17. m_value = value * fixed_point_denominator;
  18. }
  19. CSSPixels::CSSPixels(unsigned long value)
  20. {
  21. m_value = value * fixed_point_denominator;
  22. }
  23. CSSPixels::CSSPixels(float value)
  24. {
  25. if (!isnan(value))
  26. m_value = AK::clamp_to_int(value * fixed_point_denominator);
  27. }
  28. CSSPixels::CSSPixels(double value)
  29. {
  30. if (!isnan(value))
  31. m_value = AK::clamp_to_int(value * fixed_point_denominator);
  32. }
  33. float CSSPixels::to_float() const
  34. {
  35. return static_cast<float>(m_value) / fixed_point_denominator;
  36. }
  37. double CSSPixels::to_double() const
  38. {
  39. return static_cast<double>(m_value) / fixed_point_denominator;
  40. }
  41. int CSSPixels::to_int() const
  42. {
  43. return m_value / fixed_point_denominator;
  44. }
  45. bool CSSPixels::might_be_saturated() const
  46. {
  47. return raw_value() == NumericLimits<i32>::max() || raw_value() == NumericLimits<i32>::min();
  48. }
  49. bool CSSPixels::operator==(CSSPixels const& other) const
  50. {
  51. return raw_value() == other.raw_value();
  52. }
  53. CSSPixels& CSSPixels::operator++()
  54. {
  55. m_value += fixed_point_denominator;
  56. return *this;
  57. }
  58. CSSPixels& CSSPixels::operator--()
  59. {
  60. m_value -= fixed_point_denominator;
  61. return *this;
  62. }
  63. int CSSPixels::operator<=>(CSSPixels const& other) const
  64. {
  65. return raw_value() > other.raw_value() ? 1 : raw_value() < other.raw_value() ? -1
  66. : 0;
  67. }
  68. CSSPixels CSSPixels::operator+() const
  69. {
  70. return from_raw(+raw_value());
  71. }
  72. CSSPixels CSSPixels::operator-() const
  73. {
  74. return from_raw(-raw_value());
  75. }
  76. static inline int saturated_addition(int a, int b)
  77. {
  78. i32 overflow = (b > 0 && a > NumericLimits<i32>::max() - b);
  79. i32 underflow = (b < 0 && a < NumericLimits<i32>::min() - b);
  80. return overflow ? NumericLimits<i32>::max() : (underflow ? NumericLimits<i32>::min() : a + b);
  81. }
  82. CSSPixels CSSPixels::operator+(CSSPixels const& other) const
  83. {
  84. CSSPixels result;
  85. result.set_raw_value(saturated_addition(raw_value(), other.raw_value()));
  86. return result;
  87. }
  88. CSSPixels CSSPixels::operator-(CSSPixels const& other) const
  89. {
  90. CSSPixels result;
  91. result.set_raw_value(saturated_addition(raw_value(), -other.raw_value()));
  92. return result;
  93. }
  94. CSSPixels CSSPixels::operator*(CSSPixels const& other) const
  95. {
  96. CSSPixels result;
  97. result.set_raw_value((static_cast<i64>(raw_value()) * other.raw_value()) >> fractional_bits);
  98. return result;
  99. }
  100. CSSPixels CSSPixels::operator/(CSSPixels const& other) const
  101. {
  102. CSSPixels result;
  103. result.set_raw_value(static_cast<long long>(fixed_point_denominator) * raw_value() / other.raw_value());
  104. return result;
  105. }
  106. CSSPixels& CSSPixels::operator+=(CSSPixels const& other)
  107. {
  108. *this = *this + other;
  109. return *this;
  110. }
  111. CSSPixels& CSSPixels::operator-=(CSSPixels const& other)
  112. {
  113. *this = *this - other;
  114. return *this;
  115. }
  116. CSSPixels& CSSPixels::operator*=(CSSPixels const& other)
  117. {
  118. *this = *this * other;
  119. return *this;
  120. }
  121. CSSPixels& CSSPixels::operator/=(CSSPixels const& other)
  122. {
  123. *this = *this / other;
  124. return *this;
  125. }
  126. CSSPixels CSSPixels::abs() const
  127. {
  128. return from_raw(::abs(m_value));
  129. }
  130. }