diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/AndroidUtilities.java b/TMessagesProj/src/main/java/org/telegram/messenger/AndroidUtilities.java index 0a5b82bf4d..f74527aa8c 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/AndroidUtilities.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/AndroidUtilities.java @@ -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; @@ -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) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/TextStyleSpan.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/TextStyleSpan.java index 670dc32601..dde720dfc5 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Components/TextStyleSpan.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/TextStyleSpan.java @@ -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; @@ -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; diff --git a/TMessagesProj/src/main/java/xyz/nextalone/nnngram/helpers/EntitiesHelper.java b/TMessagesProj/src/main/java/xyz/nextalone/nnngram/helpers/EntitiesHelper.java index 9b5de4589c..ced8bd7ea4 100644 --- a/TMessagesProj/src/main/java/xyz/nextalone/nnngram/helpers/EntitiesHelper.java +++ b/TMessagesProj/src/main/java/xyz/nextalone/nnngram/helpers/EntitiesHelper.java @@ -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; @@ -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);