Add the emoji font pipeline: pinned Twemoji fetch, font builder, rendering check
tools/emoji/fetch_emoji_font.py builds Assets/Fonts/Emoji/Twemoji.ttf from the pinned jdecked/twemoji release with nanoemoji (glyf_colr_0); EmojiFont.Build is the one writer of Emoji.asset and TMP Settings' emoji routing; EmojiCheck.Run renders every single-code-point emoji and the Pirates names through TMP. The generated font, lock and asset land in the next commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
59bc0e556c
commit
4e1b4882d4
6 changed files with 341 additions and 0 deletions
118
Assets/Scripts/Editor/EmojiCheck.cs
Normal file
118
Assets/Scripts/Editor/EmojiCheck.cs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using TMPro;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
// Every single-code-point emoji the pinned font set draws, and the Pirates product names, render through an
|
||||
// ordinary TMP text on the game's body font with no missing code point and with ink in the emoji atlas.
|
||||
// Run as -executeMethod EmojiCheck.Run (exit 1 on failure in batchmode, as EngineAssetCheck). Corpus:
|
||||
// tools/emoji/emoji-singles.txt; -emojiLimit N spreads N entries evenly across it for the spike.
|
||||
public static class EmojiCheck
|
||||
{
|
||||
const string BodyFontPath = "Assets/Fonts/Ubuntu/Ubuntu-L Small SDF.asset";
|
||||
const int Chunk = 64;
|
||||
|
||||
// The eight Pirates products (addenda "The Pirates record r2"), written as code points.
|
||||
static readonly string[] PiratesNames =
|
||||
{
|
||||
"\U0001FAF3\U0001F52B", "\U0001FAA6\U0001F920", "\U0001F9FE\U0001F6AB", "\U0001F3A8\U0001F648",
|
||||
"\U0001F525", "\U0001F440\U0001F3AF", "\U0001F511\U0001F3C3\U0001F4A8", "\U0001F4E6\U0001F92B",
|
||||
};
|
||||
|
||||
[MenuItem("Aetheria/Text/Check Emoji Rendering")]
|
||||
public static void Run()
|
||||
{
|
||||
var failures = new List<string>();
|
||||
var summary = RunCheck(failures);
|
||||
foreach (var failure in failures.Take(20)) Debug.LogError(failure);
|
||||
Debug.Log($"EmojiCheck: {summary}; {failures.Count} failure(s).");
|
||||
if (Application.isBatchMode) EditorApplication.Exit(failures.Count == 0 ? 0 : 1);
|
||||
}
|
||||
|
||||
static string RunCheck(List<string> failures)
|
||||
{
|
||||
var emoji = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(EmojiFont.AssetPath);
|
||||
var body = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(BodyFontPath);
|
||||
if (emoji == null || body == null)
|
||||
{
|
||||
failures.Add("EmojiCheck: Emoji.asset or the body font is missing; run EmojiFont.Build first.");
|
||||
return "nothing checked";
|
||||
}
|
||||
|
||||
var corpus = LoadSingles();
|
||||
var missing = new HashSet<int>();
|
||||
TMP_Text.MissingCharacterEventCallback onMissing = (unicode, index, source, asset, component) => missing.Add(unicode);
|
||||
TMP_Text.OnMissingCharacter += onMissing;
|
||||
|
||||
var canvas = new GameObject("EmojiCheck", typeof(Canvas));
|
||||
var text = new GameObject("Text", typeof(RectTransform)).AddComponent<TextMeshProUGUI>();
|
||||
text.transform.SetParent(canvas.transform, false);
|
||||
text.font = body;
|
||||
text.textWrappingMode = TextWrappingModes.NoWrap;
|
||||
text.rectTransform.sizeDelta = new Vector2(100000, 100000);
|
||||
|
||||
int viaEmoji = 0, viaOther = 0, blank = 0;
|
||||
try
|
||||
{
|
||||
void Render(string content, string label)
|
||||
{
|
||||
text.text = content;
|
||||
text.ForceMeshUpdate();
|
||||
for (var i = 0; i < text.textInfo.characterCount; i++)
|
||||
{
|
||||
var info = text.textInfo.characterInfo[i];
|
||||
if (info.character == ' ' || info.elementType != TMP_TextElementType.Character) continue;
|
||||
if (info.fontAsset == emoji) viaEmoji++; else viaOther++;
|
||||
if (info.fontAsset == emoji && info.textElement.glyph.glyphRect.width <= 0)
|
||||
{
|
||||
blank++;
|
||||
failures.Add($"blank glyph U+{(int)info.character:X4} in {label}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < corpus.Count; i += Chunk)
|
||||
{
|
||||
var slice = corpus.Skip(i).Take(Chunk).ToList();
|
||||
Render(string.Join(" ", slice.Select(char.ConvertFromUtf32)), $"chunk {i}");
|
||||
}
|
||||
foreach (var name in PiratesNames) Render(name, "Pirates name");
|
||||
}
|
||||
finally
|
||||
{
|
||||
TMP_Text.OnMissingCharacter -= onMissing;
|
||||
UnityEngine.Object.DestroyImmediate(canvas);
|
||||
}
|
||||
|
||||
foreach (var cp in missing.OrderBy(c => c)) failures.Add($"missing code point U+{cp:X4}");
|
||||
|
||||
long ink = 0, colour = 0;
|
||||
foreach (var page in emoji.atlasTextures)
|
||||
{
|
||||
var pixels = page.GetPixels32();
|
||||
ink += pixels.LongCount(p => p.a != 0);
|
||||
colour += pixels.LongCount(p => p.a != 0 && (p.r != p.g || p.g != p.b));
|
||||
}
|
||||
if (ink == 0) failures.Add("the emoji atlas holds no ink after rendering");
|
||||
else if (colour == 0) failures.Add("the emoji atlas holds no coloured pixel: the glyphs rendered monochrome");
|
||||
|
||||
// Dynamic atlas content is cache: drop it so this check leaves the asset as Build wrote it.
|
||||
emoji.ClearFontAssetData(true);
|
||||
return $"checked {corpus.Count} single code points + {PiratesNames.Length} Pirates names, {missing.Count} missing, " +
|
||||
$"{viaEmoji} glyphs via the emoji asset, {viaOther} via other fonts, {blank} blank, {ink} atlas ink pixels of which {colour} coloured";
|
||||
}
|
||||
|
||||
static List<int> LoadSingles()
|
||||
{
|
||||
var path = Path.Combine(Directory.GetParent(Application.dataPath).FullName, "tools", "emoji", "emoji-singles.txt");
|
||||
var all = File.ReadAllLines(path).Where(l => l.Length > 0).Select(l => Convert.ToInt32(l, 16)).ToList();
|
||||
var args = Environment.GetCommandLineArgs();
|
||||
var at = Array.IndexOf(args, "-emojiLimit");
|
||||
if (at < 0 || at + 1 >= args.Length) return all;
|
||||
var limit = int.Parse(args[at + 1]);
|
||||
return Enumerable.Range(0, limit).Select(i => all[i * all.Count / limit]).ToList();
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Editor/EmojiCheck.cs.meta
Normal file
2
Assets/Scripts/Editor/EmojiCheck.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c6f940add256a754daac8460fc79480d
|
||||
74
Assets/Scripts/Editor/EmojiFont.cs
Normal file
74
Assets/Scripts/Editor/EmojiFont.cs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TextCore.LowLevel;
|
||||
|
||||
// The one writer of the emoji font asset and of TMP Settings' emoji routing. The font file is the
|
||||
// output of tools/emoji/fetch_emoji_font.py; the asset is a dynamic COLOR font asset whose atlas pages
|
||||
// are cache, filled on first use at runtime and cleared here. Every TMP text reaches emoji through
|
||||
// TMP Settings' emoji fallback and global fallback list; no component names an emoji font.
|
||||
public static class EmojiFont
|
||||
{
|
||||
public const string FontPath = "Assets/Fonts/Emoji/Twemoji.ttf";
|
||||
public const string AssetPath = "Assets/Fonts/Emoji/Emoji.asset";
|
||||
|
||||
// Colour outlines (COLRv0) are rasterised at one size; 109 is the strike size a CBDT set needs.
|
||||
const int SamplingPointSize = 64;
|
||||
const int AtlasPadding = 4;
|
||||
const int AtlasSize = 2048;
|
||||
|
||||
// Menu entry for interactive use; -executeMethod EmojiFont.Build for batchmode, exit 1 on failure.
|
||||
[MenuItem("Aetheria/Text/Build Emoji Font")]
|
||||
public static void Build()
|
||||
{
|
||||
var ok = TryBuild(out var message);
|
||||
if (ok) Debug.Log(message); else Debug.LogError(message);
|
||||
if (Application.isBatchMode) EditorApplication.Exit(ok ? 0 : 1);
|
||||
}
|
||||
|
||||
static bool TryBuild(out string message)
|
||||
{
|
||||
var font = AssetDatabase.LoadAssetAtPath<Font>(FontPath);
|
||||
if (font == null)
|
||||
{
|
||||
message = $"EmojiFont: {FontPath} is missing; run tools/emoji/fetch_emoji_font.py.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// An existing asset keeps its GUID, so a second Build changes nothing the settings point at.
|
||||
var asset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(AssetPath);
|
||||
if (asset == null)
|
||||
{
|
||||
asset = TMP_FontAsset.CreateFontAsset(font, SamplingPointSize, AtlasPadding, GlyphRenderMode.COLOR,
|
||||
AtlasSize, AtlasSize, AtlasPopulationMode.Dynamic, enableMultiAtlasSupport: true);
|
||||
if (asset == null)
|
||||
{
|
||||
message = $"EmojiFont: TMP could not create a font asset from {FontPath}.";
|
||||
return false;
|
||||
}
|
||||
asset.name = "Emoji";
|
||||
AssetDatabase.CreateAsset(asset, AssetPath);
|
||||
foreach (var page in asset.atlasTextures)
|
||||
{
|
||||
page.name = "Emoji Atlas";
|
||||
AssetDatabase.AddObjectToAsset(page, asset);
|
||||
}
|
||||
asset.material.name = "Emoji Atlas Material";
|
||||
AssetDatabase.AddObjectToAsset(asset.material, asset);
|
||||
}
|
||||
else asset.ClearFontAssetData(true);
|
||||
|
||||
// Text-presentation emoji (U+263A without FE0F) are not routed through the emoji list, so the
|
||||
// asset is also the global fallback.
|
||||
TMP_Settings.emojiFallbackTextAssets = new List<TMP_Asset> { asset };
|
||||
TMP_Settings.fallbackFontAssets = new List<TMP_FontAsset> { asset };
|
||||
TMP_Settings.defaultSpriteAsset = null;
|
||||
|
||||
EditorUtility.SetDirty(asset);
|
||||
EditorUtility.SetDirty(TMP_Settings.instance);
|
||||
AssetDatabase.SaveAssets();
|
||||
message = "EmojiFont: built " + AssetPath + " and routed TMP Settings' emoji fallback to it.";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Editor/EmojiFont.cs.meta
Normal file
2
Assets/Scripts/Editor/EmojiFont.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bd3a902defe23f14fbe821ca9fb26a08
|
||||
Loading…
Reference in a new issue