|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +#include "YGNodePrint.h" |
| 11 | +#include <stdarg.h> |
| 12 | +#include "YGEnums.h" |
| 13 | +#include "Yoga-internal.h" |
| 14 | + |
| 15 | +namespace facebook { |
| 16 | +namespace yoga { |
| 17 | +typedef std::string string; |
| 18 | + |
| 19 | +static void indent(string* base, uint32_t level) { |
| 20 | + for (uint32_t i = 0; i < level; ++i) { |
| 21 | + base->append(" "); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +static bool areFourValuesEqual(const YGValue four[4]) { |
| 26 | + return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) && |
| 27 | + YGValueEqual(four[0], four[3]); |
| 28 | +} |
| 29 | + |
| 30 | +static void appendFormatedString(string* str, const char* fmt, ...) { |
| 31 | + char buffer[1024]; |
| 32 | + va_list args; |
| 33 | + va_start(args, fmt); |
| 34 | + va_list argsCopy; |
| 35 | + va_copy(argsCopy, args); |
| 36 | + va_end(args); |
| 37 | + vsnprintf(buffer, 1024, fmt, argsCopy); |
| 38 | + va_end(argsCopy); |
| 39 | + string result = string(buffer); |
| 40 | + str->append(result); |
| 41 | +} |
| 42 | + |
| 43 | +static void |
| 44 | +appendFloatIfNotUndefined(string* base, const string key, const float num) { |
| 45 | + if (!YGFloatIsUndefined(num)) { |
| 46 | + appendFormatedString(base, "%s: %g; ", key.c_str(), num); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +static void appendNumberIfNotUndefined( |
| 51 | + string* base, |
| 52 | + const string key, |
| 53 | + const YGValue* const number) { |
| 54 | + if (number->unit != YGUnitUndefined) { |
| 55 | + if (number->unit == YGUnitAuto) { |
| 56 | + base->append(key + ": auto; "); |
| 57 | + } else { |
| 58 | + string unit = number->unit == YGUnitPoint ? "px" : "%%"; |
| 59 | + appendFormatedString( |
| 60 | + base, "%s: %g%s; ", key.c_str(), number->value, unit.c_str()); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +static void appendNumberIfNotAuto( |
| 66 | + string* base, |
| 67 | + const string key, |
| 68 | + const YGValue* const number) { |
| 69 | + if (number->unit != YGUnitAuto) { |
| 70 | + appendNumberIfNotUndefined(base, key, number); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +static void appendNumberIfNotZero( |
| 75 | + string* base, |
| 76 | + const string str, |
| 77 | + const YGValue* const number) { |
| 78 | + if (!YGFloatsEqual(number->value, 0)) { |
| 79 | + appendNumberIfNotUndefined(base, str, number); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +static void appendEdges(string* base, const string key, const YGValue* edges) { |
| 84 | + if (areFourValuesEqual(edges)) { |
| 85 | + appendNumberIfNotZero(base, key, &edges[YGEdgeLeft]); |
| 86 | + } else { |
| 87 | + for (int edge = YGEdgeLeft; edge != YGEdgeAll; ++edge) { |
| 88 | + string str = key + "-" + YGEdgeToString(static_cast<YGEdge>(edge)); |
| 89 | + appendNumberIfNotZero(base, str, &edges[edge]); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +static void appendEdgeIfNotUndefined( |
| 95 | + string* base, |
| 96 | + const string str, |
| 97 | + const YGValue* edges, |
| 98 | + const YGEdge edge) { |
| 99 | + appendNumberIfNotUndefined( |
| 100 | + base, str, YGComputedEdgeValue(edges, edge, &YGValueUndefined)); |
| 101 | +} |
| 102 | + |
| 103 | +void YGNodeToString( |
| 104 | + std::string* str, |
| 105 | + YGNodeRef node, |
| 106 | + YGPrintOptions options, |
| 107 | + uint32_t level) { |
| 108 | + indent(str, level); |
| 109 | + appendFormatedString(str, "<div "); |
| 110 | + if (node->print != nullptr) { |
| 111 | + node->print(node); |
| 112 | + } |
| 113 | + |
| 114 | + if (options & YGPrintOptionsLayout) { |
| 115 | + appendFormatedString(str, "layout=\""); |
| 116 | + appendFormatedString( |
| 117 | + str, "width: %g; ", node->layout.dimensions[YGDimensionWidth]); |
| 118 | + appendFormatedString( |
| 119 | + str, "height: %g; ", node->layout.dimensions[YGDimensionHeight]); |
| 120 | + appendFormatedString(str, "top: %g; ", node->layout.position[YGEdgeTop]); |
| 121 | + appendFormatedString(str, "left: %g;", node->layout.position[YGEdgeLeft]); |
| 122 | + appendFormatedString(str, "\" "); |
| 123 | + } |
| 124 | + |
| 125 | + if (options & YGPrintOptionsStyle) { |
| 126 | + appendFormatedString(str, "style=\""); |
| 127 | + if (node->style.flexDirection != gYGNodeDefaults.style.flexDirection) { |
| 128 | + appendFormatedString( |
| 129 | + str, |
| 130 | + "flex-direction: %s; ", |
| 131 | + YGFlexDirectionToString(node->style.flexDirection)); |
| 132 | + } |
| 133 | + if (node->style.justifyContent != gYGNodeDefaults.style.justifyContent) { |
| 134 | + appendFormatedString( |
| 135 | + str, |
| 136 | + "justify-content: %s; ", |
| 137 | + YGJustifyToString(node->style.justifyContent)); |
| 138 | + } |
| 139 | + if (node->style.alignItems != gYGNodeDefaults.style.alignItems) { |
| 140 | + appendFormatedString( |
| 141 | + str, "align-items: %s; ", YGAlignToString(node->style.alignItems)); |
| 142 | + } |
| 143 | + if (node->style.alignContent != gYGNodeDefaults.style.alignContent) { |
| 144 | + appendFormatedString( |
| 145 | + str, |
| 146 | + "align-content: %s; ", |
| 147 | + YGAlignToString(node->style.alignContent)); |
| 148 | + } |
| 149 | + if (node->style.alignSelf != gYGNodeDefaults.style.alignSelf) { |
| 150 | + appendFormatedString( |
| 151 | + str, "align-self: %s; ", YGAlignToString(node->style.alignSelf)); |
| 152 | + } |
| 153 | + appendFloatIfNotUndefined(str, "flex-grow", node->style.flexGrow); |
| 154 | + appendFloatIfNotUndefined(str, "flex-shrink", node->style.flexShrink); |
| 155 | + appendNumberIfNotAuto(str, "flex-basis", &node->style.flexBasis); |
| 156 | + appendFloatIfNotUndefined(str, "flex", node->style.flex); |
| 157 | + |
| 158 | + if (node->style.flexWrap != gYGNodeDefaults.style.flexWrap) { |
| 159 | + appendFormatedString( |
| 160 | + str, "flexWrap: %s; ", YGWrapToString(node->style.flexWrap)); |
| 161 | + } |
| 162 | + |
| 163 | + if (node->style.overflow != gYGNodeDefaults.style.overflow) { |
| 164 | + appendFormatedString( |
| 165 | + str, "overflow: %s; ", YGOverflowToString(node->style.overflow)); |
| 166 | + } |
| 167 | + |
| 168 | + if (node->style.display != gYGNodeDefaults.style.display) { |
| 169 | + appendFormatedString( |
| 170 | + str, "display: %s; ", YGDisplayToString(node->style.display)); |
| 171 | + } |
| 172 | + appendEdges(str, "margin", node->style.margin); |
| 173 | + appendEdges(str, "padding", node->style.padding); |
| 174 | + appendEdges(str, "border", node->style.border); |
| 175 | + |
| 176 | + appendNumberIfNotAuto( |
| 177 | + str, "width", &node->style.dimensions[YGDimensionWidth]); |
| 178 | + appendNumberIfNotAuto( |
| 179 | + str, "height", &node->style.dimensions[YGDimensionHeight]); |
| 180 | + appendNumberIfNotAuto( |
| 181 | + str, "max-width", &node->style.maxDimensions[YGDimensionWidth]); |
| 182 | + appendNumberIfNotAuto( |
| 183 | + str, "max-height", &node->style.maxDimensions[YGDimensionHeight]); |
| 184 | + appendNumberIfNotAuto( |
| 185 | + str, "min-width", &node->style.minDimensions[YGDimensionWidth]); |
| 186 | + appendNumberIfNotAuto( |
| 187 | + str, "min-height", &node->style.minDimensions[YGDimensionHeight]); |
| 188 | + |
| 189 | + if (node->style.positionType != gYGNodeDefaults.style.positionType) { |
| 190 | + appendFormatedString( |
| 191 | + str, |
| 192 | + "position: %s; ", |
| 193 | + YGPositionTypeToString(node->style.positionType)); |
| 194 | + } |
| 195 | + |
| 196 | + appendEdgeIfNotUndefined(str, "left", node->style.position, YGEdgeLeft); |
| 197 | + appendEdgeIfNotUndefined(str, "right", node->style.position, YGEdgeRight); |
| 198 | + appendEdgeIfNotUndefined(str, "top", node->style.position, YGEdgeTop); |
| 199 | + appendEdgeIfNotUndefined(str, "bottom", node->style.position, YGEdgeBottom); |
| 200 | + appendFormatedString(str, "\" "); |
| 201 | + |
| 202 | + if (node->measure != nullptr) { |
| 203 | + appendFormatedString(str, "has-custom-measure=\"true\""); |
| 204 | + } |
| 205 | + } |
| 206 | + appendFormatedString(str, ">"); |
| 207 | + |
| 208 | + const uint32_t childCount = YGNodeListCount(node->children); |
| 209 | + if (options & YGPrintOptionsChildren && childCount > 0) { |
| 210 | + for (uint32_t i = 0; i < childCount; i++) { |
| 211 | + appendFormatedString(str, "\n"); |
| 212 | + YGNodeToString(str, YGNodeGetChild(node, i), options, level + 1); |
| 213 | + } |
| 214 | + appendFormatedString(str, "\n"); |
| 215 | + indent(str, level); |
| 216 | + } |
| 217 | + appendFormatedString(str, "</div>"); |
| 218 | +} |
| 219 | +} // namespace yoga |
| 220 | +} // namespace facebook |
0 commit comments