Build 0.20.7.0
This commit is contained in:
@@ -8,6 +8,7 @@ using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
using Barotrauma.Extensions;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
@@ -45,16 +46,17 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
private ScalableFont cjkFont;
|
||||
private ImmutableDictionary<TextManager.SpeciallyHandledCharCategory, ScalableFont> specialHandlingFonts;
|
||||
|
||||
public ScalableFont CjkFont
|
||||
public ScalableFont GetFontForCategory(TextManager.SpeciallyHandledCharCategory category)
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Language != GameSettings.CurrentConfig.Language) { LoadFont(); }
|
||||
if (font.IsCJK) { return font; }
|
||||
return cjkFont;
|
||||
}
|
||||
if (Language != GameSettings.CurrentConfig.Language) { LoadFont(); }
|
||||
if (font.SpeciallyHandledCharCategory.HasFlag(category)) { return font; }
|
||||
return specialHandlingFonts.TryGetValue(category, out var resultFont)
|
||||
? resultFont
|
||||
: specialHandlingFonts.TryGetValue(TextManager.SpeciallyHandledCharCategory.CJK, out resultFont)
|
||||
? resultFont
|
||||
: font;
|
||||
}
|
||||
|
||||
public LanguageIdentifier Language { get; private set; }
|
||||
@@ -70,40 +72,68 @@ namespace Barotrauma
|
||||
string fontPath = GetFontFilePath(element);
|
||||
uint size = GetFontSize(element);
|
||||
bool dynamicLoading = GetFontDynamicLoading(element);
|
||||
bool isCJK = GetIsCJK(element);
|
||||
var shcc = GetShcc(element);
|
||||
font?.Dispose();
|
||||
cjkFont?.Dispose();
|
||||
font = new ScalableFont(fontPath, size, GameMain.Instance.GraphicsDevice, dynamicLoading, isCJK)
|
||||
specialHandlingFonts?.Values.ForEach(f => f.Dispose());
|
||||
font = new ScalableFont(
|
||||
fontPath,
|
||||
size,
|
||||
GameMain.Instance.GraphicsDevice,
|
||||
dynamicLoading,
|
||||
shcc)
|
||||
{
|
||||
ForceUpperCase = element.GetAttributeBool("forceuppercase", false)
|
||||
};
|
||||
if (!isCJK)
|
||||
|
||||
var fallbackFonts = new Dictionary<TextManager.SpeciallyHandledCharCategory, ScalableFont>();
|
||||
foreach (var flag in TextManager.SpeciallyHandledCharCategories)
|
||||
{
|
||||
cjkFont = ExtractCjkFont(element)
|
||||
?? new ScalableFont("Content/Fonts/NotoSans/NotoSansCJKsc-Bold.otf",
|
||||
font.Size, GameMain.Instance.GraphicsDevice, dynamicLoading: true, isCJK: true);
|
||||
cjkFont.ForceUpperCase = font.ForceUpperCase;
|
||||
if (shcc.HasFlag(flag)) { continue; }
|
||||
|
||||
var extractedFont = ExtractFont(flag, element);
|
||||
if (extractedFont is null) { continue; }
|
||||
fallbackFonts.Add(flag, extractedFont);
|
||||
}
|
||||
fallbackFonts.Values.ForEach(ff => ff.ForceUpperCase = font.ForceUpperCase);
|
||||
specialHandlingFonts = fallbackFonts.ToImmutableDictionary();
|
||||
Language = GameSettings.CurrentConfig.Language;
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
font?.Dispose(); font = null;
|
||||
cjkFont?.Dispose(); cjkFont = null;
|
||||
font?.Dispose();
|
||||
font = null;
|
||||
specialHandlingFonts?.Values.ForEach(f => f.Dispose());
|
||||
specialHandlingFonts = null;
|
||||
}
|
||||
|
||||
private ScalableFont ExtractCjkFont(ContentXElement element)
|
||||
private ScalableFont ExtractFont(TextManager.SpeciallyHandledCharCategory flag, ContentXElement element)
|
||||
{
|
||||
foreach (var subElement in element.Elements().Reverse())
|
||||
{
|
||||
if (subElement.NameAsIdentifier() != "override") { continue; }
|
||||
if (subElement.GetAttributeBool("iscjk", false))
|
||||
if (ScalableFont.ExtractShccFromXElement(subElement).HasFlag(flag))
|
||||
{
|
||||
return new ScalableFont(subElement, GameMain.Instance.GraphicsDevice);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
ScalableFont hardcodedFallback(string path)
|
||||
=> new ScalableFont(
|
||||
path,
|
||||
font.Size,
|
||||
GameMain.Instance.GraphicsDevice,
|
||||
dynamicLoading: true,
|
||||
speciallyHandledCharCategory: flag);
|
||||
|
||||
return flag switch
|
||||
{
|
||||
TextManager.SpeciallyHandledCharCategory.CJK
|
||||
=> hardcodedFallback("Content/Fonts/NotoSans/NotoSansCJKsc-Bold.otf"),
|
||||
TextManager.SpeciallyHandledCharCategory.Cyrillic
|
||||
=> hardcodedFallback("Content/Fonts/Oswald-Bold.ttf"),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
private string GetFontFilePath(ContentXElement element)
|
||||
@@ -154,21 +184,21 @@ namespace Barotrauma
|
||||
return element.GetAttributeBool("dynamicloading", false);
|
||||
}
|
||||
|
||||
private bool GetIsCJK(XElement element)
|
||||
private TextManager.SpeciallyHandledCharCategory GetShcc(XElement element)
|
||||
{
|
||||
foreach (var subElement in element.Elements())
|
||||
{
|
||||
if (IsValidOverride(subElement))
|
||||
{
|
||||
return subElement.GetAttributeBool("iscjk", false);
|
||||
return ScalableFont.ExtractShccFromXElement(subElement);
|
||||
}
|
||||
}
|
||||
return element.GetAttributeBool("iscjk", false);
|
||||
return ScalableFont.ExtractShccFromXElement(element);
|
||||
}
|
||||
|
||||
private bool IsValidOverride(XElement element)
|
||||
{
|
||||
if (!element.Name.ToString().Equals("override", StringComparison.OrdinalIgnoreCase)) { return false; }
|
||||
if (!element.IsOverride()) { return false; }
|
||||
var languages = element.GetAttributeIdentifierArray("language", Array.Empty<Identifier>());
|
||||
return languages.Any(l => l.ToLanguageIdentifier() == GameSettings.CurrentConfig.Language);
|
||||
}
|
||||
@@ -191,7 +221,7 @@ namespace Barotrauma
|
||||
private ScalableFont GetFontForStr(LocalizedString str) => GetFontForStr(str.Value);
|
||||
|
||||
public ScalableFont GetFontForStr(string str) =>
|
||||
TextManager.IsCJK(str) ? Prefabs.ActivePrefab.CjkFont : Prefabs.ActivePrefab.Font;
|
||||
Prefabs.ActivePrefab.GetFontForCategory(TextManager.GetSpeciallyHandledCategories(str));
|
||||
|
||||
public void DrawString(SpriteBatch sb, LocalizedString text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects se, float layerDepth)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user