Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature to use rounded values #256

Closed
wants to merge 13 commits into from
Closed
11 changes: 10 additions & 1 deletion CSSLayout/CSSLayout.c
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,8 @@ static void layoutNodeImpl(const CSSNodeRef node,
float deltaFlexShrinkScaledFactors = 0;
float deltaFlexGrowFactors = 0;
float strippedValuesDueToRounding = 0;
int numberOfRelevantRoundingChildren = 0;

currentRelativeChild = firstRelativeChild;
while (currentRelativeChild != NULL) {
childFlexBasis = currentRelativeChild->layout.computedFlexBasis;
Expand Down Expand Up @@ -1676,6 +1678,8 @@ static void layoutNodeImpl(const CSSNodeRef node,
baseMainSize =
childFlexBasis + roundToPixelGrid(remainingFreeSpace / totalFlexGrowFactors * flexGrowFactor, &strippedValuesDueToRounding);
boundMainSize = boundAxis(currentRelativeChild, mainAxis, baseMainSize);

numberOfRelevantRoundingChildren++;
if (baseMainSize != boundMainSize) {
// By excluding this item's size and flex factor from remaining,
// this item's
Expand All @@ -1699,6 +1703,11 @@ static void layoutNodeImpl(const CSSNodeRef node,
// Second pass: resolve the sizes of the flexible items
deltaFreeSpace = 0;
currentRelativeChild = firstRelativeChild;

//if rounding feature is enabled, we distribute the remaining space over the children
//we need to figure out if we should start with the first or second child
int roundingDistributionChildIndex = (int)(numberOfRelevantRoundingChildren - floorf(strippedValuesDueToRounding)) % 2;

while (currentRelativeChild != NULL) {
childFlexBasis = currentRelativeChild->layout.computedFlexBasis;
float updatedMainSize = childFlexBasis;
Expand All @@ -1724,7 +1733,7 @@ static void layoutNodeImpl(const CSSNodeRef node,
flexGrowFactor = CSSNodeStyleGetFlexGrow(currentRelativeChild);

float valueToAddFromRounding = 0;
if (CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeatureRounding) && strippedValuesDueToRounding >= 0.5)
if (CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeatureRounding) && strippedValuesDueToRounding >= 0.5 && roundingDistributionChildIndex++ % 2 == 0)
{
valueToAddFromRounding = fminf(1, strippedValuesDueToRounding--);
}
Expand Down
345 changes: 345 additions & 0 deletions tests/CSSLayoutPixelRoundingTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

/**
* @Generated by gentest/gentest.sh with the following input
*
<div id="pixel_rounding_flex_basis_flex_grow_row_width_of_100" style="width: 100px; height: 100px; flex-direction: row;">
<div style="flex-grow: 1;"></div>
<div style="flex-grow: 1;"></div>
<div style="flex-grow: 1;"></div>
</div>

<div id="pixel_rounding_flex_basis_flex_grow_row_prime_number_width" style="width: 113px; height: 100px; flex-direction: row;">
<div style="flex-grow: 1;"></div>
<div style="flex-grow: 1;"></div>
<div style="flex-grow: 1;"></div>
<div style="flex-grow: 1;"></div>
<div style="flex-grow: 1;"></div>
</div>

<div id="pixel_rounding_flex_basis_flex_shrink_row" style="width: 101px; height: 100px; flex-direction: row;">
<div style="flex-basis: 100px; flex-shrink: 1;"></div>
<div style="flex-basis: 25px;"></div>
<div style="flex-basis: 25px;"></div>
</div>

<div id="pixel_rounding_flex_basis_overrides_main_size" style="height: 113px; width: 100px;">
<div style="height: 20px; flex-grow:1; flex-basis:50px;"></div>
<div style="height: 10px; flex-grow:1;"></div>
<div style="height: 10px; flex-grow:1;"></div>
</div>
*
*/

#include <stdafx.h>
#include <CSSLayout/CSSLayout.h>
#include <gtest/gtest.h>

class CSSLayoutFeatureRoundingTest : public ::testing::Test {

protected:
virtual void SetUp() {
CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeatureRounding, true);
}

virtual void TearDown() {
CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeatureRounding, false);
}

};

TEST_F(CSSLayoutFeatureRoundingTest, pixel_rounding_flex_basis_flex_grow_row_width_of_100, Setup, Teardown) {
const CSSNodeRef root = CSSNodeNew();
CSSNodeStyleSetFlexDirection(root, CSSFlexDirectionRow);
CSSNodeStyleSetWidth(root, 100);
CSSNodeStyleSetHeight(root, 100);

const CSSNodeRef root_child0 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child0, 1);
CSSNodeInsertChild(root, root_child0, 0);

const CSSNodeRef root_child1 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child1, 1);
CSSNodeInsertChild(root, root_child1, 1);

const CSSNodeRef root_child2 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child2, 1);
CSSNodeInsertChild(root, root_child2, 2);
CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR);

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetWidth(root));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root));

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(33, CSSNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child0));

