Change NodeToString.cpp to output JSON not html #1563

Closed
joevilches wants to merge 3 commits from export-D52929268 into main
19 changed files with 271 additions and 442 deletions
Showing only changes of commit 4fcc0d8491 - Show all commits

View File

@@ -76,6 +76,27 @@ jobs:
run: ./build/benchmark run: ./build/benchmark
working-directory: benchmark working-directory: benchmark
capture:
name: Capture [${{ matrix.toolchain }}]
runs-on: ${{ (matrix.toolchain == 'MSVC') && 'windows-latest' || 'ubuntu-latest' }}
strategy:
matrix:
toolchain: [Clang, GCC, MSVC]
steps:
- uses: actions/checkout@v3
- name: Setup
uses: ./.github/actions/setup-cpp
with:
toolchain: ${{ matrix.toolchain }}
- name: Build capture
run: |
cmake -S . -B build -G Ninja -D CMAKE_BUILD_TYPE=Release
cmake --build build
working-directory: capture
clang-format: clang-format:
name: Format name: Format
runs-on: ubuntu-latest runs-on: ubuntu-latest

19
capture/CMakeLists.txt Normal file
View File

@@ -0,0 +1,19 @@
# 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.
cmake_minimum_required(VERSION 3.13...3.26)
project(capture)
set(CMAKE_VERBOSE_MAKEFILE on)
set(YOGA_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
include(${YOGA_ROOT}/cmake/project-defaults.cmake)
add_subdirectory(${YOGA_ROOT}/yoga ${CMAKE_CURRENT_BINARY_DIR}/yoga)
file(GLOB SOURCES CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
add_library(capture STATIC ${SOURCES})
target_link_libraries(capture yogacore)

228
capture/NodeToString.cpp Normal file
View File

@@ -0,0 +1,228 @@
/*
* 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.
*/
#include <stdarg.h>
#include <capture/NodeToString.h>
#include <algorithm>
namespace facebook::yoga {
static void indent(std::string& base, uint32_t level) {
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(
std::string& base,
const std::string key,
const float num) {
if (num != YGUndefined) {
appendFormattedString(base, "%s: %g; ", key.c_str(), num);
}
}
static void appendNumberIfNotUndefined(
std::string& base,
const std::string key,
const YGValue& number) {
if (number.unit != YGUnitUndefined) {
if (number.unit == YGUnitAuto) {
base.append(key + ": auto; ");
} else {
std::string unit = number.unit == YGUnitPoint ? "px" : "%%";
appendFormattedString(
base, "%s: %g%s; ", key.c_str(), number.value, unit.c_str());
}
}
}
static void appendNumberIfNotAuto(
std::string& base,
const std::string& key,
const YGValue& number) {
if (number.unit != YGUnitAuto) {
appendNumberIfNotUndefined(base, key, number);
}
}
static void appendNumberIfNotZero(
std::string& base,
const std::string& str,
const YGValue& number) {
if (number.unit == YGUnitAuto) {
base.append(str + ": auto; ");
} else if (number.value != 0) {
appendNumberIfNotUndefined(base, str, number);
}
}
template <auto Field>
static void
appendEdges(std::string& base, const std::string& key, YGNodeRef node) {
appendNumberIfNotZero(base, key + "-left", (*Field)(node, YGEdgeLeft));
appendNumberIfNotZero(base, key + "-right", (*Field)(node, YGEdgeRight));
appendNumberIfNotZero(base, key + "-top", (*Field)(node, YGEdgeTop));
appendNumberIfNotZero(base, key + "-bottom", (*Field)(node, YGEdgeBottom));
appendNumberIfNotZero(base, key + "-all", (*Field)(node, YGEdgeAll));
appendNumberIfNotZero(base, key + "-start", (*Field)(node, YGEdgeStart));
appendNumberIfNotZero(base, key + "-end", (*Field)(node, YGEdgeEnd));
appendNumberIfNotZero(
base, key + "-horizontal", (*Field)(node, YGEdgeHorizontal));
appendNumberIfNotZero(
base, key + "-vertical", (*Field)(node, YGEdgeVertical));
}
YGValue borderFloatToYGValue(YGNodeRef node, YGEdge edge) {
float val = YGNodeStyleGetBorder(node, edge);
YGUnit unit = val == YGUndefined ? YGUnitUndefined : YGUnitPoint;
return YGValue{val, unit};
}
void nodeToString(
std::string& str,
YGNodeRef node,
YGPrintOptions options,
uint32_t level) {
indent(str, level);
appendFormattedString(str, "<div ");
if ((options & YGPrintOptionsLayout) == YGPrintOptionsLayout) {
appendFormattedString(str, "layout=\"");
appendFormattedString(str, "width: %g; ", YGNodeLayoutGetWidth(node));
appendFormattedString(str, "height: %g; ", YGNodeLayoutGetHeight(node));
appendFormattedString(str, "top: %g; ", YGNodeLayoutGetTop(node));
appendFormattedString(str, "left: %g;", YGNodeLayoutGetLeft(node));
appendFormattedString(str, "\" ");
}
if ((options & YGPrintOptionsStyle) == YGPrintOptionsStyle) {
const YGNodeRef defaultNode = YGNodeNew();
appendFormattedString(str, "style=\"");
if (YGNodeStyleGetFlexDirection(node) !=
YGNodeStyleGetFlexDirection(defaultNode)) {
appendFormattedString(
str,
"flex-direction: %s; ",
YGFlexDirectionToString(YGNodeStyleGetFlexDirection(node)));
}
if (YGNodeStyleGetJustifyContent(node) !=
YGNodeStyleGetJustifyContent(defaultNode)) {
appendFormattedString(
str,
"justify-content: %s; ",
YGJustifyToString(YGNodeStyleGetJustifyContent(node)));
}
if (YGNodeStyleGetAlignItems(node) !=
YGNodeStyleGetAlignItems(defaultNode)) {
appendFormattedString(
str,
"align-items: %s; ",
YGAlignToString(YGNodeStyleGetAlignItems(node)));
}
if (YGNodeStyleGetAlignContent(node) !=
YGNodeStyleGetAlignContent(defaultNode)) {
appendFormattedString(
str,
"align-content: %s; ",
YGAlignToString(YGNodeStyleGetAlignContent(node)));
}
if (YGNodeStyleGetAlignSelf(node) != YGNodeStyleGetAlignSelf(defaultNode)) {
appendFormattedString(
str,
"align-self: %s; ",
YGAlignToString(YGNodeStyleGetAlignSelf(node)));
}
appendFloatOptionalIfDefined(
str, "flex-grow", YGNodeStyleGetFlexGrow(node));
appendFloatOptionalIfDefined(
str, "flex-shrink", YGNodeStyleGetFlexShrink(node));
appendNumberIfNotAuto(str, "flex-basis", YGNodeStyleGetFlexBasis(node));
appendFloatOptionalIfDefined(str, "flex", YGNodeStyleGetFlex(node));
if (YGNodeStyleGetFlexWrap(node) != YGNodeStyleGetFlexWrap(defaultNode)) {
appendFormattedString(
str, "flex-wrap: %s; ", YGWrapToString(YGNodeStyleGetFlexWrap(node)));
}
if (YGNodeStyleGetOverflow(node) != YGNodeStyleGetOverflow(defaultNode)) {
appendFormattedString(
str,
"overflow: %s; ",
YGOverflowToString(YGNodeStyleGetOverflow(node)));
}
if (YGNodeStyleGetDisplay(node) != YGNodeStyleGetDisplay(defaultNode)) {
appendFormattedString(
str, "display: %s; ", YGDisplayToString(YGNodeStyleGetDisplay(node)));
}
appendEdges<&YGNodeStyleGetMargin>(str, "margin", node);
appendEdges<&YGNodeStyleGetPadding>(str, "padding", node);
appendEdges<&borderFloatToYGValue>(str, "border", node);
if (YGNodeStyleGetGap(node, YGGutterAll) != YGUndefined) {
appendFloatOptionalIfDefined(
str, "gap", YGNodeStyleGetGap(node, YGGutterAll));
} else {
appendFloatOptionalIfDefined(
str, "column-gap", YGNodeStyleGetGap(node, YGGutterColumn));
appendFloatOptionalIfDefined(
str, "row-gap", YGNodeStyleGetGap(node, YGGutterRow));
}
appendNumberIfNotAuto(str, "width", YGNodeStyleGetWidth(node));
appendNumberIfNotAuto(str, "height", YGNodeStyleGetHeight(node));
appendNumberIfNotAuto(str, "max-width", YGNodeStyleGetMaxWidth(node));
appendNumberIfNotAuto(str, "max-height", YGNodeStyleGetMaxHeight(node));
appendNumberIfNotAuto(str, "min-width", YGNodeStyleGetMinWidth(node));
appendNumberIfNotAuto(str, "min-height", YGNodeStyleGetMinHeight(node));
if (YGNodeStyleGetPositionType(node) !=
YGNodeStyleGetPositionType(defaultNode)) {
appendFormattedString(
str,
"position: %s; ",
YGPositionTypeToString(YGNodeStyleGetPositionType(node)));
}
appendEdges<&YGNodeStyleGetPosition>(str, "position", node);
appendFormattedString(str, "\" ");
if (YGNodeHasMeasureFunc(node)) {
appendFormattedString(str, "has-custom-measure=\"true\"");
}
}
appendFormattedString(str, ">");
const size_t childCount = YGNodeGetChildCount(node);
if ((options & YGPrintOptionsChildren) == YGPrintOptionsChildren &&
childCount > 0) {
for (size_t i = 0; i < childCount; i++) {
appendFormattedString(str, "\n");
nodeToString(str, YGNodeGetChild(node, i), options, level + 1);
}
appendFormattedString(str, "\n");
indent(str, level);
}
appendFormattedString(str, "</div>");
}
} // namespace facebook::yoga

View File

@@ -5,25 +5,18 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
#ifdef DEBUG
#pragma once #pragma once
#include <string> #include <string>
#include <yoga/enums/PrintOptions.h> #include <yoga/Yoga.h>
#include <yoga/node/Node.h>
namespace facebook::yoga { namespace facebook::yoga {
void nodeToString( void nodeToString(
std::string& str, std::string& str,
const yoga::Node* node, YGNodeRef node,
PrintOptions options, YGPrintOptions options,
uint32_t level); uint32_t level);
void print(const yoga::Node* node, PrintOptions options);
} // namespace facebook::yoga } // namespace facebook::yoga
#endif

View File

@@ -26,7 +26,6 @@
#include <yoga/algorithm/TrailingPosition.h> #include <yoga/algorithm/TrailingPosition.h>
#include <yoga/debug/AssertFatal.h> #include <yoga/debug/AssertFatal.h>
#include <yoga/debug/Log.h> #include <yoga/debug/Log.h>
#include <yoga/debug/NodeToString.h>
#include <yoga/event/event.h> #include <yoga/event/event.h>
#include <yoga/node/Node.h> #include <yoga/node/Node.h>
#include <yoga/numeric/Comparison.h> #include <yoga/numeric/Comparison.h>
@@ -2366,14 +2365,6 @@ void calculateLayout(
node->setPosition( node->setPosition(
node->getLayout().direction(), ownerWidth, ownerHeight, ownerWidth); node->getLayout().direction(), ownerWidth, ownerHeight, ownerWidth);
roundLayoutResultsToPixelGrid(node, 0.0f, 0.0f); roundLayoutResultsToPixelGrid(node, 0.0f, 0.0f);
#ifdef DEBUG
if (node->getConfig()->shouldPrintTree()) {
yoga::print(
node,
PrintOptions::Layout | PrintOptions::Children | PrintOptions::Style);
}
#endif
} }
Event::publish<Event::LayoutPassEnd>(node, {&markerData}); Event::publish<Event::LayoutPassEnd>(node, {&markerData});

View File

@@ -32,14 +32,6 @@ bool Config::useWebDefaults() const {
return useWebDefaults_; return useWebDefaults_;
} }
void Config::setShouldPrintTree(bool printTree) {
printTree_ = printTree;
}
bool Config::shouldPrintTree() const {
return printTree_;
}
void Config::setExperimentalFeatureEnabled( void Config::setExperimentalFeatureEnabled(
ExperimentalFeature feature, ExperimentalFeature feature,
bool enabled) { bool enabled) {

View File

@@ -37,9 +37,6 @@ class YG_EXPORT Config : public ::YGConfig {
void setUseWebDefaults(bool useWebDefaults); void setUseWebDefaults(bool useWebDefaults);
bool useWebDefaults() const; bool useWebDefaults() const;
void setShouldPrintTree(bool printTree);
bool shouldPrintTree() const;
void setExperimentalFeatureEnabled(ExperimentalFeature feature, bool enabled); void setExperimentalFeatureEnabled(ExperimentalFeature feature, bool enabled);
bool isExperimentalFeatureEnabled(ExperimentalFeature feature) const; bool isExperimentalFeatureEnabled(ExperimentalFeature feature) const;
ExperimentalFeatureSet getEnabledExperiments() const; ExperimentalFeatureSet getEnabledExperiments() const;
@@ -74,7 +71,6 @@ class YG_EXPORT Config : public ::YGConfig {
YGLogger logger_; YGLogger logger_;
bool useWebDefaults_ : 1 = false; bool useWebDefaults_ : 1 = false;
bool printTree_ : 1 = false;
ExperimentalFeatureSet experimentalFeatures_{}; ExperimentalFeatureSet experimentalFeatures_{};
Errata errata_ = Errata::None; Errata errata_ = Errata::None;

View File

@@ -1,210 +0,0 @@
/*
* 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 <stdarg.h>
#include <yoga/debug/Log.h>
#include <yoga/debug/NodeToString.h>
#include <yoga/numeric/Comparison.h>
namespace facebook::yoga {
static void indent(std::string& base, uint32_t level) {
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(
std::string& base,
const std::string key,
const FloatOptional num) {
if (num.isDefined()) {
appendFormattedString(base, "%s: %g; ", key.c_str(), num.unwrap());
}
}
static void appendNumberIfNotUndefined(
std::string& base,
const std::string key,
const Style::Length& number) {
if (number.unit() != Unit::Undefined) {
if (number.unit() == Unit::Auto) {
base.append(key + ": auto; ");
} else {
std::string unit = number.unit() == Unit::Point ? "px" : "%%";
appendFormattedString(
base,
"%s: %g%s; ",
key.c_str(),
number.value().unwrap(),
unit.c_str());
}
}
}
static void appendNumberIfNotAuto(
std::string& base,
const std::string& key,
const Style::Length& number) {
if (number.unit() != Unit::Auto) {
appendNumberIfNotUndefined(base, key, number);
}
}
static void appendNumberIfNotZero(
std::string& base,
const std::string& str,
const Style::Length& number) {
if (number.unit() == Unit::Auto) {
base.append(str + ": auto; ");
} else if (!yoga::inexactEquals(number.value().unwrap(), 0)) {
appendNumberIfNotUndefined(base, str, number);
}
}
template <auto Field>
static void
appendEdges(std::string& base, const std::string& key, const Style& style) {
for (auto edge : ordinals<Edge>()) {
std::string str = key + "-" + toString(edge);
appendNumberIfNotZero(base, str, (style.*Field)(edge));
}
}
void nodeToString(
std::string& str,
const yoga::Node* node,
PrintOptions options,
uint32_t level) {
indent(str, level);
appendFormattedString(str, "<div ");
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) {
std::string str;
yoga::nodeToString(str, node, options, 0);
yoga::log(node, LogLevel::Debug, str.c_str());
}
} // namespace facebook::yoga
#endif