Skip to content

Commit

Permalink
Add android_hyphenationStrategy to ParagraphAttributes
Browse files Browse the repository at this point in the history
Summary:
Expose android_hyphenationFrequency in Fabric as part of ParagraphAttributes

Changelog: [Internal]

Reviewed By: JoshuaGross

Differential Revision: D30583215

fbshipit-source-id: f4e9e9d6ea8efcfc10db29e1fbd651462f442837
  • Loading branch information
genkikondo authored and facebook-github-bot committed Aug 27, 2021
1 parent 732ac17 commit ca60be8
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ bool ParagraphAttributes::operator==(const ParagraphAttributes &rhs) const {
ellipsizeMode,
textBreakStrategy,
adjustsFontSizeToFit,
includeFontPadding) ==
includeFontPadding,
android_hyphenationFrequency) ==
std::tie(
rhs.maximumNumberOfLines,
rhs.ellipsizeMode,
rhs.textBreakStrategy,
rhs.adjustsFontSizeToFit,
rhs.includeFontPadding) &&
rhs.includeFontPadding,
rhs.android_hyphenationFrequency) &&
floatEquality(minimumFontSize, rhs.minimumFontSize) &&
floatEquality(maximumFontSize, rhs.maximumFontSize);
}
Expand All @@ -47,7 +49,10 @@ SharedDebugStringConvertibleList ParagraphAttributes::getDebugProps() const {
debugStringConvertibleItem("adjustsFontSizeToFit", adjustsFontSizeToFit),
debugStringConvertibleItem("minimumFontSize", minimumFontSize),
debugStringConvertibleItem("maximumFontSize", maximumFontSize),
debugStringConvertibleItem("includeFontPadding", includeFontPadding)};
debugStringConvertibleItem("includeFontPadding", includeFontPadding),
debugStringConvertibleItem(
"android_hyphenationFrequency",
android_hyphenationFrequency)};
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class ParagraphAttributes : public DebugStringConvertible {
*/
EllipsizeMode ellipsizeMode{};

/*
* (Android only) Break strategy for breaking paragraphs into lines.
*/
TextBreakStrategy textBreakStrategy{};

/*
Expand All @@ -55,6 +58,11 @@ class ParagraphAttributes : public DebugStringConvertible {
*/
bool includeFontPadding{true};

/*
* (Android only) Frequency of automatic hyphenation to use when determining word breaks.
*/
HyphenationFrequency android_hyphenationFrequency{};

/*
* In case of font size adjustment enabled, defines minimum and maximum
* font sizes.
Expand Down Expand Up @@ -89,7 +97,8 @@ struct hash<facebook::react::ParagraphAttributes> {
attributes.adjustsFontSizeToFit,
attributes.minimumFontSize,
attributes.maximumFontSize,
attributes.includeFontPadding);
attributes.includeFontPadding,
attributes.android_hyphenationFrequency);
}
};
} // namespace std
51 changes: 51 additions & 0 deletions ReactCommon/react/renderer/attributedstring/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,48 @@ inline void fromRawValue(
result = AccessibilityRole::None;
}

inline std::string toString(const HyphenationFrequency &hyphenationFrequency) {
switch (hyphenationFrequency) {
case HyphenationFrequency::None:
return "none";
case HyphenationFrequency::Normal:
return "normal";
case HyphenationFrequency::Full:
return "full";
}

LOG(ERROR) << "Unsupported HyphenationFrequency value";
react_native_assert(false);
return "none";
}

inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
HyphenationFrequency &result) {
react_native_assert(value.hasType<std::string>());
if (value.hasType<std::string>()) {
auto string = (std::string)value;
if (string == "none") {
result = HyphenationFrequency::None;
} else if (string == "normal") {
result = HyphenationFrequency::Normal;
} else if (string == "full") {
result = HyphenationFrequency::Full;
} else {
// sane default
LOG(ERROR) << "Unsupported HyphenationFrequency value: " << string;
react_native_assert(false);
result = HyphenationFrequency::None;
}
return;
}

LOG(ERROR) << "Unsupported HyphenationFrequency type";
react_native_assert(false);
result = HyphenationFrequency::None;
}

inline ParagraphAttributes convertRawProp(
const PropsParserContext &context,
RawProps const &rawProps,
Expand Down Expand Up @@ -761,6 +803,12 @@ inline ParagraphAttributes convertRawProp(
"includeFontPadding",
sourceParagraphAttributes.includeFontPadding,
defaultParagraphAttributes.includeFontPadding);
paragraphAttributes.android_hyphenationFrequency = convertRawProp(
context,
rawProps,
"android_hyphenationFrequency",
sourceParagraphAttributes.android_hyphenationFrequency,
defaultParagraphAttributes.android_hyphenationFrequency);

return paragraphAttributes;
}
Expand Down Expand Up @@ -796,6 +844,9 @@ inline folly::dynamic toDynamic(
values("textBreakStrategy", toString(paragraphAttributes.textBreakStrategy));
values("adjustsFontSizeToFit", paragraphAttributes.adjustsFontSizeToFit);
values("includeFontPadding", paragraphAttributes.includeFontPadding);
values(
"android_hyphenationFrequency",
toString(paragraphAttributes.android_hyphenationFrequency));

return values;
}
Expand Down
19 changes: 18 additions & 1 deletion ReactCommon/react/renderer/attributedstring/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ enum class EllipsizeMode {
Middle // Truncate middle of line: "ab...yz".
};

enum class TextBreakStrategy { Simple, Balanced, HighQuality };
enum class TextBreakStrategy {
Simple, // Simple strategy.
Balanced, // Balances line lengths.
HighQuality // High-quality strategy, including hyphenation.
};

enum class TextAlignment {
Natural, // Indicates the default alignment for script.
Expand Down Expand Up @@ -126,6 +130,12 @@ enum class TextTransform {
Unset,
};

enum class HyphenationFrequency {
None, // No hyphenation.
Normal, // Less frequent hyphenation.
Full // Standard amount of hyphenation.
};

} // namespace react
} // namespace facebook

Expand Down Expand Up @@ -213,4 +223,11 @@ struct hash<facebook::react::TextTransform> {
return hash<int>()(static_cast<int>(v));
}
};

template <>
struct hash<facebook::react::HyphenationFrequency> {
size_t operator()(const facebook::react::HyphenationFrequency &v) const {
return hash<int>()(static_cast<int>(v));
}
};
} // namespace std

0 comments on commit ca60be8

Please sign in to comment.