Skip to content

Commit

Permalink
Basics for loading TMP font replacement from a .ttf in StreamingAsset…
Browse files Browse the repository at this point in the history
…s/Fonts at runtime

First part of simplification pass to localization.
Replacement font must be named after font being replaced - e.g. "FONT0003-SDF.ttf".
This needs to be built out further to support user-specified character file input to build atlas - e.g. Cyrillic or Kanji unicodes.
No longer required to use Editor, write code, or build a .dfmod to distribute a custom font. Just drop it into the correct path with appropriate name.
  • Loading branch information
Interkarma committed Nov 8, 2022
1 parent 4642378 commit 4d5b9b6
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Assets/Scripts/Game/UserInterface/DaggerfallFont.cs
Expand Up @@ -484,6 +484,14 @@ public bool LoadSDFFontAsset(string path)
if (!tmpFont)
return false;

// Attempt to ingest a StreamingAssets/Fonts .ttf override file
TMP_FontAsset replacement;
if (ReplaceTMPFontFromTTF(Path.GetFileNameWithoutExtension(path) + ".ttf", tmpFont, out replacement))
{
tmpFont = replacement;
// TODO: Output debug text that font was replaced
}

UseSDFFontAsset(tmpFont);

return true;
Expand Down Expand Up @@ -622,6 +630,48 @@ float GetGlyphSpacing()
return (!IsSDFCapable) ? classicGlyphSpacing : sdfGlyphSpacing;
}

/// <summary>
/// Replace TMP font asset using a .ttf font in StreamingAssets/Fonts.
/// </summary>
/// <param name="filename">Filename of replacement font including .ttf extension. Font file must be present in StreamingAssets/Fonts to load.</param>
/// <param name="source">Source TMP font for initial character table population.</param>
/// <param name="replacement">Replacement TMP font output.</param>
/// <returns>True is successfully created replacement TMP font.</returns>
bool ReplaceTMPFontFromTTF(string filename, TMP_FontAsset source, out TMP_FontAsset replacement)
{
// Compose path to font file
string path = Path.Combine(Application.streamingAssetsPath, "Fonts", filename);

// Check file exists
replacement = null;
if (!File.Exists(path))
return false;

// Get characters of source font
List<uint> sourceUnicodes = new List<uint>();
foreach(TMP_Character c in source.characterTable)
{
sourceUnicodes.Add(c.unicode);
}

// Create replacement TMP font asset from path
Font font = new Font(path);
replacement = TMP_FontAsset.CreateFontAsset(font);
if (replacement == null)
return false;

// Attempt to add unicode characters from source
uint[] missingUnicodesSource = null;
if (!replacement.TryAddCharacters(sourceUnicodes.ToArray(), out missingUnicodesSource))
{
// TODO: Output list of missing characters from source unicodes
}

// TODO: Attempt to add user-specified unicode characters from a source file

return true;
}

#endregion
}
}

0 comments on commit 4d5b9b6

Please sign in to comment.