Round node dimensions in double so far-off nodes keep their full size (#2037)

Summary:
* Round node dimensions in double so far-off nodes keep their full size

A node's rounded width and height are the difference of its two rounded
absolute edges. roundValueToPixelGrid returns float, so that difference was
taken in float; far from the origin a float has fewer bits than a pixel, and
when the two edges fall on either side of a power of two they are quantized
differently. A measured text node spanning y = 2048 on a 3x screen came out
43.9998779 tall instead of 44, one float step short of the two lines it was
measured at, and the text renderer then dropped the last line.

Round in double and subtract before narrowing to float. The public
roundValueToPixelGrid keeps its signature.

* Cover both fractions on either side of 1024, 2048 and 4096 in the rounding test

On the previous rounding each of these came out off by a float step
(44.00006, 43.99994, 43.99988, 43.99988 and 43.99976); all are exactly 44
now.

* Remove comments

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D125427496

fbshipit-source-id: 4ba056bb164207d540032252633112358db80581

Author set to the author of react/yoga#2037; the commit on main carries the Yoga sync account.

Co-authored-by: nishan (o^▽^o) <nishanbende@gmail.com>
(cherry picked from commit 4de5b675b0)
This commit is contained in:
ybelakov authored and t committed 2026-10-26 12:43:48 +00:00
1 parent dd11eb80cf
commit c2a4a95b54
1 file changed
+31 -16
@@ -12,7 +12,9 @@
namespace facebook::yoga {
float roundValueToPixelGrid(
namespace {
double roundValueToPixelGridDouble(
const double value,
const double pointScaleFactor,
const bool forceCeil,
@@ -59,7 +61,18 @@ float roundValueToPixelGrid(
}
return (std::isnan(scaledValue) || std::isnan(pointScaleFactor))
? YGUndefined
: (float)(scaledValue / pointScaleFactor);
: scaledValue / pointScaleFactor;
}
} // namespace
float roundValueToPixelGrid(
const double value,
const double pointScaleFactor,
const bool forceCeil,
const bool forceFloor) {
return static_cast<float>(roundValueToPixelGridDouble(
value, pointScaleFactor, forceCeil, forceFloor));
}
void roundLayoutResultsToPixelGrid(
@@ -108,23 +121,25 @@ void roundLayoutResultsToPixelGrid(
node->getLayout().setDimension(
Dimension::Width,
roundValueToPixelGrid(
absoluteNodeRight,
pointScaleFactor,
(textRounding && hasFractionalWidth),
(textRounding && !hasFractionalWidth)) -
roundValueToPixelGrid(
absoluteNodeLeft, pointScaleFactor, false, textRounding));
static_cast<float>(
roundValueToPixelGridDouble(
absoluteNodeRight,
pointScaleFactor,
(textRounding && hasFractionalWidth),
(textRounding && !hasFractionalWidth)) -
roundValueToPixelGridDouble(
absoluteNodeLeft, pointScaleFactor, false, textRounding)));
node->getLayout().setDimension(
Dimension::Height,
roundValueToPixelGrid(
absoluteNodeBottom,
pointScaleFactor,
(textRounding && hasFractionalHeight),
(textRounding && !hasFractionalHeight)) -
roundValueToPixelGrid(
absoluteNodeTop, pointScaleFactor, false, textRounding));
static_cast<float>(
roundValueToPixelGridDouble(
absoluteNodeBottom,
pointScaleFactor,
(textRounding && hasFractionalHeight),
(textRounding && !hasFractionalHeight)) -
roundValueToPixelGridDouble(
absoluteNodeTop, pointScaleFactor, false, textRounding)));
}
for (yoga::Node* child : node->getChildren()) {