1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:17:35 +00:00

LibWeb: Add CSSPixels::nearest_value_for(FloatingPoint)

This is intended to annotate conversions from unknown floating-point
values to CSSPixels, and make it more obvious the fp value will be
rounded to the nearest fixed-point value.
This commit is contained in:
MacDue 2023-08-26 15:57:31 +01:00 committed by Alexander Kalenik
parent 360c0eb509
commit 71baa8c31a
28 changed files with 120 additions and 112 deletions

View file

@ -78,13 +78,21 @@ public:
template<FloatingPoint F>
explicit CSSPixels(F value)
{
*this = nearest_value_for(value);
}
template<FloatingPoint F>
static CSSPixels nearest_value_for(F value)
{
i32 raw_value = 0;
if (!isnan(value))
m_value = AK::clamp_to_int(value * fixed_point_denominator);
raw_value = AK::clamp_to_int(value * fixed_point_denominator);
// Note: The resolution of CSSPixels is 0.015625, so care must be taken when converting
// floats/doubles to CSSPixels as small values (such as scale factors) can underflow to zero,
// or otherwise produce inaccurate results (when scaled back up).
if (m_value == 0 && value != 0)
if (raw_value == 0 && value != 0)
dbgln_if(LIBWEB_CSS_DEBUG, "CSSPixels: Conversion from float or double underflowed to zero");
return from_raw(raw_value);
}
template<Unsigned U>