From 24e2fc95dd996a6de2fcfb1e3aaa8ccdacdc17f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20W=C3=B6hrl?= Date: Tue, 4 Jul 2017 06:18:49 -0700 Subject: [PATCH] Fix text node rounding with fractional dimensions matching the rounding factor Summary: If we have a fractional measure output which matches the subpixel rounding factor, we still should round both dimension into the same direction. Fixes facebook/yoga#580. Closes https://github.com/facebook/yoga/pull/583 Reviewed By: marco-cova Differential Revision: D5274212 Pulled By: emilsjolander fbshipit-source-id: 1febf9194210437ab77f91319d10d4da9b284b79 --- tests/YGRoundingMeasureFuncTest.cpp | 34 ++++++++++++++++++++++++++++- yoga/Yoga.c | 4 ++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/tests/YGRoundingMeasureFuncTest.cpp b/tests/YGRoundingMeasureFuncTest.cpp index d8ff045d..3df6557b 100644 --- a/tests/YGRoundingMeasureFuncTest.cpp +++ b/tests/YGRoundingMeasureFuncTest.cpp @@ -26,7 +26,17 @@ static YGSize _measureCeil(YGNodeRef node, float height, YGMeasureMode heightMode) { return YGSize{ - width = 10.5, height = 10.5, + width = 10.5f, height = 10.5f, + }; +} + +static YGSize _measureFractial(YGNodeRef node, + float width, + YGMeasureMode widthMode, + float height, + YGMeasureMode heightMode) { + return YGSize{ + width = 0.5f, height = 0.5f, }; } @@ -97,3 +107,25 @@ TEST(YogaTest, rounding_feature_with_custom_measure_func_ceil) { YGConfigFree(config); } + +TEST(YogaTest, rounding_feature_with_custom_measure_and_fractial_matching_scale) { + const YGConfigRef config = YGConfigNew(); + const YGNodeRef root = YGNodeNewWithConfig(config); + + const YGNodeRef root_child0 = YGNodeNewWithConfig(config); + YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 73.625); + YGNodeSetMeasureFunc(root_child0, _measureFractial); + YGNodeInsertChild(root, root_child0, 0); + + YGConfigSetPointScaleFactor(config, 2.0f); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + ASSERT_FLOAT_EQ(0.5, YGNodeLayoutGetWidth(root_child0)); + ASSERT_FLOAT_EQ(0.5, YGNodeLayoutGetHeight(root_child0)); + ASSERT_FLOAT_EQ(73.5, YGNodeLayoutGetLeft(root_child0)); + + YGNodeFreeRecursive(root); + + YGConfigFree(config); +} diff --git a/yoga/Yoga.c b/yoga/Yoga.c index aba42c7d..bde8deae 100644 --- a/yoga/Yoga.c +++ b/yoga/Yoga.c @@ -3471,8 +3471,8 @@ static void YGRoundToPixelGrid(const YGNodeRef node, node->layout.position[YGEdgeTop] = YGRoundValueToPixelGrid(nodeTop, pointScaleFactor, false, textRounding); - const bool hasFractionalWidth = !YGFloatsEqual(fmodf(nodeWidth, 1.0), 0); - const bool hasFractionalHeight = !YGFloatsEqual(fmodf(nodeHeight, 1.0), 0); + const bool hasFractionalWidth = !YGFloatsEqual(fmodf(nodeWidth, 1 / pointScaleFactor), 0); + const bool hasFractionalHeight = !YGFloatsEqual(fmodf(nodeHeight, 1 / pointScaleFactor), 0); node->layout.dimensions[YGDimensionWidth] = YGRoundValueToPixelGrid(