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
93 lines
2.8 KiB
Java
93 lines
2.8 KiB
Java
/*
|
|
* 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.
|
|
*/
|
|
|
|
package com.facebook.yoga.utils;
|
|
|
|
import com.facebook.yoga.YogaMeasureMode;
|
|
import com.facebook.yoga.YogaMeasureOutput;
|
|
import com.facebook.yoga.YogaMeasureFunction;
|
|
import com.facebook.yoga.YogaNode;
|
|
|
|
public class TestUtils {
|
|
|
|
public static class intrinsicMeasureFunction implements YogaMeasureFunction {
|
|
|
|
private static float widthPerChar = 10;
|
|
private static float heightPerChar = 10;
|
|
|
|
@Override
|
|
public long measure(
|
|
YogaNode node,
|
|
float width,
|
|
YogaMeasureMode widthMode,
|
|
float height,
|
|
YogaMeasureMode heightMode) {
|
|
String text = (String) node.getData();
|
|
float measuredWidth;
|
|
float measuredHeight;
|
|
|
|
if (widthMode == YogaMeasureMode.EXACTLY) {
|
|
measuredWidth = width;
|
|
} else if (widthMode == YogaMeasureMode.AT_MOST) {
|
|
measuredWidth = Math.min(text.length() * widthPerChar, width);
|
|
} else {
|
|
measuredWidth = text.length() * widthPerChar;
|
|
}
|
|
|
|
if (heightMode == YogaMeasureMode.EXACTLY) {
|
|
measuredHeight = height;
|
|
} else if (heightMode == YogaMeasureMode.AT_MOST) {
|
|
measuredHeight =
|
|
Math.min(
|
|
caclulateHeight(text, Math.max(measuredWidth, getWidestWordWidth(text))), height);
|
|
} else {
|
|
measuredHeight = caclulateHeight(text, Math.max(measuredWidth, getWidestWordWidth(text)));
|
|
}
|
|
|
|
return YogaMeasureOutput.make(measuredWidth, measuredHeight);
|
|
}
|
|
|
|
static float getWidestWordWidth(String text) {
|
|
int widestWordLength = 0;
|
|
String[] words = text.split(" ");
|
|
for (String word : words) {
|
|
int wordLength = word.length();
|
|
if (widestWordLength < wordLength) {
|
|
widestWordLength = wordLength;
|
|
}
|
|
}
|
|
return (float) widestWordLength * widthPerChar;
|
|
}
|
|
|
|
static float caclulateHeight(String text, float measuredWidth) {
|
|
if (text.length() * widthPerChar <= measuredWidth) {
|
|
return heightPerChar;
|
|
}
|
|
|
|
String[] words = text.split(" ");
|
|
float lines = 1;
|
|
float currentLineLength = 0;
|
|
for (String word : words) {
|
|
float wordWidth = 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;
|
|
}
|
|
}
|
|
}
|