PixelUnits.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. CSSPixels::CSSPixels(int value)
  10. {
  11. m_value = value * fixed_point_denominator;
  12. }
  13. CSSPixels::CSSPixels(unsigned int value)
  14. {
  15. m_value = value * fixed_point_denominator;
  16. }
  17. CSSPixels::CSSPixels(unsigned long value)
  18. {
  19. m_value = value * fixed_point_denominator;
  20. }
  21. CSSPixels::CSSPixels(float value)
  22. {
  23. if (!isnan(value))
  24. m_value = AK::clamp_to_int(value * fixed_point_denominator);
  25. }
  26. CSSPixels::CSSPixels(double value)
  27. {
  28. if (!isnan(value))
  29. m_value = AK::clamp_to_int(value * fixed_point_denominator);
  30. }
  31. float CSSPixels::to_float() const
  32. {
  33. return static_cast<float>(m_value) / fixed_point_denominator;
  34. }
  35. double CSSPixels::to_double() const
  36. {
  37. return static_cast<double>(m_value) / fixed_point_denominator;
  38. }
  39. int CSSPixels::to_int() const
  40. {
  41. return m_value / fixed_point_denominator;
  42. }
  43. bool CSSPixels::might_be_saturated() const
  44. {
  45. return raw_value() == NumericLimits<i32>::max() || raw_value() == NumericLimits<i32>::min();
  46. }
  47. bool CSSPixels::operator==(CSSPixels const& other) const
  48. {
  49. return raw_value() == other.raw_value();
  50. }
  51. CSSPixels& CSSPixels::operator++()
  52. {
  53. m_value = Checked<int>::saturating_add(m_value, fixed_point_denominator);
  54. return *this;
  55. }
  56. CSSPixels& CSSPixels::operator--()
  57. {
  58. m_value = Checked<int>::saturating_sub(m_value, fixed_point_denominator);
  59. return *this;
  60. }
  61. int CSSPixels::operator<=>(CSSPixels const& other) const
  62. {
  63. return raw_value() > other.raw_value() ? 1 : raw_value() < other.raw_value() ? -1
  64. : 0;
  65. }
  66. CSSPixels CSSPixels::operator+() const
  67. {
  68. return from_raw(+raw_value());
  69. }
  70. CSSPixels CSSPixels::operator-() const
  71. {
  72. return from_raw(-raw_value());
  73. }
  74. CSSPixels CSSPixels::operator+(CSSPixels const& other) const
  75. {
  76. return from_raw(Checked<int>::saturating_add(raw_value(), other.raw_value()));
  77. }
  78. CSSPixels CSSPixels::operator-(CSSPixels const& other) const
  79. {
  80. return from_raw(Checked<int>::saturating_sub(raw_value(), other.raw_value()));
  81. }
  82. CSSPixels CSSPixels::operator*(CSSPixels const& other) const
  83. {
  84. i64 value = raw_value();
  85. value *= other.raw_value();
  86. int int_value = AK::clamp_to_int(value >> fractional_bits);
  87. // Rounding:
  88. // If last bit cut off was 1:
  89. if (value & (1u << (fractional_bits - 1))) {
  90. // If the bit after was 1 as well
  91. if (value & (radix_mask >> 2u)) {
  92. // We need to round away from 0
  93. int_value = Checked<int>::saturating_add(int_value, 1);
  94. } else {
  95. // Otherwise we round to the next even value
  96. // Which means we add the least significant bit of the raw integer value
  97. int_value = Checked<int>::saturating_add(int_value, int_value & 1);
  98. }
  99. }
  100. return from_raw(int_value);
  101. }
  102. CSSPixels CSSPixels::operator/(CSSPixels const& other) const
  103. {
  104. CSSPixels result;
  105. result.set_raw_value(static_cast<long long>(fixed_point_denominator) * raw_value() / other.raw_value());
  106. return result;
  107. }
  108. CSSPixels& CSSPixels::operator+=(CSSPixels const& other)
  109. {
  110. *this = *this + other;
  111. return *this;
  112. }
  113. CSSPixels& CSSPixels::operator-=(CSSPixels const& other)
  114. {
  115. *this = *this - other;
  116. return *this;
  117. }
  118. CSSPixels& CSSPixels::operator*=(CSSPixels const& other)
  119. {
  120. *this = *this * other;
  121. return *this;
  122. }
  123. CSSPixels& CSSPixels::operator/=(CSSPixels const& other)
  124. {
  125. *this = *this / other;
  126. return *this;
  127. }
  128. CSSPixels CSSPixels::abs() const
  129. {
  130. return from_raw(::abs(m_value));
  131. }
  132. }