1
0
mirror of https://github.com/MGislv/NekoX.git synced 2024-07-02 10:33:36 +00:00

Fix: display most of emoji correctly in Avatar

This commit is contained in:
luvletter2333 2021-04-19 15:37:14 +08:00
parent 64b9d411cb
commit e2a5097979
No known key found for this signature in database
GPG Key ID: BFD68B892BECC1D8
2 changed files with 44 additions and 10 deletions

View File

@ -727,4 +727,43 @@ public class Emoji {
}
preferences.edit().putString("color", stringBuilder.toString()).commit();
}
/**
* NekoX: This function tries to fix incomplete emoji display shown in AvatarDrawable
* In AvatarDrawable, only the first char is used to draw "avatar".
* @return The first char or the first emoji
*/
public static String getFirstCharSafely(String source) {
source = source.trim();
if (source.length() <= 1)
return source;
StringBuilder sb = new StringBuilder();
boolean nextEmoji = false;
int code = source.codePointAt(0);
sb.appendCodePoint(code); // append the first "char"
for (int offset = code > 0xFFFF ? 2 : 1; offset < source.length(); offset++) {
code = source.codePointAt(offset);
if (code > 0xFFFF) offset++;
if (nextEmoji || code == 0xFE0F || code == 0x20E3 || (code >= 0x1F3FB && code <= 0x1F3FF)) {
// 0xFE0F: VARIATION SELECTOR-16, 20E3: Keycap, 0x1F3FB ~ 0x1F3FF: skin tone
sb.appendCodePoint(code);
nextEmoji = false;
continue;
} else if ((code >= 0x1F1E6 && code <= 0x1F1FF)) {
sb.appendCodePoint(code);
break;
// 0x1F1E6 ~ 0x1F1FF: regional indicator symbol letter a to z
// These unicode are also used in the first "char" of country flag emoji, so break immediately to prevent appending two emoji
} else if (code == 0x200D) {
// 0x200D: ZWJ
sb.appendCodePoint(code);
nextEmoji = true;
continue;
}
if (!nextEmoji)
break;
}
return sb.toString();
}
}

View File

@ -22,6 +22,7 @@ import android.text.TextPaint;
import androidx.core.graphics.ColorUtils;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.Emoji;
import org.telegram.messenger.FileLog;
import org.telegram.messenger.UserObject;
import org.telegram.tgnet.TLObject;
@ -239,17 +240,10 @@ public class AvatarDrawable extends Drawable {
stringBuilder.appendCodePoint(firstName.codePointAt(0));
}
if (lastName != null && lastName.length() > 0) {
Integer lastch = null;
for (int a = lastName.length() - 1; a >= 0; a--) {
if (lastch != null && lastName.charAt(a) == ' ') {
break;
}
lastch = lastName.codePointAt(a);
}
if (Build.VERSION.SDK_INT > 17) {
stringBuilder.append("\u200C");
}
stringBuilder.appendCodePoint(lastch);
stringBuilder.append(Emoji.getFirstCharSafely(lastName));
} else if (firstName != null && firstName.length() > 0) {
for (int a = firstName.length() - 1; a >= 0; a--) {
if (firstName.charAt(a) == ' ') {
@ -257,7 +251,7 @@ public class AvatarDrawable extends Drawable {
if (Build.VERSION.SDK_INT > 17) {
stringBuilder.append("\u200C");
}
stringBuilder.appendCodePoint(firstName.codePointAt(a + 1));
stringBuilder.append(Emoji.getFirstCharSafely(firstName.substring(a+1)));
break;
}
}
@ -266,7 +260,8 @@ public class AvatarDrawable extends Drawable {
}
if (stringBuilder.length() > 0) {
String text = stringBuilder.toString().toUpperCase();
CharSequence text = Emoji.replaceEmoji(stringBuilder.toString().toUpperCase(), namePaint.getFontMetricsInt(), (int) namePaint.getTextSize(), false);
try {
textLayout = new StaticLayout(text, namePaint, AndroidUtilities.dp(100), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
if (textLayout.getLineCount() > 0) {