TryAddCharacters() stuck in infinite loop with tiny multi-atlas textures

Hi Stephan,

I noticed that you can get an infinite loop when calling TMP_FontAsset.TryAddCharacters() if you try to add a character that doesn’t fit in the atlas size.

This is kind of a “bad path” state for sure (what I’m asking TMP to do is impossible), but this should ideally cause an error/failure and not hang the editor.

Specifically, if multi-atlas textures are on, and you have a sufficiently small atlas and sufficiently large character, with some sampling point size / atlasPadding values, then the character does not fit by itself in the atlas and infinitely loops, hanging the editor.

It gets stuck in this loop in TMP_FontAsset.TryAddCharacters()

while (allGlyphsAddedToTexture == false)
    allGlyphsAddedToTexture = TryAddGlyphsToNewAtlasTexture();

Every iteration, it tries to add the character to a new texture, it doesn’t fit by itself, it detects that not all characters are added, and then it tries again. I think probably there should be some logic that detects that no progress has been made (no new characters were added to the new texture), and then gives up at that point and fails to add the new characters.

In my testing I was passing more characters, some that fit and some that didn’t, but I think this is reproducible with this small example:

var tmpFont = TMP_FontAsset.CreateFontAsset(font, 50, 5, GlyphRenderMode.SDFAA, 64, 64, AtlasPopulationMode.Dynamic, true);
tmpFont.TryAddCharacters(﷼, out var missingCharacters);

The character I’m trying here is the Rial sign ﷼ - Rial Sign, Unicode Number: U+FDFC 📖 Symbol Meaning ✂ Copy & 📋 Paste (◕‿◕) SYMBL and the font I’m using is Noto Sans Arabic Regular Noto Sans Arabic - Google Fonts but I think this should be reproducible with any character in any font so long as a sufficiently restrictively small texture size is used.

From a user perspective obviously the solution is to use larger textures and/or different sampling sizes, but it would be nice to avoid some editor restarts while tweaking those values.

Thanks for reporting this behavior.

I’ll take a closer look as soon as I have a chance and make the necessary changes.

I will try to get this (fix) into preview 2.