File: TestUtil.cpp

package info (click to toggle)
yoga 3.2.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 984 kB
  • sloc: cpp: 12,985; python: 260; sh: 32; makefile: 6
file content (158 lines) | stat: -rw-r--r-- 3,873 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * 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 "TestUtil.h"

#include <yoga/YGEnums.h>
#include <yoga/YGNode.h>
#include <yoga/event/event.h>
#include <yoga/node/Node.h>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>

namespace facebook::yoga::test {

int nodeInstanceCount = 0;

namespace {

void yogaEventSubscriber(
    YGNodeConstRef /*node*/,
    Event::Type eventType,
    const Event::Data& /*eventData*/) {
  switch (eventType) {
    case Event::NodeAllocation:
      nodeInstanceCount++;
      break;
    case Event::NodeDeallocation:
      nodeInstanceCount--;
      break;
    default:
      break;
  }
}
} // namespace

void TestUtil::startCountingNodes() {
  nodeInstanceCount = 0;
  Event::subscribe(yogaEventSubscriber);
}

int TestUtil::nodeCount() {
  return nodeInstanceCount;
}

int TestUtil::stopCountingNodes() {
  Event::reset();
  auto prev = nodeInstanceCount;
  nodeInstanceCount = 0;
  return prev;
}

ScopedEventSubscription::ScopedEventSubscription(
    std::function<Event::Subscriber>&& s) {
  Event::subscribe(std::move(s));
}

ScopedEventSubscription::~ScopedEventSubscription() {
  Event::reset();
}

YGSize IntrinsicSizeMeasure(
    YGNodeConstRef node,
    float width,
    YGMeasureMode widthMode,
    float height,
    YGMeasureMode heightMode) {
  std::string_view innerText((char*)YGNodeGetContext(node));
  float heightPerChar = 10;
  float widthPerChar = 10;
  float measuredWidth;
  float measuredHeight;

  if (widthMode == YGMeasureModeExactly) {
    measuredWidth = width;
  } else if (widthMode == YGMeasureModeAtMost) {
    measuredWidth = std::min((float)innerText.size() * widthPerChar, width);
  } else {
    measuredWidth = (float)innerText.size() * widthPerChar;
  }

  if (heightMode == YGMeasureModeExactly) {
    measuredHeight = height;
  } else if (heightMode == YGMeasureModeAtMost) {
    measuredHeight = std::min(
        calculateHeight(
            innerText,
            std::max(longestWordWidth(innerText, widthPerChar), measuredWidth),
            widthPerChar,
            heightPerChar),
        height);
  } else {
    measuredHeight = calculateHeight(
        innerText,
        std::max(longestWordWidth(innerText, widthPerChar), measuredWidth),
        widthPerChar,
        heightPerChar);
  }

  return YGSize{measuredWidth, measuredHeight};
}

float longestWordWidth(std::string_view text, float widthPerChar) {
  int maxLength = 0;
  int currentLength = 0;
  for (auto c : text) {
    if (c == ' ') {
      maxLength = std::max(currentLength, maxLength);
      currentLength = 0;
    } else {
      currentLength++;
    }
  }
  return (float)std::max(currentLength, maxLength) * widthPerChar;
}

float calculateHeight(
    std::string_view text,
    float measuredWidth,
    float widthPerChar,
    float heightPerChar) {
  if ((float)text.size() * widthPerChar <= measuredWidth) {
    return heightPerChar;
  }

  std::vector<std::string> words;
  std::istringstream iss((std::string)text);
  std::string currentWord;
  while (getline(iss, currentWord, ' ')) {
    words.push_back(currentWord);
  }

  float lines = 1;
  float currentLineLength = 0;
  for (const std::string& word : words) {
    float wordWidth = (float)word.length() * widthPerChar;
    if (wordWidth > measuredWidth) {
      if (currentLineLength > 0) {
        lines++;
      }
      lines++;
      currentLineLength = 0;
    } else if (currentLineLength + wordWidth <= measuredWidth) {
      currentLineLength += wordWidth + widthPerChar;
    } else {
      lines++;
      currentLineLength = wordWidth + widthPerChar;
    }
  }
  return (currentLineLength == 0 ? lines - 1 : lines) * heightPerChar;
}

} // namespace facebook::yoga::test