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

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 c15a6b6202
commit 70b7488f97
5 changed files with 99 additions and 37 deletions

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

@ -9,8 +9,8 @@
#include "YGEnums.h" #include "YGEnums.h"
const char *YGAlignToString(const YGAlign value) { const char *YGAlignToString(const YGAlign value){
switch (value) { switch(value){
case YGAlignAuto: case YGAlignAuto:
return "auto"; return "auto";
case YGAlignFlexStart: case YGAlignFlexStart:
@ -31,8 +31,8 @@ const char *YGAlignToString(const YGAlign value) {
return "unknown"; return "unknown";
} }
const char *YGDimensionToString(const YGDimension value) { const char *YGDimensionToString(const YGDimension value){
switch (value) { switch(value){
case YGDimensionWidth: case YGDimensionWidth:
return "width"; return "width";
case YGDimensionHeight: case YGDimensionHeight:
@ -41,8 +41,8 @@ const char *YGDimensionToString(const YGDimension value) {
return "unknown"; return "unknown";
} }
const char *YGDirectionToString(const YGDirection value) { const char *YGDirectionToString(const YGDirection value){
switch (value) { switch(value){
case YGDirectionInherit: case YGDirectionInherit:
return "inherit"; return "inherit";
case YGDirectionLTR: case YGDirectionLTR:
@ -53,8 +53,8 @@ const char *YGDirectionToString(const YGDirection value) {
return "unknown"; return "unknown";
} }
const char *YGDisplayToString(const YGDisplay value) { const char *YGDisplayToString(const YGDisplay value){
switch (value) { switch(value){
case YGDisplayFlex: case YGDisplayFlex:
return "flex"; return "flex";
case YGDisplayNone: case YGDisplayNone:
@ -63,8 +63,8 @@ const char *YGDisplayToString(const YGDisplay value) {
return "unknown"; return "unknown";
} }
const char *YGEdgeToString(const YGEdge value) { const char *YGEdgeToString(const YGEdge value){
switch (value) { switch(value){
case YGEdgeLeft: case YGEdgeLeft:
return "left"; return "left";
case YGEdgeTop: case YGEdgeTop:
@ -87,16 +87,16 @@ const char *YGEdgeToString(const YGEdge value) {
return "unknown"; return "unknown";
} }
const char *YGExperimentalFeatureToString(const YGExperimentalFeature value) { const char *YGExperimentalFeatureToString(const YGExperimentalFeature value){
switch (value) { switch(value){
case YGExperimentalFeatureWebFlexBasis: case YGExperimentalFeatureWebFlexBasis:
return "web-flex-basis"; return "web-flex-basis";
} }
return "unknown"; return "unknown";
} }
const char *YGFlexDirectionToString(const YGFlexDirection value) { const char *YGFlexDirectionToString(const YGFlexDirection value){
switch (value) { switch(value){
case YGFlexDirectionColumn: case YGFlexDirectionColumn:
return "column"; return "column";
case YGFlexDirectionColumnReverse: case YGFlexDirectionColumnReverse:
@ -109,8 +109,8 @@ const char *YGFlexDirectionToString(const YGFlexDirection value) {
return "unknown"; return "unknown";
} }
const char *YGJustifyToString(const YGJustify value) { const char *YGJustifyToString(const YGJustify value){
switch (value) { switch(value){
case YGJustifyFlexStart: case YGJustifyFlexStart:
return "flex-start"; return "flex-start";
case YGJustifyCenter: case YGJustifyCenter:
@ -125,8 +125,8 @@ const char *YGJustifyToString(const YGJustify value) {
return "unknown"; return "unknown";
} }
const char *YGLogLevelToString(const YGLogLevel value) { const char *YGLogLevelToString(const YGLogLevel value){
switch (value) { switch(value){
case YGLogLevelError: case YGLogLevelError:
return "error"; return "error";
case YGLogLevelWarn: case YGLogLevelWarn:
@ -143,8 +143,8 @@ const char *YGLogLevelToString(const YGLogLevel value) {
return "unknown"; return "unknown";
} }
const char *YGMeasureModeToString(const YGMeasureMode value) { const char *YGMeasureModeToString(const YGMeasureMode value){
switch (value) { switch(value){
case YGMeasureModeUndefined: case YGMeasureModeUndefined:
return "undefined"; return "undefined";
case YGMeasureModeExactly: case YGMeasureModeExactly:
@ -155,8 +155,18 @@ const char *YGMeasureModeToString(const YGMeasureMode value) {
return "unknown"; return "unknown";
} }
const char *YGOverflowToString(const YGOverflow value) { const char *YGNodeTypeToString(const YGNodeType value){
switch (value) { switch(value){
case YGNodeTypeDefault:
return "default";
case YGNodeTypeText:
return "text";
}
return "unknown";
}
const char *YGOverflowToString(const YGOverflow value){
switch(value){
case YGOverflowVisible: case YGOverflowVisible:
return "visible"; return "visible";
case YGOverflowHidden: case YGOverflowHidden:
@ -167,8 +177,8 @@ const char *YGOverflowToString(const YGOverflow value) {
return "unknown"; return "unknown";
} }
const char *YGPositionTypeToString(const YGPositionType value) { const char *YGPositionTypeToString(const YGPositionType value){
switch (value) { switch(value){
case YGPositionTypeRelative: case YGPositionTypeRelative:
return "relative"; return "relative";
case YGPositionTypeAbsolute: case YGPositionTypeAbsolute:
@ -177,8 +187,8 @@ const char *YGPositionTypeToString(const YGPositionType value) {
return "unknown"; return "unknown";
} }
const char *YGPrintOptionsToString(const YGPrintOptions value) { const char *YGPrintOptionsToString(const YGPrintOptions value){
switch (value) { switch(value){
case YGPrintOptionsLayout: case YGPrintOptionsLayout:
return "layout"; return "layout";
case YGPrintOptionsStyle: case YGPrintOptionsStyle:
@ -189,8 +199,8 @@ const char *YGPrintOptionsToString(const YGPrintOptions value) {
return "unknown"; return "unknown";
} }
const char *YGUnitToString(const YGUnit value) { const char *YGUnitToString(const YGUnit value){
switch (value) { switch(value){
case YGUnitUndefined: case YGUnitUndefined:
return "undefined"; return "undefined";
case YGUnitPoint: case YGUnitPoint:
@ -203,8 +213,8 @@ const char *YGUnitToString(const YGUnit value) {
return "unknown"; return "unknown";
} }
const char *YGWrapToString(const YGWrap value) { const char *YGWrapToString(const YGWrap value){
switch (value) { switch(value){
case YGWrapNoWrap: case YGWrapNoWrap:
return "no-wrap"; return "no-wrap";
case YGWrapWrap: case YGWrapWrap:
@ -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);