diff --git a/React/CSSLayout/CSSLayout.c b/React/CSSLayout/CSSLayout.c index 7a5766eda..52d2e5c84 100644 --- a/React/CSSLayout/CSSLayout.c +++ b/React/CSSLayout/CSSLayout.c @@ -54,7 +54,7 @@ typedef struct CSSLayout { CSSCachedMeasurement cachedMeasurements[CSS_MAX_CACHED_RESULT_COUNT]; float measuredDimensions[2]; - CSSCachedMeasurement cached_layout; + CSSCachedMeasurement cachedLayout; } CSSLayout; typedef struct CSSStyle { @@ -186,8 +186,8 @@ void CSSNodeInit(const CSSNodeRef node) { node->layout.measuredDimensions[CSSDimensionWidth] = CSSUndefined; node->layout.measuredDimensions[CSSDimensionHeight] = CSSUndefined; - node->layout.cached_layout.widthMeasureMode = (CSSMeasureMode) -1; - node->layout.cached_layout.heightMeasureMode = (CSSMeasureMode) -1; + node->layout.cachedLayout.widthMeasureMode = (CSSMeasureMode) -1; + node->layout.cachedLayout.heightMeasureMode = (CSSMeasureMode) -1; } void _CSSNodeMarkDirty(const CSSNodeRef node) { @@ -362,24 +362,24 @@ static void indent(const uint32_t n) { } } -static void print_number_0(const char *str, const float number) { +static void printNumberIfNotZero(const char *str, const float number) { if (!eq(number, 0)) { printf("%s: %g, ", str, number); } } -static void print_number_nan(const char *str, const float number) { - if (!isnan(number)) { +static void printNumberIfNotUndefined(const char *str, const float number) { + if (!CSSValueIsUndefined(number)) { printf("%s: %g, ", str, number); } } -static bool four_equal(const float four[4]) { +static bool eqFour(const float four[4]) { return eq(four[0], four[1]) && eq(four[0], four[2]) && eq(four[0], four[3]); } static void -print_css_node_rec(const CSSNodeRef node, const CSSPrintOptions options, const uint32_t level) { +_CSSNodePrint(const CSSNodeRef node, const CSSPrintOptions options, const uint32_t level) { indent(level); printf("{"); @@ -443,9 +443,9 @@ print_css_node_rec(const CSSNodeRef node, const CSSPrintOptions options, const u printf("alignSelf: 'stretch', "); } - print_number_nan("flexGrow", node->style.flexGrow); - print_number_nan("flexShrink", node->style.flexShrink); - print_number_nan("flexBasis", node->style.flexBasis); + printNumberIfNotUndefined("flexGrow", node->style.flexGrow); + printNumberIfNotUndefined("flexShrink", node->style.flexShrink); + printNumberIfNotUndefined("flexBasis", node->style.flexBasis); if (node->style.overflow == CSSOverflowHidden) { printf("overflow: 'hidden', "); @@ -453,62 +453,70 @@ print_css_node_rec(const CSSNodeRef node, const CSSPrintOptions options, const u printf("overflow: 'visible', "); } - if (four_equal(node->style.margin)) { - print_number_0("margin", computedEdgeValue(node->style.margin, CSSEdgeLeft, 0)); + if (eqFour(node->style.margin)) { + printNumberIfNotZero("margin", computedEdgeValue(node->style.margin, CSSEdgeLeft, 0)); } else { - print_number_0("marginLeft", computedEdgeValue(node->style.margin, CSSEdgeLeft, 0)); - print_number_0("marginRight", computedEdgeValue(node->style.margin, CSSEdgeRight, 0)); - print_number_0("marginTop", computedEdgeValue(node->style.margin, CSSEdgeTop, 0)); - print_number_0("marginBottom", computedEdgeValue(node->style.margin, CSSEdgeBottom, 0)); - print_number_0("marginStart", computedEdgeValue(node->style.margin, CSSEdgeStart, 0)); - print_number_0("marginEnd", computedEdgeValue(node->style.margin, CSSEdgeEnd, 0)); + printNumberIfNotZero("marginLeft", computedEdgeValue(node->style.margin, CSSEdgeLeft, 0)); + printNumberIfNotZero("marginRight", computedEdgeValue(node->style.margin, CSSEdgeRight, 0)); + printNumberIfNotZero("marginTop", computedEdgeValue(node->style.margin, CSSEdgeTop, 0)); + printNumberIfNotZero("marginBottom", computedEdgeValue(node->style.margin, CSSEdgeBottom, 0)); + printNumberIfNotZero("marginStart", computedEdgeValue(node->style.margin, CSSEdgeStart, 0)); + printNumberIfNotZero("marginEnd", computedEdgeValue(node->style.margin, CSSEdgeEnd, 0)); } - if (four_equal(node->style.padding)) { - print_number_0("padding", computedEdgeValue(node->style.padding, CSSEdgeLeft, 0)); + if (eqFour(node->style.padding)) { + printNumberIfNotZero("padding", computedEdgeValue(node->style.padding, CSSEdgeLeft, 0)); } else { - print_number_0("paddingLeft", computedEdgeValue(node->style.padding, CSSEdgeLeft, 0)); - print_number_0("paddingRight", computedEdgeValue(node->style.padding, CSSEdgeRight, 0)); - print_number_0("paddingTop", computedEdgeValue(node->style.padding, CSSEdgeTop, 0)); - print_number_0("paddingBottom", computedEdgeValue(node->style.padding, CSSEdgeBottom, 0)); - print_number_0("paddingStart", computedEdgeValue(node->style.padding, CSSEdgeStart, 0)); - print_number_0("paddingEnd", computedEdgeValue(node->style.padding, CSSEdgeEnd, 0)); + printNumberIfNotZero("paddingLeft", computedEdgeValue(node->style.padding, CSSEdgeLeft, 0)); + printNumberIfNotZero("paddingRight", computedEdgeValue(node->style.padding, CSSEdgeRight, 0)); + printNumberIfNotZero("paddingTop", computedEdgeValue(node->style.padding, CSSEdgeTop, 0)); + printNumberIfNotZero("paddingBottom", + computedEdgeValue(node->style.padding, CSSEdgeBottom, 0)); + printNumberIfNotZero("paddingStart", computedEdgeValue(node->style.padding, CSSEdgeStart, 0)); + printNumberIfNotZero("paddingEnd", computedEdgeValue(node->style.padding, CSSEdgeEnd, 0)); } - if (four_equal(node->style.border)) { - print_number_0("borderWidth", computedEdgeValue(node->style.border, CSSEdgeLeft, 0)); + if (eqFour(node->style.border)) { + printNumberIfNotZero("borderWidth", computedEdgeValue(node->style.border, CSSEdgeLeft, 0)); } else { - print_number_0("borderLeftWidth", computedEdgeValue(node->style.border, CSSEdgeLeft, 0)); - print_number_0("borderRightWidth", computedEdgeValue(node->style.border, CSSEdgeRight, 0)); - print_number_0("borderTopWidth", computedEdgeValue(node->style.border, CSSEdgeTop, 0)); - print_number_0("borderBottomWidth", computedEdgeValue(node->style.border, CSSEdgeBottom, 0)); - print_number_0("borderStartWidth", computedEdgeValue(node->style.border, CSSEdgeStart, 0)); - print_number_0("borderEndWidth", computedEdgeValue(node->style.border, CSSEdgeEnd, 0)); + printNumberIfNotZero("borderLeftWidth", + computedEdgeValue(node->style.border, CSSEdgeLeft, 0)); + printNumberIfNotZero("borderRightWidth", + computedEdgeValue(node->style.border, CSSEdgeRight, 0)); + printNumberIfNotZero("borderTopWidth", computedEdgeValue(node->style.border, CSSEdgeTop, 0)); + printNumberIfNotZero("borderBottomWidth", + computedEdgeValue(node->style.border, CSSEdgeBottom, 0)); + printNumberIfNotZero("borderStartWidth", + computedEdgeValue(node->style.border, CSSEdgeStart, 0)); + printNumberIfNotZero("borderEndWidth", computedEdgeValue(node->style.border, CSSEdgeEnd, 0)); } - print_number_nan("width", node->style.dimensions[CSSDimensionWidth]); - print_number_nan("height", node->style.dimensions[CSSDimensionHeight]); - print_number_nan("maxWidth", node->style.maxDimensions[CSSDimensionWidth]); - print_number_nan("maxHeight", node->style.maxDimensions[CSSDimensionHeight]); - print_number_nan("minWidth", node->style.minDimensions[CSSDimensionWidth]); - print_number_nan("minHeight", node->style.minDimensions[CSSDimensionHeight]); + printNumberIfNotUndefined("width", node->style.dimensions[CSSDimensionWidth]); + printNumberIfNotUndefined("height", node->style.dimensions[CSSDimensionHeight]); + printNumberIfNotUndefined("maxWidth", node->style.maxDimensions[CSSDimensionWidth]); + printNumberIfNotUndefined("maxHeight", node->style.maxDimensions[CSSDimensionHeight]); + printNumberIfNotUndefined("minWidth", node->style.minDimensions[CSSDimensionWidth]); + printNumberIfNotUndefined("minHeight", node->style.minDimensions[CSSDimensionHeight]); if (node->style.positionType == CSSPositionTypeAbsolute) { printf("position: 'absolute', "); } - print_number_nan("left", computedEdgeValue(node->style.position, CSSEdgeLeft, CSSUndefined)); - print_number_nan("right", computedEdgeValue(node->style.position, CSSEdgeRight, CSSUndefined)); - print_number_nan("top", computedEdgeValue(node->style.position, CSSEdgeTop, CSSUndefined)); - print_number_nan("bottom", - computedEdgeValue(node->style.position, CSSEdgeBottom, CSSUndefined)); + printNumberIfNotUndefined("left", + computedEdgeValue(node->style.position, CSSEdgeLeft, CSSUndefined)); + printNumberIfNotUndefined("right", + computedEdgeValue(node->style.position, CSSEdgeRight, CSSUndefined)); + printNumberIfNotUndefined("top", + computedEdgeValue(node->style.position, CSSEdgeTop, CSSUndefined)); + printNumberIfNotUndefined("bottom", + computedEdgeValue(node->style.position, CSSEdgeBottom, CSSUndefined)); } const uint32_t childCount = CSSNodeListCount(node->children); if (options & CSSPrintOptionsChildren && childCount > 0) { printf("children: [\n"); for (uint32_t i = 0; i < childCount; i++) { - print_css_node_rec(CSSNodeGetChild(node, i), options, level + 1); + _CSSNodePrint(CSSNodeGetChild(node, i), options, level + 1); } indent(level); printf("]},\n"); @@ -518,7 +526,7 @@ print_css_node_rec(const CSSNodeRef node, const CSSPrintOptions options, const u } void CSSNodePrint(const CSSNodeRef node, const CSSPrintOptions options) { - print_css_node_rec(node, options, 0); + _CSSNodePrint(node, options, 0); } static const CSSEdge leading[4] = { @@ -555,8 +563,7 @@ static bool isColumnDirection(const CSSFlexDirection flexDirection) { } static float getLeadingMargin(const CSSNodeRef node, const CSSFlexDirection axis) { - if (isRowDirection(axis) && - !CSSValueIsUndefined(node->style.margin[CSSEdgeStart])) { + if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.margin[CSSEdgeStart])) { return node->style.margin[CSSEdgeStart]; } @@ -564,8 +571,7 @@ static float getLeadingMargin(const CSSNodeRef node, const CSSFlexDirection axis } static float getTrailingMargin(const CSSNodeRef node, const CSSFlexDirection axis) { - if (isRowDirection(axis) && - !CSSValueIsUndefined(node->style.margin[CSSEdgeEnd])) { + if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.margin[CSSEdgeEnd])) { return node->style.margin[CSSEdgeEnd]; } @@ -573,8 +579,7 @@ static float getTrailingMargin(const CSSNodeRef node, const CSSFlexDirection axi } static float getLeadingPadding(const CSSNodeRef node, const CSSFlexDirection axis) { - if (isRowDirection(axis) && - !CSSValueIsUndefined(node->style.padding[CSSEdgeStart]) && + if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.padding[CSSEdgeStart]) && node->style.padding[CSSEdgeStart] >= 0) { return node->style.padding[CSSEdgeStart]; } @@ -587,8 +592,7 @@ static float getLeadingPadding(const CSSNodeRef node, const CSSFlexDirection axi } static float getTrailingPadding(const CSSNodeRef node, const CSSFlexDirection axis) { - if (isRowDirection(axis) && - !CSSValueIsUndefined(node->style.padding[CSSEdgeEnd]) && + if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.padding[CSSEdgeEnd]) && node->style.padding[CSSEdgeEnd] >= 0) { return node->style.padding[CSSEdgeEnd]; } @@ -601,8 +605,7 @@ static float getTrailingPadding(const CSSNodeRef node, const CSSFlexDirection ax } static float getLeadingBorder(const CSSNodeRef node, const CSSFlexDirection axis) { - if (isRowDirection(axis) && - !CSSValueIsUndefined(node->style.border[CSSEdgeStart]) && + if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.border[CSSEdgeStart]) && node->style.border[CSSEdgeStart] >= 0) { return node->style.border[CSSEdgeStart]; } @@ -615,8 +618,7 @@ static float getLeadingBorder(const CSSNodeRef node, const CSSFlexDirection axis } static float getTrailingBorder(const CSSNodeRef node, const CSSFlexDirection axis) { - if (isRowDirection(axis) && - !CSSValueIsUndefined(node->style.border[CSSEdgeEnd]) && + if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.border[CSSEdgeEnd]) && node->style.border[CSSEdgeEnd] >= 0) { return node->style.border[CSSEdgeEnd]; } @@ -1941,60 +1943,59 @@ static const char *getModeName(const CSSMeasureMode mode, const bool performLayo static bool canUseCachedMeasurement(const bool isTextNode, const float availableWidth, const float availableHeight, - const float margin_row, - const float margin_column, + const float marginRow, + const float marginColumn, const CSSMeasureMode widthMeasureMode, const CSSMeasureMode heightMeasureMode, - CSSCachedMeasurement cached_layout) { - const bool is_height_same = (cached_layout.heightMeasureMode == CSSMeasureModeUndefined && - heightMeasureMode == CSSMeasureModeUndefined) || - (cached_layout.heightMeasureMode == heightMeasureMode && - eq(cached_layout.availableHeight, availableHeight)); + CSSCachedMeasurement cachedLayout) { + const bool isHeightSame = (cachedLayout.heightMeasureMode == CSSMeasureModeUndefined && + heightMeasureMode == CSSMeasureModeUndefined) || + (cachedLayout.heightMeasureMode == heightMeasureMode && + eq(cachedLayout.availableHeight, availableHeight)); - const bool is_width_same = (cached_layout.widthMeasureMode == CSSMeasureModeUndefined && - widthMeasureMode == CSSMeasureModeUndefined) || - (cached_layout.widthMeasureMode == widthMeasureMode && - eq(cached_layout.availableWidth, availableWidth)); + const bool isWidthSame = (cachedLayout.widthMeasureMode == CSSMeasureModeUndefined && + widthMeasureMode == CSSMeasureModeUndefined) || + (cachedLayout.widthMeasureMode == widthMeasureMode && + eq(cachedLayout.availableWidth, availableWidth)); - if (is_height_same && is_width_same) { + if (isHeightSame && isWidthSame) { return true; } - const bool is_height_valid = - (cached_layout.heightMeasureMode == CSSMeasureModeUndefined && - heightMeasureMode == CSSMeasureModeAtMost && - cached_layout.computedHeight <= (availableHeight - margin_column)) || - (heightMeasureMode == CSSMeasureModeExactly && - eq(cached_layout.computedHeight, availableHeight - margin_column)); + const bool isHeightValid = (cachedLayout.heightMeasureMode == CSSMeasureModeUndefined && + heightMeasureMode == CSSMeasureModeAtMost && + cachedLayout.computedHeight <= (availableHeight - marginColumn)) || + (heightMeasureMode == CSSMeasureModeExactly && + eq(cachedLayout.computedHeight, availableHeight - marginColumn)); - if (is_width_same && is_height_valid) { + if (isWidthSame && isHeightValid) { return true; } - const bool is_width_valid = (cached_layout.widthMeasureMode == CSSMeasureModeUndefined && - widthMeasureMode == CSSMeasureModeAtMost && - cached_layout.computedWidth <= (availableWidth - margin_row)) || - (widthMeasureMode == CSSMeasureModeExactly && - eq(cached_layout.computedWidth, availableWidth - margin_row)); + const bool isWidthValid = (cachedLayout.widthMeasureMode == CSSMeasureModeUndefined && + widthMeasureMode == CSSMeasureModeAtMost && + cachedLayout.computedWidth <= (availableWidth - marginRow)) || + (widthMeasureMode == CSSMeasureModeExactly && + eq(cachedLayout.computedWidth, availableWidth - marginRow)); - if (is_height_same && is_width_valid) { + if (isHeightSame && isWidthValid) { return true; } - if (is_height_valid && is_width_valid) { + if (isHeightValid && isWidthValid) { return true; } // We know this to be text so we can apply some more specialized heuristics. if (isTextNode) { - if (is_width_same) { + if (isWidthSame) { if (heightMeasureMode == CSSMeasureModeUndefined) { // Width is the same and height is not restricted. Re-use cahced value. return true; } if (heightMeasureMode == CSSMeasureModeAtMost && - cached_layout.computedHeight < (availableHeight - margin_column)) { + cachedLayout.computedHeight < (availableHeight - marginColumn)) { // Width is the same and height restriction is greater than the cached // height. Re-use cached // value. @@ -2004,14 +2005,14 @@ static bool canUseCachedMeasurement(const bool isTextNode, // Width is the same but height restriction imposes smaller height than // previously measured. // Update the cached value to respect the new height restriction. - cached_layout.computedHeight = availableHeight - margin_column; + cachedLayout.computedHeight = availableHeight - marginColumn; return true; } - if (cached_layout.widthMeasureMode == CSSMeasureModeUndefined) { + if (cachedLayout.widthMeasureMode == CSSMeasureModeUndefined) { if (widthMeasureMode == CSSMeasureModeUndefined || (widthMeasureMode == CSSMeasureModeAtMost && - cached_layout.computedWidth <= (availableWidth - margin_row))) { + cachedLayout.computedWidth <= (availableWidth - marginRow))) { // Previsouly this text was measured with no width restriction, if width // is now restricted // but to a larger value than the previsouly measured width we can @@ -2052,8 +2053,8 @@ bool layoutNodeInternal(const CSSNodeRef node, if (needToVisitNode) { // Invalidate the cached results. layout->nextCachedMeasurementsIndex = 0; - layout->cached_layout.widthMeasureMode = (CSSMeasureMode) -1; - layout->cached_layout.heightMeasureMode = (CSSMeasureMode) -1; + layout->cachedLayout.widthMeasureMode = (CSSMeasureMode) -1; + layout->cachedLayout.heightMeasureMode = (CSSMeasureMode) -1; } CSSCachedMeasurement *cachedResults = NULL; @@ -2082,8 +2083,8 @@ bool layoutNodeInternal(const CSSNodeRef node, marginAxisColumn, widthMeasureMode, heightMeasureMode, - layout->cached_layout)) { - cachedResults = &layout->cached_layout; + layout->cachedLayout)) { + cachedResults = &layout->cachedLayout; } else { // Try to use the measurement cache. for (uint32_t i = 0; i < layout->nextCachedMeasurementsIndex; i++) { @@ -2101,11 +2102,11 @@ bool layoutNodeInternal(const CSSNodeRef node, } } } else if (performLayout) { - if (eq(layout->cached_layout.availableWidth, availableWidth) && - eq(layout->cached_layout.availableHeight, availableHeight) && - layout->cached_layout.widthMeasureMode == widthMeasureMode && - layout->cached_layout.heightMeasureMode == heightMeasureMode) { - cachedResults = &layout->cached_layout; + if (eq(layout->cachedLayout.availableWidth, availableWidth) && + eq(layout->cachedLayout.availableHeight, availableHeight) && + layout->cachedLayout.widthMeasureMode == widthMeasureMode && + layout->cachedLayout.heightMeasureMode == heightMeasureMode) { + cachedResults = &layout->cachedLayout; } } else { for (uint32_t i = 0; i < layout->nextCachedMeasurementsIndex; i++) { @@ -2185,7 +2186,7 @@ bool layoutNodeInternal(const CSSNodeRef node, CSSCachedMeasurement *newCacheEntry; if (performLayout) { // Use the single layout cache entry. - newCacheEntry = &layout->cached_layout; + newCacheEntry = &layout->cachedLayout; } else { // Allocate a new measurement cache entry. newCacheEntry = &layout->cachedMeasurements[layout->nextCachedMeasurementsIndex];