ASSERT_FLOAT_EQ(33, CSSNodeLayoutGetLeft(root_child1));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child1));
ASSERT_FLOAT_EQ(34, CSSNodeLayoutGetWidth(root_child1));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child1));

ASSERT_FLOAT_EQ(67, CSSNodeLayoutGetLeft(root_child2));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child2));
ASSERT_FLOAT_EQ(33, CSSNodeLayoutGetWidth(root_child2));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child2));

CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionRTL);

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetWidth(root));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root));

ASSERT_FLOAT_EQ(67, CSSNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(33, CSSNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child0));

ASSERT_FLOAT_EQ(33, CSSNodeLayoutGetLeft(root_child1));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child1));
ASSERT_FLOAT_EQ(34, CSSNodeLayoutGetWidth(root_child1));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child1));

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root_child2));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child2));
ASSERT_FLOAT_EQ(33, CSSNodeLayoutGetWidth(root_child2));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child2));

CSSNodeFreeRecursive(root);
}

TEST_F(CSSLayoutFeatureRoundingTest, pixel_rounding_flex_basis_flex_grow_row_prime_number_width) {
const CSSNodeRef root = CSSNodeNew();
CSSNodeStyleSetFlexDirection(root, CSSFlexDirectionRow);
CSSNodeStyleSetWidth(root, 113);
CSSNodeStyleSetHeight(root, 100);

const CSSNodeRef root_child0 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child0, 1);
CSSNodeInsertChild(root, root_child0, 0);

const CSSNodeRef root_child1 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child1, 1);
CSSNodeInsertChild(root, root_child1, 1);

const CSSNodeRef root_child2 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child2, 1);
CSSNodeInsertChild(root, root_child2, 2);

const CSSNodeRef root_child3 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child3, 1);
CSSNodeInsertChild(root, root_child3, 3);

const CSSNodeRef root_child4 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child4, 1);
CSSNodeInsertChild(root, root_child4, 4);
CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR);

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root));
ASSERT_FLOAT_EQ(113, CSSNodeLayoutGetWidth(root));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root));

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(23, CSSNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child0));

ASSERT_FLOAT_EQ(23, CSSNodeLayoutGetLeft(root_child1));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child1));
ASSERT_FLOAT_EQ(22, CSSNodeLayoutGetWidth(root_child1));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child1));

ASSERT_FLOAT_EQ(45, CSSNodeLayoutGetLeft(root_child2));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child2));
ASSERT_FLOAT_EQ(23, CSSNodeLayoutGetWidth(root_child2));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child2));

ASSERT_FLOAT_EQ(68, CSSNodeLayoutGetLeft(root_child3));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child3));
ASSERT_FLOAT_EQ(22, CSSNodeLayoutGetWidth(root_child3));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child3));

ASSERT_FLOAT_EQ(90, CSSNodeLayoutGetLeft(root_child4));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child4));
ASSERT_FLOAT_EQ(23, CSSNodeLayoutGetWidth(root_child4));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child4));

CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionRTL);

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root));
ASSERT_FLOAT_EQ(113, CSSNodeLayoutGetWidth(root));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root));

ASSERT_FLOAT_EQ(90, CSSNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(23, CSSNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child0));

ASSERT_FLOAT_EQ(68, CSSNodeLayoutGetLeft(root_child1));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child1));
ASSERT_FLOAT_EQ(22, CSSNodeLayoutGetWidth(root_child1));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child1));

ASSERT_FLOAT_EQ(45, CSSNodeLayoutGetLeft(root_child2));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child2));
ASSERT_FLOAT_EQ(23, CSSNodeLayoutGetWidth(root_child2));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child2));

ASSERT_FLOAT_EQ(23, CSSNodeLayoutGetLeft(root_child3));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child3));
ASSERT_FLOAT_EQ(22, CSSNodeLayoutGetWidth(root_child3));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child3));

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root_child4));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child4));
ASSERT_FLOAT_EQ(23, CSSNodeLayoutGetWidth(root_child4));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child4));

CSSNodeFreeRecursive(root);
}

TEST_F(CSSLayoutFeatureRoundingTest, pixel_rounding_flex_basis_flex_shrink_row) {
const CSSNodeRef root = CSSNodeNew();
CSSNodeStyleSetFlexDirection(root, CSSFlexDirectionRow);
CSSNodeStyleSetWidth(root, 101);
CSSNodeStyleSetHeight(root, 100);

const CSSNodeRef root_child0 = CSSNodeNew();
CSSNodeStyleSetFlexShrink(root_child0, 1);
CSSNodeStyleSetFlexBasis(root_child0, 100);
CSSNodeInsertChild(root, root_child0, 0);

const CSSNodeRef root_child1 = CSSNodeNew();
CSSNodeStyleSetFlexBasis(root_child1, 25);
CSSNodeInsertChild(root, root_child1, 1);

const CSSNodeRef root_child2 = CSSNodeNew();
CSSNodeStyleSetFlexBasis(root_child2, 25);
CSSNodeInsertChild(root, root_child2, 2);
CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR);

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root));
ASSERT_FLOAT_EQ(101, CSSNodeLayoutGetWidth(root));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root));

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(51, CSSNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child0));

