private static string FormatEmojiEntity(string targetText, TwitterEntityEmoji entity) { if (!SettingManager.Local.UseTwemoji) { return(t(e(targetText))); } return("<img class=\"emoji\" src=\"" + e(entity.Url) + "\" alt=\"" + e(entity.Text) + "\" />"); }
/// <summary> /// テキストから絵文字を抽出して <see cref="TwitterEntityEmoji"/> として返します /// </summary> public static IEnumerable <TwitterEntityEmoji> ExtractEmojiEntities(string text) { var matches = TweetExtractor.EmojiPattern.Matches(text); foreach (var match in matches.Cast <Match>()) { var input = match.Value; // 異字体セレクタ U+FE0F (emoji style) は除去する (ZWJ を含まない場合のみ) if (!input.Contains('\u200D')) { input = input.Replace("\uFE0F", ""); } var codepointHex = new List <string>(); for (var i = 0; i < input.Length; i += char.IsSurrogatePair(input, i) ? 2 : 1) { var codepoint = char.ConvertToUtf32(input, i); codepointHex.Add($"{codepoint:x}"); } var startPos = text.GetCodepointCount(0, match.Index); var endPos = startPos + text.GetCodepointCount(match.Index, match.Index + match.Length); TwitterEntityEmoji entity; if (codepointHex.Count >= 1) { entity = new TwitterEntityEmoji { Indices = new[] { startPos, endPos }, Text = input, Url = "https://twemoji.maxcdn.com/2/72x72/" + string.Join("-", codepointHex) + ".png", }; } else { entity = new TwitterEntityEmoji { Indices = new[] { startPos, endPos }, Text = input, Url = "", }; } yield return(entity); } }