From 109d0befa0f0e28fcb182620b9cab31efa3199fd Mon Sep 17 00:00:00 2001 From: Jakub Trzebiatowski Date: Wed, 1 Nov 2023 10:38:24 +0100 Subject: [PATCH] Extract a shared `FontConstants.UNSET` constant --- .../facebook/react/common/assets/ReactFontManager.java | 3 ++- .../com/facebook/react/internal/fonts/FontConstants.kt | 8 ++++++++ .../internal/views/text/EffectiveTextAttributeProvider.kt | 8 ++------ .../views/text/HierarchicTextAttributeProvider.kt | 3 ++- .../facebook/react/internal/views/text/TextLayoutUtils.kt | 3 +-- .../com/facebook/react/views/text/CustomStyleSpan.java | 5 +++-- .../react/views/text/ReactBaseTextShadowNode.java | 3 ++- .../com/facebook/react/views/text/ReactTypefaceUtils.java | 6 ++++-- 8 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/fonts/FontConstants.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/assets/ReactFontManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/assets/ReactFontManager.java index 7e7df01bd140ff..30438a3600a6e8 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/assets/ReactFontManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/assets/ReactFontManager.java @@ -7,6 +7,8 @@ package com.facebook.react.common.assets; +import static com.facebook.react.views.text.TextAttributeProps.UNSET; + import android.content.Context; import android.content.res.AssetManager; import android.graphics.Typeface; @@ -167,7 +169,6 @@ public static class TypefaceStyle { public static final int BOLD = 700; public static final int NORMAL = 400; - public static final int UNSET = -1; private static final int MIN_WEIGHT = 1; private static final int MAX_WEIGHT = 1000; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/fonts/FontConstants.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/fonts/FontConstants.kt new file mode 100644 index 00000000000000..aa943adddcde99 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/fonts/FontConstants.kt @@ -0,0 +1,8 @@ +package com.facebook.react.internal.fonts + +object FontConstants { + /** + * A special value indicating that a font-related property is not set + */ + const val UNSET: Int = -1 +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/EffectiveTextAttributeProvider.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/EffectiveTextAttributeProvider.kt index bc95997bd4dcf5..4bdd77cb867b2b 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/EffectiveTextAttributeProvider.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/EffectiveTextAttributeProvider.kt @@ -1,22 +1,18 @@ package com.facebook.react.internal.views.text -import com.facebook.react.common.assets.ReactFontManager +import com.facebook.react.internal.fonts.FontConstants import com.facebook.react.views.text.TextTransform /** * Interface for an entity providing effective text attributes of a text node/fragment */ interface EffectiveTextAttributeProvider : BasicTextAttributeProvider { - companion object { - const val UNSET = ReactFontManager.TypefaceStyle.UNSET - } - val textTransform: TextTransform val effectiveLetterSpacing: Float /** - * @return The effective font size, or [UNSET] if not set + * @return The effective font size, or [FontConstants.UNSET] if not set */ val effectiveFontSize: Int diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/HierarchicTextAttributeProvider.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/HierarchicTextAttributeProvider.kt index 1076c6e1049845..65138138f12e54 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/HierarchicTextAttributeProvider.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/HierarchicTextAttributeProvider.kt @@ -1,5 +1,6 @@ package com.facebook.react.internal.views.text +import com.facebook.react.internal.fonts.FontConstants import com.facebook.react.views.text.ReactBaseTextShadowNode import com.facebook.react.views.text.TextAttributes import com.facebook.react.views.text.TextTransform @@ -37,7 +38,7 @@ class HierarchicTextAttributeProvider( return if (parentTextAttributes == null || parentTextAttributes.effectiveFontSize != fontSize) { fontSize } else { - EffectiveTextAttributeProvider.UNSET + FontConstants.UNSET } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/TextLayoutUtils.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/TextLayoutUtils.kt index 6859fee7b42a16..862596898bd821 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/TextLayoutUtils.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/TextLayoutUtils.kt @@ -11,7 +11,7 @@ import android.content.Context import android.graphics.Color import android.text.* import android.view.View -import com.facebook.react.common.assets.ReactFontManager +import com.facebook.react.internal.fonts.FontConstants.UNSET import com.facebook.react.uimanager.PixelUtil import com.facebook.react.uimanager.ReactAccessibilityDelegate import com.facebook.react.internal.views.text.fragments.TextFragmentList @@ -22,7 +22,6 @@ import com.facebook.react.views.text.* */ object TextLayoutUtils { private const val INLINE_VIEW_PLACEHOLDER = "0" - private const val UNSET = ReactFontManager.TypefaceStyle.UNSET fun buildSpannableFromTextFragmentList( context: Context, diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java index 51fd4dd0149c77..1a1ebd9f75d333 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java @@ -15,6 +15,7 @@ import androidx.annotation.Nullable; import com.facebook.infer.annotation.Nullsafe; import com.facebook.react.common.assets.ReactFontManager; +import com.facebook.react.internal.fonts.FontConstants; @Nullsafe(Nullsafe.Mode.LOCAL) public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan { @@ -61,11 +62,11 @@ public void updateMeasureState(TextPaint paint) { } public int getStyle() { - return mStyle == ReactFontManager.TypefaceStyle.UNSET ? Typeface.NORMAL : mStyle; + return mStyle == FontConstants.UNSET ? Typeface.NORMAL : mStyle; } public int getWeight() { - return mWeight == ReactFontManager.TypefaceStyle.UNSET + return mWeight == FontConstants.UNSET ? ReactFontManager.TypefaceStyle.NORMAL : mWeight; } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java index 3ed806a450a2b9..2faf3ce60ebd88 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java @@ -7,6 +7,8 @@ package com.facebook.react.views.text; +import static com.facebook.react.internal.fonts.FontConstants.UNSET; + import android.graphics.Color; import android.graphics.Typeface; import android.os.Build; @@ -59,7 +61,6 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode implement // character. // https://en.wikipedia.org/wiki/Bi-directional_text#weak_characters private static final String INLINE_VIEW_PLACEHOLDER = "0"; - public static final int UNSET = ReactFontManager.TypefaceStyle.UNSET; public static final String PROP_SHADOW_OFFSET = "textShadowOffset"; public static final String PROP_SHADOW_OFFSET_WIDTH = "width"; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTypefaceUtils.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTypefaceUtils.java index 64fe5e36c0eb0d..db2ba2ee449a57 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTypefaceUtils.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTypefaceUtils.java @@ -14,6 +14,8 @@ import com.facebook.infer.annotation.Nullsafe; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.common.assets.ReactFontManager; +import com.facebook.react.internal.fonts.FontConstants; + import java.util.ArrayList; import java.util.List; @@ -45,7 +47,7 @@ public static int parseFontWeight(@Nullable String fontWeightString) { return 900; } } - return ReactFontManager.TypefaceStyle.UNSET; + return FontConstants.UNSET; } public static int parseFontStyle(@Nullable String fontStyleString) { @@ -57,7 +59,7 @@ public static int parseFontStyle(@Nullable String fontStyleString) { return Typeface.NORMAL; } } - return ReactFontManager.TypefaceStyle.UNSET; + return FontConstants.UNSET; } public static @Nullable String parseFontVariant(@Nullable ReadableArray fontVariantArray) {