ASSERT_FLOAT_EQ(51, CSSNodeLayoutGetLeft(root_child1));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child1));
ASSERT_FLOAT_EQ(25, CSSNodeLayoutGetWidth(root_child1));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child1));

ASSERT_FLOAT_EQ(76, CSSNodeLayoutGetLeft(root_child2));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child2));
ASSERT_FLOAT_EQ(25, CSSNodeLayoutGetWidth(root_child2));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child2));

CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionRTL);

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root));
ASSERT_FLOAT_EQ(101, CSSNodeLayoutGetWidth(root));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root));

ASSERT_FLOAT_EQ(50, CSSNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(51, CSSNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child0));

ASSERT_FLOAT_EQ(25, CSSNodeLayoutGetLeft(root_child1));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child1));
ASSERT_FLOAT_EQ(25, CSSNodeLayoutGetWidth(root_child1));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child1));

ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetLeft(root_child2));
ASSERT_FLOAT_EQ(0, CSSNodeLayoutGetTop(root_child2));
ASSERT_FLOAT_EQ(25, CSSNodeLayoutGetWidth(root_child2));
ASSERT_FLOAT_EQ(100, CSSNodeLayoutGetHeight(root_child2));

CSSNodeFreeRecursive(root);
}


TEST_F(CSSLayoutFeatureRoundingTest, pixel_roundingflex_basis_overrides_main_size) {
const CSSNodeRef root = CSSNodeNew();
CSSNodeStyleSetWidth(root, 100);
CSSNodeStyleSetHeight(root, 113);

const CSSNodeRef root_child0 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child0, 1);
CSSNodeStyleSetFlexBasis(root_child0, 50);
CSSNodeStyleSetHeight(root_child0, 20);
CSSNodeInsertChild(root, root_child0, 0);

const CSSNodeRef root_child1 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child1, 1);
CSSNodeStyleSetHeight(root_child1, 10);
CSSNodeInsertChild(root, root_child1, 1);

const CSSNodeRef root_child2 = CSSNodeNew();
CSSNodeStyleSetFlexGrow(root_child2, 1);
CSSNodeStyleSetHeight(root_child2, 10);
CSSNodeInsertChild(root, root_child2, 2);
CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR);

ASSERT_EQ(0, CSSNodeLayoutGetLeft(root));
ASSERT_EQ(0, CSSNodeLayoutGetTop(root));
ASSERT_EQ(100, CSSNodeLayoutGetWidth(root));
ASSERT_EQ(113, CSSNodeLayoutGetHeight(root));

ASSERT_EQ(0, CSSNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, CSSNodeLayoutGetTop(root_child0));
ASSERT_EQ(100, CSSNodeLayoutGetWidth(root_child0));
ASSERT_EQ(64, CSSNodeLayoutGetHeight(root_child0));

ASSERT_EQ(0, CSSNodeLayoutGetLeft(root_child1));
ASSERT_EQ(64, CSSNodeLayoutGetTop(root_child1));
ASSERT_EQ(100, CSSNodeLayoutGetWidth(root_child1));
ASSERT_EQ(25, CSSNodeLayoutGetHeight(root_child1));

ASSERT_EQ(0, CSSNodeLayoutGetLeft(root_child2));
ASSERT_EQ(89, CSSNodeLayoutGetTop(root_child2));
ASSERT_EQ(100, CSSNodeLayoutGetWidth(root_child2));
ASSERT_EQ(24, CSSNodeLayoutGetHeight(root_child2));

CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionRTL);

ASSERT_EQ(0, CSSNodeLayoutGetLeft(root));
ASSERT_EQ(0, CSSNodeLayoutGetTop(root));
ASSERT_EQ(100, CSSNodeLayoutGetWidth(root));
ASSERT_EQ(113, CSSNodeLayoutGetHeight(root));

ASSERT_EQ(0, CSSNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, CSSNodeLayoutGetTop(root_child0));
ASSERT_EQ(100, CSSNodeLayoutGetWidth(root_child0));
ASSERT_EQ(64, CSSNodeLayoutGetHeight(root_child0));

ASSERT_EQ(0, CSSNodeLayoutGetLeft(root_child1));
ASSERT_EQ(64, CSSNodeLayoutGetTop(root_child1));
ASSERT_EQ(100, CSSNodeLayoutGetWidth(root_child1));
ASSERT_EQ(25, CSSNodeLayoutGetHeight(root_child1));

ASSERT_EQ(0, CSSNodeLayoutGetLeft(root_child2));
ASSERT_EQ(89, CSSNodeLayoutGetTop(root_child2));
ASSERT_EQ(100, CSSNodeLayoutGetWidth(root_child2));
ASSERT_EQ(24, CSSNodeLayoutGetHeight(root_child2));

CSSNodeFreeRecursive(root);
}