Yoga-internal.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the LICENSE
  5. * file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <algorithm>
  9. #include <array>
  10. #include <cmath>
  11. #include <vector>
  12. #include "CompactValue.h"
  13. #include "Yoga.h"
  14. using YGVector = std::vector<YGNodeRef>;
  15. YG_EXTERN_C_BEGIN
  16. WIN_EXPORT float YGRoundValueToPixelGrid(
  17. const float value,
  18. const float pointScaleFactor,
  19. const bool forceCeil,
  20. const bool forceFloor);
  21. void YGNodeCalculateLayoutWithContext(
  22. YGNodeRef node,
  23. float availableWidth,
  24. float availableHeight,
  25. YGDirection ownerDirection,
  26. void* layoutContext);
  27. void YGSetUsedCachedEntries(size_t);
  28. YG_EXTERN_C_END
  29. namespace facebook {
  30. namespace yoga {
  31. inline bool isUndefined(float value) {
  32. return std::isnan(value);
  33. }
  34. } // namespace yoga
  35. } // namespace facebook
  36. using namespace facebook;
  37. extern const std::array<YGEdge, 4> trailing;
  38. extern const std::array<YGEdge, 4> leading;
  39. extern const YGValue YGValueUndefined;
  40. extern const YGValue YGValueAuto;
  41. extern const YGValue YGValueZero;
  42. struct YGCachedMeasurement {
  43. float availableWidth;
  44. float availableHeight;
  45. YGMeasureMode widthMeasureMode;
  46. YGMeasureMode heightMeasureMode;
  47. float computedWidth;
  48. float computedHeight;
  49. YGCachedMeasurement()
  50. : availableWidth(0),
  51. availableHeight(0),
  52. widthMeasureMode((YGMeasureMode) -1),
  53. heightMeasureMode((YGMeasureMode) -1),
  54. computedWidth(-1),
  55. computedHeight(-1) {}
  56. bool operator==(YGCachedMeasurement measurement) const {
  57. bool isEqual = widthMeasureMode == measurement.widthMeasureMode &&
  58. heightMeasureMode == measurement.heightMeasureMode;
  59. if (!yoga::isUndefined(availableWidth) ||
  60. !yoga::isUndefined(measurement.availableWidth)) {
  61. isEqual = isEqual && availableWidth == measurement.availableWidth;
  62. }
  63. if (!yoga::isUndefined(availableHeight) ||
  64. !yoga::isUndefined(measurement.availableHeight)) {
  65. isEqual = isEqual && availableHeight == measurement.availableHeight;
  66. }
  67. if (!yoga::isUndefined(computedWidth) ||
  68. !yoga::isUndefined(measurement.computedWidth)) {
  69. isEqual = isEqual && computedWidth == measurement.computedWidth;
  70. }
  71. if (!yoga::isUndefined(computedHeight) ||
  72. !yoga::isUndefined(measurement.computedHeight)) {
  73. isEqual = isEqual && computedHeight == measurement.computedHeight;
  74. }
  75. return isEqual;
  76. }
  77. };
  78. // This value was chosen based on empiracle data. Even the most complicated
  79. // layouts should not require more than 16 entries to fit within the cache.
  80. #define YG_MAX_CACHED_RESULT_COUNT 16
  81. namespace facebook {
  82. namespace yoga {
  83. namespace detail {
  84. template <size_t Size>
  85. class Values {
  86. private:
  87. std::array<CompactValue, Size> values_;
  88. public:
  89. Values() = default;
  90. explicit Values(const YGValue& defaultValue) noexcept {
  91. values_.fill(defaultValue);
  92. }
  93. const CompactValue& operator[](size_t i) const noexcept {
  94. return values_[i];
  95. }
  96. CompactValue& operator[](size_t i) noexcept {
  97. return values_[i];
  98. }
  99. template <size_t I>
  100. YGValue get() const noexcept {
  101. return std::get<I>(values_);
  102. }
  103. template <size_t I>
  104. void set(YGValue& value) noexcept {
  105. std::get<I>(values_) = value;
  106. }
  107. template <size_t I>
  108. void set(YGValue&& value) noexcept {
  109. set<I>(value);
  110. }
  111. bool operator==(const Values& other) const noexcept {
  112. for (size_t i = 0; i < Size; ++i) {
  113. if (values_[i] != other.values_[i]) {
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. Values& operator=(const Values& other) = default;
  120. };
  121. } // namespace detail
  122. } // namespace yoga
  123. } // namespace facebook
  124. static const float kDefaultFlexGrow = 0.0f;
  125. static const float kDefaultFlexShrink = 0.0f;
  126. static const float kWebDefaultFlexShrink = 1.0f;
  127. extern bool YGFloatsEqual(const float a, const float b);
  128. extern facebook::yoga::detail::CompactValue YGComputedEdgeValue(
  129. const facebook::yoga::detail::Values<
  130. facebook::yoga::enums::count<YGEdge>()>& edges,
  131. YGEdge edge,
  132. facebook::yoga::detail::CompactValue defaultValue);