Resolves style issues in CustomTypefaceSpan

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:
This commit is contained in:
Cyrus Bakhtiari-Haftlang 2020-09-30 09:26:56 +02:00 committed by Dimitry Ivanov
parent 7002dbfb8d
commit 89e89c766e

View File

@ -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;
@ -34,11 +35,22 @@ public class CustomTypefaceSpan extends MetricAffectingSpan {
}
@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);
}
}