Skip to content

Commit

Permalink
Resolves style issues in CustomTypefaceSpan
Browse files Browse the repository at this point in the history
When two or more `CustomTypefaceSpan` are applied on a same character
sequence, previous `TextPaint`'s style was not respected.

Example:
--------
**Strong Emphasis _strong emphasis and emphasis combined_**

Before the inner text will only have the font applied without respecting
that the outer text is strong.

Related (bug report)[https://issuetracker.google.com/issues/169658958]
on Google:
  • Loading branch information
c-b-h committed Sep 30, 2020
1 parent 05b78e9 commit d6583ea
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.noties.markwon.core.spans;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
Expand Down Expand Up @@ -34,11 +35,22 @@ public void updateMeasureState(@NonNull TextPaint p) {
}

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

private void updatePaint(@NonNull TextPaint paint) {
paint.setTypeface(typeface);
final Typeface old = paint.getTypeface();
final int oldStyle;
if (old == null) {
oldStyle = Typeface.NORMAL;
} else {
oldStyle = old.getStyle();
}

@SuppressLint("WrongConstant") final int want = oldStyle | typeface.getStyle();
final Typeface styledTypeface = Typeface.create(typeface, want);

paint.setTypeface(styledTypeface);
}
}

0 comments on commit d6583ea

Please sign in to comment.