From 475df0699d9aaa5b498a82fd378ed2a64edcc544 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Sat, 18 Jan 2020 18:45:45 -0800 Subject: [PATCH] Fix rendering of FB emoji in RN Android Summary: This diff ensures the measurement and rendering of FB emojis is correnct in RN Android. Before this commit we were customizing Spannable object with FB emojis right before rendering the text into the TextView, this diff ensures that the Spannable is "customized" as soon as it is created, ensuring the measurement of the Text. changelog: [internal] Reviewed By: JoshuaGross Differential Revision: D19354107 fbshipit-source-id: 92e07cf30503404f7820f25eaa9efdc02f6bddbd --- .../react/views/text/ReactTextShadowNode.java | 9 +++++++ .../text/ReactTextViewManagerCallback.java | 25 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManagerCallback.java diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java index 28180b31f9d36f..1a6599e643370b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java @@ -57,6 +57,8 @@ public class ReactTextShadowNode extends ReactBaseTextShadowNode { private boolean mShouldNotifyOnTextLayout; + private @Nullable ReactTextViewManagerCallback mReactTextViewManagerCallback = null; + private final YogaMeasureFunction mTextMeasureFunction = new YogaMeasureFunction() { @Override @@ -196,6 +198,10 @@ private void initMeasureFunction() { } } + public void setReactTextViewManagerCallback(ReactTextViewManagerCallback callback) { + mReactTextViewManagerCallback = callback; + } + // Return text alignment according to LTR or RTL style private int getTextAlign() { int textAlign = mTextAlign; @@ -217,6 +223,9 @@ public void onBeforeLayout(NativeViewHierarchyOptimizer nativeViewHierarchyOptim /* text (e.g. from `value` prop): */ null, /* supportsInlineViews: */ true, nativeViewHierarchyOptimizer); + if (mReactTextViewManagerCallback != null) { + mReactTextViewManagerCallback.onPostProcessSpannable(mPreparedSpannableText); + } markUpdated(); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManagerCallback.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManagerCallback.java new file mode 100644 index 00000000000000..16eeab59d70ea5 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManagerCallback.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) Facebook, Inc. and its 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; + +import android.text.Spannable; + +/** + * This interface allows clients of {@link ReactTextViewManager} to customize or prepare {@link + * Spannable} object that represent text that will be rendered on the screen. + */ +public interface ReactTextViewManagerCallback { + + /** + * Callback executed right after the {@link Spannable} object is created by React. + * + *

This callback can be used by different implementations of ReactTextViewManager to customize + * Spannable or extend managed created by React. + */ + void onPostProcessSpannable(Spannable text); +}