Skip to content

Commit aab4763

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Fix textShadow on Android Fabric (facebook#36585)
Summary: Pull Request resolved: facebook#36585 1. We don't add `ShadowStyleSpan` unless an offset is set. This is incorrect, and less permissive than `BaseTextShadowNode` in Paper. 2. We don't serialize textShadowOffset to MapBuffer Changelog: [Android][Fixed] - Fix textShadow on Android Fabric Reviewed By: javache Differential Revision: D44302691 fbshipit-source-id: 2cfd3bff3d0e4f329c98d1ed2defff94ad3b7dc3
1 parent 987c6fd commit aab4763

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ public class TextAttributeProps {
5151
public static final short TA_KEY_TEXT_DECORATION_STYLE = 16;
5252
public static final short TA_KEY_TEXT_SHADOW_RADIUS = 18;
5353
public static final short TA_KEY_TEXT_SHADOW_COLOR = 19;
54-
public static final short TA_KEY_IS_HIGHLIGHTED = 20;
55-
public static final short TA_KEY_LAYOUT_DIRECTION = 21;
56-
public static final short TA_KEY_ACCESSIBILITY_ROLE = 22;
54+
public static final short TA_KEY_TEXT_SHADOW_OFFSET_DX = 20;
55+
public static final short TA_KEY_TEXT_SHADOW_OFFSET_DY = 21;
56+
public static final short TA_KEY_IS_HIGHLIGHTED = 22;
57+
public static final short TA_KEY_LAYOUT_DIRECTION = 23;
58+
public static final short TA_KEY_ACCESSIBILITY_ROLE = 24;
5759

5860
public static final int UNSET = -1;
5961

@@ -198,6 +200,12 @@ public static TextAttributeProps fromMapBuffer(MapBuffer props) {
198200
case TA_KEY_TEXT_SHADOW_COLOR:
199201
result.setTextShadowColor(entry.getIntValue());
200202
break;
203+
case TA_KEY_TEXT_SHADOW_OFFSET_DX:
204+
result.setTextShadowOffsetDx((float) entry.getDoubleValue());
205+
break;
206+
case TA_KEY_TEXT_SHADOW_OFFSET_DY:
207+
result.setTextShadowOffsetDy((float) entry.getDoubleValue());
208+
break;
201209
case TA_KEY_IS_HIGHLIGHTED:
202210
break;
203211
case TA_KEY_LAYOUT_DIRECTION:
@@ -213,7 +221,6 @@ public static TextAttributeProps fromMapBuffer(MapBuffer props) {
213221
// setNumberOfLines
214222
// setColor
215223
// setIncludeFontPadding
216-
// setTextShadowOffset
217224
// setTextTransform
218225
return result;
219226
}
@@ -556,6 +563,14 @@ private void setTextShadowOffset(ReadableMap offsetMap) {
556563
}
557564
}
558565

566+
private void setTextShadowOffsetDx(float dx) {
567+
mTextShadowOffsetDx = PixelUtil.toPixelFromDIP(dx);
568+
}
569+
570+
private void setTextShadowOffsetDy(float dy) {
571+
mTextShadowOffsetDy = PixelUtil.toPixelFromDIP(dy);
572+
}
573+
559574
public static int getLayoutDirection(@Nullable String layoutDirection) {
560575
int androidLayoutDirection;
561576
if (layoutDirection == null || "undefined".equals(layoutDirection)) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static com.facebook.react.views.text.TextAttributeProps.UNSET;
1111

1212
import android.content.Context;
13+
import android.graphics.Color;
1314
import android.os.Build;
1415
import android.text.BoringLayout;
1516
import android.text.Layout;
@@ -165,7 +166,10 @@ private static void buildSpannableFromFragment(
165166
if (textAttributes.mIsLineThroughTextDecorationSet) {
166167
ops.add(new SetSpanOperation(start, end, new ReactStrikethroughSpan()));
167168
}
168-
if (textAttributes.mTextShadowOffsetDx != 0 || textAttributes.mTextShadowOffsetDy != 0) {
169+
if ((textAttributes.mTextShadowOffsetDx != 0
170+
|| textAttributes.mTextShadowOffsetDy != 0
171+
|| textAttributes.mTextShadowRadius != 0)
172+
&& Color.alpha(textAttributes.mTextShadowColor) != 0) {
169173
ops.add(
170174
new SetSpanOperation(
171175
start,

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManagerMapBuffer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static com.facebook.react.views.text.TextAttributeProps.UNSET;
1111

1212
import android.content.Context;
13+
import android.graphics.Color;
1314
import android.os.Build;
1415
import android.text.BoringLayout;
1516
import android.text.Layout;
@@ -179,7 +180,10 @@ private static void buildSpannableFromFragment(
179180
if (textAttributes.mIsLineThroughTextDecorationSet) {
180181
ops.add(new SetSpanOperation(start, end, new ReactStrikethroughSpan()));
181182
}
182-
if (textAttributes.mTextShadowOffsetDx != 0 || textAttributes.mTextShadowOffsetDy != 0) {
183+
if ((textAttributes.mTextShadowOffsetDx != 0
184+
|| textAttributes.mTextShadowOffsetDy != 0
185+
|| textAttributes.mTextShadowRadius != 0)
186+
&& Color.alpha(textAttributes.mTextShadowColor) != 0) {
183187
ops.add(
184188
new SetSpanOperation(
185189
start,

packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,12 @@ constexpr static MapBuffer::Key TA_KEY_TEXT_DECORATION_LINE = 15;
11061106
constexpr static MapBuffer::Key TA_KEY_TEXT_DECORATION_STYLE = 16;
11071107
constexpr static MapBuffer::Key TA_KEY_TEXT_SHADOW_RADIUS = 18;
11081108
constexpr static MapBuffer::Key TA_KEY_TEXT_SHADOW_COLOR = 19;
1109-
constexpr static MapBuffer::Key TA_KEY_IS_HIGHLIGHTED = 20;
1110-
constexpr static MapBuffer::Key TA_KEY_LAYOUT_DIRECTION = 21;
1111-
constexpr static MapBuffer::Key TA_KEY_ACCESSIBILITY_ROLE = 22;
1112-
constexpr static MapBuffer::Key TA_KEY_LINE_BREAK_STRATEGY = 23;
1109+
constexpr static MapBuffer::Key TA_KEY_TEXT_SHADOW_OFFSET_DX = 20;
1110+
constexpr static MapBuffer::Key TA_KEY_TEXT_SHADOW_OFFSET_DY = 21;
1111+
constexpr static MapBuffer::Key TA_KEY_IS_HIGHLIGHTED = 22;
1112+
constexpr static MapBuffer::Key TA_KEY_LAYOUT_DIRECTION = 23;
1113+
constexpr static MapBuffer::Key TA_KEY_ACCESSIBILITY_ROLE = 24;
1114+
constexpr static MapBuffer::Key TA_KEY_LINE_BREAK_STRATEGY = 25;
11131115

11141116
// constants for ParagraphAttributes serialization
11151117
constexpr static MapBuffer::Key PA_KEY_MAX_NUMBER_OF_LINES = 0;
@@ -1244,6 +1246,12 @@ inline MapBuffer toMapBuffer(const TextAttributes &textAttributes) {
12441246
TA_KEY_TEXT_SHADOW_COLOR,
12451247
toAndroidRepr(textAttributes.textShadowColor));
12461248
}
1249+
if (textAttributes.textShadowOffset) {
1250+
builder.putDouble(
1251+
TA_KEY_TEXT_SHADOW_OFFSET_DX, textAttributes.textShadowOffset->width);
1252+
builder.putDouble(
1253+
TA_KEY_TEXT_SHADOW_OFFSET_DY, textAttributes.textShadowOffset->height);
1254+
}
12471255
// Special
12481256
if (textAttributes.isHighlighted.has_value()) {
12491257
builder.putBool(TA_KEY_IS_HIGHLIGHTED, *textAttributes.isHighlighted);

0 commit comments

Comments
 (0)