[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / third-party / yoga / YGNodePrint.cpp
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7
8 #include "YGNodePrint.h"
9 #include <stdarg.h>
10 #include "YGEnums.h"
11 #include "YGNode.h"
12 #include "Yoga-internal.h"
13
14 namespace facebook {
15 namespace yoga {
16 typedef std::string string;
17
18 static void indent(string* base, uint32_t level) {
19   for (uint32_t i = 0; i < level; ++i) {
20     base->append("  ");
21   }
22 }
23
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]);
27 }
28
29 static void appendFormatedString(string* str, const char* fmt, ...) {
30   va_list args;
31   va_start(args, fmt);
32   va_list argsCopy;
33   va_copy(argsCopy, args);
34   std::vector<char> buf(1 + vsnprintf(NULL, 0, fmt, args));
35   va_end(args);
36   vsnprintf(buf.data(), buf.size(), fmt, argsCopy);
37   va_end(argsCopy);
38   string result = string(buf.begin(), buf.end() - 1);
39   str->append(result);
40 }
41
42 static void appendFloatOptionalIfDefined(
43     string* base,
44     const string key,
45     const YGFloatOptional num) {
46   if (!num.isUndefined()) {
47     appendFormatedString(base, "%s: %g; ", key.c_str(), num.getValue());
48   }
49 }
50
51 static void appendNumberIfNotUndefined(
52     string* base,
53     const string key,
54     const YGValue number) {
55   if (number.unit != YGUnitUndefined) {
56     if (number.unit == YGUnitAuto) {
57       base->append(key + ": auto; ");
58     } else {
59       string unit = number.unit == YGUnitPoint ? "px" : "%%";
60       appendFormatedString(
61           base, "%s: %g%s; ", key.c_str(), number.value, unit.c_str());
62     }
63   }
64 }
65
66 static void
67 appendNumberIfNotAuto(string* base, const string& key, const YGValue number) {
68   if (number.unit != YGUnitAuto) {
69     appendNumberIfNotUndefined(base, key, number);
70   }
71 }
72
73 static void
74 appendNumberIfNotZero(string* base, const string& str, const YGValue number) {
75
76   if (number.unit == YGUnitAuto) {
77     base->append(str + ": auto; ");
78   } else if (!YGFloatsEqual(number.value, 0)) {
79     appendNumberIfNotUndefined(base, str, number);
80   }
81 }
82
83 static void appendEdges(
84     string* base,
85     const string& key,
86     const std::array<YGValue, YGEdgeCount>& edges) {
87   if (areFourValuesEqual(edges)) {
88     appendNumberIfNotZero(base, key, edges[YGEdgeLeft]);
89   } else {
90     for (int edge = YGEdgeLeft; edge != YGEdgeAll; ++edge) {
91       string str = key + "-" + YGEdgeToString(static_cast<YGEdge>(edge));
92       appendNumberIfNotZero(base, str, edges[edge]);
93     }
94   }
95 }
96
97 static void appendEdgeIfNotUndefined(
98     string* base,
99     const string& str,
100     const std::array<YGValue, YGEdgeCount>& edges,
101     const YGEdge edge) {
102   appendNumberIfNotUndefined(
103       base, str, *YGComputedEdgeValue(edges, edge, &YGValueUndefined));
104 }
105
106 void YGNodeToString(
107     std::string* str,
108     YGNodeRef node,
109     YGPrintOptions options,
110     uint32_t level) {
111   indent(str, level);
112   appendFormatedString(str, "<div ");
113   if (node->getPrintFunc() != nullptr) {
114     node->getPrintFunc()(node);
115   }
116
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, "\" ");
128   }
129
130   if (options & YGPrintOptionsStyle) {
131     appendFormatedString(str, "style=\"");
132     if (node->getStyle().flexDirection != YGNode().getStyle().flexDirection) {
133       appendFormatedString(
134           str,
135           "flex-direction: %s; ",
136           YGFlexDirectionToString(node->getStyle().flexDirection));
137     }
138     if (node->getStyle().justifyContent != YGNode().getStyle().justifyContent) {
139       appendFormatedString(
140           str,
141           "justify-content: %s; ",
142           YGJustifyToString(node->getStyle().justifyContent));
143     }
144     if (node->getStyle().alignItems != YGNode().getStyle().alignItems) {
145       appendFormatedString(
146           str,
147           "align-items: %s; ",
148           YGAlignToString(node->getStyle().alignItems));
149     }
150     if (node->getStyle().alignContent != YGNode().getStyle().alignContent) {
151       appendFormatedString(
152           str,
153           "align-content: %s; ",
154           YGAlignToString(node->getStyle().alignContent));
155     }
156     if (node->getStyle().alignSelf != YGNode().getStyle().alignSelf) {
157       appendFormatedString(
158           str, "align-self: %s; ", YGAlignToString(node->getStyle().alignSelf));
159     }
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);
165
166     if (node->getStyle().flexWrap != YGNode().getStyle().flexWrap) {
167       appendFormatedString(
168           str, "flexWrap: %s; ", YGWrapToString(node->getStyle().flexWrap));
169     }
170
171     if (node->getStyle().overflow != YGNode().getStyle().overflow) {
172       appendFormatedString(
173           str, "overflow: %s; ", YGOverflowToString(node->getStyle().overflow));
174     }
175
176     if (node->getStyle().display != YGNode().getStyle().display) {
177       appendFormatedString(
178           str, "display: %s; ", YGDisplayToString(node->getStyle().display));
179     }
180     appendEdges(str, "margin", node->getStyle().margin);
181     appendEdges(str, "padding", node->getStyle().padding);
182     appendEdges(str, "border", node->getStyle().border);
183
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]);
196
197     if (node->getStyle().positionType != YGNode().getStyle().positionType) {
198       appendFormatedString(
199           str,
200           "position: %s; ",
201           YGPositionTypeToString(node->getStyle().positionType));
202     }
203
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, "\" ");
212
213     if (node->getMeasure() != nullptr) {
214       appendFormatedString(str, "has-custom-measure=\"true\"");
215     }
216   }
217   appendFormatedString(str, ">");
218
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);
224     }
225     appendFormatedString(str, "\n");
226     indent(str, level);
227   }
228   appendFormatedString(str, "</div>");
229 }
230 } // namespace yoga
231 } // namespace facebook