diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java index 1a379ca443791d..b249126cf95750 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java @@ -8,6 +8,7 @@ package com.facebook.react.views.text; import android.content.res.AssetManager; +import android.graphics.Paint; import android.graphics.Typeface; import android.text.TextPaint; import android.text.style.MetricAffectingSpan; @@ -34,10 +35,6 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan { private final int mWeight; private final @Nullable String mFeatureSettings; private final @Nullable String mFontFamily; - private int mSize = 0; - private TextAlignVertical mTextAlignVertical = TextAlignVertical.CENTER; - private int mHighestLineHeight = 0; - private int mHighestFontSize = 0; public CustomStyleSpan( int fontStyle, @@ -52,61 +49,14 @@ public CustomStyleSpan( mAssetManager = assetManager; } - public CustomStyleSpan( - int fontStyle, - int fontWeight, - @Nullable String fontFeatureSettings, - @Nullable String fontFamily, - AssetManager assetManager, - TextAlignVertical textAlignVertical, - int textSize) { - this(fontStyle, fontWeight, fontFeatureSettings, fontFamily, assetManager); - mTextAlignVertical = textAlignVertical; - mSize = textSize; - } - - public enum TextAlignVertical { - TOP, - BOTTOM, - CENTER, - } - - public TextAlignVertical getTextAlignVertical() { - return mTextAlignVertical; - } - - public int getSize() { - return mSize; - } - @Override public void updateDrawState(TextPaint ds) { - apply( - ds, - mStyle, - mWeight, - mFeatureSettings, - mFontFamily, - mAssetManager, - mTextAlignVertical, - mSize, - mHighestLineHeight, - mHighestFontSize); + apply(ds, mStyle, mWeight, mFeatureSettings, mFontFamily, mAssetManager); } @Override public void updateMeasureState(TextPaint paint) { - apply( - paint, - mStyle, - mWeight, - mFeatureSettings, - mFontFamily, - mAssetManager, - mTextAlignVertical, - mSize, - 0, - 0); + apply(paint, mStyle, mWeight, mFeatureSettings, mFontFamily, mAssetManager); } public int getStyle() { @@ -122,68 +72,16 @@ public int getWeight() { } private static void apply( - TextPaint ds, + Paint paint, int style, int weight, @Nullable String fontFeatureSettings, @Nullable String family, - AssetManager assetManager, - TextAlignVertical textAlignVertical, - int textSize, - int highestLineHeight, - int highestFontSize) { + AssetManager assetManager) { Typeface typeface = - ReactTypefaceUtils.applyStyles(ds.getTypeface(), style, weight, family, assetManager); - ds.setFontFeatureSettings(fontFeatureSettings); - ds.setTypeface(typeface); - ds.setSubpixelText(true); - - if (textAlignVertical == TextAlignVertical.CENTER || highestLineHeight == 0) { - return; - } - - // https://stackoverflow.com/a/27631737/7295772 - // top ------------- -10 - // ascent ------------- -5 - // baseline __my Text____ 0 - // descent _____________ 2 - // bottom _____________ 5 - TextPaint textPaintCopy = new TextPaint(); - textPaintCopy.set(ds); - if (textSize > 0) { - textPaintCopy.setTextSize(textSize); - } - - if (textSize == highestFontSize) { - // aligns text vertically in the lineHeight - // and adjust their position depending on the fontSize - if (textAlignVertical == TextAlignVertical.TOP) { - ds.baselineShift -= highestLineHeight / 2 - textPaintCopy.getTextSize() / 2; - } - if (textAlignVertical == TextAlignVertical.BOTTOM) { - ds.baselineShift += - highestLineHeight / 2 - textPaintCopy.getTextSize() / 2 - textPaintCopy.descent(); - } - } else if (highestFontSize != 0 && textSize < highestFontSize) { - // aligns correctly text that has smaller font - if (textAlignVertical == TextAlignVertical.TOP) { - ds.baselineShift -= - highestLineHeight / 2 - - highestFontSize / 2 - // smaller font aligns on the baseline of bigger font - // moves the baseline of text with smaller font up - // so it aligns on the top of the larger font - + (highestFontSize - textSize) - + (textPaintCopy.getFontMetrics().top - textPaintCopy.ascent()); - } - if (textAlignVertical == TextAlignVertical.BOTTOM) { - ds.baselineShift += highestLineHeight / 2 - highestFontSize / 2 - textPaintCopy.descent(); - } - } - } - - public void updateSpan(int highestLineHeight, int highestFontSize) { - mHighestLineHeight = highestLineHeight; - mHighestFontSize = highestFontSize; + ReactTypefaceUtils.applyStyles(paint.getTypeface(), style, weight, family, assetManager); + paint.setFontFeatureSettings(fontFeatureSettings); + paint.setTypeface(typeface); + paint.setSubpixelText(true); } }