Add test support for intrinsic sized nodes (#1671)

Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1671

This diff adds support for intrinsic sizing in generated tests. This is done by importing a testing font called "Ahem" which, as used, has an exact width and height of 10px for each character. Support has been added for C++, Java, and Javascript generated tests.

Reviewed By: NickGerleman

Differential Revision: D58307002

fbshipit-source-id: e1dcc1e03310d35a32e0c70f71994880d8f7de55
This commit is contained in:
Maddie Lord
2024-06-26 10:00:17 -07:00
committed by Facebook GitHub Bot
parent 0888b20098
commit 1a3bc6bfbc
77 changed files with 2284 additions and 119 deletions

View File

@@ -7,8 +7,14 @@
#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 {
@@ -58,4 +64,95 @@ 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