@@ -37,6 +37,7 @@ class TextBounds {
3737 private int mTextEnd ;
3838 private String mText ;
3939 private float mLineHeight ;
40+ private boolean mUseAbsoluteSizes ;
4041
4142 public RNLinearTextGradientSpan (
4243 float [] locations ,
@@ -49,7 +50,8 @@ public RNLinearTextGradientSpan(
4950 int textStart ,
5051 int textEnd ,
5152 String text ,
52- float lineHeight
53+ float lineHeight ,
54+ boolean useAbsoluteSizes
5355 ) {
5456 mLocations = locations ;
5557 mColors = colors ;
@@ -62,6 +64,7 @@ public RNLinearTextGradientSpan(
6264 mTextEnd = textEnd ;
6365 mText = text ;
6466 mLineHeight = lineHeight ;
67+ mUseAbsoluteSizes = useAbsoluteSizes ;
6568 }
6669
6770 @ Override
@@ -73,7 +76,12 @@ public void updateDrawState(TextPaint paint) {
7376 mText != null &&
7477 !YogaConstants .isUndefined (mMaxWidth )
7578 ) {
76- float lineHeight = (int ) Math .ceil (Float .isNaN (mLineHeight ) ? paint .getFontSpacing () : mLineHeight );
79+
80+ FontMetrics metrics = paint .getFontMetrics ();
81+
82+ float lineHeight = (int ) Math .ceil (
83+ Float .isNaN (mLineHeight ) ? (metrics .descent - metrics .ascent ) : mLineHeight
84+ );
7785
7886 TextBounds rectBeforeGradient = textBounds (
7987 mText .substring (0 , mTextStart ),
@@ -93,18 +101,24 @@ public void updateDrawState(TextPaint paint) {
93101
94102 float width = mUseViewFrame ? mMaxWidth : gradientRect .width ;
95103 float height = mUseViewFrame ? mMaxHeight : gradientRect .height ;
104+ float x0 = mUseAbsoluteSizes ? mStart [0 ] : (gradientRect .left + mStart [0 ] * width );
105+ float y0 = mUseAbsoluteSizes ? mStart [1 ] : (gradientRect .top + mStart [1 ] * height );
106+ float x1 = mUseAbsoluteSizes ? mEnd [0 ] : (gradientRect .left + mEnd [0 ] * width );
107+ float y1 = mUseAbsoluteSizes ? mEnd [1 ] : (gradientRect .top + mEnd [1 ] * height );
96108
97109 // Log.d(ReactConstants.TAG, "before: 0 - " + String.valueOf(mTextStart) + " " + textBoundsAsString(rectBeforeGradient));
98110 // Log.d(ReactConstants.TAG, "gradient: " + String.valueOf(mTextStart) + " - " + String.valueOf(mTextEnd) + " " + textBoundsAsString(gradientRect));
99- // Log.d(ReactConstants.TAG, "width " + String.valueOf(width) + " height " + String.valueOf(height));
100- // Log.d(ReactConstants.TAG, "lineHeight " + String.valueOf(lineHeight) + " spacing " + String.valueOf(paint.getFontSpacing()));
101- // Log.d(ReactConstants.TAG, "text " + mText);
111+ // Log.d(ReactConstants.TAG, "width " + String.valueOf(width) + " height " + String.valueOf(height) + " maxWidth " + String.valueOf(mMaxWidth));
112+ // Log.d(ReactConstants.TAG, "lineHeight " + String.valueOf(lineHeight) + " metrics " + String.valueOf(metrics.bottom - metrics.top));
113+ // Log.d(ReactConstants.TAG, "text " + mText.substring(mTextStart, mTextEnd));
114+ // Log.d(ReactConstants.TAG, "metrics " + "top: " + String.valueOf(metrics.top) + " ascent: " + String.valueOf(metrics.ascent) + " descent: " + String.valueOf(metrics.descent) + " bottom: " + String.valueOf(metrics.bottom) + " leading: " + String.valueOf(metrics.leading));
115+ // Log.d(ReactConstants.TAG, "coord " + String.valueOf(mUseAbsoluteSizes) + " x0: " + String.valueOf(x0) + " y0: " + String.valueOf(y0)+ " x1: " + String.valueOf(x1)+ " y1: " + String.valueOf(y1));
102116
103117 LinearGradient gradient = new LinearGradient (
104- gradientRect . left + mStart [ 0 ] * width ,
105- gradientRect . top + mStart [ 1 ] * height ,
106- gradientRect . left + mEnd [ 0 ] * width ,
107- gradientRect . top + mEnd [ 1 ] * height ,
118+ x0 ,
119+ y0 ,
120+ x1 ,
121+ y1 ,
108122 mColors ,
109123 mLocations ,
110124 Shader .TileMode .CLAMP
@@ -115,9 +129,9 @@ public void updateDrawState(TextPaint paint) {
115129 }
116130 }
117131
118- // private String textBoundsAsString(TextBounds bounds) {
119- // return "TextBounds(t: " + bounds.top + " l: " + bounds.left + " b: " + bounds.bottom + " r: " + bounds.right + " h: " + bounds.height + " w: " + bounds.width + " )";
120- // }
132+ private String textBoundsAsString (TextBounds bounds ) {
133+ return "TextBounds(t: " + bounds .top + " l: " + bounds .left + " b: " + bounds .bottom + " r: " + bounds .right + " h: " + bounds .height + " w: " + bounds .width + " )" ;
134+ }
121135
122136 private TextBounds textBounds (String text , Paint paint , float startX , float startY , float lineHeight ) {
123137 TextBounds bounds = new TextBounds ();
@@ -133,21 +147,21 @@ private TextBounds textBounds(String text, Paint paint, float startX, float star
133147 float lineOffset = startX ;
134148
135149 while (lineEnd <= text .length ()) {
136- float lineWidth = paint .measureText (mText , lineStart , lineEnd ) + lineOffset ;
150+ float lineWidth = paint .measureText (text , lineStart , lineEnd ) + lineOffset ;
137151
138152 if (lineWidth > mMaxWidth ) {
139153 if (lineEnd == 1 ) {
140154 bounds .top += lineHeight ;
141155 bounds .left = 0 ;
142156 } else {
143157 bounds .width = Math .max (lineWidth , bounds .width );
158+ bounds .height += lineHeight ;
144159 }
145160
146161 lineStart = lineEnd - 1 ;
147162 lineOffset = 0 ;
148163 bounds .bottom += lineHeight ;
149164 bounds .right = 0 ;
150- bounds .height += lineHeight ;
151165 bounds .left = 0 ;
152166 } else {
153167 lineEnd ++;
0 commit comments