TextAttributes.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #pragma once
  8. #include <functional>
  9. #include <limits>
  10. #include <folly/Hash.h>
  11. #include <folly/Optional.h>
  12. #include <react/attributedstring/primitives.h>
  13. #include <react/core/LayoutPrimitives.h>
  14. #include <react/core/ReactPrimitives.h>
  15. #include <react/debug/DebugStringConvertible.h>
  16. #include <react/graphics/Color.h>
  17. #include <react/graphics/Geometry.h>
  18. namespace facebook {
  19. namespace react {
  20. class TextAttributes;
  21. using SharedTextAttributes = std::shared_ptr<const TextAttributes>;
  22. class TextAttributes : public DebugStringConvertible {
  23. public:
  24. /*
  25. * Returns TextAttribute object which has actual default attribute values
  26. * (e.g. `foregroundColor = black`), in oppose to TextAttribute's default
  27. * constructor which creates an object with nulled attributes.
  28. */
  29. static TextAttributes defaultTextAttributes();
  30. #pragma mark - Fields
  31. // Color
  32. SharedColor foregroundColor{};
  33. SharedColor backgroundColor{};
  34. Float opacity{std::numeric_limits<Float>::quiet_NaN()};
  35. // Font
  36. std::string fontFamily{""};
  37. Float fontSize{std::numeric_limits<Float>::quiet_NaN()};
  38. Float fontSizeMultiplier{std::numeric_limits<Float>::quiet_NaN()};
  39. folly::Optional<FontWeight> fontWeight{};
  40. folly::Optional<FontStyle> fontStyle{};
  41. folly::Optional<FontVariant> fontVariant{};
  42. folly::Optional<bool> allowFontScaling{};
  43. Float letterSpacing{std::numeric_limits<Float>::quiet_NaN()};
  44. // Paragraph Styles
  45. Float lineHeight{std::numeric_limits<Float>::quiet_NaN()};
  46. folly::Optional<TextAlignment> alignment{};
  47. folly::Optional<WritingDirection> baseWritingDirection{};
  48. // Decoration
  49. SharedColor textDecorationColor{};
  50. folly::Optional<TextDecorationLineType> textDecorationLineType{};
  51. folly::Optional<TextDecorationLineStyle> textDecorationLineStyle{};
  52. folly::Optional<TextDecorationLinePattern> textDecorationLinePattern{};
  53. // Shadow
  54. // TODO: Use `Point` type instead of `Size` for `textShadowOffset` attribute.
  55. folly::Optional<Size> textShadowOffset{};
  56. Float textShadowRadius{std::numeric_limits<Float>::quiet_NaN()};
  57. SharedColor textShadowColor{};
  58. // Special
  59. folly::Optional<bool> isHighlighted{};
  60. // TODO T59221129: document where this value comes from and how it is set.
  61. // It's not clear if this is being used properly, or if it's being set at all.
  62. // Currently, it is intentionally *not* being set as part of BaseTextProps
  63. // construction.
  64. folly::Optional<LayoutDirection> layoutDirection{};
  65. #pragma mark - Operations
  66. void apply(TextAttributes textAttributes);
  67. #pragma mark - Operators
  68. bool operator==(const TextAttributes &rhs) const;
  69. bool operator!=(const TextAttributes &rhs) const;
  70. #pragma mark - DebugStringConvertible
  71. #if RN_DEBUG_STRING_CONVERTIBLE
  72. SharedDebugStringConvertibleList getDebugProps() const override;
  73. #endif
  74. };
  75. } // namespace react
  76. } // namespace facebook
  77. namespace std {
  78. template <>
  79. struct hash<facebook::react::TextAttributes> {
  80. size_t operator()(
  81. const facebook::react::TextAttributes &textAttributes) const {
  82. return folly::hash::hash_combine(
  83. 0,
  84. textAttributes.foregroundColor,
  85. textAttributes.backgroundColor,
  86. textAttributes.opacity,
  87. textAttributes.fontFamily,
  88. textAttributes.fontSize,
  89. textAttributes.fontSizeMultiplier,
  90. textAttributes.fontWeight,
  91. textAttributes.fontStyle,
  92. textAttributes.fontVariant,
  93. textAttributes.allowFontScaling,
  94. textAttributes.letterSpacing,
  95. textAttributes.lineHeight,
  96. textAttributes.alignment,
  97. textAttributes.baseWritingDirection,
  98. textAttributes.textDecorationColor,
  99. textAttributes.textDecorationLineType,
  100. textAttributes.textDecorationLineStyle,
  101. textAttributes.textDecorationLinePattern,
  102. textAttributes.textShadowOffset,
  103. textAttributes.textShadowRadius,
  104. textAttributes.textShadowColor,
  105. textAttributes.isHighlighted,
  106. textAttributes.layoutDirection);
  107. }
  108. };
  109. } // namespace std