From bf28ba19146d16ba5e65b835ef2175c137c8ff51 Mon Sep 17 00:00:00 2001 From: Elias Fierke Date: Sat, 16 May 2026 18:19:50 +0200 Subject: [PATCH] [chore:] logging for StableFontResolver.cs --- Tasks/StableFontResolver.cs | 143 +++++++++++++++++++++--------------- 1 file changed, 85 insertions(+), 58 deletions(-) diff --git a/Tasks/StableFontResolver.cs b/Tasks/StableFontResolver.cs index b0fa783..7883880 100644 --- a/Tasks/StableFontResolver.cs +++ b/Tasks/StableFontResolver.cs @@ -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 []; + } }