7
7
8
8
package com .facebook .react .views .text ;
9
9
10
+ import com .facebook .react .bridge .JSApplicationIllegalArgumentException ;
10
11
import com .facebook .react .uimanager .PixelUtil ;
11
12
import com .facebook .react .uimanager .ViewDefaults ;
12
13
15
16
* to child so inheritance can be implemented correctly. An example complexity that causes a prop
16
17
* to end up in TextAttributes is when multiple props need to be considered together to determine
17
18
* the rendered aka effective value. For example, to figure out the rendered/effective font size,
18
- * you need to take into account the fontSize and allowFontScaling props.
19
+ * you need to take into account the fontSize, maxFontSizeMultiplier, and allowFontScaling props.
19
20
*/
20
21
public class TextAttributes {
22
+ // Setting the default to 0 indicates that there is no max.
23
+ public static final float DEFAULT_MAX_FONT_SIZE_MULTIPLIER = 0.0f ;
24
+
21
25
private boolean mAllowFontScaling = true ;
22
26
private float mFontSize = Float .NaN ;
23
27
private float mLineHeight = Float .NaN ;
24
28
private float mLetterSpacing = Float .NaN ;
29
+ private float mMaxFontSizeMultiplier = Float .NaN ;
25
30
private float mHeightOfTallestInlineImage = Float .NaN ;
26
31
27
32
public TextAttributes () {
@@ -37,6 +42,7 @@ public TextAttributes applyChild(TextAttributes child) {
37
42
result .mFontSize = !Float .isNaN (child .mFontSize ) ? child .mFontSize : mFontSize ;
38
43
result .mLineHeight = !Float .isNaN (child .mLineHeight ) ? child .mLineHeight : mLineHeight ;
39
44
result .mLetterSpacing = !Float .isNaN (child .mLetterSpacing ) ? child .mLetterSpacing : mLetterSpacing ;
45
+ result .mMaxFontSizeMultiplier = !Float .isNaN (child .mMaxFontSizeMultiplier ) ? child .mMaxFontSizeMultiplier : mMaxFontSizeMultiplier ;
40
46
result .mHeightOfTallestInlineImage = !Float .isNaN (child .mHeightOfTallestInlineImage ) ? child .mHeightOfTallestInlineImage : mHeightOfTallestInlineImage ;
41
47
42
48
return result ;
@@ -77,6 +83,17 @@ public void setLetterSpacing(float value) {
77
83
mLetterSpacing = value ;
78
84
}
79
85
86
+ public float getMaxFontSizeMultiplier () {
87
+ return mMaxFontSizeMultiplier ;
88
+ }
89
+
90
+ public void setMaxFontSizeMultiplier (float maxFontSizeMultiplier ) {
91
+ if (maxFontSizeMultiplier != 0 && maxFontSizeMultiplier < 1 ) {
92
+ throw new JSApplicationIllegalArgumentException ("maxFontSizeMultiplier must be NaN, 0, or >= 1" );
93
+ }
94
+ mMaxFontSizeMultiplier = maxFontSizeMultiplier ;
95
+ }
96
+
80
97
public float getHeightOfTallestInlineImage () {
81
98
return mHeightOfTallestInlineImage ;
82
99
}
@@ -94,7 +111,7 @@ public void setHeightOfTallestInlineImage(float value) {
94
111
public int getEffectiveFontSize () {
95
112
float fontSize = !Float .isNaN (mFontSize ) ? mFontSize : ViewDefaults .FONT_SIZE_SP ;
96
113
return mAllowFontScaling
97
- ? (int ) Math .ceil (PixelUtil .toPixelFromSP (fontSize ))
114
+ ? (int ) Math .ceil (PixelUtil .toPixelFromSP (fontSize , getEffectiveMaxFontSizeMultiplier () ))
98
115
: (int ) Math .ceil (PixelUtil .toPixelFromDIP (fontSize ));
99
116
}
100
117
@@ -104,7 +121,7 @@ public float getEffectiveLineHeight() {
104
121
}
105
122
106
123
float lineHeight = mAllowFontScaling
107
- ? PixelUtil .toPixelFromSP (mLineHeight )
124
+ ? PixelUtil .toPixelFromSP (mLineHeight , getEffectiveMaxFontSizeMultiplier () )
108
125
: PixelUtil .toPixelFromDIP (mLineHeight );
109
126
110
127
// Take into account the requested line height
@@ -121,14 +138,21 @@ public float getEffectiveLetterSpacing() {
121
138
}
122
139
123
140
float letterSpacingPixels = mAllowFontScaling
124
- ? PixelUtil .toPixelFromSP (mLetterSpacing )
141
+ ? PixelUtil .toPixelFromSP (mLetterSpacing , getEffectiveMaxFontSizeMultiplier () )
125
142
: PixelUtil .toPixelFromDIP (mLetterSpacing );
126
143
127
144
// `letterSpacingPixels` and `getEffectiveFontSize` are both in pixels,
128
145
// yielding an accurate em value.
129
146
return letterSpacingPixels / getEffectiveFontSize ();
130
147
}
131
148
149
+ // Never returns NaN
150
+ public float getEffectiveMaxFontSizeMultiplier () {
151
+ return !Float .isNaN (mMaxFontSizeMultiplier )
152
+ ? mMaxFontSizeMultiplier
153
+ : DEFAULT_MAX_FONT_SIZE_MULTIPLIER ;
154
+ }
155
+
132
156
public String toString () {
133
157
return (
134
158
"TextAttributes {"
@@ -140,6 +164,8 @@ public String toString() {
140
164
+ "\n getEffectiveLetterSpacing(): " + getEffectiveLetterSpacing ()
141
165
+ "\n getLineHeight(): " + getLineHeight ()
142
166
+ "\n getEffectiveLineHeight(): " + getEffectiveLineHeight ()
167
+ + "\n getMaxFontSizeMultiplier(): " + getMaxFontSizeMultiplier ()
168
+ + "\n getEffectiveMaxFontSizeMultiplier(): " + getEffectiveMaxFontSizeMultiplier ()
143
169
+ "\n }"
144
170
);
145
171
}
0 commit comments