[chore:] logging for StableFontResolver.cs
This commit is contained in:
+85
-58
@@ -62,75 +62,102 @@ public class StableFontResolver : IFontResolver
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
catch (Exception ex)
|
||||
{
|
||||
// ignore resolver init errors
|
||||
Logger.Log($"Error while initializing FontResolver: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private static string NormalizeStyle(string s)
|
||||
{
|
||||
if (string.IsNullOrEmpty(s)) return "Regular";
|
||||
s = s.ToLowerInvariant();
|
||||
if (s.Contains("bold") && s.Contains("italic")) return "BoldItalic";
|
||||
if (s.Contains("bold")) return "Bold";
|
||||
if (s.Contains("italic") || s.Contains("oblique")) return "Italic";
|
||||
if (s.Contains("regular") || s == "r") return "Regular";
|
||||
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s);
|
||||
}
|
||||
|
||||
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
|
||||
{
|
||||
// If requested family exists, pick corresponding style if available
|
||||
string familyToUse = null;
|
||||
if (!string.IsNullOrEmpty(familyName) && _families.ContainsKey(familyName))
|
||||
familyToUse = familyName;
|
||||
|
||||
if (familyToUse == null && _orderedFamilies.Count > 0)
|
||||
familyToUse = _orderedFamilies[0];
|
||||
|
||||
if (familyToUse == null)
|
||||
return new FontResolverInfo("Arial");
|
||||
|
||||
var style = "Regular";
|
||||
if (isBold && isItalic) style = "BoldItalic";
|
||||
else if (isBold) style = "Bold";
|
||||
else if (isItalic) style = "Italic";
|
||||
|
||||
// Face name format: Family#Style
|
||||
return new FontResolverInfo($"{familyToUse}#{style}");
|
||||
}
|
||||
|
||||
public byte[] GetFont(string faceName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(faceName)) return null;
|
||||
|
||||
// faceName expected as "Family#Style" or just "Family"
|
||||
var family = faceName;
|
||||
var style = "Regular";
|
||||
var idx = faceName.IndexOf('#');
|
||||
if (idx >= 0)
|
||||
try
|
||||
{
|
||||
family = faceName.Substring(0, idx);
|
||||
style = faceName.Substring(idx + 1);
|
||||
if (string.IsNullOrEmpty(s)) return "Regular";
|
||||
s = s.ToLowerInvariant();
|
||||
if (s.Contains("bold") && s.Contains("italic")) return "BoldItalic";
|
||||
if (s.Contains("bold")) return "Bold";
|
||||
if (s.Contains("italic") || s.Contains("oblique")) return "Italic";
|
||||
if (s.Contains("regular") || s == "r") return "Regular";
|
||||
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s);
|
||||
}
|
||||
|
||||
style = NormalizeStyle(style);
|
||||
|
||||
if (_families.TryGetValue(family, out var dict))
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Try exact style
|
||||
if (dict.TryGetValue(style, out var data)) return data;
|
||||
|
||||
// Fallback order
|
||||
if (style != "Regular" && dict.TryGetValue("Regular", out data)) return data;
|
||||
if (dict.TryGetValue("Bold", out data)) return data;
|
||||
if (dict.TryGetValue("Italic", out data)) return data;
|
||||
|
||||
// Any available
|
||||
foreach (var kv in dict) return kv.Value;
|
||||
Logger.Log($"Error while normalizing style: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
|
||||
{
|
||||
try
|
||||
{
|
||||
// If requested family exists, pick corresponding style if available
|
||||
string familyToUse = null;
|
||||
if (!string.IsNullOrEmpty(familyName) && _families.ContainsKey(familyName))
|
||||
familyToUse = familyName;
|
||||
|
||||
if (familyToUse == null && _orderedFamilies.Count > 0)
|
||||
familyToUse = _orderedFamilies[0];
|
||||
|
||||
if (familyToUse == null)
|
||||
return new FontResolverInfo("Arial");
|
||||
|
||||
var style = "Regular";
|
||||
if (isBold && isItalic) style = "BoldItalic";
|
||||
else if (isBold) style = "Bold";
|
||||
else if (isItalic) style = "Italic";
|
||||
|
||||
// Face name format: Family#Style
|
||||
return new FontResolverInfo($"{familyToUse}#{style}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while resolving typeface: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public byte[] GetFont(string faceName)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(faceName)) return null;
|
||||
|
||||
// faceName expected as "Family#Style" or just "Family"
|
||||
var family = faceName;
|
||||
var style = "Regular";
|
||||
var idx = faceName.IndexOf('#');
|
||||
if (idx >= 0)
|
||||
{
|
||||
family = faceName.Substring(0, idx);
|
||||
style = faceName.Substring(idx + 1);
|
||||
}
|
||||
|
||||
style = NormalizeStyle(style);
|
||||
|
||||
if (_families.TryGetValue(family, out var dict))
|
||||
{
|
||||
// Try exact style
|
||||
if (dict.TryGetValue(style, out var data)) return data;
|
||||
|
||||
// Fallback order
|
||||
if (style != "Regular" && dict.TryGetValue("Regular", out data)) return data;
|
||||
if (dict.TryGetValue("Bold", out data)) return data;
|
||||
if (dict.TryGetValue("Italic", out data)) return data;
|
||||
|
||||
// Any available
|
||||
foreach (var kv in dict) return kv.Value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while getting font: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user