Ability to recreate yoga trees from JSON captures #1566
354
benchmark/Benchmark.cpp
Normal file
354
benchmark/Benchmark.cpp
Normal file
@@ -0,0 +1,354 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <yoga/Yoga.h>
|
||||||
|
#include <yoga/benchmark/Benchmark.h>
|
||||||
|
#include <yoga/debug/NodeToString.h>
|
||||||
|
#include <yoga/enums/PrintOptions.h>
|
||||||
|
|
||||||
|
namespace facebook::yoga {
|
||||||
|
|
||||||
|
using namespace nlohmann;
|
||||||
|
|
||||||
|
std::string parentPath() {
|
||||||
|
std::filesystem::path currentPath = __FILE__;
|
||||||
|
return currentPath.parent_path().string();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::filesystem::path capturePath(std::string captureName) {
|
||||||
|
return parentPath() + "/captures/" + captureName + ".json";
|
||||||
|
}
|
||||||
|
|
||||||
|
void captureTree(const Node* node, std::string captureName) {
|
||||||
|
std::string str;
|
||||||
|
nodeToString(str, node, PrintOptions::Style | PrintOptions::Children);
|
||||||
|
std::ofstream captureFile(capturePath(captureName));
|
||||||
|
captureFile << str;
|
||||||
|
captureFile.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string invalidArgumentMessage(std::string arg, std::string enumName) {
|
||||||
|
return arg + " does not represent any " + enumName + " values";
|
||||||
|
}
|
||||||
|
|
||||||
|
YGFlexDirection flexDirectionFromString(std::string str) {
|
||||||
|
if (str == "row") {
|
||||||
|
return YGFlexDirectionRow;
|
||||||
|
} else if (str == "row-reverse") {
|
||||||
|
return YGFlexDirectionRowReverse;
|
||||||
|
} else if (str == "column") {
|
||||||
|
return YGFlexDirectionColumn;
|
||||||
|
} else if (str == "column-reverse") {
|
||||||
|
return YGFlexDirectionColumnReverse;
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument(invalidArgumentMessage(str, "YGFlexDirection"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
YGJustify justifyContentFromString(std::string str) {
|
||||||
|
if (str == "flex-start") {
|
||||||
|
return YGJustifyFlexStart;
|
||||||
|
} else if (str == "center") {
|
||||||
|
return YGJustifyCenter;
|
||||||
|
} else if (str == "flex-end") {
|
||||||
|
return YGJustifyFlexEnd;
|
||||||
|
} else if (str == "space-between") {
|
||||||
|
return YGJustifySpaceBetween;
|
||||||
|
} else if (str == "space-around") {
|
||||||
|
return YGJustifySpaceAround;
|
||||||
|
} else if (str == "space-evenly") {
|
||||||
|
return YGJustifySpaceEvenly;
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument(invalidArgumentMessage(str, "YGJustify"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
YGAlign alignFromString(std::string str) {
|
||||||
|
if (str == "auto") {
|
||||||
|
return YGAlignAuto;
|
||||||
|
} else if (str == "flex-start") {
|
||||||
|
return YGAlignFlexStart;
|
||||||
|
} else if (str == "center") {
|
||||||
|
return YGAlignCenter;
|
||||||
|
} else if (str == "flex-end") {
|
||||||
|
return YGAlignFlexEnd;
|
||||||
|
} else if (str == "stretch") {
|
||||||
|
return YGAlignStretch;
|
||||||
|
} else if (str == "baseline") {
|
||||||
|
return YGAlignBaseline;
|
||||||
|
} else if (str == "space-between") {
|
||||||
|
return YGAlignSpaceBetween;
|
||||||
|
} else if (str == "space-around") {
|
||||||
|
return YGAlignSpaceAround;
|
||||||
|
} else if (str == "space-evenly") {
|
||||||
|
return YGAlignSpaceEvenly;
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
YGWrap wrapFromString(std::string str) {
|
||||||
|
if (str == "no-wrap") {
|
||||||
|
return YGWrapNoWrap;
|
||||||
|
} else if (str == "wrap") {
|
||||||
|
return YGWrapWrap;
|
||||||
|
} else if (str == "wrap-reverse") {
|
||||||
|
return YGWrapWrapReverse;
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
YGOverflow overflowFromString(std::string str) {
|
||||||
|
if (str == "visible") {
|
||||||
|
return YGOverflowVisible;
|
||||||
|
} else if (str == "hidden") {
|
||||||
|
return YGOverflowHidden;
|
||||||
|
} else if (str == "scroll") {
|
||||||
|
return YGOverflowScroll;
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
YGDisplay displayFromString(std::string str) {
|
||||||
|
if (str == "flex") {
|
||||||
|
return YGDisplayFlex;
|
||||||
|
} else if (str == "none") {
|
||||||
|
return YGDisplayNone;
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
YGPositionType positionTypeFromString(std::string str) {
|
||||||
|
if (str == "static") {
|
||||||
|
return YGPositionTypeStatic;
|
||||||
|
} else if (str == "relative") {
|
||||||
|
return YGPositionTypeRelative;
|
||||||
|
} else if (str == "absolute") {
|
||||||
|
return YGPositionTypeAbsolute;
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isAuto(json& j) {
|
||||||
|
return j.is_string() && j == "auto";
|
||||||
|
}
|
||||||
|
|
||||||
|
YGUnit unitFromJson(json& j) {
|
||||||
|
if (isAuto(j)) {
|
||||||
|
return YGUnitAuto;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string unit = j["unit"];
|
||||||
|
if (unit == "px") {
|
||||||
|
return YGUnitPoint;
|
||||||
|
} else if (unit == "\%") {
|
||||||
|
return YGUnitPercent;
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument(invalidArgumentMessage(unit, "YGUnit"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
YGEdge edgeFromString(std::string str) {
|
||||||
|
if (str == "left") {
|
||||||
|
return YGEdgeLeft;
|
||||||
|
} else if (str == "top") {
|
||||||
|
return YGEdgeTop;
|
||||||
|
} else if (str == "right") {
|
||||||
|
return YGEdgeRight;
|
||||||
|
} else if (str == "bottom") {
|
||||||
|
return YGEdgeBottom;
|
||||||
|
} else if (str == "start") {
|
||||||
|
return YGEdgeStart;
|
||||||
|
} else if (str == "end") {
|
||||||
|
return YGEdgeEnd;
|
||||||
|
} else if (str == "horizontal") {
|
||||||
|
return YGEdgeHorizontal;
|
||||||
|
} else if (str == "vertical") {
|
||||||
|
return YGEdgeVertical;
|
||||||
|
} else if (str == "all") {
|
||||||
|
return YGEdgeAll;
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument(invalidArgumentMessage(str, "YGEdge"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string edgeStringFromPropertyName(
|
||||||
|
json::iterator it,
|
||||||
|
std::string propertyName) {
|
||||||
|
return it.key().substr(propertyName.length() + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
YGNodeRef buildTreeFromJson(json& j, YGNodeRef parent, size_t index) {
|
||||||
|
const YGNodeRef node = YGNodeNew();
|
||||||
|
if (parent != nullptr) {
|
||||||
|
YGNodeInsertChild(parent, node, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
json style = j["style"];
|
||||||
|
for (json::iterator it = style.begin(); it != style.end(); it++) {
|
||||||
|
if (it.key() == "flex-direction") {
|
||||||
|
YGNodeStyleSetFlexDirection(node, flexDirectionFromString(it.value()));
|
||||||
|
} else if (it.key() == "justify-content") {
|
||||||
|
YGNodeStyleSetJustifyContent(node, justifyContentFromString(it.value()));
|
||||||
|
} else if (it.key() == "align-items") {
|
||||||
|
YGNodeStyleSetAlignItems(node, alignFromString(it.value()));
|
||||||
|
} else if (it.key() == "align-content") {
|
||||||
|
YGNodeStyleSetAlignContent(node, alignFromString(it.value()));
|
||||||
|
} else if (it.key() == "align-self") {
|
||||||
|
YGNodeStyleSetAlignSelf(node, alignFromString(it.value()));
|
||||||
|
} else if (it.key() == "flex-wrap") {
|
||||||
|
YGNodeStyleSetFlexWrap(node, wrapFromString(it.value()));
|
||||||
|
} else if (it.key() == "overflow") {
|
||||||
|
YGNodeStyleSetOverflow(node, overflowFromString(it.value()));
|
||||||
|
} else if (it.key() == "display") {
|
||||||
|
YGNodeStyleSetDisplay(node, displayFromString(it.value()));
|
||||||
|
} else if (it.key() == "position-type") {
|
||||||
|
YGNodeStyleSetPositionType(node, positionTypeFromString(it.value()));
|
||||||
|
} else if (it.key() == "flex-grow") {
|
||||||
|
YGNodeStyleSetFlexGrow(node, it.value());
|
||||||
|
} else if (it.key() == "flex-shrink") {
|
||||||
|
YGNodeStyleSetFlexShrink(node, it.value());
|
||||||
|
} else if (it.key() == "flex") {
|
||||||
|
YGNodeStyleSetFlex(node, it.value());
|
||||||
|
} else if (it.key() == "flex-basis") {
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitAuto) {
|
||||||
|
YGNodeStyleSetFlexBasisAuto(node);
|
||||||
|
} else if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetFlexBasis(node, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitPercent) {
|
||||||
|
YGNodeStyleSetFlexBasisPercent(node, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key().starts_with("position")) {
|
||||||
|
YGEdge edge = edgeFromString(edgeStringFromPropertyName(it, "position"));
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetPosition(node, edge, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitPercent) {
|
||||||
|
YGNodeStyleSetPositionPercent(node, edge, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key().starts_with("padding")) {
|
||||||
|
YGEdge edge = edgeFromString(edgeStringFromPropertyName(it, "padding"));
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetPadding(node, edge, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitPercent) {
|
||||||
|
YGNodeStyleSetPaddingPercent(node, edge, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key().starts_with("border")) {
|
||||||
|
YGEdge edge = edgeFromString(edgeStringFromPropertyName(it, "border"));
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetBorder(node, edge, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key().starts_with("margin")) {
|
||||||
|
YGEdge edge = edgeFromString(edgeStringFromPropertyName(it, "margin"));
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetMargin(node, edge, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitPercent) {
|
||||||
|
YGNodeStyleSetMarginPercent(node, edge, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitAuto) {
|
||||||
|
YGNodeStyleSetMarginAuto(node, edge);
|
||||||
|
}
|
||||||
|
} else if (it.key() == "gap") {
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetGap(node, YGGutterAll, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key() == "column-gap") {
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetGap(node, YGGutterColumn, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key() == "row-gap") {
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetGap(node, YGGutterRow, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key() == "height") {
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitAuto) {
|
||||||
|
YGNodeStyleSetHeightAuto(node);
|
||||||
|
} else if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetHeight(node, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitPercent) {
|
||||||
|
YGNodeStyleSetHeightPercent(node, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key() == "width") {
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitAuto) {
|
||||||
|
YGNodeStyleSetWidthAuto(node);
|
||||||
|
} else if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetWidth(node, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitPercent) {
|
||||||
|
YGNodeStyleSetWidthPercent(node, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key() == "min-height") {
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetMinHeight(node, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitPercent) {
|
||||||
|
YGNodeStyleSetMinHeightPercent(node, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key() == "min-width") {
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetMinWidth(node, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitPercent) {
|
||||||
|
YGNodeStyleSetMinWidthPercent(node, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key() == "max-height") {
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetMaxHeight(node, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitPercent) {
|
||||||
|
YGNodeStyleSetMaxHeightPercent(node, it.value()["value"]);
|
||||||
|
}
|
||||||
|
} else if (it.key() == "max-width") {
|
||||||
|
YGUnit unit = unitFromJson(it.value());
|
||||||
|
if (unit == YGUnitPoint) {
|
||||||
|
YGNodeStyleSetMaxWidth(node, it.value()["value"]);
|
||||||
|
} else if (unit == YGUnitPercent) {
|
||||||
|
YGNodeStyleSetMaxWidthPercent(node, it.value()["value"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
json children = j["children"];
|
||||||
|
size_t childIndex = 0;
|
||||||
|
for (json child : children) {
|
||||||
|
buildTreeFromJson(child, node, childIndex);
|
||||||
|
childIndex++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void generateBenchmark(std::string captureName) {
|
||||||
|
std::ifstream captureFile(capturePath(captureName));
|
||||||
|
json capture = json::parse(captureFile);
|
||||||
|
|
||||||
|
YGNodeRef root = buildTreeFromJson(capture, nullptr, 0 /*index*/);
|
||||||
|
|
||||||
|
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
||||||
|
|
||||||
|
YGNodeFreeRecursive(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace facebook::yoga
|
||||||
|
#endif
|
21
benchmark/Benchmark.h
Normal file
21
benchmark/Benchmark.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <yoga/node/Node.h>
|
||||||
|
|
||||||
|
namespace facebook::yoga {
|
||||||
|
|
||||||
|
void captureTree(const Node* node, std::string captureName);
|
||||||
|
|
||||||
|
void generateBenchmark(std::string captureName);
|
||||||
|
|
||||||
|
} // namespace facebook::yoga
|
||||||
|
#endif
|
24766
lib/json/json.hpp
Normal file
24766
lib/json/json.hpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -9,200 +9,159 @@
|
|||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
#include <yoga/debug/Log.h>
|
#include <yoga/debug/Log.h>
|
||||||
#include <yoga/debug/NodeToString.h>
|
#include <yoga/debug/NodeToString.h>
|
||||||
#include <yoga/numeric/Comparison.h>
|
#include <yoga/numeric/Comparison.h>
|
||||||
|
|
||||||
namespace facebook::yoga {
|
namespace facebook::yoga {
|
||||||
|
|
||||||
static void indent(std::string& base, uint32_t level) {
|
using namespace nlohmann;
|
||||||
for (uint32_t i = 0; i < level; ++i) {
|
|
||||||
base.append(" ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void appendFormattedString(std::string& str, const char* fmt, ...) {
|
|
||||||
va_list args;
|
|
||||||
va_start(args, fmt);
|
|
||||||
va_list argsCopy;
|
|
||||||
va_copy(argsCopy, args);
|
|
||||||
std::vector<char> buf(1 + static_cast<size_t>(vsnprintf(NULL, 0, fmt, args)));
|
|
||||||
va_end(args);
|
|
||||||
vsnprintf(buf.data(), buf.size(), fmt, argsCopy);
|
|
||||||
va_end(argsCopy);
|
|
||||||
std::string result = std::string(buf.begin(), buf.end() - 1);
|
|
||||||
str.append(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void appendFloatOptionalIfDefined(
|
static void appendFloatOptionalIfDefined(
|
||||||
std::string& base,
|
json& j,
|
||||||
const std::string key,
|
const std::string key,
|
||||||
const FloatOptional num) {
|
const FloatOptional num) {
|
||||||
if (num.isDefined()) {
|
if (num.isDefined()) {
|
||||||
appendFormattedString(base, "%s: %g; ", key.c_str(), num.unwrap());
|
j["style"][key] = num.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void appendNumberIfNotUndefined(
|
static void appendNumberIfNotUndefined(
|
||||||
std::string& base,
|
json& j,
|
||||||
const std::string key,
|
const std::string key,
|
||||||
const Style::Length& number) {
|
const Style::Length& number) {
|
||||||
if (number.unit() != Unit::Undefined) {
|
if (number.unit() != Unit::Undefined) {
|
||||||
if (number.unit() == Unit::Auto) {
|
if (number.unit() == Unit::Auto) {
|
||||||
base.append(key + ": auto; ");
|
j["style"][key] = "auto";
|
||||||
} else {
|
} else {
|
||||||
std::string unit = number.unit() == Unit::Point ? "px" : "%%";
|
std::string unit = number.unit() == Unit::Point ? "px" : "%%";
|
||||||
appendFormattedString(
|
j["style"][key]["value"] = number.value().unwrap();
|
||||||
base,
|
j["style"][key]["unit"] = unit;
|
||||||
"%s: %g%s; ",
|
|
||||||
key.c_str(),
|
|
||||||
number.value().unwrap(),
|
|
||||||
unit.c_str());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void appendNumberIfNotAuto(
|
static void appendNumberIfNotAuto(
|
||||||
std::string& base,
|
json& j,
|
||||||
const std::string& key,
|
const std::string& key,
|
||||||
const Style::Length& number) {
|
const Style::Length& number) {
|
||||||
if (number.unit() != Unit::Auto) {
|
if (number.unit() != Unit::Auto) {
|
||||||
appendNumberIfNotUndefined(base, key, number);
|
appendNumberIfNotUndefined(j, key, number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void appendNumberIfNotZero(
|
static void appendNumberIfNotZero(
|
||||||
std::string& base,
|
json& j,
|
||||||
const std::string& str,
|
const std::string& str,
|
||||||
const Style::Length& number) {
|
const Style::Length& number) {
|
||||||
if (number.unit() == Unit::Auto) {
|
if (number.unit() == Unit::Auto) {
|
||||||
base.append(str + ": auto; ");
|
j["style"][str] = "auto";
|
||||||
} else if (!yoga::inexactEquals(number.value().unwrap(), 0)) {
|
} else if (!yoga::inexactEquals(number.value().unwrap(), 0)) {
|
||||||
appendNumberIfNotUndefined(base, str, number);
|
appendNumberIfNotUndefined(j, str, number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <auto Field>
|
template <auto Field>
|
||||||
static void
|
static void appendEdges(json& j, const std::string& key, const Style& style) {
|
||||||
appendEdges(std::string& base, const std::string& key, const Style& style) {
|
|
||||||
for (auto edge : ordinals<Edge>()) {
|
for (auto edge : ordinals<Edge>()) {
|
||||||
std::string str = key + "-" + toString(edge);
|
std::string str = key + "-" + toString(edge);
|
||||||
appendNumberIfNotZero(base, str, (style.*Field)(edge));
|
appendNumberIfNotZero(j, str, (style.*Field)(edge));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
nodeToStringImpl(json& j, const yoga::Node* node, PrintOptions options) {
|
||||||
|
if ((options & PrintOptions::Layout) == PrintOptions::Layout) {
|
||||||
|
j["layout"]["width"] = node->getLayout().dimension(Dimension::Width);
|
||||||
|
j["layout"]["height"] = node->getLayout().dimension(Dimension::Height);
|
||||||
|
j["layout"]["top"] = node->getLayout().position(PhysicalEdge::Top);
|
||||||
|
j["layout"]["left"] = node->getLayout().position(PhysicalEdge::Top);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((options & PrintOptions::Style) == PrintOptions::Style) {
|
||||||
|
const auto& style = node->style();
|
||||||
|
if (style.flexDirection() != yoga::Style{}.flexDirection()) {
|
||||||
|
j["style"]["flex-direction"] = toString(style.flexDirection());
|
||||||
|
}
|
||||||
|
if (style.justifyContent() != yoga::Style{}.justifyContent()) {
|
||||||
|
j["style"]["justify-content"] = toString(style.justifyContent());
|
||||||
|
}
|
||||||
|
if (style.alignItems() != yoga::Style{}.alignItems()) {
|
||||||
|
j["style"]["align-items"] = toString(style.alignItems());
|
||||||
|
}
|
||||||
|
if (style.alignContent() != yoga::Style{}.alignContent()) {
|
||||||
|
j["style"]["align-content"] = toString(style.alignContent());
|
||||||
|
}
|
||||||
|
if (style.alignSelf() != yoga::Style{}.alignSelf()) {
|
||||||
|
j["style"]["align-self"] = toString(style.alignSelf());
|
||||||
|
}
|
||||||
|
if (style.flexWrap() != yoga::Style{}.flexWrap()) {
|
||||||
|
j["style"]["flex-wrap"] = toString(style.flexWrap());
|
||||||
|
}
|
||||||
|
if (style.overflow() != yoga::Style{}.overflow()) {
|
||||||
|
j["style"]["overflow"] = toString(style.overflow());
|
||||||
|
}
|
||||||
|
if (style.display() != yoga::Style{}.display()) {
|
||||||
|
j["style"]["display"] = toString(style.display());
|
||||||
|
}
|
||||||
|
if (style.positionType() != yoga::Style{}.positionType()) {
|
||||||
|
j["style"]["position-type"] = toString(style.positionType());
|
||||||
|
}
|
||||||
|
|
||||||
|
appendFloatOptionalIfDefined(j, "flex-grow", style.flexGrow());
|
||||||
|
appendFloatOptionalIfDefined(j, "flex-shrink", style.flexShrink());
|
||||||
|
appendFloatOptionalIfDefined(j, "flex", style.flex());
|
||||||
|
appendNumberIfNotAuto(j, "flex-basis", style.flexBasis());
|
||||||
|
|
||||||
|
appendEdges<&Style::margin>(j, "margin", style);
|
||||||
|
appendEdges<&Style::padding>(j, "padding", style);
|
||||||
|
appendEdges<&Style::border>(j, "border", style);
|
||||||
|
appendEdges<&Style::position>(j, "position", style);
|
||||||
|
|
||||||
|
if (style.gap(Gutter::All).isDefined()) {
|
||||||
|
appendNumberIfNotUndefined(j, "gap", style.gap(Gutter::All));
|
||||||
|
} else {
|
||||||
|
appendNumberIfNotUndefined(j, "column-gap", style.gap(Gutter::Column));
|
||||||
|
appendNumberIfNotUndefined(j, "row-gap", style.gap(Gutter::Row));
|
||||||
|
}
|
||||||
|
|
||||||
|
appendNumberIfNotAuto(j, "width", style.dimension(Dimension::Width));
|
||||||
|
appendNumberIfNotAuto(j, "height", style.dimension(Dimension::Height));
|
||||||
|
appendNumberIfNotAuto(j, "max-width", style.maxDimension(Dimension::Width));
|
||||||
|
appendNumberIfNotAuto(
|
||||||
|
j, "max-height", style.maxDimension(Dimension::Height));
|
||||||
|
appendNumberIfNotAuto(j, "min-width", style.minDimension(Dimension::Width));
|
||||||
|
appendNumberIfNotAuto(
|
||||||
|
j, "min-height", style.minDimension(Dimension::Height));
|
||||||
|
|
||||||
|
if (node->hasMeasureFunc()) {
|
||||||
|
j["style"]["has-custom-measure"] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t childCount = node->getChildCount();
|
||||||
|
if ((options & PrintOptions::Children) == PrintOptions::Children &&
|
||||||
|
childCount > 0) {
|
||||||
|
for (size_t i = 0; i < childCount; i++) {
|
||||||
|
j["children"].push_back({});
|
||||||
|
nodeToStringImpl(j["children"][i], node->getChild(i), options);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void nodeToString(
|
void nodeToString(
|
||||||
std::string& str,
|
std::string& str,
|
||||||
const yoga::Node* node,
|
const yoga::Node* node,
|
||||||
PrintOptions options,
|
PrintOptions options) {
|
||||||
uint32_t level) {
|
json j;
|
||||||
indent(str, level);
|
nodeToStringImpl(j, node, options);
|
||||||
appendFormattedString(str, "<div ");
|
str = j.dump(2);
|
||||||
|
|
||||||
if ((options & PrintOptions::Layout) == PrintOptions::Layout) {
|
|
||||||
appendFormattedString(str, "layout=\"");
|
|
||||||
appendFormattedString(
|
|
||||||
str, "width: %g; ", node->getLayout().dimension(Dimension::Width));
|
|
||||||
appendFormattedString(
|
|
||||||
str, "height: %g; ", node->getLayout().dimension(Dimension::Height));
|
|
||||||
appendFormattedString(
|
|
||||||
str, "top: %g; ", node->getLayout().position(PhysicalEdge::Top));
|
|
||||||
appendFormattedString(
|
|
||||||
str, "left: %g;", node->getLayout().position(PhysicalEdge::Left));
|
|
||||||
appendFormattedString(str, "\" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((options & PrintOptions::Style) == PrintOptions::Style) {
|
|
||||||
appendFormattedString(str, "style=\"");
|
|
||||||
const auto& style = node->style();
|
|
||||||
if (style.flexDirection() != yoga::Style{}.flexDirection()) {
|
|
||||||
appendFormattedString(
|
|
||||||
str, "flex-direction: %s; ", toString(style.flexDirection()));
|
|
||||||
}
|
|
||||||
if (style.justifyContent() != yoga::Style{}.justifyContent()) {
|
|
||||||
appendFormattedString(
|
|
||||||
str, "justify-content: %s; ", toString(style.justifyContent()));
|
|
||||||
}
|
|
||||||
if (style.alignItems() != yoga::Style{}.alignItems()) {
|
|
||||||
appendFormattedString(
|
|
||||||
str, "align-items: %s; ", toString(style.alignItems()));
|
|
||||||
}
|
|
||||||
if (style.alignContent() != yoga::Style{}.alignContent()) {
|
|
||||||
appendFormattedString(
|
|
||||||
str, "align-content: %s; ", toString(style.alignContent()));
|
|
||||||
}
|
|
||||||
if (style.alignSelf() != yoga::Style{}.alignSelf()) {
|
|
||||||
appendFormattedString(
|
|
||||||
str, "align-self: %s; ", toString(style.alignSelf()));
|
|
||||||
}
|
|
||||||
appendFloatOptionalIfDefined(str, "flex-grow", style.flexGrow());
|
|
||||||
appendFloatOptionalIfDefined(str, "flex-shrink", style.flexShrink());
|
|
||||||
appendNumberIfNotAuto(str, "flex-basis", style.flexBasis());
|
|
||||||
appendFloatOptionalIfDefined(str, "flex", style.flex());
|
|
||||||
|
|
||||||
if (style.flexWrap() != yoga::Style{}.flexWrap()) {
|
|
||||||
appendFormattedString(str, "flex-wrap: %s; ", toString(style.flexWrap()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (style.overflow() != yoga::Style{}.overflow()) {
|
|
||||||
appendFormattedString(str, "overflow: %s; ", toString(style.overflow()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (style.display() != yoga::Style{}.display()) {
|
|
||||||
appendFormattedString(str, "display: %s; ", toString(style.display()));
|
|
||||||
}
|
|
||||||
appendEdges<&Style::margin>(str, "margin", style);
|
|
||||||
appendEdges<&Style::padding>(str, "padding", style);
|
|
||||||
appendEdges<&Style::border>(str, "border", style);
|
|
||||||
|
|
||||||
if (style.gap(Gutter::All).isDefined()) {
|
|
||||||
appendNumberIfNotUndefined(str, "gap", style.gap(Gutter::All));
|
|
||||||
} else {
|
|
||||||
appendNumberIfNotUndefined(str, "column-gap", style.gap(Gutter::Column));
|
|
||||||
appendNumberIfNotUndefined(str, "row-gap", style.gap(Gutter::Row));
|
|
||||||
}
|
|
||||||
|
|
||||||
appendNumberIfNotAuto(str, "width", style.dimension(Dimension::Width));
|
|
||||||
appendNumberIfNotAuto(str, "height", style.dimension(Dimension::Height));
|
|
||||||
appendNumberIfNotAuto(
|
|
||||||
str, "max-width", style.maxDimension(Dimension::Width));
|
|
||||||
appendNumberIfNotAuto(
|
|
||||||
str, "max-height", style.maxDimension(Dimension::Height));
|
|
||||||
appendNumberIfNotAuto(
|
|
||||||
str, "min-width", style.minDimension(Dimension::Width));
|
|
||||||
appendNumberIfNotAuto(
|
|
||||||
str, "min-height", style.minDimension(Dimension::Height));
|
|
||||||
|
|
||||||
if (style.positionType() != yoga::Style{}.positionType()) {
|
|
||||||
appendFormattedString(
|
|
||||||
str, "position: %s; ", toString(style.positionType()));
|
|
||||||
}
|
|
||||||
|
|
||||||
appendEdges<&Style::position>(str, "position", style);
|
|
||||||
appendFormattedString(str, "\" ");
|
|
||||||
|
|
||||||
if (node->hasMeasureFunc()) {
|
|
||||||
appendFormattedString(str, "has-custom-measure=\"true\"");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
appendFormattedString(str, ">");
|
|
||||||
|
|
||||||
const size_t childCount = node->getChildCount();
|
|
||||||
if ((options & PrintOptions::Children) == PrintOptions::Children &&
|
|
||||||
childCount > 0) {
|
|
||||||
for (size_t i = 0; i < childCount; i++) {
|
|
||||||
appendFormattedString(str, "\n");
|
|
||||||
nodeToString(str, node->getChild(i), options, level + 1);
|
|
||||||
}
|
|
||||||
appendFormattedString(str, "\n");
|
|
||||||
indent(str, level);
|
|
||||||
}
|
|
||||||
appendFormattedString(str, "</div>");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void print(const yoga::Node* node, PrintOptions options) {
|
void print(const yoga::Node* node, PrintOptions options) {
|
||||||
std::string str;
|
std::string str;
|
||||||
yoga::nodeToString(str, node, options, 0);
|
yoga::nodeToString(str, node, options);
|
||||||
yoga::log(node, LogLevel::Debug, str.c_str());
|
yoga::log(node, LogLevel::Debug, str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -19,8 +19,7 @@ namespace facebook::yoga {
|
|||||||
void nodeToString(
|
void nodeToString(
|
||||||
std::string& str,
|
std::string& str,
|
||||||
const yoga::Node* node,
|
const yoga::Node* node,
|
||||||
PrintOptions options,
|
PrintOptions options);
|
||||||
uint32_t level);
|
|
||||||
|
|
||||||
void print(const yoga::Node* node, PrintOptions options);
|
void print(const yoga::Node* node, PrintOptions options);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user