Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-duplicate building Spannable on Android #39630

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.facebook.react.common

/**
* General-purpose integer constants
*/
internal object IntConstants {
/**
* Some types have built-in support for representing a "missing" or "unset" value, for example
* NaN in the case of floating point numbers or null in the case of object references. Integers
* don't have such a special value. When an integer represent an inherently non-negative value,
* we use a special negative value to mark it as "unset".
*/
const val UNSET: Int = -1
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.common.IntConstants;

import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -167,8 +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;

Expand All @@ -177,11 +177,11 @@ public static class TypefaceStyle {

public TypefaceStyle(int weight, boolean italic) {
mItalic = italic;
mWeight = weight == UNSET ? NORMAL : weight;
mWeight = weight == IntConstants.UNSET ? NORMAL : weight;
}

public TypefaceStyle(int style) {
if (style == UNSET) {
if (style == IntConstants.UNSET) {
style = Typeface.NORMAL;
}

Expand All @@ -194,12 +194,12 @@ public TypefaceStyle(int style) {
* existing weight bit in `style` will be used.
*/
public TypefaceStyle(int style, int weight) {
if (style == UNSET) {
if (style == IntConstants.UNSET) {
style = Typeface.NORMAL;
}

mItalic = (style & Typeface.ITALIC) != 0;
mWeight = weight == UNSET ? (style & Typeface.BOLD) != 0 ? BOLD : NORMAL : weight;
mWeight = weight == IntConstants.UNSET ? (style & Typeface.BOLD) != 0 ? BOLD : NORMAL : weight;
}

public int getNearestStyle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,7 @@ public class ReactFeatureFlags {

/** When enabled, the default value of the position style property is relative. */
public static boolean positionRelativeDefault = false;

/** Enables the new unified {@link android.text.Spannable} building logic. */
public static boolean enableSpannableBuildingUnification = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.facebook.react.views.text

import com.facebook.react.uimanager.ReactAccessibilityDelegate

/**
* Interface for an entity providing basic text attributes of a text node/fragment. "Basic" means
* that they can be provided trivially, without processing the parent element.
*/
internal interface BasicTextAttributeProvider {
val role: ReactAccessibilityDelegate.Role?

val accessibilityRole: ReactAccessibilityDelegate.AccessibilityRole?

val isBackgroundColorSet: Boolean

val backgroundColor: Int

val isColorSet: Boolean

val color: Int

val fontStyle: Int

val fontWeight: Int

val fontFamily: String?

val fontFeatureSettings: String?

val isUnderlineTextDecorationSet: Boolean

val isLineThroughTextDecorationSet: Boolean

val textShadowOffsetDx: Float

val textShadowOffsetDy: Float

val textShadowRadius: Float

val textShadowColor: Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.text.style.MetricAffectingSpan;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.common.IntConstants;
import com.facebook.react.common.assets.ReactFontManager;

@Nullsafe(Nullsafe.Mode.LOCAL)
Expand Down Expand Up @@ -61,11 +62,11 @@ public void updateMeasureState(TextPaint paint) {
}

public int getStyle() {
return mStyle == ReactFontManager.TypefaceStyle.UNSET ? Typeface.NORMAL : mStyle;
return mStyle == IntConstants.UNSET ? Typeface.NORMAL : mStyle;
}

public int getWeight() {
return mWeight == ReactFontManager.TypefaceStyle.UNSET
return mWeight == IntConstants.UNSET
? ReactFontManager.TypefaceStyle.NORMAL
: mWeight;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.facebook.react.views.text

import com.facebook.react.common.IntConstants.UNSET

/**
* Interface for an entity providing effective text attributes of a text node/fragment
*/
internal interface EffectiveTextAttributeProvider : BasicTextAttributeProvider {
val textTransform: TextTransform

val effectiveLetterSpacing: Float

/**
* @return The effective font size, or [UNSET] if not set
*/
val effectiveFontSize: Int

val effectiveLineHeight: Float
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.facebook.react.views.text

import com.facebook.react.common.IntConstants

/**
* Implementation of [EffectiveTextAttributeProvider] that provides effective text
* attributes based on a [ReactBaseTextShadowNode] instance and its parent.
*/
internal class HierarchicTextAttributeProvider(
private val textShadowNode: ReactBaseTextShadowNode,
private val parentTextAttributes: TextAttributes?,
private val textAttributes: TextAttributes
) : EffectiveTextAttributeProvider, BasicTextAttributeProvider by textShadowNode {
override val textTransform: TextTransform
get() = textAttributes.textTransform

override val effectiveLetterSpacing: Float
get() {
val letterSpacing = textAttributes.effectiveLetterSpacing

val isParentLetterSpacingDifferent =
parentTextAttributes == null || parentTextAttributes.effectiveLetterSpacing != letterSpacing

return if (!letterSpacing.isNaN() && isParentLetterSpacingDifferent) {
letterSpacing
} else {
Float.NaN
}
}

override val effectiveFontSize: Int
get() {
val fontSize = textAttributes.effectiveFontSize

return if (parentTextAttributes == null || parentTextAttributes.effectiveFontSize != fontSize) {
fontSize
} else {
IntConstants.UNSET
}
}

override val effectiveLineHeight: Float
get() {
val lineHeight = textAttributes.effectiveLineHeight
val isParentLineHeightDifferent =
parentTextAttributes == null || parentTextAttributes.effectiveLineHeight != lineHeight

return if (!lineHeight.isNaN() && isParentLineHeightDifferent) {
lineHeight
} else {
Float.NaN
}
}
}
Loading