Skip to content

Commit 88260b8

Browse files
committed
Add support for colored emojis
1 parent 952a23c commit 88260b8

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

BeatSaberMarkupLanguage/Config.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public class Config
1313
[NonNullable]
1414
[UseConverter(typeof(ListConverter<string>))]
1515
public virtual List<string> HiddenTabs { get; set; } = new List<string>();
16+
17+
public virtual bool UseColoredEmoji { get; set; } = true;
1618
}
1719
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using System.Reflection.Emit;
3+
using HarmonyLib;
4+
using TMPro;
5+
using UnityEngine;
6+
7+
namespace BeatSaberMarkupLanguage.Harmony_Patches
8+
{
9+
[HarmonyPatch]
10+
internal class EmojiSupport
11+
{
12+
[HarmonyPatch(typeof(TMP_FontAsset), nameof(TMP_FontAsset.CreateFontAssetInstance))]
13+
[HarmonyTranspiler]
14+
public static IEnumerable<CodeInstruction> TMP_FontAsset_CreateFontAssetInstance_Transpiler(IEnumerable<CodeInstruction> instructions)
15+
{
16+
return new CodeMatcher(instructions)
17+
.MatchForward(
18+
false,
19+
new CodeMatch(OpCodes.Ldstr, "TextMeshPro/Sprite"), // This shader is not included in the game build.
20+
new CodeMatch(i => i.Calls(AccessTools.DeclaredMethod(typeof(Shader), nameof(Shader.Find)))),
21+
new CodeMatch(OpCodes.Newobj))
22+
.SetAndAdvance(OpCodes.Nop, null)
23+
.SetAndAdvance(OpCodes.Nop, null)
24+
.SetAndAdvance(OpCodes.Call, AccessTools.DeclaredMethod(typeof(EmojiSupport), nameof(GetSpriteMaterial)))
25+
.InstructionEnumeration();
26+
}
27+
28+
private static Material GetSpriteMaterial()
29+
{
30+
// This is the material used for ImageViews. It won't support all of TMP's features but curves correctly.
31+
return new Material(Utilities.ImageResources.NoGlowMat)
32+
{
33+
name = "TextMeshPro Sprite No Glow",
34+
mainTexture = null,
35+
};
36+
}
37+
}
38+
}

BeatSaberMarkupLanguage/Plugin.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
using System.Reflection;
44
using System.Runtime.CompilerServices;
55
using System.Threading.Tasks;
6-
using BeatSaberMarkupLanguage.Harmony_Patches;
76
using BeatSaberMarkupLanguage.Util;
87
using HarmonyLib;
98
using IPA;
109
using IPA.Config.Stores;
1110
using TMPro;
11+
using UnityEngine;
12+
using UnityEngine.TextCore;
13+
using UnityEngine.TextCore.LowLevel;
1214
using Conf = IPA.Config.Config;
1315
using IPALogger = IPA.Logging.Logger;
1416

@@ -58,18 +60,6 @@ public void OnExit()
5860
private async Task LoadAndSetUpFontFallbacksAsync()
5961
{
6062
await FontManager.AsyncLoadSystemFonts();
61-
await MainSystemInitAwaiter.WaitForMainSystemInitAsync();
62-
63-
TMP_FontAsset[] fontAssets = FontManager.CreateFallbackFonts(FontNamesToLoad);
64-
65-
if (fontAssets.Length == 0)
66-
{
67-
Logger.Log.Error("Failed to find any fallback fonts");
68-
return;
69-
}
70-
71-
Logger.Log.Debug("Waiting for default font presence");
72-
7363
await MainMenuAwaiter.WaitForMainMenuAsync();
7464

7565
Logger.Log.Debug("Setting up default font fallbacks");
@@ -78,8 +68,20 @@ private async Task LoadAndSetUpFontFallbacksAsync()
7868
TMP_FontAsset mainTextFont = BeatSaberUI.MainTextFont;
7969
mainTextFont.fallbackFontAssets.RemoveAll((asset) => FontNamesToRemove.Contains(asset.name));
8070
mainTextFont.fallbackFontAssetTable.RemoveAll((asset) => FontNamesToRemove.Contains(asset.name));
81-
mainTextFont.fallbackFontAssetTable.AddRange(fontAssets);
8271
mainTextFont.boldSpacing = 2.2f; // default bold spacing is rather w i d e
72+
73+
if (Config.UseColoredEmoji && FontManager.TryGetFontByFullName("Segoe UI Emoji", out Font font))
74+
{
75+
TMP_FontAsset emoji = TMP_FontAsset.CreateFontAsset(font, 90, 9, GlyphRenderMode.COLOR, 4096, 4096);
76+
emoji.name = "Segoe UI Emoji (Color)";
77+
FaceInfo faceInfo = emoji.faceInfo;
78+
faceInfo.scale = 0.85f;
79+
emoji.faceInfo = faceInfo;
80+
81+
mainTextFont.fallbackFontAssetTable.Add(emoji);
82+
}
83+
84+
mainTextFont.fallbackFontAssetTable.AddRange(FontManager.CreateFallbackFonts(FontNamesToLoad));
8385
}
8486
}
8587
}

0 commit comments

Comments
 (0)