Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <react/cxxstableapi/FrameworksGuard.h>

#include <cmath>

#include <React/RendererCore.h>
#include <React/Utils.h>
#include <react/renderer/attributedstring/AttributedString.h>
Expand Down Expand Up @@ -72,6 +74,8 @@ class TextMeasureCacheKey final {
// are rounded to the pixel grid. Two otherwise-identical measures at different
// densities are not interchangeable, so the scale factor is part of the key.
Float pointScaleFactor{};
// Android initializes its measurement paint from the paragraph's base attributes.
bool includeBaseTextAttributes{};
};

// The Key type that is used for Line Measure Cache.
Expand All @@ -82,6 +86,7 @@ class LineMeasureCacheKey final {
AttributedString attributedString{};
ParagraphAttributes paragraphAttributes{};
Size size{};
bool includeBaseTextAttributes{};
};

/**
Expand Down Expand Up @@ -209,6 +214,47 @@ inline size_t attributedStringFragmentHashDisplayWise(const AttributedString::Fr
return facebook::react::hash_combine(fragment.string, fragment.textAttributes);
}

inline auto androidBaseTextPaintAttributes(const AttributedString &attributedString)
{
const auto &attributes = attributedString.getBaseTextAttributes();
// Android's MapBuffer omits NaN values, regardless of their representation.
auto optionalFloat = [](Float value) -> std::optional<Float> {
return std::isnan(value) ? std::nullopt : std::optional{value};
};

// Match TextLayoutManager.updateTextPaint and TextAttributeProps' SP conversion.
return std::tuple_cat(
std::tie(
attributes.fontFamily,
attributes.fontWeight,
attributes.fontStyle,
attributes.fontVariant,
attributes.fontVariationSettings,
attributes.allowFontScaling),
std::tuple{
optionalFloat(attributes.fontSize),
optionalFloat(attributes.maxFontSizeMultiplier),
optionalFloat(attributes.letterSpacing)});
}

inline void hashAndroidBaseTextPaintAttributes(size_t &seed, const AttributedString &attributedString)
{
std::apply(
[&seed](const auto &fontFamily, const auto &...optionals) {
hash_combine(seed, fontFamily);
hash_combine_optionals(seed, optionals...);
},
androidBaseTextPaintAttributes(attributedString));
}

template <typename Key>
inline bool areAndroidBaseTextPaintAttributesEquivalent(const Key &lhs, const Key &rhs)
{
return lhs.includeBaseTextAttributes == rhs.includeBaseTextAttributes &&
(!lhs.includeBaseTextAttributes ||
androidBaseTextPaintAttributes(lhs.attributedString) == androidBaseTextPaintAttributes(rhs.attributedString));
}

inline bool areAttributedStringsEquivalentLayoutWise(const AttributedString &lhs, const AttributedString &rhs)
{
auto &lhsFragment = lhs.getFragments();
Expand All @@ -230,6 +276,10 @@ inline bool areAttributedStringsEquivalentLayoutWise(const AttributedString &lhs

inline bool areAttributedStringsEquivalentDisplayWise(const AttributedString &lhs, const AttributedString &rhs)
{
if (androidBaseTextPaintAttributes(lhs) != androidBaseTextPaintAttributes(rhs)) {
return false;
}

auto &lhsFragment = lhs.getFragments();
auto &rhsFragment = rhs.getFragments();

Expand All @@ -247,14 +297,17 @@ inline bool areAttributedStringsEquivalentDisplayWise(const AttributedString &lh
return true;
}

inline size_t attributedStringHashLayoutWise(const AttributedString &attributedString)
inline size_t attributedStringHashLayoutWise(const AttributedString &attributedString, bool includeBaseTextAttributes)
{
auto seed = size_t{0};

for (const auto &fragment : attributedString.getFragments()) {
facebook::react::hash_combine(seed, attributedStringFragmentHashLayoutWise(fragment));
}

if (includeBaseTextAttributes) {
hashAndroidBaseTextPaintAttributes(seed, attributedString);
}
return seed;
}

Expand All @@ -266,18 +319,27 @@ inline size_t attributedStringHashDisplayWise(const AttributedString &attributed
facebook::react::hash_combine(seed, attributedStringFragmentHashDisplayWise(fragment));
}

hashAndroidBaseTextPaintAttributes(seed, attributedString);
return seed;
}

inline bool operator==(const TextMeasureCacheKey &lhs, const TextMeasureCacheKey &rhs)
{
if (!areAndroidBaseTextPaintAttributesEquivalent(lhs, rhs)) {
return false;
}

return areAttributedStringsEquivalentLayoutWise(lhs.attributedString, rhs.attributedString) &&
lhs.paragraphAttributes == rhs.paragraphAttributes && lhs.layoutConstraints == rhs.layoutConstraints &&
floatEquality(lhs.pointScaleFactor, rhs.pointScaleFactor);
}

inline bool operator==(const LineMeasureCacheKey &lhs, const LineMeasureCacheKey &rhs)
{
if (!areAndroidBaseTextPaintAttributesEquivalent(lhs, rhs)) {
return false;
}

return areAttributedStringsEquivalentLayoutWise(lhs.attributedString, rhs.attributedString) &&
lhs.paragraphAttributes == rhs.paragraphAttributes && lhs.size == rhs.size;
}
Expand All @@ -298,7 +360,7 @@ struct hash<facebook::react::TextMeasureCacheKey> {
size_t operator()(const facebook::react::TextMeasureCacheKey &key) const
{
return facebook::react::hash_combine(
attributedStringHashLayoutWise(key.attributedString),
attributedStringHashLayoutWise(key.attributedString, key.includeBaseTextAttributes),
key.paragraphAttributes,
key.layoutConstraints,
key.pointScaleFactor);
Expand All @@ -310,7 +372,9 @@ struct hash<facebook::react::LineMeasureCacheKey> {
size_t operator()(const facebook::react::LineMeasureCacheKey &key) const
{
return facebook::react::hash_combine(
attributedStringHashLayoutWise(key.attributedString), key.paragraphAttributes, key.size);
attributedStringHashLayoutWise(key.attributedString, key.includeBaseTextAttributes),
key.paragraphAttributes,
key.size);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ TextMeasurement TextLayoutManager::measure(
{.attributedString = attributedString,
.paragraphAttributes = paragraphAttributes,
.layoutConstraints = layoutConstraints,
.pointScaleFactor = layoutContext.pointScaleFactor},
.pointScaleFactor = layoutContext.pointScaleFactor,
.includeBaseTextAttributes = true},
std::move(measureText));

measurement.size = layoutConstraints.clamp(measurement.size);
Expand Down Expand Up @@ -288,7 +289,8 @@ LinesMeasurements TextLayoutManager::measureLines(
: lineMeasureCache_.get(
{.attributedString = attributedString,
.paragraphAttributes = paragraphAttributes,
.size = size},
.size = size,
.includeBaseTextAttributes = true},
std::move(doMeasureLines));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <array>
#include <cmath>
#include <limits>
#include <type_traits>

#include <react/renderer/textlayoutmanager/TextMeasureCache.h>
#include <react/renderer/textlayoutmanager/TextMeasurementRounding.h>
Expand All @@ -24,6 +26,118 @@ TEST(TextLayoutManagerTest, defaultTextAttributesAreLayoutEquivalent) {
textAttributesHashLayoutWise(lhs), textAttributesHashLayoutWise(rhs));
}

namespace {
template <typename Check>
void checkAndroidBasePaintKeys(
const AttributedString& first,
const AttributedString& second,
Check check) {
check(
TextMeasureCacheKey{
.attributedString = first, .includeBaseTextAttributes = true},
TextMeasureCacheKey{
.attributedString = second, .includeBaseTextAttributes = true});
check(
LineMeasureCacheKey{
.attributedString = first, .includeBaseTextAttributes = true},
LineMeasureCacheKey{
.attributedString = second, .includeBaseTextAttributes = true});
check(
PreparedTextCacheKey{.attributedString = first},
PreparedTextCacheKey{.attributedString = second});
}
} // namespace

TEST(TextLayoutManagerTest, androidBasePaintAffectsAllTextCacheKeys) {
AttributedString first;
TextAttributes inner;
inner.fontSize = 20;
first.appendFragment({.string = "Text", .textAttributes = inner});
first.setBaseTextAttributes(inner);

struct TestCase {
const char* name;
void (*change)(TextAttributes&);
};
const std::array<TestCase, 9> cases{{
{"fontFamily", [](TextAttributes& a) { a.fontFamily = "serif"; }},
{"fontWeight",
[](TextAttributes& a) { a.fontWeight = FontWeight::Weight700; }},
{"fontStyle", [](TextAttributes& a) { a.fontStyle = FontStyle::Italic; }},
{"fontVariant",
[](TextAttributes& a) { a.fontVariant = FontVariant::SmallCaps; }},
{"fontVariationSettings",
[](TextAttributes& a) { a.fontVariationSettings = "'wght' 700"; }},
{"allowFontScaling",
[](TextAttributes& a) { a.allowFontScaling = false; }},
{"fontSize", [](TextAttributes& a) { a.fontSize = 44; }},
{"maxFontSizeMultiplier",
[](TextAttributes& a) { a.maxFontSizeMultiplier = 2; }},
{"letterSpacing", [](TextAttributes& a) { a.letterSpacing = 1; }},
}};
for (const auto& testCase : cases) {
SCOPED_TRACE(testCase.name);
auto second = first;
auto base = first.getBaseTextAttributes();
testCase.change(base);
second.setBaseTextAttributes(base);
checkAndroidBasePaintKeys(
first, second, [](const auto& lhs, const auto& rhs) {
EXPECT_FALSE(lhs == rhs);
});
}
}

TEST(TextLayoutManagerTest, androidBaseFontSizeContributesToKeyHash) {
AttributedString first;
first.appendFragment({.string = "Text"});
auto second = first;
TextAttributes base;
base.fontSize = 20;
first.setBaseTextAttributes(base);
base.fontSize = 44;
second.setBaseTextAttributes(base);

checkAndroidBasePaintKeys(
first, second, [](const auto& lhs, const auto& rhs) {
using Key = std::decay_t<decltype(lhs)>;
EXPECT_NE(std::hash<Key>{}(lhs), std::hash<Key>{}(rhs));
});

first.setBaseTextAttributes(TextAttributes{});
base.fontSize = -std::numeric_limits<Float>::quiet_NaN();
second.setBaseTextAttributes(base);
checkAndroidBasePaintKeys(
first, second, [](const auto& lhs, const auto& rhs) {
using Key = std::decay_t<decltype(lhs)>;
EXPECT_EQ(lhs, rhs);
EXPECT_EQ(std::hash<Key>{}(lhs), std::hash<Key>{}(rhs));
});
}

TEST(TextLayoutManagerTest, basePaintDoesNotAffectKeysWithoutAndroidOptIn) {
AttributedString first;
first.appendFragment({.string = "Text"});
auto second = first;
TextAttributes base;
base.fontSize = 44;
second.setBaseTextAttributes(base);

const TextMeasureCacheKey sizeA{.attributedString = first};
const TextMeasureCacheKey sizeB{.attributedString = second};
EXPECT_EQ(sizeA, sizeB);
EXPECT_EQ(
std::hash<TextMeasureCacheKey>{}(sizeA),
std::hash<TextMeasureCacheKey>{}(sizeB));

const LineMeasureCacheKey lineA{.attributedString = first};
const LineMeasureCacheKey lineB{.attributedString = second};
EXPECT_EQ(lineA, lineB);
EXPECT_EQ(
std::hash<LineMeasureCacheKey>{}(lineA),
std::hash<LineMeasureCacheKey>{}(lineB));
}

TEST(TextLayoutManagerTest, maxFontSizeMultiplierAffectsLayoutCacheEquality) {
TextAttributes lhs;
TextAttributes rhs;
Expand Down
8 changes: 7 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ concept facebook::react::TextLayoutManagerWithPreparedTextLayout = requires(
AAssetManager* facebook::react::extractAssetManager(jni::alias_ref<JAssetManager::javaobject> assetManager);
JSI_EXPORT std::shared_ptr<facebook::react::TurboModule> facebook::react::FBReactNativeSpec_ModuleProvider(const std::string& moduleName, const facebook::react::JavaTurboModule::InitParams& params);
YGDirection facebook::react::yogaDirectionFromLayoutDirection(facebook::react::LayoutDirection direction);
auto facebook::react::androidBaseTextPaintAttributes(const facebook::react::AttributedString& attributedString);
bool facebook::react::areAttributedStringFragmentsEquivalentDisplayWise(const facebook::react::AttributedString::Fragment& lhs, const facebook::react::AttributedString::Fragment& rhs);
bool facebook::react::areAttributedStringFragmentsEquivalentLayoutWise(const facebook::react::AttributedString::Fragment& lhs, const facebook::react::AttributedString::Fragment& rhs);
bool facebook::react::areAttributedStringsEquivalentDisplayWise(const facebook::react::AttributedString& lhs, const facebook::react::AttributedString& rhs);
Expand Down Expand Up @@ -781,7 +782,7 @@ int facebook::react::toInt(const facebook::react::LayoutDirection& layoutDirecti
size_t facebook::react::attributedStringFragmentHashDisplayWise(const facebook::react::AttributedString::Fragment& fragment);
size_t facebook::react::attributedStringFragmentHashLayoutWise(const facebook::react::AttributedString::Fragment& fragment);
size_t facebook::react::attributedStringHashDisplayWise(const facebook::react::AttributedString& attributedString);
size_t facebook::react::attributedStringHashLayoutWise(const facebook::react::AttributedString& attributedString);
size_t facebook::react::attributedStringHashLayoutWise(const facebook::react::AttributedString& attributedString, bool includeBaseTextAttributes);
size_t facebook::react::textAttributesHashLayoutWise(const facebook::react::TextAttributes& textAttributes);
std::optional<facebook::react::BlendMode> facebook::react::blendModeFromString(std::string_view blendModeName);
std::optional<facebook::react::FilterType> facebook::react::filterTypeFromString(std::string_view filterName);
Expand Down Expand Up @@ -849,6 +850,8 @@ template <typename ComponentDescriptorT>
facebook::react::ComponentDescriptorProvider facebook::react::concreteComponentDescriptorProvider();
template <typename DestinationTimePointT, typename SourceTimePointT, typename DestnationClockT = typename DestinationTimePointT::clock, typename SourceClockT = typename SourceTimePointT::clock>
DestinationTimePointT facebook::react::clockCast(SourceTimePointT timePoint);
template <typename Key>
bool facebook::react::areAndroidBaseTextPaintAttributesEquivalent(const Key& lhs, const Key& rhs);
template <typename T, typename U = T>
T facebook::react::convertRawProp(const facebook::react::PropsParserContext& context, const facebook::react::RawProps& rawProps, const char* name, const T& sourceValue, const U& defaultValue);
template <typename T>
Expand Down Expand Up @@ -957,6 +960,7 @@ void facebook::react::fromRawValueShared(const facebook::react::ContextContainer
void facebook::react::fromString(const std::string& string, facebook::react::AccessibilityTraits& result);
void facebook::react::g_setNativeAnimatedNowTimestampFunction(facebook::react::TimePointFunction nowFunction);
void facebook::react::handleJSError(facebook::jsi::Runtime& runtime, const facebook::jsi::JSError& error, bool isFatal);
void facebook::react::hashAndroidBaseTextPaintAttributes(size_t& seed, const facebook::react::AttributedString& attributedString);
void facebook::react::parseProcessedBackgroundImage(const facebook::react::PropsParserContext& context, const facebook::react::RawValue& value, std::vector<facebook::react::BackgroundImage>& result);
void facebook::react::parseProcessedFontVariant(const facebook::react::PropsParserContext&, const facebook::react::RawValue& value, facebook::react::FontVariant& result);
void facebook::react::parseUnprocessedBackgroundImageList(const facebook::react::PropsParserContext& context, const std::vector<facebook::react::RawValue>& value, std::vector<facebook::react::BackgroundImage>& result);
Expand Down Expand Up @@ -2621,6 +2625,7 @@ struct facebook::react::LayoutableShadowNode::LayoutInspectingPolicy {
}

class facebook::react::LineMeasureCacheKey {
public bool includeBaseTextAttributes;
public facebook::react::AttributedString attributedString;
public facebook::react::ParagraphAttributes paragraphAttributes;
public facebook::react::Size size;
Expand Down Expand Up @@ -4044,6 +4049,7 @@ class facebook::react::TextLayoutManager::PreparedTextLayout {
}

class facebook::react::TextMeasureCacheKey {
public bool includeBaseTextAttributes;
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
Expand Down
5 changes: 4 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -3472,6 +3472,7 @@ NSArray<NSString*>* facebook::react::extractKeysFromFollyDynamic(const folly::dy
NSError* facebook::react::tryAndReturnError(const std::function<void()>& func);
NSString* facebook::react::deriveSourceURL(NSURL* url);
YGDirection facebook::react::yogaDirectionFromLayoutDirection(facebook::react::LayoutDirection direction);
auto facebook::react::androidBaseTextPaintAttributes(const facebook::react::AttributedString& attributedString);
bool facebook::react::areAttributedStringFragmentsEquivalentDisplayWise(const facebook::react::AttributedString::Fragment& lhs, const facebook::react::AttributedString::Fragment& rhs);
bool facebook::react::areAttributedStringFragmentsEquivalentLayoutWise(const facebook::react::AttributedString::Fragment& lhs, const facebook::react::AttributedString::Fragment& rhs);
bool facebook::react::areAttributedStringsEquivalentDisplayWise(const facebook::react::AttributedString& lhs, const facebook::react::AttributedString& rhs);
Expand Down Expand Up @@ -3534,7 +3535,7 @@ int facebook::react::toInt(const facebook::react::LayoutDirection& layoutDirecti
size_t facebook::react::attributedStringFragmentHashDisplayWise(const facebook::react::AttributedString::Fragment& fragment);
size_t facebook::react::attributedStringFragmentHashLayoutWise(const facebook::react::AttributedString::Fragment& fragment);
size_t facebook::react::attributedStringHashDisplayWise(const facebook::react::AttributedString& attributedString);
size_t facebook::react::attributedStringHashLayoutWise(const facebook::react::AttributedString& attributedString);
size_t facebook::react::attributedStringHashLayoutWise(const facebook::react::AttributedString& attributedString, bool includeBaseTextAttributes);
size_t facebook::react::textAttributesHashLayoutWise(const facebook::react::TextAttributes& textAttributes);
std::optional<facebook::react::BlendMode> facebook::react::blendModeFromString(std::string_view blendModeName);
std::optional<facebook::react::FilterType> facebook::react::filterTypeFromString(std::string_view filterName);
Expand Down Expand Up @@ -4814,6 +4815,7 @@ struct facebook::react::LayoutableShadowNode::LayoutInspectingPolicy {
}

class facebook::react::LineMeasureCacheKey {
public bool includeBaseTextAttributes;
public facebook::react::AttributedString attributedString;
public facebook::react::ParagraphAttributes paragraphAttributes;
public facebook::react::Size size;
Expand Down Expand Up @@ -6220,6 +6222,7 @@ class facebook::react::TextLayoutManager {
}

class facebook::react::TextMeasureCacheKey {
public bool includeBaseTextAttributes;
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
Expand Down
Loading
Loading