Use the new Rune API for handling emoji code points

pull/278/head
Alexey Golub 5 years ago
parent aa50dd6dc7
commit 1475ffaf0e

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using Tyrrrz.Extensions; using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Models namespace DiscordChatExporter.Core.Models
@ -28,14 +29,21 @@ namespace DiscordChatExporter.Core.Models
public partial class Emoji public partial class Emoji
{ {
private static IEnumerable<int> GetCodePoints(string emoji) private static IEnumerable<Rune> GetRunes(string emoji)
{ {
for (var i = 0; i < emoji.Length; i += char.IsHighSurrogate(emoji[i]) ? 2 : 1) var lastIndex = 0;
yield return char.ConvertToUtf32(emoji, i); while (lastIndex < emoji.Length && Rune.TryGetRuneAt(emoji, lastIndex, out var rune))
{
// Skip variant selector rune
if (rune.Value != 0xfe0f)
yield return rune;
lastIndex += rune.Utf16SequenceLength;
}
} }
private static string GetTwemojiName(string emoji) => private static string GetTwemojiName(IEnumerable<Rune> runes) =>
GetCodePoints(emoji).Select(i => i.ToString("x")).JoinToString("-"); runes.Select(r => r.Value.ToString("x")).JoinToString("-");
public static string GetImageUrl(string? id, string name, bool isAnimated) public static string GetImageUrl(string? id, string name, bool isAnimated)
{ {
@ -50,9 +58,17 @@ namespace DiscordChatExporter.Core.Models
return $"https://cdn.discordapp.com/emojis/{id}.png"; return $"https://cdn.discordapp.com/emojis/{id}.png";
} }
// Standard unicode emoji (via twemoji) // Standard unicode emoji
var twemojiName = GetTwemojiName(name); var emojiRunes = GetRunes(name).ToArray();
return $"https://twemoji.maxcdn.com/2/72x72/{twemojiName}.png"; if (emojiRunes.Any())
{
// Get corresponding Twemoji image
var twemojiName = GetTwemojiName(emojiRunes);
return $"https://twemoji.maxcdn.com/2/72x72/{twemojiName}.png";
}
// Fallback in case of failure
return name;
} }
} }
} }
Loading…
Cancel
Save