Skip to content

🗑️ support fontSize in nested text vertical align - CLOSED 🗑️ #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Nullsafe;

@Nullsafe(Nullsafe.Mode.LOCAL)
Expand All @@ -38,6 +39,7 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
private final String mCurrentText;
private String mTextAlignVertical;
private int mHighestLineHeight;
private static float mFontSize;

public CustomStyleSpan(
int fontStyle,
Expand All @@ -46,14 +48,16 @@ public CustomStyleSpan(
@Nullable String fontFamily,
AssetManager assetManager,
@Nullable String textAlignVertical,
String currentText) {
String currentText,
@Nullable int fontSize) {
mStyle = fontStyle;
mWeight = fontWeight;
mFeatureSettings = fontFeatureSettings;
mFontFamily = fontFamily;
mAssetManager = assetManager;
mTextAlignVertical = textAlignVertical;
mCurrentText = currentText;
mFontSize = fontSize;
}

@Override
Expand Down Expand Up @@ -117,29 +121,47 @@ private static void apply(
paint.getTextBounds(currentText, 0, currentText.length(), bounds);
if (textAlignVertical == "top-child") {
if (highestLineHeight != 0) {
// the span with the highest lineHeight over-rides sets the height for all rows
// the span with the highest lineHeight sets the height for all rows
paint.baselineShift -= highestLineHeight / 2 - paint.getTextSize() / 2;
} else {
String methodName = new Object() {}.getClass().getEnclosingMethod().getName();
FLog.w(
"React::" + "CustomStyleSpan",
methodName
+ " mCurrentText: "
+ (currentText)
+ " paint.getFontMetrics().top: "
+ (paint.getFontMetrics().top)
+ " paint.ascent(): "
+ (paint.ascent())
+ " paint.baselineShift: "
+ (paint.baselineShift)
+ " paint.getFontMetrics().bottom: "
+ (paint.getFontMetrics().bottom)
+ " paint.descent(): "
+ (paint.descent())
+ " paint.getTextSize(): "
+ (paint.getTextSize())
+ " mFontSize: "
+ (mFontSize));
// works only with single line
// if lineHeight is not set, align the text using the font metrics
// https://stackoverflow.com/a/27631737/7295772
// top ------------- -26
// ascent ------------- -30
// baseline __my Text____ 0
// descent _____________ 8
// bottom _____________ 1
paint.baselineShift -= bounds.top + bounds.bottom - paint.ascent();
// top ------------- -41 ^ ^
// ascent ------------- -36 | -51 | +36 ^ -15
// baseline __my Text____ 0 | v ^
// descent _____________ 9 | ^
// bottom _____________ 10 |
// paint.baselineShift +=
// (paint.getFontMetrics().top - paint.getFontMetrics().ascent) * mFontSize / 33;
}
}
if (textAlignVertical == "bottom-child") {
if (highestLineHeight != 0) {
// the span with the highest lineHeight over-rides sets the height for all rows
paint.baselineShift += highestLineHeight / 2 - paint.getTextSize() / 2;
} else {
// works only with single line
// if lineHeight is not set, align the text using the font metrics
// https://stackoverflow.com/a/27631737/7295772
paint.baselineShift += paint.descent();
// paint.baselineShift += (paint.getFontMetrics().bottom - paint.descent()) * mFontSize /
// 33;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@

package com.facebook.react.views.text;

import android.text.TextPaint;
import android.text.style.AbsoluteSizeSpan;

/*
* Wraps {@link AbsoluteSizeSpan} as a {@link ReactSpan}.
*/
public class ReactAbsoluteSizeSpan extends AbsoluteSizeSpan implements ReactSpan {
public ReactAbsoluteSizeSpan(int size) {
private String mTextAlignVertical;

public ReactAbsoluteSizeSpan(int size, @Nullable String textAlignVertical) {
super(size);
mTextAlignVertical = textVerticalAlign;
}

@Override
public void updateDrawState(TextPaint ds) {
if (mTextAlignVertical == "top-child") {
ds.baselineShift += 50;
}
}

@Override
public void updateMeasureState(TextPaint paint) {
// paint.baselineShift += 50;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private static void buildSpannedFromShadowNode(
textShadowNode.mFontWeight,
textShadowNode.mFontFeatureSettings,
textShadowNode.mFontFamily,
textShadowNode.getThemedContext().getAssets(), /* textAlignVertical not supported on Paper */ null, null)));
textShadowNode.getThemedContext().getAssets(), /* textAlignVertical not supported on Paper */ null, null, 0)));
}
if (textShadowNode.mIsUnderlineTextDecorationSet) {
ops.add(new SetSpanOperation(start, end, new ReactUnderlineSpan()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private static void buildSpannableFromFragment(
textAttributes.mFontWeight,
textAttributes.mFontFeatureSettings,
textAttributes.mFontFamily,
context.getAssets(), /* textAlignVertical not supported on Paper */ null, null)));
context.getAssets(), /* textAlignVertical not supported on Paper */ null, null, 0)));
}
if (textAttributes.mIsUnderlineTextDecorationSet) {
ops.add(new SetSpanOperation(start, end, new ReactUnderlineSpan()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ private static void buildSpannableFromFragment(
start, end, new CustomLetterSpacingSpan(textAttributes.getLetterSpacing())));
}
ops.add(
new SetSpanOperation(start, end, new ReactAbsoluteSizeSpan(textAttributes.mFontSize)));
new SetSpanOperation(
start,
end,
new ReactAbsoluteSizeSpan(
textAttributes.mFontSize, textAttributes.mVerticalAlign)));
if (textAttributes.mFontStyle != UNSET
|| textAttributes.mFontWeight != UNSET
|| textAttributes.mFontFamily != null
Expand All @@ -176,7 +180,8 @@ private static void buildSpannableFromFragment(
textAttributes.mFontFamily,
context.getAssets(),
textAttributes.mVerticalAlign,
currentText)));
currentText,
textAttributes.mFontSize)));
}
if (textAttributes.mIsUnderlineTextDecorationSet) {
ops.add(new SetSpanOperation(start, end, new ReactUnderlineSpan()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ private void addSpansForMeasurement(Spannable spannable) {
mFontWeight,
null, // TODO: do we need to support FontFeatureSettings / fontVariant?
mFontFamily,
getReactContext(ReactEditText.this).getAssets(), /* textAlignVertical not supported on TextInput */ null, null)));
getReactContext(ReactEditText.this).getAssets(), /* textAlignVertical not supported on TextInput */ null, null, 0)));
}
if (!Float.isNaN(mTextAttributes.getEffectiveLineHeight())) {
ops.add(
Expand Down