Skip to content

Commit

Permalink
Implement stroke style
Browse files Browse the repository at this point in the history
Summary:
# Context
We are working on migrating showreel bloks ads to native implementation and one important feature is TextOverlay. In showreel bloks, the text overlay rendering heavily rely on RichTextView. We tried using TextView instead (See diff stack D63923873) but experiment shows large frame drop regressions (https://fburl.com/gdoc/fcnxfgzs) which is likely caused by the dynamic text sizing logic. To make the native implementation as close as showreel bloks, we plan to switch to RichText litho component so we can reuse similar dynamic text sizing logic from showreel bloks.

Tech spec: https://fburl.com/gdoc/myn5fzc0

# This diff
* Implemented stroke style on text. android doesn't support text fill and text stroke being different colors so we draw the text twice once with fill and once with stroke

Reviewed By: xzhch1622

Differential Revision: D68187778

fbshipit-source-id: 34fcc8363d78e6c89fa0dd4a287a1e6a02f7edaa
  • Loading branch information
Ale Van Praag authored and facebook-github-bot committed Jan 21, 2025
1 parent 27aff84 commit 519f13c
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public class RCTextView extends View {
private boolean mShouldHandleTouch;
private boolean mShouldHandleKeyEvents;
@Nullable private Integer mWasFocusable;
private int mStrokeColor;
private float mStrokeWidth;

public RCTextView(Context context) {
super(context);
Expand Down Expand Up @@ -181,6 +183,9 @@ public void draw(@NonNull Canvas canvas) {
}

private void drawLayout(Canvas canvas) {
if (shouldDrawStrokeStyle()) {
drawStrokeStyle(canvas);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
Api34Utils.draw(mLayout, canvas, getSelectionPath(), mHighlightPaint);
} else {
Expand All @@ -189,6 +194,29 @@ private void drawLayout(Canvas canvas) {
}
}

private boolean shouldDrawStrokeStyle() {
return mStrokeColor != 0 && mStrokeWidth != 0;
}

private void drawStrokeStyle(Canvas canvas) {
if (mLayout == null) {
return;
}
Paint mPaint = mLayout.getPaint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(mStrokeColor);
mPaint.setStrokeWidth(mStrokeWidth);
mPaint.setStrokeJoin(Paint.Join.ROUND);
Path path = getSelectionPath();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
Api34Utils.draw(mLayout, canvas, path, mHighlightPaint);
} else if (path != null) {
mLayout.draw(canvas, path, mHighlightPaint, 0);
}
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mLinkColor);
}

private OnPrePostDrawSpan[] getOnPrePostDrawSpans() {
if (!(mText instanceof Spanned)) {
return new OnPrePostDrawSpan[0];
Expand Down Expand Up @@ -226,6 +254,8 @@ public void mount(TextLayout textLayout) {
.setColor(mColorStateList.getColorForState(getDrawableState(), mLinkColor));
}
}
mStrokeColor = textLayout.textStyle.strokeColor;
mStrokeWidth = textLayout.textStyle.strokeWidth;

if (highlightOffsetsValid(
mText,
Expand Down

0 comments on commit 519f13c

Please sign in to comment.