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
1 change: 1 addition & 0 deletions CSSLayout/CSSEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef enum CSSDirection {
} CSSDirection;

typedef enum CSSExperimentalFeature {
CSSExperimentalFeatureRounding,
CSSExperimentalFeatureCount,
} CSSExperimentalFeature;

Expand Down
25 changes: 23 additions & 2 deletions CSSLayout/CSSLayout.c
Original file line number Diff line number Diff line change
Expand Up @@ -2462,6 +2462,23 @@ bool layoutNodeInternal(const CSSNodeRef node,
return (needToVisitNode || cachedResults == NULL);
}


static void roundToPixelGrid(const CSSNodeRef node){

const float fractialLeft = node->layout.position[CSSEdgeLeft] - floorf(node->layout.position[CSSEdgeLeft]);
const float fractialTop = node->layout.position[CSSEdgeTop] - floorf(node->layout.position[CSSEdgeTop]);
node->layout.dimensions[CSSDimensionWidth] = roundf(fractialLeft + node->layout.dimensions[CSSDimensionWidth]) - roundf(fractialLeft);
node->layout.dimensions[CSSDimensionHeight] = roundf(fractialTop + node->layout.dimensions[CSSDimensionHeight]) - roundf(fractialTop);

node->layout.position[CSSEdgeLeft] = roundf(node->layout.position[CSSEdgeLeft]);
node->layout.position[CSSEdgeTop] = roundf(node->layout.position[CSSEdgeTop]);

const uint32_t childCount = CSSNodeListCount(node->children);
for (uint32_t i = 0; i < childCount; i++) {
roundToPixelGrid(CSSNodeGetChild(node, i));
}
}

void CSSNodeCalculateLayout(const CSSNodeRef node,
const float availableWidth,
const float availableHeight,
Expand Down Expand Up @@ -2511,6 +2528,10 @@ void CSSNodeCalculateLayout(const CSSNodeRef node,
"l")) {
setPosition(node, node->layout.direction);

if (CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeatureRounding)){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this in here as it is very little memory and changing this back and forth when adding/removing experiments is just annoying.

roundToPixelGrid(node);
}

if (gPrintTree) {
CSSNodePrint(node, CSSPrintOptionsLayout | CSSPrintOptionsChildren | CSSPrintOptionsStyle);
}
Expand All @@ -2534,7 +2555,7 @@ void CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeature feature, bool
experimentalFeatures[feature] = enabled;
}

bool CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeature feature) {
inline bool CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeature feature) {
return experimentalFeatures[feature];
}

Expand All @@ -2559,4 +2580,4 @@ void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc,
gCSSRealloc = cssRealloc;
gCSSFree = cssFree;
}
}
}
Loading