Skip to content

🗑️ Calculate font top and bottom with different fontSizes - CLOSED 🗑️ #9

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 1 commit 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 @@ -109,10 +109,10 @@ private static void apply(
// other use cases will be added in separate PRs
// the span with the highest lineHeight sets the height for all rows
if (textAlignVertical == "top-child" && highestLineHeight != 0) {
tp.baselineShift -= highestLineHeight / 2 - tp.getTextSize() / 2;
// tp.baselineShift -= highestLineHeight / 2 - tp.getTextSize() / 2;
}
if (textAlignVertical == "bottom-child" && highestLineHeight != 0) {
tp.baselineShift += highestLineHeight / 2 - tp.getTextSize() / 2;
// tp.baselineShift += highestLineHeight / 2 - tp.getTextSize() / 2;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,51 @@

package com.facebook.react.views.text;

import android.graphics.Rect;
import android.text.TextPaint;
import android.text.style.AbsoluteSizeSpan;
import androidx.annotation.Nullable;

/*
* Wraps {@link AbsoluteSizeSpan} as a {@link ReactSpan}.
*/
public class ReactAbsoluteSizeSpan extends AbsoluteSizeSpan implements ReactSpan {
public ReactAbsoluteSizeSpan(int size) {
private static final String TAG = "ReactAbsoluteSizeSpan";
private final String mText;
private String mTextAlignVertical = "center-child";

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

@Override
public void updateMeasureState(TextPaint tp) {
updateDrawState(tp);
}

@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
// if lineHeight is not set, align the text using the font metrics
// works only with single line
// https://stackoverflow.com/a/27631737/7295772
// top ------------- -26
// ascent ------------- -30
// baseline __my Text____ 0
// descent _____________ 8
// bottom _____________ 1
if (mText != null) {
Rect bounds = new Rect();
ds.getTextBounds(mText, 0, mText.length(), bounds);
if (mTextAlignVertical == "top-child") {
ds.baselineShift += ds.getFontMetrics().top - ds.ascent() - ds.descent();
}
if (mTextAlignVertical == "bottom-child") {
ds.baselineShift += ds.getFontMetrics().bottom - ds.descent();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private static void buildSpannedFromShadowNode(
// `Float.NaN`.
parentTextAttributes == null
|| parentTextAttributes.getEffectiveFontSize() != effectiveFontSize) {
ops.add(new SetSpanOperation(start, end, new ReactAbsoluteSizeSpan(effectiveFontSize)));
ops.add(new SetSpanOperation(start, end, new ReactAbsoluteSizeSpan(effectiveFontSize, null, null)));
}
if (textShadowNode.mFontStyle != UNSET
|| textShadowNode.mFontWeight != UNSET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public long measure(
for (ReactAbsoluteSizeSpan span : sizeSpans) {
text.setSpan(
new ReactAbsoluteSizeSpan(
(int) Math.max((span.getSize() * ratio), minimumFontSize)),
(int) Math.max((span.getSize() * ratio), minimumFontSize), null, null),
text.getSpanStart(span),
text.getSpanEnd(span),
text.getSpanFlags(span));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ 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, null, null)));
if (textAttributes.mFontStyle != UNSET
|| textAttributes.mFontWeight != UNSET
|| textAttributes.mFontFamily != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,13 @@ private static void buildSpannableFromFragment(
new SetSpanOperation(
start, end, new CustomLetterSpacingSpan(textAttributes.getLetterSpacing())));
}
String currentText = String.valueOf(sb.subSequence(start, end));
ops.add(
new SetSpanOperation(start, end, new ReactAbsoluteSizeSpan(textAttributes.mFontSize)));
new SetSpanOperation(
start,
end,
new ReactAbsoluteSizeSpan(
textAttributes.mFontSize, textAttributes.mTextAlignVertical, currentText)));
if (textAttributes.mFontStyle != UNSET
|| textAttributes.mFontWeight != UNSET
|| textAttributes.mFontFamily != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ private void addSpansForMeasurement(Spannable spannable) {
}
ops.add(
new TextLayoutManager.SetSpanOperation(
start, end, new ReactAbsoluteSizeSpan((int) mTextAttributes.getEffectiveFontSize())));
start, end, new ReactAbsoluteSizeSpan((int) mTextAttributes.getEffectiveFontSize(), null, null)));
if (mFontStyle != UNSET || mFontWeight != UNSET || mFontFamily != null) {
ops.add(
new TextLayoutManager.SetSpanOperation(
Expand Down