Emoji sequences: EnableLigatures writes liga into every serialized TMP text and TMP Settings; EmojiCheck renders every fully-qualified sequence as one visible character

Adds tools/emoji/emoji-sequences.txt (2760 fully-qualified multi-code-point sequences from the pinned emoji-test.txt), written by fetch_emoji_font.py and recorded in the lock. Not compiled yet (first Unity pass follows); the asset changes land in a later commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-12 04:22:40 +02:00
commit bd78401967
5 changed files with 2928 additions and 3 deletions

View file

@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.TextCore.LowLevel;
@ -10,6 +13,9 @@ using UnityEngine.TextCore.LowLevel;
// route, TMP Settings' global fallback list; the emoji fallback list stays empty, and no component names
// an emoji font. (Measured: the global list alone resolves every single code point; the emoji list alone
// leaves 200 text-presentation emoji unresolved. A second route could only disagree with the first.)
// Sequences (ZWJ, skin tone, flag, keycap) resolve only through the emoji font's GSUB ligatures, and TMP applies
// them per component, gated by that component's font features: EnableLigatures writes `liga` into every
// serialized TMP text and into TMP Settings (so a text created later inherits it); there is no global switch.
public static class EmojiFont
{
public const string FontPath = "Assets/Fonts/Emoji/Twemoji.ttf";
@ -74,4 +80,85 @@ public static class EmojiFont
message = "EmojiFont: built " + AssetPath + " and made it TMP Settings' only emoji route (the global fallback).";
return true;
}
// Menu entry for interactive use; -executeMethod EmojiFont.EnableLigatures for batchmode, exit 1 on failure.
// Idempotent: a second run changes nothing and saves nothing.
[MenuItem("Aetheria/Text/Enable Ligatures")]
public static void EnableLigatures()
{
try
{
var settings = TMP_Settings.fontFeatures;
var settingsChanged = Enable(settings);
if (settingsChanged) EditorUtility.SetDirty(TMP_Settings.instance);
var (files, components) = VisitSerializedTexts(text =>
{
var features = text.fontFeatures.ToList();
if (!Enable(features, text.enableKerning)) return false;
text.fontFeatures = features;
EditorUtility.SetDirty(text);
return true;
});
AssetDatabase.SaveAssets();
Debug.Log($"EmojiFont: liga added to {components} TMP component(s) in {files} file(s)" +
(settingsChanged ? " and to TMP Settings." : "; TMP Settings already had it."));
if (Application.isBatchMode) EditorApplication.Exit(0);
}
catch (Exception e)
{
Debug.LogError("EmojiFont.EnableLigatures failed: " + e);
if (Application.isBatchMode) EditorApplication.Exit(1);
}
}
// A legacy list ([0], no feature serialized) is first converted to [kern] when kerning is on, as TMP does on load.
static bool Enable(List<OTL_FeatureTag> features, bool kerning = true)
{
if (features.Contains(OTL_FeatureTag.liga)) return false;
features.RemoveAll(tag => tag == 0);
if (kerning && !features.Contains(OTL_FeatureTag.kern)) features.Add(OTL_FeatureTag.kern);
features.Add(OTL_FeatureTag.liga);
return true;
}
// The one walk over every TMP text a serialized asset owns: each prefab and each scene under Assets/, skipping a
// component that belongs to a prefab instance unless that instance overrides its font features (otherwise it
// inherits from its source prefab, which is walked itself). `visit` returns whether it changed the component;
// an asset that changed is saved. Returns the files and components changed.
public static (int files, int components) VisitSerializedTexts(Func<TMP_Text, bool> visit)
{
int files = 0, components = 0;
bool Owns(TMP_Text text) =>
!PrefabUtility.IsPartOfPrefabInstance(text) ||
new SerializedObject(text).FindProperty("m_ActiveFontFeatures")?.prefabOverride == true;
int Visit(IEnumerable<TMP_Text> texts) => texts.Where(Owns).Count(visit);
foreach (var path in AssetDatabase.FindAssets("t:Prefab", new[] { "Assets" }).Select(AssetDatabase.GUIDToAssetPath))
{
var root = PrefabUtility.LoadPrefabContents(path);
try
{
var changed = Visit(root.GetComponentsInChildren<TMP_Text>(true));
if (changed == 0) continue;
PrefabUtility.SaveAsPrefabAsset(root, path);
files++;
components += changed;
}
finally { PrefabUtility.UnloadPrefabContents(root); }
}
foreach (var path in AssetDatabase.FindAssets("t:Scene", new[] { "Assets" }).Select(AssetDatabase.GUIDToAssetPath))
{
var scene = EditorSceneManager.OpenScene(path, OpenSceneMode.Additive);
try
{
var changed = Visit(scene.GetRootGameObjects().SelectMany(root => root.GetComponentsInChildren<TMP_Text>(true)));
if (changed == 0) continue;
EditorSceneManager.SaveScene(scene);
files++;
components += changed;
}
finally { EditorSceneManager.CloseScene(scene, true); }
}
return (files, components);
}
}