2 * Copyright (c) 2017-present, Facebook, Inc.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
8 #include "YGNodePrint.h"
12 #include "Yoga-internal.h"
16 typedef std::string string;
18 static void indent(string* base, uint32_t level) {
19 for (uint32_t i = 0; i < level; ++i) {
24 static bool areFourValuesEqual(const std::array<YGValue, YGEdgeCount>& four) {
25 return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) &&
26 YGValueEqual(four[0], four[3]);
29 static void appendFormatedString(string* str, const char* fmt, ...) {
33 va_copy(argsCopy, args);
34 std::vector<char> buf(1 + vsnprintf(NULL, 0, fmt, args));
36 vsnprintf(buf.data(), buf.size(), fmt, argsCopy);
38 string result = string(buf.begin(), buf.end() - 1);
42 static void appendFloatOptionalIfDefined(
45 const YGFloatOptional num) {
46 if (!num.isUndefined()) {
47 appendFormatedString(base, "%s: %g; ", key.c_str(), num.getValue());
51 static void appendNumberIfNotUndefined(
54 const YGValue number) {
55 if (number.unit != YGUnitUndefined) {
56 if (number.unit == YGUnitAuto) {
57 base->append(key + ": auto; ");
59 string unit = number.unit == YGUnitPoint ? "px" : "%%";
61 base, "%s: %g%s; ", key.c_str(), number.value, unit.c_str());
67 appendNumberIfNotAuto(string* base, const string& key, const YGValue number) {
68 if (number.unit != YGUnitAuto) {
69 appendNumberIfNotUndefined(base, key, number);
74 appendNumberIfNotZero(string* base, const string& str, const YGValue number) {
76 if (number.unit == YGUnitAuto) {
77 base->append(str + ": auto; ");
78 } else if (!YGFloatsEqual(number.value, 0)) {
79 appendNumberIfNotUndefined(base, str, number);
83 static void appendEdges(
86 const std::array<YGValue, YGEdgeCount>& edges) {
87 if (areFourValuesEqual(edges)) {
88 appendNumberIfNotZero(base, key, edges[YGEdgeLeft]);
90 for (int edge = YGEdgeLeft; edge != YGEdgeAll; ++edge) {
91 string str = key + "-" + YGEdgeToString(static_cast<YGEdge>(edge));
92 appendNumberIfNotZero(base, str, edges[edge]);
97 static void appendEdgeIfNotUndefined(
100 const std::array<YGValue, YGEdgeCount>& edges,
102 appendNumberIfNotUndefined(
103 base, str, *YGComputedEdgeValue(edges, edge, &YGValueUndefined));
109 YGPrintOptions options,
112 appendFormatedString(str, "<div ");
113 if (node->getPrintFunc() != nullptr) {
114 node->getPrintFunc()(node);
117 if (options & YGPrintOptionsLayout) {
118 appendFormatedString(str, "layout=\"");
119 appendFormatedString(
120 str, "width: %g; ", node->getLayout().dimensions[YGDimensionWidth]);
121 appendFormatedString(
122 str, "height: %g; ", node->getLayout().dimensions[YGDimensionHeight]);
123 appendFormatedString(
124 str, "top: %g; ", node->getLayout().position[YGEdgeTop]);
125 appendFormatedString(
126 str, "left: %g;", node->getLayout().position[YGEdgeLeft]);
127 appendFormatedString(str, "\" ");
130 if (options & YGPrintOptionsStyle) {
131 appendFormatedString(str, "style=\"");
132 if (node->getStyle().flexDirection != YGNode().getStyle().flexDirection) {
133 appendFormatedString(
135 "flex-direction: %s; ",
136 YGFlexDirectionToString(node->getStyle().flexDirection));
138 if (node->getStyle().justifyContent != YGNode().getStyle().justifyContent) {
139 appendFormatedString(
141 "justify-content: %s; ",
142 YGJustifyToString(node->getStyle().justifyContent));
144 if (node->getStyle().alignItems != YGNode().getStyle().alignItems) {
145 appendFormatedString(
148 YGAlignToString(node->getStyle().alignItems));
150 if (node->getStyle().alignContent != YGNode().getStyle().alignContent) {
151 appendFormatedString(
153 "align-content: %s; ",
154 YGAlignToString(node->getStyle().alignContent));
156 if (node->getStyle().alignSelf != YGNode().getStyle().alignSelf) {
157 appendFormatedString(
158 str, "align-self: %s; ", YGAlignToString(node->getStyle().alignSelf));
160 appendFloatOptionalIfDefined(str, "flex-grow", node->getStyle().flexGrow);
161 appendFloatOptionalIfDefined(
162 str, "flex-shrink", node->getStyle().flexShrink);
163 appendNumberIfNotAuto(str, "flex-basis", node->getStyle().flexBasis);
164 appendFloatOptionalIfDefined(str, "flex", node->getStyle().flex);
166 if (node->getStyle().flexWrap != YGNode().getStyle().flexWrap) {
167 appendFormatedString(
168 str, "flexWrap: %s; ", YGWrapToString(node->getStyle().flexWrap));
171 if (node->getStyle().overflow != YGNode().getStyle().overflow) {
172 appendFormatedString(
173 str, "overflow: %s; ", YGOverflowToString(node->getStyle().overflow));
176 if (node->getStyle().display != YGNode().getStyle().display) {
177 appendFormatedString(
178 str, "display: %s; ", YGDisplayToString(node->getStyle().display));
180 appendEdges(str, "margin", node->getStyle().margin);
181 appendEdges(str, "padding", node->getStyle().padding);
182 appendEdges(str, "border", node->getStyle().border);
184 appendNumberIfNotAuto(
185 str, "width", node->getStyle().dimensions[YGDimensionWidth]);
186 appendNumberIfNotAuto(
187 str, "height", node->getStyle().dimensions[YGDimensionHeight]);
188 appendNumberIfNotAuto(
189 str, "max-width", node->getStyle().maxDimensions[YGDimensionWidth]);
190 appendNumberIfNotAuto(
191 str, "max-height", node->getStyle().maxDimensions[YGDimensionHeight]);
192 appendNumberIfNotAuto(
193 str, "min-width", node->getStyle().minDimensions[YGDimensionWidth]);
194 appendNumberIfNotAuto(
195 str, "min-height", node->getStyle().minDimensions[YGDimensionHeight]);
197 if (node->getStyle().positionType != YGNode().getStyle().positionType) {
198 appendFormatedString(
201 YGPositionTypeToString(node->getStyle().positionType));
204 appendEdgeIfNotUndefined(
205 str, "left", node->getStyle().position, YGEdgeLeft);
206 appendEdgeIfNotUndefined(
207 str, "right", node->getStyle().position, YGEdgeRight);
208 appendEdgeIfNotUndefined(str, "top", node->getStyle().position, YGEdgeTop);
209 appendEdgeIfNotUndefined(
210 str, "bottom", node->getStyle().position, YGEdgeBottom);
211 appendFormatedString(str, "\" ");
213 if (node->getMeasure() != nullptr) {
214 appendFormatedString(str, "has-custom-measure=\"true\"");
217 appendFormatedString(str, ">");
219 const uint32_t childCount = static_cast<uint32_t>(node->getChildren().size());
220 if (options & YGPrintOptionsChildren && childCount > 0) {
221 for (uint32_t i = 0; i < childCount; i++) {
222 appendFormatedString(str, "\n");
223 YGNodeToString(str, YGNodeGetChild(node, i), options, level + 1);
225 appendFormatedString(str, "\n");
228 appendFormatedString(str, "</div>");
231 } // namespace facebook