Adding node type and moving rounding dependency to rely on that type

Summary: This diff adds node type definition to Yoga and moves rounding to rely on the node type. If the node has measure function we consider that node to be text node, otherwise we have default behavior.

Reviewed By: emilsjolander

Differential Revision: D5025107

fbshipit-source-id: a8d66e2f9c5d02ab080784cc474be583a09b92e2
This commit is contained in:
Georgiy Kassabli
2017-05-11 08:09:30 -07:00
committed by Facebook Github Bot
parent 626a05fb6f
commit 3fefe9fc49
8 changed files with 124 additions and 37 deletions

View File

@@ -0,0 +1,17 @@
/**
* 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.
*/
namespace Facebook.Yoga
{
public enum YogaNodeType
{
Default,
Text,
}
}

View File

@@ -84,6 +84,10 @@ ENUMS = {
'Vertical', 'Vertical',
'All', 'All',
], ],
'NodeType': [
'Default',
'Text',
],
'LogLevel': [ 'LogLevel': [
'Error', 'Error',
'Warn', 'Warn',

View File

@@ -0,0 +1,36 @@
/**
* 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.
*/
package com.facebook.yoga;
import com.facebook.proguard.annotations.DoNotStrip;
@DoNotStrip
public enum YogaNodeType {
DEFAULT(0),
TEXT(1);
private int mIntValue;
YogaNodeType(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static YogaNodeType fromInt(int value) {
switch (value) {
case 0: return DEFAULT;
case 1: return TEXT;
default: throw new IllegalArgumentException("Unknown enum value: " + value);
}
}
}

View File

@@ -72,6 +72,10 @@ module.exports = {
MEASURE_MODE_EXACTLY: 1, MEASURE_MODE_EXACTLY: 1,
MEASURE_MODE_AT_MOST: 2, MEASURE_MODE_AT_MOST: 2,
NODE_TYPE_COUNT: 2,
NODE_TYPE_DEFAULT: 0,
NODE_TYPE_TEXT: 1,
OVERFLOW_COUNT: 3, OVERFLOW_COUNT: 3,
OVERFLOW_VISIBLE: 0, OVERFLOW_VISIBLE: 0,
OVERFLOW_HIDDEN: 1, OVERFLOW_HIDDEN: 1,

View File

@@ -155,6 +155,16 @@ const char *YGMeasureModeToString(const YGMeasureMode value) {
return "unknown"; return "unknown";
} }
const char *YGNodeTypeToString(const YGNodeType value){
switch(value){
case YGNodeTypeDefault:
return "default";
case YGNodeTypeText:
return "text";
}
return "unknown";
}
const char *YGOverflowToString(const YGOverflow value){ const char *YGOverflowToString(const YGOverflow value){
switch(value){ switch(value){
case YGOverflowVisible: case YGOverflowVisible:
@@ -214,3 +224,4 @@ const char *YGWrapToString(const YGWrap value) {
} }
return "unknown"; return "unknown";
} }

View File

@@ -106,6 +106,13 @@ typedef YG_ENUM_BEGIN(YGMeasureMode) {
} YG_ENUM_END(YGMeasureMode); } YG_ENUM_END(YGMeasureMode);
WIN_EXPORT const char *YGMeasureModeToString(const YGMeasureMode value); WIN_EXPORT const char *YGMeasureModeToString(const YGMeasureMode value);
#define YGNodeTypeCount 2
typedef YG_ENUM_BEGIN(YGNodeType) {
YGNodeTypeDefault,
YGNodeTypeText,
} YG_ENUM_END(YGNodeType);
WIN_EXPORT const char *YGNodeTypeToString(const YGNodeType value);
#define YGOverflowCount 3 #define YGOverflowCount 3
typedef YG_ENUM_BEGIN(YGOverflow) { typedef YG_ENUM_BEGIN(YGOverflow) {
YGOverflowVisible, YGOverflowVisible,

View File

@@ -121,6 +121,7 @@ typedef struct YGNode {
bool isDirty; bool isDirty;
bool hasNewLayout; bool hasNewLayout;
YGNodeType nodeType;
YGValue const *resolvedDimensions[2]; YGValue const *resolvedDimensions[2];
} YGNode; } YGNode;
@@ -158,6 +159,7 @@ static YGNode gYGNodeDefaults = {
.children = NULL, .children = NULL,
.hasNewLayout = true, .hasNewLayout = true,
.isDirty = false, .isDirty = false,
.nodeType = YGNodeTypeDefault,
.resolvedDimensions = {[YGDimensionWidth] = &YGValueUndefined, .resolvedDimensions = {[YGDimensionWidth] = &YGValueUndefined,
[YGDimensionHeight] = &YGValueUndefined}, [YGDimensionHeight] = &YGValueUndefined},
@@ -444,11 +446,15 @@ static void YGNodeMarkDirtyInternal(const YGNodeRef node) {
void YGNodeSetMeasureFunc(const YGNodeRef node, YGMeasureFunc measureFunc) { void YGNodeSetMeasureFunc(const YGNodeRef node, YGMeasureFunc measureFunc) {
if (measureFunc == NULL) { if (measureFunc == NULL) {
node->measure = NULL; node->measure = NULL;
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate places in Litho
node->nodeType = YGNodeTypeDefault;
} else { } else {
YGAssertWithNode(node, YGAssertWithNode(node,
YGNodeGetChildCount(node) == 0, YGNodeGetChildCount(node) == 0,
"Cannot set measure function: Nodes with measure functions cannot have children."); "Cannot set measure function: Nodes with measure functions cannot have children.");
node->measure = measureFunc; node->measure = measureFunc;
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate places in Litho
node->nodeType = YGNodeTypeText;
} }
} }
@@ -722,6 +728,7 @@ static inline const YGValue *YGNodeResolveFlexBasisPtr(const YGNodeRef node) {
YG_NODE_PROPERTY_IMPL(void *, Context, context, context); YG_NODE_PROPERTY_IMPL(void *, Context, context, context);
YG_NODE_PROPERTY_IMPL(YGPrintFunc, PrintFunc, printFunc, print); YG_NODE_PROPERTY_IMPL(YGPrintFunc, PrintFunc, printFunc, print);
YG_NODE_PROPERTY_IMPL(bool, HasNewLayout, hasNewLayout, hasNewLayout); YG_NODE_PROPERTY_IMPL(bool, HasNewLayout, hasNewLayout, hasNewLayout);
YG_NODE_PROPERTY_IMPL(YGNodeType, NodeType, nodeType, nodeType);
YG_NODE_STYLE_PROPERTY_IMPL(YGDirection, Direction, direction, direction); YG_NODE_STYLE_PROPERTY_IMPL(YGDirection, Direction, direction, direction);
YG_NODE_STYLE_PROPERTY_IMPL(YGFlexDirection, FlexDirection, flexDirection, flexDirection); YG_NODE_STYLE_PROPERTY_IMPL(YGFlexDirection, FlexDirection, flexDirection, flexDirection);
@@ -3402,19 +3409,19 @@ static void YGRoundToPixelGrid(const YGNodeRef node,
// If a node has a custom measure function we never want to round down its size as this could // If a node has a custom measure function we never want to round down its size as this could
// lead to unwanted text truncation. // lead to unwanted text truncation.
const bool hasMeasure = node->measure != NULL; const bool textRounding = node->nodeType == YGNodeTypeText;
node->layout.position[YGEdgeLeft] = node->layout.position[YGEdgeLeft] =
YGRoundValueToPixelGrid(nodeLeft, pointScaleFactor, false, hasMeasure); YGRoundValueToPixelGrid(nodeLeft, pointScaleFactor, false, textRounding);
node->layout.position[YGEdgeTop] = node->layout.position[YGEdgeTop] =
YGRoundValueToPixelGrid(nodeTop, pointScaleFactor, false, hasMeasure); YGRoundValueToPixelGrid(nodeTop, pointScaleFactor, false, textRounding);
node->layout.dimensions[YGDimensionWidth] = node->layout.dimensions[YGDimensionWidth] =
YGRoundValueToPixelGrid(absoluteNodeRight, pointScaleFactor, hasMeasure, false) - YGRoundValueToPixelGrid(absoluteNodeRight, pointScaleFactor, textRounding, false) -
YGRoundValueToPixelGrid(absoluteNodeLeft, pointScaleFactor, false, hasMeasure); YGRoundValueToPixelGrid(absoluteNodeLeft, pointScaleFactor, false, textRounding);
node->layout.dimensions[YGDimensionHeight] = node->layout.dimensions[YGDimensionHeight] =
YGRoundValueToPixelGrid(absoluteNodeBottom, pointScaleFactor, hasMeasure, false) - YGRoundValueToPixelGrid(absoluteNodeBottom, pointScaleFactor, textRounding, false) -
YGRoundValueToPixelGrid(absoluteNodeTop, pointScaleFactor, false, hasMeasure); YGRoundValueToPixelGrid(absoluteNodeTop, pointScaleFactor, false, textRounding);
const uint32_t childCount = YGNodeListCount(node->children); const uint32_t childCount = YGNodeListCount(node->children);
for (uint32_t i = 0; i < childCount; i++) { for (uint32_t i = 0; i < childCount; i++) {

View File

@@ -161,6 +161,7 @@ YG_NODE_PROPERTY(YGMeasureFunc, MeasureFunc, measureFunc);
YG_NODE_PROPERTY(YGBaselineFunc, BaselineFunc, baselineFunc) YG_NODE_PROPERTY(YGBaselineFunc, BaselineFunc, baselineFunc)
YG_NODE_PROPERTY(YGPrintFunc, PrintFunc, printFunc); YG_NODE_PROPERTY(YGPrintFunc, PrintFunc, printFunc);
YG_NODE_PROPERTY(bool, HasNewLayout, hasNewLayout); YG_NODE_PROPERTY(bool, HasNewLayout, hasNewLayout);
YG_NODE_PROPERTY(YGNodeType, NodeType, nodeType);
YG_NODE_STYLE_PROPERTY(YGDirection, Direction, direction); YG_NODE_STYLE_PROPERTY(YGDirection, Direction, direction);
YG_NODE_STYLE_PROPERTY(YGFlexDirection, FlexDirection, flexDirection); YG_NODE_STYLE_PROPERTY(YGFlexDirection, FlexDirection, flexDirection);