Skip to content

Commit

Permalink
Fix negative value rounding issue for nodes across an axis (#688)
Browse files Browse the repository at this point in the history
Summary:
This fix issue #683 the rounding calculation is incorrect if a node is crossing an axis and it will shrink it's width/height on layout calculation.

The following test reproduce the issue :

```
TEST(YogaTest, node_shrink_on_axis)
{
  const YGConfigRef config = YGConfigNew();
  const YGNodeRef root = YGNodeNewWithConfig(config);
  const YGNodeRef child = YGNodeNewWithConfig(config);

  YGNodeInsertChild(root, child, 0);

  YGNodeStyleSetWidth(child, 10);
  YGNodeStyleSetHeight(child, 10);
  YGNodeStyleSetPosition(root, YGEdgeLeft, -0.75f);
  YGNodeStyleSetPosition(root, YGEdgeTop, -0.75f);

  YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

  ASSERT_FLOAT_EQ(YGNodeLayoutGetWidth(child), 10);
  ASSERT_FLOAT_EQ(YGNodeLayoutGetHeight(child), 10);

  YGNodeFreeRecursive(root);

  YGConfigFree(config);
}
```

Pull Request resolved: #688

Reviewed By: NickGerleman

Differential Revision: D13866122

Pulled By: rozele

fbshipit-source-id: 4faf8a9efc86723c303f600d730660a2e13d8a73
  • Loading branch information
Jonathan Maurice authored and facebook-github-bot committed Feb 2, 2023
1 parent 483e399 commit 4266409
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
23 changes: 23 additions & 0 deletions tests/YGRoundingFunctionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,26 @@ TEST(YogaTest, consistent_rounding_during_repeated_layouts) {

YGConfigFree(config);
}

// Regression test for https://github.com/facebook/yoga/issues/683
TEST(YogaTest, negative_value_rounding) {
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
const YGNodeRef child = YGNodeNewWithConfig(config);

YGNodeInsertChild(root, child, 0);

YGNodeStyleSetWidth(child, 10);
YGNodeStyleSetHeight(child, 10);
YGNodeStyleSetPosition(root, YGEdgeLeft, -0.75f);
YGNodeStyleSetPosition(root, YGEdgeTop, -0.75f);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

ASSERT_FLOAT_EQ(YGNodeLayoutGetWidth(child), 10);
ASSERT_FLOAT_EQ(YGNodeLayoutGetHeight(child), 10);

YGNodeFreeRecursive(root);

YGConfigFree(config);
}
10 changes: 3 additions & 7 deletions yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3701,16 +3701,12 @@ YOGA_EXPORT float YGRoundValueToPixelGrid(
scaledValue = scaledValue - fractial + 1.0;
} else if (forceCeil) {
// Next we check if we need to use forced rounding
scaledValue = scaledValue - fractial + 1.0;
scaledValue = ceil(scaledValue);
} else if (forceFloor) {
scaledValue = scaledValue - fractial;
scaledValue = floor(scaledValue);
} else {
// Finally we just round the value
scaledValue = scaledValue - fractial +
(!YGDoubleIsUndefined(fractial) &&
(fractial > 0.5 || YGDoubleEqual(fractial, 0.5))
? 1.0
: 0.0);
scaledValue = round(scaledValue);
}
return (YGDoubleIsUndefined(scaledValue) ||
YGDoubleIsUndefined(pointScaleFactor))
Expand Down

0 comments on commit 4266409

Please sign in to comment.