88package com .facebook .react .views .text ;
99
1010import android .content .res .AssetManager ;
11+ import android .graphics .Paint ;
1112import android .graphics .Typeface ;
1213import android .text .TextPaint ;
1314import android .text .style .MetricAffectingSpan ;
@@ -34,10 +35,6 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
3435 private final int mWeight ;
3536 private final @ Nullable String mFeatureSettings ;
3637 private final @ Nullable String mFontFamily ;
37- private int mSize = 0 ;
38- private TextAlignVertical mTextAlignVertical = TextAlignVertical .CENTER ;
39- private int mHighestLineHeight = 0 ;
40- private int mHighestFontSize = 0 ;
4138
4239 public CustomStyleSpan (
4340 int fontStyle ,
@@ -52,61 +49,14 @@ public CustomStyleSpan(
5249 mAssetManager = assetManager ;
5350 }
5451
55- public CustomStyleSpan (
56- int fontStyle ,
57- int fontWeight ,
58- @ Nullable String fontFeatureSettings ,
59- @ Nullable String fontFamily ,
60- AssetManager assetManager ,
61- TextAlignVertical textAlignVertical ,
62- int textSize ) {
63- this (fontStyle , fontWeight , fontFeatureSettings , fontFamily , assetManager );
64- mTextAlignVertical = textAlignVertical ;
65- mSize = textSize ;
66- }
67-
68- public enum TextAlignVertical {
69- TOP ,
70- BOTTOM ,
71- CENTER ,
72- }
73-
74- public TextAlignVertical getTextAlignVertical () {
75- return mTextAlignVertical ;
76- }
77-
78- public int getSize () {
79- return mSize ;
80- }
81-
8252 @ Override
8353 public void updateDrawState (TextPaint ds ) {
84- apply (
85- ds ,
86- mStyle ,
87- mWeight ,
88- mFeatureSettings ,
89- mFontFamily ,
90- mAssetManager ,
91- mTextAlignVertical ,
92- mSize ,
93- mHighestLineHeight ,
94- mHighestFontSize );
54+ apply (ds , mStyle , mWeight , mFeatureSettings , mFontFamily , mAssetManager );
9555 }
9656
9757 @ Override
9858 public void updateMeasureState (TextPaint paint ) {
99- apply (
100- paint ,
101- mStyle ,
102- mWeight ,
103- mFeatureSettings ,
104- mFontFamily ,
105- mAssetManager ,
106- mTextAlignVertical ,
107- mSize ,
108- 0 ,
109- 0 );
59+ apply (paint , mStyle , mWeight , mFeatureSettings , mFontFamily , mAssetManager );
11060 }
11161
11262 public int getStyle () {
@@ -122,68 +72,16 @@ public int getWeight() {
12272 }
12373
12474 private static void apply (
125- TextPaint ds ,
75+ Paint paint ,
12676 int style ,
12777 int weight ,
12878 @ Nullable String fontFeatureSettings ,
12979 @ Nullable String family ,
130- AssetManager assetManager ,
131- TextAlignVertical textAlignVertical ,
132- int textSize ,
133- int highestLineHeight ,
134- int highestFontSize ) {
80+ AssetManager assetManager ) {
13581 Typeface typeface =
136- ReactTypefaceUtils .applyStyles (ds .getTypeface (), style , weight , family , assetManager );
137- ds .setFontFeatureSettings (fontFeatureSettings );
138- ds .setTypeface (typeface );
139- ds .setSubpixelText (true );
140-
141- if (textAlignVertical == TextAlignVertical .CENTER || highestLineHeight == 0 ) {
142- return ;
143- }
144-
145- // https://stackoverflow.com/a/27631737/7295772
146- // top ------------- -10
147- // ascent ------------- -5
148- // baseline __my Text____ 0
149- // descent _____________ 2
150- // bottom _____________ 5
151- TextPaint textPaintCopy = new TextPaint ();
152- textPaintCopy .set (ds );
153- if (textSize > 0 ) {
154- textPaintCopy .setTextSize (textSize );
155- }
156-
157- if (textSize == highestFontSize ) {
158- // aligns text vertically in the lineHeight
159- // and adjust their position depending on the fontSize
160- if (textAlignVertical == TextAlignVertical .TOP ) {
161- ds .baselineShift -= highestLineHeight / 2 - textPaintCopy .getTextSize () / 2 ;
162- }
163- if (textAlignVertical == TextAlignVertical .BOTTOM ) {
164- ds .baselineShift +=
165- highestLineHeight / 2 - textPaintCopy .getTextSize () / 2 - textPaintCopy .descent ();
166- }
167- } else if (highestFontSize != 0 && textSize < highestFontSize ) {
168- // aligns correctly text that has smaller font
169- if (textAlignVertical == TextAlignVertical .TOP ) {
170- ds .baselineShift -=
171- highestLineHeight / 2
172- - highestFontSize / 2
173- // smaller font aligns on the baseline of bigger font
174- // moves the baseline of text with smaller font up
175- // so it aligns on the top of the larger font
176- + (highestFontSize - textSize )
177- + (textPaintCopy .getFontMetrics ().top - textPaintCopy .ascent ());
178- }
179- if (textAlignVertical == TextAlignVertical .BOTTOM ) {
180- ds .baselineShift += highestLineHeight / 2 - highestFontSize / 2 - textPaintCopy .descent ();
181- }
182- }
183- }
184-
185- public void updateSpan (int highestLineHeight , int highestFontSize ) {
186- mHighestLineHeight = highestLineHeight ;
187- mHighestFontSize = highestFontSize ;
82+ ReactTypefaceUtils .applyStyles (paint .getTypeface (), style , weight , family , assetManager );
83+ paint .setFontFeatureSettings (fontFeatureSettings );
84+ paint .setTypeface (typeface );
85+ paint .setSubpixelText (true );
18886 }
18987}
0 commit comments