diff --git a/markwon-core/src/main/java/ru/noties/markwon/core/spans/CustomTypefaceSpan.java b/markwon-core/src/main/java/ru/noties/markwon/core/spans/CustomTypefaceSpan.java new file mode 100644 index 00000000..99ae2111 --- /dev/null +++ b/markwon-core/src/main/java/ru/noties/markwon/core/spans/CustomTypefaceSpan.java @@ -0,0 +1,43 @@ +package ru.noties.markwon.core.spans; + +import android.graphics.Typeface; +import android.support.annotation.NonNull; +import android.text.TextPaint; +import android.text.style.MetricAffectingSpan; + +/** + * A span implementation that allow applying custom Typeface. Although it is + * not used directly by the library, it\'s helpful for customizations. + *
+ * Please note that this implementation does not validate current paint state + * and won\'t be updating/modifying supplied Typeface. + * + * @since 3.0.0 + */ +public class CustomTypefaceSpan extends MetricAffectingSpan { + + @NonNull + public static CustomTypefaceSpan create(@NonNull Typeface typeface) { + return new CustomTypefaceSpan(typeface); + } + + private final Typeface typeface; + + public CustomTypefaceSpan(@NonNull Typeface typeface) { + this.typeface = typeface; + } + + @Override + public void updateMeasureState(@NonNull TextPaint p) { + updatePaint(p); + } + + @Override + public void updateDrawState(TextPaint tp) { + updatePaint(tp); + } + + private void updatePaint(@NonNull TextPaint paint) { + paint.setTypeface(typeface); + } +}