Prevent NPE, load Korean font if it exists (#802)

Co-authored-by: goaaats <goatsdev@protonmail.com>
This commit is contained in:
kizer 2022-04-23 23:42:44 +09:00 committed by GitHub
parent 0b6417fd7c
commit 2a36122c2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 30 deletions

View file

@ -658,9 +658,19 @@ namespace Dalamud.Interface.Internal
fontConfig.OversampleH = 1;
fontConfig.OversampleV = 1;
var fontPathJp = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "NotoSansCJKjp-Medium.otf");
var fontPathJp = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "NotoSansCJKjp-Regular.otf");
if (!File.Exists(fontPathJp))
fontPathJp = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "NotoSansCJKjp-Medium.otf");
if (!File.Exists(fontPathJp))
ShowFontError(fontPathJp);
Log.Verbose("[FONT] fontPathJp = {0}", fontPathJp);
var fontPathKr = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "NotoSansCJKkr-Regular.otf");
if (!File.Exists(fontPathKr))
fontPathKr = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "NotoSansKR-Regular.otf");
if (!File.Exists(fontPathKr))
fontPathKr = null;
Log.Verbose("[FONT] fontPathKr = {0}", fontPathKr);
// Default font
Log.Verbose("[FONT] SetupFonts - Default font");
@ -671,10 +681,10 @@ namespace Dalamud.Interface.Internal
io.FontGlobalScale,
disableBigFonts);
Log.Verbose("[FONT] SetupFonts - Default corresponding AXIS size: {0}pt ({1}px)", fontInfo.SourceAxis.Style.BaseSizePt, fontInfo.SourceAxis.Style.BaseSizePx);
fontConfig.SizePixels = disableBigFonts ? Math.Min(MinimumFallbackFontSizePx, fontInfo.TargetSizePx) : fontInfo.TargetSizePx * io.FontGlobalScale;
if (this.UseAxis)
{
fontConfig.GlyphRanges = dummyRangeHandle.AddrOfPinnedObject();
fontConfig.SizePixels = fontInfo.TargetSizePx;
fontConfig.PixelSnapH = false;
DefaultFont = ioFonts.AddFontDefault(fontConfig);
this.loadedFontInfo[DefaultFont] = fontInfo;
@ -686,10 +696,19 @@ namespace Dalamud.Interface.Internal
fontConfig.GlyphRanges = japaneseRangeHandle.AddrOfPinnedObject();
fontConfig.PixelSnapH = true;
DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, disableBigFonts ? Math.Min(MinimumFallbackFontSizePx, fontInfo.TargetSizePx) : fontInfo.TargetSizePx * io.FontGlobalScale, fontConfig);
DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, fontConfig.SizePixels, fontConfig);
this.loadedFontInfo[DefaultFont] = fontInfo;
}
if (fontPathKr != null && Service<DalamudConfiguration>.Get().EffectiveLanguage == "ko")
{
fontConfig.MergeMode = true;
fontConfig.GlyphRanges = ioFonts.GetGlyphRangesKorean();
fontConfig.PixelSnapH = true;
ioFonts.AddFontFromFileTTF(fontPathKr, fontConfig.SizePixels, fontConfig);
fontConfig.MergeMode = false;
}
// FontAwesome icon font
Log.Verbose("[FONT] SetupFonts - FontAwesome icon font");
{
@ -887,8 +906,19 @@ namespace Dalamud.Interface.Internal
foreach (var (font, mod) in this.loadedFontInfo)
{
var nameBytes = Encoding.UTF8.GetBytes(mod.Name + "\0");
Marshal.Copy(nameBytes, 0, (IntPtr)font.ConfigData.Name.Data, Math.Min(nameBytes.Length, font.ConfigData.Name.Count));
// I have no idea what's causing NPE, so just to be safe
try
{
if (font.NativePtr != null && font.NativePtr->ConfigData != null)
{
var nameBytes = Encoding.UTF8.GetBytes($"{mod.Name}\0");
Marshal.Copy(nameBytes, 0, (IntPtr)font.ConfigData.Name.Data, Math.Min(nameBytes.Length, font.ConfigData.Name.Count));
}
}
catch (NullReferenceException)
{
// do nothing
}
Log.Verbose("[FONT] {0}: Unscale with scale value of {1}", mod.Name, mod.Scale);
GameFontManager.UnscaleFont(font, mod.Scale, false);

View file

@ -134,26 +134,9 @@ namespace Dalamud.Interface.Internal.Windows
};
this.languages = Localization.ApplicableLangCodes.Prepend("en").ToArray();
try
{
if (string.IsNullOrEmpty(configuration.LanguageOverride))
{
var currentUiLang = CultureInfo.CurrentUICulture;
if (Localization.ApplicableLangCodes.Any(x => currentUiLang.TwoLetterISOLanguageName == x))
this.langIndex = Array.IndexOf(this.languages, currentUiLang.TwoLetterISOLanguageName);
else
this.langIndex = 0;
}
else
{
this.langIndex = Array.IndexOf(this.languages, configuration.LanguageOverride);
}
}
catch (Exception)
{
this.langIndex = Array.IndexOf(this.languages, configuration.EffectiveLanguage);
if (this.langIndex == -1)
this.langIndex = 0;
}
try
{