| 1 | /* |
| 2 | * Copyright (C) 2016 The Qt Company Ltd. |
| 3 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 4 | * |
| 5 | * SPDX-License-Identifier: MIT |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <array> |
| 12 | #include <cmath> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <yoga/Yoga.h> |
| 16 | |
| 17 | #include "CompactValue.h" |
| 18 | |
| 19 | USE_QT_YOGA_NAMESPACE |
| 20 | |
| 21 | using YGVector = std::vector<YGNodeRef>; |
| 22 | |
| 23 | QT_YOGA_NAMESPACE_BEGIN |
| 24 | |
| 25 | YG_EXTERN_C_BEGIN |
| 26 | |
| 27 | void YGNodeCalculateLayoutWithContext( |
| 28 | YGNodeRef node, |
| 29 | float availableWidth, |
| 30 | float availableHeight, |
| 31 | YGDirection ownerDirection, |
| 32 | void* layoutContext); |
| 33 | |
| 34 | // Deallocates a Yoga Node. Unlike YGNodeFree, does not remove the node from |
| 35 | // its parent or children. |
| 36 | void YGNodeDeallocate(YGNodeRef node); |
| 37 | |
| 38 | YG_EXTERN_C_END |
| 39 | |
| 40 | namespace facebook { |
| 41 | namespace yoga { |
| 42 | |
| 43 | inline bool isUndefined(float value) { |
| 44 | return std::isnan(x: value); |
| 45 | } |
| 46 | |
| 47 | inline bool isUndefined(double value) { |
| 48 | return std::isnan(x: value); |
| 49 | } |
| 50 | |
| 51 | void throwLogicalErrorWithMessage(const char* message); |
| 52 | |
| 53 | } // namespace yoga |
| 54 | } // namespace facebook |
| 55 | |
| 56 | extern const std::array<YGEdge, 4> trailing; |
| 57 | extern const std::array<YGEdge, 4> leading; |
| 58 | extern const YGValue YGValueUndefined; |
| 59 | extern const YGValue YGValueAuto; |
| 60 | extern const YGValue YGValueZero; |
| 61 | |
| 62 | struct YGCachedMeasurement { |
| 63 | float availableWidth; |
| 64 | float availableHeight; |
| 65 | YGMeasureMode widthMeasureMode; |
| 66 | YGMeasureMode heightMeasureMode; |
| 67 | |
| 68 | float computedWidth; |
| 69 | float computedHeight; |
| 70 | |
| 71 | YGCachedMeasurement() |
| 72 | : availableWidth(-1), |
| 73 | availableHeight(-1), |
| 74 | widthMeasureMode(YGMeasureModeUndefined), |
| 75 | heightMeasureMode(YGMeasureModeUndefined), |
| 76 | computedWidth(-1), |
| 77 | computedHeight(-1) {} |
| 78 | |
| 79 | bool operator==(YGCachedMeasurement measurement) const { |
| 80 | using namespace facebook; |
| 81 | |
| 82 | bool isEqual = widthMeasureMode == measurement.widthMeasureMode && |
| 83 | heightMeasureMode == measurement.heightMeasureMode; |
| 84 | |
| 85 | if (!yoga::isUndefined(value: availableWidth) || |
| 86 | !yoga::isUndefined(value: measurement.availableWidth)) { |
| 87 | isEqual = isEqual && availableWidth == measurement.availableWidth; |
| 88 | } |
| 89 | if (!yoga::isUndefined(value: availableHeight) || |
| 90 | !yoga::isUndefined(value: measurement.availableHeight)) { |
| 91 | isEqual = isEqual && availableHeight == measurement.availableHeight; |
| 92 | } |
| 93 | if (!yoga::isUndefined(value: computedWidth) || |
| 94 | !yoga::isUndefined(value: measurement.computedWidth)) { |
| 95 | isEqual = isEqual && computedWidth == measurement.computedWidth; |
| 96 | } |
| 97 | if (!yoga::isUndefined(value: computedHeight) || |
| 98 | !yoga::isUndefined(value: measurement.computedHeight)) { |
| 99 | isEqual = isEqual && computedHeight == measurement.computedHeight; |
| 100 | } |
| 101 | |
| 102 | return isEqual; |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | // This value was chosen based on empirical data: |
| 107 | // 98% of analyzed layouts require less than 8 entries. |
| 108 | #define YG_MAX_CACHED_RESULT_COUNT 8 |
| 109 | |
| 110 | namespace facebook { |
| 111 | namespace yoga { |
| 112 | namespace detail { |
| 113 | |
| 114 | template <size_t Size> |
| 115 | class Values { |
| 116 | private: |
| 117 | std::array<CompactValue, Size> values_; |
| 118 | |
| 119 | public: |
| 120 | Values() = default; |
| 121 | Values(const Values& other) = default; |
| 122 | |
| 123 | explicit Values(const YGValue& defaultValue) noexcept { |
| 124 | values_.fill(defaultValue); |
| 125 | } |
| 126 | |
| 127 | const CompactValue& operator[](size_t i) const noexcept { return values_[i]; } |
| 128 | CompactValue& operator[](size_t i) noexcept { return values_[i]; } |
| 129 | |
| 130 | template <size_t I> |
| 131 | YGValue get() const noexcept { |
| 132 | return std::get<I>(values_); |
| 133 | } |
| 134 | |
| 135 | template <size_t I> |
| 136 | void set(YGValue& value) noexcept { |
| 137 | std::get<I>(values_) = value; |
| 138 | } |
| 139 | |
| 140 | template <size_t I> |
| 141 | void set(YGValue&& value) noexcept { |
| 142 | set<I>(value); |
| 143 | } |
| 144 | |
| 145 | bool operator==(const Values& other) const noexcept { |
| 146 | for (size_t i = 0; i < Size; ++i) { |
| 147 | if (values_[i] != other.values_[i]) { |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | Values& operator=(const Values& other) = default; |
| 155 | }; |
| 156 | } // namespace detail |
| 157 | } // namespace yoga |
| 158 | } // namespace facebook |
| 159 | |
| 160 | static const float kDefaultFlexGrow = 0.0f; |
| 161 | static const float kDefaultFlexShrink = 0.0f; |
| 162 | static const float kWebDefaultFlexShrink = 1.0f; |
| 163 | |
| 164 | extern bool YGFloatsEqual(const float a, const float b); |
| 165 | |
| 166 | QT_YOGA_NAMESPACE_END |
| 167 | |