Move YGLogger into YGConfig and associate YGNodeRef with log events

Summary:
Moves the `YGLogger` into `YGConfig` and pass the `YGNodeRef` into the logger to be able to associate the log messages and assertions with the specific node.

Tackles facebook/yoga#530 and facebook/yoga#446
Closes https://github.com/facebook/yoga/pull/531

Reviewed By: astreet

Differential Revision: D4970149

Pulled By: emilsjolander

fbshipit-source-id: b7fcdaa273143ea2fa35861620b2e4d79f04f0af
This commit is contained in:
Lukas Wöhrl
2017-05-03 09:22:35 -07:00
committed by Facebook Github Bot
parent 40eba60cf5
commit 91230ae177
36 changed files with 863 additions and 606 deletions

View File

@@ -9,8 +9,8 @@
#include "YGEnums.h"
const char *YGAlignToString(const YGAlign value){
switch(value){
const char *YGAlignToString(const YGAlign value) {
switch (value) {
case YGAlignAuto:
return "auto";
case YGAlignFlexStart:
@@ -31,8 +31,8 @@ const char *YGAlignToString(const YGAlign value){
return "unknown";
}
const char *YGDimensionToString(const YGDimension value){
switch(value){
const char *YGDimensionToString(const YGDimension value) {
switch (value) {
case YGDimensionWidth:
return "width";
case YGDimensionHeight:
@@ -41,8 +41,8 @@ const char *YGDimensionToString(const YGDimension value){
return "unknown";
}
const char *YGDirectionToString(const YGDirection value){
switch(value){
const char *YGDirectionToString(const YGDirection value) {
switch (value) {
case YGDirectionInherit:
return "inherit";
case YGDirectionLTR:
@@ -53,8 +53,8 @@ const char *YGDirectionToString(const YGDirection value){
return "unknown";
}
const char *YGDisplayToString(const YGDisplay value){
switch(value){
const char *YGDisplayToString(const YGDisplay value) {
switch (value) {
case YGDisplayFlex:
return "flex";
case YGDisplayNone:
@@ -63,8 +63,8 @@ const char *YGDisplayToString(const YGDisplay value){
return "unknown";
}
const char *YGEdgeToString(const YGEdge value){
switch(value){
const char *YGEdgeToString(const YGEdge value) {
switch (value) {
case YGEdgeLeft:
return "left";
case YGEdgeTop:
@@ -87,16 +87,16 @@ const char *YGEdgeToString(const YGEdge value){
return "unknown";
}
const char *YGExperimentalFeatureToString(const YGExperimentalFeature value){
switch(value){
const char *YGExperimentalFeatureToString(const YGExperimentalFeature value) {
switch (value) {
case YGExperimentalFeatureWebFlexBasis:
return "web-flex-basis";
}
return "unknown";
}
const char *YGFlexDirectionToString(const YGFlexDirection value){
switch(value){
const char *YGFlexDirectionToString(const YGFlexDirection value) {
switch (value) {
case YGFlexDirectionColumn:
return "column";
case YGFlexDirectionColumnReverse:
@@ -109,8 +109,8 @@ const char *YGFlexDirectionToString(const YGFlexDirection value){
return "unknown";
}
const char *YGJustifyToString(const YGJustify value){
switch(value){
const char *YGJustifyToString(const YGJustify value) {
switch (value) {
case YGJustifyFlexStart:
return "flex-start";
case YGJustifyCenter:
@@ -125,8 +125,8 @@ const char *YGJustifyToString(const YGJustify value){
return "unknown";
}
const char *YGLogLevelToString(const YGLogLevel value){
switch(value){
const char *YGLogLevelToString(const YGLogLevel value) {
switch (value) {
case YGLogLevelError:
return "error";
case YGLogLevelWarn:
@@ -137,12 +137,14 @@ const char *YGLogLevelToString(const YGLogLevel value){
return "debug";
case YGLogLevelVerbose:
return "verbose";
case YGLogLevelFatal:
return "fatal";
}
return "unknown";
}
const char *YGMeasureModeToString(const YGMeasureMode value){
switch(value){
const char *YGMeasureModeToString(const YGMeasureMode value) {
switch (value) {
case YGMeasureModeUndefined:
return "undefined";
case YGMeasureModeExactly:
@@ -153,8 +155,8 @@ const char *YGMeasureModeToString(const YGMeasureMode value){
return "unknown";
}
const char *YGOverflowToString(const YGOverflow value){
switch(value){
const char *YGOverflowToString(const YGOverflow value) {
switch (value) {
case YGOverflowVisible:
return "visible";
case YGOverflowHidden:
@@ -165,8 +167,8 @@ const char *YGOverflowToString(const YGOverflow value){
return "unknown";
}
const char *YGPositionTypeToString(const YGPositionType value){
switch(value){
const char *YGPositionTypeToString(const YGPositionType value) {
switch (value) {
case YGPositionTypeRelative:
return "relative";
case YGPositionTypeAbsolute:
@@ -175,8 +177,8 @@ const char *YGPositionTypeToString(const YGPositionType value){
return "unknown";
}
const char *YGPrintOptionsToString(const YGPrintOptions value){
switch(value){
const char *YGPrintOptionsToString(const YGPrintOptions value) {
switch (value) {
case YGPrintOptionsLayout:
return "layout";
case YGPrintOptionsStyle:
@@ -187,8 +189,8 @@ const char *YGPrintOptionsToString(const YGPrintOptions value){
return "unknown";
}
const char *YGUnitToString(const YGUnit value){
switch(value){
const char *YGUnitToString(const YGUnit value) {
switch (value) {
case YGUnitUndefined:
return "undefined";
case YGUnitPoint:
@@ -201,8 +203,8 @@ const char *YGUnitToString(const YGUnit value){
return "unknown";
}
const char *YGWrapToString(const YGWrap value){
switch(value){
const char *YGWrapToString(const YGWrap value) {
switch (value) {
case YGWrapNoWrap:
return "no-wrap";
case YGWrapWrap:
@@ -212,4 +214,3 @@ const char *YGWrapToString(const YGWrap value){
}
return "unknown";
}

View File

@@ -87,13 +87,14 @@ typedef YG_ENUM_BEGIN(YGJustify) {
} YG_ENUM_END(YGJustify);
WIN_EXPORT const char *YGJustifyToString(const YGJustify value);
#define YGLogLevelCount 5
#define YGLogLevelCount 6
typedef YG_ENUM_BEGIN(YGLogLevel) {
YGLogLevelError,
YGLogLevelWarn,
YGLogLevelInfo,
YGLogLevelDebug,
YGLogLevelVerbose,
YGLogLevelFatal,
} YG_ENUM_END(YGLogLevel);
WIN_EXPORT const char *YGLogLevelToString(const YGLogLevel value);

View File

@@ -24,7 +24,7 @@
#endif
#ifdef WINARMDLL
#define WIN_STRUCT(type) type*
#define WIN_STRUCT(type) type *
#define WIN_STRUCT_REF(value) &value
#else
#define WIN_STRUCT(type) type
@@ -35,20 +35,6 @@
#define FB_ASSERTIONS_ENABLED 1
#endif
#if FB_ASSERTIONS_ENABLED
#define YG_ABORT() abort()
#else
#define YG_ABORT()
#endif
#ifndef YG_ASSERT
#define YG_ASSERT(X, message) \
if (!(X)) { \
YGLog(YGLogLevelError, "%s", message); \
YG_ABORT(); \
}
#endif
#ifdef NS_ENUM
// Cannot use NSInteger as NSInteger has a different size than int (which is the default type of a
// enum).

View File

@@ -21,12 +21,12 @@ struct YGNodeList {
YGNodeListRef YGNodeListNew(const uint32_t initialCapacity) {
const YGNodeListRef list = gYGMalloc(sizeof(struct YGNodeList));
YG_ASSERT(list != NULL, "Could not allocate memory for list");
YGAssert(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");
YGAssert(list->items != NULL, "Could not allocate memory for items");
return list;
}
@@ -61,7 +61,7 @@ void YGNodeListInsert(YGNodeListRef *listp, const YGNodeRef node, const uint32_t
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");
YGAssert(list->items != NULL, "Could not extend allocation for items");
}
for (uint32_t i = list->count; i > index; i--) {

View File

@@ -99,6 +99,8 @@ typedef struct YGConfig {
bool useWebDefaults;
bool useLegacyStretchBehaviour;
float pointScaleFactor;
YGLogger logger;
void *context;
} YGConfig;
typedef struct YGNode {
@@ -200,13 +202,33 @@ static YGNode gYGNodeDefaults = {
},
};
#ifdef ANDROID
static int YGAndroidLog(const YGConfigRef config,
const YGNodeRef node,
YGLogLevel level,
const char *format,
va_list args);
#else
static int YGDefaultLog(const YGConfigRef config,
const YGNodeRef node,
YGLogLevel level,
const char *format,
va_list args);
#endif
static YGConfig gYGConfigDefaults = {
.experimentalFeatures =
{
[YGExperimentalFeatureWebFlexBasis] = false,
},
.useWebDefaults = false,
.pointScaleFactor = 1.0f
.pointScaleFactor = 1.0f,
#ifdef ANDROID
.logger = &YGAndroidLog,
#else
.logger = &YGDefaultLog,
#endif
.context = NULL,
};
static void YGNodeMarkDirtyInternal(const YGNodeRef node);
@@ -220,9 +242,16 @@ static YGValue YGValueZero = {.value = 0, .unit = YGUnitPoint};
#ifdef ANDROID
#include <android/log.h>
static int YGAndroidLog(YGLogLevel level, const char *format, va_list args) {
static int YGAndroidLog(const YGConfigRef config,
const YGNodeRef node,
YGLogLevel level,
const char *format,
va_list args) {
int androidLevel = YGLogLevelDebug;
switch (level) {
case YGLogLevelFatal:
androidLevel = ANDROID_LOG_FATAL;
break;
case YGLogLevelError:
androidLevel = ANDROID_LOG_ERROR;
break;
@@ -239,14 +268,18 @@ static int YGAndroidLog(YGLogLevel level, const char *format, va_list args) {
androidLevel = ANDROID_LOG_VERBOSE;
break;
}
const int result = __android_log_vprint(androidLevel, "YG-layout", format, args);
const int result = __android_log_vprint(androidLevel, "yoga", format, args);
return result;
}
static YGLogger gLogger = &YGAndroidLog;
#else
static int YGDefaultLog(YGLogLevel level, const char *format, va_list args) {
static int YGDefaultLog(const YGConfigRef config,
const YGNodeRef node,
YGLogLevel level,
const char *format,
va_list args) {
switch (level) {
case YGLogLevelError:
case YGLogLevelFatal:
return vfprintf(stderr, format, args);
case YGLogLevelWarn:
case YGLogLevelInfo:
@@ -256,14 +289,11 @@ static int YGDefaultLog(YGLogLevel level, const char *format, va_list args) {
return vprintf(format, args);
}
}
static YGLogger gLogger = &YGDefaultLog;
#endif
static inline const YGValue *YGComputedEdgeValue(const YGValue edges[YGEdgeCount],
const YGEdge edge,
const YGValue *const defaultValue) {
YG_ASSERT(edge <= YGEdgeEnd, "Cannot get computed value of multi-edge shorthands");
if (edges[edge].unit != YGUnitUndefined) {
return &edges[edge];
}
@@ -311,7 +341,7 @@ int32_t gConfigInstanceCount = 0;
WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) {
const YGNodeRef node = gYGMalloc(sizeof(YGNode));
YG_ASSERT(node, "Could not allocate memory for node");
YGAssertWithConfig(config, node != NULL, "Could not allocate memory for node");
gNodeInstanceCount++;
memcpy(node, &gYGNodeDefaults, sizeof(YGNode));
@@ -354,9 +384,10 @@ void YGNodeFreeRecursive(const YGNodeRef root) {
}
void YGNodeReset(const YGNodeRef node) {
YG_ASSERT(YGNodeGetChildCount(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");
YGAssertWithNode(node,
YGNodeGetChildCount(node) == 0,
"Cannot reset a node which still has children attached");
YGAssertWithNode(node, node->parent == NULL, "Cannot reset a node still attached to a parent");
YGNodeListFree(node->children);
@@ -377,9 +408,15 @@ int32_t YGConfigGetInstanceCount(void) {
return gConfigInstanceCount;
}
// Export only for C#
YGConfigRef YGConfigGetDefault() {
return &gYGConfigDefaults;
}
YGConfigRef YGConfigNew(void) {
const YGConfigRef config = gYGMalloc(sizeof(YGConfig));
YG_ASSERT(config, "Could not allocate memory for config");
YGLog(NULL, config != NULL, "Could not allocate memory for config");
gConfigInstanceCount++;
memcpy(config, &gYGConfigDefaults, sizeof(YGConfig));
return config;
@@ -408,8 +445,9 @@ void YGNodeSetMeasureFunc(const YGNodeRef node, YGMeasureFunc measureFunc) {
if (measureFunc == NULL) {
node->measure = NULL;
} else {
YG_ASSERT(YGNodeGetChildCount(node) == 0,
"Cannot set measure function: Nodes with measure functions cannot have children.");
YGAssertWithNode(node,
YGNodeGetChildCount(node) == 0,
"Cannot set measure function: Nodes with measure functions cannot have children.");
node->measure = measureFunc;
}
}
@@ -427,9 +465,11 @@ YGBaselineFunc YGNodeGetBaselineFunc(const YGNodeRef node) {
}
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.");
YGAssertWithNode(node, child->parent == NULL, "Child already has a parent, it must be removed first.");
YGAssertWithNode(node,
node->measure == NULL,
"Cannot add child: Nodes with measure functions cannot have children.");
YGNodeListInsert(&node->children, child, index);
child->parent = node;
YGNodeMarkDirtyInternal(node);
@@ -456,9 +496,11 @@ inline uint32_t YGNodeGetChildCount(const YGNodeRef node) {
}
void YGNodeMarkDirty(const YGNodeRef node) {
YG_ASSERT(node->measure != NULL,
"Only leaf nodes with custom measure functions"
"should manually mark themselves as dirty");
YGAssertWithNode(node,
node->measure != NULL,
"Only leaf nodes with custom measure functions"
"should manually mark themselves as dirty");
YGNodeMarkDirtyInternal(node);
}
@@ -488,14 +530,17 @@ float YGNodeStyleGetFlexGrow(const YGNodeRef node) {
}
float YGNodeStyleGetFlexShrink(const YGNodeRef node) {
return YGFloatIsUndefined(node->style.flexShrink) ? (node->config->useWebDefaults ? kWebDefaultFlexShrink : kDefaultFlexShrink) : node->style.flexShrink;
return YGFloatIsUndefined(node->style.flexShrink)
? (node->config->useWebDefaults ? kWebDefaultFlexShrink : kDefaultFlexShrink)
: node->style.flexShrink;
}
static inline float YGNodeResolveFlexShrink(const YGNodeRef node) {
if (!YGFloatIsUndefined(node->style.flexShrink)) {
return node->style.flexShrink;
}
if (!node->config->useWebDefaults && !YGFloatIsUndefined(node->style.flex) && node->style.flex < 0.0f) {
if (!node->config->useWebDefaults && !YGFloatIsUndefined(node->style.flex) &&
node->style.flex < 0.0f) {
return -node->style.flex;
}
return node->config->useWebDefaults ? kWebDefaultFlexShrink : kDefaultFlexShrink;
@@ -528,25 +573,23 @@ static inline const YGValue *YGNodeResolveFlexBasisPtr(const YGNodeRef node) {
} \
}
#define YG_NODE_STYLE_PROPERTY_SETTER_UNIT_IMPL(type, name, paramName, instanceName) \
void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \
if (node->style.instanceName.value != paramName || \
node->style.instanceName.unit != YGUnitPoint) { \
node->style.instanceName.value = paramName; \
node->style.instanceName.unit = \
YGFloatIsUndefined(paramName) ? YGUnitAuto : YGUnitPoint; \
YGNodeMarkDirtyInternal(node); \
} \
} \
\
void YGNodeStyleSet##name##Percent(const YGNodeRef node, const type paramName) { \
if (node->style.instanceName.value != paramName || \
node->style.instanceName.unit != YGUnitPercent) { \
node->style.instanceName.value = paramName; \
node->style.instanceName.unit = \
YGFloatIsUndefined(paramName) ? YGUnitAuto : YGUnitPercent; \
YGNodeMarkDirtyInternal(node); \
} \
#define YG_NODE_STYLE_PROPERTY_SETTER_UNIT_IMPL(type, name, paramName, instanceName) \
void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \
if (node->style.instanceName.value != paramName || \
node->style.instanceName.unit != YGUnitPoint) { \
node->style.instanceName.value = paramName; \
node->style.instanceName.unit = YGFloatIsUndefined(paramName) ? YGUnitAuto : YGUnitPoint; \
YGNodeMarkDirtyInternal(node); \
} \
} \
\
void YGNodeStyleSet##name##Percent(const YGNodeRef node, const type paramName) { \
if (node->style.instanceName.value != paramName || \
node->style.instanceName.unit != YGUnitPercent) { \
node->style.instanceName.value = paramName; \
node->style.instanceName.unit = YGFloatIsUndefined(paramName) ? YGUnitAuto : YGUnitPercent; \
YGNodeMarkDirtyInternal(node); \
} \
}
#define YG_NODE_STYLE_PROPERTY_SETTER_UNIT_AUTO_IMPL(type, name, paramName, instanceName) \
@@ -653,27 +696,27 @@ static inline const YGValue *YGNodeResolveFlexBasisPtr(const YGNodeRef node) {
return node->layout.instanceName; \
}
#define YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(type, name, instanceName) \
type YGNodeLayoutGet##name(const YGNodeRef node, const YGEdge edge) { \
YG_ASSERT(edge <= YGEdgeEnd, "Cannot get layout properties of multi-edge shorthands"); \
\
if (edge == YGEdgeLeft) { \
if (node->layout.direction == YGDirectionRTL) { \
return node->layout.instanceName[YGEdgeEnd]; \
} else { \
return node->layout.instanceName[YGEdgeStart]; \
} \
} \
\
if (edge == YGEdgeRight) { \
if (node->layout.direction == YGDirectionRTL) { \
return node->layout.instanceName[YGEdgeStart]; \
} else { \
return node->layout.instanceName[YGEdgeEnd]; \
} \
} \
\
return node->layout.instanceName[edge]; \
#define YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(type, name, instanceName) \
type YGNodeLayoutGet##name(const YGNodeRef node, const YGEdge edge) { \
YGAssertWithNode(node, edge < YGEdgeEnd, "Cannot get layout properties of multi-edge shorthands"); \
\
if (edge == YGEdgeLeft) { \
if (node->layout.direction == YGDirectionRTL) { \
return node->layout.instanceName[YGEdgeEnd]; \
} else { \
return node->layout.instanceName[YGEdgeStart]; \
} \
} \
\
if (edge == YGEdgeRight) { \
if (node->layout.direction == YGDirectionRTL) { \
return node->layout.instanceName[YGEdgeStart]; \
} else { \
return node->layout.instanceName[YGEdgeEnd]; \
} \
} \
\
return node->layout.instanceName[edge]; \
}
YG_NODE_PROPERTY_IMPL(void *, Context, context, context);
@@ -772,42 +815,53 @@ static inline bool YGFloatsEqual(const float a, const float b) {
return fabs(a - b) < 0.0001f;
}
static void YGIndent(const uint32_t n) {
static void YGIndent(const YGNodeRef node, const uint32_t n) {
for (uint32_t i = 0; i < n; i++) {
YGLog(YGLogLevelDebug, " ");
YGLog(node, YGLogLevelDebug, " ");
}
}
static void YGPrintNumberIfNotUndefinedf(const char *str, const float number) {
static void YGPrintNumberIfNotUndefinedf(const YGNodeRef node,
const char *str,
const float number) {
if (!YGFloatIsUndefined(number)) {
YGLog(YGLogLevelDebug, "%s: %g; ", str, number);
YGLog(node, YGLogLevelDebug, "%s: %g; ", str, number);
}
}
static void YGPrintNumberIfNotUndefined(const char *str, const YGValue *const number) {
static void YGPrintNumberIfNotUndefined(const YGNodeRef node,
const char *str,
const YGValue *const number) {
if (number->unit != YGUnitUndefined) {
if (number->unit == YGUnitAuto) {
YGLog(YGLogLevelDebug, "%s: auto; ", str);
YGLog(node, YGLogLevelDebug, "%s: auto; ", str);
} else {
const char *unit = number->unit == YGUnitPoint ? "px" : "%";
YGLog(YGLogLevelDebug, "%s: %g%s; ", str, number->value, unit);
YGLog(node, YGLogLevelDebug, "%s: %g%s; ", str, number->value, unit);
}
}
}
static void YGPrintNumberIfNotAuto(const char *str, const YGValue *const number) {
static void YGPrintNumberIfNotAuto(const YGNodeRef node,
const char *str,
const YGValue *const number) {
if (number->unit != YGUnitAuto) {
YGPrintNumberIfNotUndefined(str, number);
YGPrintNumberIfNotUndefined(node, str, number);
}
}
static void YGPrintEdgeIfNotUndefined(const char *str, const YGValue *edges, const YGEdge edge) {
YGPrintNumberIfNotUndefined(str, YGComputedEdgeValue(edges, YGEdgeLeft, &YGValueUndefined));
static void YGPrintEdgeIfNotUndefined(const YGNodeRef node,
const char *str,
const YGValue *edges,
const YGEdge edge) {
YGPrintNumberIfNotUndefined(node, str, YGComputedEdgeValue(edges, YGEdgeLeft, &YGValueUndefined));
}
static void YGPrintNumberIfNotZero(const char *str, const YGValue *const number) {
static void YGPrintNumberIfNotZero(const YGNodeRef node,
const char *str,
const YGValue *const number) {
if (!YGFloatsEqual(number->value, 0)) {
YGPrintNumberIfNotUndefined(str, number);
YGPrintNumberIfNotUndefined(node, str, number);
}
}
@@ -816,14 +870,14 @@ static bool YGFourValuesEqual(const YGValue four[4]) {
YGValueEqual(four[0], four[3]);
}
static void YGPrintEdges(const char *str, const YGValue *edges) {
static void YGPrintEdges(const YGNodeRef node, const char *str, const YGValue *edges) {
if (YGFourValuesEqual(edges)) {
YGPrintNumberIfNotZero(str, &edges[YGEdgeLeft]);
YGPrintNumberIfNotZero(node, str, &edges[YGEdgeLeft]);
} else {
for (YGEdge edge = YGEdgeLeft; edge < YGEdgeCount; edge++) {
char buf[30];
snprintf(buf, sizeof(buf), "%s-%s", str, YGEdgeToString(edge));
YGPrintNumberIfNotZero(buf, &edges[edge]);
YGPrintNumberIfNotZero(node, buf, &edges[edge]);
}
}
}
@@ -831,98 +885,103 @@ static void YGPrintEdges(const char *str, const YGValue *edges) {
static void YGNodePrintInternal(const YGNodeRef node,
const YGPrintOptions options,
const uint32_t level) {
YGIndent(level);
YGLog(YGLogLevelDebug, "<div ");
YGIndent(node, level);
YGLog(node, YGLogLevelDebug, "<div ");
if (node->print) {
node->print(node);
}
if (options & YGPrintOptionsLayout) {
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, "\" ");
YGLog(node, YGLogLevelDebug, "layout=\"");
YGLog(node, YGLogLevelDebug, "width: %g; ", node->layout.dimensions[YGDimensionWidth]);
YGLog(node, YGLogLevelDebug, "height: %g; ", node->layout.dimensions[YGDimensionHeight]);
YGLog(node, YGLogLevelDebug, "top: %g; ", node->layout.position[YGEdgeTop]);
YGLog(node, YGLogLevelDebug, "left: %g;", node->layout.position[YGEdgeLeft]);
YGLog(node, YGLogLevelDebug, "\" ");
}
if (options & YGPrintOptionsStyle) {
YGLog(YGLogLevelDebug, "style=\"");
YGLog(node, YGLogLevelDebug, "style=\"");
if (node->style.flexDirection != gYGNodeDefaults.style.flexDirection) {
YGLog(YGLogLevelDebug,
YGLog(node,
YGLogLevelDebug,
"flex-direction: %s; ",
YGFlexDirectionToString(node->style.flexDirection));
}
if (node->style.justifyContent != gYGNodeDefaults.style.justifyContent) {
YGLog(YGLogLevelDebug,
YGLog(node,
YGLogLevelDebug,
"justify-content: %s; ",
YGJustifyToString(node->style.justifyContent));
}
if (node->style.alignItems != gYGNodeDefaults.style.alignItems) {
YGLog(YGLogLevelDebug, "align-items: %s; ", YGAlignToString(node->style.alignItems));
YGLog(node, YGLogLevelDebug, "align-items: %s; ", YGAlignToString(node->style.alignItems));
}
if (node->style.alignContent != gYGNodeDefaults.style.alignContent) {
YGLog(YGLogLevelDebug, "align-content: %s; ", YGAlignToString(node->style.alignContent));
YGLog(node, YGLogLevelDebug, "align-content: %s; ", YGAlignToString(node->style.alignContent));
}
if (node->style.alignSelf != gYGNodeDefaults.style.alignSelf) {
YGLog(YGLogLevelDebug, "align-self: %s; ", YGAlignToString(node->style.alignSelf));
YGLog(node, YGLogLevelDebug, "align-self: %s; ", YGAlignToString(node->style.alignSelf));
}
YGPrintNumberIfNotUndefinedf("flex-grow", node->style.flexGrow);
YGPrintNumberIfNotUndefinedf("flex-shrink", node->style.flexShrink);
YGPrintNumberIfNotAuto("flex-basis", &node->style.flexBasis);
YGPrintNumberIfNotUndefinedf("flex", node->style.flex);
YGPrintNumberIfNotUndefinedf(node, "flex-grow", node->style.flexGrow);
YGPrintNumberIfNotUndefinedf(node, "flex-shrink", node->style.flexShrink);
YGPrintNumberIfNotAuto(node, "flex-basis", &node->style.flexBasis);
YGPrintNumberIfNotUndefinedf(node, "flex", node->style.flex);
if (node->style.flexWrap != gYGNodeDefaults.style.flexWrap) {
YGLog(YGLogLevelDebug, "flexWrap: %s; ", YGWrapToString(node->style.flexWrap));
YGLog(node, YGLogLevelDebug, "flexWrap: %s; ", YGWrapToString(node->style.flexWrap));
}
if (node->style.overflow != gYGNodeDefaults.style.overflow) {
YGLog(YGLogLevelDebug, "overflow: %s; ", YGOverflowToString(node->style.overflow));
YGLog(node, YGLogLevelDebug, "overflow: %s; ", YGOverflowToString(node->style.overflow));
}
if (node->style.display != gYGNodeDefaults.style.display) {
YGLog(YGLogLevelDebug, "display: %s; ", YGDisplayToString(node->style.display));
YGLog(node, YGLogLevelDebug, "display: %s; ", YGDisplayToString(node->style.display));
}
YGPrintEdges("margin", node->style.margin);
YGPrintEdges("padding", node->style.padding);
YGPrintEdges("border", node->style.border);
YGPrintEdges(node, "margin", node->style.margin);
YGPrintEdges(node, "padding", node->style.padding);
YGPrintEdges(node, "border", node->style.border);
YGPrintNumberIfNotAuto("width", &node->style.dimensions[YGDimensionWidth]);
YGPrintNumberIfNotAuto("height", &node->style.dimensions[YGDimensionHeight]);
YGPrintNumberIfNotAuto("max-width", &node->style.maxDimensions[YGDimensionWidth]);
YGPrintNumberIfNotAuto("max-height", &node->style.maxDimensions[YGDimensionHeight]);
YGPrintNumberIfNotAuto("min-width", &node->style.minDimensions[YGDimensionWidth]);
YGPrintNumberIfNotAuto("min-height", &node->style.minDimensions[YGDimensionHeight]);
YGPrintNumberIfNotAuto(node, "width", &node->style.dimensions[YGDimensionWidth]);
YGPrintNumberIfNotAuto(node, "height", &node->style.dimensions[YGDimensionHeight]);
YGPrintNumberIfNotAuto(node, "max-width", &node->style.maxDimensions[YGDimensionWidth]);
YGPrintNumberIfNotAuto(node, "max-height", &node->style.maxDimensions[YGDimensionHeight]);
YGPrintNumberIfNotAuto(node, "min-width", &node->style.minDimensions[YGDimensionWidth]);
YGPrintNumberIfNotAuto(node, "min-height", &node->style.minDimensions[YGDimensionHeight]);
if (node->style.positionType != gYGNodeDefaults.style.positionType) {
YGLog(YGLogLevelDebug, "position: %s; ", YGPositionTypeToString(node->style.positionType));
YGLog(node,
YGLogLevelDebug,
"position: %s; ",
YGPositionTypeToString(node->style.positionType));
}
YGPrintEdgeIfNotUndefined("left", node->style.position, YGEdgeLeft);
YGPrintEdgeIfNotUndefined("right", node->style.position, YGEdgeRight);
YGPrintEdgeIfNotUndefined("top", node->style.position, YGEdgeTop);
YGPrintEdgeIfNotUndefined("bottom", node->style.position, YGEdgeBottom);
YGLog(YGLogLevelDebug, "\" ");
YGPrintEdgeIfNotUndefined(node, "left", node->style.position, YGEdgeLeft);
YGPrintEdgeIfNotUndefined(node, "right", node->style.position, YGEdgeRight);
YGPrintEdgeIfNotUndefined(node, "top", node->style.position, YGEdgeTop);
YGPrintEdgeIfNotUndefined(node, "bottom", node->style.position, YGEdgeBottom);
YGLog(node, YGLogLevelDebug, "\" ");
if (node->measure != NULL) {
YGLog(YGLogLevelDebug, "has-custom-measure=\"true\"");
YGLog(node, YGLogLevelDebug, "has-custom-measure=\"true\"");
}
}
YGLog(YGLogLevelDebug, ">");
YGLog(node, YGLogLevelDebug, ">");
const uint32_t childCount = YGNodeListCount(node->children);
if (options & YGPrintOptionsChildren && childCount > 0) {
for (uint32_t i = 0; i < childCount; i++) {
YGLog(YGLogLevelDebug, "\n");
YGLog(node, YGLogLevelDebug, "\n");
YGNodePrintInternal(YGNodeGetChild(node, i), options, level + 1);
}
YGIndent(level);
YGLog(YGLogLevelDebug, "\n");
YGIndent(node, level);
YGLog(node, YGLogLevelDebug, "\n");
}
YGLog(YGLogLevelDebug, "</div>");
YGLog(node, YGLogLevelDebug, "</div>");
}
void YGNodePrint(const YGNodeRef node, const YGPrintOptions options) {
@@ -1076,7 +1135,9 @@ static float YGBaseline(const YGNodeRef node) {
const float baseline = node->baseline(node,
node->layout.measuredDimensions[YGDimensionWidth],
node->layout.measuredDimensions[YGDimensionHeight]);
YG_ASSERT(!YGFloatIsUndefined(baseline), "Expect custom baseline function to not return NaN")
YGAssertWithNode(node,
!YGFloatIsUndefined(baseline),
"Expect custom baseline function to not return NaN");
return baseline;
}
@@ -1459,8 +1520,12 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,
YGConstrainMaxSizeForMode(
child, YGFlexDirectionRow, parentWidth, parentWidth, &childWidthMeasureMode, &childWidth);
YGConstrainMaxSizeForMode(
child, YGFlexDirectionColumn, parentHeight, parentWidth, &childHeightMeasureMode, &childHeight);
YGConstrainMaxSizeForMode(child,
YGFlexDirectionColumn,
parentHeight,
parentWidth,
&childHeightMeasureMode,
&childHeight);
// Measure the child
YGLayoutNodeInternal(child,
@@ -1640,7 +1705,7 @@ static void YGNodeWithMeasureFuncSetMeasuredDimensions(const YGNodeRef node,
const YGMeasureMode heightMeasureMode,
const float parentWidth,
const float parentHeight) {
YG_ASSERT(node->measure, "Expected node to have custom measure function");
YGAssertWithNode(node, node->measure != NULL, "Expected node to have custom measure function");
const float paddingAndBorderAxisRow =
YGNodePaddingAndBorderForAxis(node, YGFlexDirectionRow, availableWidth);
@@ -1876,13 +1941,14 @@ static void YGNodelayoutImpl(const YGNodeRef node,
const float parentHeight,
const bool performLayout,
const YGConfigRef config) {
YG_ASSERT(YGFloatIsUndefined(availableWidth) ? widthMeasureMode == YGMeasureModeUndefined : true,
"availableWidth is indefinite so widthMeasureMode must be "
"YGMeasureModeUndefined");
YG_ASSERT(YGFloatIsUndefined(availableHeight) ? heightMeasureMode == YGMeasureModeUndefined
: true,
"availableHeight is indefinite so heightMeasureMode must be "
"YGMeasureModeUndefined");
YGAssertWithNode(node,
YGFloatIsUndefined(availableWidth) ? widthMeasureMode == YGMeasureModeUndefined : true,
"availableWidth is indefinite so widthMeasureMode must be "
"YGMeasureModeUndefined");
YGAssertWithNode(node,
YGFloatIsUndefined(availableHeight) ? heightMeasureMode == YGMeasureModeUndefined : true,
"availableHeight is indefinite so heightMeasureMode must be "
"YGMeasureModeUndefined");
// Set the resolved resolution in the node's layout.
const YGDirection direction = YGNodeResolveDirection(node, parentDirection);
@@ -2203,10 +2269,12 @@ static void YGNodelayoutImpl(const YGNodeRef node,
if (measureModeMainDim != YGMeasureModeExactly) {
if (!YGFloatIsUndefined(minInnerMainDim) && sizeConsumedOnCurrentLine < minInnerMainDim) {
availableInnerMainDim = minInnerMainDim;
} else if (!YGFloatIsUndefined(maxInnerMainDim) && sizeConsumedOnCurrentLine > maxInnerMainDim) {
} else if (!YGFloatIsUndefined(maxInnerMainDim) &&
sizeConsumedOnCurrentLine > maxInnerMainDim) {
availableInnerMainDim = maxInnerMainDim;
} else {
if (!node->config->useLegacyStretchBehaviour && (totalFlexGrowFactors == 0 || YGResolveFlexGrow(node) == 0)) {
if (!node->config->useLegacyStretchBehaviour &&
(totalFlexGrowFactors == 0 || YGResolveFlexGrow(node) == 0)) {
// If we don't have any children to flex or we can't flex the node itself,
// space we've used is all space we need
availableInnerMainDim = sizeConsumedOnCurrentLine;
@@ -2218,7 +2286,8 @@ static void YGNodelayoutImpl(const YGNodeRef node,
if (!YGFloatIsUndefined(availableInnerMainDim)) {
remainingFreeSpace = availableInnerMainDim - sizeConsumedOnCurrentLine;
} else if (sizeConsumedOnCurrentLine < 0) {
// availableInnerMainDim is indefinite which means the node is being sized based on its content.
// availableInnerMainDim is indefinite which means the node is being sized based on its
// content.
// sizeConsumedOnCurrentLine is negative which means the node will allocate 0 points for
// its content. Consequently, remainingFreeSpace is 0 - sizeConsumedOnCurrentLine.
remainingFreeSpace = -sizeConsumedOnCurrentLine;
@@ -3281,7 +3350,8 @@ bool YGLayoutNodeInternal(const YGNodeRef node,
}
void YGConfigSetPointScaleFactor(const YGConfigRef config, const float pixelsInPoint) {
YG_ASSERT(pixelsInPoint >= 0.0f, "Scale factor should not be less than zero");
YGAssertWithConfig(config, pixelsInPoint >= 0.0f, "Scale factor should not be less than zero");
// We store points for Pixel as we will use it for rounding
if (pixelsInPoint == 0.0f) {
// Zero is used to skip rounding
@@ -3291,7 +3361,10 @@ void YGConfigSetPointScaleFactor(const YGConfigRef config, const float pixelsInP
}
}
static float YGRoundValueToPixelGrid(const float value, const float pointScaleFactor, const bool forceCeil, const bool forceFloor) {
static float YGRoundValueToPixelGrid(const float value,
const float pointScaleFactor,
const bool forceCeil,
const bool forceFloor) {
float fractial = fmodf(value, pointScaleFactor);
if (YGFloatsEqual(fractial, 0)) {
// Still remove fractial as fractial could be extremely small.
@@ -3307,7 +3380,10 @@ static float YGRoundValueToPixelGrid(const float value, const float pointScaleFa
}
}
static void YGRoundToPixelGrid(const YGNodeRef node, const float pointScaleFactor, const float absoluteLeft, const float absoluteTop) {
static void YGRoundToPixelGrid(const YGNodeRef node,
const float pointScaleFactor,
const float absoluteLeft,
const float absoluteTop) {
if (pointScaleFactor == 0.0f) {
return;
}
@@ -3328,13 +3404,17 @@ static void YGRoundToPixelGrid(const YGNodeRef node, const float pointScaleFacto
// lead to unwanted text truncation.
const bool hasMeasure = node->measure != NULL;
node->layout.position[YGEdgeLeft] = YGRoundValueToPixelGrid(nodeLeft, pointScaleFactor, false, hasMeasure);
node->layout.position[YGEdgeTop] = YGRoundValueToPixelGrid(nodeTop, pointScaleFactor, false, hasMeasure);
node->layout.position[YGEdgeLeft] =
YGRoundValueToPixelGrid(nodeLeft, pointScaleFactor, false, hasMeasure);
node->layout.position[YGEdgeTop] =
YGRoundValueToPixelGrid(nodeTop, pointScaleFactor, false, hasMeasure);
node->layout.dimensions[YGDimensionWidth] =
YGRoundValueToPixelGrid(absoluteNodeRight, pointScaleFactor, hasMeasure, false) - YGRoundValueToPixelGrid(absoluteNodeLeft, pointScaleFactor, false, hasMeasure);
YGRoundValueToPixelGrid(absoluteNodeRight, pointScaleFactor, hasMeasure, false) -
YGRoundValueToPixelGrid(absoluteNodeLeft, pointScaleFactor, false, hasMeasure);
node->layout.dimensions[YGDimensionHeight] =
YGRoundValueToPixelGrid(absoluteNodeBottom, pointScaleFactor, hasMeasure, false) - YGRoundValueToPixelGrid(absoluteNodeTop, pointScaleFactor, false, hasMeasure);
YGRoundValueToPixelGrid(absoluteNodeBottom, pointScaleFactor, hasMeasure, false) -
YGRoundValueToPixelGrid(absoluteNodeTop, pointScaleFactor, false, hasMeasure);
const uint32_t childCount = YGNodeListCount(node->children);
for (uint32_t i = 0; i < childCount; i++) {
@@ -3375,8 +3455,7 @@ void YGNodeCalculateLayout(const YGNodeRef node,
height = YGResolveValue(node->resolvedDimensions[dim[YGFlexDirectionColumn]], parentHeight) +
YGNodeMarginForAxis(node, YGFlexDirectionColumn, parentWidth);
heightMeasureMode = YGMeasureModeExactly;
} else if (YGResolveValue(&node->style.maxDimensions[YGDimensionHeight], parentHeight) >=
0.0f) {
} else if (YGResolveValue(&node->style.maxDimensions[YGDimensionHeight], parentHeight) >= 0.0f) {
height = YGResolveValue(&node->style.maxDimensions[YGDimensionHeight], parentHeight);
heightMeasureMode = YGMeasureModeAtMost;
} else {
@@ -3404,33 +3483,71 @@ void YGNodeCalculateLayout(const YGNodeRef node,
}
}
void YGSetLogger(YGLogger logger) {
void YGConfigSetLogger(const YGConfigRef config, YGLogger logger) {
if (logger != NULL) {
gLogger = logger;
config->logger = logger;
} else {
#ifdef ANDROID
gLogger = &YGAndroidLog;
config->logger = &YGAndroidLog;
#else
gLogger = &YGDefaultLog;
config->logger = &YGDefaultLog;
#endif
}
}
void YGLog(YGLogLevel level, const char *format, ...) {
static void YGVLog(const YGConfigRef config,
const YGNodeRef node,
YGLogLevel level,
const char *format,
va_list args) {
const YGConfigRef logConfig = config != NULL ? config : &gYGConfigDefaults;
logConfig->logger(logConfig, node, level, format, args);
if (level == YGLogLevelFatal) {
abort();
}
}
void YGLogWithConfig(const YGConfigRef config, YGLogLevel level, const char *format, ...) {
va_list args;
va_start(args, format);
gLogger(level, format, args);
YGVLog(config, NULL, level, format, args);
va_end(args);
}
void YGLog(const YGNodeRef node, YGLogLevel level, const char *format, ...) {
va_list args;
va_start(args, format);
YGVLog(node == NULL ? NULL : node->config, node, level, format, args);
va_end(args);
}
void YGAssert(const bool condition, const char *message) {
if (!condition) {
YGLog(NULL, YGLogLevelFatal, "%s\n", message);
}
}
void YGAssertWithNode(const YGNodeRef node, const bool condition, const char *message) {
if (!condition) {
YGLog(node, YGLogLevelFatal, "%s\n", message);
}
}
void YGAssertWithConfig(const YGConfigRef config, const bool condition, const char *message) {
if (!condition) {
YGLogWithConfig(config, YGLogLevelFatal, "%s\n", message);
}
}
void YGConfigSetExperimentalFeatureEnabled(const YGConfigRef config,
const YGExperimentalFeature feature,
const bool enabled) {
const YGExperimentalFeature feature,
const bool enabled) {
config->experimentalFeatures[feature] = enabled;
}
inline bool YGConfigIsExperimentalFeatureEnabled(const YGConfigRef config,
const YGExperimentalFeature feature) {
const YGExperimentalFeature feature) {
return config->experimentalFeatures[feature];
}
@@ -3438,7 +3555,8 @@ void YGConfigSetUseWebDefaults(const YGConfigRef config, const bool enabled) {
config->useWebDefaults = enabled;
}
void YGConfigSetUseLegacyStretchBehaviour(const YGConfigRef config, const bool useLegacyStretchBehaviour) {
void YGConfigSetUseLegacyStretchBehaviour(const YGConfigRef config,
const bool useLegacyStretchBehaviour) {
config->useLegacyStretchBehaviour = useLegacyStretchBehaviour;
}
@@ -3446,12 +3564,20 @@ bool YGConfigGetUseWebDefaults(const YGConfigRef config) {
return config->useWebDefaults;
}
void YGConfigSetContext(const YGConfigRef config, void *context) {
config->context = context;
}
void *YGConfigGetContext(const YGConfigRef config) {
return config->context;
}
void YGSetMemoryFuncs(YGMalloc ygmalloc, YGCalloc yccalloc, YGRealloc ygrealloc, YGFree ygfree) {
YG_ASSERT(gNodeInstanceCount == 0 && gConfigInstanceCount == 0,
"Cannot set memory functions: all node must be freed first");
YG_ASSERT((ygmalloc == NULL && yccalloc == NULL && ygrealloc == NULL && ygfree == NULL) ||
(ygmalloc != NULL && yccalloc != NULL && ygrealloc != NULL && ygfree != NULL),
"Cannot set memory functions: functions must be all NULL or Non-NULL");
YGAssert(gNodeInstanceCount == 0 && gConfigInstanceCount == 0,
"Cannot set memory functions: all node must be freed first");
YGAssert((ygmalloc == NULL && yccalloc == NULL && ygrealloc == NULL && ygfree == NULL) ||
(ygmalloc != NULL && yccalloc != NULL && ygrealloc != NULL && ygfree != NULL),
"Cannot set memory functions: functions must be all NULL or Non-NULL");
if (ygmalloc == NULL || yccalloc == NULL || ygrealloc == NULL || ygfree == NULL) {
gYGMalloc = &malloc;

View File

@@ -55,7 +55,11 @@ typedef YGSize (*YGMeasureFunc)(YGNodeRef node,
YGMeasureMode heightMode);
typedef float (*YGBaselineFunc)(YGNodeRef node, const float width, const float height);
typedef void (*YGPrintFunc)(YGNodeRef node);
typedef int (*YGLogger)(YGLogLevel level, const char *format, va_list args);
typedef int (*YGLogger)(const YGConfigRef config,
const YGNodeRef node,
YGLogLevel level,
const char *format,
va_list args);
typedef void *(*YGMalloc)(size_t size);
typedef void *(*YGCalloc)(size_t count, size_t size);
@@ -218,17 +222,27 @@ YG_NODE_LAYOUT_EDGE_PROPERTY(float, Margin);
YG_NODE_LAYOUT_EDGE_PROPERTY(float, Border);
YG_NODE_LAYOUT_EDGE_PROPERTY(float, Padding);
WIN_EXPORT void YGSetLogger(YGLogger logger);
WIN_EXPORT void YGLog(YGLogLevel level, const char *message, ...);
WIN_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger);
WIN_EXPORT void YGLog(const YGNodeRef node, YGLogLevel level, const char *message, ...);
WIN_EXPORT void YGLogWithConfig(const YGConfigRef config, YGLogLevel level, const char *format, ...);
WIN_EXPORT void YGAssert(const bool condition, const char *message);
WIN_EXPORT void YGAssertWithNode(const YGNodeRef node, const bool condition, const char *message);
WIN_EXPORT void YGAssertWithConfig(const YGConfigRef config,
const bool condition,
const char *message);
// Set this to number of pixels in 1 point to round calculation results
// If you want to avoid rounding - set PointScaleFactor to 0
WIN_EXPORT void YGConfigSetPointScaleFactor(const YGConfigRef config, const float pixelsInPoint);
// Yoga previously had an error where containers would take the maximum space possible instead of the minimum
// like they are supposed to. In practice this resulted in implicit behaviour similar to align-self: stretch;
// Because this was such a long-standing bug we must allow legacy users to switch back to this behaviour.
WIN_EXPORT void YGConfigSetUseLegacyStretchBehaviour(const YGConfigRef config, const bool useLegacyStretchBehaviour);
// Yoga previously had an error where containers would take the maximum space possible instead of
// the minimum
// like they are supposed to. In practice this resulted in implicit behaviour similar to align-self:
// stretch;
// Because this was such a long-standing bug we must allow legacy users to switch back to this
// behaviour.
WIN_EXPORT void YGConfigSetUseLegacyStretchBehaviour(const YGConfigRef config,
const bool useLegacyStretchBehaviour);
// YGConfig
WIN_EXPORT YGConfigRef YGConfigNew(void);
@@ -245,9 +259,14 @@ WIN_EXPORT bool YGConfigIsExperimentalFeatureEnabled(const YGConfigRef config,
// Using the web defaults is the prefered configuration for new projects.
// Usage of non web defaults should be considered as legacy.
WIN_EXPORT void YGConfigSetUseWebDefaults(const YGConfigRef config, const bool enabled);
WIN_EXPORT bool YGConfigGetUseWebDefaults(const YGConfigRef config);
// Export only for C#
WIN_EXPORT YGConfigRef YGConfigGetDefault(void);
WIN_EXPORT void YGConfigSetContext(const YGConfigRef config, void *context);
WIN_EXPORT void *YGConfigGetContext(const YGConfigRef config);
WIN_EXPORT void
YGSetMemoryFuncs(YGMalloc ygmalloc, YGCalloc yccalloc, YGRealloc ygrealloc, YGFree ygfree);