Skip to content

Commit dce7242

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Breaking: per-node pointScaleFactor (#39403)
Summary: X-link: facebook/yoga#1379 Pull Request resolved: #39403 Right now we have a `pointScaleFactor` per-node, but only ever read the one off the root node. In most cases where config is global, these will be the same, but it is possible for these to differ. This... doesn't make much sense from an API perspective, and there are edge cases where we may want to allow laying out a subtree with a different DPI then the rest of the tree (though I think there might be other solutions to that). We should rethink some of what is currently on config being allowed per-node (do we really need each node to be able to have a separate logger?), but this makes the model consistent in the meantime. This change is breaking to any users relying on setting `pointScaleFactor` on the config of the root node, but not other nodes. Reviewed By: yungsters Differential Revision: D49181131 fbshipit-source-id: f1363ca242094f04b995fd50c1e56834d5003425
1 parent fe4dab9 commit dce7242

File tree

3 files changed

+50
-55
lines changed

3 files changed

+50
-55
lines changed

packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,7 +2487,7 @@ bool calculateLayoutInternal(
24872487
layout->cachedLayout.computedHeight,
24882488
marginAxisRow,
24892489
marginAxisColumn,
2490-
config)) {
2490+
node->getConfig())) {
24912491
cachedResults = &layout->cachedLayout;
24922492
} else {
24932493
// Try to use the measurement cache.
@@ -2505,7 +2505,7 @@ bool calculateLayoutInternal(
25052505
layout->cachedMeasurements[i].computedHeight,
25062506
marginAxisRow,
25072507
marginAxisColumn,
2508-
config)) {
2508+
node->getConfig())) {
25092509
cachedResults = &layout->cachedMeasurements[i];
25102510
break;
25112511
}
@@ -2756,8 +2756,7 @@ void calculateLayout(
27562756
gCurrentGenerationCount.load(std::memory_order_relaxed))) {
27572757
node->setPosition(
27582758
node->getLayout().direction(), ownerWidth, ownerHeight, ownerWidth);
2759-
roundLayoutResultsToPixelGrid(
2760-
node, node->getConfig()->getPointScaleFactor(), 0.0f, 0.0f);
2759+
roundLayoutResultsToPixelGrid(node, 0.0f, 0.0f);
27612760

27622761
#ifdef DEBUG
27632762
if (node->getConfig()->shouldPrintTree()) {

packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,9 @@ float roundValueToPixelGrid(
6464

6565
void roundLayoutResultsToPixelGrid(
6666
yoga::Node* const node,
67-
const double pointScaleFactor,
6867
const double absoluteLeft,
6968
const double absoluteTop) {
70-
if (pointScaleFactor == 0.0f) {
71-
return;
72-
}
69+
const auto pointScaleFactor = node->getConfig()->getPointScaleFactor();
7370

7471
const double nodeLeft = node->getLayout().position[YGEdgeLeft];
7572
const double nodeTop = node->getLayout().position[YGEdgeTop];
@@ -83,52 +80,52 @@ void roundLayoutResultsToPixelGrid(
8380
const double absoluteNodeRight = absoluteNodeLeft + nodeWidth;
8481
const double absoluteNodeBottom = absoluteNodeTop + nodeHeight;
8582

86-
// If a node has a custom measure function we never want to round down its
87-
// size as this could lead to unwanted text truncation.
88-
const bool textRounding = node->getNodeType() == YGNodeTypeText;
89-
90-
node->setLayoutPosition(
91-
roundValueToPixelGrid(nodeLeft, pointScaleFactor, false, textRounding),
92-
YGEdgeLeft);
93-
94-
node->setLayoutPosition(
95-
roundValueToPixelGrid(nodeTop, pointScaleFactor, false, textRounding),
96-
YGEdgeTop);
97-
98-
// We multiply dimension by scale factor and if the result is close to the
99-
// whole number, we don't have any fraction To verify if the result is close
100-
// to whole number we want to check both floor and ceil numbers
101-
const bool hasFractionalWidth =
102-
!yoga::inexactEquals(fmod(nodeWidth * pointScaleFactor, 1.0), 0) &&
103-
!yoga::inexactEquals(fmod(nodeWidth * pointScaleFactor, 1.0), 1.0);
104-
const bool hasFractionalHeight =
105-
!yoga::inexactEquals(fmod(nodeHeight * pointScaleFactor, 1.0), 0) &&
106-
!yoga::inexactEquals(fmod(nodeHeight * pointScaleFactor, 1.0), 1.0);
107-
108-
node->setLayoutDimension(
109-
roundValueToPixelGrid(
110-
absoluteNodeRight,
111-
pointScaleFactor,
112-
(textRounding && hasFractionalWidth),
113-
(textRounding && !hasFractionalWidth)) -
114-
roundValueToPixelGrid(
115-
absoluteNodeLeft, pointScaleFactor, false, textRounding),
116-
YGDimensionWidth);
117-
118-
node->setLayoutDimension(
119-
roundValueToPixelGrid(
120-
absoluteNodeBottom,
121-
pointScaleFactor,
122-
(textRounding && hasFractionalHeight),
123-
(textRounding && !hasFractionalHeight)) -
124-
roundValueToPixelGrid(
125-
absoluteNodeTop, pointScaleFactor, false, textRounding),
126-
YGDimensionHeight);
127-
128-
const size_t childCount = node->getChildCount();
129-
for (size_t i = 0; i < childCount; i++) {
130-
roundLayoutResultsToPixelGrid(
131-
node->getChild(i), pointScaleFactor, absoluteNodeLeft, absoluteNodeTop);
83+
if (pointScaleFactor != 0.0f) {
84+
// If a node has a custom measure function we never want to round down its
85+
// size as this could lead to unwanted text truncation.
86+
const bool textRounding = node->getNodeType() == YGNodeTypeText;
87+
88+
node->setLayoutPosition(
89+
roundValueToPixelGrid(nodeLeft, pointScaleFactor, false, textRounding),
90+
YGEdgeLeft);
91+
92+
node->setLayoutPosition(
93+
roundValueToPixelGrid(nodeTop, pointScaleFactor, false, textRounding),
94+
YGEdgeTop);
95+
96+
// We multiply dimension by scale factor and if the result is close to the
97+
// whole number, we don't have any fraction To verify if the result is close
98+
// to whole number we want to check both floor and ceil numbers
99+
const bool hasFractionalWidth =
100+
!yoga::inexactEquals(fmod(nodeWidth * pointScaleFactor, 1.0), 0) &&
101+
!yoga::inexactEquals(fmod(nodeWidth * pointScaleFactor, 1.0), 1.0);
102+
const bool hasFractionalHeight =
103+
!yoga::inexactEquals(fmod(nodeHeight * pointScaleFactor, 1.0), 0) &&
104+
!yoga::inexactEquals(fmod(nodeHeight * pointScaleFactor, 1.0), 1.0);
105+
106+
node->setLayoutDimension(
107+
roundValueToPixelGrid(
108+
absoluteNodeRight,
109+
pointScaleFactor,
110+
(textRounding && hasFractionalWidth),
111+
(textRounding && !hasFractionalWidth)) -
112+
roundValueToPixelGrid(
113+
absoluteNodeLeft, pointScaleFactor, false, textRounding),
114+
YGDimensionWidth);
115+
116+
node->setLayoutDimension(
117+
roundValueToPixelGrid(
118+
absoluteNodeBottom,
119+
pointScaleFactor,
120+
(textRounding && hasFractionalHeight),
121+
(textRounding && !hasFractionalHeight)) -
122+
roundValueToPixelGrid(
123+
absoluteNodeTop, pointScaleFactor, false, textRounding),
124+
YGDimensionHeight);
125+
}
126+
127+
for (yoga::Node* child : node->getChildren()) {
128+
roundLayoutResultsToPixelGrid(child, absoluteNodeLeft, absoluteNodeTop);
132129
}
133130
}
134131

packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ float roundValueToPixelGrid(
2323
// Round the layout results of a node and its subtree to the pixel grid.
2424
void roundLayoutResultsToPixelGrid(
2525
yoga::Node* const node,
26-
const double pointScaleFactor,
2726
const double absoluteLeft,
2827
const double absoluteTop);
2928

0 commit comments

Comments
 (0)