Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

namespace facebook::yoga {

float roundValueToPixelGrid(
namespace {

double roundValueToPixelGridDouble(
const double value,
const double pointScaleFactor,
const bool forceCeil,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -98,33 +111,42 @@ void roundLayoutResultsToPixelGrid(
// whole number, we don't have any fraction To verify if the result is close
// to whole number we want to check both floor and ceil numbers

const double scaledNodeWith = nodeWidth * pointScaleFactor;
const double scaledNodeWidth = nodeWidth * pointScaleFactor;
const bool hasFractionalWidth =
!yoga::inexactEquals(round(scaledNodeWith), scaledNodeWith);
!yoga::inexactEquals(round(scaledNodeWidth), scaledNodeWidth);

const double scaledNodeHeight = nodeHeight * pointScaleFactor;
const bool hasFractionalHeight =
!yoga::inexactEquals(round(scaledNodeHeight), scaledNodeHeight);

node->getLayout().setDimension(
Dimension::Width,
roundValueToPixelGrid(
absoluteNodeRight,
pointScaleFactor,
(textRounding && hasFractionalWidth),
(textRounding && !hasFractionalWidth)) -
roundValueToPixelGrid(
absoluteNodeLeft, pointScaleFactor, false, textRounding));
textRounding && !hasFractionalWidth
? static_cast<float>(round(scaledNodeWidth) / pointScaleFactor)
: static_cast<float>(
roundValueToPixelGridDouble(
absoluteNodeRight,
pointScaleFactor,
textRounding,
false) -
roundValueToPixelGridDouble(
absoluteNodeLeft,
pointScaleFactor,
false,
textRounding)));

node->getLayout().setDimension(
Dimension::Height,
roundValueToPixelGrid(
absoluteNodeBottom,
pointScaleFactor,
(textRounding && hasFractionalHeight),
(textRounding && !hasFractionalHeight)) -
roundValueToPixelGrid(
absoluteNodeTop, pointScaleFactor, false, textRounding));
textRounding && !hasFractionalHeight
? static_cast<float>(round(scaledNodeHeight) / pointScaleFactor)
: static_cast<float>(
roundValueToPixelGridDouble(
absoluteNodeBottom,
pointScaleFactor,
textRounding,
false) -
roundValueToPixelGridDouble(
absoluteNodeTop, pointScaleFactor, false, textRounding)));
}

for (yoga::Node* child : node->getChildren()) {
Expand Down