using System.Security.Cryptography; using QuestPDF.Drawing; namespace LehrerApp.Templating; /// /// Registers application-supplied TTF/OTF font bytes for TEXT and drawing commands without /// exposing QuestPDF types to the calling application. /// public static class DrawingFontRegistry { private static readonly object Gate = new(); private static readonly HashSet Registered = new(StringComparer.Ordinal); public static void RegisterFont(string familyName, byte[] fontData) { if (string.IsNullOrWhiteSpace(familyName) || familyName.Length > 80) throw new ArgumentException("Der Schriftname fehlt oder ist zu lang.", nameof(familyName)); if (fontData.Length is 0 or > 20 * 1024 * 1024) throw new ArgumentException("Eine Schriftdatei muss zwischen 1 Byte und 20 MB groß sein.", nameof(fontData)); var key = familyName + ":" + Convert.ToHexString(SHA256.HashData(fontData)); lock (Gate) { if (!Registered.Add(key)) return; using var stream = new MemoryStream(fontData, writable: false); FontManager.RegisterFontWithCustomName(familyName, stream); } } }