Skip to content

Commit

Permalink
Revert "fix: unable to render bold text for CJK character"
Browse files Browse the repository at this point in the history
This reverts commit d3eddfa.
  • Loading branch information
NextAlone committed Dec 13, 2023
1 parent ce9768a commit c5ced60
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
import xyz.nextalone.gen.Config;
import xyz.nextalone.nnngram.utils.AlertUtil;
import xyz.nextalone.nnngram.utils.AnalyticsUtils;
import xyz.nextalone.nnngram.utils.TypefaceUtils;

public class AndroidUtilities {
public final static int LIGHT_STATUS_BAR_OVERLAY = 0x0f000000, DARK_STATUS_BAR_OVERLAY = 0x33000000;
Expand Down Expand Up @@ -1941,17 +1942,43 @@ public static Typeface getTypeface(String assetPath) {
if (!typefaceCache.containsKey(assetPath)) {
try {
Typeface t;
if (Build.VERSION.SDK_INT >= 26) {
Typeface.Builder builder = new Typeface.Builder(ApplicationLoader.applicationContext.getAssets(), assetPath);
if (assetPath.contains("medium")) {
builder.setWeight(700);
}
if (assetPath.contains("italic")) {
builder.setItalic(true);
}
t = builder.build();
} else {
t = Typeface.createFromAsset(ApplicationLoader.applicationContext.getAssets(), assetPath);
switch (assetPath) {
case TYPEFACE_ROBOTO_MEDIUM:
if (TypefaceUtils.isMediumWeightSupported()) {
t = Typeface.create("sans-serif-medium", Typeface.NORMAL);
} else {
t = Typeface.create("sans-serif", Typeface.BOLD);
}
break;
case "fonts/ritalic.ttf":
t = Typeface.create("sans-serif", Typeface.ITALIC);
break;
case TYPEFACE_ROBOTO_MEDIUM_ITALIC:
if (TypefaceUtils.isMediumWeightSupported()) {
t = Typeface.create("sans-serif-medium", Typeface.ITALIC);
} else {
t = Typeface.create("sans-serif", Typeface.BOLD_ITALIC);
}
break;
case TYPEFACE_ROBOTO_MONO:
t = Typeface.MONOSPACE;
break;
case "fonts/rcondensedbold.ttf":
t = Typeface.create("sans-serif-condensed", Typeface.BOLD);
break;
default:
if (Build.VERSION.SDK_INT >= 26) {
Typeface.Builder builder = new Typeface.Builder(ApplicationLoader.applicationContext.getAssets(), assetPath);
if (assetPath.contains("medium")) {
builder.setWeight(700);
}
if (assetPath.contains("italic")) {
builder.setItalic(true);
}
t = builder.build();
} else {
t = Typeface.createFromAsset(ApplicationLoader.applicationContext.getAssets(), assetPath);
}
}
typefaceCache.put(assetPath, t);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.ActionBar.Theme;

import top.qwq2333.nullgram.utils.TypefaceUtils;

public class TextStyleSpan extends MetricAffectingSpan {

private int textSize;
Expand Down Expand Up @@ -78,11 +80,11 @@ public void applyStyle(TextPaint p) {
public Typeface getTypeface() {
if ((flags & FLAG_STYLE_MONO) != 0 || (flags & FLAG_STYLE_CODE) != 0) {
return Typeface.MONOSPACE;
} else if ((flags & FLAG_STYLE_BOLD) != 0 && (flags & FLAG_STYLE_ITALIC) != 0) {
} else if ((flags & FLAG_STYLE_BOLD) != 0 && (flags & FLAG_STYLE_ITALIC) != 0 && TypefaceUtils.isMediumWeightSupported() && TypefaceUtils.isItalicSupported()) {
return AndroidUtilities.getTypeface("fonts/rmediumitalic.ttf");
} else if ((flags & FLAG_STYLE_BOLD) != 0) {
} else if ((flags & FLAG_STYLE_BOLD) != 0 && TypefaceUtils.isMediumWeightSupported()) {
return AndroidUtilities.getTypeface("fonts/rmedium.ttf");
} else if ((flags & FLAG_STYLE_ITALIC) != 0) {
} else if ((flags & FLAG_STYLE_ITALIC) != 0 && TypefaceUtils.isItalicSupported()) {
return AndroidUtilities.getTypeface("fonts/ritalic.ttf");
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import android.text.Spanned;
import android.text.TextUtils;

import org.telegram.messenger.CodeHighlighting;
import org.telegram.messenger.LinkifyPort;
import org.telegram.messenger.MediaDataController;
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.Components.TextStyleSpan;
import org.telegram.ui.Components.URLSpanReplacement;

Expand Down Expand Up @@ -78,47 +78,39 @@ public static void parseMarkdown(CharSequence[] message, boolean allowStrike) {
continue find;
}
}
var codeHighlightingSpans = spannable.getSpans(start, end, CodeHighlighting.Span.class);
for (var codeHighlightingSpan : codeHighlightingSpans) {
int spanStart = spannable.getSpanStart(codeHighlightingSpan);
int spanEnd = spannable.getSpanEnd(codeHighlightingSpan);
if (spanStart < start + length || spanEnd > end - length) {
continue find;
}
}

var destination = new SpannableStringBuilder(spannable.subSequence(m.start(i == 0 ? 2 : 1), m.end(i == 0 ? 2 : 1)));
if (destination.length() > 0) {
if (i == 0) {
if (destination.charAt(destination.length() - 1) == '\n') {
destination = (SpannableStringBuilder) destination.subSequence(0, destination.length() - 1);
}
destination.setSpan(new CodeHighlighting.Span(true, 0, null, m.group(1), destination.toString()), 0, destination.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (i < 8) {
var run = new TextStyleSpan.TextStyleRun();
switch (i) {
case 1:
case 2:
case 3:
run.flags |= TextStyleSpan.FLAG_STYLE_MONO;
break;
case 4:
run.flags |= TextStyleSpan.FLAG_STYLE_BOLD;
break;
case 5:
run.flags |= TextStyleSpan.FLAG_STYLE_ITALIC;
break;
case 6:
run.flags |= TextStyleSpan.FLAG_STYLE_STRIKE;
break;
case 7:
run.flags |= TextStyleSpan.FLAG_STYLE_SPOILER;
break;
}
MediaDataController.addStyleToText(new TextStyleSpan(run), 0, destination.length(), destination, true);
} else {
destination.setSpan(new URLSpanReplacement(m.group(2)), 0, destination.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (i < 8) {
var run = new TextStyleSpan.TextStyleRun();
switch (i) {
case 0:
case 1:
case 2:
case 3:
run.flags |= TextStyleSpan.FLAG_STYLE_MONO;
if (i != 3) {
run.start = start;
run.end = end;
run.urlEntity = new TLRPC.TL_messageEntityPre();
run.urlEntity.language = i == 0 ? m.group(1) : "";
}
break;
case 4:
run.flags |= TextStyleSpan.FLAG_STYLE_BOLD;
break;
case 5:
run.flags |= TextStyleSpan.FLAG_STYLE_ITALIC;
break;
case 6:
run.flags |= TextStyleSpan.FLAG_STYLE_STRIKE;
break;
case 7:
run.flags |= TextStyleSpan.FLAG_STYLE_SPOILER;
break;
}
MediaDataController.addStyleToText(new TextStyleSpan(run), 0, destination.length(), destination, true);
} else {
destination.setSpan(new URLSpanReplacement(m.group(2)), 0, destination.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
sources.add(m.group(0));
destinations.add(destination);
Expand Down

0 comments on commit c5ced60

Please sign in to comment.