From 85ac5fc35449a2e8c89114f19e644d6833705f25 Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Sat, 3 Dec 2016 04:40:18 -0800 Subject: [PATCH] Rename C api Differential Revision: D4259190 fbshipit-source-id: 26c8b356ca464d4304f5f9dc4192bff10cea2dc9 --- .../UIExplorerUnitTests/RCTShadowViewTests.m | 100 +- Libraries/Text/RCTShadowText.m | 34 +- Libraries/Text/RCTTextManager.m | 2 +- React/Base/RCTConvert.h | 2 +- React/CSSLayout/CSSLayout.h | 183 -- React/CSSLayout/{CSSEnums.h => YGEnums.h} | 0 .../CSSMacros.h => React/CSSLayout/YGMacros.h | 22 +- React/CSSLayout/YGNodeList.c | 104 + React/CSSLayout/YGNodeList.h | 33 + React/CSSLayout/{CSSLayout.c => Yoga.c} | 1761 +++++++++-------- React/CSSLayout/Yoga.h | 181 ++ React/Modules/RCTUIManager.m | 2 +- React/React.xcodeproj/project.pbxproj | 104 +- React/Views/RCTRootShadowView.m | 12 +- React/Views/RCTShadowView.h | 14 +- React/Views/RCTShadowView.m | 196 +- .../java/com/facebook/csslayout/CSSNode.java | 248 +-- .../jni/first-party/csslayoutjni/Android.mk | 2 +- .../first-party/csslayoutjni/jni/CSSJNI.cpp | 340 ---- .../first-party/csslayoutjni/jni/YGJNI.cpp | 333 ++++ ReactCommon/CSSLayout/Android.mk | 4 +- ReactCommon/CSSLayout/CSSLayout/CSSLayout.h | 183 -- ReactCommon/CSSLayout/CSSLayout/CSSNodeList.c | 104 - ReactCommon/CSSLayout/CSSLayout/CSSNodeList.h | 33 - .../CSSLayout/{CSSEnums.h => YGEnums.h} | 0 .../CSSLayout/CSSLayout/YGMacros.h | 22 +- ReactCommon/CSSLayout/CSSLayout/YGNodeList.c | 104 + ReactCommon/CSSLayout/CSSLayout/YGNodeList.h | 33 + .../CSSLayout/{CSSLayout.c => Yoga.c} | 1761 +++++++++-------- ReactCommon/CSSLayout/CSSLayout/Yoga.h | 181 ++ 30 files changed, 3139 insertions(+), 2959 deletions(-) delete mode 100644 React/CSSLayout/CSSLayout.h rename React/CSSLayout/{CSSEnums.h => YGEnums.h} (100%) rename ReactCommon/CSSLayout/CSSLayout/CSSMacros.h => React/CSSLayout/YGMacros.h (60%) create mode 100644 React/CSSLayout/YGNodeList.c create mode 100644 React/CSSLayout/YGNodeList.h rename React/CSSLayout/{CSSLayout.c => Yoga.c} (50%) create mode 100644 React/CSSLayout/Yoga.h delete mode 100644 ReactAndroid/src/main/jni/first-party/csslayoutjni/jni/CSSJNI.cpp create mode 100644 ReactAndroid/src/main/jni/first-party/csslayoutjni/jni/YGJNI.cpp delete mode 100644 ReactCommon/CSSLayout/CSSLayout/CSSLayout.h delete mode 100644 ReactCommon/CSSLayout/CSSLayout/CSSNodeList.c delete mode 100644 ReactCommon/CSSLayout/CSSLayout/CSSNodeList.h rename ReactCommon/CSSLayout/CSSLayout/{CSSEnums.h => YGEnums.h} (100%) rename React/CSSLayout/CSSMacros.h => ReactCommon/CSSLayout/CSSLayout/YGMacros.h (60%) create mode 100644 ReactCommon/CSSLayout/CSSLayout/YGNodeList.c create mode 100644 ReactCommon/CSSLayout/CSSLayout/YGNodeList.h rename ReactCommon/CSSLayout/CSSLayout/{CSSLayout.c => Yoga.c} (50%) create mode 100644 ReactCommon/CSSLayout/CSSLayout/Yoga.h diff --git a/Examples/UIExplorer/UIExplorerUnitTests/RCTShadowViewTests.m b/Examples/UIExplorer/UIExplorerUnitTests/RCTShadowViewTests.m index 91a1196c6..ab338eda5 100644 --- a/Examples/UIExplorer/UIExplorerUnitTests/RCTShadowViewTests.m +++ b/Examples/UIExplorer/UIExplorerUnitTests/RCTShadowViewTests.m @@ -28,10 +28,10 @@ { [super setUp]; - self.parentView = [self _shadowViewWithConfig:^(CSSNodeRef node) { - CSSNodeStyleSetFlexDirection(node, YGFlexDirectionColumn); - CSSNodeStyleSetWidth(node, 440); - CSSNodeStyleSetHeight(node, 440); + self.parentView = [self _shadowViewWithConfig:^(YGNodeRef node) { + YGNodeStyleSetFlexDirection(node, YGFlexDirectionColumn); + YGNodeStyleSetWidth(node, 440); + YGNodeStyleSetHeight(node, 440); }]; self.parentView.reactTag = @1; // must be valid rootView tag } @@ -50,43 +50,43 @@ // - (void)testApplyingLayoutRecursivelyToShadowView { - RCTShadowView *leftView = [self _shadowViewWithConfig:^(CSSNodeRef node) { - CSSNodeStyleSetFlex(node, 1); + RCTShadowView *leftView = [self _shadowViewWithConfig:^(YGNodeRef node) { + YGNodeStyleSetFlex(node, 1); }]; - RCTShadowView *centerView = [self _shadowViewWithConfig:^(CSSNodeRef node) { - CSSNodeStyleSetFlex(node, 2); - CSSNodeStyleSetMargin(node, YGEdgeLeft, 10); - CSSNodeStyleSetMargin(node, YGEdgeRight, 10); + RCTShadowView *centerView = [self _shadowViewWithConfig:^(YGNodeRef node) { + YGNodeStyleSetFlex(node, 2); + YGNodeStyleSetMargin(node, YGEdgeLeft, 10); + YGNodeStyleSetMargin(node, YGEdgeRight, 10); }]; - RCTShadowView *rightView = [self _shadowViewWithConfig:^(CSSNodeRef node) { - CSSNodeStyleSetFlex(node, 1); + RCTShadowView *rightView = [self _shadowViewWithConfig:^(YGNodeRef node) { + YGNodeStyleSetFlex(node, 1); }]; - RCTShadowView *mainView = [self _shadowViewWithConfig:^(CSSNodeRef node) { - CSSNodeStyleSetFlexDirection(node, YGFlexDirectionRow); - CSSNodeStyleSetFlex(node, 2); - CSSNodeStyleSetMargin(node, YGEdgeTop, 10); - CSSNodeStyleSetMargin(node, YGEdgeBottom, 10); + RCTShadowView *mainView = [self _shadowViewWithConfig:^(YGNodeRef node) { + YGNodeStyleSetFlexDirection(node, YGFlexDirectionRow); + YGNodeStyleSetFlex(node, 2); + YGNodeStyleSetMargin(node, YGEdgeTop, 10); + YGNodeStyleSetMargin(node, YGEdgeBottom, 10); }]; [mainView insertReactSubview:leftView atIndex:0]; [mainView insertReactSubview:centerView atIndex:1]; [mainView insertReactSubview:rightView atIndex:2]; - RCTShadowView *headerView = [self _shadowViewWithConfig:^(CSSNodeRef node) { - CSSNodeStyleSetFlex(node, 1); + RCTShadowView *headerView = [self _shadowViewWithConfig:^(YGNodeRef node) { + YGNodeStyleSetFlex(node, 1); }]; - RCTShadowView *footerView = [self _shadowViewWithConfig:^(CSSNodeRef node) { - CSSNodeStyleSetFlex(node, 1); + RCTShadowView *footerView = [self _shadowViewWithConfig:^(YGNodeRef node) { + YGNodeStyleSetFlex(node, 1); }]; - CSSNodeStyleSetPadding(self.parentView.cssNode, YGEdgeLeft, 10); - CSSNodeStyleSetPadding(self.parentView.cssNode, YGEdgeTop, 10); - CSSNodeStyleSetPadding(self.parentView.cssNode, YGEdgeRight, 10); - CSSNodeStyleSetPadding(self.parentView.cssNode, YGEdgeBottom, 10); + YGNodeStyleSetPadding(self.parentView.cssNode, YGEdgeLeft, 10); + YGNodeStyleSetPadding(self.parentView.cssNode, YGEdgeTop, 10); + YGNodeStyleSetPadding(self.parentView.cssNode, YGEdgeRight, 10); + YGNodeStyleSetPadding(self.parentView.cssNode, YGEdgeBottom, 10); [self.parentView insertReactSubview:headerView atIndex:0]; [self.parentView insertReactSubview:mainView atIndex:1]; @@ -108,18 +108,18 @@ - (void)testAncestorCheck { - RCTShadowView *centerView = [self _shadowViewWithConfig:^(CSSNodeRef node) { - CSSNodeStyleSetFlex(node, 1); + RCTShadowView *centerView = [self _shadowViewWithConfig:^(YGNodeRef node) { + YGNodeStyleSetFlex(node, 1); }]; - RCTShadowView *mainView = [self _shadowViewWithConfig:^(CSSNodeRef node) { - CSSNodeStyleSetFlex(node, 1); + RCTShadowView *mainView = [self _shadowViewWithConfig:^(YGNodeRef node) { + YGNodeStyleSetFlex(node, 1); }]; [mainView insertReactSubview:centerView atIndex:0]; - RCTShadowView *footerView = [self _shadowViewWithConfig:^(CSSNodeRef node) { - CSSNodeStyleSetFlex(node, 1); + RCTShadowView *footerView = [self _shadowViewWithConfig:^(YGNodeRef node) { + YGNodeStyleSetFlex(node, 1); }]; [self.parentView insertReactSubview:mainView atIndex:0]; @@ -131,10 +131,10 @@ - (void)testAssignsSuggestedWidthDimension { - [self _withShadowViewWithStyle:^(CSSNodeRef node) { - CSSNodeStyleSetPosition(node, YGEdgeLeft, 0); - CSSNodeStyleSetPosition(node, YGEdgeTop, 0); - CSSNodeStyleSetHeight(node, 10); + [self _withShadowViewWithStyle:^(YGNodeRef node) { + YGNodeStyleSetPosition(node, YGEdgeLeft, 0); + YGNodeStyleSetPosition(node, YGEdgeTop, 0); + YGNodeStyleSetHeight(node, 10); } assertRelativeLayout:CGRectMake(0, 0, 3, 10) withIntrinsicContentSize:CGSizeMake(3, UIViewNoIntrinsicMetric)]; @@ -142,10 +142,10 @@ - (void)testAssignsSuggestedHeightDimension { - [self _withShadowViewWithStyle:^(CSSNodeRef node) { - CSSNodeStyleSetPosition(node, YGEdgeLeft, 0); - CSSNodeStyleSetPosition(node, YGEdgeTop, 0); - CSSNodeStyleSetWidth(node, 10); + [self _withShadowViewWithStyle:^(YGNodeRef node) { + YGNodeStyleSetPosition(node, YGEdgeLeft, 0); + YGNodeStyleSetPosition(node, YGEdgeTop, 0); + YGNodeStyleSetWidth(node, 10); } assertRelativeLayout:CGRectMake(0, 0, 10, 4) withIntrinsicContentSize:CGSizeMake(UIViewNoIntrinsicMetric, 4)]; @@ -153,11 +153,11 @@ - (void)testDoesNotOverrideDimensionStyleWithSuggestedDimensions { - [self _withShadowViewWithStyle:^(CSSNodeRef node) { - CSSNodeStyleSetPosition(node, YGEdgeLeft, 0); - CSSNodeStyleSetPosition(node, YGEdgeTop, 0); - CSSNodeStyleSetWidth(node, 10); - CSSNodeStyleSetHeight(node, 10); + [self _withShadowViewWithStyle:^(YGNodeRef node) { + YGNodeStyleSetPosition(node, YGEdgeLeft, 0); + YGNodeStyleSetPosition(node, YGEdgeTop, 0); + YGNodeStyleSetWidth(node, 10); + YGNodeStyleSetHeight(node, 10); } assertRelativeLayout:CGRectMake(0, 0, 10, 10) withIntrinsicContentSize:CGSizeMake(3, 4)]; @@ -165,16 +165,16 @@ - (void)testDoesNotAssignSuggestedDimensionsWhenStyledWithFlexAttribute { - float parentWidth = CSSNodeStyleGetWidth(self.parentView.cssNode); - float parentHeight = CSSNodeStyleGetHeight(self.parentView.cssNode); - [self _withShadowViewWithStyle:^(CSSNodeRef node) { - CSSNodeStyleSetFlex(node, 1); + float parentWidth = YGNodeStyleGetWidth(self.parentView.cssNode); + float parentHeight = YGNodeStyleGetHeight(self.parentView.cssNode); + [self _withShadowViewWithStyle:^(YGNodeRef node) { + YGNodeStyleSetFlex(node, 1); } assertRelativeLayout:CGRectMake(0, 0, parentWidth, parentHeight) withIntrinsicContentSize:CGSizeMake(3, 4)]; } -- (void)_withShadowViewWithStyle:(void(^)(CSSNodeRef node))configBlock +- (void)_withShadowViewWithStyle:(void(^)(YGNodeRef node))configBlock assertRelativeLayout:(CGRect)expectedRect withIntrinsicContentSize:(CGSize)contentSize { @@ -189,7 +189,7 @@ NSStringFromCGRect(actualRect)); } -- (RCTRootShadowView *)_shadowViewWithConfig:(void(^)(CSSNodeRef node))configBlock +- (RCTRootShadowView *)_shadowViewWithConfig:(void(^)(YGNodeRef node))configBlock { RCTRootShadowView *shadowView = [RCTRootShadowView new]; configBlock(shadowView.cssNode); diff --git a/Libraries/Text/RCTShadowText.m b/Libraries/Text/RCTShadowText.m index dfa730134..2a2dc518c 100644 --- a/Libraries/Text/RCTShadowText.m +++ b/Libraries/Text/RCTShadowText.m @@ -39,16 +39,16 @@ CGFloat const RCTTextAutoSizeGranularity = 0.001f; CGFloat _effectiveLetterSpacing; } -static CSSSize RCTMeasure(CSSNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode) +static YGSize RCTMeasure(YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode) { - RCTShadowText *shadowText = (__bridge RCTShadowText *)CSSNodeGetContext(node); + RCTShadowText *shadowText = (__bridge RCTShadowText *)YGNodeGetContext(node); NSTextStorage *textStorage = [shadowText buildTextStorageForWidth:width widthMode:widthMode]; [shadowText calculateTextFrame:textStorage]; NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject; NSTextContainer *textContainer = layoutManager.textContainers.firstObject; CGSize computedSize = [layoutManager usedRectForTextContainer:textContainer].size; - CSSSize result; + YGSize result; result.width = RCTCeilPixelValue(computedSize.width); if (shadowText->_effectiveLetterSpacing < 0) { result.width -= shadowText->_effectiveLetterSpacing; @@ -68,7 +68,7 @@ static CSSSize RCTMeasure(CSSNodeRef node, float width, YGMeasureMode widthMode, _cachedTextStorageWidth = -1; _cachedTextStorageWidthMode = -1; _fontSizeMultiplier = 1.0; - CSSNodeSetMeasureFunc(self.cssNode, RCTMeasure); + YGNodeSetMeasureFunc(self.cssNode, RCTMeasure); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentSizeMultiplierDidChange:) name:RCTUIManagerWillUpdateViewsDueToContentSizeMultiplierChangeNotification @@ -95,7 +95,7 @@ static CSSSize RCTMeasure(CSSNodeRef node, float width, YGMeasureMode widthMode, - (void)contentSizeMultiplierDidChange:(NSNotification *)note { - CSSNodeMarkDirty(self.cssNode); + YGNodeMarkDirty(self.cssNode); [self dirtyText]; } @@ -140,7 +140,7 @@ static CSSSize RCTMeasure(CSSNodeRef node, float width, YGMeasureMode widthMode, return parentProperties; } -- (void)applyLayoutNode:(CSSNodeRef)node +- (void)applyLayoutNode:(YGNodeRef)node viewsWithNewFrame:(NSMutableSet *)viewsWithNewFrame absolutePosition:(CGPoint)absolutePosition { @@ -148,7 +148,7 @@ static CSSSize RCTMeasure(CSSNodeRef node, float width, YGMeasureMode widthMode, [self dirtyPropagation]; } -- (void)applyLayoutToChildren:(CSSNodeRef)node +- (void)applyLayoutToChildren:(YGNodeRef)node viewsWithNewFrame:(NSMutableSet *)viewsWithNewFrame absolutePosition:(CGPoint)absolutePosition { @@ -160,10 +160,10 @@ static CSSSize RCTMeasure(CSSNodeRef node, float width, YGMeasureMode widthMode, NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL]; [layoutManager.textStorage enumerateAttribute:RCTShadowViewAttributeName inRange:characterRange options:0 usingBlock:^(RCTShadowView *child, NSRange range, BOOL *_) { if (child) { - CSSNodeRef childNode = child.cssNode; - float width = CSSNodeStyleGetWidth(childNode); - float height = CSSNodeStyleGetHeight(childNode); - if (CSSValueIsUndefined(width) || CSSValueIsUndefined(height)) { + YGNodeRef childNode = child.cssNode; + float width = YGNodeStyleGetWidth(childNode); + float height = YGNodeStyleGetHeight(childNode); + if (YGValueIsUndefined(width) || YGValueIsUndefined(height)) { RCTLogError(@"Views nested within a must have a width and height"); } UIFont *font = [textStorage attribute:NSFontAttributeName atIndex:range.location effectiveRange:nil]; @@ -307,9 +307,9 @@ static CSSSize RCTMeasure(CSSNodeRef node, float width, YGMeasureMode widthMode, [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:shadowRawText.text ?: @""]]; [child setTextComputed]; } else { - float width = CSSNodeStyleGetWidth(child.cssNode); - float height = CSSNodeStyleGetHeight(child.cssNode); - if (CSSValueIsUndefined(width) || CSSValueIsUndefined(height)) { + float width = YGNodeStyleGetWidth(child.cssNode); + float height = YGNodeStyleGetHeight(child.cssNode); + if (YGValueIsUndefined(width) || YGValueIsUndefined(height)) { RCTLogError(@"Views nested within a must have a width and height"); } NSTextAttachment *attachment = [NSTextAttachment new]; @@ -348,7 +348,7 @@ static CSSSize RCTMeasure(CSSNodeRef node, float width, YGMeasureMode widthMode, // create a non-mutable attributedString for use by the Text system which avoids copies down the line _cachedAttributedString = [[NSAttributedString alloc] initWithAttributedString:attributedString]; - CSSNodeMarkDirty(self.cssNode); + YGNodeMarkDirty(self.cssNode); return _cachedAttributedString; } @@ -403,10 +403,10 @@ static CSSSize RCTMeasure(CSSNodeRef node, float width, YGMeasureMode widthMode, // We will climb up to the first node which style has been setted as non-inherit if (newTextAlign == NSTextAlignmentRight || newTextAlign == NSTextAlignmentLeft) { RCTShadowView *view = self; - while (view != nil && CSSNodeStyleGetDirection(view.cssNode) == YGDirectionInherit) { + while (view != nil && YGNodeStyleGetDirection(view.cssNode) == YGDirectionInherit) { view = [view reactSuperview]; } - if (view != nil && CSSNodeStyleGetDirection(view.cssNode) == YGDirectionRTL) { + if (view != nil && YGNodeStyleGetDirection(view.cssNode) == YGDirectionRTL) { if (newTextAlign == NSTextAlignmentRight) { newTextAlign = NSTextAlignmentLeft; } else if (newTextAlign == NSTextAlignmentLeft) { diff --git a/Libraries/Text/RCTTextManager.m b/Libraries/Text/RCTTextManager.m index d29fe61ba..526ef281e 100644 --- a/Libraries/Text/RCTTextManager.m +++ b/Libraries/Text/RCTTextManager.m @@ -9,7 +9,7 @@ #import "RCTTextManager.h" -#import +#import #import #import #import diff --git a/React/Base/RCTConvert.h b/React/Base/RCTConvert.h index be85ec443..4197bb9bd 100644 --- a/React/Base/RCTConvert.h +++ b/React/Base/RCTConvert.h @@ -10,7 +10,7 @@ #import #import -#import +#import #import #import #import diff --git a/React/CSSLayout/CSSLayout.h b/React/CSSLayout/CSSLayout.h deleted file mode 100644 index 62f5b16cc..000000000 --- a/React/CSSLayout/CSSLayout.h +++ /dev/null @@ -1,183 +0,0 @@ -/** - * 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. - */ - -#pragma once - -#include -#include -#include -#include -#include -#include - -#ifndef __cplusplus -#include -#endif - -// Not defined in MSVC++ -#ifndef NAN -static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff}; -#define NAN (*(const float *) __nan) -#endif - -#define YGUndefined NAN - -#include "CSSEnums.h" -#include "CSSMacros.h" - -CSS_EXTERN_C_BEGIN - -typedef struct CSSSize { - float width; - float height; -} CSSSize; - -typedef struct CSSNode *CSSNodeRef; -typedef CSSSize (*CSSMeasureFunc)(CSSNodeRef node, - float width, - YGMeasureMode widthMode, - float height, - YGMeasureMode heightMode); -typedef void (*CSSPrintFunc)(CSSNodeRef node); -typedef int (*CSSLogger)(YGLogLevel level, const char *format, va_list args); - -typedef void *(*CSSMalloc)(size_t size); -typedef void *(*CSSCalloc)(size_t count, size_t size); -typedef void *(*CSSRealloc)(void *ptr, size_t size); -typedef void (*CSSFree)(void *ptr); - -// CSSNode -WIN_EXPORT CSSNodeRef CSSNodeNew(void); -WIN_EXPORT void CSSNodeInit(const CSSNodeRef node); -WIN_EXPORT void CSSNodeFree(const CSSNodeRef node); -WIN_EXPORT void CSSNodeFreeRecursive(const CSSNodeRef node); -WIN_EXPORT void CSSNodeReset(const CSSNodeRef node); -WIN_EXPORT int32_t CSSNodeGetInstanceCount(void); - -WIN_EXPORT void CSSNodeInsertChild(const CSSNodeRef node, - const CSSNodeRef child, - const uint32_t index); -WIN_EXPORT void CSSNodeRemoveChild(const CSSNodeRef node, const CSSNodeRef child); -WIN_EXPORT CSSNodeRef CSSNodeGetChild(const CSSNodeRef node, const uint32_t index); -WIN_EXPORT uint32_t CSSNodeChildCount(const CSSNodeRef node); - -WIN_EXPORT void CSSNodeCalculateLayout(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGDirection parentDirection); - -// Mark a node as dirty. Only valid for nodes with a custom measure function -// set. -// CSSLayout knows when to mark all other nodes as dirty but because nodes with -// measure functions -// depends on information not known to CSSLayout they must perform this dirty -// marking manually. -WIN_EXPORT void CSSNodeMarkDirty(const CSSNodeRef node); -WIN_EXPORT bool CSSNodeIsDirty(const CSSNodeRef node); - -WIN_EXPORT void CSSNodePrint(const CSSNodeRef node, const YGPrintOptions options); - -WIN_EXPORT bool CSSValueIsUndefined(const float value); - -WIN_EXPORT bool CSSNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, - const float width, - const YGMeasureMode heightMode, - const float height, - const YGMeasureMode lastWidthMode, - const float lastWidth, - const YGMeasureMode lastHeightMode, - const float lastHeight, - const float lastComputedWidth, - const float lastComputedHeight, - const float marginRow, - const float marginColumn); - -WIN_EXPORT void CSSNodeCopyStyle(const CSSNodeRef dstNode, const CSSNodeRef srcNode); - -#define CSS_NODE_PROPERTY(type, name, paramName) \ - WIN_EXPORT void CSSNodeSet##name(const CSSNodeRef node, type paramName); \ - WIN_EXPORT type CSSNodeGet##name(const CSSNodeRef node); - -#define CSS_NODE_STYLE_PROPERTY(type, name, paramName) \ - WIN_EXPORT void CSSNodeStyleSet##name(const CSSNodeRef node, const type paramName); \ - WIN_EXPORT type CSSNodeStyleGet##name(const CSSNodeRef node); - -#define CSS_NODE_STYLE_EDGE_PROPERTY(type, name, paramName) \ - WIN_EXPORT void CSSNodeStyleSet##name(const CSSNodeRef node, \ - const YGEdge edge, \ - const type paramName); \ - WIN_EXPORT type CSSNodeStyleGet##name(const CSSNodeRef node, const YGEdge edge); - -#define CSS_NODE_LAYOUT_PROPERTY(type, name) \ - WIN_EXPORT type CSSNodeLayoutGet##name(const CSSNodeRef node); - -CSS_NODE_PROPERTY(void *, Context, context); -CSS_NODE_PROPERTY(CSSMeasureFunc, MeasureFunc, measureFunc); -CSS_NODE_PROPERTY(CSSPrintFunc, PrintFunc, printFunc); -CSS_NODE_PROPERTY(bool, HasNewLayout, hasNewLayout); - -CSS_NODE_STYLE_PROPERTY(YGDirection, Direction, direction); -CSS_NODE_STYLE_PROPERTY(YGFlexDirection, FlexDirection, flexDirection); -CSS_NODE_STYLE_PROPERTY(YGJustify, JustifyContent, justifyContent); -CSS_NODE_STYLE_PROPERTY(YGAlign, AlignContent, alignContent); -CSS_NODE_STYLE_PROPERTY(YGAlign, AlignItems, alignItems); -CSS_NODE_STYLE_PROPERTY(YGAlign, AlignSelf, alignSelf); -CSS_NODE_STYLE_PROPERTY(YGPositionType, PositionType, positionType); -CSS_NODE_STYLE_PROPERTY(YGWrap, FlexWrap, flexWrap); -CSS_NODE_STYLE_PROPERTY(YGOverflow, Overflow, overflow); - -WIN_EXPORT void CSSNodeStyleSetFlex(const CSSNodeRef node, const float flex); -CSS_NODE_STYLE_PROPERTY(float, FlexGrow, flexGrow); -CSS_NODE_STYLE_PROPERTY(float, FlexShrink, flexShrink); -CSS_NODE_STYLE_PROPERTY(float, FlexBasis, flexBasis); - -CSS_NODE_STYLE_EDGE_PROPERTY(float, Position, position); -CSS_NODE_STYLE_EDGE_PROPERTY(float, Margin, margin); -CSS_NODE_STYLE_EDGE_PROPERTY(float, Padding, padding); -CSS_NODE_STYLE_EDGE_PROPERTY(float, Border, border); - -CSS_NODE_STYLE_PROPERTY(float, Width, width); -CSS_NODE_STYLE_PROPERTY(float, Height, height); -CSS_NODE_STYLE_PROPERTY(float, MinWidth, minWidth); -CSS_NODE_STYLE_PROPERTY(float, MinHeight, minHeight); -CSS_NODE_STYLE_PROPERTY(float, MaxWidth, maxWidth); -CSS_NODE_STYLE_PROPERTY(float, MaxHeight, maxHeight); - -// Yoga specific properties, not compatible with flexbox specification -// Aspect ratio control the size of the undefined dimension of a node. -// - On a node with a set width/height aspect ratio control the size of the unset dimension -// - On a node with a set flex basis aspect ratio controls the size of the node in the cross axis if -// unset -// - On a node with a measure function aspect ratio works as though the measure function measures -// the flex basis -// - On a node with flex grow/shrink aspect ratio controls the size of the node in the cross axis if -// unset -// - Aspect ratio takes min/max dimensions into account -CSS_NODE_STYLE_PROPERTY(float, AspectRatio, aspectRatio); - -CSS_NODE_LAYOUT_PROPERTY(float, Left); -CSS_NODE_LAYOUT_PROPERTY(float, Top); -CSS_NODE_LAYOUT_PROPERTY(float, Right); -CSS_NODE_LAYOUT_PROPERTY(float, Bottom); -CSS_NODE_LAYOUT_PROPERTY(float, Width); -CSS_NODE_LAYOUT_PROPERTY(float, Height); -CSS_NODE_LAYOUT_PROPERTY(YGDirection, Direction); - -WIN_EXPORT void CSSLayoutSetLogger(CSSLogger logger); -WIN_EXPORT void CSSLog(YGLogLevel level, const char *message, ...); - -WIN_EXPORT void CSSLayoutSetExperimentalFeatureEnabled(YGExperimentalFeature feature, bool enabled); -WIN_EXPORT bool CSSLayoutIsExperimentalFeatureEnabled(YGExperimentalFeature feature); - -WIN_EXPORT void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc, - CSSCalloc cssCalloc, - CSSRealloc cssRealloc, - CSSFree cssFree); - -CSS_EXTERN_C_END diff --git a/React/CSSLayout/CSSEnums.h b/React/CSSLayout/YGEnums.h similarity index 100% rename from React/CSSLayout/CSSEnums.h rename to React/CSSLayout/YGEnums.h diff --git a/ReactCommon/CSSLayout/CSSLayout/CSSMacros.h b/React/CSSLayout/YGMacros.h similarity index 60% rename from ReactCommon/CSSLayout/CSSLayout/CSSMacros.h rename to React/CSSLayout/YGMacros.h index f54a5b87d..def8371ac 100644 --- a/ReactCommon/CSSLayout/CSSLayout/CSSMacros.h +++ b/React/CSSLayout/YGMacros.h @@ -10,11 +10,11 @@ #pragma once #ifdef __cplusplus -#define CSS_EXTERN_C_BEGIN extern "C" { -#define CSS_EXTERN_C_END } +#define YG_EXTERN_C_BEGIN extern "C" { +#define YG_EXTERN_C_END } #else -#define CSS_EXTERN_C_BEGIN -#define CSS_EXTERN_C_END +#define YG_EXTERN_C_BEGIN +#define YG_EXTERN_C_END #endif #ifdef _WINDLL @@ -28,15 +28,15 @@ #endif #if FB_ASSERTIONS_ENABLED -#define CSS_ABORT() abort() +#define YG_ABORT() abort() #else -#define CSS_ABORT() +#define YG_ABORT() #endif -#ifndef CSS_ASSERT -#define CSS_ASSERT(X, message) \ - if (!(X)) { \ - CSSLog(YGLogLevelError, "%s", message); \ - CSS_ABORT(); \ +#ifndef YG_ASSERT +#define YG_ASSERT(X, message) \ + if (!(X)) { \ + YGLog(YGLogLevelError, "%s", message); \ + YG_ABORT(); \ } #endif diff --git a/React/CSSLayout/YGNodeList.c b/React/CSSLayout/YGNodeList.c new file mode 100644 index 000000000..d82753e2a --- /dev/null +++ b/React/CSSLayout/YGNodeList.c @@ -0,0 +1,104 @@ +/** + * 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. + */ + +#include "YGNodeList.h" + +extern YGMalloc gYGMalloc; +extern YGRealloc gYGRealloc; +extern YGFree gYGFree; + +struct YGNodeList { + uint32_t capacity; + uint32_t count; + YGNodeRef *items; +}; + +YGNodeListRef YGNodeListNew(const uint32_t initialCapacity) { + const YGNodeListRef list = gYGMalloc(sizeof(struct YGNodeList)); + YG_ASSERT(list != NULL, "Could not allocate memory for list"); + + list->capacity = initialCapacity; + list->count = 0; + list->items = gYGMalloc(sizeof(YGNodeRef) * list->capacity); + YG_ASSERT(list->items != NULL, "Could not allocate memory for items"); + + return list; +} + +void YGNodeListFree(const YGNodeListRef list) { + if (list) { + gYGFree(list->items); + gYGFree(list); + } +} + +uint32_t YGNodeListCount(const YGNodeListRef list) { + if (list) { + return list->count; + } + return 0; +} + +void YGNodeListAdd(YGNodeListRef *listp, const YGNodeRef node) { + if (!*listp) { + *listp = YGNodeListNew(4); + } + YGNodeListInsert(listp, node, (*listp)->count); +} + +void YGNodeListInsert(YGNodeListRef *listp, const YGNodeRef node, const uint32_t index) { + if (!*listp) { + *listp = YGNodeListNew(4); + } + YGNodeListRef list = *listp; + + if (list->count == list->capacity) { + list->capacity *= 2; + list->items = gYGRealloc(list->items, sizeof(YGNodeRef) * list->capacity); + YG_ASSERT(list->items != NULL, "Could not extend allocation for items"); + } + + for (uint32_t i = list->count; i > index; i--) { + list->items[i] = list->items[i - 1]; + } + + list->count++; + list->items[index] = node; +} + +YGNodeRef YGNodeListRemove(const YGNodeListRef list, const uint32_t index) { + const YGNodeRef removed = list->items[index]; + list->items[index] = NULL; + + for (uint32_t i = index; i < list->count - 1; i++) { + list->items[i] = list->items[i + 1]; + list->items[i + 1] = NULL; + } + + list->count--; + return removed; +} + +YGNodeRef YGNodeListDelete(const YGNodeListRef list, const YGNodeRef node) { + for (uint32_t i = 0; i < list->count; i++) { + if (list->items[i] == node) { + return YGNodeListRemove(list, i); + } + } + + return NULL; +} + +YGNodeRef YGNodeListGet(const YGNodeListRef list, const uint32_t index) { + if (YGNodeListCount(list) > 0) { + return list->items[index]; + } + + return NULL; +} diff --git a/React/CSSLayout/YGNodeList.h b/React/CSSLayout/YGNodeList.h new file mode 100644 index 000000000..41e272ab3 --- /dev/null +++ b/React/CSSLayout/YGNodeList.h @@ -0,0 +1,33 @@ +/** + * 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. + */ + +#pragma once + +#include +#include +#include +#include + +#include "YGMacros.h" +#include "Yoga.h" + +YG_EXTERN_C_BEGIN + +typedef struct YGNodeList *YGNodeListRef; + +YGNodeListRef YGNodeListNew(const uint32_t initialCapacity); +void YGNodeListFree(const YGNodeListRef list); +uint32_t YGNodeListCount(const YGNodeListRef list); +void YGNodeListAdd(YGNodeListRef *listp, const YGNodeRef node); +void YGNodeListInsert(YGNodeListRef *listp, const YGNodeRef node, const uint32_t index); +YGNodeRef YGNodeListRemove(const YGNodeListRef list, const uint32_t index); +YGNodeRef YGNodeListDelete(const YGNodeListRef list, const YGNodeRef node); +YGNodeRef YGNodeListGet(const YGNodeListRef list, const uint32_t index); + +YG_EXTERN_C_END diff --git a/React/CSSLayout/CSSLayout.c b/React/CSSLayout/Yoga.c similarity index 50% rename from React/CSSLayout/CSSLayout.c rename to React/CSSLayout/Yoga.c index c41194a1e..70afece2f 100644 --- a/React/CSSLayout/CSSLayout.c +++ b/React/CSSLayout/Yoga.c @@ -9,8 +9,8 @@ #include -#include "CSSLayout.h" -#include "CSSNodeList.h" +#include "YGNodeList.h" +#include "Yoga.h" #ifdef _MSC_VER #include @@ -30,7 +30,7 @@ __forceinline const float fmaxf(const float a, const float b) { #endif #endif -typedef struct CSSCachedMeasurement { +typedef struct YGCachedMeasurement { float availableWidth; float availableHeight; YGMeasureMode widthMeasureMode; @@ -38,13 +38,13 @@ typedef struct CSSCachedMeasurement { float computedWidth; float computedHeight; -} CSSCachedMeasurement; +} YGCachedMeasurement; // This value was chosen based on empiracle data. Even the most complicated // layouts should not require more than 16 entries to fit within the cache. -enum { CSS_MAX_CACHED_RESULT_COUNT = 16 }; +enum { YG_MAX_CACHED_RESULT_COUNT = 16 }; -typedef struct CSSLayout { +typedef struct YGLayout { float position[4]; float dimensions[2]; YGDirection direction; @@ -58,13 +58,13 @@ typedef struct CSSLayout { YGDirection lastParentDirection; uint32_t nextCachedMeasurementsIndex; - CSSCachedMeasurement cachedMeasurements[CSS_MAX_CACHED_RESULT_COUNT]; + YGCachedMeasurement cachedMeasurements[YG_MAX_CACHED_RESULT_COUNT]; float measuredDimensions[2]; - CSSCachedMeasurement cachedLayout; -} CSSLayout; + YGCachedMeasurement cachedLayout; +} YGLayout; -typedef struct CSSStyle { +typedef struct YGStyle { YGDirection direction; YGFlexDirection flexDirection; YGJustify justifyContent; @@ -88,34 +88,34 @@ typedef struct CSSStyle { // Yoga specific properties, not compatible with flexbox specification float aspectRatio; -} CSSStyle; +} YGStyle; -typedef struct CSSNode { - CSSStyle style; - CSSLayout layout; +typedef struct YGNode { + YGStyle style; + YGLayout layout; uint32_t lineIndex; bool hasNewLayout; - CSSNodeRef parent; - CSSNodeListRef children; + YGNodeRef parent; + YGNodeListRef children; bool isDirty; - struct CSSNode *nextChild; + struct YGNode *nextChild; - CSSMeasureFunc measure; - CSSPrintFunc print; + YGMeasureFunc measure; + YGPrintFunc print; void *context; -} CSSNode; +} YGNode; -static void _CSSNodeMarkDirty(const CSSNodeRef node); +static void YGNodeMarkDirtyInternal(const YGNodeRef node); -CSSMalloc gCSSMalloc = &malloc; -CSSCalloc gCSSCalloc = &calloc; -CSSRealloc gCSSRealloc = &realloc; -CSSFree gCSSFree = &free; +YGMalloc gYGMalloc = &malloc; +YGCalloc gYGCalloc = &calloc; +YGRealloc gYGRealloc = &realloc; +YGFree gYGFree = &free; #ifdef ANDROID #include -static int _csslayoutAndroidLog(YGLogLevel level, const char *format, va_list args) { +static int YGAndroidLog(YGLogLevel level, const char *format, va_list args) { int androidLevel = YGLogLevelDebug; switch (level) { case YGLogLevelError: @@ -136,12 +136,12 @@ static int _csslayoutAndroidLog(YGLogLevel level, const char *format, va_list ar case YGLogLevelCount: break; } - const int result = __android_log_vprint(androidLevel, "css-layout", format, args); + const int result = __android_log_vprint(androidLevel, "YG-layout", format, args); return result; } -static CSSLogger gLogger = &_csslayoutAndroidLog; +static YGLogger gLogger = &YGAndroidLog; #else -static int _csslayoutDefaultLog(YGLogLevel level, const char *format, va_list args) { +static int YGDefaultLog(YGLogLevel level, const char *format, va_list args) { switch (level) { case YGLogLevelError: return vfprintf(stderr, format, args); @@ -153,28 +153,28 @@ static int _csslayoutDefaultLog(YGLogLevel level, const char *format, va_list ar return vprintf(format, args); } } -static CSSLogger gLogger = &_csslayoutDefaultLog; +static YGLogger gLogger = &YGDefaultLog; #endif -static inline float computedEdgeValue(const float edges[YGEdgeCount], - const YGEdge edge, - const float defaultValue) { - CSS_ASSERT(edge <= YGEdgeEnd, "Cannot get computed value of multi-edge shorthands"); +static inline float YGComputedEdgeValue(const float edges[YGEdgeCount], + const YGEdge edge, + const float defaultValue) { + YG_ASSERT(edge <= YGEdgeEnd, "Cannot get computed value of multi-edge shorthands"); - if (!CSSValueIsUndefined(edges[edge])) { + if (!YGValueIsUndefined(edges[edge])) { return edges[edge]; } - if ((edge == YGEdgeTop || edge == YGEdgeBottom) && !CSSValueIsUndefined(edges[YGEdgeVertical])) { + if ((edge == YGEdgeTop || edge == YGEdgeBottom) && !YGValueIsUndefined(edges[YGEdgeVertical])) { return edges[YGEdgeVertical]; } if ((edge == YGEdgeLeft || edge == YGEdgeRight || edge == YGEdgeStart || edge == YGEdgeEnd) && - !CSSValueIsUndefined(edges[YGEdgeHorizontal])) { + !YGValueIsUndefined(edges[YGEdgeHorizontal])) { return edges[YGEdgeHorizontal]; } - if (!CSSValueIsUndefined(edges[YGEdgeAll])) { + if (!YGValueIsUndefined(edges[YGEdgeAll])) { return edges[YGEdgeAll]; } @@ -187,55 +187,55 @@ static inline float computedEdgeValue(const float edges[YGEdgeCount], int32_t gNodeInstanceCount = 0; -CSSNodeRef CSSNodeNew(void) { - const CSSNodeRef node = gCSSCalloc(1, sizeof(CSSNode)); - CSS_ASSERT(node, "Could not allocate memory for node"); +YGNodeRef YGNodeNew(void) { + const YGNodeRef node = gYGCalloc(1, sizeof(YGNode)); + YG_ASSERT(node, "Could not allocate memory for node"); gNodeInstanceCount++; - CSSNodeInit(node); + YGNodeInit(node); return node; } -void CSSNodeFree(const CSSNodeRef node) { +void YGNodeFree(const YGNodeRef node) { if (node->parent) { - CSSNodeListDelete(node->parent->children, node); + YGNodeListDelete(node->parent->children, node); node->parent = NULL; } - const uint32_t childCount = CSSNodeChildCount(node); + const uint32_t childCount = YGNodeChildCount(node); for (uint32_t i = 0; i < childCount; i++) { - const CSSNodeRef child = CSSNodeGetChild(node, i); + const YGNodeRef child = YGNodeGetChild(node, i); child->parent = NULL; } - CSSNodeListFree(node->children); - gCSSFree(node); + YGNodeListFree(node->children); + gYGFree(node); gNodeInstanceCount--; } -void CSSNodeFreeRecursive(const CSSNodeRef root) { - while (CSSNodeChildCount(root) > 0) { - const CSSNodeRef child = CSSNodeGetChild(root, 0); - CSSNodeRemoveChild(root, child); - CSSNodeFreeRecursive(child); +void YGNodeFreeRecursive(const YGNodeRef root) { + while (YGNodeChildCount(root) > 0) { + const YGNodeRef child = YGNodeGetChild(root, 0); + YGNodeRemoveChild(root, child); + YGNodeFreeRecursive(child); } - CSSNodeFree(root); + YGNodeFree(root); } -void CSSNodeReset(const CSSNodeRef node) { - CSS_ASSERT(CSSNodeChildCount(node) == 0, "Cannot reset a node which still has children attached"); - CSS_ASSERT(node->parent == NULL, "Cannot reset a node still attached to a parent"); +void YGNodeReset(const YGNodeRef node) { + YG_ASSERT(YGNodeChildCount(node) == 0, "Cannot reset a node which still has children attached"); + YG_ASSERT(node->parent == NULL, "Cannot reset a node still attached to a parent"); - CSSNodeListFree(node->children); - memset(node, 0, sizeof(CSSNode)); - CSSNodeInit(node); + YGNodeListFree(node->children); + memset(node, 0, sizeof(YGNode)); + YGNodeInit(node); } -int32_t CSSNodeGetInstanceCount(void) { +int32_t YGNodeGetInstanceCount(void) { return gNodeInstanceCount; } -void CSSNodeInit(const CSSNodeRef node) { +void YGNodeInit(const YGNodeRef node) { node->parent = NULL; node->children = NULL; node->hasNewLayout = true; @@ -289,386 +289,396 @@ void CSSNodeInit(const CSSNodeRef node) { node->layout.cachedLayout.computedHeight = -1; } -static void _CSSNodeMarkDirty(const CSSNodeRef node) { +static void YGNodeMarkDirtyInternal(const YGNodeRef node) { if (!node->isDirty) { node->isDirty = true; node->layout.computedFlexBasis = YGUndefined; if (node->parent) { - _CSSNodeMarkDirty(node->parent); + YGNodeMarkDirtyInternal(node->parent); } } } -void CSSNodeSetMeasureFunc(const CSSNodeRef node, CSSMeasureFunc measureFunc) { +void YGNodeSetMeasureFunc(const YGNodeRef node, YGMeasureFunc measureFunc) { if (measureFunc == NULL) { node->measure = NULL; } else { - CSS_ASSERT(CSSNodeChildCount(node) == 0, - "Cannot set measure function: Nodes with measure functions cannot have children."); + YG_ASSERT(YGNodeChildCount(node) == 0, + "Cannot set measure function: Nodes with measure functions cannot have children."); node->measure = measureFunc; } } -CSSMeasureFunc CSSNodeGetMeasureFunc(const CSSNodeRef node) { +YGMeasureFunc YGNodeGetMeasureFunc(const YGNodeRef node) { return node->measure; } -void CSSNodeInsertChild(const CSSNodeRef node, const CSSNodeRef child, const uint32_t index) { - CSS_ASSERT(child->parent == NULL, "Child already has a parent, it must be removed first."); - CSS_ASSERT(node->measure == NULL, - "Cannot add child: Nodes with measure functions cannot have children."); - CSSNodeListInsert(&node->children, child, index); +void YGNodeInsertChild(const YGNodeRef node, const YGNodeRef child, const uint32_t index) { + YG_ASSERT(child->parent == NULL, "Child already has a parent, it must be removed first."); + YG_ASSERT(node->measure == NULL, + "Cannot add child: Nodes with measure functions cannot have children."); + YGNodeListInsert(&node->children, child, index); child->parent = node; - _CSSNodeMarkDirty(node); + YGNodeMarkDirtyInternal(node); } -void CSSNodeRemoveChild(const CSSNodeRef node, const CSSNodeRef child) { - if (CSSNodeListDelete(node->children, child) != NULL) { +void YGNodeRemoveChild(const YGNodeRef node, const YGNodeRef child) { + if (YGNodeListDelete(node->children, child) != NULL) { child->parent = NULL; - _CSSNodeMarkDirty(node); + YGNodeMarkDirtyInternal(node); } } -CSSNodeRef CSSNodeGetChild(const CSSNodeRef node, const uint32_t index) { - return CSSNodeListGet(node->children, index); +YGNodeRef YGNodeGetChild(const YGNodeRef node, const uint32_t index) { + return YGNodeListGet(node->children, index); } -inline uint32_t CSSNodeChildCount(const CSSNodeRef node) { - return CSSNodeListCount(node->children); +inline uint32_t YGNodeChildCount(const YGNodeRef node) { + return YGNodeListCount(node->children); } -void CSSNodeMarkDirty(const CSSNodeRef node) { - CSS_ASSERT(node->measure != NULL, - "Only leaf nodes with custom measure functions" - "should manually mark themselves as dirty"); - _CSSNodeMarkDirty(node); +void YGNodeMarkDirty(const YGNodeRef node) { + YG_ASSERT(node->measure != NULL, + "Only leaf nodes with custom measure functions" + "should manually mark themselves as dirty"); + YGNodeMarkDirtyInternal(node); } -bool CSSNodeIsDirty(const CSSNodeRef node) { +bool YGNodeIsDirty(const YGNodeRef node) { return node->isDirty; } -void CSSNodeCopyStyle(const CSSNodeRef dstNode, const CSSNodeRef srcNode) { - if (memcmp(&dstNode->style, &srcNode->style, sizeof(CSSStyle)) != 0) { - memcpy(&dstNode->style, &srcNode->style, sizeof(CSSStyle)); - _CSSNodeMarkDirty(dstNode); +void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode) { + if (memcmp(&dstNode->style, &srcNode->style, sizeof(YGStyle)) != 0) { + memcpy(&dstNode->style, &srcNode->style, sizeof(YGStyle)); + YGNodeMarkDirtyInternal(dstNode); } } -inline float CSSNodeStyleGetFlexGrow(const CSSNodeRef node) { - if (!CSSValueIsUndefined(node->style.flexGrow)) { +inline float YGNodeStyleGetFlexGrow(const YGNodeRef node) { + if (!YGValueIsUndefined(node->style.flexGrow)) { return node->style.flexGrow; } - if (!CSSValueIsUndefined(node->style.flex) && node->style.flex > 0) { + if (!YGValueIsUndefined(node->style.flex) && node->style.flex > 0) { return node->style.flex; } return 0; } -inline float CSSNodeStyleGetFlexShrink(const CSSNodeRef node) { - if (!CSSValueIsUndefined(node->style.flexShrink)) { +inline float YGNodeStyleGetFlexShrink(const YGNodeRef node) { + if (!YGValueIsUndefined(node->style.flexShrink)) { return node->style.flexShrink; } - if (!CSSValueIsUndefined(node->style.flex) && node->style.flex < 0) { + if (!YGValueIsUndefined(node->style.flex) && node->style.flex < 0) { return -node->style.flex; } return 0; } -inline float CSSNodeStyleGetFlexBasis(const CSSNodeRef node) { - if (!CSSValueIsUndefined(node->style.flexBasis)) { +inline float YGNodeStyleGetFlexBasis(const YGNodeRef node) { + if (!YGValueIsUndefined(node->style.flexBasis)) { return node->style.flexBasis; } - if (!CSSValueIsUndefined(node->style.flex)) { + if (!YGValueIsUndefined(node->style.flex)) { return node->style.flex > 0 ? 0 : YGUndefined; } return YGUndefined; } -void CSSNodeStyleSetFlex(const CSSNodeRef node, const float flex) { +void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) { if (node->style.flex != flex) { node->style.flex = flex; - _CSSNodeMarkDirty(node); + YGNodeMarkDirtyInternal(node); } } -#define CSS_NODE_PROPERTY_IMPL(type, name, paramName, instanceName) \ - void CSSNodeSet##name(const CSSNodeRef node, type paramName) { \ - node->instanceName = paramName; \ - } \ - \ - type CSSNodeGet##name(const CSSNodeRef node) { \ - return node->instanceName; \ +#define YG_NODE_PROPERTY_IMPL(type, name, paramName, instanceName) \ + void YGNodeSet##name(const YGNodeRef node, type paramName) { \ + node->instanceName = paramName; \ + } \ + \ + type YGNodeGet##name(const YGNodeRef node) { \ + return node->instanceName; \ } -#define CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \ - void CSSNodeStyleSet##name(const CSSNodeRef node, const type paramName) { \ - if (node->style.instanceName != paramName) { \ - node->style.instanceName = paramName; \ - _CSSNodeMarkDirty(node); \ - } \ +#define YG_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \ + void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \ + if (node->style.instanceName != paramName) { \ + node->style.instanceName = paramName; \ + YGNodeMarkDirtyInternal(node); \ + } \ } -#define CSS_NODE_STYLE_PROPERTY_IMPL(type, name, paramName, instanceName) \ - CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \ - \ - type CSSNodeStyleGet##name(const CSSNodeRef node) { \ - return node->style.instanceName; \ +#define YG_NODE_STYLE_PROPERTY_IMPL(type, name, paramName, instanceName) \ + YG_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \ + \ + type YGNodeStyleGet##name(const YGNodeRef node) { \ + return node->style.instanceName; \ } -#define CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(type, name, paramName, instanceName, defaultValue) \ - void CSSNodeStyleSet##name(const CSSNodeRef node, const YGEdge edge, const type paramName) { \ - if (node->style.instanceName[edge] != paramName) { \ - node->style.instanceName[edge] = paramName; \ - _CSSNodeMarkDirty(node); \ - } \ - } \ - \ - type CSSNodeStyleGet##name(const CSSNodeRef node, const YGEdge edge) { \ - return computedEdgeValue(node->style.instanceName, edge, defaultValue); \ +#define YG_NODE_STYLE_EDGE_PROPERTY_IMPL(type, name, paramName, instanceName, defaultValue) \ + void YGNodeStyleSet##name(const YGNodeRef node, const YGEdge edge, const type paramName) { \ + if (node->style.instanceName[edge] != paramName) { \ + node->style.instanceName[edge] = paramName; \ + YGNodeMarkDirtyInternal(node); \ + } \ + } \ + \ + type YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge) { \ + return YGComputedEdgeValue(node->style.instanceName, edge, defaultValue); \ } -#define CSS_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \ - type CSSNodeLayoutGet##name(const CSSNodeRef node) { \ - return node->layout.instanceName; \ +#define YG_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \ + type YGNodeLayoutGet##name(const YGNodeRef node) { \ + return node->layout.instanceName; \ } -CSS_NODE_PROPERTY_IMPL(void *, Context, context, context); -CSS_NODE_PROPERTY_IMPL(CSSPrintFunc, PrintFunc, printFunc, print); -CSS_NODE_PROPERTY_IMPL(bool, HasNewLayout, hasNewLayout, hasNewLayout); +YG_NODE_PROPERTY_IMPL(void *, Context, context, context); +YG_NODE_PROPERTY_IMPL(YGPrintFunc, PrintFunc, printFunc, print); +YG_NODE_PROPERTY_IMPL(bool, HasNewLayout, hasNewLayout, hasNewLayout); -CSS_NODE_STYLE_PROPERTY_IMPL(YGDirection, Direction, direction, direction); -CSS_NODE_STYLE_PROPERTY_IMPL(YGFlexDirection, FlexDirection, flexDirection, flexDirection); -CSS_NODE_STYLE_PROPERTY_IMPL(YGJustify, JustifyContent, justifyContent, justifyContent); -CSS_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignContent, alignContent, alignContent); -CSS_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignItems, alignItems, alignItems); -CSS_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignSelf, alignSelf, alignSelf); -CSS_NODE_STYLE_PROPERTY_IMPL(YGPositionType, PositionType, positionType, positionType); -CSS_NODE_STYLE_PROPERTY_IMPL(YGWrap, FlexWrap, flexWrap, flexWrap); -CSS_NODE_STYLE_PROPERTY_IMPL(YGOverflow, Overflow, overflow, overflow); +YG_NODE_STYLE_PROPERTY_IMPL(YGDirection, Direction, direction, direction); +YG_NODE_STYLE_PROPERTY_IMPL(YGFlexDirection, FlexDirection, flexDirection, flexDirection); +YG_NODE_STYLE_PROPERTY_IMPL(YGJustify, JustifyContent, justifyContent, justifyContent); +YG_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignContent, alignContent, alignContent); +YG_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignItems, alignItems, alignItems); +YG_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignSelf, alignSelf, alignSelf); +YG_NODE_STYLE_PROPERTY_IMPL(YGPositionType, PositionType, positionType, positionType); +YG_NODE_STYLE_PROPERTY_IMPL(YGWrap, FlexWrap, flexWrap, flexWrap); +YG_NODE_STYLE_PROPERTY_IMPL(YGOverflow, Overflow, overflow, overflow); -CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexGrow, flexGrow, flexGrow); -CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexShrink, flexShrink, flexShrink); -CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexBasis, flexBasis, flexBasis); +YG_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexGrow, flexGrow, flexGrow); +YG_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexShrink, flexShrink, flexShrink); +YG_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexBasis, flexBasis, flexBasis); -CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Position, position, position, YGUndefined); -CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Margin, margin, margin, 0); -CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Padding, padding, padding, 0); -CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Border, border, border, 0); +YG_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Position, position, position, YGUndefined); +YG_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Margin, margin, margin, 0); +YG_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Padding, padding, padding, 0); +YG_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Border, border, border, 0); -CSS_NODE_STYLE_PROPERTY_IMPL(float, Width, width, dimensions[YGDimensionWidth]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, Height, height, dimensions[YGDimensionHeight]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MinWidth, minWidth, minDimensions[YGDimensionWidth]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MinHeight, minHeight, minDimensions[YGDimensionHeight]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MaxWidth, maxWidth, maxDimensions[YGDimensionWidth]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MaxHeight, maxHeight, maxDimensions[YGDimensionHeight]); +YG_NODE_STYLE_PROPERTY_IMPL(float, Width, width, dimensions[YGDimensionWidth]); +YG_NODE_STYLE_PROPERTY_IMPL(float, Height, height, dimensions[YGDimensionHeight]); +YG_NODE_STYLE_PROPERTY_IMPL(float, MinWidth, minWidth, minDimensions[YGDimensionWidth]); +YG_NODE_STYLE_PROPERTY_IMPL(float, MinHeight, minHeight, minDimensions[YGDimensionHeight]); +YG_NODE_STYLE_PROPERTY_IMPL(float, MaxWidth, maxWidth, maxDimensions[YGDimensionWidth]); +YG_NODE_STYLE_PROPERTY_IMPL(float, MaxHeight, maxHeight, maxDimensions[YGDimensionHeight]); // Yoga specific properties, not compatible with flexbox specification -CSS_NODE_STYLE_PROPERTY_IMPL(float, AspectRatio, aspectRatio, aspectRatio); +YG_NODE_STYLE_PROPERTY_IMPL(float, AspectRatio, aspectRatio, aspectRatio); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Left, position[YGEdgeLeft]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[YGEdgeTop]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[YGEdgeRight]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Bottom, position[YGEdgeBottom]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Width, dimensions[YGDimensionWidth]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[YGDimensionHeight]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Left, position[YGEdgeLeft]); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[YGEdgeTop]); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[YGEdgeRight]); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Bottom, position[YGEdgeBottom]); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Width, dimensions[YGDimensionWidth]); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[YGDimensionHeight]); +YG_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction); uint32_t gCurrentGenerationCount = 0; -bool layoutNodeInternal(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGDirection parentDirection, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, - const bool performLayout, - const char *reason); +bool YGLayoutNodeInternal(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGDirection parentDirection, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode, + const bool performLayout, + const char *reason); -inline bool CSSValueIsUndefined(const float value) { +inline bool YGValueIsUndefined(const float value) { return isnan(value); } -static inline bool eq(const float a, const float b) { - if (CSSValueIsUndefined(a)) { - return CSSValueIsUndefined(b); +static inline bool YGFloatsEqual(const float a, const float b) { + if (YGValueIsUndefined(a)) { + return YGValueIsUndefined(b); } return fabs(a - b) < 0.0001; } -static void indent(const uint32_t n) { +static void YGIndent(const uint32_t n) { for (uint32_t i = 0; i < n; i++) { - CSSLog(YGLogLevelDebug, " "); + YGLog(YGLogLevelDebug, " "); } } -static void printNumberIfNotZero(const char *str, const float number) { - if (!eq(number, 0)) { - CSSLog(YGLogLevelDebug, "%s: %g, ", str, number); +static void YGPrintNumberIfNotZero(const char *str, const float number) { + if (!YGFloatsEqual(number, 0)) { + YGLog(YGLogLevelDebug, "%s: %g, ", str, number); } } -static void printNumberIfNotUndefined(const char *str, const float number) { - if (!CSSValueIsUndefined(number)) { - CSSLog(YGLogLevelDebug, "%s: %g, ", str, number); +static void YGPrintNumberIfNotUndefined(const char *str, const float number) { + if (!YGValueIsUndefined(number)) { + YGLog(YGLogLevelDebug, "%s: %g, ", str, number); } } -static bool eqFour(const float four[4]) { - return eq(four[0], four[1]) && eq(four[0], four[2]) && eq(four[0], four[3]); +static bool YGFourFloatsEqual(const float four[4]) { + return YGFloatsEqual(four[0], four[1]) && YGFloatsEqual(four[0], four[2]) && + YGFloatsEqual(four[0], four[3]); } -static void _CSSNodePrint(const CSSNodeRef node, - const YGPrintOptions options, - const uint32_t level) { - indent(level); - CSSLog(YGLogLevelDebug, "{"); +static void YGNodePrintInternal(const YGNodeRef node, + const YGPrintOptions options, + const uint32_t level) { + YGIndent(level); + YGLog(YGLogLevelDebug, "{"); if (node->print) { node->print(node); } if (options & YGPrintOptionsLayout) { - CSSLog(YGLogLevelDebug, "layout: {"); - CSSLog(YGLogLevelDebug, "width: %g, ", node->layout.dimensions[YGDimensionWidth]); - CSSLog(YGLogLevelDebug, "height: %g, ", node->layout.dimensions[YGDimensionHeight]); - CSSLog(YGLogLevelDebug, "top: %g, ", node->layout.position[YGEdgeTop]); - CSSLog(YGLogLevelDebug, "left: %g", node->layout.position[YGEdgeLeft]); - CSSLog(YGLogLevelDebug, "}, "); + YGLog(YGLogLevelDebug, "layout: {"); + YGLog(YGLogLevelDebug, "width: %g, ", node->layout.dimensions[YGDimensionWidth]); + YGLog(YGLogLevelDebug, "height: %g, ", node->layout.dimensions[YGDimensionHeight]); + YGLog(YGLogLevelDebug, "top: %g, ", node->layout.position[YGEdgeTop]); + YGLog(YGLogLevelDebug, "left: %g", node->layout.position[YGEdgeLeft]); + YGLog(YGLogLevelDebug, "}, "); } if (options & YGPrintOptionsStyle) { if (node->style.flexDirection == YGFlexDirectionColumn) { - CSSLog(YGLogLevelDebug, "flexDirection: 'column', "); + YGLog(YGLogLevelDebug, "flexDirection: 'column', "); } else if (node->style.flexDirection == YGFlexDirectionColumnReverse) { - CSSLog(YGLogLevelDebug, "flexDirection: 'column-reverse', "); + YGLog(YGLogLevelDebug, "flexDirection: 'column-reverse', "); } else if (node->style.flexDirection == YGFlexDirectionRow) { - CSSLog(YGLogLevelDebug, "flexDirection: 'row', "); + YGLog(YGLogLevelDebug, "flexDirection: 'row', "); } else if (node->style.flexDirection == YGFlexDirectionRowReverse) { - CSSLog(YGLogLevelDebug, "flexDirection: 'row-reverse', "); + YGLog(YGLogLevelDebug, "flexDirection: 'row-reverse', "); } if (node->style.justifyContent == YGJustifyCenter) { - CSSLog(YGLogLevelDebug, "justifyContent: 'center', "); + YGLog(YGLogLevelDebug, "justifyContent: 'center', "); } else if (node->style.justifyContent == YGJustifyFlexEnd) { - CSSLog(YGLogLevelDebug, "justifyContent: 'flex-end', "); + YGLog(YGLogLevelDebug, "justifyContent: 'flex-end', "); } else if (node->style.justifyContent == YGJustifySpaceAround) { - CSSLog(YGLogLevelDebug, "justifyContent: 'space-around', "); + YGLog(YGLogLevelDebug, "justifyContent: 'space-around', "); } else if (node->style.justifyContent == YGJustifySpaceBetween) { - CSSLog(YGLogLevelDebug, "justifyContent: 'space-between', "); + YGLog(YGLogLevelDebug, "justifyContent: 'space-between', "); } if (node->style.alignItems == YGAlignCenter) { - CSSLog(YGLogLevelDebug, "alignItems: 'center', "); + YGLog(YGLogLevelDebug, "alignItems: 'center', "); } else if (node->style.alignItems == YGAlignFlexEnd) { - CSSLog(YGLogLevelDebug, "alignItems: 'flex-end', "); + YGLog(YGLogLevelDebug, "alignItems: 'flex-end', "); } else if (node->style.alignItems == YGAlignStretch) { - CSSLog(YGLogLevelDebug, "alignItems: 'stretch', "); + YGLog(YGLogLevelDebug, "alignItems: 'stretch', "); } if (node->style.alignContent == YGAlignCenter) { - CSSLog(YGLogLevelDebug, "alignContent: 'center', "); + YGLog(YGLogLevelDebug, "alignContent: 'center', "); } else if (node->style.alignContent == YGAlignFlexEnd) { - CSSLog(YGLogLevelDebug, "alignContent: 'flex-end', "); + YGLog(YGLogLevelDebug, "alignContent: 'flex-end', "); } else if (node->style.alignContent == YGAlignStretch) { - CSSLog(YGLogLevelDebug, "alignContent: 'stretch', "); + YGLog(YGLogLevelDebug, "alignContent: 'stretch', "); } if (node->style.alignSelf == YGAlignFlexStart) { - CSSLog(YGLogLevelDebug, "alignSelf: 'flex-start', "); + YGLog(YGLogLevelDebug, "alignSelf: 'flex-start', "); } else if (node->style.alignSelf == YGAlignCenter) { - CSSLog(YGLogLevelDebug, "alignSelf: 'center', "); + YGLog(YGLogLevelDebug, "alignSelf: 'center', "); } else if (node->style.alignSelf == YGAlignFlexEnd) { - CSSLog(YGLogLevelDebug, "alignSelf: 'flex-end', "); + YGLog(YGLogLevelDebug, "alignSelf: 'flex-end', "); } else if (node->style.alignSelf == YGAlignStretch) { - CSSLog(YGLogLevelDebug, "alignSelf: 'stretch', "); + YGLog(YGLogLevelDebug, "alignSelf: 'stretch', "); } - printNumberIfNotUndefined("flexGrow", CSSNodeStyleGetFlexGrow(node)); - printNumberIfNotUndefined("flexShrink", CSSNodeStyleGetFlexShrink(node)); - printNumberIfNotUndefined("flexBasis", CSSNodeStyleGetFlexBasis(node)); + YGPrintNumberIfNotUndefined("flexGrow", YGNodeStyleGetFlexGrow(node)); + YGPrintNumberIfNotUndefined("flexShrink", YGNodeStyleGetFlexShrink(node)); + YGPrintNumberIfNotUndefined("flexBasis", YGNodeStyleGetFlexBasis(node)); if (node->style.overflow == YGOverflowHidden) { - CSSLog(YGLogLevelDebug, "overflow: 'hidden', "); + YGLog(YGLogLevelDebug, "overflow: 'hidden', "); } else if (node->style.overflow == YGOverflowVisible) { - CSSLog(YGLogLevelDebug, "overflow: 'visible', "); + YGLog(YGLogLevelDebug, "overflow: 'visible', "); } else if (node->style.overflow == YGOverflowScroll) { - CSSLog(YGLogLevelDebug, "overflow: 'scroll', "); + YGLog(YGLogLevelDebug, "overflow: 'scroll', "); } - if (eqFour(node->style.margin)) { - printNumberIfNotZero("margin", computedEdgeValue(node->style.margin, YGEdgeLeft, 0)); + if (YGFourFloatsEqual(node->style.margin)) { + YGPrintNumberIfNotZero("margin", YGComputedEdgeValue(node->style.margin, YGEdgeLeft, 0)); } else { - printNumberIfNotZero("marginLeft", computedEdgeValue(node->style.margin, YGEdgeLeft, 0)); - printNumberIfNotZero("marginRight", computedEdgeValue(node->style.margin, YGEdgeRight, 0)); - printNumberIfNotZero("marginTop", computedEdgeValue(node->style.margin, YGEdgeTop, 0)); - printNumberIfNotZero("marginBottom", computedEdgeValue(node->style.margin, YGEdgeBottom, 0)); - printNumberIfNotZero("marginStart", computedEdgeValue(node->style.margin, YGEdgeStart, 0)); - printNumberIfNotZero("marginEnd", computedEdgeValue(node->style.margin, YGEdgeEnd, 0)); + YGPrintNumberIfNotZero("marginLeft", YGComputedEdgeValue(node->style.margin, YGEdgeLeft, 0)); + YGPrintNumberIfNotZero("marginRight", + YGComputedEdgeValue(node->style.margin, YGEdgeRight, 0)); + YGPrintNumberIfNotZero("marginTop", YGComputedEdgeValue(node->style.margin, YGEdgeTop, 0)); + YGPrintNumberIfNotZero("marginBottom", + YGComputedEdgeValue(node->style.margin, YGEdgeBottom, 0)); + YGPrintNumberIfNotZero("marginStart", + YGComputedEdgeValue(node->style.margin, YGEdgeStart, 0)); + YGPrintNumberIfNotZero("marginEnd", YGComputedEdgeValue(node->style.margin, YGEdgeEnd, 0)); } - if (eqFour(node->style.padding)) { - printNumberIfNotZero("padding", computedEdgeValue(node->style.padding, YGEdgeLeft, 0)); + if (YGFourFloatsEqual(node->style.padding)) { + YGPrintNumberIfNotZero("padding", YGComputedEdgeValue(node->style.padding, YGEdgeLeft, 0)); } else { - printNumberIfNotZero("paddingLeft", computedEdgeValue(node->style.padding, YGEdgeLeft, 0)); - printNumberIfNotZero("paddingRight", computedEdgeValue(node->style.padding, YGEdgeRight, 0)); - printNumberIfNotZero("paddingTop", computedEdgeValue(node->style.padding, YGEdgeTop, 0)); - printNumberIfNotZero("paddingBottom", - computedEdgeValue(node->style.padding, YGEdgeBottom, 0)); - printNumberIfNotZero("paddingStart", computedEdgeValue(node->style.padding, YGEdgeStart, 0)); - printNumberIfNotZero("paddingEnd", computedEdgeValue(node->style.padding, YGEdgeEnd, 0)); + YGPrintNumberIfNotZero("paddingLeft", + YGComputedEdgeValue(node->style.padding, YGEdgeLeft, 0)); + YGPrintNumberIfNotZero("paddingRight", + YGComputedEdgeValue(node->style.padding, YGEdgeRight, 0)); + YGPrintNumberIfNotZero("paddingTop", YGComputedEdgeValue(node->style.padding, YGEdgeTop, 0)); + YGPrintNumberIfNotZero("paddingBottom", + YGComputedEdgeValue(node->style.padding, YGEdgeBottom, 0)); + YGPrintNumberIfNotZero("paddingStart", + YGComputedEdgeValue(node->style.padding, YGEdgeStart, 0)); + YGPrintNumberIfNotZero("paddingEnd", YGComputedEdgeValue(node->style.padding, YGEdgeEnd, 0)); } - if (eqFour(node->style.border)) { - printNumberIfNotZero("borderWidth", computedEdgeValue(node->style.border, YGEdgeLeft, 0)); + if (YGFourFloatsEqual(node->style.border)) { + YGPrintNumberIfNotZero("borderWidth", YGComputedEdgeValue(node->style.border, YGEdgeLeft, 0)); } else { - printNumberIfNotZero("borderLeftWidth", computedEdgeValue(node->style.border, YGEdgeLeft, 0)); - printNumberIfNotZero("borderRightWidth", - computedEdgeValue(node->style.border, YGEdgeRight, 0)); - printNumberIfNotZero("borderTopWidth", computedEdgeValue(node->style.border, YGEdgeTop, 0)); - printNumberIfNotZero("borderBottomWidth", - computedEdgeValue(node->style.border, YGEdgeBottom, 0)); - printNumberIfNotZero("borderStartWidth", - computedEdgeValue(node->style.border, YGEdgeStart, 0)); - printNumberIfNotZero("borderEndWidth", computedEdgeValue(node->style.border, YGEdgeEnd, 0)); + YGPrintNumberIfNotZero("borderLeftWidth", + YGComputedEdgeValue(node->style.border, YGEdgeLeft, 0)); + YGPrintNumberIfNotZero("borderRightWidth", + YGComputedEdgeValue(node->style.border, YGEdgeRight, 0)); + YGPrintNumberIfNotZero("borderTopWidth", + YGComputedEdgeValue(node->style.border, YGEdgeTop, 0)); + YGPrintNumberIfNotZero("borderBottomWidth", + YGComputedEdgeValue(node->style.border, YGEdgeBottom, 0)); + YGPrintNumberIfNotZero("borderStartWidth", + YGComputedEdgeValue(node->style.border, YGEdgeStart, 0)); + YGPrintNumberIfNotZero("borderEndWidth", + YGComputedEdgeValue(node->style.border, YGEdgeEnd, 0)); } - printNumberIfNotUndefined("width", node->style.dimensions[YGDimensionWidth]); - printNumberIfNotUndefined("height", node->style.dimensions[YGDimensionHeight]); - printNumberIfNotUndefined("maxWidth", node->style.maxDimensions[YGDimensionWidth]); - printNumberIfNotUndefined("maxHeight", node->style.maxDimensions[YGDimensionHeight]); - printNumberIfNotUndefined("minWidth", node->style.minDimensions[YGDimensionWidth]); - printNumberIfNotUndefined("minHeight", node->style.minDimensions[YGDimensionHeight]); + YGPrintNumberIfNotUndefined("width", node->style.dimensions[YGDimensionWidth]); + YGPrintNumberIfNotUndefined("height", node->style.dimensions[YGDimensionHeight]); + YGPrintNumberIfNotUndefined("maxWidth", node->style.maxDimensions[YGDimensionWidth]); + YGPrintNumberIfNotUndefined("maxHeight", node->style.maxDimensions[YGDimensionHeight]); + YGPrintNumberIfNotUndefined("minWidth", node->style.minDimensions[YGDimensionWidth]); + YGPrintNumberIfNotUndefined("minHeight", node->style.minDimensions[YGDimensionHeight]); if (node->style.positionType == YGPositionTypeAbsolute) { - CSSLog(YGLogLevelDebug, "position: 'absolute', "); + YGLog(YGLogLevelDebug, "position: 'absolute', "); } - printNumberIfNotUndefined("left", - computedEdgeValue(node->style.position, YGEdgeLeft, YGUndefined)); - printNumberIfNotUndefined("right", - computedEdgeValue(node->style.position, YGEdgeRight, YGUndefined)); - printNumberIfNotUndefined("top", - computedEdgeValue(node->style.position, YGEdgeTop, YGUndefined)); - printNumberIfNotUndefined("bottom", - computedEdgeValue(node->style.position, YGEdgeBottom, YGUndefined)); + YGPrintNumberIfNotUndefined("left", + YGComputedEdgeValue(node->style.position, YGEdgeLeft, YGUndefined)); + YGPrintNumberIfNotUndefined( + "right", YGComputedEdgeValue(node->style.position, YGEdgeRight, YGUndefined)); + YGPrintNumberIfNotUndefined("top", + YGComputedEdgeValue(node->style.position, YGEdgeTop, YGUndefined)); + YGPrintNumberIfNotUndefined( + "bottom", YGComputedEdgeValue(node->style.position, YGEdgeBottom, YGUndefined)); } - const uint32_t childCount = CSSNodeListCount(node->children); + const uint32_t childCount = YGNodeListCount(node->children); if (options & YGPrintOptionsChildren && childCount > 0) { - CSSLog(YGLogLevelDebug, "children: [\n"); + YGLog(YGLogLevelDebug, "children: [\n"); for (uint32_t i = 0; i < childCount; i++) { - _CSSNodePrint(CSSNodeGetChild(node, i), options, level + 1); + YGNodePrintInternal(YGNodeGetChild(node, i), options, level + 1); } - indent(level); - CSSLog(YGLogLevelDebug, "]},\n"); + YGIndent(level); + YGLog(YGLogLevelDebug, "]},\n"); } else { - CSSLog(YGLogLevelDebug, "},\n"); + YGLog(YGLogLevelDebug, "},\n"); } } -void CSSNodePrint(const CSSNodeRef node, const YGPrintOptions options) { - _CSSNodePrint(node, options, 0); +void YGNodePrint(const YGNodeRef node, const YGPrintOptions options) { + YGNodePrintInternal(node, options, 0); } static const YGEdge leading[4] = { @@ -696,88 +706,91 @@ static const YGDimension dim[4] = { [YGFlexDirectionRowReverse] = YGDimensionWidth, }; -static inline bool isRowDirection(const YGFlexDirection flexDirection) { +static inline bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection) { return flexDirection == YGFlexDirectionRow || flexDirection == YGFlexDirectionRowReverse; } -static inline bool isColumnDirection(const YGFlexDirection flexDirection) { +static inline bool YGFlexDirectionIsColumn(const YGFlexDirection flexDirection) { return flexDirection == YGFlexDirectionColumn || flexDirection == YGFlexDirectionColumnReverse; } -static inline float getLeadingMargin(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.margin[YGEdgeStart])) { +static inline float YGNodeLeadingMargin(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.margin[YGEdgeStart])) { return node->style.margin[YGEdgeStart]; } - return computedEdgeValue(node->style.margin, leading[axis], 0); + return YGComputedEdgeValue(node->style.margin, leading[axis], 0); } -static float getTrailingMargin(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.margin[YGEdgeEnd])) { +static float YGNodeTrailingMargin(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.margin[YGEdgeEnd])) { return node->style.margin[YGEdgeEnd]; } - return computedEdgeValue(node->style.margin, trailing[axis], 0); + return YGComputedEdgeValue(node->style.margin, trailing[axis], 0); } -static float getLeadingPadding(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.padding[YGEdgeStart]) && +static float YGNodeLeadingPadding(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.padding[YGEdgeStart]) && node->style.padding[YGEdgeStart] >= 0) { return node->style.padding[YGEdgeStart]; } - return fmaxf(computedEdgeValue(node->style.padding, leading[axis], 0), 0); + return fmaxf(YGComputedEdgeValue(node->style.padding, leading[axis], 0), 0); } -static float getTrailingPadding(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.padding[YGEdgeEnd]) && +static float YGNodeTrailingPadding(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.padding[YGEdgeEnd]) && node->style.padding[YGEdgeEnd] >= 0) { return node->style.padding[YGEdgeEnd]; } - return fmaxf(computedEdgeValue(node->style.padding, trailing[axis], 0), 0); + return fmaxf(YGComputedEdgeValue(node->style.padding, trailing[axis], 0), 0); } -static float getLeadingBorder(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.border[YGEdgeStart]) && +static float YGNodeLeadingBorder(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.border[YGEdgeStart]) && node->style.border[YGEdgeStart] >= 0) { return node->style.border[YGEdgeStart]; } - return fmaxf(computedEdgeValue(node->style.border, leading[axis], 0), 0); + return fmaxf(YGComputedEdgeValue(node->style.border, leading[axis], 0), 0); } -static float getTrailingBorder(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.border[YGEdgeEnd]) && +static float YGNodeTrailingBorder(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.border[YGEdgeEnd]) && node->style.border[YGEdgeEnd] >= 0) { return node->style.border[YGEdgeEnd]; } - return fmaxf(computedEdgeValue(node->style.border, trailing[axis], 0), 0); + return fmaxf(YGComputedEdgeValue(node->style.border, trailing[axis], 0), 0); } -static inline float getLeadingPaddingAndBorder(const CSSNodeRef node, const YGFlexDirection axis) { - return getLeadingPadding(node, axis) + getLeadingBorder(node, axis); +static inline float YGNodeLeadingPaddingAndBorder(const YGNodeRef node, + const YGFlexDirection axis) { + return YGNodeLeadingPadding(node, axis) + YGNodeLeadingBorder(node, axis); } -static inline float getTrailingPaddingAndBorder(const CSSNodeRef node, const YGFlexDirection axis) { - return getTrailingPadding(node, axis) + getTrailingBorder(node, axis); +static inline float YGNodeTrailingPaddingAndBorder(const YGNodeRef node, + const YGFlexDirection axis) { + return YGNodeTrailingPadding(node, axis) + YGNodeTrailingBorder(node, axis); } -static inline float getMarginAxis(const CSSNodeRef node, const YGFlexDirection axis) { - return getLeadingMargin(node, axis) + getTrailingMargin(node, axis); +static inline float YGNodeMarginForAxis(const YGNodeRef node, const YGFlexDirection axis) { + return YGNodeLeadingMargin(node, axis) + YGNodeTrailingMargin(node, axis); } -static inline float getPaddingAndBorderAxis(const CSSNodeRef node, const YGFlexDirection axis) { - return getLeadingPaddingAndBorder(node, axis) + getTrailingPaddingAndBorder(node, axis); +static inline float YGNodePaddingAndBorderForAxis(const YGNodeRef node, + const YGFlexDirection axis) { + return YGNodeLeadingPaddingAndBorder(node, axis) + YGNodeTrailingPaddingAndBorder(node, axis); } -static inline YGAlign getAlignItem(const CSSNodeRef node, const CSSNodeRef child) { +static inline YGAlign YGNodeAlignItem(const YGNodeRef node, const YGNodeRef child) { return child->style.alignSelf == YGAlignAuto ? node->style.alignItems : child->style.alignSelf; } -static inline YGDirection resolveDirection(const CSSNodeRef node, - const YGDirection parentDirection) { +static inline YGDirection YGNodeResolveDirection(const YGNodeRef node, + const YGDirection parentDirection) { if (node->style.direction == YGDirectionInherit) { return parentDirection > YGDirectionInherit ? parentDirection : YGDirectionLTR; } else { @@ -785,8 +798,8 @@ static inline YGDirection resolveDirection(const CSSNodeRef node, } } -static inline YGFlexDirection resolveAxis(const YGFlexDirection flexDirection, - const YGDirection direction) { +static inline YGFlexDirection YGFlexDirectionResolve(const YGFlexDirection flexDirection, + const YGDirection direction) { if (direction == YGDirectionRTL) { if (flexDirection == YGFlexDirectionRow) { return YGFlexDirectionRowReverse; @@ -798,111 +811,117 @@ static inline YGFlexDirection resolveAxis(const YGFlexDirection flexDirection, return flexDirection; } -static YGFlexDirection getCrossFlexDirection(const YGFlexDirection flexDirection, - const YGDirection direction) { - return isColumnDirection(flexDirection) ? resolveAxis(YGFlexDirectionRow, direction) - : YGFlexDirectionColumn; +static YGFlexDirection YGFlexDirectionCross(const YGFlexDirection flexDirection, + const YGDirection direction) { + return YGFlexDirectionIsColumn(flexDirection) + ? YGFlexDirectionResolve(YGFlexDirectionRow, direction) + : YGFlexDirectionColumn; } -static inline bool isFlex(const CSSNodeRef node) { +static inline bool YGNodeIsFlex(const YGNodeRef node) { return (node->style.positionType == YGPositionTypeRelative && (node->style.flexGrow != 0 || node->style.flexShrink != 0 || node->style.flex != 0)); } -static inline float getDimWithMargin(const CSSNodeRef node, const YGFlexDirection axis) { - return node->layout.measuredDimensions[dim[axis]] + getLeadingMargin(node, axis) + - getTrailingMargin(node, axis); +static inline float YGNodeDimWithMargin(const YGNodeRef node, const YGFlexDirection axis) { + return node->layout.measuredDimensions[dim[axis]] + YGNodeLeadingMargin(node, axis) + + YGNodeTrailingMargin(node, axis); } -static inline bool isStyleDimDefined(const CSSNodeRef node, const YGFlexDirection axis) { +static inline bool YGNodeIsStyleDimDefined(const YGNodeRef node, const YGFlexDirection axis) { const float value = node->style.dimensions[dim[axis]]; - return !CSSValueIsUndefined(value) && value >= 0.0; + return !YGValueIsUndefined(value) && value >= 0.0; } -static inline bool isLayoutDimDefined(const CSSNodeRef node, const YGFlexDirection axis) { +static inline bool YGNodeIsLayoutDimDefined(const YGNodeRef node, const YGFlexDirection axis) { const float value = node->layout.measuredDimensions[dim[axis]]; - return !CSSValueIsUndefined(value) && value >= 0.0; + return !YGValueIsUndefined(value) && value >= 0.0; } -static inline bool isLeadingPosDefined(const CSSNodeRef node, const YGFlexDirection axis) { - return (isRowDirection(axis) && - !CSSValueIsUndefined( - computedEdgeValue(node->style.position, YGEdgeStart, YGUndefined))) || - !CSSValueIsUndefined(computedEdgeValue(node->style.position, leading[axis], YGUndefined)); +static inline bool YGNodeIsLeadingPosDefined(const YGNodeRef node, const YGFlexDirection axis) { + return (YGFlexDirectionIsRow(axis) && + !YGValueIsUndefined( + YGComputedEdgeValue(node->style.position, YGEdgeStart, YGUndefined))) || + !YGValueIsUndefined(YGComputedEdgeValue(node->style.position, leading[axis], YGUndefined)); } -static inline bool isTrailingPosDefined(const CSSNodeRef node, const YGFlexDirection axis) { - return (isRowDirection(axis) && - !CSSValueIsUndefined(computedEdgeValue(node->style.position, YGEdgeEnd, YGUndefined))) || - !CSSValueIsUndefined(computedEdgeValue(node->style.position, trailing[axis], YGUndefined)); +static inline bool YGNodeIsTrailingPosDefined(const YGNodeRef node, const YGFlexDirection axis) { + return (YGFlexDirectionIsRow(axis) && + !YGValueIsUndefined(YGComputedEdgeValue(node->style.position, YGEdgeEnd, YGUndefined))) || + !YGValueIsUndefined( + YGComputedEdgeValue(node->style.position, trailing[axis], YGUndefined)); } -static float getLeadingPosition(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis)) { - const float leadingPosition = computedEdgeValue(node->style.position, YGEdgeStart, YGUndefined); - if (!CSSValueIsUndefined(leadingPosition)) { +static float YGNodeLeadingPosition(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis)) { + const float leadingPosition = + YGComputedEdgeValue(node->style.position, YGEdgeStart, YGUndefined); + if (!YGValueIsUndefined(leadingPosition)) { return leadingPosition; } } - const float leadingPosition = computedEdgeValue(node->style.position, leading[axis], YGUndefined); + const float leadingPosition = + YGComputedEdgeValue(node->style.position, leading[axis], YGUndefined); - return CSSValueIsUndefined(leadingPosition) ? 0 : leadingPosition; + return YGValueIsUndefined(leadingPosition) ? 0 : leadingPosition; } -static float getTrailingPosition(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis)) { - const float trailingPosition = computedEdgeValue(node->style.position, YGEdgeEnd, YGUndefined); - if (!CSSValueIsUndefined(trailingPosition)) { +static float YGNodeTrailingPosition(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis)) { + const float trailingPosition = + YGComputedEdgeValue(node->style.position, YGEdgeEnd, YGUndefined); + if (!YGValueIsUndefined(trailingPosition)) { return trailingPosition; } } const float trailingPosition = - computedEdgeValue(node->style.position, trailing[axis], YGUndefined); + YGComputedEdgeValue(node->style.position, trailing[axis], YGUndefined); - return CSSValueIsUndefined(trailingPosition) ? 0 : trailingPosition; + return YGValueIsUndefined(trailingPosition) ? 0 : trailingPosition; } -static float boundAxisWithinMinAndMax(const CSSNodeRef node, - const YGFlexDirection axis, - const float value) { +static float YGNodeBoundAxisWithinMinAndMax(const YGNodeRef node, + const YGFlexDirection axis, + const float value) { float min = YGUndefined; float max = YGUndefined; - if (isColumnDirection(axis)) { + if (YGFlexDirectionIsColumn(axis)) { min = node->style.minDimensions[YGDimensionHeight]; max = node->style.maxDimensions[YGDimensionHeight]; - } else if (isRowDirection(axis)) { + } else if (YGFlexDirectionIsRow(axis)) { min = node->style.minDimensions[YGDimensionWidth]; max = node->style.maxDimensions[YGDimensionWidth]; } float boundValue = value; - if (!CSSValueIsUndefined(max) && max >= 0.0 && boundValue > max) { + if (!YGValueIsUndefined(max) && max >= 0.0 && boundValue > max) { boundValue = max; } - if (!CSSValueIsUndefined(min) && min >= 0.0 && boundValue < min) { + if (!YGValueIsUndefined(min) && min >= 0.0 && boundValue < min) { boundValue = min; } return boundValue; } -// Like boundAxisWithinMinAndMax but also ensures that the value doesn't go +// Like YGNodeBoundAxisWithinMinAndMax but also ensures that the value doesn't go // below the // padding and border amount. -static inline float boundAxis(const CSSNodeRef node, - const YGFlexDirection axis, - const float value) { - return fmaxf(boundAxisWithinMinAndMax(node, axis, value), getPaddingAndBorderAxis(node, axis)); +static inline float YGNodeBoundAxis(const YGNodeRef node, + const YGFlexDirection axis, + const float value) { + return fmaxf(YGNodeBoundAxisWithinMinAndMax(node, axis, value), + YGNodePaddingAndBorderForAxis(node, axis)); } -static void setTrailingPosition(const CSSNodeRef node, - const CSSNodeRef child, - const YGFlexDirection axis) { +static void YGNodeSetChildTrailingPosition(const YGNodeRef node, + const YGNodeRef child, + const YGFlexDirection axis) { const float size = child->layout.measuredDimensions[dim[axis]]; child->layout.position[trailing[axis]] = node->layout.measuredDimensions[dim[axis]] - size - child->layout.position[pos[axis]]; @@ -910,19 +929,19 @@ static void setTrailingPosition(const CSSNodeRef node, // If both left and right are defined, then use left. Otherwise return // +left or -right depending on which is defined. -static float getRelativePosition(const CSSNodeRef node, const YGFlexDirection axis) { - return isLeadingPosDefined(node, axis) ? getLeadingPosition(node, axis) - : -getTrailingPosition(node, axis); +static float YGNodeRelativePosition(const YGNodeRef node, const YGFlexDirection axis) { + return YGNodeIsLeadingPosDefined(node, axis) ? YGNodeLeadingPosition(node, axis) + : -YGNodeTrailingPosition(node, axis); } -static void constrainMaxSizeForMode(const float maxSize, YGMeasureMode *mode, float *size) { +static void YGConstrainMaxSizeForMode(const float maxSize, YGMeasureMode *mode, float *size) { switch (*mode) { case YGMeasureModeExactly: case YGMeasureModeAtMost: - *size = (CSSValueIsUndefined(maxSize) || *size < maxSize) ? *size : maxSize; + *size = (YGValueIsUndefined(maxSize) || *size < maxSize) ? *size : maxSize; break; case YGMeasureModeUndefined: - if (!CSSValueIsUndefined(maxSize)) { + if (!YGValueIsUndefined(maxSize)) { *mode = YGMeasureModeAtMost; *size = maxSize; } @@ -932,56 +951,58 @@ static void constrainMaxSizeForMode(const float maxSize, YGMeasureMode *mode, fl } } -static void setPosition(const CSSNodeRef node, const YGDirection direction) { - const YGFlexDirection mainAxis = resolveAxis(node->style.flexDirection, direction); - const YGFlexDirection crossAxis = getCrossFlexDirection(mainAxis, direction); - const float relativePositionMain = getRelativePosition(node, mainAxis); - const float relativePositionCross = getRelativePosition(node, crossAxis); +static void YGNodeSetPosition(const YGNodeRef node, const YGDirection direction) { + const YGFlexDirection mainAxis = YGFlexDirectionResolve(node->style.flexDirection, direction); + const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, direction); + const float relativePositionMain = YGNodeRelativePosition(node, mainAxis); + const float relativePositionCross = YGNodeRelativePosition(node, crossAxis); node->layout.position[leading[mainAxis]] = - getLeadingMargin(node, mainAxis) + relativePositionMain; + YGNodeLeadingMargin(node, mainAxis) + relativePositionMain; node->layout.position[trailing[mainAxis]] = - getTrailingMargin(node, mainAxis) + relativePositionMain; + YGNodeTrailingMargin(node, mainAxis) + relativePositionMain; node->layout.position[leading[crossAxis]] = - getLeadingMargin(node, crossAxis) + relativePositionCross; + YGNodeLeadingMargin(node, crossAxis) + relativePositionCross; node->layout.position[trailing[crossAxis]] = - getTrailingMargin(node, crossAxis) + relativePositionCross; + YGNodeTrailingMargin(node, crossAxis) + relativePositionCross; } -static void computeChildFlexBasis(const CSSNodeRef node, - const CSSNodeRef child, - const float width, - const YGMeasureMode widthMode, - const float height, - const YGMeasureMode heightMode, - const YGDirection direction) { - const YGFlexDirection mainAxis = resolveAxis(node->style.flexDirection, direction); - const bool isMainAxisRow = isRowDirection(mainAxis); +static void YGNodeComputeFlexBasisForChild(const YGNodeRef node, + const YGNodeRef child, + const float width, + const YGMeasureMode widthMode, + const float height, + const YGMeasureMode heightMode, + const YGDirection direction) { + const YGFlexDirection mainAxis = YGFlexDirectionResolve(node->style.flexDirection, direction); + const bool isMainAxisRow = YGFlexDirectionIsRow(mainAxis); float childWidth; float childHeight; YGMeasureMode childWidthMeasureMode; YGMeasureMode childHeightMeasureMode; - const bool isRowStyleDimDefined = isStyleDimDefined(child, YGFlexDirectionRow); - const bool isColumnStyleDimDefined = isStyleDimDefined(child, YGFlexDirectionColumn); + const bool isRowStyleDimDefined = YGNodeIsStyleDimDefined(child, YGFlexDirectionRow); + const bool isColumnStyleDimDefined = YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn); - if (!CSSValueIsUndefined(CSSNodeStyleGetFlexBasis(child)) && - !CSSValueIsUndefined(isMainAxisRow ? width : height)) { - if (CSSValueIsUndefined(child->layout.computedFlexBasis) || - (CSSLayoutIsExperimentalFeatureEnabled(YGExperimentalFeatureWebFlexBasis) && + if (!YGValueIsUndefined(YGNodeStyleGetFlexBasis(child)) && + !YGValueIsUndefined(isMainAxisRow ? width : height)) { + if (YGValueIsUndefined(child->layout.computedFlexBasis) || + (YGIsExperimentalFeatureEnabled(YGExperimentalFeatureWebFlexBasis) && child->layout.computedFlexBasisGeneration != gCurrentGenerationCount)) { child->layout.computedFlexBasis = - fmaxf(CSSNodeStyleGetFlexBasis(child), getPaddingAndBorderAxis(child, mainAxis)); + fmaxf(YGNodeStyleGetFlexBasis(child), YGNodePaddingAndBorderForAxis(child, mainAxis)); } } else if (isMainAxisRow && isRowStyleDimDefined) { // The width is definite, so use that as the flex basis. - child->layout.computedFlexBasis = fmaxf(child->style.dimensions[YGDimensionWidth], - getPaddingAndBorderAxis(child, YGFlexDirectionRow)); + child->layout.computedFlexBasis = + fmaxf(child->style.dimensions[YGDimensionWidth], + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow)); } else if (!isMainAxisRow && isColumnStyleDimDefined) { // The height is definite, so use that as the flex basis. - child->layout.computedFlexBasis = fmaxf(child->style.dimensions[YGDimensionHeight], - getPaddingAndBorderAxis(child, YGFlexDirectionColumn)); + child->layout.computedFlexBasis = + fmaxf(child->style.dimensions[YGDimensionHeight], + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionColumn)); } else { // Compute the flex basis and hypothetical main size (i.e. the clamped // flex basis). @@ -991,13 +1012,13 @@ static void computeChildFlexBasis(const CSSNodeRef node, childHeightMeasureMode = YGMeasureModeUndefined; if (isRowStyleDimDefined) { - childWidth = - child->style.dimensions[YGDimensionWidth] + getMarginAxis(child, YGFlexDirectionRow); + childWidth = child->style.dimensions[YGDimensionWidth] + + YGNodeMarginForAxis(child, YGFlexDirectionRow); childWidthMeasureMode = YGMeasureModeExactly; } if (isColumnStyleDimDefined) { - childHeight = - child->style.dimensions[YGDimensionHeight] + getMarginAxis(child, YGFlexDirectionColumn); + childHeight = child->style.dimensions[YGDimensionHeight] + + YGNodeMarginForAxis(child, YGFlexDirectionColumn); childHeightMeasureMode = YGMeasureModeExactly; } @@ -1005,7 +1026,7 @@ static void computeChildFlexBasis(const CSSNodeRef node, // but all major browsers appear to implement the following logic. if ((!isMainAxisRow && node->style.overflow == YGOverflowScroll) || node->style.overflow != YGOverflowScroll) { - if (CSSValueIsUndefined(childWidth) && !CSSValueIsUndefined(width)) { + if (YGValueIsUndefined(childWidth) && !YGValueIsUndefined(width)) { childWidth = width; childWidthMeasureMode = YGMeasureModeAtMost; } @@ -1013,7 +1034,7 @@ static void computeChildFlexBasis(const CSSNodeRef node, if ((isMainAxisRow && node->style.overflow == YGOverflowScroll) || node->style.overflow != YGOverflowScroll) { - if (CSSValueIsUndefined(childHeight) && !CSSValueIsUndefined(height)) { + if (YGValueIsUndefined(childHeight) && !YGValueIsUndefined(height)) { childHeight = height; childHeightMeasureMode = YGMeasureModeAtMost; } @@ -1022,185 +1043,188 @@ static void computeChildFlexBasis(const CSSNodeRef node, // If child has no defined size in the cross axis and is set to stretch, // set the cross // axis to be measured exactly with the available inner width - if (!isMainAxisRow && !CSSValueIsUndefined(width) && !isRowStyleDimDefined && - widthMode == YGMeasureModeExactly && getAlignItem(node, child) == YGAlignStretch) { + if (!isMainAxisRow && !YGValueIsUndefined(width) && !isRowStyleDimDefined && + widthMode == YGMeasureModeExactly && YGNodeAlignItem(node, child) == YGAlignStretch) { childWidth = width; childWidthMeasureMode = YGMeasureModeExactly; } - if (isMainAxisRow && !CSSValueIsUndefined(height) && !isColumnStyleDimDefined && - heightMode == YGMeasureModeExactly && getAlignItem(node, child) == YGAlignStretch) { + if (isMainAxisRow && !YGValueIsUndefined(height) && !isColumnStyleDimDefined && + heightMode == YGMeasureModeExactly && YGNodeAlignItem(node, child) == YGAlignStretch) { childHeight = height; childHeightMeasureMode = YGMeasureModeExactly; } - if (!CSSValueIsUndefined(child->style.aspectRatio)) { + if (!YGValueIsUndefined(child->style.aspectRatio)) { if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) { child->layout.computedFlexBasis = fmaxf(childWidth * child->style.aspectRatio, - getPaddingAndBorderAxis(child, YGFlexDirectionColumn)); + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionColumn)); return; } else if (isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) { - child->layout.computedFlexBasis = fmaxf(childHeight * child->style.aspectRatio, - getPaddingAndBorderAxis(child, YGFlexDirectionRow)); + child->layout.computedFlexBasis = + fmaxf(childHeight * child->style.aspectRatio, + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow)); return; } } - constrainMaxSizeForMode(child->style.maxDimensions[YGDimensionWidth], - &childWidthMeasureMode, - &childWidth); - constrainMaxSizeForMode(child->style.maxDimensions[YGDimensionHeight], - &childHeightMeasureMode, - &childHeight); + YGConstrainMaxSizeForMode(child->style.maxDimensions[YGDimensionWidth], + &childWidthMeasureMode, + &childWidth); + YGConstrainMaxSizeForMode(child->style.maxDimensions[YGDimensionHeight], + &childHeightMeasureMode, + &childHeight); // Measure the child - layoutNodeInternal(child, - childWidth, - childHeight, - direction, - childWidthMeasureMode, - childHeightMeasureMode, - false, - "measure"); + YGLayoutNodeInternal(child, + childWidth, + childHeight, + direction, + childWidthMeasureMode, + childHeightMeasureMode, + false, + "measure"); child->layout.computedFlexBasis = fmaxf(isMainAxisRow ? child->layout.measuredDimensions[YGDimensionWidth] : child->layout.measuredDimensions[YGDimensionHeight], - getPaddingAndBorderAxis(child, mainAxis)); + YGNodePaddingAndBorderForAxis(child, mainAxis)); } child->layout.computedFlexBasisGeneration = gCurrentGenerationCount; } -static void absoluteLayoutChild(const CSSNodeRef node, - const CSSNodeRef child, - const float width, - const YGMeasureMode widthMode, - const YGDirection direction) { - const YGFlexDirection mainAxis = resolveAxis(node->style.flexDirection, direction); - const YGFlexDirection crossAxis = getCrossFlexDirection(mainAxis, direction); - const bool isMainAxisRow = isRowDirection(mainAxis); +static void YGNodeAbsoluteLayoutChild(const YGNodeRef node, + const YGNodeRef child, + const float width, + const YGMeasureMode widthMode, + const YGDirection direction) { + const YGFlexDirection mainAxis = YGFlexDirectionResolve(node->style.flexDirection, direction); + const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, direction); + const bool isMainAxisRow = YGFlexDirectionIsRow(mainAxis); float childWidth = YGUndefined; float childHeight = YGUndefined; YGMeasureMode childWidthMeasureMode = YGMeasureModeUndefined; YGMeasureMode childHeightMeasureMode = YGMeasureModeUndefined; - if (isStyleDimDefined(child, YGFlexDirectionRow)) { + if (YGNodeIsStyleDimDefined(child, YGFlexDirectionRow)) { childWidth = - child->style.dimensions[YGDimensionWidth] + getMarginAxis(child, YGFlexDirectionRow); + child->style.dimensions[YGDimensionWidth] + YGNodeMarginForAxis(child, YGFlexDirectionRow); } else { // If the child doesn't have a specified width, compute the width based // on the left/right // offsets if they're defined. - if (isLeadingPosDefined(child, YGFlexDirectionRow) && - isTrailingPosDefined(child, YGFlexDirectionRow)) { + if (YGNodeIsLeadingPosDefined(child, YGFlexDirectionRow) && + YGNodeIsTrailingPosDefined(child, YGFlexDirectionRow)) { childWidth = node->layout.measuredDimensions[YGDimensionWidth] - - (getLeadingBorder(node, YGFlexDirectionRow) + - getTrailingBorder(node, YGFlexDirectionRow)) - - (getLeadingPosition(child, YGFlexDirectionRow) + - getTrailingPosition(child, YGFlexDirectionRow)); - childWidth = boundAxis(child, YGFlexDirectionRow, childWidth); + (YGNodeLeadingBorder(node, YGFlexDirectionRow) + + YGNodeTrailingBorder(node, YGFlexDirectionRow)) - + (YGNodeLeadingPosition(child, YGFlexDirectionRow) + + YGNodeTrailingPosition(child, YGFlexDirectionRow)); + childWidth = YGNodeBoundAxis(child, YGFlexDirectionRow, childWidth); } } - if (isStyleDimDefined(child, YGFlexDirectionColumn)) { - childHeight = - child->style.dimensions[YGDimensionHeight] + getMarginAxis(child, YGFlexDirectionColumn); + if (YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn)) { + childHeight = child->style.dimensions[YGDimensionHeight] + + YGNodeMarginForAxis(child, YGFlexDirectionColumn); } else { // If the child doesn't have a specified height, compute the height // based on the top/bottom // offsets if they're defined. - if (isLeadingPosDefined(child, YGFlexDirectionColumn) && - isTrailingPosDefined(child, YGFlexDirectionColumn)) { + if (YGNodeIsLeadingPosDefined(child, YGFlexDirectionColumn) && + YGNodeIsTrailingPosDefined(child, YGFlexDirectionColumn)) { childHeight = node->layout.measuredDimensions[YGDimensionHeight] - - (getLeadingBorder(node, YGFlexDirectionColumn) + - getTrailingBorder(node, YGFlexDirectionColumn)) - - (getLeadingPosition(child, YGFlexDirectionColumn) + - getTrailingPosition(child, YGFlexDirectionColumn)); - childHeight = boundAxis(child, YGFlexDirectionColumn, childHeight); + (YGNodeLeadingBorder(node, YGFlexDirectionColumn) + + YGNodeTrailingBorder(node, YGFlexDirectionColumn)) - + (YGNodeLeadingPosition(child, YGFlexDirectionColumn) + + YGNodeTrailingPosition(child, YGFlexDirectionColumn)); + childHeight = YGNodeBoundAxis(child, YGFlexDirectionColumn, childHeight); } } // Exactly one dimension needs to be defined for us to be able to do aspect ratio // calculation. One dimension being the anchor and the other being flexible. - if (CSSValueIsUndefined(childWidth) ^ CSSValueIsUndefined(childHeight)) { - if (!CSSValueIsUndefined(child->style.aspectRatio)) { - if (CSSValueIsUndefined(childWidth)) { + if (YGValueIsUndefined(childWidth) ^ YGValueIsUndefined(childHeight)) { + if (!YGValueIsUndefined(child->style.aspectRatio)) { + if (YGValueIsUndefined(childWidth)) { childWidth = fmaxf(childHeight * child->style.aspectRatio, - getPaddingAndBorderAxis(child, YGFlexDirectionColumn)); - } else if (CSSValueIsUndefined(childHeight)) { + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionColumn)); + } else if (YGValueIsUndefined(childHeight)) { childHeight = fmaxf(childWidth * child->style.aspectRatio, - getPaddingAndBorderAxis(child, YGFlexDirectionRow)); + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow)); } } } // If we're still missing one or the other dimension, measure the content. - if (CSSValueIsUndefined(childWidth) || CSSValueIsUndefined(childHeight)) { + if (YGValueIsUndefined(childWidth) || YGValueIsUndefined(childHeight)) { childWidthMeasureMode = - CSSValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly; + YGValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly; childHeightMeasureMode = - CSSValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly; + YGValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly; // According to the spec, if the main size is not definite and the // child's inline axis is parallel to the main axis (i.e. it's // horizontal), the child should be sized using "UNDEFINED" in // the main size. Otherwise use "AT_MOST" in the cross axis. - if (!isMainAxisRow && CSSValueIsUndefined(childWidth) && widthMode != YGMeasureModeUndefined) { + if (!isMainAxisRow && YGValueIsUndefined(childWidth) && widthMode != YGMeasureModeUndefined) { childWidth = width; childWidthMeasureMode = YGMeasureModeAtMost; } - layoutNodeInternal(child, + YGLayoutNodeInternal(child, + childWidth, + childHeight, + direction, + childWidthMeasureMode, + childHeightMeasureMode, + false, + "abs-measure"); + childWidth = child->layout.measuredDimensions[YGDimensionWidth] + + YGNodeMarginForAxis(child, YGFlexDirectionRow); + childHeight = child->layout.measuredDimensions[YGDimensionHeight] + + YGNodeMarginForAxis(child, YGFlexDirectionColumn); + } + + YGLayoutNodeInternal(child, childWidth, childHeight, direction, - childWidthMeasureMode, - childHeightMeasureMode, - false, - "abs-measure"); - childWidth = child->layout.measuredDimensions[YGDimensionWidth] + - getMarginAxis(child, YGFlexDirectionRow); - childHeight = child->layout.measuredDimensions[YGDimensionHeight] + - getMarginAxis(child, YGFlexDirectionColumn); - } + YGMeasureModeExactly, + YGMeasureModeExactly, + true, + "abs-layout"); - layoutNodeInternal(child, - childWidth, - childHeight, - direction, - YGMeasureModeExactly, - YGMeasureModeExactly, - true, - "abs-layout"); - - if (isTrailingPosDefined(child, mainAxis) && !isLeadingPosDefined(child, mainAxis)) { + if (YGNodeIsTrailingPosDefined(child, mainAxis) && !YGNodeIsLeadingPosDefined(child, mainAxis)) { child->layout.position[leading[mainAxis]] = node->layout.measuredDimensions[dim[mainAxis]] - child->layout.measuredDimensions[dim[mainAxis]] - - getTrailingBorder(node, mainAxis) - - getTrailingPosition(child, mainAxis); + YGNodeTrailingBorder(node, mainAxis) - + YGNodeTrailingPosition(child, mainAxis); } - if (isTrailingPosDefined(child, crossAxis) && !isLeadingPosDefined(child, crossAxis)) { + if (YGNodeIsTrailingPosDefined(child, crossAxis) && + !YGNodeIsLeadingPosDefined(child, crossAxis)) { child->layout.position[leading[crossAxis]] = node->layout.measuredDimensions[dim[crossAxis]] - child->layout.measuredDimensions[dim[crossAxis]] - - getTrailingBorder(node, crossAxis) - - getTrailingPosition(child, crossAxis); + YGNodeTrailingBorder(node, crossAxis) - + YGNodeTrailingPosition(child, crossAxis); } } -static void setMeasuredDimensionsForNodeWithMeasureFunc(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode) { - CSS_ASSERT(node->measure, "Expected node to have custom measure function"); +static void YGNodeWithMeasureFuncSetMeasuredDimensions(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode) { + YG_ASSERT(node->measure, "Expected node to have custom measure function"); - const float paddingAndBorderAxisRow = getPaddingAndBorderAxis(node, YGFlexDirectionRow); - const float paddingAndBorderAxisColumn = getPaddingAndBorderAxis(node, YGFlexDirectionColumn); - const float marginAxisRow = getMarginAxis(node, YGFlexDirectionRow); - const float marginAxisColumn = getMarginAxis(node, YGFlexDirectionColumn); + const float paddingAndBorderAxisRow = YGNodePaddingAndBorderForAxis(node, YGFlexDirectionRow); + const float paddingAndBorderAxisColumn = + YGNodePaddingAndBorderForAxis(node, YGFlexDirectionColumn); + const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow); + const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn); const float innerWidth = availableWidth - marginAxisRow - paddingAndBorderAxisRow; const float innerHeight = availableHeight - marginAxisColumn - paddingAndBorderAxisColumn; @@ -1208,88 +1232,91 @@ static void setMeasuredDimensionsForNodeWithMeasureFunc(const CSSNodeRef node, if (widthMeasureMode == YGMeasureModeExactly && heightMeasureMode == YGMeasureModeExactly) { // Don't bother sizing the text if both dimensions are already defined. node->layout.measuredDimensions[YGDimensionWidth] = - boundAxis(node, YGFlexDirectionRow, availableWidth - marginAxisRow); + YGNodeBoundAxis(node, YGFlexDirectionRow, availableWidth - marginAxisRow); node->layout.measuredDimensions[YGDimensionHeight] = - boundAxis(node, YGFlexDirectionColumn, availableHeight - marginAxisColumn); + YGNodeBoundAxis(node, YGFlexDirectionColumn, availableHeight - marginAxisColumn); } else if (innerWidth <= 0 || innerHeight <= 0) { // Don't bother sizing the text if there's no horizontal or vertical // space. - node->layout.measuredDimensions[YGDimensionWidth] = boundAxis(node, YGFlexDirectionRow, 0); - node->layout.measuredDimensions[YGDimensionHeight] = boundAxis(node, YGFlexDirectionColumn, 0); + node->layout.measuredDimensions[YGDimensionWidth] = + YGNodeBoundAxis(node, YGFlexDirectionRow, 0); + node->layout.measuredDimensions[YGDimensionHeight] = + YGNodeBoundAxis(node, YGFlexDirectionColumn, 0); } else { // Measure the text under the current constraints. - const CSSSize measuredSize = + const YGSize measuredSize = node->measure(node, innerWidth, widthMeasureMode, innerHeight, heightMeasureMode); node->layout.measuredDimensions[YGDimensionWidth] = - boundAxis(node, - YGFlexDirectionRow, - (widthMeasureMode == YGMeasureModeUndefined || - widthMeasureMode == YGMeasureModeAtMost) - ? measuredSize.width + paddingAndBorderAxisRow - : availableWidth - marginAxisRow); + YGNodeBoundAxis(node, + YGFlexDirectionRow, + (widthMeasureMode == YGMeasureModeUndefined || + widthMeasureMode == YGMeasureModeAtMost) + ? measuredSize.width + paddingAndBorderAxisRow + : availableWidth - marginAxisRow); node->layout.measuredDimensions[YGDimensionHeight] = - boundAxis(node, - YGFlexDirectionColumn, - (heightMeasureMode == YGMeasureModeUndefined || - heightMeasureMode == YGMeasureModeAtMost) - ? measuredSize.height + paddingAndBorderAxisColumn - : availableHeight - marginAxisColumn); + YGNodeBoundAxis(node, + YGFlexDirectionColumn, + (heightMeasureMode == YGMeasureModeUndefined || + heightMeasureMode == YGMeasureModeAtMost) + ? measuredSize.height + paddingAndBorderAxisColumn + : availableHeight - marginAxisColumn); } } // For nodes with no children, use the available values if they were provided, // or the minimum size as indicated by the padding and border sizes. -static void setMeasuredDimensionsForEmptyContainer(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode) { - const float paddingAndBorderAxisRow = getPaddingAndBorderAxis(node, YGFlexDirectionRow); - const float paddingAndBorderAxisColumn = getPaddingAndBorderAxis(node, YGFlexDirectionColumn); - const float marginAxisRow = getMarginAxis(node, YGFlexDirectionRow); - const float marginAxisColumn = getMarginAxis(node, YGFlexDirectionColumn); +static void YGNodeEmptyContainerSetMeasuredDimensions(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode) { + const float paddingAndBorderAxisRow = YGNodePaddingAndBorderForAxis(node, YGFlexDirectionRow); + const float paddingAndBorderAxisColumn = + YGNodePaddingAndBorderForAxis(node, YGFlexDirectionColumn); + const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow); + const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn); node->layout.measuredDimensions[YGDimensionWidth] = - boundAxis(node, - YGFlexDirectionRow, - (widthMeasureMode == YGMeasureModeUndefined || - widthMeasureMode == YGMeasureModeAtMost) - ? paddingAndBorderAxisRow - : availableWidth - marginAxisRow); + YGNodeBoundAxis(node, + YGFlexDirectionRow, + (widthMeasureMode == YGMeasureModeUndefined || + widthMeasureMode == YGMeasureModeAtMost) + ? paddingAndBorderAxisRow + : availableWidth - marginAxisRow); node->layout.measuredDimensions[YGDimensionHeight] = - boundAxis(node, - YGFlexDirectionColumn, - (heightMeasureMode == YGMeasureModeUndefined || - heightMeasureMode == YGMeasureModeAtMost) - ? paddingAndBorderAxisColumn - : availableHeight - marginAxisColumn); + YGNodeBoundAxis(node, + YGFlexDirectionColumn, + (heightMeasureMode == YGMeasureModeUndefined || + heightMeasureMode == YGMeasureModeAtMost) + ? paddingAndBorderAxisColumn + : availableHeight - marginAxisColumn); } -static bool setMeasuredDimensionsIfEmptyOrFixedSize(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode) { +static bool YGNodeFixedSizeSetMeasuredDimensions(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode) { if ((widthMeasureMode == YGMeasureModeAtMost && availableWidth <= 0) || (heightMeasureMode == YGMeasureModeAtMost && availableHeight <= 0) || (widthMeasureMode == YGMeasureModeExactly && heightMeasureMode == YGMeasureModeExactly)) { - const float marginAxisColumn = getMarginAxis(node, YGFlexDirectionColumn); - const float marginAxisRow = getMarginAxis(node, YGFlexDirectionRow); + const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn); + const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow); node->layout.measuredDimensions[YGDimensionWidth] = - boundAxis(node, - YGFlexDirectionRow, - CSSValueIsUndefined(availableWidth) || (widthMeasureMode == YGMeasureModeAtMost && availableWidth < 0) - ? 0 - : availableWidth - marginAxisRow); + YGNodeBoundAxis(node, + YGFlexDirectionRow, + YGValueIsUndefined(availableWidth) || (widthMeasureMode == YGMeasureModeAtMost && availableWidth < 0) + ? 0 + : availableWidth - marginAxisRow); node->layout.measuredDimensions[YGDimensionHeight] = - boundAxis(node, - YGFlexDirectionColumn, - CSSValueIsUndefined(availableHeight) || (heightMeasureMode == YGMeasureModeAtMost && availableHeight < 0) - ? 0 - : availableHeight - marginAxisColumn); + YGNodeBoundAxis(node, + YGFlexDirectionColumn, + YGValueIsUndefined(availableHeight) || (heightMeasureMode == YGMeasureModeAtMost && availableHeight < 0) + ? 0 + : availableHeight - marginAxisColumn); return true; } @@ -1300,7 +1327,7 @@ static bool setMeasuredDimensionsIfEmptyOrFixedSize(const CSSNodeRef node, // // This is the main routine that implements a subset of the flexbox layout // algorithm -// described in the W3C CSS documentation: https://www.w3.org/TR/css3-flexbox/. +// described in the W3C YG documentation: https://www.w3.org/TR/YG3-flexbox/. // // Limitations of this algorithm, compared to the full standard: // * Display property is always assumed to be 'flex' except for Text nodes, @@ -1399,45 +1426,44 @@ static bool setMeasuredDimensionsIfEmptyOrFixedSize(const CSSNodeRef node, // support default // minimum main sizes (see above for details). Each of our measure modes maps // to a layout mode -// from the spec (https://www.w3.org/TR/css3-sizing/#terms): +// from the spec (https://www.w3.org/TR/YG3-sizing/#terms): // - YGMeasureModeUndefined: max content // - YGMeasureModeExactly: fill available // - YGMeasureModeAtMost: fit content // -// When calling layoutNodeImpl and layoutNodeInternal, if the caller passes +// When calling YGNodelayoutImpl and YGLayoutNodeInternal, if the caller passes // an available size of // undefined then it must also pass a measure mode of YGMeasureModeUndefined // in that dimension. // -static void layoutNodeImpl(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGDirection parentDirection, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, - const bool performLayout) { - CSS_ASSERT(CSSValueIsUndefined(availableWidth) ? widthMeasureMode == YGMeasureModeUndefined - : true, - "availableWidth is indefinite so widthMeasureMode must be " - "YGMeasureModeUndefined"); - CSS_ASSERT(CSSValueIsUndefined(availableHeight) ? heightMeasureMode == YGMeasureModeUndefined - : true, - "availableHeight is indefinite so heightMeasureMode must be " - "YGMeasureModeUndefined"); +static void YGNodelayoutImpl(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGDirection parentDirection, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode, + const bool performLayout) { + YG_ASSERT(YGValueIsUndefined(availableWidth) ? widthMeasureMode == YGMeasureModeUndefined : true, + "availableWidth is indefinite so widthMeasureMode must be " + "YGMeasureModeUndefined"); + YG_ASSERT(YGValueIsUndefined(availableHeight) ? heightMeasureMode == YGMeasureModeUndefined + : true, + "availableHeight is indefinite so heightMeasureMode must be " + "YGMeasureModeUndefined"); // Set the resolved resolution in the node's layout. - const YGDirection direction = resolveDirection(node, parentDirection); + const YGDirection direction = YGNodeResolveDirection(node, parentDirection); node->layout.direction = direction; if (node->measure) { - setMeasuredDimensionsForNodeWithMeasureFunc( + YGNodeWithMeasureFuncSetMeasuredDimensions( node, availableWidth, availableHeight, widthMeasureMode, heightMeasureMode); return; } - const uint32_t childCount = CSSNodeListCount(node->children); + const uint32_t childCount = YGNodeListCount(node->children); if (childCount == 0) { - setMeasuredDimensionsForEmptyContainer( + YGNodeEmptyContainerSetMeasuredDimensions( node, availableWidth, availableHeight, widthMeasureMode, heightMeasureMode); return; } @@ -1445,34 +1471,35 @@ static void layoutNodeImpl(const CSSNodeRef node, // If we're not being asked to perform a full layout we can skip the algorithm if we already know // the size if (!performLayout && - setMeasuredDimensionsIfEmptyOrFixedSize( + YGNodeFixedSizeSetMeasuredDimensions( node, availableWidth, availableHeight, widthMeasureMode, heightMeasureMode)) { return; } // STEP 1: CALCULATE VALUES FOR REMAINDER OF ALGORITHM - const YGFlexDirection mainAxis = resolveAxis(node->style.flexDirection, direction); - const YGFlexDirection crossAxis = getCrossFlexDirection(mainAxis, direction); - const bool isMainAxisRow = isRowDirection(mainAxis); + const YGFlexDirection mainAxis = YGFlexDirectionResolve(node->style.flexDirection, direction); + const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, direction); + const bool isMainAxisRow = YGFlexDirectionIsRow(mainAxis); const YGJustify justifyContent = node->style.justifyContent; const bool isNodeFlexWrap = node->style.flexWrap == YGWrapWrap; - CSSNodeRef firstAbsoluteChild = NULL; - CSSNodeRef currentAbsoluteChild = NULL; + YGNodeRef firstAbsoluteChild = NULL; + YGNodeRef currentAbsoluteChild = NULL; - const float leadingPaddingAndBorderMain = getLeadingPaddingAndBorder(node, mainAxis); - const float trailingPaddingAndBorderMain = getTrailingPaddingAndBorder(node, mainAxis); - const float leadingPaddingAndBorderCross = getLeadingPaddingAndBorder(node, crossAxis); - const float paddingAndBorderAxisMain = getPaddingAndBorderAxis(node, mainAxis); - const float paddingAndBorderAxisCross = getPaddingAndBorderAxis(node, crossAxis); + const float leadingPaddingAndBorderMain = YGNodeLeadingPaddingAndBorder(node, mainAxis); + const float trailingPaddingAndBorderMain = YGNodeTrailingPaddingAndBorder(node, mainAxis); + const float leadingPaddingAndBorderCross = YGNodeLeadingPaddingAndBorder(node, crossAxis); + const float paddingAndBorderAxisMain = YGNodePaddingAndBorderForAxis(node, mainAxis); + const float paddingAndBorderAxisCross = YGNodePaddingAndBorderForAxis(node, crossAxis); const YGMeasureMode measureModeMainDim = isMainAxisRow ? widthMeasureMode : heightMeasureMode; const YGMeasureMode measureModeCrossDim = isMainAxisRow ? heightMeasureMode : widthMeasureMode; - const float paddingAndBorderAxisRow = getPaddingAndBorderAxis(node, YGFlexDirectionRow); - const float paddingAndBorderAxisColumn = getPaddingAndBorderAxis(node, YGFlexDirectionColumn); - const float marginAxisRow = getMarginAxis(node, YGFlexDirectionRow); - const float marginAxisColumn = getMarginAxis(node, YGFlexDirectionColumn); + const float paddingAndBorderAxisRow = YGNodePaddingAndBorderForAxis(node, YGFlexDirectionRow); + const float paddingAndBorderAxisColumn = + YGNodePaddingAndBorderForAxis(node, YGFlexDirectionColumn); + const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow); + const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn); // STEP 2: DETERMINE AVAILABLE SIZE IN MAIN AND CROSS DIRECTIONS const float availableInnerWidth = availableWidth - marginAxisRow - paddingAndBorderAxisRow; @@ -1484,18 +1511,18 @@ static void layoutNodeImpl(const CSSNodeRef node, // If there is only one child with flexGrow + flexShrink it means we can set the // computedFlexBasis to 0 instead of measuring and shrinking / flexing the child to exactly // match the remaining space - CSSNodeRef singleFlexChild = NULL; + YGNodeRef singleFlexChild = NULL; if ((isMainAxisRow && widthMeasureMode == YGMeasureModeExactly) || (!isMainAxisRow && heightMeasureMode == YGMeasureModeExactly)) { for (uint32_t i = 0; i < childCount; i++) { - const CSSNodeRef child = CSSNodeGetChild(node, i); + const YGNodeRef child = YGNodeGetChild(node, i); if (singleFlexChild) { - if (isFlex(child)) { + if (YGNodeIsFlex(child)) { // There is already a flexible child, abort. singleFlexChild = NULL; break; } - } else if (CSSNodeStyleGetFlexGrow(child) > 0 && CSSNodeStyleGetFlexShrink(child) > 0) { + } else if (YGNodeStyleGetFlexGrow(child) > 0 && YGNodeStyleGetFlexShrink(child) > 0) { singleFlexChild = child; } } @@ -1503,12 +1530,12 @@ static void layoutNodeImpl(const CSSNodeRef node, // STEP 3: DETERMINE FLEX BASIS FOR EACH ITEM for (uint32_t i = 0; i < childCount; i++) { - const CSSNodeRef child = CSSNodeListGet(node->children, i); + const YGNodeRef child = YGNodeListGet(node->children, i); if (performLayout) { // Set the initial position (relative to the parent). - const YGDirection childDirection = resolveDirection(child, direction); - setPosition(child, childDirection); + const YGDirection childDirection = YGNodeResolveDirection(child, direction); + YGNodeSetPosition(child, childDirection); } // Absolute-positioned children don't participate in flex layout. Add them @@ -1529,13 +1556,13 @@ static void layoutNodeImpl(const CSSNodeRef node, child->layout.computedFlexBasisGeneration = gCurrentGenerationCount; child->layout.computedFlexBasis = 0; } else { - computeChildFlexBasis(node, - child, - availableInnerWidth, - widthMeasureMode, - availableInnerHeight, - heightMeasureMode, - direction); + YGNodeComputeFlexBasisForChild(node, + child, + availableInnerWidth, + widthMeasureMode, + availableInnerHeight, + heightMeasureMode, + direction); } } } @@ -1572,17 +1599,17 @@ static void layoutNodeImpl(const CSSNodeRef node, float totalFlexShrinkScaledFactors = 0; // Maintain a linked list of the child nodes that can shrink and/or grow. - CSSNodeRef firstRelativeChild = NULL; - CSSNodeRef currentRelativeChild = NULL; + YGNodeRef firstRelativeChild = NULL; + YGNodeRef currentRelativeChild = NULL; // Add items to the current line until it's full or we run out of items. for (uint32_t i = startOfLineIndex; i < childCount; i++, endOfLineIndex++) { - const CSSNodeRef child = CSSNodeListGet(node->children, i); + const YGNodeRef child = YGNodeListGet(node->children, i); child->lineIndex = lineCount; if (child->style.positionType != YGPositionTypeAbsolute) { const float outerFlexBasis = - child->layout.computedFlexBasis + getMarginAxis(child, mainAxis); + child->layout.computedFlexBasis + YGNodeMarginForAxis(child, mainAxis); // If this is a multi-line flow and this item pushes us over the // available size, we've @@ -1596,14 +1623,14 @@ static void layoutNodeImpl(const CSSNodeRef node, sizeConsumedOnCurrentLine += outerFlexBasis; itemsOnLine++; - if (isFlex(child)) { - totalFlexGrowFactors += CSSNodeStyleGetFlexGrow(child); + if (YGNodeIsFlex(child)) { + totalFlexGrowFactors += YGNodeStyleGetFlexGrow(child); // Unlike the grow factor, the shrink factor is scaled relative to the // child // dimension. totalFlexShrinkScaledFactors += - -CSSNodeStyleGetFlexShrink(child) * child->layout.computedFlexBasis; + -YGNodeStyleGetFlexShrink(child) * child->layout.computedFlexBasis; } // Store a private linked list of children that need to be layed out. @@ -1633,7 +1660,7 @@ static void layoutNodeImpl(const CSSNodeRef node, // If the main dimension size isn't known, it is computed based on // the line length, so there's no more space left to distribute. float remainingFreeSpace = 0; - if (!CSSValueIsUndefined(availableInnerMainDim)) { + if (!YGValueIsUndefined(availableInnerMainDim)) { remainingFreeSpace = availableInnerMainDim - sizeConsumedOnCurrentLine; } else if (sizeConsumedOnCurrentLine < 0) { // availableInnerMainDim is indefinite which means the node is being sized @@ -1670,7 +1697,7 @@ static void layoutNodeImpl(const CSSNodeRef node, // // This two pass approach for resolving min/max constraints deviates from // the spec. The - // spec (https://www.w3.org/TR/css-flexbox-1/#resolve-flexible-lengths) + // spec (https://www.w3.org/TR/YG-flexbox-1/#resolve-flexible-lengths) // describes a process // that needs to be repeated a variable number of times. The algorithm // implemented here @@ -1686,15 +1713,14 @@ static void layoutNodeImpl(const CSSNodeRef node, childFlexBasis = currentRelativeChild->layout.computedFlexBasis; if (remainingFreeSpace < 0) { - flexShrinkScaledFactor = - -CSSNodeStyleGetFlexShrink(currentRelativeChild) * childFlexBasis; + flexShrinkScaledFactor = -YGNodeStyleGetFlexShrink(currentRelativeChild) * childFlexBasis; // Is this child able to shrink? if (flexShrinkScaledFactor != 0) { baseMainSize = childFlexBasis + remainingFreeSpace / totalFlexShrinkScaledFactors * flexShrinkScaledFactor; - boundMainSize = boundAxis(currentRelativeChild, mainAxis, baseMainSize); + boundMainSize = YGNodeBoundAxis(currentRelativeChild, mainAxis, baseMainSize); if (baseMainSize != boundMainSize) { // By excluding this item's size and flex factor from remaining, // this item's @@ -1707,13 +1733,13 @@ static void layoutNodeImpl(const CSSNodeRef node, } } } else if (remainingFreeSpace > 0) { - flexGrowFactor = CSSNodeStyleGetFlexGrow(currentRelativeChild); + flexGrowFactor = YGNodeStyleGetFlexGrow(currentRelativeChild); // Is this child able to grow? if (flexGrowFactor != 0) { baseMainSize = childFlexBasis + remainingFreeSpace / totalFlexGrowFactors * flexGrowFactor; - boundMainSize = boundAxis(currentRelativeChild, mainAxis, baseMainSize); + boundMainSize = YGNodeBoundAxis(currentRelativeChild, mainAxis, baseMainSize); if (baseMainSize != boundMainSize) { // By excluding this item's size and flex factor from remaining, // this item's @@ -1742,8 +1768,7 @@ static void layoutNodeImpl(const CSSNodeRef node, float updatedMainSize = childFlexBasis; if (remainingFreeSpace < 0) { - flexShrinkScaledFactor = - -CSSNodeStyleGetFlexShrink(currentRelativeChild) * childFlexBasis; + flexShrinkScaledFactor = -YGNodeStyleGetFlexShrink(currentRelativeChild) * childFlexBasis; // Is this child able to shrink? if (flexShrinkScaledFactor != 0) { float childSize; @@ -1756,18 +1781,18 @@ static void layoutNodeImpl(const CSSNodeRef node, (remainingFreeSpace / totalFlexShrinkScaledFactors) * flexShrinkScaledFactor; } - updatedMainSize = boundAxis(currentRelativeChild, mainAxis, childSize); + updatedMainSize = YGNodeBoundAxis(currentRelativeChild, mainAxis, childSize); } } else if (remainingFreeSpace > 0) { - flexGrowFactor = CSSNodeStyleGetFlexGrow(currentRelativeChild); + flexGrowFactor = YGNodeStyleGetFlexGrow(currentRelativeChild); // Is this child able to grow? if (flexGrowFactor != 0) { updatedMainSize = - boundAxis(currentRelativeChild, - mainAxis, - childFlexBasis + - remainingFreeSpace / totalFlexGrowFactors * flexGrowFactor); + YGNodeBoundAxis(currentRelativeChild, + mainAxis, + childFlexBasis + + remainingFreeSpace / totalFlexGrowFactors * flexGrowFactor); } } @@ -1779,80 +1804,82 @@ static void layoutNodeImpl(const CSSNodeRef node, YGMeasureMode childHeightMeasureMode; if (isMainAxisRow) { - childWidth = updatedMainSize + getMarginAxis(currentRelativeChild, YGFlexDirectionRow); + childWidth = + updatedMainSize + YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionRow); childWidthMeasureMode = YGMeasureModeExactly; - if (!CSSValueIsUndefined(availableInnerCrossDim) && - !isStyleDimDefined(currentRelativeChild, YGFlexDirectionColumn) && + if (!YGValueIsUndefined(availableInnerCrossDim) && + !YGNodeIsStyleDimDefined(currentRelativeChild, YGFlexDirectionColumn) && heightMeasureMode == YGMeasureModeExactly && - getAlignItem(node, currentRelativeChild) == YGAlignStretch) { + YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch) { childHeight = availableInnerCrossDim; childHeightMeasureMode = YGMeasureModeExactly; - } else if (!isStyleDimDefined(currentRelativeChild, YGFlexDirectionColumn)) { + } else if (!YGNodeIsStyleDimDefined(currentRelativeChild, YGFlexDirectionColumn)) { childHeight = availableInnerCrossDim; childHeightMeasureMode = - CSSValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeAtMost; + YGValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeAtMost; } else { childHeight = currentRelativeChild->style.dimensions[YGDimensionHeight] + - getMarginAxis(currentRelativeChild, YGFlexDirectionColumn); + YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionColumn); childHeightMeasureMode = YGMeasureModeExactly; } } else { childHeight = - updatedMainSize + getMarginAxis(currentRelativeChild, YGFlexDirectionColumn); + updatedMainSize + YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionColumn); childHeightMeasureMode = YGMeasureModeExactly; - if (!CSSValueIsUndefined(availableInnerCrossDim) && - !isStyleDimDefined(currentRelativeChild, YGFlexDirectionRow) && + if (!YGValueIsUndefined(availableInnerCrossDim) && + !YGNodeIsStyleDimDefined(currentRelativeChild, YGFlexDirectionRow) && widthMeasureMode == YGMeasureModeExactly && - getAlignItem(node, currentRelativeChild) == YGAlignStretch) { + YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch) { childWidth = availableInnerCrossDim; childWidthMeasureMode = YGMeasureModeExactly; - } else if (!isStyleDimDefined(currentRelativeChild, YGFlexDirectionRow)) { + } else if (!YGNodeIsStyleDimDefined(currentRelativeChild, YGFlexDirectionRow)) { childWidth = availableInnerCrossDim; childWidthMeasureMode = - CSSValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeAtMost; + YGValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeAtMost; } else { childWidth = currentRelativeChild->style.dimensions[YGDimensionWidth] + - getMarginAxis(currentRelativeChild, YGFlexDirectionRow); + YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionRow); childWidthMeasureMode = YGMeasureModeExactly; } } - if (!CSSValueIsUndefined(currentRelativeChild->style.aspectRatio)) { + if (!YGValueIsUndefined(currentRelativeChild->style.aspectRatio)) { if (isMainAxisRow && childHeightMeasureMode != YGMeasureModeExactly) { childHeight = fmaxf(childWidth * currentRelativeChild->style.aspectRatio, - getPaddingAndBorderAxis(currentRelativeChild, YGFlexDirectionColumn)); + YGNodePaddingAndBorderForAxis(currentRelativeChild, YGFlexDirectionColumn)); childHeightMeasureMode = YGMeasureModeExactly; } else if (!isMainAxisRow && childWidthMeasureMode != YGMeasureModeExactly) { - childWidth = fmaxf(childHeight * currentRelativeChild->style.aspectRatio, - getPaddingAndBorderAxis(currentRelativeChild, YGFlexDirectionRow)); + childWidth = + fmaxf(childHeight * currentRelativeChild->style.aspectRatio, + YGNodePaddingAndBorderForAxis(currentRelativeChild, YGFlexDirectionRow)); childWidthMeasureMode = YGMeasureModeExactly; } } - constrainMaxSizeForMode(currentRelativeChild->style.maxDimensions[YGDimensionWidth], - &childWidthMeasureMode, - &childWidth); - constrainMaxSizeForMode(currentRelativeChild->style.maxDimensions[YGDimensionHeight], - &childHeightMeasureMode, - &childHeight); + YGConstrainMaxSizeForMode(currentRelativeChild->style.maxDimensions[YGDimensionWidth], + &childWidthMeasureMode, + &childWidth); + YGConstrainMaxSizeForMode(currentRelativeChild->style.maxDimensions[YGDimensionHeight], + &childHeightMeasureMode, + &childHeight); const bool requiresStretchLayout = - !isStyleDimDefined(currentRelativeChild, crossAxis) && - getAlignItem(node, currentRelativeChild) == YGAlignStretch; + !YGNodeIsStyleDimDefined(currentRelativeChild, crossAxis) && + YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch; // Recursively call the layout algorithm for this child with the updated // main size. - layoutNodeInternal(currentRelativeChild, - childWidth, - childHeight, - direction, - childWidthMeasureMode, - childHeightMeasureMode, - performLayout && !requiresStretchLayout, - "flex"); + YGLayoutNodeInternal(currentRelativeChild, + childWidth, + childHeight, + direction, + childWidthMeasureMode, + childHeightMeasureMode, + performLayout && !requiresStretchLayout, + "flex"); currentRelativeChild = currentRelativeChild->nextChild; } @@ -1873,7 +1900,7 @@ static void layoutNodeImpl(const CSSNodeRef node, // constraint by the min size defined for the main axis. if (measureModeMainDim == YGMeasureModeAtMost && remainingFreeSpace > 0) { - if (!CSSValueIsUndefined(node->style.minDimensions[dim[mainAxis]]) && + if (!YGValueIsUndefined(node->style.minDimensions[dim[mainAxis]]) && node->style.minDimensions[dim[mainAxis]] >= 0) { remainingFreeSpace = fmaxf(0, node->style.minDimensions[dim[mainAxis]] - @@ -1911,17 +1938,17 @@ static void layoutNodeImpl(const CSSNodeRef node, float crossDim = 0; for (uint32_t i = startOfLineIndex; i < endOfLineIndex; i++) { - const CSSNodeRef child = CSSNodeListGet(node->children, i); + const YGNodeRef child = YGNodeListGet(node->children, i); if (child->style.positionType == YGPositionTypeAbsolute && - isLeadingPosDefined(child, mainAxis)) { + YGNodeIsLeadingPosDefined(child, mainAxis)) { if (performLayout) { // In case the child is position absolute and has left/top being // defined, we override the position to whatever the user said // (and margin/border). - child->layout.position[pos[mainAxis]] = getLeadingPosition(child, mainAxis) + - getLeadingBorder(node, mainAxis) + - getLeadingMargin(child, mainAxis); + child->layout.position[pos[mainAxis]] = YGNodeLeadingPosition(child, mainAxis) + + YGNodeLeadingBorder(node, mainAxis) + + YGNodeLeadingMargin(child, mainAxis); } } else { // Now that we placed the element, we need to update the variables. @@ -1935,23 +1962,23 @@ static void layoutNodeImpl(const CSSNodeRef node, if (canSkipFlex) { // If we skipped the flex step, then we can't rely on the // measuredDims because - // they weren't computed. This means we can't call getDimWithMargin. - mainDim += - betweenMainDim + getMarginAxis(child, mainAxis) + child->layout.computedFlexBasis; + // they weren't computed. This means we can't call YGNodeDimWithMargin. + mainDim += betweenMainDim + YGNodeMarginForAxis(child, mainAxis) + + child->layout.computedFlexBasis; crossDim = availableInnerCrossDim; } else { // The main dimension is the sum of all the elements dimension plus // the spacing. - mainDim += betweenMainDim + getDimWithMargin(child, mainAxis); + mainDim += betweenMainDim + YGNodeDimWithMargin(child, mainAxis); // The cross dimension is the max of the elements dimension since // there // can only be one element in that cross dimension. - crossDim = fmaxf(crossDim, getDimWithMargin(child, crossAxis)); + crossDim = fmaxf(crossDim, YGNodeDimWithMargin(child, crossAxis)); } } else if (performLayout) { child->layout.position[pos[mainAxis]] += - getLeadingBorder(node, mainAxis) + leadingMainDim; + YGNodeLeadingBorder(node, mainAxis) + leadingMainDim; } } } @@ -1962,7 +1989,7 @@ static void layoutNodeImpl(const CSSNodeRef node, if (measureModeCrossDim == YGMeasureModeUndefined || measureModeCrossDim == YGMeasureModeAtMost) { // Compute the cross axis from the max cross dimension of the children. - containerCrossAxis = boundAxis(node, crossAxis, crossDim + paddingAndBorderAxisCross) - + containerCrossAxis = YGNodeBoundAxis(node, crossAxis, crossDim + paddingAndBorderAxisCross) - paddingAndBorderAxisCross; if (measureModeCrossDim == YGMeasureModeAtMost) { @@ -1976,27 +2003,27 @@ static void layoutNodeImpl(const CSSNodeRef node, } // Clamp to the min/max size specified on the container. - crossDim = boundAxis(node, crossAxis, crossDim + paddingAndBorderAxisCross) - + crossDim = YGNodeBoundAxis(node, crossAxis, crossDim + paddingAndBorderAxisCross) - paddingAndBorderAxisCross; // STEP 7: CROSS-AXIS ALIGNMENT // We can skip child alignment if we're just measuring the container. if (performLayout) { for (uint32_t i = startOfLineIndex; i < endOfLineIndex; i++) { - const CSSNodeRef child = CSSNodeListGet(node->children, i); + const YGNodeRef child = YGNodeListGet(node->children, i); if (child->style.positionType == YGPositionTypeAbsolute) { // If the child is absolutely positioned and has a // top/left/bottom/right // set, override all the previously computed positions to set it // correctly. - if (isLeadingPosDefined(child, crossAxis)) { - child->layout.position[pos[crossAxis]] = getLeadingPosition(child, crossAxis) + - getLeadingBorder(node, crossAxis) + - getLeadingMargin(child, crossAxis); + if (YGNodeIsLeadingPosDefined(child, crossAxis)) { + child->layout.position[pos[crossAxis]] = YGNodeLeadingPosition(child, crossAxis) + + YGNodeLeadingBorder(node, crossAxis) + + YGNodeLeadingMargin(child, crossAxis); } else { child->layout.position[pos[crossAxis]] = - getLeadingBorder(node, crossAxis) + getLeadingMargin(child, crossAxis); + YGNodeLeadingBorder(node, crossAxis) + YGNodeLeadingMargin(child, crossAxis); } } else { float leadingCrossDim = leadingPaddingAndBorderCross; @@ -2004,7 +2031,7 @@ static void layoutNodeImpl(const CSSNodeRef node, // For a relative children, we're either using alignItems (parent) or // alignSelf (child) in order to determine the position in the cross // axis - const YGAlign alignItem = getAlignItem(node, child); + const YGAlign alignItem = YGNodeAlignItem(node, child); // If the child uses align stretch, we need to lay it out one more // time, this time @@ -2012,8 +2039,8 @@ static void layoutNodeImpl(const CSSNodeRef node, // current line. if (alignItem == YGAlignStretch) { const bool isCrossSizeDefinite = - (isMainAxisRow && isStyleDimDefined(child, YGFlexDirectionColumn)) || - (!isMainAxisRow && isStyleDimDefined(child, YGFlexDirectionRow)); + (isMainAxisRow && YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn)) || + (!isMainAxisRow && YGNodeIsStyleDimDefined(child, YGFlexDirectionRow)); float childWidth; float childHeight; @@ -2023,39 +2050,40 @@ static void layoutNodeImpl(const CSSNodeRef node, if (isMainAxisRow) { childHeight = crossDim; childWidth = child->layout.measuredDimensions[YGDimensionWidth] + - getMarginAxis(child, YGFlexDirectionRow); + YGNodeMarginForAxis(child, YGFlexDirectionRow); } else { childWidth = crossDim; childHeight = child->layout.measuredDimensions[YGDimensionHeight] + - getMarginAxis(child, YGFlexDirectionColumn); + YGNodeMarginForAxis(child, YGFlexDirectionColumn); } - constrainMaxSizeForMode(child->style.maxDimensions[YGDimensionWidth], - &childWidthMeasureMode, - &childWidth); - constrainMaxSizeForMode(child->style.maxDimensions[YGDimensionHeight], - &childHeightMeasureMode, - &childHeight); + YGConstrainMaxSizeForMode(child->style.maxDimensions[YGDimensionWidth], + &childWidthMeasureMode, + &childWidth); + YGConstrainMaxSizeForMode(child->style.maxDimensions[YGDimensionHeight], + &childHeightMeasureMode, + &childHeight); // If the child defines a definite size for its cross axis, there's // no need to stretch. if (!isCrossSizeDefinite) { childWidthMeasureMode = - CSSValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly; + YGValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly; childHeightMeasureMode = - CSSValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly; + YGValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly; - layoutNodeInternal(child, - childWidth, - childHeight, - direction, - childWidthMeasureMode, - childHeightMeasureMode, - true, - "stretch"); + YGLayoutNodeInternal(child, + childWidth, + childHeight, + direction, + childWidthMeasureMode, + childHeightMeasureMode, + true, + "stretch"); } } else if (alignItem != YGAlignFlexStart) { - const float remainingCrossDim = containerCrossAxis - getDimWithMargin(child, crossAxis); + const float remainingCrossDim = + containerCrossAxis - YGNodeDimWithMargin(child, crossAxis); if (alignItem == YGAlignCenter) { leadingCrossDim += remainingCrossDim / 2; @@ -2075,7 +2103,7 @@ static void layoutNodeImpl(const CSSNodeRef node, } // STEP 8: MULTI-LINE CONTENT ALIGNMENT - if (lineCount > 1 && performLayout && !CSSValueIsUndefined(availableInnerCrossDim)) { + if (lineCount > 1 && performLayout && !YGValueIsUndefined(availableInnerCrossDim)) { const float remainingAlignContentDim = availableInnerCrossDim - totalLineCrossDim; float crossDimLead = 0; @@ -2107,17 +2135,17 @@ static void layoutNodeImpl(const CSSNodeRef node, // compute the line's height and find the endIndex float lineHeight = 0; for (ii = startIndex; ii < childCount; ii++) { - const CSSNodeRef child = CSSNodeListGet(node->children, ii); + const YGNodeRef child = YGNodeListGet(node->children, ii); if (child->style.positionType == YGPositionTypeRelative) { if (child->lineIndex != i) { break; } - if (isLayoutDimDefined(child, crossAxis)) { + if (YGNodeIsLayoutDimDefined(child, crossAxis)) { lineHeight = fmaxf(lineHeight, child->layout.measuredDimensions[dim[crossAxis]] + - getMarginAxis(child, crossAxis)); + YGNodeMarginForAxis(child, crossAxis)); } } } @@ -2126,18 +2154,18 @@ static void layoutNodeImpl(const CSSNodeRef node, if (performLayout) { for (ii = startIndex; ii < endIndex; ii++) { - const CSSNodeRef child = CSSNodeListGet(node->children, ii); + const YGNodeRef child = YGNodeListGet(node->children, ii); if (child->style.positionType == YGPositionTypeRelative) { - switch (getAlignItem(node, child)) { + switch (YGNodeAlignItem(node, child)) { case YGAlignFlexStart: { child->layout.position[pos[crossAxis]] = - currentLead + getLeadingMargin(child, crossAxis); + currentLead + YGNodeLeadingMargin(child, crossAxis); break; } case YGAlignFlexEnd: { child->layout.position[pos[crossAxis]] = - currentLead + lineHeight - getTrailingMargin(child, crossAxis) - + currentLead + lineHeight - YGNodeTrailingMargin(child, crossAxis) - child->layout.measuredDimensions[dim[crossAxis]]; break; } @@ -2149,7 +2177,7 @@ static void layoutNodeImpl(const CSSNodeRef node, } case YGAlignStretch: { child->layout.position[pos[crossAxis]] = - currentLead + getLeadingMargin(child, crossAxis); + currentLead + YGNodeLeadingMargin(child, crossAxis); // TODO(prenaux): Correctly set the height of items with indefinite // (auto) crossAxis dimension. break; @@ -2168,20 +2196,21 @@ static void layoutNodeImpl(const CSSNodeRef node, // STEP 9: COMPUTING FINAL DIMENSIONS node->layout.measuredDimensions[YGDimensionWidth] = - boundAxis(node, YGFlexDirectionRow, availableWidth - marginAxisRow); + YGNodeBoundAxis(node, YGFlexDirectionRow, availableWidth - marginAxisRow); node->layout.measuredDimensions[YGDimensionHeight] = - boundAxis(node, YGFlexDirectionColumn, availableHeight - marginAxisColumn); + YGNodeBoundAxis(node, YGFlexDirectionColumn, availableHeight - marginAxisColumn); // If the user didn't specify a width or height for the node, set the // dimensions based on the children. if (measureModeMainDim == YGMeasureModeUndefined) { // Clamp the size to the min/max size, if specified, and make sure it // doesn't go below the padding and border amount. - node->layout.measuredDimensions[dim[mainAxis]] = boundAxis(node, mainAxis, maxLineMainDim); + node->layout.measuredDimensions[dim[mainAxis]] = + YGNodeBoundAxis(node, mainAxis, maxLineMainDim); } else if (measureModeMainDim == YGMeasureModeAtMost) { node->layout.measuredDimensions[dim[mainAxis]] = fmaxf(fminf(availableInnerMainDim + paddingAndBorderAxisMain, - boundAxisWithinMinAndMax(node, mainAxis, maxLineMainDim)), + YGNodeBoundAxisWithinMinAndMax(node, mainAxis, maxLineMainDim)), paddingAndBorderAxisMain); } @@ -2189,13 +2218,13 @@ static void layoutNodeImpl(const CSSNodeRef node, // Clamp the size to the min/max size, if specified, and make sure it // doesn't go below the padding and border amount. node->layout.measuredDimensions[dim[crossAxis]] = - boundAxis(node, crossAxis, totalLineCrossDim + paddingAndBorderAxisCross); + YGNodeBoundAxis(node, crossAxis, totalLineCrossDim + paddingAndBorderAxisCross); } else if (measureModeCrossDim == YGMeasureModeAtMost) { node->layout.measuredDimensions[dim[crossAxis]] = fmaxf(fminf(availableInnerCrossDim + paddingAndBorderAxisCross, - boundAxisWithinMinAndMax(node, - crossAxis, - totalLineCrossDim + paddingAndBorderAxisCross)), + YGNodeBoundAxisWithinMinAndMax(node, + crossAxis, + totalLineCrossDim + paddingAndBorderAxisCross)), paddingAndBorderAxisCross); } @@ -2203,7 +2232,7 @@ static void layoutNodeImpl(const CSSNodeRef node, // STEP 10: SIZING AND POSITIONING ABSOLUTE CHILDREN for (currentAbsoluteChild = firstAbsoluteChild; currentAbsoluteChild != NULL; currentAbsoluteChild = currentAbsoluteChild->nextChild) { - absoluteLayoutChild( + YGNodeAbsoluteLayoutChild( node, currentAbsoluteChild, availableInnerWidth, widthMeasureMode, direction); } @@ -2216,14 +2245,14 @@ static void layoutNodeImpl(const CSSNodeRef node, // Set trailing position if necessary. if (needsMainTrailingPos || needsCrossTrailingPos) { for (uint32_t i = 0; i < childCount; i++) { - const CSSNodeRef child = CSSNodeListGet(node->children, i); + const YGNodeRef child = YGNodeListGet(node->children, i); if (needsMainTrailingPos) { - setTrailingPosition(node, child, mainAxis); + YGNodeSetChildTrailingPosition(node, child, mainAxis); } if (needsCrossTrailingPos) { - setTrailingPosition(node, child, crossAxis); + YGNodeSetChildTrailingPosition(node, child, crossAxis); } } } @@ -2237,7 +2266,7 @@ bool gPrintSkips = false; static const char *spacer = " "; -static const char *getSpacer(const unsigned long level) { +static const char *YGSpacer(const unsigned long level) { const size_t spacerLen = strlen(spacer); if (level > spacerLen) { return &spacer[0]; @@ -2246,7 +2275,7 @@ static const char *getSpacer(const unsigned long level) { } } -static const char *getModeName(const YGMeasureMode mode, const bool performLayout) { +static const char *YGMeasureModeName(const YGMeasureMode mode, const bool performLayout) { const char *kMeasureModeNames[YGMeasureModeCount] = {"UNDEFINED", "EXACTLY", "AT_MOST"}; const char *kLayoutModeNames[YGMeasureModeCount] = {"LAY_UNDEFINED", "LAY_EXACTLY", @@ -2260,89 +2289,90 @@ static const char *getModeName(const YGMeasureMode mode, const bool performLayou return performLayout ? kLayoutModeNames[mode] : kMeasureModeNames[mode]; } -static inline bool newSizeIsExactAndMatchesOldMeasuredSize(YGMeasureMode sizeMode, - float size, - float lastComputedSize) { - return sizeMode == YGMeasureModeExactly && eq(size, lastComputedSize); +static inline bool YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(YGMeasureMode sizeMode, + float size, + float lastComputedSize) { + return sizeMode == YGMeasureModeExactly && YGFloatsEqual(size, lastComputedSize); } -static inline bool oldSizeIsUnspecifiedAndStillFits(YGMeasureMode sizeMode, - float size, - YGMeasureMode lastSizeMode, - float lastComputedSize) { +static inline bool YGMeasureModeOldSizeIsUnspecifiedAndStillFits(YGMeasureMode sizeMode, + float size, + YGMeasureMode lastSizeMode, + float lastComputedSize) { return sizeMode == YGMeasureModeAtMost && lastSizeMode == YGMeasureModeUndefined && size >= lastComputedSize; } -static inline bool newMeasureSizeIsStricterAndStillValid(YGMeasureMode sizeMode, - float size, - YGMeasureMode lastSizeMode, - float lastSize, - float lastComputedSize) { +static inline bool YGMeasureModeNewMeasureSizeIsStricterAndStillValid(YGMeasureMode sizeMode, + float size, + YGMeasureMode lastSizeMode, + float lastSize, + float lastComputedSize) { return lastSizeMode == YGMeasureModeAtMost && sizeMode == YGMeasureModeAtMost && lastSize > size && lastComputedSize <= size; } -bool CSSNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, - const float width, - const YGMeasureMode heightMode, - const float height, - const YGMeasureMode lastWidthMode, - const float lastWidth, - const YGMeasureMode lastHeightMode, - const float lastHeight, - const float lastComputedWidth, - const float lastComputedHeight, - const float marginRow, - const float marginColumn) { +bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, + const float width, + const YGMeasureMode heightMode, + const float height, + const YGMeasureMode lastWidthMode, + const float lastWidth, + const YGMeasureMode lastHeightMode, + const float lastHeight, + const float lastComputedWidth, + const float lastComputedHeight, + const float marginRow, + const float marginColumn) { if (lastComputedHeight < 0 || lastComputedWidth < 0) { return false; } - const bool hasSameWidthSpec = lastWidthMode == widthMode && eq(lastWidth, width); - const bool hasSameHeightSpec = lastHeightMode == heightMode && eq(lastHeight, height); + const bool hasSameWidthSpec = lastWidthMode == widthMode && YGFloatsEqual(lastWidth, width); + const bool hasSameHeightSpec = lastHeightMode == heightMode && YGFloatsEqual(lastHeight, height); const bool widthIsCompatible = - hasSameWidthSpec || - newSizeIsExactAndMatchesOldMeasuredSize(widthMode, width - marginRow, lastComputedWidth) || - oldSizeIsUnspecifiedAndStillFits(widthMode, - width - marginRow, - lastWidthMode, - lastComputedWidth) || - newMeasureSizeIsStricterAndStillValid( + hasSameWidthSpec || YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(widthMode, + width - marginRow, + lastComputedWidth) || + YGMeasureModeOldSizeIsUnspecifiedAndStillFits(widthMode, + width - marginRow, + lastWidthMode, + lastComputedWidth) || + YGMeasureModeNewMeasureSizeIsStricterAndStillValid( widthMode, width - marginRow, lastWidthMode, lastWidth, lastComputedWidth); const bool heightIsCompatible = - hasSameHeightSpec || newSizeIsExactAndMatchesOldMeasuredSize(heightMode, - height - marginColumn, - lastComputedHeight) || - oldSizeIsUnspecifiedAndStillFits(heightMode, - height - marginColumn, - lastHeightMode, - lastComputedHeight) || - newMeasureSizeIsStricterAndStillValid( + hasSameHeightSpec || YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(heightMode, + height - marginColumn, + lastComputedHeight) || + YGMeasureModeOldSizeIsUnspecifiedAndStillFits(heightMode, + height - marginColumn, + lastHeightMode, + lastComputedHeight) || + YGMeasureModeNewMeasureSizeIsStricterAndStillValid( heightMode, height - marginColumn, lastHeightMode, lastHeight, lastComputedHeight); return widthIsCompatible && heightIsCompatible; } // -// This is a wrapper around the layoutNodeImpl function. It determines +// This is a wrapper around the YGNodelayoutImpl function. It determines // whether the layout request is redundant and can be skipped. // // Parameters: -// Input parameters are the same as layoutNodeImpl (see above) +// Input parameters are the same as YGNodelayoutImpl (see above) // Return parameter is true if layout was performed, false if skipped // -bool layoutNodeInternal(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGDirection parentDirection, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, - const bool performLayout, - const char *reason) { - CSSLayout *layout = &node->layout; +bool YGLayoutNodeInternal(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGDirection parentDirection, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode, + const bool performLayout, + const char *reason) { + YGLayout *layout = &node->layout; gDepth++; @@ -2359,7 +2389,7 @@ bool layoutNodeInternal(const CSSNodeRef node, layout->cachedLayout.computedHeight = -1; } - CSSCachedMeasurement *cachedResults = NULL; + YGCachedMeasurement *cachedResults = NULL; // Determine whether the results are already cached. We maintain a separate // cache for layouts and measurements. A layout operation modifies the @@ -2374,54 +2404,54 @@ bool layoutNodeInternal(const CSSNodeRef node, // expensive to measure, so it's worth avoiding redundant measurements if at // all possible. if (node->measure) { - const float marginAxisRow = getMarginAxis(node, YGFlexDirectionRow); - const float marginAxisColumn = getMarginAxis(node, YGFlexDirectionColumn); + const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow); + const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn); // First, try to use the layout cache. - if (CSSNodeCanUseCachedMeasurement(widthMeasureMode, - availableWidth, - heightMeasureMode, - availableHeight, - layout->cachedLayout.widthMeasureMode, - layout->cachedLayout.availableWidth, - layout->cachedLayout.heightMeasureMode, - layout->cachedLayout.availableHeight, - layout->cachedLayout.computedWidth, - layout->cachedLayout.computedHeight, - marginAxisRow, - marginAxisColumn)) { + if (YGNodeCanUseCachedMeasurement(widthMeasureMode, + availableWidth, + heightMeasureMode, + availableHeight, + layout->cachedLayout.widthMeasureMode, + layout->cachedLayout.availableWidth, + layout->cachedLayout.heightMeasureMode, + layout->cachedLayout.availableHeight, + layout->cachedLayout.computedWidth, + layout->cachedLayout.computedHeight, + marginAxisRow, + marginAxisColumn)) { cachedResults = &layout->cachedLayout; } else { // Try to use the measurement cache. for (uint32_t i = 0; i < layout->nextCachedMeasurementsIndex; i++) { - if (CSSNodeCanUseCachedMeasurement(widthMeasureMode, - availableWidth, - heightMeasureMode, - availableHeight, - layout->cachedMeasurements[i].widthMeasureMode, - layout->cachedMeasurements[i].availableWidth, - layout->cachedMeasurements[i].heightMeasureMode, - layout->cachedMeasurements[i].availableHeight, - layout->cachedMeasurements[i].computedWidth, - layout->cachedMeasurements[i].computedHeight, - marginAxisRow, - marginAxisColumn)) { + if (YGNodeCanUseCachedMeasurement(widthMeasureMode, + availableWidth, + heightMeasureMode, + availableHeight, + layout->cachedMeasurements[i].widthMeasureMode, + layout->cachedMeasurements[i].availableWidth, + layout->cachedMeasurements[i].heightMeasureMode, + layout->cachedMeasurements[i].availableHeight, + layout->cachedMeasurements[i].computedWidth, + layout->cachedMeasurements[i].computedHeight, + marginAxisRow, + marginAxisColumn)) { cachedResults = &layout->cachedMeasurements[i]; break; } } } } else if (performLayout) { - if (eq(layout->cachedLayout.availableWidth, availableWidth) && - eq(layout->cachedLayout.availableHeight, availableHeight) && + if (YGFloatsEqual(layout->cachedLayout.availableWidth, availableWidth) && + YGFloatsEqual(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++) { - if (eq(layout->cachedMeasurements[i].availableWidth, availableWidth) && - eq(layout->cachedMeasurements[i].availableHeight, availableHeight) && + if (YGFloatsEqual(layout->cachedMeasurements[i].availableWidth, availableWidth) && + YGFloatsEqual(layout->cachedMeasurements[i].availableHeight, availableHeight) && layout->cachedMeasurements[i].widthMeasureMode == widthMeasureMode && layout->cachedMeasurements[i].heightMeasureMode == heightMeasureMode) { cachedResults = &layout->cachedMeasurements[i]; @@ -2435,13 +2465,13 @@ bool layoutNodeInternal(const CSSNodeRef node, layout->measuredDimensions[YGDimensionHeight] = cachedResults->computedHeight; if (gPrintChanges && gPrintSkips) { - printf("%s%d.{[skipped] ", getSpacer(gDepth), gDepth); + printf("%s%d.{[skipped] ", YGSpacer(gDepth), gDepth); if (node->print) { node->print(node); } printf("wm: %s, hm: %s, aw: %f ah: %f => d: (%f, %f) %s\n", - getModeName(widthMeasureMode, performLayout), - getModeName(heightMeasureMode, performLayout), + YGMeasureModeName(widthMeasureMode, performLayout), + YGMeasureModeName(heightMeasureMode, performLayout), availableWidth, availableHeight, cachedResults->computedWidth, @@ -2450,34 +2480,34 @@ bool layoutNodeInternal(const CSSNodeRef node, } } else { if (gPrintChanges) { - printf("%s%d.{%s", getSpacer(gDepth), gDepth, needToVisitNode ? "*" : ""); + printf("%s%d.{%s", YGSpacer(gDepth), gDepth, needToVisitNode ? "*" : ""); if (node->print) { node->print(node); } printf("wm: %s, hm: %s, aw: %f ah: %f %s\n", - getModeName(widthMeasureMode, performLayout), - getModeName(heightMeasureMode, performLayout), + YGMeasureModeName(widthMeasureMode, performLayout), + YGMeasureModeName(heightMeasureMode, performLayout), availableWidth, availableHeight, reason); } - layoutNodeImpl(node, - availableWidth, - availableHeight, - parentDirection, - widthMeasureMode, - heightMeasureMode, - performLayout); + YGNodelayoutImpl(node, + availableWidth, + availableHeight, + parentDirection, + widthMeasureMode, + heightMeasureMode, + performLayout); if (gPrintChanges) { - printf("%s%d.}%s", getSpacer(gDepth), gDepth, needToVisitNode ? "*" : ""); + printf("%s%d.}%s", YGSpacer(gDepth), gDepth, needToVisitNode ? "*" : ""); if (node->print) { node->print(node); } printf("wm: %s, hm: %s, d: (%f, %f) %s\n", - getModeName(widthMeasureMode, performLayout), - getModeName(heightMeasureMode, performLayout), + YGMeasureModeName(widthMeasureMode, performLayout), + YGMeasureModeName(heightMeasureMode, performLayout), layout->measuredDimensions[YGDimensionWidth], layout->measuredDimensions[YGDimensionHeight], reason); @@ -2486,14 +2516,14 @@ bool layoutNodeInternal(const CSSNodeRef node, layout->lastParentDirection = parentDirection; if (cachedResults == NULL) { - if (layout->nextCachedMeasurementsIndex == CSS_MAX_CACHED_RESULT_COUNT) { + if (layout->nextCachedMeasurementsIndex == YG_MAX_CACHED_RESULT_COUNT) { if (gPrintChanges) { printf("Out of cache entries!\n"); } layout->nextCachedMeasurementsIndex = 0; } - CSSCachedMeasurement *newCacheEntry; + YGCachedMeasurement *newCacheEntry; if (performLayout) { // Use the single layout cache entry. newCacheEntry = &layout->cachedLayout; @@ -2524,7 +2554,7 @@ bool layoutNodeInternal(const CSSNodeRef node, return (needToVisitNode || cachedResults == NULL); } -static void roundToPixelGrid(const CSSNodeRef node) { +static void roundToPixelGrid(const YGNodeRef node) { const float fractialLeft = node->layout.position[YGEdgeLeft] - floorf(node->layout.position[YGEdgeLeft]); const float fractialTop = @@ -2537,16 +2567,16 @@ static void roundToPixelGrid(const CSSNodeRef node) { node->layout.position[YGEdgeLeft] = roundf(node->layout.position[YGEdgeLeft]); node->layout.position[YGEdgeTop] = roundf(node->layout.position[YGEdgeTop]); - const uint32_t childCount = CSSNodeListCount(node->children); + const uint32_t childCount = YGNodeListCount(node->children); for (uint32_t i = 0; i < childCount; i++) { - roundToPixelGrid(CSSNodeGetChild(node, i)); + roundToPixelGrid(YGNodeGetChild(node, i)); } } -void CSSNodeCalculateLayout(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGDirection parentDirection) { +void YGNodeCalculateLayout(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGDirection parentDirection) { // Increment the generation count. This will force the recursive routine to // visit // all dirty nodes at least once. Subsequent visits will be skipped if the @@ -2559,54 +2589,54 @@ void CSSNodeCalculateLayout(const CSSNodeRef node, YGMeasureMode widthMeasureMode = YGMeasureModeUndefined; YGMeasureMode heightMeasureMode = YGMeasureModeUndefined; - if (!CSSValueIsUndefined(width)) { + if (!YGValueIsUndefined(width)) { widthMeasureMode = YGMeasureModeExactly; - } else if (isStyleDimDefined(node, YGFlexDirectionRow)) { - width = - node->style.dimensions[dim[YGFlexDirectionRow]] + getMarginAxis(node, YGFlexDirectionRow); + } else if (YGNodeIsStyleDimDefined(node, YGFlexDirectionRow)) { + width = node->style.dimensions[dim[YGFlexDirectionRow]] + + YGNodeMarginForAxis(node, YGFlexDirectionRow); widthMeasureMode = YGMeasureModeExactly; } else if (node->style.maxDimensions[YGDimensionWidth] >= 0.0) { width = node->style.maxDimensions[YGDimensionWidth]; widthMeasureMode = YGMeasureModeAtMost; } - if (!CSSValueIsUndefined(height)) { + if (!YGValueIsUndefined(height)) { heightMeasureMode = YGMeasureModeExactly; - } else if (isStyleDimDefined(node, YGFlexDirectionColumn)) { + } else if (YGNodeIsStyleDimDefined(node, YGFlexDirectionColumn)) { height = node->style.dimensions[dim[YGFlexDirectionColumn]] + - getMarginAxis(node, YGFlexDirectionColumn); + YGNodeMarginForAxis(node, YGFlexDirectionColumn); heightMeasureMode = YGMeasureModeExactly; } else if (node->style.maxDimensions[YGDimensionHeight] >= 0.0) { height = node->style.maxDimensions[YGDimensionHeight]; heightMeasureMode = YGMeasureModeAtMost; } - if (layoutNodeInternal(node, - width, - height, - parentDirection, - widthMeasureMode, - heightMeasureMode, - true, - "initia" - "l")) { - setPosition(node, node->layout.direction); + if (YGLayoutNodeInternal(node, + width, + height, + parentDirection, + widthMeasureMode, + heightMeasureMode, + true, + "initia" + "l")) { + YGNodeSetPosition(node, node->layout.direction); - if (CSSLayoutIsExperimentalFeatureEnabled(YGExperimentalFeatureRounding)) { + if (YGIsExperimentalFeatureEnabled(YGExperimentalFeatureRounding)) { roundToPixelGrid(node); } if (gPrintTree) { - CSSNodePrint(node, YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle); + YGNodePrint(node, YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle); } } } -void CSSLayoutSetLogger(CSSLogger logger) { +void YGSetLogger(YGLogger logger) { gLogger = logger; } -void CSSLog(YGLogLevel level, const char *format, ...) { +void YGLog(YGLogLevel level, const char *format, ...) { va_list args; va_start(args, format); gLogger(level, format, args); @@ -2615,32 +2645,29 @@ void CSSLog(YGLogLevel level, const char *format, ...) { static bool experimentalFeatures[YGExperimentalFeatureCount + 1]; -void CSSLayoutSetExperimentalFeatureEnabled(YGExperimentalFeature feature, bool enabled) { +void YGSetExperimentalFeatureEnabled(YGExperimentalFeature feature, bool enabled) { experimentalFeatures[feature] = enabled; } -inline bool CSSLayoutIsExperimentalFeatureEnabled(YGExperimentalFeature feature) { +inline bool YGIsExperimentalFeatureEnabled(YGExperimentalFeature feature) { return experimentalFeatures[feature]; } -void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc, - CSSCalloc cssCalloc, - CSSRealloc cssRealloc, - CSSFree cssFree) { - CSS_ASSERT(gNodeInstanceCount == 0, "Cannot set memory functions: all node must be freed first"); - CSS_ASSERT((cssMalloc == NULL && cssCalloc == NULL && cssRealloc == NULL && cssFree == NULL) || - (cssMalloc != NULL && cssCalloc != NULL && cssRealloc != NULL && cssFree != NULL), - "Cannot set memory functions: functions must be all NULL or Non-NULL"); +void YGSetMemoryFuncs(YGMalloc YGMalloc, YGCalloc YGCalloc, YGRealloc YGRealloc, YGFree YGFree) { + YG_ASSERT(gNodeInstanceCount == 0, "Cannot set memory functions: all node must be freed first"); + YG_ASSERT((YGMalloc == NULL && YGCalloc == NULL && YGRealloc == NULL && YGFree == NULL) || + (YGMalloc != NULL && YGCalloc != NULL && YGRealloc != NULL && YGFree != NULL), + "Cannot set memory functions: functions must be all NULL or Non-NULL"); - if (cssMalloc == NULL || cssCalloc == NULL || cssRealloc == NULL || cssFree == NULL) { - gCSSMalloc = &malloc; - gCSSCalloc = &calloc; - gCSSRealloc = &realloc; - gCSSFree = &free; + if (YGMalloc == NULL || YGCalloc == NULL || YGRealloc == NULL || YGFree == NULL) { + gYGMalloc = &malloc; + gYGCalloc = &calloc; + gYGRealloc = &realloc; + gYGFree = &free; } else { - gCSSMalloc = cssMalloc; - gCSSCalloc = cssCalloc; - gCSSRealloc = cssRealloc; - gCSSFree = cssFree; + gYGMalloc = YGMalloc; + gYGCalloc = YGCalloc; + gYGRealloc = YGRealloc; + gYGFree = YGFree; } } diff --git a/React/CSSLayout/Yoga.h b/React/CSSLayout/Yoga.h new file mode 100644 index 000000000..926a3b943 --- /dev/null +++ b/React/CSSLayout/Yoga.h @@ -0,0 +1,181 @@ +/** + * 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. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#ifndef __cplusplus +#include +#endif + +// Not defined in MSVC++ +#ifndef NAN +static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff}; +#define NAN (*(const float *) __nan) +#endif + +#define YGUndefined NAN + +#include "YGEnums.h" +#include "YGMacros.h" + +YG_EXTERN_C_BEGIN + +typedef struct YGSize { + float width; + float height; +} YGSize; + +typedef struct YGNode *YGNodeRef; +typedef YGSize (*YGMeasureFunc)(YGNodeRef node, + float width, + YGMeasureMode widthMode, + float height, + YGMeasureMode heightMode); +typedef void (*YGPrintFunc)(YGNodeRef node); +typedef int (*YGLogger)(YGLogLevel level, const char *format, va_list args); + +typedef void *(*YGMalloc)(size_t size); +typedef void *(*YGCalloc)(size_t count, size_t size); +typedef void *(*YGRealloc)(void *ptr, size_t size); +typedef void (*YGFree)(void *ptr); + +// YGNode +WIN_EXPORT YGNodeRef YGNodeNew(void); +WIN_EXPORT void YGNodeInit(const YGNodeRef node); +WIN_EXPORT void YGNodeFree(const YGNodeRef node); +WIN_EXPORT void YGNodeFreeRecursive(const YGNodeRef node); +WIN_EXPORT void YGNodeReset(const YGNodeRef node); +WIN_EXPORT int32_t YGNodeGetInstanceCount(void); + +WIN_EXPORT void YGNodeInsertChild(const YGNodeRef node, + const YGNodeRef child, + const uint32_t index); +WIN_EXPORT void YGNodeRemoveChild(const YGNodeRef node, const YGNodeRef child); +WIN_EXPORT YGNodeRef YGNodeGetChild(const YGNodeRef node, const uint32_t index); +WIN_EXPORT uint32_t YGNodeChildCount(const YGNodeRef node); + +WIN_EXPORT void YGNodeCalculateLayout(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGDirection parentDirection); + +// Mark a node as dirty. Only valid for nodes with a custom measure function +// set. +// YG knows when to mark all other nodes as dirty but because nodes with +// measure functions +// depends on information not known to YG they must perform this dirty +// marking manually. +WIN_EXPORT void YGNodeMarkDirty(const YGNodeRef node); +WIN_EXPORT bool YGNodeIsDirty(const YGNodeRef node); + +WIN_EXPORT void YGNodePrint(const YGNodeRef node, const YGPrintOptions options); + +WIN_EXPORT bool YGValueIsUndefined(const float value); + +WIN_EXPORT bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, + const float width, + const YGMeasureMode heightMode, + const float height, + const YGMeasureMode lastWidthMode, + const float lastWidth, + const YGMeasureMode lastHeightMode, + const float lastHeight, + const float lastComputedWidth, + const float lastComputedHeight, + const float marginRow, + const float marginColumn); + +WIN_EXPORT void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode); + +#define YG_NODE_PROPERTY(type, name, paramName) \ + WIN_EXPORT void YGNodeSet##name(const YGNodeRef node, type paramName); \ + WIN_EXPORT type YGNodeGet##name(const YGNodeRef node); + +#define YG_NODE_STYLE_PROPERTY(type, name, paramName) \ + WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node, const type paramName); \ + WIN_EXPORT type YGNodeStyleGet##name(const YGNodeRef node); + +#define YG_NODE_STYLE_EDGE_PROPERTY(type, name, paramName) \ + WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node, \ + const YGEdge edge, \ + const type paramName); \ + WIN_EXPORT type YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge); + +#define YG_NODE_LAYOUT_PROPERTY(type, name) \ + WIN_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node); + +YG_NODE_PROPERTY(void *, Context, context); +YG_NODE_PROPERTY(YGMeasureFunc, MeasureFunc, measureFunc); +YG_NODE_PROPERTY(YGPrintFunc, PrintFunc, printFunc); +YG_NODE_PROPERTY(bool, HasNewLayout, hasNewLayout); + +YG_NODE_STYLE_PROPERTY(YGDirection, Direction, direction); +YG_NODE_STYLE_PROPERTY(YGFlexDirection, FlexDirection, flexDirection); +YG_NODE_STYLE_PROPERTY(YGJustify, JustifyContent, justifyContent); +YG_NODE_STYLE_PROPERTY(YGAlign, AlignContent, alignContent); +YG_NODE_STYLE_PROPERTY(YGAlign, AlignItems, alignItems); +YG_NODE_STYLE_PROPERTY(YGAlign, AlignSelf, alignSelf); +YG_NODE_STYLE_PROPERTY(YGPositionType, PositionType, positionType); +YG_NODE_STYLE_PROPERTY(YGWrap, FlexWrap, flexWrap); +YG_NODE_STYLE_PROPERTY(YGOverflow, Overflow, overflow); + +WIN_EXPORT void YGNodeStyleSetFlex(const YGNodeRef node, const float flex); +YG_NODE_STYLE_PROPERTY(float, FlexGrow, flexGrow); +YG_NODE_STYLE_PROPERTY(float, FlexShrink, flexShrink); +YG_NODE_STYLE_PROPERTY(float, FlexBasis, flexBasis); + +YG_NODE_STYLE_EDGE_PROPERTY(float, Position, position); +YG_NODE_STYLE_EDGE_PROPERTY(float, Margin, margin); +YG_NODE_STYLE_EDGE_PROPERTY(float, Padding, padding); +YG_NODE_STYLE_EDGE_PROPERTY(float, Border, border); + +YG_NODE_STYLE_PROPERTY(float, Width, width); +YG_NODE_STYLE_PROPERTY(float, Height, height); +YG_NODE_STYLE_PROPERTY(float, MinWidth, minWidth); +YG_NODE_STYLE_PROPERTY(float, MinHeight, minHeight); +YG_NODE_STYLE_PROPERTY(float, MaxWidth, maxWidth); +YG_NODE_STYLE_PROPERTY(float, MaxHeight, maxHeight); + +// Yoga specific properties, not compatible with flexbox specification +// Aspect ratio control the size of the undefined dimension of a node. +// - On a node with a set width/height aspect ratio control the size of the unset dimension +// - On a node with a set flex basis aspect ratio controls the size of the node in the cross axis if +// unset +// - On a node with a measure function aspect ratio works as though the measure function measures +// the flex basis +// - On a node with flex grow/shrink aspect ratio controls the size of the node in the cross axis if +// unset +// - Aspect ratio takes min/max dimensions into account +YG_NODE_STYLE_PROPERTY(float, AspectRatio, aspectRatio); + +YG_NODE_LAYOUT_PROPERTY(float, Left); +YG_NODE_LAYOUT_PROPERTY(float, Top); +YG_NODE_LAYOUT_PROPERTY(float, Right); +YG_NODE_LAYOUT_PROPERTY(float, Bottom); +YG_NODE_LAYOUT_PROPERTY(float, Width); +YG_NODE_LAYOUT_PROPERTY(float, Height); +YG_NODE_LAYOUT_PROPERTY(YGDirection, Direction); + +WIN_EXPORT void YGSetLogger(YGLogger logger); +WIN_EXPORT void YGLog(YGLogLevel level, const char *message, ...); + +WIN_EXPORT void YGSetExperimentalFeatureEnabled(YGExperimentalFeature feature, bool enabled); +WIN_EXPORT bool YGIsExperimentalFeatureEnabled(YGExperimentalFeature feature); + +WIN_EXPORT void +YGSetMemoryFuncs(YGMalloc cssMalloc, YGCalloc cssCalloc, YGRealloc cssRealloc, YGFree cssFree); + +YG_EXTERN_C_END diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index 4926f7387..cf5b3fae7 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -11,7 +11,7 @@ #import -#import +#import #import "RCTAccessibilityManager.h" #import "RCTAnimationType.h" diff --git a/React/React.xcodeproj/project.pbxproj b/React/React.xcodeproj/project.pbxproj index 85f9ad959..9b48011bb 100644 --- a/React/React.xcodeproj/project.pbxproj +++ b/React/React.xcodeproj/project.pbxproj @@ -180,29 +180,29 @@ 3D1FA09A1DE4F50100E03CC6 /* RCTPushNotificationManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D1FA08D1DE4F4EE00E03CC6 /* RCTPushNotificationManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; 3D37B5821D522B190042D5B5 /* RCTFont.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3D37B5811D522B190042D5B5 /* RCTFont.mm */; }; 3D3C04AB1DE3337C00C268FA /* RCTAlertManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 13B07FE71A69327A00A75B9A /* RCTAlertManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3D3C083B1DE334D900C268FA /* CSSEnums.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D10A3C81DDF3D16004A0F9D /* CSSEnums.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3D3C083C1DE334D900C268FA /* CSSLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = 133683451D37ACA10077D0C3 /* CSSLayout.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3D3C083D1DE334D900C268FA /* CSSMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 133683481D37ACA10077D0C3 /* CSSMacros.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3D3C083E1DE334D900C268FA /* CSSNodeList.h in Headers */ = {isa = PBXBuildFile; fileRef = 1321C8CF1D3EB50800D58318 /* CSSNodeList.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3D3C083F1DE334DA00C268FA /* CSSEnums.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D10A3C81DDF3D16004A0F9D /* CSSEnums.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3D3C08401DE334DA00C268FA /* CSSLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = 133683451D37ACA10077D0C3 /* CSSLayout.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3D3C08411DE334DA00C268FA /* CSSMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 133683481D37ACA10077D0C3 /* CSSMacros.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3D3C08421DE334DA00C268FA /* CSSNodeList.h in Headers */ = {isa = PBXBuildFile; fileRef = 1321C8CF1D3EB50800D58318 /* CSSNodeList.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 3D3C085A1DE33E4400C268FA /* CSSLayout.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* CSSLayout.c */; }; - 3D3C085C1DE33E4E00C268FA /* CSSLayout.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* CSSLayout.c */; }; - 3D3C085E1DE33E6100C268FA /* CSSNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* CSSNodeList.c */; }; - 3D3C085F1DE33E6100C268FA /* CSSNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* CSSNodeList.c */; }; + 3D3C083B1DE334D900C268FA /* YGEnums.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D10A3C81DDF3D16004A0F9D /* YGEnums.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3D3C083C1DE334D900C268FA /* Yoga.h in Headers */ = {isa = PBXBuildFile; fileRef = 133683451D37ACA10077D0C3 /* Yoga.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3D3C083D1DE334D900C268FA /* YGMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 133683481D37ACA10077D0C3 /* YGMacros.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3D3C083E1DE334D900C268FA /* YGNodeList.h in Headers */ = {isa = PBXBuildFile; fileRef = 1321C8CF1D3EB50800D58318 /* YGNodeList.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3D3C083F1DE334DA00C268FA /* YGEnums.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D10A3C81DDF3D16004A0F9D /* YGEnums.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3D3C08401DE334DA00C268FA /* Yoga.h in Headers */ = {isa = PBXBuildFile; fileRef = 133683451D37ACA10077D0C3 /* Yoga.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3D3C08411DE334DA00C268FA /* YGMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 133683481D37ACA10077D0C3 /* YGMacros.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3D3C08421DE334DA00C268FA /* YGNodeList.h in Headers */ = {isa = PBXBuildFile; fileRef = 1321C8CF1D3EB50800D58318 /* YGNodeList.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3D3C085A1DE33E4400C268FA /* Yoga.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* Yoga.c */; }; + 3D3C085C1DE33E4E00C268FA /* Yoga.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* Yoga.c */; }; + 3D3C085E1DE33E6100C268FA /* YGNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* YGNodeList.c */; }; + 3D3C085F1DE33E6100C268FA /* YGNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* YGNodeList.c */; }; 3D3C08891DE342FB00C268FA /* libCSSLayout.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3D3C059A1DE3340900C268FA /* libCSSLayout.a */; }; 3D3C088C1DE3430500C268FA /* libCSSLayout-tvOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3D3C06751DE3340C00C268FA /* libCSSLayout-tvOS.a */; }; 3D3CD8F81DE5FB2D00167DC4 /* JSBundleType.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AC70D2EB1DE48A22002E6351 /* JSBundleType.cpp */; }; - 3D3CD9061DE5FBD600167DC4 /* CSSLayout.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* CSSLayout.c */; }; - 3D3CD9071DE5FBD600167DC4 /* CSSNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* CSSNodeList.c */; }; - 3D3CD9131DE5FBD800167DC4 /* CSSLayout.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* CSSLayout.c */; }; - 3D3CD9141DE5FBD800167DC4 /* CSSNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* CSSNodeList.c */; }; - 3D3CD9201DE5FBEC00167DC4 /* CSSLayout.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* CSSLayout.c */; }; - 3D3CD9211DE5FBEC00167DC4 /* CSSNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* CSSNodeList.c */; }; - 3D3CD92D1DE5FBEE00167DC4 /* CSSLayout.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* CSSLayout.c */; }; - 3D3CD92E1DE5FBEE00167DC4 /* CSSNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* CSSNodeList.c */; }; + 3D3CD9061DE5FBD600167DC4 /* Yoga.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* Yoga.c */; }; + 3D3CD9071DE5FBD600167DC4 /* YGNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* YGNodeList.c */; }; + 3D3CD9131DE5FBD800167DC4 /* Yoga.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* Yoga.c */; }; + 3D3CD9141DE5FBD800167DC4 /* YGNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* YGNodeList.c */; }; + 3D3CD9201DE5FBEC00167DC4 /* Yoga.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* Yoga.c */; }; + 3D3CD9211DE5FBEC00167DC4 /* YGNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* YGNodeList.c */; }; + 3D3CD92D1DE5FBEE00167DC4 /* Yoga.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* Yoga.c */; }; + 3D3CD92E1DE5FBEE00167DC4 /* YGNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* YGNodeList.c */; }; 3D3CD93D1DE5FC1400167DC4 /* JavaScriptCore.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D7A27DC1DE32541002E3F95 /* JavaScriptCore.h */; settings = {ATTRIBUTES = (Public, ); }; }; 3D3CD93E1DE5FC1400167DC4 /* JSCWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D7A27DE1DE32541002E3F95 /* JSCWrapper.h */; settings = {ATTRIBUTES = (Public, ); }; }; 3D3CD93F1DE5FC1400167DC4 /* JavaScriptCore.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D7A27DC1DE32541002E3F95 /* JavaScriptCore.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -543,11 +543,11 @@ 131B6AF11AF1093D00FFC3E0 /* RCTSegmentedControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTSegmentedControl.m; sourceTree = ""; }; 131B6AF21AF1093D00FFC3E0 /* RCTSegmentedControlManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTSegmentedControlManager.h; sourceTree = ""; }; 131B6AF31AF1093D00FFC3E0 /* RCTSegmentedControlManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTSegmentedControlManager.m; sourceTree = ""; }; - 1321C8CE1D3EB50800D58318 /* CSSNodeList.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = CSSNodeList.c; sourceTree = ""; }; - 1321C8CF1D3EB50800D58318 /* CSSNodeList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSNodeList.h; sourceTree = ""; }; - 133683441D37ACA10077D0C3 /* CSSLayout.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = CSSLayout.c; sourceTree = ""; }; - 133683451D37ACA10077D0C3 /* CSSLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSLayout.h; sourceTree = ""; }; - 133683481D37ACA10077D0C3 /* CSSMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSMacros.h; sourceTree = ""; }; + 1321C8CE1D3EB50800D58318 /* YGNodeList.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = YGNodeList.c; sourceTree = ""; }; + 1321C8CF1D3EB50800D58318 /* YGNodeList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGNodeList.h; sourceTree = ""; }; + 133683441D37ACA10077D0C3 /* Yoga.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = Yoga.c; sourceTree = ""; }; + 133683451D37ACA10077D0C3 /* Yoga.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Yoga.h; sourceTree = ""; }; + 133683481D37ACA10077D0C3 /* YGMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGMacros.h; sourceTree = ""; }; 133CAE8C1B8E5CFD00F6AD92 /* RCTDatePicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTDatePicker.h; sourceTree = ""; }; 133CAE8D1B8E5CFD00F6AD92 /* RCTDatePicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDatePicker.m; sourceTree = ""; }; 13442BF21AA90E0B0037E5B0 /* RCTAnimationType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTAnimationType.h; sourceTree = ""; }; @@ -693,7 +693,7 @@ 369123E01DDC75850095B341 /* JSCSamplingProfiler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSCSamplingProfiler.m; sourceTree = ""; }; 391E86A21C623EC800009732 /* RCTTouchEvent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTTouchEvent.m; sourceTree = ""; }; 391E86A31C623EC800009732 /* RCTTouchEvent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTTouchEvent.h; sourceTree = ""; }; - 3D10A3C81DDF3D16004A0F9D /* CSSEnums.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSEnums.h; sourceTree = ""; }; + 3D10A3C81DDF3D16004A0F9D /* YGEnums.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGEnums.h; sourceTree = ""; }; 3D1E68D81CABD13900DD7465 /* RCTDisplayLink.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTDisplayLink.h; sourceTree = ""; }; 3D1E68D91CABD13900DD7465 /* RCTDisplayLink.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDisplayLink.m; sourceTree = ""; }; 3D1FA07A1DE4F2EA00E03CC6 /* RCTNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTNetworking.h; sourceTree = ""; }; @@ -803,12 +803,12 @@ 133683431D37ACA10077D0C3 /* CSSLayout */ = { isa = PBXGroup; children = ( - 3D10A3C81DDF3D16004A0F9D /* CSSEnums.h */, - 133683441D37ACA10077D0C3 /* CSSLayout.c */, - 133683451D37ACA10077D0C3 /* CSSLayout.h */, - 133683481D37ACA10077D0C3 /* CSSMacros.h */, - 1321C8CE1D3EB50800D58318 /* CSSNodeList.c */, - 1321C8CF1D3EB50800D58318 /* CSSNodeList.h */, + 3D10A3C81DDF3D16004A0F9D /* YGEnums.h */, + 133683441D37ACA10077D0C3 /* Yoga.c */, + 133683451D37ACA10077D0C3 /* Yoga.h */, + 133683481D37ACA10077D0C3 /* YGMacros.h */, + 1321C8CE1D3EB50800D58318 /* YGNodeList.c */, + 1321C8CF1D3EB50800D58318 /* YGNodeList.h */, ); name = CSSLayout; path = CSSLayout/CSSLayout; @@ -1188,10 +1188,10 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( - 3D3C083D1DE334D900C268FA /* CSSMacros.h in Headers */, - 3D3C083C1DE334D900C268FA /* CSSLayout.h in Headers */, - 3D3C083E1DE334D900C268FA /* CSSNodeList.h in Headers */, - 3D3C083B1DE334D900C268FA /* CSSEnums.h in Headers */, + 3D3C083D1DE334D900C268FA /* YGMacros.h in Headers */, + 3D3C083C1DE334D900C268FA /* Yoga.h in Headers */, + 3D3C083E1DE334D900C268FA /* YGNodeList.h in Headers */, + 3D3C083B1DE334D900C268FA /* YGEnums.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1199,10 +1199,10 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( - 3D3C08411DE334DA00C268FA /* CSSMacros.h in Headers */, - 3D3C08401DE334DA00C268FA /* CSSLayout.h in Headers */, - 3D3C08421DE334DA00C268FA /* CSSNodeList.h in Headers */, - 3D3C083F1DE334DA00C268FA /* CSSEnums.h in Headers */, + 3D3C08411DE334DA00C268FA /* YGMacros.h in Headers */, + 3D3C08401DE334DA00C268FA /* Yoga.h in Headers */, + 3D3C08421DE334DA00C268FA /* YGNodeList.h in Headers */, + 3D3C083F1DE334DA00C268FA /* YGEnums.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1846,8 +1846,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 3D3C085A1DE33E4400C268FA /* CSSLayout.c in Sources */, - 3D3C085E1DE33E6100C268FA /* CSSNodeList.c in Sources */, + 3D3C085A1DE33E4400C268FA /* Yoga.c in Sources */, + 3D3C085E1DE33E6100C268FA /* YGNodeList.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1855,8 +1855,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 3D3C085C1DE33E4E00C268FA /* CSSLayout.c in Sources */, - 3D3C085F1DE33E6100C268FA /* CSSNodeList.c in Sources */, + 3D3C085C1DE33E4E00C268FA /* Yoga.c in Sources */, + 3D3C085F1DE33E6100C268FA /* YGNodeList.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1864,8 +1864,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 3D3CD9061DE5FBD600167DC4 /* CSSLayout.c in Sources */, - 3D3CD9071DE5FBD600167DC4 /* CSSNodeList.c in Sources */, + 3D3CD9061DE5FBD600167DC4 /* Yoga.c in Sources */, + 3D3CD9071DE5FBD600167DC4 /* YGNodeList.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1873,8 +1873,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 3D3CD9131DE5FBD800167DC4 /* CSSLayout.c in Sources */, - 3D3CD9141DE5FBD800167DC4 /* CSSNodeList.c in Sources */, + 3D3CD9131DE5FBD800167DC4 /* Yoga.c in Sources */, + 3D3CD9141DE5FBD800167DC4 /* YGNodeList.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1882,8 +1882,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 3D3CD9201DE5FBEC00167DC4 /* CSSLayout.c in Sources */, - 3D3CD9211DE5FBEC00167DC4 /* CSSNodeList.c in Sources */, + 3D3CD9201DE5FBEC00167DC4 /* Yoga.c in Sources */, + 3D3CD9211DE5FBEC00167DC4 /* YGNodeList.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1891,8 +1891,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 3D3CD92D1DE5FBEE00167DC4 /* CSSLayout.c in Sources */, - 3D3CD92E1DE5FBEE00167DC4 /* CSSNodeList.c in Sources */, + 3D3CD92D1DE5FBEE00167DC4 /* Yoga.c in Sources */, + 3D3CD92E1DE5FBEE00167DC4 /* YGNodeList.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/React/Views/RCTRootShadowView.m b/React/Views/RCTRootShadowView.m index ed427707a..96fc0c1cc 100644 --- a/React/Views/RCTRootShadowView.m +++ b/React/Views/RCTRootShadowView.m @@ -21,7 +21,7 @@ self = [super init]; if (self) { if ([[RCTI18nUtil sharedInstance] isRTL]) { - CSSNodeStyleSetDirection(self.cssNode, YGDirectionRTL); + YGNodeStyleSetDirection(self.cssNode, YGDirectionRTL); } } return self; @@ -33,14 +33,14 @@ case RCTRootViewSizeFlexibilityNone: break; case RCTRootViewSizeFlexibilityWidth: - CSSNodeStyleSetWidth(self.cssNode, YGUndefined); + YGNodeStyleSetWidth(self.cssNode, YGUndefined); break; case RCTRootViewSizeFlexibilityHeight: - CSSNodeStyleSetHeight(self.cssNode, YGUndefined); + YGNodeStyleSetHeight(self.cssNode, YGUndefined); break; case RCTRootViewSizeFlexibilityWidthAndHeight: - CSSNodeStyleSetWidth(self.cssNode, YGUndefined); - CSSNodeStyleSetHeight(self.cssNode, YGUndefined); + YGNodeStyleSetWidth(self.cssNode, YGUndefined); + YGNodeStyleSetHeight(self.cssNode, YGUndefined); break; } } @@ -49,7 +49,7 @@ { [self applySizeConstraints]; - CSSNodeCalculateLayout(self.cssNode, YGUndefined, YGUndefined, YGDirectionInherit); + YGNodeCalculateLayout(self.cssNode, YGUndefined, YGUndefined, YGDirectionInherit); NSMutableSet *viewsWithNewFrame = [NSMutableSet set]; [self applyLayoutNode:self.cssNode viewsWithNewFrame:viewsWithNewFrame absolutePosition:CGPointZero]; diff --git a/React/Views/RCTShadowView.h b/React/Views/RCTShadowView.h index dd44ef0bd..3daeb22fc 100644 --- a/React/Views/RCTShadowView.h +++ b/React/Views/RCTShadowView.h @@ -9,7 +9,7 @@ #import -#import +#import #import #import @@ -44,7 +44,7 @@ typedef void (^RCTApplierBlock)(NSDictionary *viewRegistry - (void)removeReactSubview:(RCTShadowView *)subview NS_REQUIRES_SUPER; @property (nonatomic, weak, readonly) RCTShadowView *superview; -@property (nonatomic, assign, readonly) CSSNodeRef cssNode; +@property (nonatomic, assign, readonly) YGNodeRef cssNode; @property (nonatomic, copy) NSString *viewName; @property (nonatomic, strong) UIColor *backgroundColor; // Used to propagate to children @property (nonatomic, copy) RCTDirectEventBlock onLayout; @@ -183,21 +183,21 @@ typedef void (^RCTApplierBlock)(NSDictionary *viewRegistry * is split into two methods so subclasses can override `applyLayoutToChildren:` * while using default implementation of `applyLayoutNode:`. */ -- (void)applyLayoutNode:(CSSNodeRef)node +- (void)applyLayoutNode:(YGNodeRef)node viewsWithNewFrame:(NSMutableSet *)viewsWithNewFrame absolutePosition:(CGPoint)absolutePosition NS_REQUIRES_SUPER; /** * Enumerate the child nodes and tell them to apply layout. */ -- (void)applyLayoutToChildren:(CSSNodeRef)node +- (void)applyLayoutToChildren:(YGNodeRef)node viewsWithNewFrame:(NSMutableSet *)viewsWithNewFrame absolutePosition:(CGPoint)absolutePosition; /** - * Return whether or not this node acts as a leaf node in the eyes of CSSLayout. For example - * RCTShadowText has children which it does not want CSSLayout to lay out so in the eyes of - * CSSLayout it is a leaf node. + * Return whether or not this node acts as a leaf node in the eyes of Yoga. For example + * RCTShadowText has children which it does not want Yoga to lay out so in the eyes of + * Yoga it is a leaf node. */ - (BOOL)isCSSLeafNode; diff --git a/React/Views/RCTShadowView.m b/React/Views/RCTShadowView.m index d1651657f..d1fb3acdc 100644 --- a/React/Views/RCTShadowView.m +++ b/React/Views/RCTShadowView.m @@ -50,53 +50,53 @@ typedef NS_ENUM(unsigned int, meta_prop_t) { // cssNode api -static void RCTPrint(CSSNodeRef node) +static void RCTPrint(YGNodeRef node) { - RCTShadowView *shadowView = (__bridge RCTShadowView *)CSSNodeGetContext(node); + RCTShadowView *shadowView = (__bridge RCTShadowView *)YGNodeGetContext(node); printf("%s(%zd), ", shadowView.viewName.UTF8String, shadowView.reactTag.integerValue); } // Enforces precedence rules, e.g. marginLeft > marginHorizontal > margin. #define DEFINE_PROCESS_META_PROPS(type) \ -static void RCTProcessMetaProps##type(const float metaProps[META_PROP_COUNT], CSSNodeRef node) { \ - if (!CSSValueIsUndefined(metaProps[META_PROP_LEFT])) { \ - CSSNodeStyleSet##type(node, YGEdgeStart, metaProps[META_PROP_LEFT]); \ - } else if (!CSSValueIsUndefined(metaProps[META_PROP_HORIZONTAL])) { \ - CSSNodeStyleSet##type(node, YGEdgeStart, metaProps[META_PROP_HORIZONTAL]); \ - } else if (!CSSValueIsUndefined(metaProps[META_PROP_ALL])) { \ - CSSNodeStyleSet##type(node, YGEdgeStart, metaProps[META_PROP_ALL]); \ +static void RCTProcessMetaProps##type(const float metaProps[META_PROP_COUNT], YGNodeRef node) { \ + if (!YGValueIsUndefined(metaProps[META_PROP_LEFT])) { \ + YGNodeStyleSet##type(node, YGEdgeStart, metaProps[META_PROP_LEFT]); \ + } else if (!YGValueIsUndefined(metaProps[META_PROP_HORIZONTAL])) { \ + YGNodeStyleSet##type(node, YGEdgeStart, metaProps[META_PROP_HORIZONTAL]); \ + } else if (!YGValueIsUndefined(metaProps[META_PROP_ALL])) { \ + YGNodeStyleSet##type(node, YGEdgeStart, metaProps[META_PROP_ALL]); \ } else { \ - CSSNodeStyleSet##type(node, YGEdgeStart, 0); \ + YGNodeStyleSet##type(node, YGEdgeStart, 0); \ } \ \ - if (!CSSValueIsUndefined(metaProps[META_PROP_RIGHT])) { \ - CSSNodeStyleSet##type(node, YGEdgeEnd, metaProps[META_PROP_RIGHT]); \ - } else if (!CSSValueIsUndefined(metaProps[META_PROP_HORIZONTAL])) { \ - CSSNodeStyleSet##type(node, YGEdgeEnd, metaProps[META_PROP_HORIZONTAL]); \ - } else if (!CSSValueIsUndefined(metaProps[META_PROP_ALL])) { \ - CSSNodeStyleSet##type(node, YGEdgeEnd, metaProps[META_PROP_ALL]); \ + if (!YGValueIsUndefined(metaProps[META_PROP_RIGHT])) { \ + YGNodeStyleSet##type(node, YGEdgeEnd, metaProps[META_PROP_RIGHT]); \ + } else if (!YGValueIsUndefined(metaProps[META_PROP_HORIZONTAL])) { \ + YGNodeStyleSet##type(node, YGEdgeEnd, metaProps[META_PROP_HORIZONTAL]); \ + } else if (!YGValueIsUndefined(metaProps[META_PROP_ALL])) { \ + YGNodeStyleSet##type(node, YGEdgeEnd, metaProps[META_PROP_ALL]); \ } else { \ - CSSNodeStyleSet##type(node, YGEdgeEnd, 0); \ + YGNodeStyleSet##type(node, YGEdgeEnd, 0); \ } \ \ - if (!CSSValueIsUndefined(metaProps[META_PROP_TOP])) { \ - CSSNodeStyleSet##type(node, YGEdgeTop, metaProps[META_PROP_TOP]); \ - } else if (!CSSValueIsUndefined(metaProps[META_PROP_VERTICAL])) { \ - CSSNodeStyleSet##type(node, YGEdgeTop, metaProps[META_PROP_VERTICAL]); \ - } else if (!CSSValueIsUndefined(metaProps[META_PROP_ALL])) { \ - CSSNodeStyleSet##type(node, YGEdgeTop, metaProps[META_PROP_ALL]); \ + if (!YGValueIsUndefined(metaProps[META_PROP_TOP])) { \ + YGNodeStyleSet##type(node, YGEdgeTop, metaProps[META_PROP_TOP]); \ + } else if (!YGValueIsUndefined(metaProps[META_PROP_VERTICAL])) { \ + YGNodeStyleSet##type(node, YGEdgeTop, metaProps[META_PROP_VERTICAL]); \ + } else if (!YGValueIsUndefined(metaProps[META_PROP_ALL])) { \ + YGNodeStyleSet##type(node, YGEdgeTop, metaProps[META_PROP_ALL]); \ } else { \ - CSSNodeStyleSet##type(node, YGEdgeTop, 0); \ + YGNodeStyleSet##type(node, YGEdgeTop, 0); \ } \ \ - if (!CSSValueIsUndefined(metaProps[META_PROP_BOTTOM])) { \ - CSSNodeStyleSet##type(node, YGEdgeBottom, metaProps[META_PROP_BOTTOM]); \ - } else if (!CSSValueIsUndefined(metaProps[META_PROP_VERTICAL])) { \ - CSSNodeStyleSet##type(node, YGEdgeBottom, metaProps[META_PROP_VERTICAL]); \ - } else if (!CSSValueIsUndefined(metaProps[META_PROP_ALL])) { \ - CSSNodeStyleSet##type(node, YGEdgeBottom, metaProps[META_PROP_ALL]); \ + if (!YGValueIsUndefined(metaProps[META_PROP_BOTTOM])) { \ + YGNodeStyleSet##type(node, YGEdgeBottom, metaProps[META_PROP_BOTTOM]); \ + } else if (!YGValueIsUndefined(metaProps[META_PROP_VERTICAL])) { \ + YGNodeStyleSet##type(node, YGEdgeBottom, metaProps[META_PROP_VERTICAL]); \ + } else if (!YGValueIsUndefined(metaProps[META_PROP_ALL])) { \ + YGNodeStyleSet##type(node, YGEdgeBottom, metaProps[META_PROP_ALL]); \ } else { \ - CSSNodeStyleSet##type(node, YGEdgeBottom, 0); \ + YGNodeStyleSet##type(node, YGEdgeBottom, 0); \ } \ } @@ -133,38 +133,38 @@ DEFINE_PROCESS_META_PROPS(Border); // width = 213.5 - 106.5 = 107 // You'll notice that this is the same width we calculated for the parent view because we've taken its position into account. -- (void)applyLayoutNode:(CSSNodeRef)node +- (void)applyLayoutNode:(YGNodeRef)node viewsWithNewFrame:(NSMutableSet *)viewsWithNewFrame absolutePosition:(CGPoint)absolutePosition { - if (!CSSNodeGetHasNewLayout(node)) { + if (!YGNodeGetHasNewLayout(node)) { return; } - CSSNodeSetHasNewLayout(node, false); + YGNodeSetHasNewLayout(node, false); #if RCT_DEBUG // This works around a breaking change in css-layout where setting flexBasis needs to be set explicitly, instead of relying on flex to propagate. // We check for it by seeing if a width/height is provided along with a flexBasis of 0 and the width/height is laid out as 0. - if ((!CSSValueIsUndefined(CSSNodeStyleGetFlexBasis(node)) && CSSNodeStyleGetFlexBasis(node) == 0) && - ((!CSSValueIsUndefined(CSSNodeStyleGetWidth(node)) && CSSNodeStyleGetWidth(node) > 0 && CSSNodeLayoutGetWidth(node) == 0) || - (!CSSValueIsUndefined(CSSNodeStyleGetHeight(node)) && CSSNodeStyleGetHeight(node) > 0 && CSSNodeLayoutGetHeight(node) == 0))) { + if ((!YGValueIsUndefined(YGNodeStyleGetFlexBasis(node)) && YGNodeStyleGetFlexBasis(node) == 0) && + ((!YGValueIsUndefined(YGNodeStyleGetWidth(node)) && YGNodeStyleGetWidth(node) > 0 && YGNodeLayoutGetWidth(node) == 0) || + (!YGValueIsUndefined(YGNodeStyleGetHeight(node)) && YGNodeStyleGetHeight(node) > 0 && YGNodeLayoutGetHeight(node) == 0))) { RCTLogError(@"View was rendered with explicitly set width/height but with a 0 flexBasis. (This might be fixed by changing flex: to flexGrow:) View: %@", self); } #endif CGPoint absoluteTopLeft = { - absolutePosition.x + CSSNodeLayoutGetLeft(node), - absolutePosition.y + CSSNodeLayoutGetTop(node) + absolutePosition.x + YGNodeLayoutGetLeft(node), + absolutePosition.y + YGNodeLayoutGetTop(node) }; CGPoint absoluteBottomRight = { - absolutePosition.x + CSSNodeLayoutGetLeft(node) + CSSNodeLayoutGetWidth(node), - absolutePosition.y + CSSNodeLayoutGetTop(node) + CSSNodeLayoutGetHeight(node) + absolutePosition.x + YGNodeLayoutGetLeft(node) + YGNodeLayoutGetWidth(node), + absolutePosition.y + YGNodeLayoutGetTop(node) + YGNodeLayoutGetHeight(node) }; CGRect frame = {{ - RCTRoundPixelValue(CSSNodeLayoutGetLeft(node)), - RCTRoundPixelValue(CSSNodeLayoutGetTop(node)), + RCTRoundPixelValue(YGNodeLayoutGetLeft(node)), + RCTRoundPixelValue(YGNodeLayoutGetTop(node)), }, { RCTRoundPixelValue(absoluteBottomRight.x - absoluteTopLeft.x), RCTRoundPixelValue(absoluteBottomRight.y - absoluteTopLeft.y) @@ -175,19 +175,19 @@ DEFINE_PROCESS_META_PROPS(Border); [viewsWithNewFrame addObject:self]; } - absolutePosition.x += CSSNodeLayoutGetLeft(node); - absolutePosition.y += CSSNodeLayoutGetTop(node); + absolutePosition.x += YGNodeLayoutGetLeft(node); + absolutePosition.y += YGNodeLayoutGetTop(node); [self applyLayoutToChildren:node viewsWithNewFrame:viewsWithNewFrame absolutePosition:absolutePosition]; } -- (void)applyLayoutToChildren:(CSSNodeRef)node +- (void)applyLayoutToChildren:(YGNodeRef)node viewsWithNewFrame:(NSMutableSet *)viewsWithNewFrame absolutePosition:(CGPoint)absolutePosition { - for (unsigned int i = 0; i < CSSNodeChildCount(node); ++i) { + for (unsigned int i = 0; i < YGNodeChildCount(node); ++i) { RCTShadowView *child = (RCTShadowView *)_reactSubviews[i]; - [child applyLayoutNode:CSSNodeGetChild(node, i) + [child applyLayoutNode:YGNodeGetChild(node, i) viewsWithNewFrame:viewsWithNewFrame absolutePosition:absolutePosition]; } @@ -261,14 +261,14 @@ DEFINE_PROCESS_META_PROPS(Border); } if (!CGRectEqualToRect(frame, _frame)) { - CSSNodeStyleSetPositionType(_cssNode, YGPositionTypeAbsolute); - CSSNodeStyleSetWidth(_cssNode, frame.size.width); - CSSNodeStyleSetHeight(_cssNode, frame.size.height); - CSSNodeStyleSetPosition(_cssNode, YGEdgeLeft, frame.origin.x); - CSSNodeStyleSetPosition(_cssNode, YGEdgeTop, frame.origin.y); + YGNodeStyleSetPositionType(_cssNode, YGPositionTypeAbsolute); + YGNodeStyleSetWidth(_cssNode, frame.size.width); + YGNodeStyleSetHeight(_cssNode, frame.size.height); + YGNodeStyleSetPosition(_cssNode, YGEdgeLeft, frame.origin.x); + YGNodeStyleSetPosition(_cssNode, YGEdgeTop, frame.origin.y); } - CSSNodeCalculateLayout(_cssNode, frame.size.width, frame.size.height, YGDirectionInherit); + YGNodeCalculateLayout(_cssNode, frame.size.width, frame.size.height, YGDirectionInherit); [self applyLayoutNode:_cssNode viewsWithNewFrame:viewsWithNewFrame absolutePosition:absolutePosition]; } @@ -318,9 +318,9 @@ DEFINE_PROCESS_META_PROPS(Border); _reactSubviews = [NSMutableArray array]; - _cssNode = CSSNodeNew(); - CSSNodeSetContext(_cssNode, (__bridge void *)self); - CSSNodeSetPrintFunc(_cssNode, RCTPrint); + _cssNode = YGNodeNew(); + YGNodeSetContext(_cssNode, (__bridge void *)self); + YGNodeSetPrintFunc(_cssNode, RCTPrint); } return self; } @@ -332,7 +332,7 @@ DEFINE_PROCESS_META_PROPS(Border); - (void)dealloc { - CSSNodeFree(_cssNode); + YGNodeFree(_cssNode); } - (BOOL)isCSSLeafNode @@ -375,7 +375,7 @@ DEFINE_PROCESS_META_PROPS(Border); { [_reactSubviews insertObject:subview atIndex:atIndex]; if (![self isCSSLeafNode]) { - CSSNodeInsertChild(_cssNode, subview.cssNode, (uint32_t)atIndex); + YGNodeInsertChild(_cssNode, subview.cssNode, (uint32_t)atIndex); } subview->_superview = self; _didUpdateSubviews = YES; @@ -391,7 +391,7 @@ DEFINE_PROCESS_META_PROPS(Border); subview->_superview = nil; [_reactSubviews removeObject:subview]; if (![self isCSSLeafNode]) { - CSSNodeRemoveChild(_cssNode, subview.cssNode); + YGNodeRemoveChild(_cssNode, subview.cssNode); } } @@ -491,27 +491,27 @@ RCT_PADDING_PROPERTY(Right, RIGHT) - (UIEdgeInsets)paddingAsInsets { - if (CSSNodeLayoutGetDirection(_cssNode) == YGDirectionRTL) { + if (YGNodeLayoutGetDirection(_cssNode) == YGDirectionRTL) { return (UIEdgeInsets){ - CSSNodeStyleGetPadding(_cssNode, YGEdgeTop), - !CSSValueIsUndefined(CSSNodeStyleGetPadding(_cssNode, YGEdgeEnd)) ? - CSSNodeStyleGetPadding(_cssNode, YGEdgeEnd) : - CSSNodeStyleGetPadding(_cssNode, YGEdgeLeft), - CSSNodeStyleGetPadding(_cssNode, YGEdgeBottom), - !CSSValueIsUndefined(CSSNodeStyleGetPadding(_cssNode, YGEdgeStart)) ? - CSSNodeStyleGetPadding(_cssNode, YGEdgeStart) : - CSSNodeStyleGetPadding(_cssNode, YGEdgeRight) + YGNodeStyleGetPadding(_cssNode, YGEdgeTop), + !YGValueIsUndefined(YGNodeStyleGetPadding(_cssNode, YGEdgeEnd)) ? + YGNodeStyleGetPadding(_cssNode, YGEdgeEnd) : + YGNodeStyleGetPadding(_cssNode, YGEdgeLeft), + YGNodeStyleGetPadding(_cssNode, YGEdgeBottom), + !YGValueIsUndefined(YGNodeStyleGetPadding(_cssNode, YGEdgeStart)) ? + YGNodeStyleGetPadding(_cssNode, YGEdgeStart) : + YGNodeStyleGetPadding(_cssNode, YGEdgeRight) }; } else { return (UIEdgeInsets){ - CSSNodeStyleGetPadding(_cssNode, YGEdgeTop), - !CSSValueIsUndefined(CSSNodeStyleGetPadding(_cssNode, YGEdgeStart)) ? - CSSNodeStyleGetPadding(_cssNode, YGEdgeStart) : - CSSNodeStyleGetPadding(_cssNode, YGEdgeLeft), - CSSNodeStyleGetPadding(_cssNode, YGEdgeBottom), - !CSSValueIsUndefined(CSSNodeStyleGetPadding(_cssNode, YGEdgeEnd)) ? - CSSNodeStyleGetPadding(_cssNode, YGEdgeEnd) : - CSSNodeStyleGetPadding(_cssNode, YGEdgeRight) + YGNodeStyleGetPadding(_cssNode, YGEdgeTop), + !YGValueIsUndefined(YGNodeStyleGetPadding(_cssNode, YGEdgeStart)) ? + YGNodeStyleGetPadding(_cssNode, YGEdgeStart) : + YGNodeStyleGetPadding(_cssNode, YGEdgeLeft), + YGNodeStyleGetPadding(_cssNode, YGEdgeBottom), + !YGValueIsUndefined(YGNodeStyleGetPadding(_cssNode, YGEdgeEnd)) ? + YGNodeStyleGetPadding(_cssNode, YGEdgeEnd) : + YGNodeStyleGetPadding(_cssNode, YGEdgeRight) }; } } @@ -541,12 +541,12 @@ RCT_BORDER_PROPERTY(Right, RIGHT) #define RCT_DIMENSION_PROPERTY(setProp, getProp, cssProp) \ - (void)set##setProp:(float)value \ { \ - CSSNodeStyleSet##cssProp(_cssNode, value); \ + YGNodeStyleSet##cssProp(_cssNode, value); \ [self dirtyText]; \ } \ - (float)getProp \ { \ - return CSSNodeStyleGet##cssProp(_cssNode); \ + return YGNodeStyleGet##cssProp(_cssNode); \ } RCT_DIMENSION_PROPERTY(Width, width, Width) @@ -561,12 +561,12 @@ RCT_DIMENSION_PROPERTY(MaxHeight, maxHeight, MaxHeight) #define RCT_POSITION_PROPERTY(setProp, getProp, edge) \ - (void)set##setProp:(float)value \ { \ - CSSNodeStyleSetPosition(_cssNode, edge, value); \ + YGNodeStyleSetPosition(_cssNode, edge, value); \ [self dirtyText]; \ } \ - (float)getProp \ { \ - return CSSNodeStyleGetPosition(_cssNode, edge); \ + return YGNodeStyleGetPosition(_cssNode, edge); \ } RCT_POSITION_PROPERTY(Top, top, YGEdgeTop) @@ -578,25 +578,25 @@ RCT_POSITION_PROPERTY(Left, left, YGEdgeStart) { if (!CGRectEqualToRect(frame, _frame)) { _frame = frame; - CSSNodeStyleSetPosition(_cssNode, YGEdgeLeft, CGRectGetMinX(frame)); - CSSNodeStyleSetPosition(_cssNode, YGEdgeTop, CGRectGetMinY(frame)); - CSSNodeStyleSetWidth(_cssNode, CGRectGetWidth(frame)); - CSSNodeStyleSetHeight(_cssNode, CGRectGetHeight(frame)); + YGNodeStyleSetPosition(_cssNode, YGEdgeLeft, CGRectGetMinX(frame)); + YGNodeStyleSetPosition(_cssNode, YGEdgeTop, CGRectGetMinY(frame)); + YGNodeStyleSetWidth(_cssNode, CGRectGetWidth(frame)); + YGNodeStyleSetHeight(_cssNode, CGRectGetHeight(frame)); } } -static inline void RCTAssignSuggestedDimension(CSSNodeRef cssNode, YGDimension dimension, CGFloat amount) +static inline void RCTAssignSuggestedDimension(YGNodeRef cssNode, YGDimension dimension, CGFloat amount) { if (amount != UIViewNoIntrinsicMetric) { switch (dimension) { case YGDimensionWidth: - if (isnan(CSSNodeStyleGetWidth(cssNode))) { - CSSNodeStyleSetWidth(cssNode, amount); + if (isnan(YGNodeStyleGetWidth(cssNode))) { + YGNodeStyleSetWidth(cssNode, amount); } break; case YGDimensionHeight: - if (isnan(CSSNodeStyleGetHeight(cssNode))) { - CSSNodeStyleSetHeight(cssNode, amount); + if (isnan(YGNodeStyleGetHeight(cssNode))) { + YGNodeStyleSetHeight(cssNode, amount); } break; case YGDimensionCount: @@ -607,7 +607,7 @@ static inline void RCTAssignSuggestedDimension(CSSNodeRef cssNode, YGDimension d - (void)setIntrinsicContentSize:(CGSize)size { - if (CSSNodeStyleGetFlexGrow(_cssNode) == 0 && CSSNodeStyleGetFlexShrink(_cssNode) == 0) { + if (YGNodeStyleGetFlexGrow(_cssNode) == 0 && YGNodeStyleGetFlexShrink(_cssNode) == 0) { RCTAssignSuggestedDimension(_cssNode, YGDimensionHeight, size.height); RCTAssignSuggestedDimension(_cssNode, YGDimensionWidth, size.width); } @@ -615,31 +615,31 @@ static inline void RCTAssignSuggestedDimension(CSSNodeRef cssNode, YGDimension d - (void)setTopLeft:(CGPoint)topLeft { - CSSNodeStyleSetPosition(_cssNode, YGEdgeLeft, topLeft.x); - CSSNodeStyleSetPosition(_cssNode, YGEdgeTop, topLeft.y); + YGNodeStyleSetPosition(_cssNode, YGEdgeLeft, topLeft.x); + YGNodeStyleSetPosition(_cssNode, YGEdgeTop, topLeft.y); } - (void)setSize:(CGSize)size { - CSSNodeStyleSetWidth(_cssNode, size.width); - CSSNodeStyleSetHeight(_cssNode, size.height); + YGNodeStyleSetWidth(_cssNode, size.width); + YGNodeStyleSetHeight(_cssNode, size.height); } // Flex - (void)setFlex:(float)value { - CSSNodeStyleSetFlex(_cssNode, value); + YGNodeStyleSetFlex(_cssNode, value); } #define RCT_STYLE_PROPERTY(setProp, getProp, cssProp, type) \ - (void)set##setProp:(type)value \ { \ - CSSNodeStyleSet##cssProp(_cssNode, value); \ + YGNodeStyleSet##cssProp(_cssNode, value); \ } \ - (type)getProp \ { \ - return CSSNodeStyleGet##cssProp(_cssNode); \ + return YGNodeStyleGet##cssProp(_cssNode); \ } RCT_STYLE_PROPERTY(FlexGrow, flexGrow, FlexGrow, float) diff --git a/ReactAndroid/src/main/java/com/facebook/csslayout/CSSNode.java b/ReactAndroid/src/main/java/com/facebook/csslayout/CSSNode.java index 1fc2af871..90fe46768 100644 --- a/ReactAndroid/src/main/java/com/facebook/csslayout/CSSNode.java +++ b/ReactAndroid/src/main/java/com/facebook/csslayout/CSSNode.java @@ -27,26 +27,26 @@ public class CSSNode implements CSSNodeAPI { /** * Get native instance count. Useful for testing only. */ - static native int jni_CSSNodeGetInstanceCount(); - static native void jni_CSSLog(int level, String message); + static native int jni_YGNodeGetInstanceCount(); + static native void jni_YGLog(int level, String message); - private static native void jni_CSSLayoutSetLogger(Object logger); + private static native void jni_YGSetLogger(Object logger); public static void setLogger(CSSLogger logger) { - jni_CSSLayoutSetLogger(logger); + jni_YGSetLogger(logger); } - private static native void jni_CSSLayoutSetExperimentalFeatureEnabled( + private static native void jni_YGSetExperimentalFeatureEnabled( int feature, boolean enabled); public static void setExperimentalFeatureEnabled( YogaExperimentalFeature feature, boolean enabled) { - jni_CSSLayoutSetExperimentalFeatureEnabled(feature.intValue(), enabled); + jni_YGSetExperimentalFeatureEnabled(feature.intValue(), enabled); } - private static native boolean jni_CSSLayoutIsExperimentalFeatureEnabled(int feature); + private static native boolean jni_YGIsExperimentalFeatureEnabled(int feature); public static boolean isExperimentalFeatureEnabled(YogaExperimentalFeature feature) { - return jni_CSSLayoutIsExperimentalFeatureEnabled(feature.intValue()); + return jni_YGIsExperimentalFeatureEnabled(feature.intValue()); } private CSSNode mParent; @@ -71,25 +71,25 @@ public class CSSNode implements CSSNodeAPI { @DoNotStrip private int mLayoutDirection = 0; - private native long jni_CSSNodeNew(); + private native long jni_YGNodeNew(); public CSSNode() { - mNativePointer = jni_CSSNodeNew(); + mNativePointer = jni_YGNodeNew(); if (mNativePointer == 0) { throw new IllegalStateException("Failed to allocate native memory"); } } - private native void jni_CSSNodeFree(long nativePointer); + private native void jni_YGNodeFree(long nativePointer); @Override protected void finalize() throws Throwable { try { - jni_CSSNodeFree(mNativePointer); + jni_YGNodeFree(mNativePointer); } finally { super.finalize(); } } - private native void jni_CSSNodeReset(long nativePointer); + private native void jni_YGNodeReset(long nativePointer); @Override public void reset() { mHasSetPadding = false; @@ -106,7 +106,7 @@ public class CSSNode implements CSSNodeAPI { mMeasureFunction = null; mData = null; - jni_CSSNodeReset(mNativePointer); + jni_YGNodeReset(mNativePointer); } @Override @@ -119,7 +119,7 @@ public class CSSNode implements CSSNodeAPI { return mChildren.get(i); } - private native void jni_CSSNodeInsertChild(long nativePointer, long childPointer, int index); + private native void jni_YGNodeInsertChild(long nativePointer, long childPointer, int index); @Override public void addChildAt(CSSNode child, int i) { if (child.mParent != null) { @@ -131,16 +131,16 @@ public class CSSNode implements CSSNodeAPI { } mChildren.add(i, child); child.mParent = this; - jni_CSSNodeInsertChild(mNativePointer, child.mNativePointer, i); + jni_YGNodeInsertChild(mNativePointer, child.mNativePointer, i); } - private native void jni_CSSNodeRemoveChild(long nativePointer, long childPointer); + private native void jni_YGNodeRemoveChild(long nativePointer, long childPointer); @Override public CSSNode removeChildAt(int i) { final CSSNode child = mChildren.remove(i); child.mParent = null; - jni_CSSNodeRemoveChild(mNativePointer, child.mNativePointer); + jni_YGNodeRemoveChild(mNativePointer, child.mNativePointer); return child; } @@ -155,330 +155,330 @@ public class CSSNode implements CSSNodeAPI { return mChildren == null ? -1 : mChildren.indexOf(child); } - private native void jni_CSSNodeCalculateLayout(long nativePointer); + private native void jni_YGNodeCalculateLayout(long nativePointer); @Override public void calculateLayout() { - jni_CSSNodeCalculateLayout(mNativePointer); + jni_YGNodeCalculateLayout(mNativePointer); } - private native boolean jni_CSSNodeHasNewLayout(long nativePointer); + private native boolean jni_YGNodeHasNewLayout(long nativePointer); @Override public boolean hasNewLayout() { - return jni_CSSNodeHasNewLayout(mNativePointer); + return jni_YGNodeHasNewLayout(mNativePointer); } - private native void jni_CSSNodeMarkDirty(long nativePointer); + private native void jni_YGNodeMarkDirty(long nativePointer); @Override public void dirty() { - jni_CSSNodeMarkDirty(mNativePointer); + jni_YGNodeMarkDirty(mNativePointer); } - private native boolean jni_CSSNodeIsDirty(long nativePointer); + private native boolean jni_YGNodeIsDirty(long nativePointer); @Override public boolean isDirty() { - return jni_CSSNodeIsDirty(mNativePointer); + return jni_YGNodeIsDirty(mNativePointer); } - private native void jni_CSSNodeMarkLayoutSeen(long nativePointer); + private native void jni_YGNodeMarkLayoutSeen(long nativePointer); @Override public void markLayoutSeen() { - jni_CSSNodeMarkLayoutSeen(mNativePointer); + jni_YGNodeMarkLayoutSeen(mNativePointer); } - private native void jni_CSSNodeCopyStyle(long dstNativePointer, long srcNativePointer); + private native void jni_YGNodeCopyStyle(long dstNativePointer, long srcNativePointer); @Override public void copyStyle(CSSNode srcNode) { - jni_CSSNodeCopyStyle(mNativePointer, srcNode.mNativePointer); + jni_YGNodeCopyStyle(mNativePointer, srcNode.mNativePointer); } - private native int jni_CSSNodeStyleGetDirection(long nativePointer); + private native int jni_YGNodeStyleGetDirection(long nativePointer); @Override public YogaDirection getStyleDirection() { - return YogaDirection.values()[jni_CSSNodeStyleGetDirection(mNativePointer)]; + return YogaDirection.values()[jni_YGNodeStyleGetDirection(mNativePointer)]; } - private native void jni_CSSNodeStyleSetDirection(long nativePointer, int direction); + private native void jni_YGNodeStyleSetDirection(long nativePointer, int direction); @Override public void setDirection(YogaDirection direction) { - jni_CSSNodeStyleSetDirection(mNativePointer, direction.intValue()); + jni_YGNodeStyleSetDirection(mNativePointer, direction.intValue()); } - private native int jni_CSSNodeStyleGetFlexDirection(long nativePointer); + private native int jni_YGNodeStyleGetFlexDirection(long nativePointer); @Override public YogaFlexDirection getFlexDirection() { - return YogaFlexDirection.values()[jni_CSSNodeStyleGetFlexDirection(mNativePointer)]; + return YogaFlexDirection.values()[jni_YGNodeStyleGetFlexDirection(mNativePointer)]; } - private native void jni_CSSNodeStyleSetFlexDirection(long nativePointer, int flexDirection); + private native void jni_YGNodeStyleSetFlexDirection(long nativePointer, int flexDirection); @Override public void setFlexDirection(YogaFlexDirection flexDirection) { - jni_CSSNodeStyleSetFlexDirection(mNativePointer, flexDirection.intValue()); + jni_YGNodeStyleSetFlexDirection(mNativePointer, flexDirection.intValue()); } - private native int jni_CSSNodeStyleGetJustifyContent(long nativePointer); + private native int jni_YGNodeStyleGetJustifyContent(long nativePointer); @Override public YogaJustify getJustifyContent() { - return YogaJustify.values()[jni_CSSNodeStyleGetJustifyContent(mNativePointer)]; + return YogaJustify.values()[jni_YGNodeStyleGetJustifyContent(mNativePointer)]; } - private native void jni_CSSNodeStyleSetJustifyContent(long nativePointer, int justifyContent); + private native void jni_YGNodeStyleSetJustifyContent(long nativePointer, int justifyContent); @Override public void setJustifyContent(YogaJustify justifyContent) { - jni_CSSNodeStyleSetJustifyContent(mNativePointer, justifyContent.intValue()); + jni_YGNodeStyleSetJustifyContent(mNativePointer, justifyContent.intValue()); } - private native int jni_CSSNodeStyleGetAlignItems(long nativePointer); + private native int jni_YGNodeStyleGetAlignItems(long nativePointer); @Override public YogaAlign getAlignItems() { - return YogaAlign.values()[jni_CSSNodeStyleGetAlignItems(mNativePointer)]; + return YogaAlign.values()[jni_YGNodeStyleGetAlignItems(mNativePointer)]; } - private native void jni_CSSNodeStyleSetAlignItems(long nativePointer, int alignItems); + private native void jni_YGNodeStyleSetAlignItems(long nativePointer, int alignItems); @Override public void setAlignItems(YogaAlign alignItems) { - jni_CSSNodeStyleSetAlignItems(mNativePointer, alignItems.intValue()); + jni_YGNodeStyleSetAlignItems(mNativePointer, alignItems.intValue()); } - private native int jni_CSSNodeStyleGetAlignSelf(long nativePointer); + private native int jni_YGNodeStyleGetAlignSelf(long nativePointer); @Override public YogaAlign getAlignSelf() { - return YogaAlign.values()[jni_CSSNodeStyleGetAlignSelf(mNativePointer)]; + return YogaAlign.values()[jni_YGNodeStyleGetAlignSelf(mNativePointer)]; } - private native void jni_CSSNodeStyleSetAlignSelf(long nativePointer, int alignSelf); + private native void jni_YGNodeStyleSetAlignSelf(long nativePointer, int alignSelf); @Override public void setAlignSelf(YogaAlign alignSelf) { - jni_CSSNodeStyleSetAlignSelf(mNativePointer, alignSelf.intValue()); + jni_YGNodeStyleSetAlignSelf(mNativePointer, alignSelf.intValue()); } - private native int jni_CSSNodeStyleGetAlignContent(long nativePointer); + private native int jni_YGNodeStyleGetAlignContent(long nativePointer); @Override public YogaAlign getAlignContent() { - return YogaAlign.values()[jni_CSSNodeStyleGetAlignContent(mNativePointer)]; + return YogaAlign.values()[jni_YGNodeStyleGetAlignContent(mNativePointer)]; } - private native void jni_CSSNodeStyleSetAlignContent(long nativePointer, int alignContent); + private native void jni_YGNodeStyleSetAlignContent(long nativePointer, int alignContent); @Override public void setAlignContent(YogaAlign alignContent) { - jni_CSSNodeStyleSetAlignContent(mNativePointer, alignContent.intValue()); + jni_YGNodeStyleSetAlignContent(mNativePointer, alignContent.intValue()); } - private native int jni_CSSNodeStyleGetPositionType(long nativePointer); + private native int jni_YGNodeStyleGetPositionType(long nativePointer); @Override public YogaPositionType getPositionType() { - return YogaPositionType.values()[jni_CSSNodeStyleGetPositionType(mNativePointer)]; + return YogaPositionType.values()[jni_YGNodeStyleGetPositionType(mNativePointer)]; } - private native void jni_CSSNodeStyleSetPositionType(long nativePointer, int positionType); + private native void jni_YGNodeStyleSetPositionType(long nativePointer, int positionType); @Override public void setPositionType(YogaPositionType positionType) { - jni_CSSNodeStyleSetPositionType(mNativePointer, positionType.intValue()); + jni_YGNodeStyleSetPositionType(mNativePointer, positionType.intValue()); } - private native void jni_CSSNodeStyleSetFlexWrap(long nativePointer, int wrapType); + private native void jni_YGNodeStyleSetFlexWrap(long nativePointer, int wrapType); @Override public void setWrap(YogaWrap flexWrap) { - jni_CSSNodeStyleSetFlexWrap(mNativePointer, flexWrap.intValue()); + jni_YGNodeStyleSetFlexWrap(mNativePointer, flexWrap.intValue()); } - private native int jni_CSSNodeStyleGetOverflow(long nativePointer); + private native int jni_YGNodeStyleGetOverflow(long nativePointer); @Override public YogaOverflow getOverflow() { - return YogaOverflow.values()[jni_CSSNodeStyleGetOverflow(mNativePointer)]; + return YogaOverflow.values()[jni_YGNodeStyleGetOverflow(mNativePointer)]; } - private native void jni_CSSNodeStyleSetOverflow(long nativePointer, int overflow); + private native void jni_YGNodeStyleSetOverflow(long nativePointer, int overflow); @Override public void setOverflow(YogaOverflow overflow) { - jni_CSSNodeStyleSetOverflow(mNativePointer, overflow.intValue()); + jni_YGNodeStyleSetOverflow(mNativePointer, overflow.intValue()); } - private native void jni_CSSNodeStyleSetFlex(long nativePointer, float flex); + private native void jni_YGNodeStyleSetFlex(long nativePointer, float flex); @Override public void setFlex(float flex) { - jni_CSSNodeStyleSetFlex(mNativePointer, flex); + jni_YGNodeStyleSetFlex(mNativePointer, flex); } - private native float jni_CSSNodeStyleGetFlexGrow(long nativePointer); + private native float jni_YGNodeStyleGetFlexGrow(long nativePointer); @Override public float getFlexGrow() { - return jni_CSSNodeStyleGetFlexGrow(mNativePointer); + return jni_YGNodeStyleGetFlexGrow(mNativePointer); } - private native void jni_CSSNodeStyleSetFlexGrow(long nativePointer, float flexGrow); + private native void jni_YGNodeStyleSetFlexGrow(long nativePointer, float flexGrow); @Override public void setFlexGrow(float flexGrow) { - jni_CSSNodeStyleSetFlexGrow(mNativePointer, flexGrow); + jni_YGNodeStyleSetFlexGrow(mNativePointer, flexGrow); } - private native float jni_CSSNodeStyleGetFlexShrink(long nativePointer); + private native float jni_YGNodeStyleGetFlexShrink(long nativePointer); @Override public float getFlexShrink() { - return jni_CSSNodeStyleGetFlexShrink(mNativePointer); + return jni_YGNodeStyleGetFlexShrink(mNativePointer); } - private native void jni_CSSNodeStyleSetFlexShrink(long nativePointer, float flexShrink); + private native void jni_YGNodeStyleSetFlexShrink(long nativePointer, float flexShrink); @Override public void setFlexShrink(float flexShrink) { - jni_CSSNodeStyleSetFlexShrink(mNativePointer, flexShrink); + jni_YGNodeStyleSetFlexShrink(mNativePointer, flexShrink); } - private native float jni_CSSNodeStyleGetFlexBasis(long nativePointer); + private native float jni_YGNodeStyleGetFlexBasis(long nativePointer); @Override public float getFlexBasis() { - return jni_CSSNodeStyleGetFlexBasis(mNativePointer); + return jni_YGNodeStyleGetFlexBasis(mNativePointer); } - private native void jni_CSSNodeStyleSetFlexBasis(long nativePointer, float flexBasis); + private native void jni_YGNodeStyleSetFlexBasis(long nativePointer, float flexBasis); @Override public void setFlexBasis(float flexBasis) { - jni_CSSNodeStyleSetFlexBasis(mNativePointer, flexBasis); + jni_YGNodeStyleSetFlexBasis(mNativePointer, flexBasis); } - private native float jni_CSSNodeStyleGetMargin(long nativePointer, int edge); + private native float jni_YGNodeStyleGetMargin(long nativePointer, int edge); @Override public float getMargin(YogaEdge edge) { if (!mHasSetMargin) { return edge.intValue() < YogaEdge.START.intValue() ? 0 : YogaConstants.UNDEFINED; } - return jni_CSSNodeStyleGetMargin(mNativePointer, edge.intValue()); + return jni_YGNodeStyleGetMargin(mNativePointer, edge.intValue()); } - private native void jni_CSSNodeStyleSetMargin(long nativePointer, int edge, float margin); + private native void jni_YGNodeStyleSetMargin(long nativePointer, int edge, float margin); @Override public void setMargin(YogaEdge edge, float margin) { mHasSetMargin = true; - jni_CSSNodeStyleSetMargin(mNativePointer, edge.intValue(), margin); + jni_YGNodeStyleSetMargin(mNativePointer, edge.intValue(), margin); } - private native float jni_CSSNodeStyleGetPadding(long nativePointer, int edge); + private native float jni_YGNodeStyleGetPadding(long nativePointer, int edge); @Override public float getPadding(YogaEdge edge) { if (!mHasSetPadding) { return edge.intValue() < YogaEdge.START.intValue() ? 0 : YogaConstants.UNDEFINED; } - return jni_CSSNodeStyleGetPadding(mNativePointer, edge.intValue()); + return jni_YGNodeStyleGetPadding(mNativePointer, edge.intValue()); } - private native void jni_CSSNodeStyleSetPadding(long nativePointer, int edge, float padding); + private native void jni_YGNodeStyleSetPadding(long nativePointer, int edge, float padding); @Override public void setPadding(YogaEdge edge, float padding) { mHasSetPadding = true; - jni_CSSNodeStyleSetPadding(mNativePointer, edge.intValue(), padding); + jni_YGNodeStyleSetPadding(mNativePointer, edge.intValue(), padding); } - private native float jni_CSSNodeStyleGetBorder(long nativePointer, int edge); + private native float jni_YGNodeStyleGetBorder(long nativePointer, int edge); @Override public float getBorder(YogaEdge edge) { if (!mHasSetBorder) { return edge.intValue() < YogaEdge.START.intValue() ? 0 : YogaConstants.UNDEFINED; } - return jni_CSSNodeStyleGetBorder(mNativePointer, edge.intValue()); + return jni_YGNodeStyleGetBorder(mNativePointer, edge.intValue()); } - private native void jni_CSSNodeStyleSetBorder(long nativePointer, int edge, float border); + private native void jni_YGNodeStyleSetBorder(long nativePointer, int edge, float border); @Override public void setBorder(YogaEdge edge, float border) { mHasSetBorder = true; - jni_CSSNodeStyleSetBorder(mNativePointer, edge.intValue(), border); + jni_YGNodeStyleSetBorder(mNativePointer, edge.intValue(), border); } - private native float jni_CSSNodeStyleGetPosition(long nativePointer, int edge); + private native float jni_YGNodeStyleGetPosition(long nativePointer, int edge); @Override public float getPosition(YogaEdge edge) { if (!mHasSetPosition) { return YogaConstants.UNDEFINED; } - return jni_CSSNodeStyleGetPosition(mNativePointer, edge.intValue()); + return jni_YGNodeStyleGetPosition(mNativePointer, edge.intValue()); } - private native void jni_CSSNodeStyleSetPosition(long nativePointer, int edge, float position); + private native void jni_YGNodeStyleSetPosition(long nativePointer, int edge, float position); @Override public void setPosition(YogaEdge edge, float position) { mHasSetPosition = true; - jni_CSSNodeStyleSetPosition(mNativePointer, edge.intValue(), position); + jni_YGNodeStyleSetPosition(mNativePointer, edge.intValue(), position); } - private native float jni_CSSNodeStyleGetWidth(long nativePointer); + private native float jni_YGNodeStyleGetWidth(long nativePointer); @Override public float getWidth() { - return jni_CSSNodeStyleGetWidth(mNativePointer); + return jni_YGNodeStyleGetWidth(mNativePointer); } - private native void jni_CSSNodeStyleSetWidth(long nativePointer, float width); + private native void jni_YGNodeStyleSetWidth(long nativePointer, float width); @Override public void setWidth(float width) { - jni_CSSNodeStyleSetWidth(mNativePointer, width); + jni_YGNodeStyleSetWidth(mNativePointer, width); } - private native float jni_CSSNodeStyleGetHeight(long nativePointer); + private native float jni_YGNodeStyleGetHeight(long nativePointer); @Override public float getHeight() { - return jni_CSSNodeStyleGetHeight(mNativePointer); + return jni_YGNodeStyleGetHeight(mNativePointer); } - private native void jni_CSSNodeStyleSetHeight(long nativePointer, float height); + private native void jni_YGNodeStyleSetHeight(long nativePointer, float height); @Override public void setHeight(float height) { - jni_CSSNodeStyleSetHeight(mNativePointer, height); + jni_YGNodeStyleSetHeight(mNativePointer, height); } - private native float jni_CSSNodeStyleGetMinWidth(long nativePointer); + private native float jni_YGNodeStyleGetMinWidth(long nativePointer); @Override public float getMinWidth() { - return jni_CSSNodeStyleGetMinWidth(mNativePointer); + return jni_YGNodeStyleGetMinWidth(mNativePointer); } - private native void jni_CSSNodeStyleSetMinWidth(long nativePointer, float minWidth); + private native void jni_YGNodeStyleSetMinWidth(long nativePointer, float minWidth); @Override public void setMinWidth(float minWidth) { - jni_CSSNodeStyleSetMinWidth(mNativePointer, minWidth); + jni_YGNodeStyleSetMinWidth(mNativePointer, minWidth); } - private native float jni_CSSNodeStyleGetMinHeight(long nativePointer); + private native float jni_YGNodeStyleGetMinHeight(long nativePointer); @Override public float getMinHeight() { - return jni_CSSNodeStyleGetMinHeight(mNativePointer); + return jni_YGNodeStyleGetMinHeight(mNativePointer); } - private native void jni_CSSNodeStyleSetMinHeight(long nativePointer, float minHeight); + private native void jni_YGNodeStyleSetMinHeight(long nativePointer, float minHeight); @Override public void setMinHeight(float minHeight) { - jni_CSSNodeStyleSetMinHeight(mNativePointer, minHeight); + jni_YGNodeStyleSetMinHeight(mNativePointer, minHeight); } - private native float jni_CSSNodeStyleGetMaxWidth(long nativePointer); + private native float jni_YGNodeStyleGetMaxWidth(long nativePointer); @Override public float getMaxWidth() { - return jni_CSSNodeStyleGetMaxWidth(mNativePointer); + return jni_YGNodeStyleGetMaxWidth(mNativePointer); } - private native void jni_CSSNodeStyleSetMaxWidth(long nativePointer, float maxWidth); + private native void jni_YGNodeStyleSetMaxWidth(long nativePointer, float maxWidth); @Override public void setMaxWidth(float maxWidth) { - jni_CSSNodeStyleSetMaxWidth(mNativePointer, maxWidth); + jni_YGNodeStyleSetMaxWidth(mNativePointer, maxWidth); } - private native float jni_CSSNodeStyleGetMaxHeight(long nativePointer); + private native float jni_YGNodeStyleGetMaxHeight(long nativePointer); @Override public float getMaxHeight() { - return jni_CSSNodeStyleGetMaxHeight(mNativePointer); + return jni_YGNodeStyleGetMaxHeight(mNativePointer); } - private native void jni_CSSNodeStyleSetMaxHeight(long nativePointer, float maxheight); + private native void jni_YGNodeStyleSetMaxHeight(long nativePointer, float maxheight); @Override public void setMaxHeight(float maxheight) { - jni_CSSNodeStyleSetMaxHeight(mNativePointer, maxheight); + jni_YGNodeStyleSetMaxHeight(mNativePointer, maxheight); } - private native float jni_CSSNodeStyleGetAspectRatio(long nativePointer); + private native float jni_YGNodeStyleGetAspectRatio(long nativePointer); public float getAspectRatio() { - return jni_CSSNodeStyleGetAspectRatio(mNativePointer); + return jni_YGNodeStyleGetAspectRatio(mNativePointer); } - private native void jni_CSSNodeStyleSetAspectRatio(long nativePointer, float aspectRatio); + private native void jni_YGNodeStyleSetAspectRatio(long nativePointer, float aspectRatio); public void setAspectRatio(float aspectRatio) { - jni_CSSNodeStyleSetAspectRatio(mNativePointer, aspectRatio); + jni_YGNodeStyleSetAspectRatio(mNativePointer, aspectRatio); } @Override @@ -506,11 +506,11 @@ public class CSSNode implements CSSNodeAPI { return YogaDirection.values()[mLayoutDirection]; } - private native void jni_CSSNodeSetHasMeasureFunc(long nativePointer, boolean hasMeasureFunc); + private native void jni_YGNodeSetHasMeasureFunc(long nativePointer, boolean hasMeasureFunc); @Override public void setMeasureFunction(MeasureFunction measureFunction) { mMeasureFunction = measureFunction; - jni_CSSNodeSetHasMeasureFunc(mNativePointer, measureFunction != null); + jni_YGNodeSetHasMeasureFunc(mNativePointer, measureFunction != null); } // Implementation Note: Why this method needs to stay final diff --git a/ReactAndroid/src/main/jni/first-party/csslayoutjni/Android.mk b/ReactAndroid/src/main/jni/first-party/csslayoutjni/Android.mk index 125a7ba9d..dc4401b19 100644 --- a/ReactAndroid/src/main/jni/first-party/csslayoutjni/Android.mk +++ b/ReactAndroid/src/main/jni/first-party/csslayoutjni/Android.mk @@ -5,7 +5,7 @@ include $(CLEAR_VARS) LOCAL_MODULE := csslayout LOCAL_SRC_FILES := \ - jni/CSSJNI.cpp + jni/YGJNI.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/jni diff --git a/ReactAndroid/src/main/jni/first-party/csslayoutjni/jni/CSSJNI.cpp b/ReactAndroid/src/main/jni/first-party/csslayoutjni/jni/CSSJNI.cpp deleted file mode 100644 index 1a01570d7..000000000 --- a/ReactAndroid/src/main/jni/first-party/csslayoutjni/jni/CSSJNI.cpp +++ /dev/null @@ -1,340 +0,0 @@ -/** - * 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. - */ - -#include -#include -#include - -using namespace facebook::jni; -using namespace std; - -static inline weak_ref *jobjectContext(CSSNodeRef node) { - return reinterpret_cast *>(CSSNodeGetContext(node)); -} - -static void _jniTransferLayoutDirection(CSSNodeRef node, alias_ref javaNode) { - static auto layoutDirectionField = javaNode->getClass()->getField("mLayoutDirection"); - javaNode->setFieldValue(layoutDirectionField, static_cast(CSSNodeLayoutGetDirection(node))); -} - -static void _jniTransferLayoutOutputsRecursive(CSSNodeRef root) { - if (auto obj = jobjectContext(root)->lockLocal()) { - static auto widthField = obj->getClass()->getField("mWidth"); - static auto heightField = obj->getClass()->getField("mHeight"); - static auto leftField = obj->getClass()->getField("mLeft"); - static auto topField = obj->getClass()->getField("mTop"); - - obj->setFieldValue(widthField, CSSNodeLayoutGetWidth(root)); - obj->setFieldValue(heightField, CSSNodeLayoutGetHeight(root)); - obj->setFieldValue(leftField, CSSNodeLayoutGetLeft(root)); - obj->setFieldValue(topField, CSSNodeLayoutGetTop(root)); - _jniTransferLayoutDirection(root, obj); - - for (uint32_t i = 0; i < CSSNodeChildCount(root); i++) { - _jniTransferLayoutOutputsRecursive(CSSNodeGetChild(root, i)); - } - } else { - CSSLog(YGLogLevelError, "Java CSSNode was GCed during layout calculation\n"); - } -} - -static void _jniPrint(CSSNodeRef node) { - if (auto obj = jobjectContext(node)->lockLocal()) { - cout << obj->toString() << endl; - } else { - CSSLog(YGLogLevelError, "Java CSSNode was GCed during layout calculation\n"); - } -} - -static CSSSize _jniMeasureFunc(CSSNodeRef node, - float width, - YGMeasureMode widthMode, - float height, - YGMeasureMode heightMode) { - if (auto obj = jobjectContext(node)->lockLocal()) { - static auto measureFunc = findClassLocal("com/facebook/csslayout/CSSNode") - ->getMethod("measure"); - - _jniTransferLayoutDirection(node, obj); - const auto measureResult = measureFunc(obj, width, widthMode, height, heightMode); - - static_assert(sizeof(measureResult) == 8, - "Expected measureResult to be 8 bytes, or two 32 bit ints"); - - const float measuredWidth = static_cast(0xFFFFFFFF & (measureResult >> 32)); - const float measuredHeight = static_cast(0xFFFFFFFF & measureResult); - - return CSSSize{measuredWidth, measuredHeight}; - } else { - CSSLog(YGLogLevelError, "Java CSSNode was GCed during layout calculation\n"); - return CSSSize{ - widthMode == YGMeasureModeUndefined ? 0 : width, - heightMode == YGMeasureModeUndefined ? 0 : height, - }; - } -} - -struct JYogaLogLevel : public JavaClass { - static constexpr auto kJavaDescriptor = "Lcom/facebook/csslayout/YogaLogLevel;"; -}; - -static global_ref *jLogger; -static int _jniLog(YGLogLevel level, const char *format, va_list args) { - char buffer[256]; - int result = vsnprintf(buffer, sizeof(buffer), format, args); - - static auto logFunc = findClassLocal("com/facebook/csslayout/CSSLogger") - ->getMethod, jstring)>("log"); - - static auto logLevelFromInt = - JYogaLogLevel::javaClassStatic()->getStaticMethod("fromInt"); - - logFunc(jLogger->get(), - logLevelFromInt(JYogaLogLevel::javaClassStatic(), static_cast(level)), - Environment::current()->NewStringUTF(buffer)); - - return result; -} - -static inline CSSNodeRef _jlong2CSSNodeRef(jlong addr) { - return reinterpret_cast(static_cast(addr)); -} - -void jni_CSSLayoutSetLogger(alias_ref clazz, alias_ref logger) { - if (jLogger) { - jLogger->releaseAlias(); - delete jLogger; - } - - if (logger) { - jLogger = new global_ref(make_global(logger)); - CSSLayoutSetLogger(_jniLog); - } else { - jLogger = NULL; - CSSLayoutSetLogger(NULL); - } -} - -void jni_CSSLog(alias_ref clazz, jint level, jstring message) { - const char *nMessage = Environment::current()->GetStringUTFChars(message, 0); - CSSLog(static_cast(level), "%s", nMessage); - Environment::current()->ReleaseStringUTFChars(message, nMessage); -} - -void jni_CSSLayoutSetExperimentalFeatureEnabled(alias_ref clazz, - jint feature, - jboolean enabled) { - CSSLayoutSetExperimentalFeatureEnabled(static_cast(feature), enabled); -} - -jboolean jni_CSSLayoutIsExperimentalFeatureEnabled(alias_ref clazz, jint feature) { - return CSSLayoutIsExperimentalFeatureEnabled(static_cast(feature)); -} - -jint jni_CSSNodeGetInstanceCount(alias_ref clazz) { - return CSSNodeGetInstanceCount(); -} - -jlong jni_CSSNodeNew(alias_ref thiz) { - const CSSNodeRef node = CSSNodeNew(); - CSSNodeSetContext(node, new weak_ref(make_weak(thiz))); - CSSNodeSetPrintFunc(node, _jniPrint); - return reinterpret_cast(node); -} - -void jni_CSSNodeFree(alias_ref thiz, jlong nativePointer) { - const CSSNodeRef node = _jlong2CSSNodeRef(nativePointer); - delete jobjectContext(node); - CSSNodeFree(node); -} - -void jni_CSSNodeReset(alias_ref thiz, jlong nativePointer) { - const CSSNodeRef node = _jlong2CSSNodeRef(nativePointer); - void *context = CSSNodeGetContext(node); - CSSNodeReset(node); - CSSNodeSetContext(node, context); - CSSNodeSetPrintFunc(node, _jniPrint); -} - -void jni_CSSNodeInsertChild(alias_ref, - jlong nativePointer, - jlong childPointer, - jint index) { - CSSNodeInsertChild(_jlong2CSSNodeRef(nativePointer), _jlong2CSSNodeRef(childPointer), index); -} - -void jni_CSSNodeRemoveChild(alias_ref, jlong nativePointer, jlong childPointer) { - CSSNodeRemoveChild(_jlong2CSSNodeRef(nativePointer), _jlong2CSSNodeRef(childPointer)); -} - -void jni_CSSNodeCalculateLayout(alias_ref, jlong nativePointer) { - const CSSNodeRef root = _jlong2CSSNodeRef(nativePointer); - CSSNodeCalculateLayout(root, - YGUndefined, - YGUndefined, - CSSNodeStyleGetDirection(_jlong2CSSNodeRef(nativePointer))); - _jniTransferLayoutOutputsRecursive(root); -} - -void jni_CSSNodeMarkDirty(alias_ref, jlong nativePointer) { - CSSNodeMarkDirty(_jlong2CSSNodeRef(nativePointer)); -} - -jboolean jni_CSSNodeIsDirty(alias_ref, jlong nativePointer) { - return (jboolean) CSSNodeIsDirty(_jlong2CSSNodeRef(nativePointer)); -} - -void jni_CSSNodeSetHasMeasureFunc(alias_ref, - jlong nativePointer, - jboolean hasMeasureFunc) { - CSSNodeSetMeasureFunc(_jlong2CSSNodeRef(nativePointer), hasMeasureFunc ? _jniMeasureFunc : NULL); -} - -jboolean jni_CSSNodeHasNewLayout(alias_ref, jlong nativePointer) { - return (jboolean) CSSNodeGetHasNewLayout(_jlong2CSSNodeRef(nativePointer)); -} - -void jni_CSSNodeMarkLayoutSeen(alias_ref, jlong nativePointer) { - CSSNodeSetHasNewLayout(_jlong2CSSNodeRef(nativePointer), false); -} - -void jni_CSSNodeCopyStyle(alias_ref, jlong dstNativePointer, jlong srcNativePointer) { - CSSNodeCopyStyle(_jlong2CSSNodeRef(dstNativePointer), _jlong2CSSNodeRef(srcNativePointer)); -} - -#define CSS_NODE_JNI_STYLE_PROP(javatype, type, name) \ - javatype jni_CSSNodeStyleGet##name(alias_ref, jlong nativePointer) { \ - return (javatype) CSSNodeStyleGet##name(_jlong2CSSNodeRef(nativePointer)); \ - } \ - \ - void jni_CSSNodeStyleSet##name(alias_ref, jlong nativePointer, javatype value) { \ - CSSNodeStyleSet##name(_jlong2CSSNodeRef(nativePointer), static_cast(value)); \ - } - -#define CSS_NODE_JNI_STYLE_EDGE_PROP(javatype, type, name) \ - javatype jni_CSSNodeStyleGet##name(alias_ref, jlong nativePointer, jint edge) { \ - return (javatype) CSSNodeStyleGet##name(_jlong2CSSNodeRef(nativePointer), \ - static_cast(edge)); \ - } \ - \ - void jni_CSSNodeStyleSet##name(alias_ref, \ - jlong nativePointer, \ - jint edge, \ - javatype value) { \ - CSSNodeStyleSet##name(_jlong2CSSNodeRef(nativePointer), \ - static_cast(edge), \ - static_cast(value)); \ - } - -CSS_NODE_JNI_STYLE_PROP(jint, YGDirection, Direction); -CSS_NODE_JNI_STYLE_PROP(jint, YGFlexDirection, FlexDirection); -CSS_NODE_JNI_STYLE_PROP(jint, YGJustify, JustifyContent); -CSS_NODE_JNI_STYLE_PROP(jint, YGAlign, AlignItems); -CSS_NODE_JNI_STYLE_PROP(jint, YGAlign, AlignSelf); -CSS_NODE_JNI_STYLE_PROP(jint, YGAlign, AlignContent); -CSS_NODE_JNI_STYLE_PROP(jint, YGPositionType, PositionType); -CSS_NODE_JNI_STYLE_PROP(jint, YGWrap, FlexWrap); -CSS_NODE_JNI_STYLE_PROP(jint, YGOverflow, Overflow); - -void jni_CSSNodeStyleSetFlex(alias_ref, jlong nativePointer, jfloat value) { - CSSNodeStyleSetFlex(_jlong2CSSNodeRef(nativePointer), static_cast(value)); -} -CSS_NODE_JNI_STYLE_PROP(jfloat, float, FlexGrow); -CSS_NODE_JNI_STYLE_PROP(jfloat, float, FlexShrink); -CSS_NODE_JNI_STYLE_PROP(jfloat, float, FlexBasis); - -CSS_NODE_JNI_STYLE_EDGE_PROP(jfloat, float, Position); -CSS_NODE_JNI_STYLE_EDGE_PROP(jfloat, float, Margin); -CSS_NODE_JNI_STYLE_EDGE_PROP(jfloat, float, Padding); -CSS_NODE_JNI_STYLE_EDGE_PROP(jfloat, float, Border); - -CSS_NODE_JNI_STYLE_PROP(jfloat, float, Width); -CSS_NODE_JNI_STYLE_PROP(jfloat, float, MinWidth); -CSS_NODE_JNI_STYLE_PROP(jfloat, float, MaxWidth); -CSS_NODE_JNI_STYLE_PROP(jfloat, float, Height); -CSS_NODE_JNI_STYLE_PROP(jfloat, float, MinHeight); -CSS_NODE_JNI_STYLE_PROP(jfloat, float, MaxHeight); - -// Yoga specific properties, not compatible with flexbox specification -CSS_NODE_JNI_STYLE_PROP(jfloat, float, AspectRatio); - -#define CSSMakeNativeMethod(name) makeNativeMethod(#name, name) - -jint JNI_OnLoad(JavaVM *vm, void *) { - return initialize(vm, [] { - registerNatives("com/facebook/csslayout/CSSNode", - { - CSSMakeNativeMethod(jni_CSSNodeNew), - CSSMakeNativeMethod(jni_CSSNodeFree), - CSSMakeNativeMethod(jni_CSSNodeReset), - CSSMakeNativeMethod(jni_CSSNodeInsertChild), - CSSMakeNativeMethod(jni_CSSNodeRemoveChild), - CSSMakeNativeMethod(jni_CSSNodeCalculateLayout), - CSSMakeNativeMethod(jni_CSSNodeHasNewLayout), - CSSMakeNativeMethod(jni_CSSNodeMarkDirty), - CSSMakeNativeMethod(jni_CSSNodeIsDirty), - CSSMakeNativeMethod(jni_CSSNodeMarkLayoutSeen), - CSSMakeNativeMethod(jni_CSSNodeSetHasMeasureFunc), - CSSMakeNativeMethod(jni_CSSNodeCopyStyle), - - CSSMakeNativeMethod(jni_CSSNodeStyleGetDirection), - CSSMakeNativeMethod(jni_CSSNodeStyleSetDirection), - CSSMakeNativeMethod(jni_CSSNodeStyleGetFlexDirection), - CSSMakeNativeMethod(jni_CSSNodeStyleSetFlexDirection), - CSSMakeNativeMethod(jni_CSSNodeStyleGetJustifyContent), - CSSMakeNativeMethod(jni_CSSNodeStyleSetJustifyContent), - CSSMakeNativeMethod(jni_CSSNodeStyleGetAlignItems), - CSSMakeNativeMethod(jni_CSSNodeStyleSetAlignItems), - CSSMakeNativeMethod(jni_CSSNodeStyleGetAlignSelf), - CSSMakeNativeMethod(jni_CSSNodeStyleSetAlignSelf), - CSSMakeNativeMethod(jni_CSSNodeStyleGetAlignContent), - CSSMakeNativeMethod(jni_CSSNodeStyleSetAlignContent), - CSSMakeNativeMethod(jni_CSSNodeStyleGetPositionType), - CSSMakeNativeMethod(jni_CSSNodeStyleSetPositionType), - CSSMakeNativeMethod(jni_CSSNodeStyleSetFlexWrap), - CSSMakeNativeMethod(jni_CSSNodeStyleGetOverflow), - CSSMakeNativeMethod(jni_CSSNodeStyleSetOverflow), - CSSMakeNativeMethod(jni_CSSNodeStyleSetFlex), - CSSMakeNativeMethod(jni_CSSNodeStyleGetFlexGrow), - CSSMakeNativeMethod(jni_CSSNodeStyleSetFlexGrow), - CSSMakeNativeMethod(jni_CSSNodeStyleGetFlexShrink), - CSSMakeNativeMethod(jni_CSSNodeStyleSetFlexShrink), - CSSMakeNativeMethod(jni_CSSNodeStyleGetFlexBasis), - CSSMakeNativeMethod(jni_CSSNodeStyleSetFlexBasis), - CSSMakeNativeMethod(jni_CSSNodeStyleGetMargin), - CSSMakeNativeMethod(jni_CSSNodeStyleSetMargin), - CSSMakeNativeMethod(jni_CSSNodeStyleGetPadding), - CSSMakeNativeMethod(jni_CSSNodeStyleSetPadding), - CSSMakeNativeMethod(jni_CSSNodeStyleGetBorder), - CSSMakeNativeMethod(jni_CSSNodeStyleSetBorder), - CSSMakeNativeMethod(jni_CSSNodeStyleGetPosition), - CSSMakeNativeMethod(jni_CSSNodeStyleSetPosition), - CSSMakeNativeMethod(jni_CSSNodeStyleGetWidth), - CSSMakeNativeMethod(jni_CSSNodeStyleSetWidth), - CSSMakeNativeMethod(jni_CSSNodeStyleGetHeight), - CSSMakeNativeMethod(jni_CSSNodeStyleSetHeight), - CSSMakeNativeMethod(jni_CSSNodeStyleGetMinWidth), - CSSMakeNativeMethod(jni_CSSNodeStyleSetMinWidth), - CSSMakeNativeMethod(jni_CSSNodeStyleGetMinHeight), - CSSMakeNativeMethod(jni_CSSNodeStyleSetMinHeight), - CSSMakeNativeMethod(jni_CSSNodeStyleGetMaxWidth), - CSSMakeNativeMethod(jni_CSSNodeStyleSetMaxWidth), - CSSMakeNativeMethod(jni_CSSNodeStyleGetMaxHeight), - CSSMakeNativeMethod(jni_CSSNodeStyleSetMaxHeight), - CSSMakeNativeMethod(jni_CSSNodeStyleGetAspectRatio), - CSSMakeNativeMethod(jni_CSSNodeStyleSetAspectRatio), - - CSSMakeNativeMethod(jni_CSSNodeGetInstanceCount), - CSSMakeNativeMethod(jni_CSSLayoutSetLogger), - CSSMakeNativeMethod(jni_CSSLog), - CSSMakeNativeMethod(jni_CSSLayoutSetExperimentalFeatureEnabled), - CSSMakeNativeMethod(jni_CSSLayoutIsExperimentalFeatureEnabled), - }); - }); -} diff --git a/ReactAndroid/src/main/jni/first-party/csslayoutjni/jni/YGJNI.cpp b/ReactAndroid/src/main/jni/first-party/csslayoutjni/jni/YGJNI.cpp new file mode 100644 index 000000000..a7d6d81bd --- /dev/null +++ b/ReactAndroid/src/main/jni/first-party/csslayoutjni/jni/YGJNI.cpp @@ -0,0 +1,333 @@ +/** + * 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. + */ + +#include +#include +#include + +using namespace facebook::jni; +using namespace std; + +static inline weak_ref *YGNodeJobject(YGNodeRef node) { + return reinterpret_cast *>(YGNodeGetContext(node)); +} + +static void YGTransferLayoutDirection(YGNodeRef node, alias_ref javaNode) { + static auto layoutDirectionField = javaNode->getClass()->getField("mLayoutDirection"); + javaNode->setFieldValue(layoutDirectionField, static_cast(YGNodeLayoutGetDirection(node))); +} + +static void YGTransferLayoutOutputsRecursive(YGNodeRef root) { + if (auto obj = YGNodeJobject(root)->lockLocal()) { + static auto widthField = obj->getClass()->getField("mWidth"); + static auto heightField = obj->getClass()->getField("mHeight"); + static auto leftField = obj->getClass()->getField("mLeft"); + static auto topField = obj->getClass()->getField("mTop"); + + obj->setFieldValue(widthField, YGNodeLayoutGetWidth(root)); + obj->setFieldValue(heightField, YGNodeLayoutGetHeight(root)); + obj->setFieldValue(leftField, YGNodeLayoutGetLeft(root)); + obj->setFieldValue(topField, YGNodeLayoutGetTop(root)); + YGTransferLayoutDirection(root, obj); + + for (uint32_t i = 0; i < YGNodeChildCount(root); i++) { + YGTransferLayoutOutputsRecursive(YGNodeGetChild(root, i)); + } + } else { + YGLog(YGLogLevelError, "Java YGNode was GCed during layout calculation\n"); + } +} + +static void YGPrint(YGNodeRef node) { + if (auto obj = YGNodeJobject(node)->lockLocal()) { + cout << obj->toString() << endl; + } else { + YGLog(YGLogLevelError, "Java YGNode was GCed during layout calculation\n"); + } +} + +static YGSize YGJNIMeasureFunc(YGNodeRef node, + float width, + YGMeasureMode widthMode, + float height, + YGMeasureMode heightMode) { + if (auto obj = YGNodeJobject(node)->lockLocal()) { + static auto measureFunc = findClassLocal("com/facebook/csslayout/CSSNode") + ->getMethod("measure"); + + YGTransferLayoutDirection(node, obj); + const auto measureResult = measureFunc(obj, width, widthMode, height, heightMode); + + static_assert(sizeof(measureResult) == 8, + "Expected measureResult to be 8 bytes, or two 32 bit ints"); + + const float measuredWidth = static_cast(0xFFFFFFFF & (measureResult >> 32)); + const float measuredHeight = static_cast(0xFFFFFFFF & measureResult); + + return YGSize{measuredWidth, measuredHeight}; + } else { + YGLog(YGLogLevelError, "Java YGNode was GCed during layout calculation\n"); + return YGSize{ + widthMode == YGMeasureModeUndefined ? 0 : width, + heightMode == YGMeasureModeUndefined ? 0 : height, + }; + } +} + +struct JYogaLogLevel : public JavaClass { + static constexpr auto kJavaDescriptor = "Lcom/facebook/csslayout/YogaLogLevel;"; +}; + +static global_ref *jLogger; +static int YGLog(YGLogLevel level, const char *format, va_list args) { + char buffer[256]; + int result = vsnprintf(buffer, sizeof(buffer), format, args); + + static auto logFunc = findClassLocal("com/facebook/csslayout/CSSLogger") + ->getMethod, jstring)>("log"); + + static auto logLevelFromInt = + JYogaLogLevel::javaClassStatic()->getStaticMethod("fromInt"); + + logFunc(jLogger->get(), + logLevelFromInt(JYogaLogLevel::javaClassStatic(), static_cast(level)), + Environment::current()->NewStringUTF(buffer)); + + return result; +} + +static inline YGNodeRef _jlong2YGNodeRef(jlong addr) { + return reinterpret_cast(static_cast(addr)); +} + +void jni_YGSetLogger(alias_ref clazz, alias_ref logger) { + if (jLogger) { + jLogger->releaseAlias(); + delete jLogger; + } + + if (logger) { + jLogger = new global_ref(make_global(logger)); + YGSetLogger(YGLog); + } else { + jLogger = NULL; + YGSetLogger(NULL); + } +} + +void jni_YGLog(alias_ref clazz, jint level, jstring message) { + const char *nMessage = Environment::current()->GetStringUTFChars(message, 0); + YGLog(static_cast(level), "%s", nMessage); + Environment::current()->ReleaseStringUTFChars(message, nMessage); +} + +void jni_YGSetExperimentalFeatureEnabled(alias_ref clazz, jint feature, jboolean enabled) { + YGSetExperimentalFeatureEnabled(static_cast(feature), enabled); +} + +jboolean jni_YGIsExperimentalFeatureEnabled(alias_ref clazz, jint feature) { + return YGIsExperimentalFeatureEnabled(static_cast(feature)); +} + +jint jni_YGNodeGetInstanceCount(alias_ref clazz) { + return YGNodeGetInstanceCount(); +} + +jlong jni_YGNodeNew(alias_ref thiz) { + const YGNodeRef node = YGNodeNew(); + YGNodeSetContext(node, new weak_ref(make_weak(thiz))); + YGNodeSetPrintFunc(node, YGPrint); + return reinterpret_cast(node); +} + +void jni_YGNodeFree(alias_ref thiz, jlong nativePointer) { + const YGNodeRef node = _jlong2YGNodeRef(nativePointer); + delete YGNodeJobject(node); + YGNodeFree(node); +} + +void jni_YGNodeReset(alias_ref thiz, jlong nativePointer) { + const YGNodeRef node = _jlong2YGNodeRef(nativePointer); + void *context = YGNodeGetContext(node); + YGNodeReset(node); + YGNodeSetContext(node, context); + YGNodeSetPrintFunc(node, YGPrint); +} + +void jni_YGNodeInsertChild(alias_ref, jlong nativePointer, jlong childPointer, jint index) { + YGNodeInsertChild(_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer), index); +} + +void jni_YGNodeRemoveChild(alias_ref, jlong nativePointer, jlong childPointer) { + YGNodeRemoveChild(_jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer)); +} + +void jni_YGNodeCalculateLayout(alias_ref, jlong nativePointer) { + const YGNodeRef root = _jlong2YGNodeRef(nativePointer); + YGNodeCalculateLayout(root, + YGUndefined, + YGUndefined, + YGNodeStyleGetDirection(_jlong2YGNodeRef(nativePointer))); + YGTransferLayoutOutputsRecursive(root); +} + +void jni_YGNodeMarkDirty(alias_ref, jlong nativePointer) { + YGNodeMarkDirty(_jlong2YGNodeRef(nativePointer)); +} + +jboolean jni_YGNodeIsDirty(alias_ref, jlong nativePointer) { + return (jboolean) YGNodeIsDirty(_jlong2YGNodeRef(nativePointer)); +} + +void jni_YGNodeSetHasMeasureFunc(alias_ref, jlong nativePointer, jboolean hasMeasureFunc) { + YGNodeSetMeasureFunc(_jlong2YGNodeRef(nativePointer), hasMeasureFunc ? YGJNIMeasureFunc : NULL); +} + +jboolean jni_YGNodeHasNewLayout(alias_ref, jlong nativePointer) { + return (jboolean) YGNodeGetHasNewLayout(_jlong2YGNodeRef(nativePointer)); +} + +void jni_YGNodeMarkLayoutSeen(alias_ref, jlong nativePointer) { + YGNodeSetHasNewLayout(_jlong2YGNodeRef(nativePointer), false); +} + +void jni_YGNodeCopyStyle(alias_ref, jlong dstNativePointer, jlong srcNativePointer) { + YGNodeCopyStyle(_jlong2YGNodeRef(dstNativePointer), _jlong2YGNodeRef(srcNativePointer)); +} + +#define YG_NODE_JNI_STYLE_PROP(javatype, type, name) \ + javatype jni_YGNodeStyleGet##name(alias_ref, jlong nativePointer) { \ + return (javatype) YGNodeStyleGet##name(_jlong2YGNodeRef(nativePointer)); \ + } \ + \ + void jni_YGNodeStyleSet##name(alias_ref, jlong nativePointer, javatype value) { \ + YGNodeStyleSet##name(_jlong2YGNodeRef(nativePointer), static_cast(value)); \ + } + +#define YG_NODE_JNI_STYLE_EDGE_PROP(javatype, type, name) \ + javatype jni_YGNodeStyleGet##name(alias_ref, jlong nativePointer, jint edge) { \ + return (javatype) YGNodeStyleGet##name(_jlong2YGNodeRef(nativePointer), \ + static_cast(edge)); \ + } \ + \ + void jni_YGNodeStyleSet##name(alias_ref, \ + jlong nativePointer, \ + jint edge, \ + javatype value) { \ + YGNodeStyleSet##name(_jlong2YGNodeRef(nativePointer), \ + static_cast(edge), \ + static_cast(value)); \ + } + +YG_NODE_JNI_STYLE_PROP(jint, YGDirection, Direction); +YG_NODE_JNI_STYLE_PROP(jint, YGFlexDirection, FlexDirection); +YG_NODE_JNI_STYLE_PROP(jint, YGJustify, JustifyContent); +YG_NODE_JNI_STYLE_PROP(jint, YGAlign, AlignItems); +YG_NODE_JNI_STYLE_PROP(jint, YGAlign, AlignSelf); +YG_NODE_JNI_STYLE_PROP(jint, YGAlign, AlignContent); +YG_NODE_JNI_STYLE_PROP(jint, YGPositionType, PositionType); +YG_NODE_JNI_STYLE_PROP(jint, YGWrap, FlexWrap); +YG_NODE_JNI_STYLE_PROP(jint, YGOverflow, Overflow); + +void jni_YGNodeStyleSetFlex(alias_ref, jlong nativePointer, jfloat value) { + YGNodeStyleSetFlex(_jlong2YGNodeRef(nativePointer), static_cast(value)); +} +YG_NODE_JNI_STYLE_PROP(jfloat, float, FlexGrow); +YG_NODE_JNI_STYLE_PROP(jfloat, float, FlexShrink); +YG_NODE_JNI_STYLE_PROP(jfloat, float, FlexBasis); + +YG_NODE_JNI_STYLE_EDGE_PROP(jfloat, float, Position); +YG_NODE_JNI_STYLE_EDGE_PROP(jfloat, float, Margin); +YG_NODE_JNI_STYLE_EDGE_PROP(jfloat, float, Padding); +YG_NODE_JNI_STYLE_EDGE_PROP(jfloat, float, Border); + +YG_NODE_JNI_STYLE_PROP(jfloat, float, Width); +YG_NODE_JNI_STYLE_PROP(jfloat, float, MinWidth); +YG_NODE_JNI_STYLE_PROP(jfloat, float, MaxWidth); +YG_NODE_JNI_STYLE_PROP(jfloat, float, Height); +YG_NODE_JNI_STYLE_PROP(jfloat, float, MinHeight); +YG_NODE_JNI_STYLE_PROP(jfloat, float, MaxHeight); + +// Yoga specific properties, not compatible with flexbox specification +YG_NODE_JNI_STYLE_PROP(jfloat, float, AspectRatio); + +#define YGMakeNativeMethod(name) makeNativeMethod(#name, name) + +jint JNI_OnLoad(JavaVM *vm, void *) { + return initialize(vm, [] { + registerNatives("com/facebook/csslayout/CSSNode", + { + YGMakeNativeMethod(jni_YGNodeNew), + YGMakeNativeMethod(jni_YGNodeFree), + YGMakeNativeMethod(jni_YGNodeReset), + YGMakeNativeMethod(jni_YGNodeInsertChild), + YGMakeNativeMethod(jni_YGNodeRemoveChild), + YGMakeNativeMethod(jni_YGNodeCalculateLayout), + YGMakeNativeMethod(jni_YGNodeHasNewLayout), + YGMakeNativeMethod(jni_YGNodeMarkDirty), + YGMakeNativeMethod(jni_YGNodeIsDirty), + YGMakeNativeMethod(jni_YGNodeMarkLayoutSeen), + YGMakeNativeMethod(jni_YGNodeSetHasMeasureFunc), + YGMakeNativeMethod(jni_YGNodeCopyStyle), + + YGMakeNativeMethod(jni_YGNodeStyleGetDirection), + YGMakeNativeMethod(jni_YGNodeStyleSetDirection), + YGMakeNativeMethod(jni_YGNodeStyleGetFlexDirection), + YGMakeNativeMethod(jni_YGNodeStyleSetFlexDirection), + YGMakeNativeMethod(jni_YGNodeStyleGetJustifyContent), + YGMakeNativeMethod(jni_YGNodeStyleSetJustifyContent), + YGMakeNativeMethod(jni_YGNodeStyleGetAlignItems), + YGMakeNativeMethod(jni_YGNodeStyleSetAlignItems), + YGMakeNativeMethod(jni_YGNodeStyleGetAlignSelf), + YGMakeNativeMethod(jni_YGNodeStyleSetAlignSelf), + YGMakeNativeMethod(jni_YGNodeStyleGetAlignContent), + YGMakeNativeMethod(jni_YGNodeStyleSetAlignContent), + YGMakeNativeMethod(jni_YGNodeStyleGetPositionType), + YGMakeNativeMethod(jni_YGNodeStyleSetPositionType), + YGMakeNativeMethod(jni_YGNodeStyleSetFlexWrap), + YGMakeNativeMethod(jni_YGNodeStyleGetOverflow), + YGMakeNativeMethod(jni_YGNodeStyleSetOverflow), + YGMakeNativeMethod(jni_YGNodeStyleSetFlex), + YGMakeNativeMethod(jni_YGNodeStyleGetFlexGrow), + YGMakeNativeMethod(jni_YGNodeStyleSetFlexGrow), + YGMakeNativeMethod(jni_YGNodeStyleGetFlexShrink), + YGMakeNativeMethod(jni_YGNodeStyleSetFlexShrink), + YGMakeNativeMethod(jni_YGNodeStyleGetFlexBasis), + YGMakeNativeMethod(jni_YGNodeStyleSetFlexBasis), + YGMakeNativeMethod(jni_YGNodeStyleGetMargin), + YGMakeNativeMethod(jni_YGNodeStyleSetMargin), + YGMakeNativeMethod(jni_YGNodeStyleGetPadding), + YGMakeNativeMethod(jni_YGNodeStyleSetPadding), + YGMakeNativeMethod(jni_YGNodeStyleGetBorder), + YGMakeNativeMethod(jni_YGNodeStyleSetBorder), + YGMakeNativeMethod(jni_YGNodeStyleGetPosition), + YGMakeNativeMethod(jni_YGNodeStyleSetPosition), + YGMakeNativeMethod(jni_YGNodeStyleGetWidth), + YGMakeNativeMethod(jni_YGNodeStyleSetWidth), + YGMakeNativeMethod(jni_YGNodeStyleGetHeight), + YGMakeNativeMethod(jni_YGNodeStyleSetHeight), + YGMakeNativeMethod(jni_YGNodeStyleGetMinWidth), + YGMakeNativeMethod(jni_YGNodeStyleSetMinWidth), + YGMakeNativeMethod(jni_YGNodeStyleGetMinHeight), + YGMakeNativeMethod(jni_YGNodeStyleSetMinHeight), + YGMakeNativeMethod(jni_YGNodeStyleGetMaxWidth), + YGMakeNativeMethod(jni_YGNodeStyleSetMaxWidth), + YGMakeNativeMethod(jni_YGNodeStyleGetMaxHeight), + YGMakeNativeMethod(jni_YGNodeStyleSetMaxHeight), + YGMakeNativeMethod(jni_YGNodeStyleGetAspectRatio), + YGMakeNativeMethod(jni_YGNodeStyleSetAspectRatio), + + YGMakeNativeMethod(jni_YGNodeGetInstanceCount), + YGMakeNativeMethod(jni_YGSetLogger), + YGMakeNativeMethod(jni_YGLog), + YGMakeNativeMethod(jni_YGSetExperimentalFeatureEnabled), + YGMakeNativeMethod(jni_YGIsExperimentalFeatureEnabled), + }); + }); +} diff --git a/ReactCommon/CSSLayout/Android.mk b/ReactCommon/CSSLayout/Android.mk index 8d9c94559..8a54f77b6 100644 --- a/ReactCommon/CSSLayout/Android.mk +++ b/ReactCommon/CSSLayout/Android.mk @@ -5,8 +5,8 @@ include $(CLEAR_VARS) LOCAL_MODULE := csslayoutcore LOCAL_SRC_FILES := \ - CSSLayout/CSSLayout.c \ - CSSLayout/CSSNodeList.c + CSSLayout/Yoga.c \ + CSSLayout/YGNodeList.c LOCAL_C_INCLUDES := $(LOCAL_PATH) LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) diff --git a/ReactCommon/CSSLayout/CSSLayout/CSSLayout.h b/ReactCommon/CSSLayout/CSSLayout/CSSLayout.h deleted file mode 100644 index 62f5b16cc..000000000 --- a/ReactCommon/CSSLayout/CSSLayout/CSSLayout.h +++ /dev/null @@ -1,183 +0,0 @@ -/** - * 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. - */ - -#pragma once - -#include -#include -#include -#include -#include -#include - -#ifndef __cplusplus -#include -#endif - -// Not defined in MSVC++ -#ifndef NAN -static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff}; -#define NAN (*(const float *) __nan) -#endif - -#define YGUndefined NAN - -#include "CSSEnums.h" -#include "CSSMacros.h" - -CSS_EXTERN_C_BEGIN - -typedef struct CSSSize { - float width; - float height; -} CSSSize; - -typedef struct CSSNode *CSSNodeRef; -typedef CSSSize (*CSSMeasureFunc)(CSSNodeRef node, - float width, - YGMeasureMode widthMode, - float height, - YGMeasureMode heightMode); -typedef void (*CSSPrintFunc)(CSSNodeRef node); -typedef int (*CSSLogger)(YGLogLevel level, const char *format, va_list args); - -typedef void *(*CSSMalloc)(size_t size); -typedef void *(*CSSCalloc)(size_t count, size_t size); -typedef void *(*CSSRealloc)(void *ptr, size_t size); -typedef void (*CSSFree)(void *ptr); - -// CSSNode -WIN_EXPORT CSSNodeRef CSSNodeNew(void); -WIN_EXPORT void CSSNodeInit(const CSSNodeRef node); -WIN_EXPORT void CSSNodeFree(const CSSNodeRef node); -WIN_EXPORT void CSSNodeFreeRecursive(const CSSNodeRef node); -WIN_EXPORT void CSSNodeReset(const CSSNodeRef node); -WIN_EXPORT int32_t CSSNodeGetInstanceCount(void); - -WIN_EXPORT void CSSNodeInsertChild(const CSSNodeRef node, - const CSSNodeRef child, - const uint32_t index); -WIN_EXPORT void CSSNodeRemoveChild(const CSSNodeRef node, const CSSNodeRef child); -WIN_EXPORT CSSNodeRef CSSNodeGetChild(const CSSNodeRef node, const uint32_t index); -WIN_EXPORT uint32_t CSSNodeChildCount(const CSSNodeRef node); - -WIN_EXPORT void CSSNodeCalculateLayout(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGDirection parentDirection); - -// Mark a node as dirty. Only valid for nodes with a custom measure function -// set. -// CSSLayout knows when to mark all other nodes as dirty but because nodes with -// measure functions -// depends on information not known to CSSLayout they must perform this dirty -// marking manually. -WIN_EXPORT void CSSNodeMarkDirty(const CSSNodeRef node); -WIN_EXPORT bool CSSNodeIsDirty(const CSSNodeRef node); - -WIN_EXPORT void CSSNodePrint(const CSSNodeRef node, const YGPrintOptions options); - -WIN_EXPORT bool CSSValueIsUndefined(const float value); - -WIN_EXPORT bool CSSNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, - const float width, - const YGMeasureMode heightMode, - const float height, - const YGMeasureMode lastWidthMode, - const float lastWidth, - const YGMeasureMode lastHeightMode, - const float lastHeight, - const float lastComputedWidth, - const float lastComputedHeight, - const float marginRow, - const float marginColumn); - -WIN_EXPORT void CSSNodeCopyStyle(const CSSNodeRef dstNode, const CSSNodeRef srcNode); - -#define CSS_NODE_PROPERTY(type, name, paramName) \ - WIN_EXPORT void CSSNodeSet##name(const CSSNodeRef node, type paramName); \ - WIN_EXPORT type CSSNodeGet##name(const CSSNodeRef node); - -#define CSS_NODE_STYLE_PROPERTY(type, name, paramName) \ - WIN_EXPORT void CSSNodeStyleSet##name(const CSSNodeRef node, const type paramName); \ - WIN_EXPORT type CSSNodeStyleGet##name(const CSSNodeRef node); - -#define CSS_NODE_STYLE_EDGE_PROPERTY(type, name, paramName) \ - WIN_EXPORT void CSSNodeStyleSet##name(const CSSNodeRef node, \ - const YGEdge edge, \ - const type paramName); \ - WIN_EXPORT type CSSNodeStyleGet##name(const CSSNodeRef node, const YGEdge edge); - -#define CSS_NODE_LAYOUT_PROPERTY(type, name) \ - WIN_EXPORT type CSSNodeLayoutGet##name(const CSSNodeRef node); - -CSS_NODE_PROPERTY(void *, Context, context); -CSS_NODE_PROPERTY(CSSMeasureFunc, MeasureFunc, measureFunc); -CSS_NODE_PROPERTY(CSSPrintFunc, PrintFunc, printFunc); -CSS_NODE_PROPERTY(bool, HasNewLayout, hasNewLayout); - -CSS_NODE_STYLE_PROPERTY(YGDirection, Direction, direction); -CSS_NODE_STYLE_PROPERTY(YGFlexDirection, FlexDirection, flexDirection); -CSS_NODE_STYLE_PROPERTY(YGJustify, JustifyContent, justifyContent); -CSS_NODE_STYLE_PROPERTY(YGAlign, AlignContent, alignContent); -CSS_NODE_STYLE_PROPERTY(YGAlign, AlignItems, alignItems); -CSS_NODE_STYLE_PROPERTY(YGAlign, AlignSelf, alignSelf); -CSS_NODE_STYLE_PROPERTY(YGPositionType, PositionType, positionType); -CSS_NODE_STYLE_PROPERTY(YGWrap, FlexWrap, flexWrap); -CSS_NODE_STYLE_PROPERTY(YGOverflow, Overflow, overflow); - -WIN_EXPORT void CSSNodeStyleSetFlex(const CSSNodeRef node, const float flex); -CSS_NODE_STYLE_PROPERTY(float, FlexGrow, flexGrow); -CSS_NODE_STYLE_PROPERTY(float, FlexShrink, flexShrink); -CSS_NODE_STYLE_PROPERTY(float, FlexBasis, flexBasis); - -CSS_NODE_STYLE_EDGE_PROPERTY(float, Position, position); -CSS_NODE_STYLE_EDGE_PROPERTY(float, Margin, margin); -CSS_NODE_STYLE_EDGE_PROPERTY(float, Padding, padding); -CSS_NODE_STYLE_EDGE_PROPERTY(float, Border, border); - -CSS_NODE_STYLE_PROPERTY(float, Width, width); -CSS_NODE_STYLE_PROPERTY(float, Height, height); -CSS_NODE_STYLE_PROPERTY(float, MinWidth, minWidth); -CSS_NODE_STYLE_PROPERTY(float, MinHeight, minHeight); -CSS_NODE_STYLE_PROPERTY(float, MaxWidth, maxWidth); -CSS_NODE_STYLE_PROPERTY(float, MaxHeight, maxHeight); - -// Yoga specific properties, not compatible with flexbox specification -// Aspect ratio control the size of the undefined dimension of a node. -// - On a node with a set width/height aspect ratio control the size of the unset dimension -// - On a node with a set flex basis aspect ratio controls the size of the node in the cross axis if -// unset -// - On a node with a measure function aspect ratio works as though the measure function measures -// the flex basis -// - On a node with flex grow/shrink aspect ratio controls the size of the node in the cross axis if -// unset -// - Aspect ratio takes min/max dimensions into account -CSS_NODE_STYLE_PROPERTY(float, AspectRatio, aspectRatio); - -CSS_NODE_LAYOUT_PROPERTY(float, Left); -CSS_NODE_LAYOUT_PROPERTY(float, Top); -CSS_NODE_LAYOUT_PROPERTY(float, Right); -CSS_NODE_LAYOUT_PROPERTY(float, Bottom); -CSS_NODE_LAYOUT_PROPERTY(float, Width); -CSS_NODE_LAYOUT_PROPERTY(float, Height); -CSS_NODE_LAYOUT_PROPERTY(YGDirection, Direction); - -WIN_EXPORT void CSSLayoutSetLogger(CSSLogger logger); -WIN_EXPORT void CSSLog(YGLogLevel level, const char *message, ...); - -WIN_EXPORT void CSSLayoutSetExperimentalFeatureEnabled(YGExperimentalFeature feature, bool enabled); -WIN_EXPORT bool CSSLayoutIsExperimentalFeatureEnabled(YGExperimentalFeature feature); - -WIN_EXPORT void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc, - CSSCalloc cssCalloc, - CSSRealloc cssRealloc, - CSSFree cssFree); - -CSS_EXTERN_C_END diff --git a/ReactCommon/CSSLayout/CSSLayout/CSSNodeList.c b/ReactCommon/CSSLayout/CSSLayout/CSSNodeList.c deleted file mode 100644 index 886f78cb3..000000000 --- a/ReactCommon/CSSLayout/CSSLayout/CSSNodeList.c +++ /dev/null @@ -1,104 +0,0 @@ -/** - * 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. - */ - -#include "CSSNodeList.h" - -extern CSSMalloc gCSSMalloc; -extern CSSRealloc gCSSRealloc; -extern CSSFree gCSSFree; - -struct CSSNodeList { - uint32_t capacity; - uint32_t count; - CSSNodeRef *items; -}; - -CSSNodeListRef CSSNodeListNew(const uint32_t initialCapacity) { - const CSSNodeListRef list = gCSSMalloc(sizeof(struct CSSNodeList)); - CSS_ASSERT(list != NULL, "Could not allocate memory for list"); - - list->capacity = initialCapacity; - list->count = 0; - list->items = gCSSMalloc(sizeof(CSSNodeRef) * list->capacity); - CSS_ASSERT(list->items != NULL, "Could not allocate memory for items"); - - return list; -} - -void CSSNodeListFree(const CSSNodeListRef list) { - if (list) { - gCSSFree(list->items); - gCSSFree(list); - } -} - -uint32_t CSSNodeListCount(const CSSNodeListRef list) { - if (list) { - return list->count; - } - return 0; -} - -void CSSNodeListAdd(CSSNodeListRef *listp, const CSSNodeRef node) { - if (!*listp) { - *listp = CSSNodeListNew(4); - } - CSSNodeListInsert(listp, node, (*listp)->count); -} - -void CSSNodeListInsert(CSSNodeListRef *listp, const CSSNodeRef node, const uint32_t index) { - if (!*listp) { - *listp = CSSNodeListNew(4); - } - CSSNodeListRef list = *listp; - - if (list->count == list->capacity) { - list->capacity *= 2; - list->items = gCSSRealloc(list->items, sizeof(CSSNodeRef) * list->capacity); - CSS_ASSERT(list->items != NULL, "Could not extend allocation for items"); - } - - for (uint32_t i = list->count; i > index; i--) { - list->items[i] = list->items[i - 1]; - } - - list->count++; - list->items[index] = node; -} - -CSSNodeRef CSSNodeListRemove(const CSSNodeListRef list, const uint32_t index) { - const CSSNodeRef removed = list->items[index]; - list->items[index] = NULL; - - for (uint32_t i = index; i < list->count - 1; i++) { - list->items[i] = list->items[i + 1]; - list->items[i + 1] = NULL; - } - - list->count--; - return removed; -} - -CSSNodeRef CSSNodeListDelete(const CSSNodeListRef list, const CSSNodeRef node) { - for (uint32_t i = 0; i < list->count; i++) { - if (list->items[i] == node) { - return CSSNodeListRemove(list, i); - } - } - - return NULL; -} - -CSSNodeRef CSSNodeListGet(const CSSNodeListRef list, const uint32_t index) { - if (CSSNodeListCount(list) > 0) { - return list->items[index]; - } - - return NULL; -} diff --git a/ReactCommon/CSSLayout/CSSLayout/CSSNodeList.h b/ReactCommon/CSSLayout/CSSLayout/CSSNodeList.h deleted file mode 100644 index 155fcf6e5..000000000 --- a/ReactCommon/CSSLayout/CSSLayout/CSSNodeList.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * 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. - */ - -#pragma once - -#include -#include -#include -#include - -#include "CSSLayout.h" -#include "CSSMacros.h" - -CSS_EXTERN_C_BEGIN - -typedef struct CSSNodeList *CSSNodeListRef; - -CSSNodeListRef CSSNodeListNew(const uint32_t initialCapacity); -void CSSNodeListFree(const CSSNodeListRef list); -uint32_t CSSNodeListCount(const CSSNodeListRef list); -void CSSNodeListAdd(CSSNodeListRef *listp, const CSSNodeRef node); -void CSSNodeListInsert(CSSNodeListRef *listp, const CSSNodeRef node, const uint32_t index); -CSSNodeRef CSSNodeListRemove(const CSSNodeListRef list, const uint32_t index); -CSSNodeRef CSSNodeListDelete(const CSSNodeListRef list, const CSSNodeRef node); -CSSNodeRef CSSNodeListGet(const CSSNodeListRef list, const uint32_t index); - -CSS_EXTERN_C_END diff --git a/ReactCommon/CSSLayout/CSSLayout/CSSEnums.h b/ReactCommon/CSSLayout/CSSLayout/YGEnums.h similarity index 100% rename from ReactCommon/CSSLayout/CSSLayout/CSSEnums.h rename to ReactCommon/CSSLayout/CSSLayout/YGEnums.h diff --git a/React/CSSLayout/CSSMacros.h b/ReactCommon/CSSLayout/CSSLayout/YGMacros.h similarity index 60% rename from React/CSSLayout/CSSMacros.h rename to ReactCommon/CSSLayout/CSSLayout/YGMacros.h index f54a5b87d..def8371ac 100644 --- a/React/CSSLayout/CSSMacros.h +++ b/ReactCommon/CSSLayout/CSSLayout/YGMacros.h @@ -10,11 +10,11 @@ #pragma once #ifdef __cplusplus -#define CSS_EXTERN_C_BEGIN extern "C" { -#define CSS_EXTERN_C_END } +#define YG_EXTERN_C_BEGIN extern "C" { +#define YG_EXTERN_C_END } #else -#define CSS_EXTERN_C_BEGIN -#define CSS_EXTERN_C_END +#define YG_EXTERN_C_BEGIN +#define YG_EXTERN_C_END #endif #ifdef _WINDLL @@ -28,15 +28,15 @@ #endif #if FB_ASSERTIONS_ENABLED -#define CSS_ABORT() abort() +#define YG_ABORT() abort() #else -#define CSS_ABORT() +#define YG_ABORT() #endif -#ifndef CSS_ASSERT -#define CSS_ASSERT(X, message) \ - if (!(X)) { \ - CSSLog(YGLogLevelError, "%s", message); \ - CSS_ABORT(); \ +#ifndef YG_ASSERT +#define YG_ASSERT(X, message) \ + if (!(X)) { \ + YGLog(YGLogLevelError, "%s", message); \ + YG_ABORT(); \ } #endif diff --git a/ReactCommon/CSSLayout/CSSLayout/YGNodeList.c b/ReactCommon/CSSLayout/CSSLayout/YGNodeList.c new file mode 100644 index 000000000..d82753e2a --- /dev/null +++ b/ReactCommon/CSSLayout/CSSLayout/YGNodeList.c @@ -0,0 +1,104 @@ +/** + * 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. + */ + +#include "YGNodeList.h" + +extern YGMalloc gYGMalloc; +extern YGRealloc gYGRealloc; +extern YGFree gYGFree; + +struct YGNodeList { + uint32_t capacity; + uint32_t count; + YGNodeRef *items; +}; + +YGNodeListRef YGNodeListNew(const uint32_t initialCapacity) { + const YGNodeListRef list = gYGMalloc(sizeof(struct YGNodeList)); + YG_ASSERT(list != NULL, "Could not allocate memory for list"); + + list->capacity = initialCapacity; + list->count = 0; + list->items = gYGMalloc(sizeof(YGNodeRef) * list->capacity); + YG_ASSERT(list->items != NULL, "Could not allocate memory for items"); + + return list; +} + +void YGNodeListFree(const YGNodeListRef list) { + if (list) { + gYGFree(list->items); + gYGFree(list); + } +} + +uint32_t YGNodeListCount(const YGNodeListRef list) { + if (list) { + return list->count; + } + return 0; +} + +void YGNodeListAdd(YGNodeListRef *listp, const YGNodeRef node) { + if (!*listp) { + *listp = YGNodeListNew(4); + } + YGNodeListInsert(listp, node, (*listp)->count); +} + +void YGNodeListInsert(YGNodeListRef *listp, const YGNodeRef node, const uint32_t index) { + if (!*listp) { + *listp = YGNodeListNew(4); + } + YGNodeListRef list = *listp; + + if (list->count == list->capacity) { + list->capacity *= 2; + list->items = gYGRealloc(list->items, sizeof(YGNodeRef) * list->capacity); + YG_ASSERT(list->items != NULL, "Could not extend allocation for items"); + } + + for (uint32_t i = list->count; i > index; i--) { + list->items[i] = list->items[i - 1]; + } + + list->count++; + list->items[index] = node; +} + +YGNodeRef YGNodeListRemove(const YGNodeListRef list, const uint32_t index) { + const YGNodeRef removed = list->items[index]; + list->items[index] = NULL; + + for (uint32_t i = index; i < list->count - 1; i++) { + list->items[i] = list->items[i + 1]; + list->items[i + 1] = NULL; + } + + list->count--; + return removed; +} + +YGNodeRef YGNodeListDelete(const YGNodeListRef list, const YGNodeRef node) { + for (uint32_t i = 0; i < list->count; i++) { + if (list->items[i] == node) { + return YGNodeListRemove(list, i); + } + } + + return NULL; +} + +YGNodeRef YGNodeListGet(const YGNodeListRef list, const uint32_t index) { + if (YGNodeListCount(list) > 0) { + return list->items[index]; + } + + return NULL; +} diff --git a/ReactCommon/CSSLayout/CSSLayout/YGNodeList.h b/ReactCommon/CSSLayout/CSSLayout/YGNodeList.h new file mode 100644 index 000000000..41e272ab3 --- /dev/null +++ b/ReactCommon/CSSLayout/CSSLayout/YGNodeList.h @@ -0,0 +1,33 @@ +/** + * 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. + */ + +#pragma once + +#include +#include +#include +#include + +#include "YGMacros.h" +#include "Yoga.h" + +YG_EXTERN_C_BEGIN + +typedef struct YGNodeList *YGNodeListRef; + +YGNodeListRef YGNodeListNew(const uint32_t initialCapacity); +void YGNodeListFree(const YGNodeListRef list); +uint32_t YGNodeListCount(const YGNodeListRef list); +void YGNodeListAdd(YGNodeListRef *listp, const YGNodeRef node); +void YGNodeListInsert(YGNodeListRef *listp, const YGNodeRef node, const uint32_t index); +YGNodeRef YGNodeListRemove(const YGNodeListRef list, const uint32_t index); +YGNodeRef YGNodeListDelete(const YGNodeListRef list, const YGNodeRef node); +YGNodeRef YGNodeListGet(const YGNodeListRef list, const uint32_t index); + +YG_EXTERN_C_END diff --git a/ReactCommon/CSSLayout/CSSLayout/CSSLayout.c b/ReactCommon/CSSLayout/CSSLayout/Yoga.c similarity index 50% rename from ReactCommon/CSSLayout/CSSLayout/CSSLayout.c rename to ReactCommon/CSSLayout/CSSLayout/Yoga.c index c41194a1e..70afece2f 100644 --- a/ReactCommon/CSSLayout/CSSLayout/CSSLayout.c +++ b/ReactCommon/CSSLayout/CSSLayout/Yoga.c @@ -9,8 +9,8 @@ #include -#include "CSSLayout.h" -#include "CSSNodeList.h" +#include "YGNodeList.h" +#include "Yoga.h" #ifdef _MSC_VER #include @@ -30,7 +30,7 @@ __forceinline const float fmaxf(const float a, const float b) { #endif #endif -typedef struct CSSCachedMeasurement { +typedef struct YGCachedMeasurement { float availableWidth; float availableHeight; YGMeasureMode widthMeasureMode; @@ -38,13 +38,13 @@ typedef struct CSSCachedMeasurement { float computedWidth; float computedHeight; -} CSSCachedMeasurement; +} YGCachedMeasurement; // This value was chosen based on empiracle data. Even the most complicated // layouts should not require more than 16 entries to fit within the cache. -enum { CSS_MAX_CACHED_RESULT_COUNT = 16 }; +enum { YG_MAX_CACHED_RESULT_COUNT = 16 }; -typedef struct CSSLayout { +typedef struct YGLayout { float position[4]; float dimensions[2]; YGDirection direction; @@ -58,13 +58,13 @@ typedef struct CSSLayout { YGDirection lastParentDirection; uint32_t nextCachedMeasurementsIndex; - CSSCachedMeasurement cachedMeasurements[CSS_MAX_CACHED_RESULT_COUNT]; + YGCachedMeasurement cachedMeasurements[YG_MAX_CACHED_RESULT_COUNT]; float measuredDimensions[2]; - CSSCachedMeasurement cachedLayout; -} CSSLayout; + YGCachedMeasurement cachedLayout; +} YGLayout; -typedef struct CSSStyle { +typedef struct YGStyle { YGDirection direction; YGFlexDirection flexDirection; YGJustify justifyContent; @@ -88,34 +88,34 @@ typedef struct CSSStyle { // Yoga specific properties, not compatible with flexbox specification float aspectRatio; -} CSSStyle; +} YGStyle; -typedef struct CSSNode { - CSSStyle style; - CSSLayout layout; +typedef struct YGNode { + YGStyle style; + YGLayout layout; uint32_t lineIndex; bool hasNewLayout; - CSSNodeRef parent; - CSSNodeListRef children; + YGNodeRef parent; + YGNodeListRef children; bool isDirty; - struct CSSNode *nextChild; + struct YGNode *nextChild; - CSSMeasureFunc measure; - CSSPrintFunc print; + YGMeasureFunc measure; + YGPrintFunc print; void *context; -} CSSNode; +} YGNode; -static void _CSSNodeMarkDirty(const CSSNodeRef node); +static void YGNodeMarkDirtyInternal(const YGNodeRef node); -CSSMalloc gCSSMalloc = &malloc; -CSSCalloc gCSSCalloc = &calloc; -CSSRealloc gCSSRealloc = &realloc; -CSSFree gCSSFree = &free; +YGMalloc gYGMalloc = &malloc; +YGCalloc gYGCalloc = &calloc; +YGRealloc gYGRealloc = &realloc; +YGFree gYGFree = &free; #ifdef ANDROID #include -static int _csslayoutAndroidLog(YGLogLevel level, const char *format, va_list args) { +static int YGAndroidLog(YGLogLevel level, const char *format, va_list args) { int androidLevel = YGLogLevelDebug; switch (level) { case YGLogLevelError: @@ -136,12 +136,12 @@ static int _csslayoutAndroidLog(YGLogLevel level, const char *format, va_list ar case YGLogLevelCount: break; } - const int result = __android_log_vprint(androidLevel, "css-layout", format, args); + const int result = __android_log_vprint(androidLevel, "YG-layout", format, args); return result; } -static CSSLogger gLogger = &_csslayoutAndroidLog; +static YGLogger gLogger = &YGAndroidLog; #else -static int _csslayoutDefaultLog(YGLogLevel level, const char *format, va_list args) { +static int YGDefaultLog(YGLogLevel level, const char *format, va_list args) { switch (level) { case YGLogLevelError: return vfprintf(stderr, format, args); @@ -153,28 +153,28 @@ static int _csslayoutDefaultLog(YGLogLevel level, const char *format, va_list ar return vprintf(format, args); } } -static CSSLogger gLogger = &_csslayoutDefaultLog; +static YGLogger gLogger = &YGDefaultLog; #endif -static inline float computedEdgeValue(const float edges[YGEdgeCount], - const YGEdge edge, - const float defaultValue) { - CSS_ASSERT(edge <= YGEdgeEnd, "Cannot get computed value of multi-edge shorthands"); +static inline float YGComputedEdgeValue(const float edges[YGEdgeCount], + const YGEdge edge, + const float defaultValue) { + YG_ASSERT(edge <= YGEdgeEnd, "Cannot get computed value of multi-edge shorthands"); - if (!CSSValueIsUndefined(edges[edge])) { + if (!YGValueIsUndefined(edges[edge])) { return edges[edge]; } - if ((edge == YGEdgeTop || edge == YGEdgeBottom) && !CSSValueIsUndefined(edges[YGEdgeVertical])) { + if ((edge == YGEdgeTop || edge == YGEdgeBottom) && !YGValueIsUndefined(edges[YGEdgeVertical])) { return edges[YGEdgeVertical]; } if ((edge == YGEdgeLeft || edge == YGEdgeRight || edge == YGEdgeStart || edge == YGEdgeEnd) && - !CSSValueIsUndefined(edges[YGEdgeHorizontal])) { + !YGValueIsUndefined(edges[YGEdgeHorizontal])) { return edges[YGEdgeHorizontal]; } - if (!CSSValueIsUndefined(edges[YGEdgeAll])) { + if (!YGValueIsUndefined(edges[YGEdgeAll])) { return edges[YGEdgeAll]; } @@ -187,55 +187,55 @@ static inline float computedEdgeValue(const float edges[YGEdgeCount], int32_t gNodeInstanceCount = 0; -CSSNodeRef CSSNodeNew(void) { - const CSSNodeRef node = gCSSCalloc(1, sizeof(CSSNode)); - CSS_ASSERT(node, "Could not allocate memory for node"); +YGNodeRef YGNodeNew(void) { + const YGNodeRef node = gYGCalloc(1, sizeof(YGNode)); + YG_ASSERT(node, "Could not allocate memory for node"); gNodeInstanceCount++; - CSSNodeInit(node); + YGNodeInit(node); return node; } -void CSSNodeFree(const CSSNodeRef node) { +void YGNodeFree(const YGNodeRef node) { if (node->parent) { - CSSNodeListDelete(node->parent->children, node); + YGNodeListDelete(node->parent->children, node); node->parent = NULL; } - const uint32_t childCount = CSSNodeChildCount(node); + const uint32_t childCount = YGNodeChildCount(node); for (uint32_t i = 0; i < childCount; i++) { - const CSSNodeRef child = CSSNodeGetChild(node, i); + const YGNodeRef child = YGNodeGetChild(node, i); child->parent = NULL; } - CSSNodeListFree(node->children); - gCSSFree(node); + YGNodeListFree(node->children); + gYGFree(node); gNodeInstanceCount--; } -void CSSNodeFreeRecursive(const CSSNodeRef root) { - while (CSSNodeChildCount(root) > 0) { - const CSSNodeRef child = CSSNodeGetChild(root, 0); - CSSNodeRemoveChild(root, child); - CSSNodeFreeRecursive(child); +void YGNodeFreeRecursive(const YGNodeRef root) { + while (YGNodeChildCount(root) > 0) { + const YGNodeRef child = YGNodeGetChild(root, 0); + YGNodeRemoveChild(root, child); + YGNodeFreeRecursive(child); } - CSSNodeFree(root); + YGNodeFree(root); } -void CSSNodeReset(const CSSNodeRef node) { - CSS_ASSERT(CSSNodeChildCount(node) == 0, "Cannot reset a node which still has children attached"); - CSS_ASSERT(node->parent == NULL, "Cannot reset a node still attached to a parent"); +void YGNodeReset(const YGNodeRef node) { + YG_ASSERT(YGNodeChildCount(node) == 0, "Cannot reset a node which still has children attached"); + YG_ASSERT(node->parent == NULL, "Cannot reset a node still attached to a parent"); - CSSNodeListFree(node->children); - memset(node, 0, sizeof(CSSNode)); - CSSNodeInit(node); + YGNodeListFree(node->children); + memset(node, 0, sizeof(YGNode)); + YGNodeInit(node); } -int32_t CSSNodeGetInstanceCount(void) { +int32_t YGNodeGetInstanceCount(void) { return gNodeInstanceCount; } -void CSSNodeInit(const CSSNodeRef node) { +void YGNodeInit(const YGNodeRef node) { node->parent = NULL; node->children = NULL; node->hasNewLayout = true; @@ -289,386 +289,396 @@ void CSSNodeInit(const CSSNodeRef node) { node->layout.cachedLayout.computedHeight = -1; } -static void _CSSNodeMarkDirty(const CSSNodeRef node) { +static void YGNodeMarkDirtyInternal(const YGNodeRef node) { if (!node->isDirty) { node->isDirty = true; node->layout.computedFlexBasis = YGUndefined; if (node->parent) { - _CSSNodeMarkDirty(node->parent); + YGNodeMarkDirtyInternal(node->parent); } } } -void CSSNodeSetMeasureFunc(const CSSNodeRef node, CSSMeasureFunc measureFunc) { +void YGNodeSetMeasureFunc(const YGNodeRef node, YGMeasureFunc measureFunc) { if (measureFunc == NULL) { node->measure = NULL; } else { - CSS_ASSERT(CSSNodeChildCount(node) == 0, - "Cannot set measure function: Nodes with measure functions cannot have children."); + YG_ASSERT(YGNodeChildCount(node) == 0, + "Cannot set measure function: Nodes with measure functions cannot have children."); node->measure = measureFunc; } } -CSSMeasureFunc CSSNodeGetMeasureFunc(const CSSNodeRef node) { +YGMeasureFunc YGNodeGetMeasureFunc(const YGNodeRef node) { return node->measure; } -void CSSNodeInsertChild(const CSSNodeRef node, const CSSNodeRef child, const uint32_t index) { - CSS_ASSERT(child->parent == NULL, "Child already has a parent, it must be removed first."); - CSS_ASSERT(node->measure == NULL, - "Cannot add child: Nodes with measure functions cannot have children."); - CSSNodeListInsert(&node->children, child, index); +void YGNodeInsertChild(const YGNodeRef node, const YGNodeRef child, const uint32_t index) { + YG_ASSERT(child->parent == NULL, "Child already has a parent, it must be removed first."); + YG_ASSERT(node->measure == NULL, + "Cannot add child: Nodes with measure functions cannot have children."); + YGNodeListInsert(&node->children, child, index); child->parent = node; - _CSSNodeMarkDirty(node); + YGNodeMarkDirtyInternal(node); } -void CSSNodeRemoveChild(const CSSNodeRef node, const CSSNodeRef child) { - if (CSSNodeListDelete(node->children, child) != NULL) { +void YGNodeRemoveChild(const YGNodeRef node, const YGNodeRef child) { + if (YGNodeListDelete(node->children, child) != NULL) { child->parent = NULL; - _CSSNodeMarkDirty(node); + YGNodeMarkDirtyInternal(node); } } -CSSNodeRef CSSNodeGetChild(const CSSNodeRef node, const uint32_t index) { - return CSSNodeListGet(node->children, index); +YGNodeRef YGNodeGetChild(const YGNodeRef node, const uint32_t index) { + return YGNodeListGet(node->children, index); } -inline uint32_t CSSNodeChildCount(const CSSNodeRef node) { - return CSSNodeListCount(node->children); +inline uint32_t YGNodeChildCount(const YGNodeRef node) { + return YGNodeListCount(node->children); } -void CSSNodeMarkDirty(const CSSNodeRef node) { - CSS_ASSERT(node->measure != NULL, - "Only leaf nodes with custom measure functions" - "should manually mark themselves as dirty"); - _CSSNodeMarkDirty(node); +void YGNodeMarkDirty(const YGNodeRef node) { + YG_ASSERT(node->measure != NULL, + "Only leaf nodes with custom measure functions" + "should manually mark themselves as dirty"); + YGNodeMarkDirtyInternal(node); } -bool CSSNodeIsDirty(const CSSNodeRef node) { +bool YGNodeIsDirty(const YGNodeRef node) { return node->isDirty; } -void CSSNodeCopyStyle(const CSSNodeRef dstNode, const CSSNodeRef srcNode) { - if (memcmp(&dstNode->style, &srcNode->style, sizeof(CSSStyle)) != 0) { - memcpy(&dstNode->style, &srcNode->style, sizeof(CSSStyle)); - _CSSNodeMarkDirty(dstNode); +void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode) { + if (memcmp(&dstNode->style, &srcNode->style, sizeof(YGStyle)) != 0) { + memcpy(&dstNode->style, &srcNode->style, sizeof(YGStyle)); + YGNodeMarkDirtyInternal(dstNode); } } -inline float CSSNodeStyleGetFlexGrow(const CSSNodeRef node) { - if (!CSSValueIsUndefined(node->style.flexGrow)) { +inline float YGNodeStyleGetFlexGrow(const YGNodeRef node) { + if (!YGValueIsUndefined(node->style.flexGrow)) { return node->style.flexGrow; } - if (!CSSValueIsUndefined(node->style.flex) && node->style.flex > 0) { + if (!YGValueIsUndefined(node->style.flex) && node->style.flex > 0) { return node->style.flex; } return 0; } -inline float CSSNodeStyleGetFlexShrink(const CSSNodeRef node) { - if (!CSSValueIsUndefined(node->style.flexShrink)) { +inline float YGNodeStyleGetFlexShrink(const YGNodeRef node) { + if (!YGValueIsUndefined(node->style.flexShrink)) { return node->style.flexShrink; } - if (!CSSValueIsUndefined(node->style.flex) && node->style.flex < 0) { + if (!YGValueIsUndefined(node->style.flex) && node->style.flex < 0) { return -node->style.flex; } return 0; } -inline float CSSNodeStyleGetFlexBasis(const CSSNodeRef node) { - if (!CSSValueIsUndefined(node->style.flexBasis)) { +inline float YGNodeStyleGetFlexBasis(const YGNodeRef node) { + if (!YGValueIsUndefined(node->style.flexBasis)) { return node->style.flexBasis; } - if (!CSSValueIsUndefined(node->style.flex)) { + if (!YGValueIsUndefined(node->style.flex)) { return node->style.flex > 0 ? 0 : YGUndefined; } return YGUndefined; } -void CSSNodeStyleSetFlex(const CSSNodeRef node, const float flex) { +void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) { if (node->style.flex != flex) { node->style.flex = flex; - _CSSNodeMarkDirty(node); + YGNodeMarkDirtyInternal(node); } } -#define CSS_NODE_PROPERTY_IMPL(type, name, paramName, instanceName) \ - void CSSNodeSet##name(const CSSNodeRef node, type paramName) { \ - node->instanceName = paramName; \ - } \ - \ - type CSSNodeGet##name(const CSSNodeRef node) { \ - return node->instanceName; \ +#define YG_NODE_PROPERTY_IMPL(type, name, paramName, instanceName) \ + void YGNodeSet##name(const YGNodeRef node, type paramName) { \ + node->instanceName = paramName; \ + } \ + \ + type YGNodeGet##name(const YGNodeRef node) { \ + return node->instanceName; \ } -#define CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \ - void CSSNodeStyleSet##name(const CSSNodeRef node, const type paramName) { \ - if (node->style.instanceName != paramName) { \ - node->style.instanceName = paramName; \ - _CSSNodeMarkDirty(node); \ - } \ +#define YG_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \ + void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \ + if (node->style.instanceName != paramName) { \ + node->style.instanceName = paramName; \ + YGNodeMarkDirtyInternal(node); \ + } \ } -#define CSS_NODE_STYLE_PROPERTY_IMPL(type, name, paramName, instanceName) \ - CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \ - \ - type CSSNodeStyleGet##name(const CSSNodeRef node) { \ - return node->style.instanceName; \ +#define YG_NODE_STYLE_PROPERTY_IMPL(type, name, paramName, instanceName) \ + YG_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \ + \ + type YGNodeStyleGet##name(const YGNodeRef node) { \ + return node->style.instanceName; \ } -#define CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(type, name, paramName, instanceName, defaultValue) \ - void CSSNodeStyleSet##name(const CSSNodeRef node, const YGEdge edge, const type paramName) { \ - if (node->style.instanceName[edge] != paramName) { \ - node->style.instanceName[edge] = paramName; \ - _CSSNodeMarkDirty(node); \ - } \ - } \ - \ - type CSSNodeStyleGet##name(const CSSNodeRef node, const YGEdge edge) { \ - return computedEdgeValue(node->style.instanceName, edge, defaultValue); \ +#define YG_NODE_STYLE_EDGE_PROPERTY_IMPL(type, name, paramName, instanceName, defaultValue) \ + void YGNodeStyleSet##name(const YGNodeRef node, const YGEdge edge, const type paramName) { \ + if (node->style.instanceName[edge] != paramName) { \ + node->style.instanceName[edge] = paramName; \ + YGNodeMarkDirtyInternal(node); \ + } \ + } \ + \ + type YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge) { \ + return YGComputedEdgeValue(node->style.instanceName, edge, defaultValue); \ } -#define CSS_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \ - type CSSNodeLayoutGet##name(const CSSNodeRef node) { \ - return node->layout.instanceName; \ +#define YG_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \ + type YGNodeLayoutGet##name(const YGNodeRef node) { \ + return node->layout.instanceName; \ } -CSS_NODE_PROPERTY_IMPL(void *, Context, context, context); -CSS_NODE_PROPERTY_IMPL(CSSPrintFunc, PrintFunc, printFunc, print); -CSS_NODE_PROPERTY_IMPL(bool, HasNewLayout, hasNewLayout, hasNewLayout); +YG_NODE_PROPERTY_IMPL(void *, Context, context, context); +YG_NODE_PROPERTY_IMPL(YGPrintFunc, PrintFunc, printFunc, print); +YG_NODE_PROPERTY_IMPL(bool, HasNewLayout, hasNewLayout, hasNewLayout); -CSS_NODE_STYLE_PROPERTY_IMPL(YGDirection, Direction, direction, direction); -CSS_NODE_STYLE_PROPERTY_IMPL(YGFlexDirection, FlexDirection, flexDirection, flexDirection); -CSS_NODE_STYLE_PROPERTY_IMPL(YGJustify, JustifyContent, justifyContent, justifyContent); -CSS_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignContent, alignContent, alignContent); -CSS_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignItems, alignItems, alignItems); -CSS_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignSelf, alignSelf, alignSelf); -CSS_NODE_STYLE_PROPERTY_IMPL(YGPositionType, PositionType, positionType, positionType); -CSS_NODE_STYLE_PROPERTY_IMPL(YGWrap, FlexWrap, flexWrap, flexWrap); -CSS_NODE_STYLE_PROPERTY_IMPL(YGOverflow, Overflow, overflow, overflow); +YG_NODE_STYLE_PROPERTY_IMPL(YGDirection, Direction, direction, direction); +YG_NODE_STYLE_PROPERTY_IMPL(YGFlexDirection, FlexDirection, flexDirection, flexDirection); +YG_NODE_STYLE_PROPERTY_IMPL(YGJustify, JustifyContent, justifyContent, justifyContent); +YG_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignContent, alignContent, alignContent); +YG_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignItems, alignItems, alignItems); +YG_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignSelf, alignSelf, alignSelf); +YG_NODE_STYLE_PROPERTY_IMPL(YGPositionType, PositionType, positionType, positionType); +YG_NODE_STYLE_PROPERTY_IMPL(YGWrap, FlexWrap, flexWrap, flexWrap); +YG_NODE_STYLE_PROPERTY_IMPL(YGOverflow, Overflow, overflow, overflow); -CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexGrow, flexGrow, flexGrow); -CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexShrink, flexShrink, flexShrink); -CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexBasis, flexBasis, flexBasis); +YG_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexGrow, flexGrow, flexGrow); +YG_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexShrink, flexShrink, flexShrink); +YG_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexBasis, flexBasis, flexBasis); -CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Position, position, position, YGUndefined); -CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Margin, margin, margin, 0); -CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Padding, padding, padding, 0); -CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Border, border, border, 0); +YG_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Position, position, position, YGUndefined); +YG_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Margin, margin, margin, 0); +YG_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Padding, padding, padding, 0); +YG_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Border, border, border, 0); -CSS_NODE_STYLE_PROPERTY_IMPL(float, Width, width, dimensions[YGDimensionWidth]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, Height, height, dimensions[YGDimensionHeight]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MinWidth, minWidth, minDimensions[YGDimensionWidth]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MinHeight, minHeight, minDimensions[YGDimensionHeight]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MaxWidth, maxWidth, maxDimensions[YGDimensionWidth]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MaxHeight, maxHeight, maxDimensions[YGDimensionHeight]); +YG_NODE_STYLE_PROPERTY_IMPL(float, Width, width, dimensions[YGDimensionWidth]); +YG_NODE_STYLE_PROPERTY_IMPL(float, Height, height, dimensions[YGDimensionHeight]); +YG_NODE_STYLE_PROPERTY_IMPL(float, MinWidth, minWidth, minDimensions[YGDimensionWidth]); +YG_NODE_STYLE_PROPERTY_IMPL(float, MinHeight, minHeight, minDimensions[YGDimensionHeight]); +YG_NODE_STYLE_PROPERTY_IMPL(float, MaxWidth, maxWidth, maxDimensions[YGDimensionWidth]); +YG_NODE_STYLE_PROPERTY_IMPL(float, MaxHeight, maxHeight, maxDimensions[YGDimensionHeight]); // Yoga specific properties, not compatible with flexbox specification -CSS_NODE_STYLE_PROPERTY_IMPL(float, AspectRatio, aspectRatio, aspectRatio); +YG_NODE_STYLE_PROPERTY_IMPL(float, AspectRatio, aspectRatio, aspectRatio); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Left, position[YGEdgeLeft]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[YGEdgeTop]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[YGEdgeRight]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Bottom, position[YGEdgeBottom]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Width, dimensions[YGDimensionWidth]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[YGDimensionHeight]); -CSS_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Left, position[YGEdgeLeft]); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[YGEdgeTop]); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[YGEdgeRight]); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Bottom, position[YGEdgeBottom]); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Width, dimensions[YGDimensionWidth]); +YG_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[YGDimensionHeight]); +YG_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction); uint32_t gCurrentGenerationCount = 0; -bool layoutNodeInternal(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGDirection parentDirection, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, - const bool performLayout, - const char *reason); +bool YGLayoutNodeInternal(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGDirection parentDirection, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode, + const bool performLayout, + const char *reason); -inline bool CSSValueIsUndefined(const float value) { +inline bool YGValueIsUndefined(const float value) { return isnan(value); } -static inline bool eq(const float a, const float b) { - if (CSSValueIsUndefined(a)) { - return CSSValueIsUndefined(b); +static inline bool YGFloatsEqual(const float a, const float b) { + if (YGValueIsUndefined(a)) { + return YGValueIsUndefined(b); } return fabs(a - b) < 0.0001; } -static void indent(const uint32_t n) { +static void YGIndent(const uint32_t n) { for (uint32_t i = 0; i < n; i++) { - CSSLog(YGLogLevelDebug, " "); + YGLog(YGLogLevelDebug, " "); } } -static void printNumberIfNotZero(const char *str, const float number) { - if (!eq(number, 0)) { - CSSLog(YGLogLevelDebug, "%s: %g, ", str, number); +static void YGPrintNumberIfNotZero(const char *str, const float number) { + if (!YGFloatsEqual(number, 0)) { + YGLog(YGLogLevelDebug, "%s: %g, ", str, number); } } -static void printNumberIfNotUndefined(const char *str, const float number) { - if (!CSSValueIsUndefined(number)) { - CSSLog(YGLogLevelDebug, "%s: %g, ", str, number); +static void YGPrintNumberIfNotUndefined(const char *str, const float number) { + if (!YGValueIsUndefined(number)) { + YGLog(YGLogLevelDebug, "%s: %g, ", str, number); } } -static bool eqFour(const float four[4]) { - return eq(four[0], four[1]) && eq(four[0], four[2]) && eq(four[0], four[3]); +static bool YGFourFloatsEqual(const float four[4]) { + return YGFloatsEqual(four[0], four[1]) && YGFloatsEqual(four[0], four[2]) && + YGFloatsEqual(four[0], four[3]); } -static void _CSSNodePrint(const CSSNodeRef node, - const YGPrintOptions options, - const uint32_t level) { - indent(level); - CSSLog(YGLogLevelDebug, "{"); +static void YGNodePrintInternal(const YGNodeRef node, + const YGPrintOptions options, + const uint32_t level) { + YGIndent(level); + YGLog(YGLogLevelDebug, "{"); if (node->print) { node->print(node); } if (options & YGPrintOptionsLayout) { - CSSLog(YGLogLevelDebug, "layout: {"); - CSSLog(YGLogLevelDebug, "width: %g, ", node->layout.dimensions[YGDimensionWidth]); - CSSLog(YGLogLevelDebug, "height: %g, ", node->layout.dimensions[YGDimensionHeight]); - CSSLog(YGLogLevelDebug, "top: %g, ", node->layout.position[YGEdgeTop]); - CSSLog(YGLogLevelDebug, "left: %g", node->layout.position[YGEdgeLeft]); - CSSLog(YGLogLevelDebug, "}, "); + YGLog(YGLogLevelDebug, "layout: {"); + YGLog(YGLogLevelDebug, "width: %g, ", node->layout.dimensions[YGDimensionWidth]); + YGLog(YGLogLevelDebug, "height: %g, ", node->layout.dimensions[YGDimensionHeight]); + YGLog(YGLogLevelDebug, "top: %g, ", node->layout.position[YGEdgeTop]); + YGLog(YGLogLevelDebug, "left: %g", node->layout.position[YGEdgeLeft]); + YGLog(YGLogLevelDebug, "}, "); } if (options & YGPrintOptionsStyle) { if (node->style.flexDirection == YGFlexDirectionColumn) { - CSSLog(YGLogLevelDebug, "flexDirection: 'column', "); + YGLog(YGLogLevelDebug, "flexDirection: 'column', "); } else if (node->style.flexDirection == YGFlexDirectionColumnReverse) { - CSSLog(YGLogLevelDebug, "flexDirection: 'column-reverse', "); + YGLog(YGLogLevelDebug, "flexDirection: 'column-reverse', "); } else if (node->style.flexDirection == YGFlexDirectionRow) { - CSSLog(YGLogLevelDebug, "flexDirection: 'row', "); + YGLog(YGLogLevelDebug, "flexDirection: 'row', "); } else if (node->style.flexDirection == YGFlexDirectionRowReverse) { - CSSLog(YGLogLevelDebug, "flexDirection: 'row-reverse', "); + YGLog(YGLogLevelDebug, "flexDirection: 'row-reverse', "); } if (node->style.justifyContent == YGJustifyCenter) { - CSSLog(YGLogLevelDebug, "justifyContent: 'center', "); + YGLog(YGLogLevelDebug, "justifyContent: 'center', "); } else if (node->style.justifyContent == YGJustifyFlexEnd) { - CSSLog(YGLogLevelDebug, "justifyContent: 'flex-end', "); + YGLog(YGLogLevelDebug, "justifyContent: 'flex-end', "); } else if (node->style.justifyContent == YGJustifySpaceAround) { - CSSLog(YGLogLevelDebug, "justifyContent: 'space-around', "); + YGLog(YGLogLevelDebug, "justifyContent: 'space-around', "); } else if (node->style.justifyContent == YGJustifySpaceBetween) { - CSSLog(YGLogLevelDebug, "justifyContent: 'space-between', "); + YGLog(YGLogLevelDebug, "justifyContent: 'space-between', "); } if (node->style.alignItems == YGAlignCenter) { - CSSLog(YGLogLevelDebug, "alignItems: 'center', "); + YGLog(YGLogLevelDebug, "alignItems: 'center', "); } else if (node->style.alignItems == YGAlignFlexEnd) { - CSSLog(YGLogLevelDebug, "alignItems: 'flex-end', "); + YGLog(YGLogLevelDebug, "alignItems: 'flex-end', "); } else if (node->style.alignItems == YGAlignStretch) { - CSSLog(YGLogLevelDebug, "alignItems: 'stretch', "); + YGLog(YGLogLevelDebug, "alignItems: 'stretch', "); } if (node->style.alignContent == YGAlignCenter) { - CSSLog(YGLogLevelDebug, "alignContent: 'center', "); + YGLog(YGLogLevelDebug, "alignContent: 'center', "); } else if (node->style.alignContent == YGAlignFlexEnd) { - CSSLog(YGLogLevelDebug, "alignContent: 'flex-end', "); + YGLog(YGLogLevelDebug, "alignContent: 'flex-end', "); } else if (node->style.alignContent == YGAlignStretch) { - CSSLog(YGLogLevelDebug, "alignContent: 'stretch', "); + YGLog(YGLogLevelDebug, "alignContent: 'stretch', "); } if (node->style.alignSelf == YGAlignFlexStart) { - CSSLog(YGLogLevelDebug, "alignSelf: 'flex-start', "); + YGLog(YGLogLevelDebug, "alignSelf: 'flex-start', "); } else if (node->style.alignSelf == YGAlignCenter) { - CSSLog(YGLogLevelDebug, "alignSelf: 'center', "); + YGLog(YGLogLevelDebug, "alignSelf: 'center', "); } else if (node->style.alignSelf == YGAlignFlexEnd) { - CSSLog(YGLogLevelDebug, "alignSelf: 'flex-end', "); + YGLog(YGLogLevelDebug, "alignSelf: 'flex-end', "); } else if (node->style.alignSelf == YGAlignStretch) { - CSSLog(YGLogLevelDebug, "alignSelf: 'stretch', "); + YGLog(YGLogLevelDebug, "alignSelf: 'stretch', "); } - printNumberIfNotUndefined("flexGrow", CSSNodeStyleGetFlexGrow(node)); - printNumberIfNotUndefined("flexShrink", CSSNodeStyleGetFlexShrink(node)); - printNumberIfNotUndefined("flexBasis", CSSNodeStyleGetFlexBasis(node)); + YGPrintNumberIfNotUndefined("flexGrow", YGNodeStyleGetFlexGrow(node)); + YGPrintNumberIfNotUndefined("flexShrink", YGNodeStyleGetFlexShrink(node)); + YGPrintNumberIfNotUndefined("flexBasis", YGNodeStyleGetFlexBasis(node)); if (node->style.overflow == YGOverflowHidden) { - CSSLog(YGLogLevelDebug, "overflow: 'hidden', "); + YGLog(YGLogLevelDebug, "overflow: 'hidden', "); } else if (node->style.overflow == YGOverflowVisible) { - CSSLog(YGLogLevelDebug, "overflow: 'visible', "); + YGLog(YGLogLevelDebug, "overflow: 'visible', "); } else if (node->style.overflow == YGOverflowScroll) { - CSSLog(YGLogLevelDebug, "overflow: 'scroll', "); + YGLog(YGLogLevelDebug, "overflow: 'scroll', "); } - if (eqFour(node->style.margin)) { - printNumberIfNotZero("margin", computedEdgeValue(node->style.margin, YGEdgeLeft, 0)); + if (YGFourFloatsEqual(node->style.margin)) { + YGPrintNumberIfNotZero("margin", YGComputedEdgeValue(node->style.margin, YGEdgeLeft, 0)); } else { - printNumberIfNotZero("marginLeft", computedEdgeValue(node->style.margin, YGEdgeLeft, 0)); - printNumberIfNotZero("marginRight", computedEdgeValue(node->style.margin, YGEdgeRight, 0)); - printNumberIfNotZero("marginTop", computedEdgeValue(node->style.margin, YGEdgeTop, 0)); - printNumberIfNotZero("marginBottom", computedEdgeValue(node->style.margin, YGEdgeBottom, 0)); - printNumberIfNotZero("marginStart", computedEdgeValue(node->style.margin, YGEdgeStart, 0)); - printNumberIfNotZero("marginEnd", computedEdgeValue(node->style.margin, YGEdgeEnd, 0)); + YGPrintNumberIfNotZero("marginLeft", YGComputedEdgeValue(node->style.margin, YGEdgeLeft, 0)); + YGPrintNumberIfNotZero("marginRight", + YGComputedEdgeValue(node->style.margin, YGEdgeRight, 0)); + YGPrintNumberIfNotZero("marginTop", YGComputedEdgeValue(node->style.margin, YGEdgeTop, 0)); + YGPrintNumberIfNotZero("marginBottom", + YGComputedEdgeValue(node->style.margin, YGEdgeBottom, 0)); + YGPrintNumberIfNotZero("marginStart", + YGComputedEdgeValue(node->style.margin, YGEdgeStart, 0)); + YGPrintNumberIfNotZero("marginEnd", YGComputedEdgeValue(node->style.margin, YGEdgeEnd, 0)); } - if (eqFour(node->style.padding)) { - printNumberIfNotZero("padding", computedEdgeValue(node->style.padding, YGEdgeLeft, 0)); + if (YGFourFloatsEqual(node->style.padding)) { + YGPrintNumberIfNotZero("padding", YGComputedEdgeValue(node->style.padding, YGEdgeLeft, 0)); } else { - printNumberIfNotZero("paddingLeft", computedEdgeValue(node->style.padding, YGEdgeLeft, 0)); - printNumberIfNotZero("paddingRight", computedEdgeValue(node->style.padding, YGEdgeRight, 0)); - printNumberIfNotZero("paddingTop", computedEdgeValue(node->style.padding, YGEdgeTop, 0)); - printNumberIfNotZero("paddingBottom", - computedEdgeValue(node->style.padding, YGEdgeBottom, 0)); - printNumberIfNotZero("paddingStart", computedEdgeValue(node->style.padding, YGEdgeStart, 0)); - printNumberIfNotZero("paddingEnd", computedEdgeValue(node->style.padding, YGEdgeEnd, 0)); + YGPrintNumberIfNotZero("paddingLeft", + YGComputedEdgeValue(node->style.padding, YGEdgeLeft, 0)); + YGPrintNumberIfNotZero("paddingRight", + YGComputedEdgeValue(node->style.padding, YGEdgeRight, 0)); + YGPrintNumberIfNotZero("paddingTop", YGComputedEdgeValue(node->style.padding, YGEdgeTop, 0)); + YGPrintNumberIfNotZero("paddingBottom", + YGComputedEdgeValue(node->style.padding, YGEdgeBottom, 0)); + YGPrintNumberIfNotZero("paddingStart", + YGComputedEdgeValue(node->style.padding, YGEdgeStart, 0)); + YGPrintNumberIfNotZero("paddingEnd", YGComputedEdgeValue(node->style.padding, YGEdgeEnd, 0)); } - if (eqFour(node->style.border)) { - printNumberIfNotZero("borderWidth", computedEdgeValue(node->style.border, YGEdgeLeft, 0)); + if (YGFourFloatsEqual(node->style.border)) { + YGPrintNumberIfNotZero("borderWidth", YGComputedEdgeValue(node->style.border, YGEdgeLeft, 0)); } else { - printNumberIfNotZero("borderLeftWidth", computedEdgeValue(node->style.border, YGEdgeLeft, 0)); - printNumberIfNotZero("borderRightWidth", - computedEdgeValue(node->style.border, YGEdgeRight, 0)); - printNumberIfNotZero("borderTopWidth", computedEdgeValue(node->style.border, YGEdgeTop, 0)); - printNumberIfNotZero("borderBottomWidth", - computedEdgeValue(node->style.border, YGEdgeBottom, 0)); - printNumberIfNotZero("borderStartWidth", - computedEdgeValue(node->style.border, YGEdgeStart, 0)); - printNumberIfNotZero("borderEndWidth", computedEdgeValue(node->style.border, YGEdgeEnd, 0)); + YGPrintNumberIfNotZero("borderLeftWidth", + YGComputedEdgeValue(node->style.border, YGEdgeLeft, 0)); + YGPrintNumberIfNotZero("borderRightWidth", + YGComputedEdgeValue(node->style.border, YGEdgeRight, 0)); + YGPrintNumberIfNotZero("borderTopWidth", + YGComputedEdgeValue(node->style.border, YGEdgeTop, 0)); + YGPrintNumberIfNotZero("borderBottomWidth", + YGComputedEdgeValue(node->style.border, YGEdgeBottom, 0)); + YGPrintNumberIfNotZero("borderStartWidth", + YGComputedEdgeValue(node->style.border, YGEdgeStart, 0)); + YGPrintNumberIfNotZero("borderEndWidth", + YGComputedEdgeValue(node->style.border, YGEdgeEnd, 0)); } - printNumberIfNotUndefined("width", node->style.dimensions[YGDimensionWidth]); - printNumberIfNotUndefined("height", node->style.dimensions[YGDimensionHeight]); - printNumberIfNotUndefined("maxWidth", node->style.maxDimensions[YGDimensionWidth]); - printNumberIfNotUndefined("maxHeight", node->style.maxDimensions[YGDimensionHeight]); - printNumberIfNotUndefined("minWidth", node->style.minDimensions[YGDimensionWidth]); - printNumberIfNotUndefined("minHeight", node->style.minDimensions[YGDimensionHeight]); + YGPrintNumberIfNotUndefined("width", node->style.dimensions[YGDimensionWidth]); + YGPrintNumberIfNotUndefined("height", node->style.dimensions[YGDimensionHeight]); + YGPrintNumberIfNotUndefined("maxWidth", node->style.maxDimensions[YGDimensionWidth]); + YGPrintNumberIfNotUndefined("maxHeight", node->style.maxDimensions[YGDimensionHeight]); + YGPrintNumberIfNotUndefined("minWidth", node->style.minDimensions[YGDimensionWidth]); + YGPrintNumberIfNotUndefined("minHeight", node->style.minDimensions[YGDimensionHeight]); if (node->style.positionType == YGPositionTypeAbsolute) { - CSSLog(YGLogLevelDebug, "position: 'absolute', "); + YGLog(YGLogLevelDebug, "position: 'absolute', "); } - printNumberIfNotUndefined("left", - computedEdgeValue(node->style.position, YGEdgeLeft, YGUndefined)); - printNumberIfNotUndefined("right", - computedEdgeValue(node->style.position, YGEdgeRight, YGUndefined)); - printNumberIfNotUndefined("top", - computedEdgeValue(node->style.position, YGEdgeTop, YGUndefined)); - printNumberIfNotUndefined("bottom", - computedEdgeValue(node->style.position, YGEdgeBottom, YGUndefined)); + YGPrintNumberIfNotUndefined("left", + YGComputedEdgeValue(node->style.position, YGEdgeLeft, YGUndefined)); + YGPrintNumberIfNotUndefined( + "right", YGComputedEdgeValue(node->style.position, YGEdgeRight, YGUndefined)); + YGPrintNumberIfNotUndefined("top", + YGComputedEdgeValue(node->style.position, YGEdgeTop, YGUndefined)); + YGPrintNumberIfNotUndefined( + "bottom", YGComputedEdgeValue(node->style.position, YGEdgeBottom, YGUndefined)); } - const uint32_t childCount = CSSNodeListCount(node->children); + const uint32_t childCount = YGNodeListCount(node->children); if (options & YGPrintOptionsChildren && childCount > 0) { - CSSLog(YGLogLevelDebug, "children: [\n"); + YGLog(YGLogLevelDebug, "children: [\n"); for (uint32_t i = 0; i < childCount; i++) { - _CSSNodePrint(CSSNodeGetChild(node, i), options, level + 1); + YGNodePrintInternal(YGNodeGetChild(node, i), options, level + 1); } - indent(level); - CSSLog(YGLogLevelDebug, "]},\n"); + YGIndent(level); + YGLog(YGLogLevelDebug, "]},\n"); } else { - CSSLog(YGLogLevelDebug, "},\n"); + YGLog(YGLogLevelDebug, "},\n"); } } -void CSSNodePrint(const CSSNodeRef node, const YGPrintOptions options) { - _CSSNodePrint(node, options, 0); +void YGNodePrint(const YGNodeRef node, const YGPrintOptions options) { + YGNodePrintInternal(node, options, 0); } static const YGEdge leading[4] = { @@ -696,88 +706,91 @@ static const YGDimension dim[4] = { [YGFlexDirectionRowReverse] = YGDimensionWidth, }; -static inline bool isRowDirection(const YGFlexDirection flexDirection) { +static inline bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection) { return flexDirection == YGFlexDirectionRow || flexDirection == YGFlexDirectionRowReverse; } -static inline bool isColumnDirection(const YGFlexDirection flexDirection) { +static inline bool YGFlexDirectionIsColumn(const YGFlexDirection flexDirection) { return flexDirection == YGFlexDirectionColumn || flexDirection == YGFlexDirectionColumnReverse; } -static inline float getLeadingMargin(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.margin[YGEdgeStart])) { +static inline float YGNodeLeadingMargin(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.margin[YGEdgeStart])) { return node->style.margin[YGEdgeStart]; } - return computedEdgeValue(node->style.margin, leading[axis], 0); + return YGComputedEdgeValue(node->style.margin, leading[axis], 0); } -static float getTrailingMargin(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.margin[YGEdgeEnd])) { +static float YGNodeTrailingMargin(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.margin[YGEdgeEnd])) { return node->style.margin[YGEdgeEnd]; } - return computedEdgeValue(node->style.margin, trailing[axis], 0); + return YGComputedEdgeValue(node->style.margin, trailing[axis], 0); } -static float getLeadingPadding(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.padding[YGEdgeStart]) && +static float YGNodeLeadingPadding(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.padding[YGEdgeStart]) && node->style.padding[YGEdgeStart] >= 0) { return node->style.padding[YGEdgeStart]; } - return fmaxf(computedEdgeValue(node->style.padding, leading[axis], 0), 0); + return fmaxf(YGComputedEdgeValue(node->style.padding, leading[axis], 0), 0); } -static float getTrailingPadding(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.padding[YGEdgeEnd]) && +static float YGNodeTrailingPadding(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.padding[YGEdgeEnd]) && node->style.padding[YGEdgeEnd] >= 0) { return node->style.padding[YGEdgeEnd]; } - return fmaxf(computedEdgeValue(node->style.padding, trailing[axis], 0), 0); + return fmaxf(YGComputedEdgeValue(node->style.padding, trailing[axis], 0), 0); } -static float getLeadingBorder(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.border[YGEdgeStart]) && +static float YGNodeLeadingBorder(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.border[YGEdgeStart]) && node->style.border[YGEdgeStart] >= 0) { return node->style.border[YGEdgeStart]; } - return fmaxf(computedEdgeValue(node->style.border, leading[axis], 0), 0); + return fmaxf(YGComputedEdgeValue(node->style.border, leading[axis], 0), 0); } -static float getTrailingBorder(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.border[YGEdgeEnd]) && +static float YGNodeTrailingBorder(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis) && !YGValueIsUndefined(node->style.border[YGEdgeEnd]) && node->style.border[YGEdgeEnd] >= 0) { return node->style.border[YGEdgeEnd]; } - return fmaxf(computedEdgeValue(node->style.border, trailing[axis], 0), 0); + return fmaxf(YGComputedEdgeValue(node->style.border, trailing[axis], 0), 0); } -static inline float getLeadingPaddingAndBorder(const CSSNodeRef node, const YGFlexDirection axis) { - return getLeadingPadding(node, axis) + getLeadingBorder(node, axis); +static inline float YGNodeLeadingPaddingAndBorder(const YGNodeRef node, + const YGFlexDirection axis) { + return YGNodeLeadingPadding(node, axis) + YGNodeLeadingBorder(node, axis); } -static inline float getTrailingPaddingAndBorder(const CSSNodeRef node, const YGFlexDirection axis) { - return getTrailingPadding(node, axis) + getTrailingBorder(node, axis); +static inline float YGNodeTrailingPaddingAndBorder(const YGNodeRef node, + const YGFlexDirection axis) { + return YGNodeTrailingPadding(node, axis) + YGNodeTrailingBorder(node, axis); } -static inline float getMarginAxis(const CSSNodeRef node, const YGFlexDirection axis) { - return getLeadingMargin(node, axis) + getTrailingMargin(node, axis); +static inline float YGNodeMarginForAxis(const YGNodeRef node, const YGFlexDirection axis) { + return YGNodeLeadingMargin(node, axis) + YGNodeTrailingMargin(node, axis); } -static inline float getPaddingAndBorderAxis(const CSSNodeRef node, const YGFlexDirection axis) { - return getLeadingPaddingAndBorder(node, axis) + getTrailingPaddingAndBorder(node, axis); +static inline float YGNodePaddingAndBorderForAxis(const YGNodeRef node, + const YGFlexDirection axis) { + return YGNodeLeadingPaddingAndBorder(node, axis) + YGNodeTrailingPaddingAndBorder(node, axis); } -static inline YGAlign getAlignItem(const CSSNodeRef node, const CSSNodeRef child) { +static inline YGAlign YGNodeAlignItem(const YGNodeRef node, const YGNodeRef child) { return child->style.alignSelf == YGAlignAuto ? node->style.alignItems : child->style.alignSelf; } -static inline YGDirection resolveDirection(const CSSNodeRef node, - const YGDirection parentDirection) { +static inline YGDirection YGNodeResolveDirection(const YGNodeRef node, + const YGDirection parentDirection) { if (node->style.direction == YGDirectionInherit) { return parentDirection > YGDirectionInherit ? parentDirection : YGDirectionLTR; } else { @@ -785,8 +798,8 @@ static inline YGDirection resolveDirection(const CSSNodeRef node, } } -static inline YGFlexDirection resolveAxis(const YGFlexDirection flexDirection, - const YGDirection direction) { +static inline YGFlexDirection YGFlexDirectionResolve(const YGFlexDirection flexDirection, + const YGDirection direction) { if (direction == YGDirectionRTL) { if (flexDirection == YGFlexDirectionRow) { return YGFlexDirectionRowReverse; @@ -798,111 +811,117 @@ static inline YGFlexDirection resolveAxis(const YGFlexDirection flexDirection, return flexDirection; } -static YGFlexDirection getCrossFlexDirection(const YGFlexDirection flexDirection, - const YGDirection direction) { - return isColumnDirection(flexDirection) ? resolveAxis(YGFlexDirectionRow, direction) - : YGFlexDirectionColumn; +static YGFlexDirection YGFlexDirectionCross(const YGFlexDirection flexDirection, + const YGDirection direction) { + return YGFlexDirectionIsColumn(flexDirection) + ? YGFlexDirectionResolve(YGFlexDirectionRow, direction) + : YGFlexDirectionColumn; } -static inline bool isFlex(const CSSNodeRef node) { +static inline bool YGNodeIsFlex(const YGNodeRef node) { return (node->style.positionType == YGPositionTypeRelative && (node->style.flexGrow != 0 || node->style.flexShrink != 0 || node->style.flex != 0)); } -static inline float getDimWithMargin(const CSSNodeRef node, const YGFlexDirection axis) { - return node->layout.measuredDimensions[dim[axis]] + getLeadingMargin(node, axis) + - getTrailingMargin(node, axis); +static inline float YGNodeDimWithMargin(const YGNodeRef node, const YGFlexDirection axis) { + return node->layout.measuredDimensions[dim[axis]] + YGNodeLeadingMargin(node, axis) + + YGNodeTrailingMargin(node, axis); } -static inline bool isStyleDimDefined(const CSSNodeRef node, const YGFlexDirection axis) { +static inline bool YGNodeIsStyleDimDefined(const YGNodeRef node, const YGFlexDirection axis) { const float value = node->style.dimensions[dim[axis]]; - return !CSSValueIsUndefined(value) && value >= 0.0; + return !YGValueIsUndefined(value) && value >= 0.0; } -static inline bool isLayoutDimDefined(const CSSNodeRef node, const YGFlexDirection axis) { +static inline bool YGNodeIsLayoutDimDefined(const YGNodeRef node, const YGFlexDirection axis) { const float value = node->layout.measuredDimensions[dim[axis]]; - return !CSSValueIsUndefined(value) && value >= 0.0; + return !YGValueIsUndefined(value) && value >= 0.0; } -static inline bool isLeadingPosDefined(const CSSNodeRef node, const YGFlexDirection axis) { - return (isRowDirection(axis) && - !CSSValueIsUndefined( - computedEdgeValue(node->style.position, YGEdgeStart, YGUndefined))) || - !CSSValueIsUndefined(computedEdgeValue(node->style.position, leading[axis], YGUndefined)); +static inline bool YGNodeIsLeadingPosDefined(const YGNodeRef node, const YGFlexDirection axis) { + return (YGFlexDirectionIsRow(axis) && + !YGValueIsUndefined( + YGComputedEdgeValue(node->style.position, YGEdgeStart, YGUndefined))) || + !YGValueIsUndefined(YGComputedEdgeValue(node->style.position, leading[axis], YGUndefined)); } -static inline bool isTrailingPosDefined(const CSSNodeRef node, const YGFlexDirection axis) { - return (isRowDirection(axis) && - !CSSValueIsUndefined(computedEdgeValue(node->style.position, YGEdgeEnd, YGUndefined))) || - !CSSValueIsUndefined(computedEdgeValue(node->style.position, trailing[axis], YGUndefined)); +static inline bool YGNodeIsTrailingPosDefined(const YGNodeRef node, const YGFlexDirection axis) { + return (YGFlexDirectionIsRow(axis) && + !YGValueIsUndefined(YGComputedEdgeValue(node->style.position, YGEdgeEnd, YGUndefined))) || + !YGValueIsUndefined( + YGComputedEdgeValue(node->style.position, trailing[axis], YGUndefined)); } -static float getLeadingPosition(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis)) { - const float leadingPosition = computedEdgeValue(node->style.position, YGEdgeStart, YGUndefined); - if (!CSSValueIsUndefined(leadingPosition)) { +static float YGNodeLeadingPosition(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis)) { + const float leadingPosition = + YGComputedEdgeValue(node->style.position, YGEdgeStart, YGUndefined); + if (!YGValueIsUndefined(leadingPosition)) { return leadingPosition; } } - const float leadingPosition = computedEdgeValue(node->style.position, leading[axis], YGUndefined); + const float leadingPosition = + YGComputedEdgeValue(node->style.position, leading[axis], YGUndefined); - return CSSValueIsUndefined(leadingPosition) ? 0 : leadingPosition; + return YGValueIsUndefined(leadingPosition) ? 0 : leadingPosition; } -static float getTrailingPosition(const CSSNodeRef node, const YGFlexDirection axis) { - if (isRowDirection(axis)) { - const float trailingPosition = computedEdgeValue(node->style.position, YGEdgeEnd, YGUndefined); - if (!CSSValueIsUndefined(trailingPosition)) { +static float YGNodeTrailingPosition(const YGNodeRef node, const YGFlexDirection axis) { + if (YGFlexDirectionIsRow(axis)) { + const float trailingPosition = + YGComputedEdgeValue(node->style.position, YGEdgeEnd, YGUndefined); + if (!YGValueIsUndefined(trailingPosition)) { return trailingPosition; } } const float trailingPosition = - computedEdgeValue(node->style.position, trailing[axis], YGUndefined); + YGComputedEdgeValue(node->style.position, trailing[axis], YGUndefined); - return CSSValueIsUndefined(trailingPosition) ? 0 : trailingPosition; + return YGValueIsUndefined(trailingPosition) ? 0 : trailingPosition; } -static float boundAxisWithinMinAndMax(const CSSNodeRef node, - const YGFlexDirection axis, - const float value) { +static float YGNodeBoundAxisWithinMinAndMax(const YGNodeRef node, + const YGFlexDirection axis, + const float value) { float min = YGUndefined; float max = YGUndefined; - if (isColumnDirection(axis)) { + if (YGFlexDirectionIsColumn(axis)) { min = node->style.minDimensions[YGDimensionHeight]; max = node->style.maxDimensions[YGDimensionHeight]; - } else if (isRowDirection(axis)) { + } else if (YGFlexDirectionIsRow(axis)) { min = node->style.minDimensions[YGDimensionWidth]; max = node->style.maxDimensions[YGDimensionWidth]; } float boundValue = value; - if (!CSSValueIsUndefined(max) && max >= 0.0 && boundValue > max) { + if (!YGValueIsUndefined(max) && max >= 0.0 && boundValue > max) { boundValue = max; } - if (!CSSValueIsUndefined(min) && min >= 0.0 && boundValue < min) { + if (!YGValueIsUndefined(min) && min >= 0.0 && boundValue < min) { boundValue = min; } return boundValue; } -// Like boundAxisWithinMinAndMax but also ensures that the value doesn't go +// Like YGNodeBoundAxisWithinMinAndMax but also ensures that the value doesn't go // below the // padding and border amount. -static inline float boundAxis(const CSSNodeRef node, - const YGFlexDirection axis, - const float value) { - return fmaxf(boundAxisWithinMinAndMax(node, axis, value), getPaddingAndBorderAxis(node, axis)); +static inline float YGNodeBoundAxis(const YGNodeRef node, + const YGFlexDirection axis, + const float value) { + return fmaxf(YGNodeBoundAxisWithinMinAndMax(node, axis, value), + YGNodePaddingAndBorderForAxis(node, axis)); } -static void setTrailingPosition(const CSSNodeRef node, - const CSSNodeRef child, - const YGFlexDirection axis) { +static void YGNodeSetChildTrailingPosition(const YGNodeRef node, + const YGNodeRef child, + const YGFlexDirection axis) { const float size = child->layout.measuredDimensions[dim[axis]]; child->layout.position[trailing[axis]] = node->layout.measuredDimensions[dim[axis]] - size - child->layout.position[pos[axis]]; @@ -910,19 +929,19 @@ static void setTrailingPosition(const CSSNodeRef node, // If both left and right are defined, then use left. Otherwise return // +left or -right depending on which is defined. -static float getRelativePosition(const CSSNodeRef node, const YGFlexDirection axis) { - return isLeadingPosDefined(node, axis) ? getLeadingPosition(node, axis) - : -getTrailingPosition(node, axis); +static float YGNodeRelativePosition(const YGNodeRef node, const YGFlexDirection axis) { + return YGNodeIsLeadingPosDefined(node, axis) ? YGNodeLeadingPosition(node, axis) + : -YGNodeTrailingPosition(node, axis); } -static void constrainMaxSizeForMode(const float maxSize, YGMeasureMode *mode, float *size) { +static void YGConstrainMaxSizeForMode(const float maxSize, YGMeasureMode *mode, float *size) { switch (*mode) { case YGMeasureModeExactly: case YGMeasureModeAtMost: - *size = (CSSValueIsUndefined(maxSize) || *size < maxSize) ? *size : maxSize; + *size = (YGValueIsUndefined(maxSize) || *size < maxSize) ? *size : maxSize; break; case YGMeasureModeUndefined: - if (!CSSValueIsUndefined(maxSize)) { + if (!YGValueIsUndefined(maxSize)) { *mode = YGMeasureModeAtMost; *size = maxSize; } @@ -932,56 +951,58 @@ static void constrainMaxSizeForMode(const float maxSize, YGMeasureMode *mode, fl } } -static void setPosition(const CSSNodeRef node, const YGDirection direction) { - const YGFlexDirection mainAxis = resolveAxis(node->style.flexDirection, direction); - const YGFlexDirection crossAxis = getCrossFlexDirection(mainAxis, direction); - const float relativePositionMain = getRelativePosition(node, mainAxis); - const float relativePositionCross = getRelativePosition(node, crossAxis); +static void YGNodeSetPosition(const YGNodeRef node, const YGDirection direction) { + const YGFlexDirection mainAxis = YGFlexDirectionResolve(node->style.flexDirection, direction); + const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, direction); + const float relativePositionMain = YGNodeRelativePosition(node, mainAxis); + const float relativePositionCross = YGNodeRelativePosition(node, crossAxis); node->layout.position[leading[mainAxis]] = - getLeadingMargin(node, mainAxis) + relativePositionMain; + YGNodeLeadingMargin(node, mainAxis) + relativePositionMain; node->layout.position[trailing[mainAxis]] = - getTrailingMargin(node, mainAxis) + relativePositionMain; + YGNodeTrailingMargin(node, mainAxis) + relativePositionMain; node->layout.position[leading[crossAxis]] = - getLeadingMargin(node, crossAxis) + relativePositionCross; + YGNodeLeadingMargin(node, crossAxis) + relativePositionCross; node->layout.position[trailing[crossAxis]] = - getTrailingMargin(node, crossAxis) + relativePositionCross; + YGNodeTrailingMargin(node, crossAxis) + relativePositionCross; } -static void computeChildFlexBasis(const CSSNodeRef node, - const CSSNodeRef child, - const float width, - const YGMeasureMode widthMode, - const float height, - const YGMeasureMode heightMode, - const YGDirection direction) { - const YGFlexDirection mainAxis = resolveAxis(node->style.flexDirection, direction); - const bool isMainAxisRow = isRowDirection(mainAxis); +static void YGNodeComputeFlexBasisForChild(const YGNodeRef node, + const YGNodeRef child, + const float width, + const YGMeasureMode widthMode, + const float height, + const YGMeasureMode heightMode, + const YGDirection direction) { + const YGFlexDirection mainAxis = YGFlexDirectionResolve(node->style.flexDirection, direction); + const bool isMainAxisRow = YGFlexDirectionIsRow(mainAxis); float childWidth; float childHeight; YGMeasureMode childWidthMeasureMode; YGMeasureMode childHeightMeasureMode; - const bool isRowStyleDimDefined = isStyleDimDefined(child, YGFlexDirectionRow); - const bool isColumnStyleDimDefined = isStyleDimDefined(child, YGFlexDirectionColumn); + const bool isRowStyleDimDefined = YGNodeIsStyleDimDefined(child, YGFlexDirectionRow); + const bool isColumnStyleDimDefined = YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn); - if (!CSSValueIsUndefined(CSSNodeStyleGetFlexBasis(child)) && - !CSSValueIsUndefined(isMainAxisRow ? width : height)) { - if (CSSValueIsUndefined(child->layout.computedFlexBasis) || - (CSSLayoutIsExperimentalFeatureEnabled(YGExperimentalFeatureWebFlexBasis) && + if (!YGValueIsUndefined(YGNodeStyleGetFlexBasis(child)) && + !YGValueIsUndefined(isMainAxisRow ? width : height)) { + if (YGValueIsUndefined(child->layout.computedFlexBasis) || + (YGIsExperimentalFeatureEnabled(YGExperimentalFeatureWebFlexBasis) && child->layout.computedFlexBasisGeneration != gCurrentGenerationCount)) { child->layout.computedFlexBasis = - fmaxf(CSSNodeStyleGetFlexBasis(child), getPaddingAndBorderAxis(child, mainAxis)); + fmaxf(YGNodeStyleGetFlexBasis(child), YGNodePaddingAndBorderForAxis(child, mainAxis)); } } else if (isMainAxisRow && isRowStyleDimDefined) { // The width is definite, so use that as the flex basis. - child->layout.computedFlexBasis = fmaxf(child->style.dimensions[YGDimensionWidth], - getPaddingAndBorderAxis(child, YGFlexDirectionRow)); + child->layout.computedFlexBasis = + fmaxf(child->style.dimensions[YGDimensionWidth], + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow)); } else if (!isMainAxisRow && isColumnStyleDimDefined) { // The height is definite, so use that as the flex basis. - child->layout.computedFlexBasis = fmaxf(child->style.dimensions[YGDimensionHeight], - getPaddingAndBorderAxis(child, YGFlexDirectionColumn)); + child->layout.computedFlexBasis = + fmaxf(child->style.dimensions[YGDimensionHeight], + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionColumn)); } else { // Compute the flex basis and hypothetical main size (i.e. the clamped // flex basis). @@ -991,13 +1012,13 @@ static void computeChildFlexBasis(const CSSNodeRef node, childHeightMeasureMode = YGMeasureModeUndefined; if (isRowStyleDimDefined) { - childWidth = - child->style.dimensions[YGDimensionWidth] + getMarginAxis(child, YGFlexDirectionRow); + childWidth = child->style.dimensions[YGDimensionWidth] + + YGNodeMarginForAxis(child, YGFlexDirectionRow); childWidthMeasureMode = YGMeasureModeExactly; } if (isColumnStyleDimDefined) { - childHeight = - child->style.dimensions[YGDimensionHeight] + getMarginAxis(child, YGFlexDirectionColumn); + childHeight = child->style.dimensions[YGDimensionHeight] + + YGNodeMarginForAxis(child, YGFlexDirectionColumn); childHeightMeasureMode = YGMeasureModeExactly; } @@ -1005,7 +1026,7 @@ static void computeChildFlexBasis(const CSSNodeRef node, // but all major browsers appear to implement the following logic. if ((!isMainAxisRow && node->style.overflow == YGOverflowScroll) || node->style.overflow != YGOverflowScroll) { - if (CSSValueIsUndefined(childWidth) && !CSSValueIsUndefined(width)) { + if (YGValueIsUndefined(childWidth) && !YGValueIsUndefined(width)) { childWidth = width; childWidthMeasureMode = YGMeasureModeAtMost; } @@ -1013,7 +1034,7 @@ static void computeChildFlexBasis(const CSSNodeRef node, if ((isMainAxisRow && node->style.overflow == YGOverflowScroll) || node->style.overflow != YGOverflowScroll) { - if (CSSValueIsUndefined(childHeight) && !CSSValueIsUndefined(height)) { + if (YGValueIsUndefined(childHeight) && !YGValueIsUndefined(height)) { childHeight = height; childHeightMeasureMode = YGMeasureModeAtMost; } @@ -1022,185 +1043,188 @@ static void computeChildFlexBasis(const CSSNodeRef node, // If child has no defined size in the cross axis and is set to stretch, // set the cross // axis to be measured exactly with the available inner width - if (!isMainAxisRow && !CSSValueIsUndefined(width) && !isRowStyleDimDefined && - widthMode == YGMeasureModeExactly && getAlignItem(node, child) == YGAlignStretch) { + if (!isMainAxisRow && !YGValueIsUndefined(width) && !isRowStyleDimDefined && + widthMode == YGMeasureModeExactly && YGNodeAlignItem(node, child) == YGAlignStretch) { childWidth = width; childWidthMeasureMode = YGMeasureModeExactly; } - if (isMainAxisRow && !CSSValueIsUndefined(height) && !isColumnStyleDimDefined && - heightMode == YGMeasureModeExactly && getAlignItem(node, child) == YGAlignStretch) { + if (isMainAxisRow && !YGValueIsUndefined(height) && !isColumnStyleDimDefined && + heightMode == YGMeasureModeExactly && YGNodeAlignItem(node, child) == YGAlignStretch) { childHeight = height; childHeightMeasureMode = YGMeasureModeExactly; } - if (!CSSValueIsUndefined(child->style.aspectRatio)) { + if (!YGValueIsUndefined(child->style.aspectRatio)) { if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) { child->layout.computedFlexBasis = fmaxf(childWidth * child->style.aspectRatio, - getPaddingAndBorderAxis(child, YGFlexDirectionColumn)); + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionColumn)); return; } else if (isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) { - child->layout.computedFlexBasis = fmaxf(childHeight * child->style.aspectRatio, - getPaddingAndBorderAxis(child, YGFlexDirectionRow)); + child->layout.computedFlexBasis = + fmaxf(childHeight * child->style.aspectRatio, + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow)); return; } } - constrainMaxSizeForMode(child->style.maxDimensions[YGDimensionWidth], - &childWidthMeasureMode, - &childWidth); - constrainMaxSizeForMode(child->style.maxDimensions[YGDimensionHeight], - &childHeightMeasureMode, - &childHeight); + YGConstrainMaxSizeForMode(child->style.maxDimensions[YGDimensionWidth], + &childWidthMeasureMode, + &childWidth); + YGConstrainMaxSizeForMode(child->style.maxDimensions[YGDimensionHeight], + &childHeightMeasureMode, + &childHeight); // Measure the child - layoutNodeInternal(child, - childWidth, - childHeight, - direction, - childWidthMeasureMode, - childHeightMeasureMode, - false, - "measure"); + YGLayoutNodeInternal(child, + childWidth, + childHeight, + direction, + childWidthMeasureMode, + childHeightMeasureMode, + false, + "measure"); child->layout.computedFlexBasis = fmaxf(isMainAxisRow ? child->layout.measuredDimensions[YGDimensionWidth] : child->layout.measuredDimensions[YGDimensionHeight], - getPaddingAndBorderAxis(child, mainAxis)); + YGNodePaddingAndBorderForAxis(child, mainAxis)); } child->layout.computedFlexBasisGeneration = gCurrentGenerationCount; } -static void absoluteLayoutChild(const CSSNodeRef node, - const CSSNodeRef child, - const float width, - const YGMeasureMode widthMode, - const YGDirection direction) { - const YGFlexDirection mainAxis = resolveAxis(node->style.flexDirection, direction); - const YGFlexDirection crossAxis = getCrossFlexDirection(mainAxis, direction); - const bool isMainAxisRow = isRowDirection(mainAxis); +static void YGNodeAbsoluteLayoutChild(const YGNodeRef node, + const YGNodeRef child, + const float width, + const YGMeasureMode widthMode, + const YGDirection direction) { + const YGFlexDirection mainAxis = YGFlexDirectionResolve(node->style.flexDirection, direction); + const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, direction); + const bool isMainAxisRow = YGFlexDirectionIsRow(mainAxis); float childWidth = YGUndefined; float childHeight = YGUndefined; YGMeasureMode childWidthMeasureMode = YGMeasureModeUndefined; YGMeasureMode childHeightMeasureMode = YGMeasureModeUndefined; - if (isStyleDimDefined(child, YGFlexDirectionRow)) { + if (YGNodeIsStyleDimDefined(child, YGFlexDirectionRow)) { childWidth = - child->style.dimensions[YGDimensionWidth] + getMarginAxis(child, YGFlexDirectionRow); + child->style.dimensions[YGDimensionWidth] + YGNodeMarginForAxis(child, YGFlexDirectionRow); } else { // If the child doesn't have a specified width, compute the width based // on the left/right // offsets if they're defined. - if (isLeadingPosDefined(child, YGFlexDirectionRow) && - isTrailingPosDefined(child, YGFlexDirectionRow)) { + if (YGNodeIsLeadingPosDefined(child, YGFlexDirectionRow) && + YGNodeIsTrailingPosDefined(child, YGFlexDirectionRow)) { childWidth = node->layout.measuredDimensions[YGDimensionWidth] - - (getLeadingBorder(node, YGFlexDirectionRow) + - getTrailingBorder(node, YGFlexDirectionRow)) - - (getLeadingPosition(child, YGFlexDirectionRow) + - getTrailingPosition(child, YGFlexDirectionRow)); - childWidth = boundAxis(child, YGFlexDirectionRow, childWidth); + (YGNodeLeadingBorder(node, YGFlexDirectionRow) + + YGNodeTrailingBorder(node, YGFlexDirectionRow)) - + (YGNodeLeadingPosition(child, YGFlexDirectionRow) + + YGNodeTrailingPosition(child, YGFlexDirectionRow)); + childWidth = YGNodeBoundAxis(child, YGFlexDirectionRow, childWidth); } } - if (isStyleDimDefined(child, YGFlexDirectionColumn)) { - childHeight = - child->style.dimensions[YGDimensionHeight] + getMarginAxis(child, YGFlexDirectionColumn); + if (YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn)) { + childHeight = child->style.dimensions[YGDimensionHeight] + + YGNodeMarginForAxis(child, YGFlexDirectionColumn); } else { // If the child doesn't have a specified height, compute the height // based on the top/bottom // offsets if they're defined. - if (isLeadingPosDefined(child, YGFlexDirectionColumn) && - isTrailingPosDefined(child, YGFlexDirectionColumn)) { + if (YGNodeIsLeadingPosDefined(child, YGFlexDirectionColumn) && + YGNodeIsTrailingPosDefined(child, YGFlexDirectionColumn)) { childHeight = node->layout.measuredDimensions[YGDimensionHeight] - - (getLeadingBorder(node, YGFlexDirectionColumn) + - getTrailingBorder(node, YGFlexDirectionColumn)) - - (getLeadingPosition(child, YGFlexDirectionColumn) + - getTrailingPosition(child, YGFlexDirectionColumn)); - childHeight = boundAxis(child, YGFlexDirectionColumn, childHeight); + (YGNodeLeadingBorder(node, YGFlexDirectionColumn) + + YGNodeTrailingBorder(node, YGFlexDirectionColumn)) - + (YGNodeLeadingPosition(child, YGFlexDirectionColumn) + + YGNodeTrailingPosition(child, YGFlexDirectionColumn)); + childHeight = YGNodeBoundAxis(child, YGFlexDirectionColumn, childHeight); } } // Exactly one dimension needs to be defined for us to be able to do aspect ratio // calculation. One dimension being the anchor and the other being flexible. - if (CSSValueIsUndefined(childWidth) ^ CSSValueIsUndefined(childHeight)) { - if (!CSSValueIsUndefined(child->style.aspectRatio)) { - if (CSSValueIsUndefined(childWidth)) { + if (YGValueIsUndefined(childWidth) ^ YGValueIsUndefined(childHeight)) { + if (!YGValueIsUndefined(child->style.aspectRatio)) { + if (YGValueIsUndefined(childWidth)) { childWidth = fmaxf(childHeight * child->style.aspectRatio, - getPaddingAndBorderAxis(child, YGFlexDirectionColumn)); - } else if (CSSValueIsUndefined(childHeight)) { + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionColumn)); + } else if (YGValueIsUndefined(childHeight)) { childHeight = fmaxf(childWidth * child->style.aspectRatio, - getPaddingAndBorderAxis(child, YGFlexDirectionRow)); + YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow)); } } } // If we're still missing one or the other dimension, measure the content. - if (CSSValueIsUndefined(childWidth) || CSSValueIsUndefined(childHeight)) { + if (YGValueIsUndefined(childWidth) || YGValueIsUndefined(childHeight)) { childWidthMeasureMode = - CSSValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly; + YGValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly; childHeightMeasureMode = - CSSValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly; + YGValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly; // According to the spec, if the main size is not definite and the // child's inline axis is parallel to the main axis (i.e. it's // horizontal), the child should be sized using "UNDEFINED" in // the main size. Otherwise use "AT_MOST" in the cross axis. - if (!isMainAxisRow && CSSValueIsUndefined(childWidth) && widthMode != YGMeasureModeUndefined) { + if (!isMainAxisRow && YGValueIsUndefined(childWidth) && widthMode != YGMeasureModeUndefined) { childWidth = width; childWidthMeasureMode = YGMeasureModeAtMost; } - layoutNodeInternal(child, + YGLayoutNodeInternal(child, + childWidth, + childHeight, + direction, + childWidthMeasureMode, + childHeightMeasureMode, + false, + "abs-measure"); + childWidth = child->layout.measuredDimensions[YGDimensionWidth] + + YGNodeMarginForAxis(child, YGFlexDirectionRow); + childHeight = child->layout.measuredDimensions[YGDimensionHeight] + + YGNodeMarginForAxis(child, YGFlexDirectionColumn); + } + + YGLayoutNodeInternal(child, childWidth, childHeight, direction, - childWidthMeasureMode, - childHeightMeasureMode, - false, - "abs-measure"); - childWidth = child->layout.measuredDimensions[YGDimensionWidth] + - getMarginAxis(child, YGFlexDirectionRow); - childHeight = child->layout.measuredDimensions[YGDimensionHeight] + - getMarginAxis(child, YGFlexDirectionColumn); - } + YGMeasureModeExactly, + YGMeasureModeExactly, + true, + "abs-layout"); - layoutNodeInternal(child, - childWidth, - childHeight, - direction, - YGMeasureModeExactly, - YGMeasureModeExactly, - true, - "abs-layout"); - - if (isTrailingPosDefined(child, mainAxis) && !isLeadingPosDefined(child, mainAxis)) { + if (YGNodeIsTrailingPosDefined(child, mainAxis) && !YGNodeIsLeadingPosDefined(child, mainAxis)) { child->layout.position[leading[mainAxis]] = node->layout.measuredDimensions[dim[mainAxis]] - child->layout.measuredDimensions[dim[mainAxis]] - - getTrailingBorder(node, mainAxis) - - getTrailingPosition(child, mainAxis); + YGNodeTrailingBorder(node, mainAxis) - + YGNodeTrailingPosition(child, mainAxis); } - if (isTrailingPosDefined(child, crossAxis) && !isLeadingPosDefined(child, crossAxis)) { + if (YGNodeIsTrailingPosDefined(child, crossAxis) && + !YGNodeIsLeadingPosDefined(child, crossAxis)) { child->layout.position[leading[crossAxis]] = node->layout.measuredDimensions[dim[crossAxis]] - child->layout.measuredDimensions[dim[crossAxis]] - - getTrailingBorder(node, crossAxis) - - getTrailingPosition(child, crossAxis); + YGNodeTrailingBorder(node, crossAxis) - + YGNodeTrailingPosition(child, crossAxis); } } -static void setMeasuredDimensionsForNodeWithMeasureFunc(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode) { - CSS_ASSERT(node->measure, "Expected node to have custom measure function"); +static void YGNodeWithMeasureFuncSetMeasuredDimensions(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode) { + YG_ASSERT(node->measure, "Expected node to have custom measure function"); - const float paddingAndBorderAxisRow = getPaddingAndBorderAxis(node, YGFlexDirectionRow); - const float paddingAndBorderAxisColumn = getPaddingAndBorderAxis(node, YGFlexDirectionColumn); - const float marginAxisRow = getMarginAxis(node, YGFlexDirectionRow); - const float marginAxisColumn = getMarginAxis(node, YGFlexDirectionColumn); + const float paddingAndBorderAxisRow = YGNodePaddingAndBorderForAxis(node, YGFlexDirectionRow); + const float paddingAndBorderAxisColumn = + YGNodePaddingAndBorderForAxis(node, YGFlexDirectionColumn); + const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow); + const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn); const float innerWidth = availableWidth - marginAxisRow - paddingAndBorderAxisRow; const float innerHeight = availableHeight - marginAxisColumn - paddingAndBorderAxisColumn; @@ -1208,88 +1232,91 @@ static void setMeasuredDimensionsForNodeWithMeasureFunc(const CSSNodeRef node, if (widthMeasureMode == YGMeasureModeExactly && heightMeasureMode == YGMeasureModeExactly) { // Don't bother sizing the text if both dimensions are already defined. node->layout.measuredDimensions[YGDimensionWidth] = - boundAxis(node, YGFlexDirectionRow, availableWidth - marginAxisRow); + YGNodeBoundAxis(node, YGFlexDirectionRow, availableWidth - marginAxisRow); node->layout.measuredDimensions[YGDimensionHeight] = - boundAxis(node, YGFlexDirectionColumn, availableHeight - marginAxisColumn); + YGNodeBoundAxis(node, YGFlexDirectionColumn, availableHeight - marginAxisColumn); } else if (innerWidth <= 0 || innerHeight <= 0) { // Don't bother sizing the text if there's no horizontal or vertical // space. - node->layout.measuredDimensions[YGDimensionWidth] = boundAxis(node, YGFlexDirectionRow, 0); - node->layout.measuredDimensions[YGDimensionHeight] = boundAxis(node, YGFlexDirectionColumn, 0); + node->layout.measuredDimensions[YGDimensionWidth] = + YGNodeBoundAxis(node, YGFlexDirectionRow, 0); + node->layout.measuredDimensions[YGDimensionHeight] = + YGNodeBoundAxis(node, YGFlexDirectionColumn, 0); } else { // Measure the text under the current constraints. - const CSSSize measuredSize = + const YGSize measuredSize = node->measure(node, innerWidth, widthMeasureMode, innerHeight, heightMeasureMode); node->layout.measuredDimensions[YGDimensionWidth] = - boundAxis(node, - YGFlexDirectionRow, - (widthMeasureMode == YGMeasureModeUndefined || - widthMeasureMode == YGMeasureModeAtMost) - ? measuredSize.width + paddingAndBorderAxisRow - : availableWidth - marginAxisRow); + YGNodeBoundAxis(node, + YGFlexDirectionRow, + (widthMeasureMode == YGMeasureModeUndefined || + widthMeasureMode == YGMeasureModeAtMost) + ? measuredSize.width + paddingAndBorderAxisRow + : availableWidth - marginAxisRow); node->layout.measuredDimensions[YGDimensionHeight] = - boundAxis(node, - YGFlexDirectionColumn, - (heightMeasureMode == YGMeasureModeUndefined || - heightMeasureMode == YGMeasureModeAtMost) - ? measuredSize.height + paddingAndBorderAxisColumn - : availableHeight - marginAxisColumn); + YGNodeBoundAxis(node, + YGFlexDirectionColumn, + (heightMeasureMode == YGMeasureModeUndefined || + heightMeasureMode == YGMeasureModeAtMost) + ? measuredSize.height + paddingAndBorderAxisColumn + : availableHeight - marginAxisColumn); } } // For nodes with no children, use the available values if they were provided, // or the minimum size as indicated by the padding and border sizes. -static void setMeasuredDimensionsForEmptyContainer(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode) { - const float paddingAndBorderAxisRow = getPaddingAndBorderAxis(node, YGFlexDirectionRow); - const float paddingAndBorderAxisColumn = getPaddingAndBorderAxis(node, YGFlexDirectionColumn); - const float marginAxisRow = getMarginAxis(node, YGFlexDirectionRow); - const float marginAxisColumn = getMarginAxis(node, YGFlexDirectionColumn); +static void YGNodeEmptyContainerSetMeasuredDimensions(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode) { + const float paddingAndBorderAxisRow = YGNodePaddingAndBorderForAxis(node, YGFlexDirectionRow); + const float paddingAndBorderAxisColumn = + YGNodePaddingAndBorderForAxis(node, YGFlexDirectionColumn); + const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow); + const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn); node->layout.measuredDimensions[YGDimensionWidth] = - boundAxis(node, - YGFlexDirectionRow, - (widthMeasureMode == YGMeasureModeUndefined || - widthMeasureMode == YGMeasureModeAtMost) - ? paddingAndBorderAxisRow - : availableWidth - marginAxisRow); + YGNodeBoundAxis(node, + YGFlexDirectionRow, + (widthMeasureMode == YGMeasureModeUndefined || + widthMeasureMode == YGMeasureModeAtMost) + ? paddingAndBorderAxisRow + : availableWidth - marginAxisRow); node->layout.measuredDimensions[YGDimensionHeight] = - boundAxis(node, - YGFlexDirectionColumn, - (heightMeasureMode == YGMeasureModeUndefined || - heightMeasureMode == YGMeasureModeAtMost) - ? paddingAndBorderAxisColumn - : availableHeight - marginAxisColumn); + YGNodeBoundAxis(node, + YGFlexDirectionColumn, + (heightMeasureMode == YGMeasureModeUndefined || + heightMeasureMode == YGMeasureModeAtMost) + ? paddingAndBorderAxisColumn + : availableHeight - marginAxisColumn); } -static bool setMeasuredDimensionsIfEmptyOrFixedSize(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode) { +static bool YGNodeFixedSizeSetMeasuredDimensions(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode) { if ((widthMeasureMode == YGMeasureModeAtMost && availableWidth <= 0) || (heightMeasureMode == YGMeasureModeAtMost && availableHeight <= 0) || (widthMeasureMode == YGMeasureModeExactly && heightMeasureMode == YGMeasureModeExactly)) { - const float marginAxisColumn = getMarginAxis(node, YGFlexDirectionColumn); - const float marginAxisRow = getMarginAxis(node, YGFlexDirectionRow); + const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn); + const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow); node->layout.measuredDimensions[YGDimensionWidth] = - boundAxis(node, - YGFlexDirectionRow, - CSSValueIsUndefined(availableWidth) || (widthMeasureMode == YGMeasureModeAtMost && availableWidth < 0) - ? 0 - : availableWidth - marginAxisRow); + YGNodeBoundAxis(node, + YGFlexDirectionRow, + YGValueIsUndefined(availableWidth) || (widthMeasureMode == YGMeasureModeAtMost && availableWidth < 0) + ? 0 + : availableWidth - marginAxisRow); node->layout.measuredDimensions[YGDimensionHeight] = - boundAxis(node, - YGFlexDirectionColumn, - CSSValueIsUndefined(availableHeight) || (heightMeasureMode == YGMeasureModeAtMost && availableHeight < 0) - ? 0 - : availableHeight - marginAxisColumn); + YGNodeBoundAxis(node, + YGFlexDirectionColumn, + YGValueIsUndefined(availableHeight) || (heightMeasureMode == YGMeasureModeAtMost && availableHeight < 0) + ? 0 + : availableHeight - marginAxisColumn); return true; } @@ -1300,7 +1327,7 @@ static bool setMeasuredDimensionsIfEmptyOrFixedSize(const CSSNodeRef node, // // This is the main routine that implements a subset of the flexbox layout // algorithm -// described in the W3C CSS documentation: https://www.w3.org/TR/css3-flexbox/. +// described in the W3C YG documentation: https://www.w3.org/TR/YG3-flexbox/. // // Limitations of this algorithm, compared to the full standard: // * Display property is always assumed to be 'flex' except for Text nodes, @@ -1399,45 +1426,44 @@ static bool setMeasuredDimensionsIfEmptyOrFixedSize(const CSSNodeRef node, // support default // minimum main sizes (see above for details). Each of our measure modes maps // to a layout mode -// from the spec (https://www.w3.org/TR/css3-sizing/#terms): +// from the spec (https://www.w3.org/TR/YG3-sizing/#terms): // - YGMeasureModeUndefined: max content // - YGMeasureModeExactly: fill available // - YGMeasureModeAtMost: fit content // -// When calling layoutNodeImpl and layoutNodeInternal, if the caller passes +// When calling YGNodelayoutImpl and YGLayoutNodeInternal, if the caller passes // an available size of // undefined then it must also pass a measure mode of YGMeasureModeUndefined // in that dimension. // -static void layoutNodeImpl(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGDirection parentDirection, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, - const bool performLayout) { - CSS_ASSERT(CSSValueIsUndefined(availableWidth) ? widthMeasureMode == YGMeasureModeUndefined - : true, - "availableWidth is indefinite so widthMeasureMode must be " - "YGMeasureModeUndefined"); - CSS_ASSERT(CSSValueIsUndefined(availableHeight) ? heightMeasureMode == YGMeasureModeUndefined - : true, - "availableHeight is indefinite so heightMeasureMode must be " - "YGMeasureModeUndefined"); +static void YGNodelayoutImpl(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGDirection parentDirection, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode, + const bool performLayout) { + YG_ASSERT(YGValueIsUndefined(availableWidth) ? widthMeasureMode == YGMeasureModeUndefined : true, + "availableWidth is indefinite so widthMeasureMode must be " + "YGMeasureModeUndefined"); + YG_ASSERT(YGValueIsUndefined(availableHeight) ? heightMeasureMode == YGMeasureModeUndefined + : true, + "availableHeight is indefinite so heightMeasureMode must be " + "YGMeasureModeUndefined"); // Set the resolved resolution in the node's layout. - const YGDirection direction = resolveDirection(node, parentDirection); + const YGDirection direction = YGNodeResolveDirection(node, parentDirection); node->layout.direction = direction; if (node->measure) { - setMeasuredDimensionsForNodeWithMeasureFunc( + YGNodeWithMeasureFuncSetMeasuredDimensions( node, availableWidth, availableHeight, widthMeasureMode, heightMeasureMode); return; } - const uint32_t childCount = CSSNodeListCount(node->children); + const uint32_t childCount = YGNodeListCount(node->children); if (childCount == 0) { - setMeasuredDimensionsForEmptyContainer( + YGNodeEmptyContainerSetMeasuredDimensions( node, availableWidth, availableHeight, widthMeasureMode, heightMeasureMode); return; } @@ -1445,34 +1471,35 @@ static void layoutNodeImpl(const CSSNodeRef node, // If we're not being asked to perform a full layout we can skip the algorithm if we already know // the size if (!performLayout && - setMeasuredDimensionsIfEmptyOrFixedSize( + YGNodeFixedSizeSetMeasuredDimensions( node, availableWidth, availableHeight, widthMeasureMode, heightMeasureMode)) { return; } // STEP 1: CALCULATE VALUES FOR REMAINDER OF ALGORITHM - const YGFlexDirection mainAxis = resolveAxis(node->style.flexDirection, direction); - const YGFlexDirection crossAxis = getCrossFlexDirection(mainAxis, direction); - const bool isMainAxisRow = isRowDirection(mainAxis); + const YGFlexDirection mainAxis = YGFlexDirectionResolve(node->style.flexDirection, direction); + const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, direction); + const bool isMainAxisRow = YGFlexDirectionIsRow(mainAxis); const YGJustify justifyContent = node->style.justifyContent; const bool isNodeFlexWrap = node->style.flexWrap == YGWrapWrap; - CSSNodeRef firstAbsoluteChild = NULL; - CSSNodeRef currentAbsoluteChild = NULL; + YGNodeRef firstAbsoluteChild = NULL; + YGNodeRef currentAbsoluteChild = NULL; - const float leadingPaddingAndBorderMain = getLeadingPaddingAndBorder(node, mainAxis); - const float trailingPaddingAndBorderMain = getTrailingPaddingAndBorder(node, mainAxis); - const float leadingPaddingAndBorderCross = getLeadingPaddingAndBorder(node, crossAxis); - const float paddingAndBorderAxisMain = getPaddingAndBorderAxis(node, mainAxis); - const float paddingAndBorderAxisCross = getPaddingAndBorderAxis(node, crossAxis); + const float leadingPaddingAndBorderMain = YGNodeLeadingPaddingAndBorder(node, mainAxis); + const float trailingPaddingAndBorderMain = YGNodeTrailingPaddingAndBorder(node, mainAxis); + const float leadingPaddingAndBorderCross = YGNodeLeadingPaddingAndBorder(node, crossAxis); + const float paddingAndBorderAxisMain = YGNodePaddingAndBorderForAxis(node, mainAxis); + const float paddingAndBorderAxisCross = YGNodePaddingAndBorderForAxis(node, crossAxis); const YGMeasureMode measureModeMainDim = isMainAxisRow ? widthMeasureMode : heightMeasureMode; const YGMeasureMode measureModeCrossDim = isMainAxisRow ? heightMeasureMode : widthMeasureMode; - const float paddingAndBorderAxisRow = getPaddingAndBorderAxis(node, YGFlexDirectionRow); - const float paddingAndBorderAxisColumn = getPaddingAndBorderAxis(node, YGFlexDirectionColumn); - const float marginAxisRow = getMarginAxis(node, YGFlexDirectionRow); - const float marginAxisColumn = getMarginAxis(node, YGFlexDirectionColumn); + const float paddingAndBorderAxisRow = YGNodePaddingAndBorderForAxis(node, YGFlexDirectionRow); + const float paddingAndBorderAxisColumn = + YGNodePaddingAndBorderForAxis(node, YGFlexDirectionColumn); + const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow); + const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn); // STEP 2: DETERMINE AVAILABLE SIZE IN MAIN AND CROSS DIRECTIONS const float availableInnerWidth = availableWidth - marginAxisRow - paddingAndBorderAxisRow; @@ -1484,18 +1511,18 @@ static void layoutNodeImpl(const CSSNodeRef node, // If there is only one child with flexGrow + flexShrink it means we can set the // computedFlexBasis to 0 instead of measuring and shrinking / flexing the child to exactly // match the remaining space - CSSNodeRef singleFlexChild = NULL; + YGNodeRef singleFlexChild = NULL; if ((isMainAxisRow && widthMeasureMode == YGMeasureModeExactly) || (!isMainAxisRow && heightMeasureMode == YGMeasureModeExactly)) { for (uint32_t i = 0; i < childCount; i++) { - const CSSNodeRef child = CSSNodeGetChild(node, i); + const YGNodeRef child = YGNodeGetChild(node, i); if (singleFlexChild) { - if (isFlex(child)) { + if (YGNodeIsFlex(child)) { // There is already a flexible child, abort. singleFlexChild = NULL; break; } - } else if (CSSNodeStyleGetFlexGrow(child) > 0 && CSSNodeStyleGetFlexShrink(child) > 0) { + } else if (YGNodeStyleGetFlexGrow(child) > 0 && YGNodeStyleGetFlexShrink(child) > 0) { singleFlexChild = child; } } @@ -1503,12 +1530,12 @@ static void layoutNodeImpl(const CSSNodeRef node, // STEP 3: DETERMINE FLEX BASIS FOR EACH ITEM for (uint32_t i = 0; i < childCount; i++) { - const CSSNodeRef child = CSSNodeListGet(node->children, i); + const YGNodeRef child = YGNodeListGet(node->children, i); if (performLayout) { // Set the initial position (relative to the parent). - const YGDirection childDirection = resolveDirection(child, direction); - setPosition(child, childDirection); + const YGDirection childDirection = YGNodeResolveDirection(child, direction); + YGNodeSetPosition(child, childDirection); } // Absolute-positioned children don't participate in flex layout. Add them @@ -1529,13 +1556,13 @@ static void layoutNodeImpl(const CSSNodeRef node, child->layout.computedFlexBasisGeneration = gCurrentGenerationCount; child->layout.computedFlexBasis = 0; } else { - computeChildFlexBasis(node, - child, - availableInnerWidth, - widthMeasureMode, - availableInnerHeight, - heightMeasureMode, - direction); + YGNodeComputeFlexBasisForChild(node, + child, + availableInnerWidth, + widthMeasureMode, + availableInnerHeight, + heightMeasureMode, + direction); } } } @@ -1572,17 +1599,17 @@ static void layoutNodeImpl(const CSSNodeRef node, float totalFlexShrinkScaledFactors = 0; // Maintain a linked list of the child nodes that can shrink and/or grow. - CSSNodeRef firstRelativeChild = NULL; - CSSNodeRef currentRelativeChild = NULL; + YGNodeRef firstRelativeChild = NULL; + YGNodeRef currentRelativeChild = NULL; // Add items to the current line until it's full or we run out of items. for (uint32_t i = startOfLineIndex; i < childCount; i++, endOfLineIndex++) { - const CSSNodeRef child = CSSNodeListGet(node->children, i); + const YGNodeRef child = YGNodeListGet(node->children, i); child->lineIndex = lineCount; if (child->style.positionType != YGPositionTypeAbsolute) { const float outerFlexBasis = - child->layout.computedFlexBasis + getMarginAxis(child, mainAxis); + child->layout.computedFlexBasis + YGNodeMarginForAxis(child, mainAxis); // If this is a multi-line flow and this item pushes us over the // available size, we've @@ -1596,14 +1623,14 @@ static void layoutNodeImpl(const CSSNodeRef node, sizeConsumedOnCurrentLine += outerFlexBasis; itemsOnLine++; - if (isFlex(child)) { - totalFlexGrowFactors += CSSNodeStyleGetFlexGrow(child); + if (YGNodeIsFlex(child)) { + totalFlexGrowFactors += YGNodeStyleGetFlexGrow(child); // Unlike the grow factor, the shrink factor is scaled relative to the // child // dimension. totalFlexShrinkScaledFactors += - -CSSNodeStyleGetFlexShrink(child) * child->layout.computedFlexBasis; + -YGNodeStyleGetFlexShrink(child) * child->layout.computedFlexBasis; } // Store a private linked list of children that need to be layed out. @@ -1633,7 +1660,7 @@ static void layoutNodeImpl(const CSSNodeRef node, // If the main dimension size isn't known, it is computed based on // the line length, so there's no more space left to distribute. float remainingFreeSpace = 0; - if (!CSSValueIsUndefined(availableInnerMainDim)) { + if (!YGValueIsUndefined(availableInnerMainDim)) { remainingFreeSpace = availableInnerMainDim - sizeConsumedOnCurrentLine; } else if (sizeConsumedOnCurrentLine < 0) { // availableInnerMainDim is indefinite which means the node is being sized @@ -1670,7 +1697,7 @@ static void layoutNodeImpl(const CSSNodeRef node, // // This two pass approach for resolving min/max constraints deviates from // the spec. The - // spec (https://www.w3.org/TR/css-flexbox-1/#resolve-flexible-lengths) + // spec (https://www.w3.org/TR/YG-flexbox-1/#resolve-flexible-lengths) // describes a process // that needs to be repeated a variable number of times. The algorithm // implemented here @@ -1686,15 +1713,14 @@ static void layoutNodeImpl(const CSSNodeRef node, childFlexBasis = currentRelativeChild->layout.computedFlexBasis; if (remainingFreeSpace < 0) { - flexShrinkScaledFactor = - -CSSNodeStyleGetFlexShrink(currentRelativeChild) * childFlexBasis; + flexShrinkScaledFactor = -YGNodeStyleGetFlexShrink(currentRelativeChild) * childFlexBasis; // Is this child able to shrink? if (flexShrinkScaledFactor != 0) { baseMainSize = childFlexBasis + remainingFreeSpace / totalFlexShrinkScaledFactors * flexShrinkScaledFactor; - boundMainSize = boundAxis(currentRelativeChild, mainAxis, baseMainSize); + boundMainSize = YGNodeBoundAxis(currentRelativeChild, mainAxis, baseMainSize); if (baseMainSize != boundMainSize) { // By excluding this item's size and flex factor from remaining, // this item's @@ -1707,13 +1733,13 @@ static void layoutNodeImpl(const CSSNodeRef node, } } } else if (remainingFreeSpace > 0) { - flexGrowFactor = CSSNodeStyleGetFlexGrow(currentRelativeChild); + flexGrowFactor = YGNodeStyleGetFlexGrow(currentRelativeChild); // Is this child able to grow? if (flexGrowFactor != 0) { baseMainSize = childFlexBasis + remainingFreeSpace / totalFlexGrowFactors * flexGrowFactor; - boundMainSize = boundAxis(currentRelativeChild, mainAxis, baseMainSize); + boundMainSize = YGNodeBoundAxis(currentRelativeChild, mainAxis, baseMainSize); if (baseMainSize != boundMainSize) { // By excluding this item's size and flex factor from remaining, // this item's @@ -1742,8 +1768,7 @@ static void layoutNodeImpl(const CSSNodeRef node, float updatedMainSize = childFlexBasis; if (remainingFreeSpace < 0) { - flexShrinkScaledFactor = - -CSSNodeStyleGetFlexShrink(currentRelativeChild) * childFlexBasis; + flexShrinkScaledFactor = -YGNodeStyleGetFlexShrink(currentRelativeChild) * childFlexBasis; // Is this child able to shrink? if (flexShrinkScaledFactor != 0) { float childSize; @@ -1756,18 +1781,18 @@ static void layoutNodeImpl(const CSSNodeRef node, (remainingFreeSpace / totalFlexShrinkScaledFactors) * flexShrinkScaledFactor; } - updatedMainSize = boundAxis(currentRelativeChild, mainAxis, childSize); + updatedMainSize = YGNodeBoundAxis(currentRelativeChild, mainAxis, childSize); } } else if (remainingFreeSpace > 0) { - flexGrowFactor = CSSNodeStyleGetFlexGrow(currentRelativeChild); + flexGrowFactor = YGNodeStyleGetFlexGrow(currentRelativeChild); // Is this child able to grow? if (flexGrowFactor != 0) { updatedMainSize = - boundAxis(currentRelativeChild, - mainAxis, - childFlexBasis + - remainingFreeSpace / totalFlexGrowFactors * flexGrowFactor); + YGNodeBoundAxis(currentRelativeChild, + mainAxis, + childFlexBasis + + remainingFreeSpace / totalFlexGrowFactors * flexGrowFactor); } } @@ -1779,80 +1804,82 @@ static void layoutNodeImpl(const CSSNodeRef node, YGMeasureMode childHeightMeasureMode; if (isMainAxisRow) { - childWidth = updatedMainSize + getMarginAxis(currentRelativeChild, YGFlexDirectionRow); + childWidth = + updatedMainSize + YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionRow); childWidthMeasureMode = YGMeasureModeExactly; - if (!CSSValueIsUndefined(availableInnerCrossDim) && - !isStyleDimDefined(currentRelativeChild, YGFlexDirectionColumn) && + if (!YGValueIsUndefined(availableInnerCrossDim) && + !YGNodeIsStyleDimDefined(currentRelativeChild, YGFlexDirectionColumn) && heightMeasureMode == YGMeasureModeExactly && - getAlignItem(node, currentRelativeChild) == YGAlignStretch) { + YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch) { childHeight = availableInnerCrossDim; childHeightMeasureMode = YGMeasureModeExactly; - } else if (!isStyleDimDefined(currentRelativeChild, YGFlexDirectionColumn)) { + } else if (!YGNodeIsStyleDimDefined(currentRelativeChild, YGFlexDirectionColumn)) { childHeight = availableInnerCrossDim; childHeightMeasureMode = - CSSValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeAtMost; + YGValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeAtMost; } else { childHeight = currentRelativeChild->style.dimensions[YGDimensionHeight] + - getMarginAxis(currentRelativeChild, YGFlexDirectionColumn); + YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionColumn); childHeightMeasureMode = YGMeasureModeExactly; } } else { childHeight = - updatedMainSize + getMarginAxis(currentRelativeChild, YGFlexDirectionColumn); + updatedMainSize + YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionColumn); childHeightMeasureMode = YGMeasureModeExactly; - if (!CSSValueIsUndefined(availableInnerCrossDim) && - !isStyleDimDefined(currentRelativeChild, YGFlexDirectionRow) && + if (!YGValueIsUndefined(availableInnerCrossDim) && + !YGNodeIsStyleDimDefined(currentRelativeChild, YGFlexDirectionRow) && widthMeasureMode == YGMeasureModeExactly && - getAlignItem(node, currentRelativeChild) == YGAlignStretch) { + YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch) { childWidth = availableInnerCrossDim; childWidthMeasureMode = YGMeasureModeExactly; - } else if (!isStyleDimDefined(currentRelativeChild, YGFlexDirectionRow)) { + } else if (!YGNodeIsStyleDimDefined(currentRelativeChild, YGFlexDirectionRow)) { childWidth = availableInnerCrossDim; childWidthMeasureMode = - CSSValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeAtMost; + YGValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeAtMost; } else { childWidth = currentRelativeChild->style.dimensions[YGDimensionWidth] + - getMarginAxis(currentRelativeChild, YGFlexDirectionRow); + YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionRow); childWidthMeasureMode = YGMeasureModeExactly; } } - if (!CSSValueIsUndefined(currentRelativeChild->style.aspectRatio)) { + if (!YGValueIsUndefined(currentRelativeChild->style.aspectRatio)) { if (isMainAxisRow && childHeightMeasureMode != YGMeasureModeExactly) { childHeight = fmaxf(childWidth * currentRelativeChild->style.aspectRatio, - getPaddingAndBorderAxis(currentRelativeChild, YGFlexDirectionColumn)); + YGNodePaddingAndBorderForAxis(currentRelativeChild, YGFlexDirectionColumn)); childHeightMeasureMode = YGMeasureModeExactly; } else if (!isMainAxisRow && childWidthMeasureMode != YGMeasureModeExactly) { - childWidth = fmaxf(childHeight * currentRelativeChild->style.aspectRatio, - getPaddingAndBorderAxis(currentRelativeChild, YGFlexDirectionRow)); + childWidth = + fmaxf(childHeight * currentRelativeChild->style.aspectRatio, + YGNodePaddingAndBorderForAxis(currentRelativeChild, YGFlexDirectionRow)); childWidthMeasureMode = YGMeasureModeExactly; } } - constrainMaxSizeForMode(currentRelativeChild->style.maxDimensions[YGDimensionWidth], - &childWidthMeasureMode, - &childWidth); - constrainMaxSizeForMode(currentRelativeChild->style.maxDimensions[YGDimensionHeight], - &childHeightMeasureMode, - &childHeight); + YGConstrainMaxSizeForMode(currentRelativeChild->style.maxDimensions[YGDimensionWidth], + &childWidthMeasureMode, + &childWidth); + YGConstrainMaxSizeForMode(currentRelativeChild->style.maxDimensions[YGDimensionHeight], + &childHeightMeasureMode, + &childHeight); const bool requiresStretchLayout = - !isStyleDimDefined(currentRelativeChild, crossAxis) && - getAlignItem(node, currentRelativeChild) == YGAlignStretch; + !YGNodeIsStyleDimDefined(currentRelativeChild, crossAxis) && + YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch; // Recursively call the layout algorithm for this child with the updated // main size. - layoutNodeInternal(currentRelativeChild, - childWidth, - childHeight, - direction, - childWidthMeasureMode, - childHeightMeasureMode, - performLayout && !requiresStretchLayout, - "flex"); + YGLayoutNodeInternal(currentRelativeChild, + childWidth, + childHeight, + direction, + childWidthMeasureMode, + childHeightMeasureMode, + performLayout && !requiresStretchLayout, + "flex"); currentRelativeChild = currentRelativeChild->nextChild; } @@ -1873,7 +1900,7 @@ static void layoutNodeImpl(const CSSNodeRef node, // constraint by the min size defined for the main axis. if (measureModeMainDim == YGMeasureModeAtMost && remainingFreeSpace > 0) { - if (!CSSValueIsUndefined(node->style.minDimensions[dim[mainAxis]]) && + if (!YGValueIsUndefined(node->style.minDimensions[dim[mainAxis]]) && node->style.minDimensions[dim[mainAxis]] >= 0) { remainingFreeSpace = fmaxf(0, node->style.minDimensions[dim[mainAxis]] - @@ -1911,17 +1938,17 @@ static void layoutNodeImpl(const CSSNodeRef node, float crossDim = 0; for (uint32_t i = startOfLineIndex; i < endOfLineIndex; i++) { - const CSSNodeRef child = CSSNodeListGet(node->children, i); + const YGNodeRef child = YGNodeListGet(node->children, i); if (child->style.positionType == YGPositionTypeAbsolute && - isLeadingPosDefined(child, mainAxis)) { + YGNodeIsLeadingPosDefined(child, mainAxis)) { if (performLayout) { // In case the child is position absolute and has left/top being // defined, we override the position to whatever the user said // (and margin/border). - child->layout.position[pos[mainAxis]] = getLeadingPosition(child, mainAxis) + - getLeadingBorder(node, mainAxis) + - getLeadingMargin(child, mainAxis); + child->layout.position[pos[mainAxis]] = YGNodeLeadingPosition(child, mainAxis) + + YGNodeLeadingBorder(node, mainAxis) + + YGNodeLeadingMargin(child, mainAxis); } } else { // Now that we placed the element, we need to update the variables. @@ -1935,23 +1962,23 @@ static void layoutNodeImpl(const CSSNodeRef node, if (canSkipFlex) { // If we skipped the flex step, then we can't rely on the // measuredDims because - // they weren't computed. This means we can't call getDimWithMargin. - mainDim += - betweenMainDim + getMarginAxis(child, mainAxis) + child->layout.computedFlexBasis; + // they weren't computed. This means we can't call YGNodeDimWithMargin. + mainDim += betweenMainDim + YGNodeMarginForAxis(child, mainAxis) + + child->layout.computedFlexBasis; crossDim = availableInnerCrossDim; } else { // The main dimension is the sum of all the elements dimension plus // the spacing. - mainDim += betweenMainDim + getDimWithMargin(child, mainAxis); + mainDim += betweenMainDim + YGNodeDimWithMargin(child, mainAxis); // The cross dimension is the max of the elements dimension since // there // can only be one element in that cross dimension. - crossDim = fmaxf(crossDim, getDimWithMargin(child, crossAxis)); + crossDim = fmaxf(crossDim, YGNodeDimWithMargin(child, crossAxis)); } } else if (performLayout) { child->layout.position[pos[mainAxis]] += - getLeadingBorder(node, mainAxis) + leadingMainDim; + YGNodeLeadingBorder(node, mainAxis) + leadingMainDim; } } } @@ -1962,7 +1989,7 @@ static void layoutNodeImpl(const CSSNodeRef node, if (measureModeCrossDim == YGMeasureModeUndefined || measureModeCrossDim == YGMeasureModeAtMost) { // Compute the cross axis from the max cross dimension of the children. - containerCrossAxis = boundAxis(node, crossAxis, crossDim + paddingAndBorderAxisCross) - + containerCrossAxis = YGNodeBoundAxis(node, crossAxis, crossDim + paddingAndBorderAxisCross) - paddingAndBorderAxisCross; if (measureModeCrossDim == YGMeasureModeAtMost) { @@ -1976,27 +2003,27 @@ static void layoutNodeImpl(const CSSNodeRef node, } // Clamp to the min/max size specified on the container. - crossDim = boundAxis(node, crossAxis, crossDim + paddingAndBorderAxisCross) - + crossDim = YGNodeBoundAxis(node, crossAxis, crossDim + paddingAndBorderAxisCross) - paddingAndBorderAxisCross; // STEP 7: CROSS-AXIS ALIGNMENT // We can skip child alignment if we're just measuring the container. if (performLayout) { for (uint32_t i = startOfLineIndex; i < endOfLineIndex; i++) { - const CSSNodeRef child = CSSNodeListGet(node->children, i); + const YGNodeRef child = YGNodeListGet(node->children, i); if (child->style.positionType == YGPositionTypeAbsolute) { // If the child is absolutely positioned and has a // top/left/bottom/right // set, override all the previously computed positions to set it // correctly. - if (isLeadingPosDefined(child, crossAxis)) { - child->layout.position[pos[crossAxis]] = getLeadingPosition(child, crossAxis) + - getLeadingBorder(node, crossAxis) + - getLeadingMargin(child, crossAxis); + if (YGNodeIsLeadingPosDefined(child, crossAxis)) { + child->layout.position[pos[crossAxis]] = YGNodeLeadingPosition(child, crossAxis) + + YGNodeLeadingBorder(node, crossAxis) + + YGNodeLeadingMargin(child, crossAxis); } else { child->layout.position[pos[crossAxis]] = - getLeadingBorder(node, crossAxis) + getLeadingMargin(child, crossAxis); + YGNodeLeadingBorder(node, crossAxis) + YGNodeLeadingMargin(child, crossAxis); } } else { float leadingCrossDim = leadingPaddingAndBorderCross; @@ -2004,7 +2031,7 @@ static void layoutNodeImpl(const CSSNodeRef node, // For a relative children, we're either using alignItems (parent) or // alignSelf (child) in order to determine the position in the cross // axis - const YGAlign alignItem = getAlignItem(node, child); + const YGAlign alignItem = YGNodeAlignItem(node, child); // If the child uses align stretch, we need to lay it out one more // time, this time @@ -2012,8 +2039,8 @@ static void layoutNodeImpl(const CSSNodeRef node, // current line. if (alignItem == YGAlignStretch) { const bool isCrossSizeDefinite = - (isMainAxisRow && isStyleDimDefined(child, YGFlexDirectionColumn)) || - (!isMainAxisRow && isStyleDimDefined(child, YGFlexDirectionRow)); + (isMainAxisRow && YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn)) || + (!isMainAxisRow && YGNodeIsStyleDimDefined(child, YGFlexDirectionRow)); float childWidth; float childHeight; @@ -2023,39 +2050,40 @@ static void layoutNodeImpl(const CSSNodeRef node, if (isMainAxisRow) { childHeight = crossDim; childWidth = child->layout.measuredDimensions[YGDimensionWidth] + - getMarginAxis(child, YGFlexDirectionRow); + YGNodeMarginForAxis(child, YGFlexDirectionRow); } else { childWidth = crossDim; childHeight = child->layout.measuredDimensions[YGDimensionHeight] + - getMarginAxis(child, YGFlexDirectionColumn); + YGNodeMarginForAxis(child, YGFlexDirectionColumn); } - constrainMaxSizeForMode(child->style.maxDimensions[YGDimensionWidth], - &childWidthMeasureMode, - &childWidth); - constrainMaxSizeForMode(child->style.maxDimensions[YGDimensionHeight], - &childHeightMeasureMode, - &childHeight); + YGConstrainMaxSizeForMode(child->style.maxDimensions[YGDimensionWidth], + &childWidthMeasureMode, + &childWidth); + YGConstrainMaxSizeForMode(child->style.maxDimensions[YGDimensionHeight], + &childHeightMeasureMode, + &childHeight); // If the child defines a definite size for its cross axis, there's // no need to stretch. if (!isCrossSizeDefinite) { childWidthMeasureMode = - CSSValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly; + YGValueIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly; childHeightMeasureMode = - CSSValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly; + YGValueIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly; - layoutNodeInternal(child, - childWidth, - childHeight, - direction, - childWidthMeasureMode, - childHeightMeasureMode, - true, - "stretch"); + YGLayoutNodeInternal(child, + childWidth, + childHeight, + direction, + childWidthMeasureMode, + childHeightMeasureMode, + true, + "stretch"); } } else if (alignItem != YGAlignFlexStart) { - const float remainingCrossDim = containerCrossAxis - getDimWithMargin(child, crossAxis); + const float remainingCrossDim = + containerCrossAxis - YGNodeDimWithMargin(child, crossAxis); if (alignItem == YGAlignCenter) { leadingCrossDim += remainingCrossDim / 2; @@ -2075,7 +2103,7 @@ static void layoutNodeImpl(const CSSNodeRef node, } // STEP 8: MULTI-LINE CONTENT ALIGNMENT - if (lineCount > 1 && performLayout && !CSSValueIsUndefined(availableInnerCrossDim)) { + if (lineCount > 1 && performLayout && !YGValueIsUndefined(availableInnerCrossDim)) { const float remainingAlignContentDim = availableInnerCrossDim - totalLineCrossDim; float crossDimLead = 0; @@ -2107,17 +2135,17 @@ static void layoutNodeImpl(const CSSNodeRef node, // compute the line's height and find the endIndex float lineHeight = 0; for (ii = startIndex; ii < childCount; ii++) { - const CSSNodeRef child = CSSNodeListGet(node->children, ii); + const YGNodeRef child = YGNodeListGet(node->children, ii); if (child->style.positionType == YGPositionTypeRelative) { if (child->lineIndex != i) { break; } - if (isLayoutDimDefined(child, crossAxis)) { + if (YGNodeIsLayoutDimDefined(child, crossAxis)) { lineHeight = fmaxf(lineHeight, child->layout.measuredDimensions[dim[crossAxis]] + - getMarginAxis(child, crossAxis)); + YGNodeMarginForAxis(child, crossAxis)); } } } @@ -2126,18 +2154,18 @@ static void layoutNodeImpl(const CSSNodeRef node, if (performLayout) { for (ii = startIndex; ii < endIndex; ii++) { - const CSSNodeRef child = CSSNodeListGet(node->children, ii); + const YGNodeRef child = YGNodeListGet(node->children, ii); if (child->style.positionType == YGPositionTypeRelative) { - switch (getAlignItem(node, child)) { + switch (YGNodeAlignItem(node, child)) { case YGAlignFlexStart: { child->layout.position[pos[crossAxis]] = - currentLead + getLeadingMargin(child, crossAxis); + currentLead + YGNodeLeadingMargin(child, crossAxis); break; } case YGAlignFlexEnd: { child->layout.position[pos[crossAxis]] = - currentLead + lineHeight - getTrailingMargin(child, crossAxis) - + currentLead + lineHeight - YGNodeTrailingMargin(child, crossAxis) - child->layout.measuredDimensions[dim[crossAxis]]; break; } @@ -2149,7 +2177,7 @@ static void layoutNodeImpl(const CSSNodeRef node, } case YGAlignStretch: { child->layout.position[pos[crossAxis]] = - currentLead + getLeadingMargin(child, crossAxis); + currentLead + YGNodeLeadingMargin(child, crossAxis); // TODO(prenaux): Correctly set the height of items with indefinite // (auto) crossAxis dimension. break; @@ -2168,20 +2196,21 @@ static void layoutNodeImpl(const CSSNodeRef node, // STEP 9: COMPUTING FINAL DIMENSIONS node->layout.measuredDimensions[YGDimensionWidth] = - boundAxis(node, YGFlexDirectionRow, availableWidth - marginAxisRow); + YGNodeBoundAxis(node, YGFlexDirectionRow, availableWidth - marginAxisRow); node->layout.measuredDimensions[YGDimensionHeight] = - boundAxis(node, YGFlexDirectionColumn, availableHeight - marginAxisColumn); + YGNodeBoundAxis(node, YGFlexDirectionColumn, availableHeight - marginAxisColumn); // If the user didn't specify a width or height for the node, set the // dimensions based on the children. if (measureModeMainDim == YGMeasureModeUndefined) { // Clamp the size to the min/max size, if specified, and make sure it // doesn't go below the padding and border amount. - node->layout.measuredDimensions[dim[mainAxis]] = boundAxis(node, mainAxis, maxLineMainDim); + node->layout.measuredDimensions[dim[mainAxis]] = + YGNodeBoundAxis(node, mainAxis, maxLineMainDim); } else if (measureModeMainDim == YGMeasureModeAtMost) { node->layout.measuredDimensions[dim[mainAxis]] = fmaxf(fminf(availableInnerMainDim + paddingAndBorderAxisMain, - boundAxisWithinMinAndMax(node, mainAxis, maxLineMainDim)), + YGNodeBoundAxisWithinMinAndMax(node, mainAxis, maxLineMainDim)), paddingAndBorderAxisMain); } @@ -2189,13 +2218,13 @@ static void layoutNodeImpl(const CSSNodeRef node, // Clamp the size to the min/max size, if specified, and make sure it // doesn't go below the padding and border amount. node->layout.measuredDimensions[dim[crossAxis]] = - boundAxis(node, crossAxis, totalLineCrossDim + paddingAndBorderAxisCross); + YGNodeBoundAxis(node, crossAxis, totalLineCrossDim + paddingAndBorderAxisCross); } else if (measureModeCrossDim == YGMeasureModeAtMost) { node->layout.measuredDimensions[dim[crossAxis]] = fmaxf(fminf(availableInnerCrossDim + paddingAndBorderAxisCross, - boundAxisWithinMinAndMax(node, - crossAxis, - totalLineCrossDim + paddingAndBorderAxisCross)), + YGNodeBoundAxisWithinMinAndMax(node, + crossAxis, + totalLineCrossDim + paddingAndBorderAxisCross)), paddingAndBorderAxisCross); } @@ -2203,7 +2232,7 @@ static void layoutNodeImpl(const CSSNodeRef node, // STEP 10: SIZING AND POSITIONING ABSOLUTE CHILDREN for (currentAbsoluteChild = firstAbsoluteChild; currentAbsoluteChild != NULL; currentAbsoluteChild = currentAbsoluteChild->nextChild) { - absoluteLayoutChild( + YGNodeAbsoluteLayoutChild( node, currentAbsoluteChild, availableInnerWidth, widthMeasureMode, direction); } @@ -2216,14 +2245,14 @@ static void layoutNodeImpl(const CSSNodeRef node, // Set trailing position if necessary. if (needsMainTrailingPos || needsCrossTrailingPos) { for (uint32_t i = 0; i < childCount; i++) { - const CSSNodeRef child = CSSNodeListGet(node->children, i); + const YGNodeRef child = YGNodeListGet(node->children, i); if (needsMainTrailingPos) { - setTrailingPosition(node, child, mainAxis); + YGNodeSetChildTrailingPosition(node, child, mainAxis); } if (needsCrossTrailingPos) { - setTrailingPosition(node, child, crossAxis); + YGNodeSetChildTrailingPosition(node, child, crossAxis); } } } @@ -2237,7 +2266,7 @@ bool gPrintSkips = false; static const char *spacer = " "; -static const char *getSpacer(const unsigned long level) { +static const char *YGSpacer(const unsigned long level) { const size_t spacerLen = strlen(spacer); if (level > spacerLen) { return &spacer[0]; @@ -2246,7 +2275,7 @@ static const char *getSpacer(const unsigned long level) { } } -static const char *getModeName(const YGMeasureMode mode, const bool performLayout) { +static const char *YGMeasureModeName(const YGMeasureMode mode, const bool performLayout) { const char *kMeasureModeNames[YGMeasureModeCount] = {"UNDEFINED", "EXACTLY", "AT_MOST"}; const char *kLayoutModeNames[YGMeasureModeCount] = {"LAY_UNDEFINED", "LAY_EXACTLY", @@ -2260,89 +2289,90 @@ static const char *getModeName(const YGMeasureMode mode, const bool performLayou return performLayout ? kLayoutModeNames[mode] : kMeasureModeNames[mode]; } -static inline bool newSizeIsExactAndMatchesOldMeasuredSize(YGMeasureMode sizeMode, - float size, - float lastComputedSize) { - return sizeMode == YGMeasureModeExactly && eq(size, lastComputedSize); +static inline bool YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(YGMeasureMode sizeMode, + float size, + float lastComputedSize) { + return sizeMode == YGMeasureModeExactly && YGFloatsEqual(size, lastComputedSize); } -static inline bool oldSizeIsUnspecifiedAndStillFits(YGMeasureMode sizeMode, - float size, - YGMeasureMode lastSizeMode, - float lastComputedSize) { +static inline bool YGMeasureModeOldSizeIsUnspecifiedAndStillFits(YGMeasureMode sizeMode, + float size, + YGMeasureMode lastSizeMode, + float lastComputedSize) { return sizeMode == YGMeasureModeAtMost && lastSizeMode == YGMeasureModeUndefined && size >= lastComputedSize; } -static inline bool newMeasureSizeIsStricterAndStillValid(YGMeasureMode sizeMode, - float size, - YGMeasureMode lastSizeMode, - float lastSize, - float lastComputedSize) { +static inline bool YGMeasureModeNewMeasureSizeIsStricterAndStillValid(YGMeasureMode sizeMode, + float size, + YGMeasureMode lastSizeMode, + float lastSize, + float lastComputedSize) { return lastSizeMode == YGMeasureModeAtMost && sizeMode == YGMeasureModeAtMost && lastSize > size && lastComputedSize <= size; } -bool CSSNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, - const float width, - const YGMeasureMode heightMode, - const float height, - const YGMeasureMode lastWidthMode, - const float lastWidth, - const YGMeasureMode lastHeightMode, - const float lastHeight, - const float lastComputedWidth, - const float lastComputedHeight, - const float marginRow, - const float marginColumn) { +bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, + const float width, + const YGMeasureMode heightMode, + const float height, + const YGMeasureMode lastWidthMode, + const float lastWidth, + const YGMeasureMode lastHeightMode, + const float lastHeight, + const float lastComputedWidth, + const float lastComputedHeight, + const float marginRow, + const float marginColumn) { if (lastComputedHeight < 0 || lastComputedWidth < 0) { return false; } - const bool hasSameWidthSpec = lastWidthMode == widthMode && eq(lastWidth, width); - const bool hasSameHeightSpec = lastHeightMode == heightMode && eq(lastHeight, height); + const bool hasSameWidthSpec = lastWidthMode == widthMode && YGFloatsEqual(lastWidth, width); + const bool hasSameHeightSpec = lastHeightMode == heightMode && YGFloatsEqual(lastHeight, height); const bool widthIsCompatible = - hasSameWidthSpec || - newSizeIsExactAndMatchesOldMeasuredSize(widthMode, width - marginRow, lastComputedWidth) || - oldSizeIsUnspecifiedAndStillFits(widthMode, - width - marginRow, - lastWidthMode, - lastComputedWidth) || - newMeasureSizeIsStricterAndStillValid( + hasSameWidthSpec || YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(widthMode, + width - marginRow, + lastComputedWidth) || + YGMeasureModeOldSizeIsUnspecifiedAndStillFits(widthMode, + width - marginRow, + lastWidthMode, + lastComputedWidth) || + YGMeasureModeNewMeasureSizeIsStricterAndStillValid( widthMode, width - marginRow, lastWidthMode, lastWidth, lastComputedWidth); const bool heightIsCompatible = - hasSameHeightSpec || newSizeIsExactAndMatchesOldMeasuredSize(heightMode, - height - marginColumn, - lastComputedHeight) || - oldSizeIsUnspecifiedAndStillFits(heightMode, - height - marginColumn, - lastHeightMode, - lastComputedHeight) || - newMeasureSizeIsStricterAndStillValid( + hasSameHeightSpec || YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(heightMode, + height - marginColumn, + lastComputedHeight) || + YGMeasureModeOldSizeIsUnspecifiedAndStillFits(heightMode, + height - marginColumn, + lastHeightMode, + lastComputedHeight) || + YGMeasureModeNewMeasureSizeIsStricterAndStillValid( heightMode, height - marginColumn, lastHeightMode, lastHeight, lastComputedHeight); return widthIsCompatible && heightIsCompatible; } // -// This is a wrapper around the layoutNodeImpl function. It determines +// This is a wrapper around the YGNodelayoutImpl function. It determines // whether the layout request is redundant and can be skipped. // // Parameters: -// Input parameters are the same as layoutNodeImpl (see above) +// Input parameters are the same as YGNodelayoutImpl (see above) // Return parameter is true if layout was performed, false if skipped // -bool layoutNodeInternal(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGDirection parentDirection, - const YGMeasureMode widthMeasureMode, - const YGMeasureMode heightMeasureMode, - const bool performLayout, - const char *reason) { - CSSLayout *layout = &node->layout; +bool YGLayoutNodeInternal(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGDirection parentDirection, + const YGMeasureMode widthMeasureMode, + const YGMeasureMode heightMeasureMode, + const bool performLayout, + const char *reason) { + YGLayout *layout = &node->layout; gDepth++; @@ -2359,7 +2389,7 @@ bool layoutNodeInternal(const CSSNodeRef node, layout->cachedLayout.computedHeight = -1; } - CSSCachedMeasurement *cachedResults = NULL; + YGCachedMeasurement *cachedResults = NULL; // Determine whether the results are already cached. We maintain a separate // cache for layouts and measurements. A layout operation modifies the @@ -2374,54 +2404,54 @@ bool layoutNodeInternal(const CSSNodeRef node, // expensive to measure, so it's worth avoiding redundant measurements if at // all possible. if (node->measure) { - const float marginAxisRow = getMarginAxis(node, YGFlexDirectionRow); - const float marginAxisColumn = getMarginAxis(node, YGFlexDirectionColumn); + const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow); + const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn); // First, try to use the layout cache. - if (CSSNodeCanUseCachedMeasurement(widthMeasureMode, - availableWidth, - heightMeasureMode, - availableHeight, - layout->cachedLayout.widthMeasureMode, - layout->cachedLayout.availableWidth, - layout->cachedLayout.heightMeasureMode, - layout->cachedLayout.availableHeight, - layout->cachedLayout.computedWidth, - layout->cachedLayout.computedHeight, - marginAxisRow, - marginAxisColumn)) { + if (YGNodeCanUseCachedMeasurement(widthMeasureMode, + availableWidth, + heightMeasureMode, + availableHeight, + layout->cachedLayout.widthMeasureMode, + layout->cachedLayout.availableWidth, + layout->cachedLayout.heightMeasureMode, + layout->cachedLayout.availableHeight, + layout->cachedLayout.computedWidth, + layout->cachedLayout.computedHeight, + marginAxisRow, + marginAxisColumn)) { cachedResults = &layout->cachedLayout; } else { // Try to use the measurement cache. for (uint32_t i = 0; i < layout->nextCachedMeasurementsIndex; i++) { - if (CSSNodeCanUseCachedMeasurement(widthMeasureMode, - availableWidth, - heightMeasureMode, - availableHeight, - layout->cachedMeasurements[i].widthMeasureMode, - layout->cachedMeasurements[i].availableWidth, - layout->cachedMeasurements[i].heightMeasureMode, - layout->cachedMeasurements[i].availableHeight, - layout->cachedMeasurements[i].computedWidth, - layout->cachedMeasurements[i].computedHeight, - marginAxisRow, - marginAxisColumn)) { + if (YGNodeCanUseCachedMeasurement(widthMeasureMode, + availableWidth, + heightMeasureMode, + availableHeight, + layout->cachedMeasurements[i].widthMeasureMode, + layout->cachedMeasurements[i].availableWidth, + layout->cachedMeasurements[i].heightMeasureMode, + layout->cachedMeasurements[i].availableHeight, + layout->cachedMeasurements[i].computedWidth, + layout->cachedMeasurements[i].computedHeight, + marginAxisRow, + marginAxisColumn)) { cachedResults = &layout->cachedMeasurements[i]; break; } } } } else if (performLayout) { - if (eq(layout->cachedLayout.availableWidth, availableWidth) && - eq(layout->cachedLayout.availableHeight, availableHeight) && + if (YGFloatsEqual(layout->cachedLayout.availableWidth, availableWidth) && + YGFloatsEqual(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++) { - if (eq(layout->cachedMeasurements[i].availableWidth, availableWidth) && - eq(layout->cachedMeasurements[i].availableHeight, availableHeight) && + if (YGFloatsEqual(layout->cachedMeasurements[i].availableWidth, availableWidth) && + YGFloatsEqual(layout->cachedMeasurements[i].availableHeight, availableHeight) && layout->cachedMeasurements[i].widthMeasureMode == widthMeasureMode && layout->cachedMeasurements[i].heightMeasureMode == heightMeasureMode) { cachedResults = &layout->cachedMeasurements[i]; @@ -2435,13 +2465,13 @@ bool layoutNodeInternal(const CSSNodeRef node, layout->measuredDimensions[YGDimensionHeight] = cachedResults->computedHeight; if (gPrintChanges && gPrintSkips) { - printf("%s%d.{[skipped] ", getSpacer(gDepth), gDepth); + printf("%s%d.{[skipped] ", YGSpacer(gDepth), gDepth); if (node->print) { node->print(node); } printf("wm: %s, hm: %s, aw: %f ah: %f => d: (%f, %f) %s\n", - getModeName(widthMeasureMode, performLayout), - getModeName(heightMeasureMode, performLayout), + YGMeasureModeName(widthMeasureMode, performLayout), + YGMeasureModeName(heightMeasureMode, performLayout), availableWidth, availableHeight, cachedResults->computedWidth, @@ -2450,34 +2480,34 @@ bool layoutNodeInternal(const CSSNodeRef node, } } else { if (gPrintChanges) { - printf("%s%d.{%s", getSpacer(gDepth), gDepth, needToVisitNode ? "*" : ""); + printf("%s%d.{%s", YGSpacer(gDepth), gDepth, needToVisitNode ? "*" : ""); if (node->print) { node->print(node); } printf("wm: %s, hm: %s, aw: %f ah: %f %s\n", - getModeName(widthMeasureMode, performLayout), - getModeName(heightMeasureMode, performLayout), + YGMeasureModeName(widthMeasureMode, performLayout), + YGMeasureModeName(heightMeasureMode, performLayout), availableWidth, availableHeight, reason); } - layoutNodeImpl(node, - availableWidth, - availableHeight, - parentDirection, - widthMeasureMode, - heightMeasureMode, - performLayout); + YGNodelayoutImpl(node, + availableWidth, + availableHeight, + parentDirection, + widthMeasureMode, + heightMeasureMode, + performLayout); if (gPrintChanges) { - printf("%s%d.}%s", getSpacer(gDepth), gDepth, needToVisitNode ? "*" : ""); + printf("%s%d.}%s", YGSpacer(gDepth), gDepth, needToVisitNode ? "*" : ""); if (node->print) { node->print(node); } printf("wm: %s, hm: %s, d: (%f, %f) %s\n", - getModeName(widthMeasureMode, performLayout), - getModeName(heightMeasureMode, performLayout), + YGMeasureModeName(widthMeasureMode, performLayout), + YGMeasureModeName(heightMeasureMode, performLayout), layout->measuredDimensions[YGDimensionWidth], layout->measuredDimensions[YGDimensionHeight], reason); @@ -2486,14 +2516,14 @@ bool layoutNodeInternal(const CSSNodeRef node, layout->lastParentDirection = parentDirection; if (cachedResults == NULL) { - if (layout->nextCachedMeasurementsIndex == CSS_MAX_CACHED_RESULT_COUNT) { + if (layout->nextCachedMeasurementsIndex == YG_MAX_CACHED_RESULT_COUNT) { if (gPrintChanges) { printf("Out of cache entries!\n"); } layout->nextCachedMeasurementsIndex = 0; } - CSSCachedMeasurement *newCacheEntry; + YGCachedMeasurement *newCacheEntry; if (performLayout) { // Use the single layout cache entry. newCacheEntry = &layout->cachedLayout; @@ -2524,7 +2554,7 @@ bool layoutNodeInternal(const CSSNodeRef node, return (needToVisitNode || cachedResults == NULL); } -static void roundToPixelGrid(const CSSNodeRef node) { +static void roundToPixelGrid(const YGNodeRef node) { const float fractialLeft = node->layout.position[YGEdgeLeft] - floorf(node->layout.position[YGEdgeLeft]); const float fractialTop = @@ -2537,16 +2567,16 @@ static void roundToPixelGrid(const CSSNodeRef node) { node->layout.position[YGEdgeLeft] = roundf(node->layout.position[YGEdgeLeft]); node->layout.position[YGEdgeTop] = roundf(node->layout.position[YGEdgeTop]); - const uint32_t childCount = CSSNodeListCount(node->children); + const uint32_t childCount = YGNodeListCount(node->children); for (uint32_t i = 0; i < childCount; i++) { - roundToPixelGrid(CSSNodeGetChild(node, i)); + roundToPixelGrid(YGNodeGetChild(node, i)); } } -void CSSNodeCalculateLayout(const CSSNodeRef node, - const float availableWidth, - const float availableHeight, - const YGDirection parentDirection) { +void YGNodeCalculateLayout(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGDirection parentDirection) { // Increment the generation count. This will force the recursive routine to // visit // all dirty nodes at least once. Subsequent visits will be skipped if the @@ -2559,54 +2589,54 @@ void CSSNodeCalculateLayout(const CSSNodeRef node, YGMeasureMode widthMeasureMode = YGMeasureModeUndefined; YGMeasureMode heightMeasureMode = YGMeasureModeUndefined; - if (!CSSValueIsUndefined(width)) { + if (!YGValueIsUndefined(width)) { widthMeasureMode = YGMeasureModeExactly; - } else if (isStyleDimDefined(node, YGFlexDirectionRow)) { - width = - node->style.dimensions[dim[YGFlexDirectionRow]] + getMarginAxis(node, YGFlexDirectionRow); + } else if (YGNodeIsStyleDimDefined(node, YGFlexDirectionRow)) { + width = node->style.dimensions[dim[YGFlexDirectionRow]] + + YGNodeMarginForAxis(node, YGFlexDirectionRow); widthMeasureMode = YGMeasureModeExactly; } else if (node->style.maxDimensions[YGDimensionWidth] >= 0.0) { width = node->style.maxDimensions[YGDimensionWidth]; widthMeasureMode = YGMeasureModeAtMost; } - if (!CSSValueIsUndefined(height)) { + if (!YGValueIsUndefined(height)) { heightMeasureMode = YGMeasureModeExactly; - } else if (isStyleDimDefined(node, YGFlexDirectionColumn)) { + } else if (YGNodeIsStyleDimDefined(node, YGFlexDirectionColumn)) { height = node->style.dimensions[dim[YGFlexDirectionColumn]] + - getMarginAxis(node, YGFlexDirectionColumn); + YGNodeMarginForAxis(node, YGFlexDirectionColumn); heightMeasureMode = YGMeasureModeExactly; } else if (node->style.maxDimensions[YGDimensionHeight] >= 0.0) { height = node->style.maxDimensions[YGDimensionHeight]; heightMeasureMode = YGMeasureModeAtMost; } - if (layoutNodeInternal(node, - width, - height, - parentDirection, - widthMeasureMode, - heightMeasureMode, - true, - "initia" - "l")) { - setPosition(node, node->layout.direction); + if (YGLayoutNodeInternal(node, + width, + height, + parentDirection, + widthMeasureMode, + heightMeasureMode, + true, + "initia" + "l")) { + YGNodeSetPosition(node, node->layout.direction); - if (CSSLayoutIsExperimentalFeatureEnabled(YGExperimentalFeatureRounding)) { + if (YGIsExperimentalFeatureEnabled(YGExperimentalFeatureRounding)) { roundToPixelGrid(node); } if (gPrintTree) { - CSSNodePrint(node, YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle); + YGNodePrint(node, YGPrintOptionsLayout | YGPrintOptionsChildren | YGPrintOptionsStyle); } } } -void CSSLayoutSetLogger(CSSLogger logger) { +void YGSetLogger(YGLogger logger) { gLogger = logger; } -void CSSLog(YGLogLevel level, const char *format, ...) { +void YGLog(YGLogLevel level, const char *format, ...) { va_list args; va_start(args, format); gLogger(level, format, args); @@ -2615,32 +2645,29 @@ void CSSLog(YGLogLevel level, const char *format, ...) { static bool experimentalFeatures[YGExperimentalFeatureCount + 1]; -void CSSLayoutSetExperimentalFeatureEnabled(YGExperimentalFeature feature, bool enabled) { +void YGSetExperimentalFeatureEnabled(YGExperimentalFeature feature, bool enabled) { experimentalFeatures[feature] = enabled; } -inline bool CSSLayoutIsExperimentalFeatureEnabled(YGExperimentalFeature feature) { +inline bool YGIsExperimentalFeatureEnabled(YGExperimentalFeature feature) { return experimentalFeatures[feature]; } -void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc, - CSSCalloc cssCalloc, - CSSRealloc cssRealloc, - CSSFree cssFree) { - CSS_ASSERT(gNodeInstanceCount == 0, "Cannot set memory functions: all node must be freed first"); - CSS_ASSERT((cssMalloc == NULL && cssCalloc == NULL && cssRealloc == NULL && cssFree == NULL) || - (cssMalloc != NULL && cssCalloc != NULL && cssRealloc != NULL && cssFree != NULL), - "Cannot set memory functions: functions must be all NULL or Non-NULL"); +void YGSetMemoryFuncs(YGMalloc YGMalloc, YGCalloc YGCalloc, YGRealloc YGRealloc, YGFree YGFree) { + YG_ASSERT(gNodeInstanceCount == 0, "Cannot set memory functions: all node must be freed first"); + YG_ASSERT((YGMalloc == NULL && YGCalloc == NULL && YGRealloc == NULL && YGFree == NULL) || + (YGMalloc != NULL && YGCalloc != NULL && YGRealloc != NULL && YGFree != NULL), + "Cannot set memory functions: functions must be all NULL or Non-NULL"); - if (cssMalloc == NULL || cssCalloc == NULL || cssRealloc == NULL || cssFree == NULL) { - gCSSMalloc = &malloc; - gCSSCalloc = &calloc; - gCSSRealloc = &realloc; - gCSSFree = &free; + if (YGMalloc == NULL || YGCalloc == NULL || YGRealloc == NULL || YGFree == NULL) { + gYGMalloc = &malloc; + gYGCalloc = &calloc; + gYGRealloc = &realloc; + gYGFree = &free; } else { - gCSSMalloc = cssMalloc; - gCSSCalloc = cssCalloc; - gCSSRealloc = cssRealloc; - gCSSFree = cssFree; + gYGMalloc = YGMalloc; + gYGCalloc = YGCalloc; + gYGRealloc = YGRealloc; + gYGFree = YGFree; } } diff --git a/ReactCommon/CSSLayout/CSSLayout/Yoga.h b/ReactCommon/CSSLayout/CSSLayout/Yoga.h new file mode 100644 index 000000000..926a3b943 --- /dev/null +++ b/ReactCommon/CSSLayout/CSSLayout/Yoga.h @@ -0,0 +1,181 @@ +/** + * 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. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#ifndef __cplusplus +#include +#endif + +// Not defined in MSVC++ +#ifndef NAN +static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff}; +#define NAN (*(const float *) __nan) +#endif + +#define YGUndefined NAN + +#include "YGEnums.h" +#include "YGMacros.h" + +YG_EXTERN_C_BEGIN + +typedef struct YGSize { + float width; + float height; +} YGSize; + +typedef struct YGNode *YGNodeRef; +typedef YGSize (*YGMeasureFunc)(YGNodeRef node, + float width, + YGMeasureMode widthMode, + float height, + YGMeasureMode heightMode); +typedef void (*YGPrintFunc)(YGNodeRef node); +typedef int (*YGLogger)(YGLogLevel level, const char *format, va_list args); + +typedef void *(*YGMalloc)(size_t size); +typedef void *(*YGCalloc)(size_t count, size_t size); +typedef void *(*YGRealloc)(void *ptr, size_t size); +typedef void (*YGFree)(void *ptr); + +// YGNode +WIN_EXPORT YGNodeRef YGNodeNew(void); +WIN_EXPORT void YGNodeInit(const YGNodeRef node); +WIN_EXPORT void YGNodeFree(const YGNodeRef node); +WIN_EXPORT void YGNodeFreeRecursive(const YGNodeRef node); +WIN_EXPORT void YGNodeReset(const YGNodeRef node); +WIN_EXPORT int32_t YGNodeGetInstanceCount(void); + +WIN_EXPORT void YGNodeInsertChild(const YGNodeRef node, + const YGNodeRef child, + const uint32_t index); +WIN_EXPORT void YGNodeRemoveChild(const YGNodeRef node, const YGNodeRef child); +WIN_EXPORT YGNodeRef YGNodeGetChild(const YGNodeRef node, const uint32_t index); +WIN_EXPORT uint32_t YGNodeChildCount(const YGNodeRef node); + +WIN_EXPORT void YGNodeCalculateLayout(const YGNodeRef node, + const float availableWidth, + const float availableHeight, + const YGDirection parentDirection); + +// Mark a node as dirty. Only valid for nodes with a custom measure function +// set. +// YG knows when to mark all other nodes as dirty but because nodes with +// measure functions +// depends on information not known to YG they must perform this dirty +// marking manually. +WIN_EXPORT void YGNodeMarkDirty(const YGNodeRef node); +WIN_EXPORT bool YGNodeIsDirty(const YGNodeRef node); + +WIN_EXPORT void YGNodePrint(const YGNodeRef node, const YGPrintOptions options); + +WIN_EXPORT bool YGValueIsUndefined(const float value); + +WIN_EXPORT bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode, + const float width, + const YGMeasureMode heightMode, + const float height, + const YGMeasureMode lastWidthMode, + const float lastWidth, + const YGMeasureMode lastHeightMode, + const float lastHeight, + const float lastComputedWidth, + const float lastComputedHeight, + const float marginRow, + const float marginColumn); + +WIN_EXPORT void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode); + +#define YG_NODE_PROPERTY(type, name, paramName) \ + WIN_EXPORT void YGNodeSet##name(const YGNodeRef node, type paramName); \ + WIN_EXPORT type YGNodeGet##name(const YGNodeRef node); + +#define YG_NODE_STYLE_PROPERTY(type, name, paramName) \ + WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node, const type paramName); \ + WIN_EXPORT type YGNodeStyleGet##name(const YGNodeRef node); + +#define YG_NODE_STYLE_EDGE_PROPERTY(type, name, paramName) \ + WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node, \ + const YGEdge edge, \ + const type paramName); \ + WIN_EXPORT type YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge); + +#define YG_NODE_LAYOUT_PROPERTY(type, name) \ + WIN_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node); + +YG_NODE_PROPERTY(void *, Context, context); +YG_NODE_PROPERTY(YGMeasureFunc, MeasureFunc, measureFunc); +YG_NODE_PROPERTY(YGPrintFunc, PrintFunc, printFunc); +YG_NODE_PROPERTY(bool, HasNewLayout, hasNewLayout); + +YG_NODE_STYLE_PROPERTY(YGDirection, Direction, direction); +YG_NODE_STYLE_PROPERTY(YGFlexDirection, FlexDirection, flexDirection); +YG_NODE_STYLE_PROPERTY(YGJustify, JustifyContent, justifyContent); +YG_NODE_STYLE_PROPERTY(YGAlign, AlignContent, alignContent); +YG_NODE_STYLE_PROPERTY(YGAlign, AlignItems, alignItems); +YG_NODE_STYLE_PROPERTY(YGAlign, AlignSelf, alignSelf); +YG_NODE_STYLE_PROPERTY(YGPositionType, PositionType, positionType); +YG_NODE_STYLE_PROPERTY(YGWrap, FlexWrap, flexWrap); +YG_NODE_STYLE_PROPERTY(YGOverflow, Overflow, overflow); + +WIN_EXPORT void YGNodeStyleSetFlex(const YGNodeRef node, const float flex); +YG_NODE_STYLE_PROPERTY(float, FlexGrow, flexGrow); +YG_NODE_STYLE_PROPERTY(float, FlexShrink, flexShrink); +YG_NODE_STYLE_PROPERTY(float, FlexBasis, flexBasis); + +YG_NODE_STYLE_EDGE_PROPERTY(float, Position, position); +YG_NODE_STYLE_EDGE_PROPERTY(float, Margin, margin); +YG_NODE_STYLE_EDGE_PROPERTY(float, Padding, padding); +YG_NODE_STYLE_EDGE_PROPERTY(float, Border, border); + +YG_NODE_STYLE_PROPERTY(float, Width, width); +YG_NODE_STYLE_PROPERTY(float, Height, height); +YG_NODE_STYLE_PROPERTY(float, MinWidth, minWidth); +YG_NODE_STYLE_PROPERTY(float, MinHeight, minHeight); +YG_NODE_STYLE_PROPERTY(float, MaxWidth, maxWidth); +YG_NODE_STYLE_PROPERTY(float, MaxHeight, maxHeight); + +// Yoga specific properties, not compatible with flexbox specification +// Aspect ratio control the size of the undefined dimension of a node. +// - On a node with a set width/height aspect ratio control the size of the unset dimension +// - On a node with a set flex basis aspect ratio controls the size of the node in the cross axis if +// unset +// - On a node with a measure function aspect ratio works as though the measure function measures +// the flex basis +// - On a node with flex grow/shrink aspect ratio controls the size of the node in the cross axis if +// unset +// - Aspect ratio takes min/max dimensions into account +YG_NODE_STYLE_PROPERTY(float, AspectRatio, aspectRatio); + +YG_NODE_LAYOUT_PROPERTY(float, Left); +YG_NODE_LAYOUT_PROPERTY(float, Top); +YG_NODE_LAYOUT_PROPERTY(float, Right); +YG_NODE_LAYOUT_PROPERTY(float, Bottom); +YG_NODE_LAYOUT_PROPERTY(float, Width); +YG_NODE_LAYOUT_PROPERTY(float, Height); +YG_NODE_LAYOUT_PROPERTY(YGDirection, Direction); + +WIN_EXPORT void YGSetLogger(YGLogger logger); +WIN_EXPORT void YGLog(YGLogLevel level, const char *message, ...); + +WIN_EXPORT void YGSetExperimentalFeatureEnabled(YGExperimentalFeature feature, bool enabled); +WIN_EXPORT bool YGIsExperimentalFeatureEnabled(YGExperimentalFeature feature); + +WIN_EXPORT void +YGSetMemoryFuncs(YGMalloc cssMalloc, YGCalloc cssCalloc, YGRealloc cssRealloc, YGFree cssFree); + +YG_EXTERN_C_END