Skip to content

Commit

Permalink
Merge pull request #2579 from Jagget/Support-loading-font-file-from-.…
Browse files Browse the repository at this point in the history
…dfmod

Support loading font file from .dfmod
  • Loading branch information
KABoissonneault authored May 10, 2024
2 parents 6d62f17 + 3060936 commit 0038a1e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
3 changes: 0 additions & 3 deletions Assets/Game/Addons/ModSupport/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ void Awake()
{
if (string.IsNullOrEmpty(ModDirectory))
ModDirectory = Path.Combine(Application.streamingAssetsPath, "Mods");
}

void Start()
{
SetupSingleton();

if (Instance == this)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Game/Addons/ModSupport/ModManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 42 additions & 15 deletions Assets/Scripts/Game/UserInterface/DaggerfallFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Text;
using DaggerfallConnect;
using DaggerfallConnect.Arena2;
using DaggerfallWorkshop.Game.Utility.ModSupport;
using DaggerfallWorkshop.Utility;
using TMPro;

Expand Down Expand Up @@ -265,6 +266,7 @@ public float DrawSDFGlyph(SDFGlyphInfo glyph, Vector2 position, Vector2 scale, C
{
Graphics.DrawTexture(targetRect, sdfFontInfo.Value.atlasTexture, glyph.rect, 0, 0, 0, 0, color, DaggerfallUI.Instance.SDFFontMaterial);
}

return GetGlyphWidth(glyph, scale, GlyphSpacing);
}

Expand Down Expand Up @@ -640,33 +642,58 @@ float GetGlyphSpacing()
}

/// <summary>
/// Replace TMP font asset using a .ttf or .otf font in StreamingAssets/Fonts.
/// TODO: Support loading font file from .dfmod.
/// Replace TMP font asset using a .ttf or .otf font in StreamingAssets/Fonts or in dfmod asset
/// </summary>
/// <param name="filename">Filename of replacement font including .ttf extension. Font file must be present in StreamingAssets/Fonts to load.</param>
/// <param name="filename">Filename of replacement font including .ttf extension. Font file must be present in StreamingAssets/Fonts or in dfmod asset 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 ReplaceTMPFontFromFile(string filename, TMP_FontAsset source, out TMP_FontAsset replacement)
{
const string ttfExt = ".ttf";
const string otfExt = ".otf";
replacement = null;

Font otfFontFromMods = null;
Font ttfFontFromMods = null;

// Seek font replacement file from mods
if (ModManager.Instance != null)
{
if (!ModManager.Instance.TryGetAsset(filename + otfExt, false, out otfFontFromMods))
{
ModManager.Instance.TryGetAsset(filename + ttfExt, false, out ttfFontFromMods);
}
}

if (otfFontFromMods != null)
{
replacement = TMP_FontAsset.CreateFontAsset(otfFontFromMods, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic);
}
else if (ttfFontFromMods != null)
{
replacement = TMP_FontAsset.CreateFontAsset(ttfFontFromMods, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic);
}

// Compose path to font file
string path = Path.Combine(Application.streamingAssetsPath, "Fonts", filename);
var path = Path.Combine(Application.streamingAssetsPath, "Fonts", filename);

// Check file exists
replacement = null;
if (File.Exists(path + ttfExt))
path += ttfExt;
else if (File.Exists(path + otfExt))
path += otfExt;
else
return false;
if (replacement == null)
{
// Check file exists
replacement = null;
if (File.Exists(path + ttfExt))
path += ttfExt;
else if (File.Exists(path + otfExt))
path += otfExt;
else
return false;

// Create replacement TMP font asset from path
Font replacementFont = new Font(path);
replacement = TMP_FontAsset.CreateFontAsset(replacementFont, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic);
}

// Create replacement TMP font asset from path
Font font = new Font(path);
replacement = TMP_FontAsset.CreateFontAsset(font, 45, 6, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 4096, 4096, AtlasPopulationMode.Dynamic);
if (replacement == null)
return false;

Expand Down

0 comments on commit 0038a1e

Please sign in to comment.