TextAttributes.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #include "TextAttributes.h"
  8. #include <react/attributedstring/conversions.h>
  9. #include <react/core/conversions.h>
  10. #include <react/graphics/conversions.h>
  11. #include <react/utils/FloatComparison.h>
  12. #include <cmath>
  13. #include <react/debug/debugStringConvertibleUtils.h>
  14. namespace facebook {
  15. namespace react {
  16. void TextAttributes::apply(TextAttributes textAttributes) {
  17. // Color
  18. foregroundColor = textAttributes.foregroundColor
  19. ? textAttributes.foregroundColor
  20. : foregroundColor;
  21. backgroundColor = textAttributes.backgroundColor
  22. ? textAttributes.backgroundColor
  23. : backgroundColor;
  24. opacity =
  25. !std::isnan(textAttributes.opacity) ? textAttributes.opacity : opacity;
  26. // Font
  27. fontFamily = !textAttributes.fontFamily.empty() ? textAttributes.fontFamily
  28. : fontFamily;
  29. fontSize =
  30. !std::isnan(textAttributes.fontSize) ? textAttributes.fontSize : fontSize;
  31. fontSizeMultiplier = !std::isnan(textAttributes.fontSizeMultiplier)
  32. ? textAttributes.fontSizeMultiplier
  33. : fontSizeMultiplier;
  34. fontWeight = textAttributes.fontWeight.hasValue() ? textAttributes.fontWeight
  35. : fontWeight;
  36. fontStyle = textAttributes.fontStyle.hasValue() ? textAttributes.fontStyle
  37. : fontStyle;
  38. fontVariant = textAttributes.fontVariant.hasValue()
  39. ? textAttributes.fontVariant
  40. : fontVariant;
  41. allowFontScaling = textAttributes.allowFontScaling.hasValue()
  42. ? textAttributes.allowFontScaling
  43. : allowFontScaling;
  44. letterSpacing = !std::isnan(textAttributes.letterSpacing)
  45. ? textAttributes.letterSpacing
  46. : letterSpacing;
  47. // Paragraph Styles
  48. lineHeight = !std::isnan(textAttributes.lineHeight)
  49. ? textAttributes.lineHeight
  50. : lineHeight;
  51. alignment = textAttributes.alignment.hasValue() ? textAttributes.alignment
  52. : alignment;
  53. baseWritingDirection = textAttributes.baseWritingDirection.hasValue()
  54. ? textAttributes.baseWritingDirection
  55. : baseWritingDirection;
  56. // Decoration
  57. textDecorationColor = textAttributes.textDecorationColor
  58. ? textAttributes.textDecorationColor
  59. : textDecorationColor;
  60. textDecorationLineType = textAttributes.textDecorationLineType.hasValue()
  61. ? textAttributes.textDecorationLineType
  62. : textDecorationLineType;
  63. textDecorationLineStyle = textAttributes.textDecorationLineStyle.hasValue()
  64. ? textAttributes.textDecorationLineStyle
  65. : textDecorationLineStyle;
  66. textDecorationLinePattern =
  67. textAttributes.textDecorationLinePattern.hasValue()
  68. ? textAttributes.textDecorationLinePattern
  69. : textDecorationLinePattern;
  70. // Shadow
  71. textShadowOffset = textAttributes.textShadowOffset.hasValue()
  72. ? textAttributes.textShadowOffset.value()
  73. : textShadowOffset;
  74. textShadowRadius = !std::isnan(textAttributes.textShadowRadius)
  75. ? textAttributes.textShadowRadius
  76. : textShadowRadius;
  77. textShadowColor = textAttributes.textShadowColor
  78. ? textAttributes.textShadowColor
  79. : textShadowColor;
  80. // Special
  81. isHighlighted = textAttributes.isHighlighted.hasValue()
  82. ? textAttributes.isHighlighted
  83. : isHighlighted;
  84. layoutDirection = textAttributes.layoutDirection.hasValue()
  85. ? textAttributes.layoutDirection
  86. : layoutDirection;
  87. }
  88. #pragma mark - Operators
  89. bool TextAttributes::operator==(const TextAttributes &rhs) const {
  90. return std::tie(
  91. foregroundColor,
  92. backgroundColor,
  93. fontFamily,
  94. fontWeight,
  95. fontStyle,
  96. fontVariant,
  97. allowFontScaling,
  98. alignment,
  99. baseWritingDirection,
  100. textDecorationColor,
  101. textDecorationLineType,
  102. textDecorationLineStyle,
  103. textDecorationLinePattern,
  104. textShadowOffset,
  105. textShadowColor,
  106. isHighlighted,
  107. layoutDirection) ==
  108. std::tie(
  109. rhs.foregroundColor,
  110. rhs.backgroundColor,
  111. rhs.fontFamily,
  112. rhs.fontWeight,
  113. rhs.fontStyle,
  114. rhs.fontVariant,
  115. rhs.allowFontScaling,
  116. rhs.alignment,
  117. rhs.baseWritingDirection,
  118. rhs.textDecorationColor,
  119. rhs.textDecorationLineType,
  120. rhs.textDecorationLineStyle,
  121. rhs.textDecorationLinePattern,
  122. rhs.textShadowOffset,
  123. rhs.textShadowColor,
  124. rhs.isHighlighted,
  125. rhs.layoutDirection) &&
  126. floatEquality(opacity, rhs.opacity) &&
  127. floatEquality(fontSize, rhs.fontSize) &&
  128. floatEquality(fontSizeMultiplier, rhs.fontSizeMultiplier) &&
  129. floatEquality(letterSpacing, rhs.letterSpacing) &&
  130. floatEquality(lineHeight, rhs.lineHeight) &&
  131. floatEquality(textShadowRadius, rhs.textShadowRadius);
  132. }
  133. bool TextAttributes::operator!=(const TextAttributes &rhs) const {
  134. return !(*this == rhs);
  135. }
  136. TextAttributes TextAttributes::defaultTextAttributes() {
  137. static auto textAttributes = [] {
  138. auto textAttributes = TextAttributes{};
  139. // Non-obvious (can be different among platforms) default text attributes.
  140. textAttributes.foregroundColor = blackColor();
  141. textAttributes.backgroundColor = clearColor();
  142. textAttributes.fontSize = 14.0;
  143. textAttributes.fontSizeMultiplier = 1.0;
  144. return textAttributes;
  145. }();
  146. return textAttributes;
  147. }
  148. #pragma mark - DebugStringConvertible
  149. #if RN_DEBUG_STRING_CONVERTIBLE
  150. SharedDebugStringConvertibleList TextAttributes::getDebugProps() const {
  151. return {
  152. // Color
  153. debugStringConvertibleItem("backgroundColor", backgroundColor),
  154. debugStringConvertibleItem("foregroundColor", foregroundColor),
  155. debugStringConvertibleItem("opacity", opacity),
  156. // Font
  157. debugStringConvertibleItem("fontFamily", fontFamily),
  158. debugStringConvertibleItem("fontSize", fontSize),
  159. debugStringConvertibleItem("fontSizeMultiplier", fontSizeMultiplier),
  160. debugStringConvertibleItem("fontWeight", fontWeight),
  161. debugStringConvertibleItem("fontStyle", fontStyle),
  162. debugStringConvertibleItem("fontVariant", fontVariant),
  163. debugStringConvertibleItem("allowFontScaling", allowFontScaling),
  164. debugStringConvertibleItem("letterSpacing", letterSpacing),
  165. // Paragraph Styles
  166. debugStringConvertibleItem("lineHeight", lineHeight),
  167. debugStringConvertibleItem("alignment", alignment),
  168. debugStringConvertibleItem("baseWritingDirection", baseWritingDirection),
  169. // Decoration
  170. debugStringConvertibleItem("textDecorationColor", textDecorationColor),
  171. debugStringConvertibleItem(
  172. "textDecorationLineType", textDecorationLineType),
  173. debugStringConvertibleItem(
  174. "textDecorationLineStyle", textDecorationLineStyle),
  175. debugStringConvertibleItem(
  176. "textDecorationLinePattern", textDecorationLinePattern),
  177. // Shadow
  178. debugStringConvertibleItem("textShadowOffset", textShadowOffset),
  179. debugStringConvertibleItem("textShadowRadius", textShadowRadius),
  180. debugStringConvertibleItem("textShadowColor", textShadowColor),
  181. // Special
  182. debugStringConvertibleItem("isHighlighted", isHighlighted),
  183. debugStringConvertibleItem("layoutDirection", layoutDirection),
  184. };
  185. }
  186. #endif
  187. } // namespace react
  188. } // namespace facebook