Skip to content
Open
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 @@ -15,6 +15,23 @@
namespace facebook::react {

bool ParagraphAttributes::operator==(const ParagraphAttributes& rhs) const {
return equals<false>(rhs);
}

bool ParagraphAttributes::equalsForTextCache(
const ParagraphAttributes& rhs) const {
return equals<true>(rhs);
}

template <bool Exact>
bool ParagraphAttributes::equals(const ParagraphAttributes& rhs) const {
const auto equal = [](Float lhs, Float rhs) {
if constexpr (Exact) {
return exactFloatEquality(lhs, rhs);
}
return floatEquality(lhs, rhs);
};

return std::tie(
maximumNumberOfLines,
ellipsizeMode,
Expand All @@ -37,8 +54,8 @@ bool ParagraphAttributes::operator==(const ParagraphAttributes& rhs) const {
rhs.lineBreak,
rhs.wordBreak,
rhs.textAlignVertical) &&
floatEquality(minimumFontSize, rhs.minimumFontSize) &&
floatEquality(minimumFontScale, rhs.minimumFontScale);
equal(minimumFontSize, rhs.minimumFontSize) &&
equal(minimumFontScale, rhs.minimumFontScale);
}

#pragma mark - DebugStringConvertible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <React/RendererDebug.h>
#include <React/Utils.h>
#include <react/renderer/attributedstring/primitives.h>
#include <react/utils/FloatComparison.h>

namespace facebook::react {

Expand Down Expand Up @@ -99,12 +100,17 @@ class ParagraphAttributes : public DebugStringConvertible {
std::optional<TextAlignmentVertical> textAlignVertical{};

bool operator==(const ParagraphAttributes &rhs) const;
bool equalsForTextCache(const ParagraphAttributes &rhs) const;

#pragma mark - DebugStringConvertible

#if RN_DEBUG_STRING_CONVERTIBLE
SharedDebugStringConvertibleList getDebugProps() const override;
#endif

private:
template <bool Exact>
bool equals(const ParagraphAttributes &rhs) const;
};

} // namespace facebook::react
Expand All @@ -121,12 +127,12 @@ struct hash<facebook::react::ParagraphAttributes> {
attributes.textBreakStrategy,
attributes.textWidthMode,
attributes.adjustsFontSizeToFit,
attributes.minimumFontSize,
facebook::react::canonicalizeNaN(attributes.minimumFontSize),
attributes.includeFontPadding,
attributes.android_hyphenationFrequency,
attributes.lineBreak,
attributes.wordBreak,
attributes.minimumFontScale,
facebook::react::canonicalizeNaN(attributes.minimumFontScale),
attributes.textAlignVertical);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,32 @@ void TextAttributes::apply(TextAttributes textAttributes) {
#pragma mark - Operators

bool TextAttributes::operator==(const TextAttributes& rhs) const {
return textShadowOffset == rhs.textShadowOffset && equals<false>(rhs);
}

bool TextAttributes::equalsForTextCache(const TextAttributes& rhs) const {
if (textShadowOffset.has_value() != rhs.textShadowOffset.has_value()) {
return false;
}
if (textShadowOffset &&
(!exactFloatEquality(
textShadowOffset->width, rhs.textShadowOffset->width) ||
!exactFloatEquality(
textShadowOffset->height, rhs.textShadowOffset->height))) {
return false;
}
return lineBreakMode == rhs.lineBreakMode && equals<true>(rhs);
}

template <bool Exact>
bool TextAttributes::equals(const TextAttributes& rhs) const {
const auto equal = [](Float lhs, Float rhs) {
if constexpr (Exact) {
return exactFloatEquality(lhs, rhs);
}
return floatEquality(lhs, rhs);
};

return std::tie(
foregroundColor,
backgroundColor,
Expand All @@ -149,7 +175,6 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
textDecorationColor,
textDecorationLineType,
textDecorationStyle,
textShadowOffset,
textShadowColor,
isHighlighted,
isPressable,
Expand All @@ -174,7 +199,6 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
rhs.textDecorationColor,
rhs.textDecorationLineType,
rhs.textDecorationStyle,
rhs.textShadowOffset,
rhs.textShadowColor,
rhs.isHighlighted,
rhs.isPressable,
Expand All @@ -183,13 +207,12 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
rhs.role,
rhs.textTransform,
rhs.textEffects) &&
floatEquality(maxFontSizeMultiplier, rhs.maxFontSizeMultiplier) &&
floatEquality(opacity, rhs.opacity) &&
floatEquality(fontSize, rhs.fontSize) &&
floatEquality(fontSizeMultiplier, rhs.fontSizeMultiplier) &&
floatEquality(letterSpacing, rhs.letterSpacing) &&
floatEquality(lineHeight, rhs.lineHeight) &&
floatEquality(textShadowRadius, rhs.textShadowRadius);
equal(maxFontSizeMultiplier, rhs.maxFontSizeMultiplier) &&
equal(opacity, rhs.opacity) && equal(fontSize, rhs.fontSize) &&
equal(fontSizeMultiplier, rhs.fontSizeMultiplier) &&
equal(letterSpacing, rhs.letterSpacing) &&
equal(lineHeight, rhs.lineHeight) &&
equal(textShadowRadius, rhs.textShadowRadius);
}

TextAttributes TextAttributes::defaultTextAttributes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <React/View.h>
#include <folly/dynamic.h>
#include <react/renderer/attributedstring/primitives.h>
#include <react/utils/FloatComparison.h>

namespace facebook::react {

Expand Down Expand Up @@ -107,12 +108,17 @@ class TextAttributes : public DebugStringConvertible {
#pragma mark - Operators

bool operator==(const TextAttributes &rhs) const;
bool equalsForTextCache(const TextAttributes &rhs) const;

#pragma mark - DebugStringConvertible

#if RN_DEBUG_STRING_CONVERTIBLE
SharedDebugStringConvertibleList getDebugProps() const override;
#endif

private:
template <bool Exact>
bool equals(const TextAttributes &rhs) const;
};

} // namespace facebook::react
Expand Down Expand Up @@ -142,17 +148,22 @@ struct hash<facebook::react::TextAttributes> {
seed,
textAttributes.foregroundColor,
textAttributes.backgroundColor,
textAttributes.opacity,
facebook::react::canonicalizeNaN(textAttributes.opacity),
textAttributes.fontFamily,
textAttributes.fontSize,
textAttributes.maxFontSizeMultiplier,
textAttributes.fontSizeMultiplier,
textAttributes.letterSpacing,
textAttributes.lineHeight,
textAttributes.textShadowRadius,
facebook::react::canonicalizeNaN(textAttributes.fontSize),
facebook::react::canonicalizeNaN(textAttributes.maxFontSizeMultiplier),
facebook::react::canonicalizeNaN(textAttributes.fontSizeMultiplier),
facebook::react::canonicalizeNaN(textAttributes.letterSpacing),
facebook::react::canonicalizeNaN(textAttributes.lineHeight),
facebook::react::canonicalizeNaN(textAttributes.textShadowRadius),
textAttributes.textDecorationColor,
textAttributes.textShadowColor,
textEffectsHash);
auto shadowOffset = textAttributes.textShadowOffset;
if (shadowOffset) {
shadowOffset->width = facebook::react::canonicalizeNaN(shadowOffset->width);
shadowOffset->height = facebook::react::canonicalizeNaN(shadowOffset->height);
}
facebook::react::hash_combine_optionals(
seed,
textAttributes.fontWeight,
Expand All @@ -167,7 +178,7 @@ struct hash<facebook::react::TextAttributes> {
textAttributes.lineBreakMode,
textAttributes.textDecorationLineType,
textAttributes.textDecorationStyle,
textAttributes.textShadowOffset,
shadowOffset,
textAttributes.isHighlighted,
textAttributes.isPressable,
textAttributes.layoutDirection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ inline bool areTextAttributesEquivalentLayoutWise(const TextAttributes &lhs, con
rhs.lineBreakMode,
rhs.layoutDirection,
rhs.alignment) &&
floatEquality(lhs.fontSize, rhs.fontSize) && floatEquality(lhs.fontSizeMultiplier, rhs.fontSizeMultiplier) &&
floatEquality(lhs.letterSpacing, rhs.letterSpacing) && floatEquality(lhs.lineHeight, rhs.lineHeight) &&
floatEquality(lhs.maxFontSizeMultiplier, rhs.maxFontSizeMultiplier) && lhs.textEffects == rhs.textEffects;
exactFloatEquality(lhs.fontSize, rhs.fontSize) &&
exactFloatEquality(lhs.fontSizeMultiplier, rhs.fontSizeMultiplier) &&
exactFloatEquality(lhs.letterSpacing, rhs.letterSpacing) && exactFloatEquality(lhs.lineHeight, rhs.lineHeight) &&
exactFloatEquality(lhs.maxFontSizeMultiplier, rhs.maxFontSizeMultiplier) && lhs.textEffects == rhs.textEffects;
}

inline size_t textAttributesHashLayoutWise(const TextAttributes &textAttributes)
Expand All @@ -165,11 +166,11 @@ inline size_t textAttributesHashLayoutWise(const TextAttributes &textAttributes)
facebook::react::hash_combine(
seed,
textAttributes.fontFamily,
textAttributes.fontSize,
textAttributes.fontSizeMultiplier,
textAttributes.maxFontSizeMultiplier,
textAttributes.letterSpacing,
textAttributes.lineHeight);
canonicalizeNaN(textAttributes.fontSize),
canonicalizeNaN(textAttributes.fontSizeMultiplier),
canonicalizeNaN(textAttributes.maxFontSizeMultiplier),
canonicalizeNaN(textAttributes.letterSpacing),
canonicalizeNaN(textAttributes.lineHeight));
facebook::react::hash_combine_optionals(
seed,
textAttributes.fontWeight,
Expand All @@ -196,7 +197,7 @@ inline bool areAttributedStringFragmentsEquivalentDisplayWise(
const AttributedString::Fragment &lhs,
const AttributedString::Fragment &rhs)
{
return lhs.isContentEqual(rhs) &&
return lhs.string == rhs.string && lhs.textAttributes.equalsForTextCache(rhs.textAttributes) &&
// LayoutMetrics of an attachment fragment affects the size of a measured
// attributed string.
(!lhs.isAttachment() || (lhs.parentShadowView.layoutMetrics == rhs.parentShadowView.layoutMetrics));
Expand Down Expand Up @@ -281,21 +282,21 @@ inline size_t attributedStringHashDisplayWise(const AttributedString &attributed
inline bool operator==(const TextMeasureCacheKey &lhs, const TextMeasureCacheKey &rhs)
{
return areAttributedStringsEquivalentLayoutWise(lhs.attributedString, rhs.attributedString) &&
lhs.paragraphAttributes == rhs.paragraphAttributes && lhs.layoutConstraints == rhs.layoutConstraints &&
floatEquality(lhs.pointScaleFactor, rhs.pointScaleFactor);
lhs.paragraphAttributes.equalsForTextCache(rhs.paragraphAttributes) &&
lhs.layoutConstraints == rhs.layoutConstraints && exactFloatEquality(lhs.pointScaleFactor, rhs.pointScaleFactor);
}

inline bool operator==(const LineMeasureCacheKey &lhs, const LineMeasureCacheKey &rhs)
{
return areAttributedStringsEquivalentLayoutWise(lhs.attributedString, rhs.attributedString) &&
lhs.paragraphAttributes == rhs.paragraphAttributes && lhs.size == rhs.size;
lhs.paragraphAttributes.equalsForTextCache(rhs.paragraphAttributes) && lhs.size == rhs.size;
}

inline bool operator==(const PreparedTextCacheKey &lhs, const PreparedTextCacheKey &rhs)
{
return areAttributedStringsEquivalentDisplayWise(lhs.attributedString, rhs.attributedString) &&
lhs.paragraphAttributes == rhs.paragraphAttributes && lhs.layoutConstraints == rhs.layoutConstraints &&
floatEquality(lhs.pointScaleFactor, rhs.pointScaleFactor);
lhs.paragraphAttributes.equalsForTextCache(rhs.paragraphAttributes) &&
lhs.layoutConstraints == rhs.layoutConstraints && exactFloatEquality(lhs.pointScaleFactor, rhs.pointScaleFactor);
}

} // namespace facebook::react
Expand All @@ -310,7 +311,7 @@ struct hash<facebook::react::TextMeasureCacheKey> {
attributedStringHashLayoutWise(key.attributedString),
key.paragraphAttributes,
key.layoutConstraints,
key.pointScaleFactor);
facebook::react::canonicalizeNaN(key.pointScaleFactor));
}
};

Expand All @@ -331,7 +332,7 @@ struct hash<facebook::react::PreparedTextCacheKey> {
attributedStringHashDisplayWise(key.attributedString),
key.paragraphAttributes,
key.layoutConstraints,
key.pointScaleFactor);
facebook::react::canonicalizeNaN(key.pointScaleFactor));
}
};

Expand Down
Loading
Loading