From a2d53d5ea08959da0bbce3c494e06634e3d52d2a Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Thu, 29 Aug 2024 20:43:30 -0700 Subject: [PATCH] Support simple opacity in nested text (#46267) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46267 We can propagate opacity, already part of AttributedString, to alpha channel of paint used to draw text and background on canvas. This does not support propagating to views, and contrary to the iOS example added which originated with legacy arch, does not correctly support nesting opacity. This is a limitation of new arch more generally, where an AttributedString fragment only contains inner-most opacity. Bg and foreground are drawn separately with alpha as well, instead of rendering overlapping content offscreen to properly apply it (this is an issue on RN Android more generally, and existing color alpha support, but is pretty noticeable here). This impl targets new arch only. Changelog: [Android][Added] - Support simple opacity in nested text Reviewed By: alanleedev Differential Revision: D61999163 fbshipit-source-id: adb99834e94e00cb84a98d56f422c15b1bd849db --- .../ReactAndroid/api/ReactAndroid.api | 2 ++ .../react/views/text/TextAttributeProps.java | 11 +++++++ .../react/views/text/TextLayoutManager.java | 5 +++ .../text/internal/span/ReactOpacitySpan.kt | 33 +++++++++++++++++++ .../js/examples/Text/TextExample.android.js | 15 +++++++++ 5 files changed, 66 insertions(+) create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/ReactOpacitySpan.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 71749dc25d7..282cc585b33 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -7823,6 +7823,7 @@ public class com/facebook/react/views/text/TextAttributeProps { protected field mLineHeight F protected field mLineHeightInput F protected field mNumberOfLines I + protected field mOpacity F protected field mRole Lcom/facebook/react/uimanager/ReactAccessibilityDelegate$Role; protected field mTextAlign I protected field mTextShadowColor I @@ -7846,6 +7847,7 @@ public class com/facebook/react/views/text/TextAttributeProps { public static fun getJustificationMode (Lcom/facebook/react/uimanager/ReactStylesDiffMap;I)I public static fun getLayoutDirection (Ljava/lang/String;)I public fun getLetterSpacing ()F + public fun getOpacity ()F public fun getRole ()Lcom/facebook/react/uimanager/ReactAccessibilityDelegate$Role; public static fun getTextAlignment (Lcom/facebook/react/uimanager/ReactStylesDiffMap;ZI)I public static fun getTextBreakStrategy (Ljava/lang/String;)I diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java index f5dc16e69bd..999e4279a7a 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java @@ -84,6 +84,7 @@ public class TextAttributeProps { protected int mColor; protected boolean mIsBackgroundColorSet = false; protected int mBackgroundColor; + protected float mOpacity = Float.NaN; protected int mNumberOfLines = ReactConstants.UNSET; protected int mFontSize = ReactConstants.UNSET; @@ -161,6 +162,7 @@ public class TextAttributeProps { result.setBackgroundColor(entry.getIntValue()); break; case TA_KEY_OPACITY: + result.setOpacity((float) entry.getDoubleValue()); break; case TA_KEY_FONT_FAMILY: result.setFontFamily(entry.getStringValue()); @@ -251,6 +253,7 @@ public class TextAttributeProps { props.hasKey(ViewProps.BACKGROUND_COLOR) ? props.getInt(ViewProps.BACKGROUND_COLOR, 0) : null); + result.setOpacity(getFloatProp(props, ViewProps.OPACITY, Float.NaN)); result.setFontFamily(getStringProp(props, ViewProps.FONT_FAMILY)); result.setFontWeight(getStringProp(props, ViewProps.FONT_WEIGHT)); result.setFontStyle(getStringProp(props, ViewProps.FONT_STYLE)); @@ -453,6 +456,14 @@ public class TextAttributeProps { // } } + public float getOpacity() { + return mOpacity; + } + + private void setOpacity(float opacity) { + mOpacity = opacity; + } + public boolean isBackgroundColorSet() { return mIsBackgroundColorSet; } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java index 4857780a3a2..706b77825eb 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java @@ -40,6 +40,7 @@ import com.facebook.react.views.text.internal.span.ReactAbsoluteSizeSpan; import com.facebook.react.views.text.internal.span.ReactBackgroundColorSpan; import com.facebook.react.views.text.internal.span.ReactClickableSpan; import com.facebook.react.views.text.internal.span.ReactForegroundColorSpan; +import com.facebook.react.views.text.internal.span.ReactOpacitySpan; import com.facebook.react.views.text.internal.span.ReactStrikethroughSpan; import com.facebook.react.views.text.internal.span.ReactTagSpan; import com.facebook.react.views.text.internal.span.ReactUnderlineSpan; @@ -240,6 +241,10 @@ public class TextLayoutManager { new SetSpanOperation( start, end, new ReactBackgroundColorSpan(textAttributes.mBackgroundColor))); } + if (!Float.isNaN(textAttributes.getOpacity())) { + ops.add( + new SetSpanOperation(start, end, new ReactOpacitySpan(textAttributes.getOpacity()))); + } if (!Float.isNaN(textAttributes.getLetterSpacing())) { ops.add( new SetSpanOperation( diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/ReactOpacitySpan.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/ReactOpacitySpan.kt new file mode 100644 index 00000000000..dec1f1eedf8 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/ReactOpacitySpan.kt @@ -0,0 +1,33 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.views.text.internal.span + +import android.graphics.Color +import android.text.TextPaint +import android.text.style.CharacterStyle +import android.text.style.UpdateAppearance +import kotlin.math.roundToInt + +/** Multiplies foreground and background alpha channels by given opacity */ +public class ReactOpacitySpan(public val opacity: Float) : + CharacterStyle(), UpdateAppearance, ReactSpan { + + override fun updateDrawState(paint: TextPaint) { + paint.alpha = (Color.alpha(paint.color) * opacity).roundToInt() + + if (paint.bgColor != 0) { + paint.bgColor = + Color.argb( + (Color.alpha(paint.bgColor) * opacity).roundToInt(), + Color.red(paint.bgColor), + Color.green(paint.bgColor), + Color.blue(paint.bgColor), + ) + } + } +} diff --git a/packages/rn-tester/js/examples/Text/TextExample.android.js b/packages/rn-tester/js/examples/Text/TextExample.android.js index 9b03b28ac64..1821b526a77 100644 --- a/packages/rn-tester/js/examples/Text/TextExample.android.js +++ b/packages/rn-tester/js/examples/Text/TextExample.android.js @@ -675,6 +675,21 @@ function NestedExample(props: {}): React.Node { blue color, and red color again + + (opacity + + (is inherited + + (and accumulated + + (and also applies to the background) + + ) + + ) + + ) + ); }