Skip to content

Commit 828b747

Browse files
committed
Introduce an abstraction for getting the effective font size
...which further de-duplicates the `Spannable`-building code
1 parent 2ed4760 commit 828b747

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package com.facebook.react.views.text;
22

3+
import com.facebook.react.common.assets.ReactFontManager;
34
import com.facebook.react.uimanager.ReactAccessibilityDelegate;
45
import com.facebook.react.uimanager.ReactAccessibilityDelegate.Role;
56

67
public interface EffectiveTextAttributeProvider {
8+
int UNSET = ReactFontManager.TypefaceStyle.UNSET;
9+
710
TextTransform getTextTransform();
811

912
float getEffectiveLetterSpacing();
1013

14+
/**
15+
* @return The effective font size, or {@link #UNSET} if not set
16+
*/
17+
int getEffectiveFontSize();
18+
1119
Role getRole();
1220

1321
ReactAccessibilityDelegate.AccessibilityRole getAccessibilityRole();

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ public float getEffectiveLetterSpacing() {
160160
}
161161
}
162162

163+
@Override
164+
public int getEffectiveFontSize() {
165+
final int fontSize = textAttributes.getEffectiveFontSize();
166+
167+
if (
168+
// `getEffectiveFontSize` always returns a value so don't need to check for anything like `Float.NaN`.
169+
parentTextAttributes == null
170+
|| parentTextAttributes.getEffectiveFontSize() != fontSize) {
171+
return fontSize;
172+
} else {
173+
return UNSET;
174+
}
175+
}
176+
163177
@Override
164178
public float getEffectiveLineHeight() {
165179
final float lineHeight = textAttributes.getEffectiveLineHeight();
@@ -276,13 +290,8 @@ private static void buildSpannedFromShadowNode(
276290

277291
TextLayoutUtils.addLetterSpacingSpanIfApplicable(ops, textAttributeProvider, start, end);
278292

279-
int effectiveFontSize = textAttributes.getEffectiveFontSize();
280-
if ( // `getEffectiveFontSize` always returns a value so don't need to check for anything like
281-
// `Float.NaN`.
282-
parentTextAttributes == null
283-
|| parentTextAttributes.getEffectiveFontSize() != effectiveFontSize) {
284-
TextLayoutUtils.addFontSizeSpan(ops, start, end, effectiveFontSize);
285-
}
293+
TextLayoutUtils.addFontSizeSpanIfApplicable(ops, textAttributeProvider, start, end);
294+
286295
TextLayoutUtils.addCustomStyleSpanIfApplicable(ops, textAttributeProvider, textShadowNode.getThemedContext(), start, end);
287296

288297
TextLayoutUtils.addUnderlineSpanIfApplicable(ops, textAttributeProvider, start, end);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ public float getEffectiveLetterSpacing() {
392392
return letterSpacingPixels / mFontSize;
393393
}
394394

395+
@Override
396+
public int getEffectiveFontSize() {
397+
return mFontSize;
398+
}
399+
395400
private void setAllowFontScaling(boolean allowFontScaling) {
396401
if (allowFontScaling != mAllowFontScaling) {
397402
mAllowFontScaling = allowFontScaling;

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void buildSpannableFromTextFragmentList(Context context, TextFragm
5151

5252
addLetterSpacingSpanIfApplicable(ops, textAttributes, start, end);
5353

54-
addFontSizeSpan(ops, start, end, textAttributes.mFontSize);
54+
addFontSizeSpanIfApplicable(ops, textAttributes, start, end);
5555

5656
addCustomStyleSpanIfApplicable(ops, textAttributes, context, start, end);
5757

@@ -119,9 +119,14 @@ public static void addLetterSpacingSpanIfApplicable(List<SetSpanOperation> ops,
119119
}
120120

121121

122-
public static void addFontSizeSpan(List<SetSpanOperation> ops, int start, int end, int effectiveFontSize) {
122+
public static void addFontSizeSpanIfApplicable(List<SetSpanOperation> ops,
123+
EffectiveTextAttributeProvider textAttributeProvider, int start,
124+
int end) {
125+
final int effectiveFontSize = textAttributeProvider.getEffectiveFontSize();
123126

124-
ops.add(new SetSpanOperation(start, end, new ReactAbsoluteSizeSpan(effectiveFontSize)));
127+
if (effectiveFontSize != UNSET) {
128+
ops.add(new SetSpanOperation(start, end, new ReactAbsoluteSizeSpan(effectiveFontSize)));
129+
}
125130
}
126131

127132
public static void addCustomStyleSpanIfApplicable(List<SetSpanOperation> ops,

0 commit comments

Comments
 (0)