Skip to content

Commit

Permalink
Support custom font family for codeblocks and inline code on Android (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw authored Jan 18, 2024
1 parent 1d3e118 commit c823d1f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
package com.expensify.livemarkdown;

import android.text.style.TypefaceSpan;
import android.content.res.AssetManager;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;

import androidx.annotation.NonNull;

public class MarkdownFontFamilySpan extends TypefaceSpan implements MarkdownSpan {
public MarkdownFontFamilySpan(@NonNull String fontFamily) {
super(fontFamily);
import com.facebook.react.views.text.ReactFontManager;

public class MarkdownFontFamilySpan extends MetricAffectingSpan implements MarkdownSpan {

private final @NonNull String mFontFamily;
private final @NonNull AssetManager mAssetManager;

public MarkdownFontFamilySpan(@NonNull String fontFamily, @NonNull AssetManager assetManager) {
mFontFamily = fontFamily;
mAssetManager = assetManager;
}

@Override
public void updateMeasureState(@NonNull TextPaint textPaint) {
apply(textPaint);
}

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

private void apply(@NonNull TextPaint textPaint) {
int style = textPaint.getTypeface().getStyle();
Typeface typeface = ReactFontManager.getInstance().getTypeface(mFontFamily, style, mAssetManager);
textPaint.setTypeface(typeface);
textPaint.setFlags(textPaint.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import androidx.annotation.Nullable;

import android.content.Context;
import android.content.res.AssetManager;
import android.text.TextWatcher;
import android.util.AttributeSet;

Expand Down Expand Up @@ -51,8 +52,9 @@ protected void onAttachedToWindow() {
}

if (previousSibling instanceof ReactEditText) {
MarkdownUtils.maybeInitializeRuntime(getContext().getAssets());
mMarkdownUtils = new MarkdownUtils();
AssetManager assetManager = getContext().getAssets();
MarkdownUtils.maybeInitializeRuntime(assetManager);
mMarkdownUtils = new MarkdownUtils(assetManager);
mMarkdownUtils.setMarkdownStyle(mMarkdownStyle);
mReactEditText = (ReactEditText) previousSibling;
mTextWatcher = new MarkdownTextWatcher(mMarkdownUtils);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import androidx.annotation.NonNull;

import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.views.text.CustomLineHeightSpan;
Expand Down Expand Up @@ -56,6 +57,12 @@ private static String parseMarkdown(String input) {

private static native String nativeParseMarkdown(String input);

public MarkdownUtils(@NonNull AssetManager assetManager) {
mAssetManager = assetManager;
}

private final @NonNull AssetManager mAssetManager;

private String mPrevInput;

private String mPrevOutput;
Expand Down Expand Up @@ -124,12 +131,12 @@ private void applyRange(SpannableStringBuilder ssb, String type, int start, int
setSpan(ssb, new MarkdownForegroundColorSpan(mMarkdownStyle.getLinkColor()), start, end);
break;
case "code":
setSpan(ssb, new MarkdownFontFamilySpan(mMarkdownStyle.getCodeFontFamily()), start, end);
setSpan(ssb, new MarkdownFontFamilySpan(mMarkdownStyle.getCodeFontFamily(), mAssetManager), start, end);
setSpan(ssb, new MarkdownForegroundColorSpan(mMarkdownStyle.getCodeColor()), start, end);
setSpan(ssb, new MarkdownBackgroundColorSpan(mMarkdownStyle.getCodeBackgroundColor()), start, end);
break;
case "pre":
setSpan(ssb, new MarkdownFontFamilySpan(mMarkdownStyle.getPreFontFamily()), start, end);
setSpan(ssb, new MarkdownFontFamilySpan(mMarkdownStyle.getPreFontFamily(), mAssetManager), start, end);
setSpan(ssb, new MarkdownForegroundColorSpan(mMarkdownStyle.getPreColor()), start, end);
setSpan(ssb, new MarkdownBackgroundColorSpan(mMarkdownStyle.getPreBackgroundColor()), start, end);
break;
Expand Down

0 comments on commit c823d1f

Please sign in to comment.