30 lines
1.2 KiB
C#
30 lines
1.2 KiB
C#
using System.Security.Cryptography;
|
|
using QuestPDF.Drawing;
|
|
|
|
namespace LehrerApp.Templating;
|
|
|
|
/// <summary>
|
|
/// Registers application-supplied TTF/OTF font bytes for TEXT and drawing commands without
|
|
/// exposing QuestPDF types to the calling application.
|
|
/// </summary>
|
|
public static class DrawingFontRegistry
|
|
{
|
|
private static readonly object Gate = new();
|
|
private static readonly HashSet<string> 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);
|
|
}
|
|
}
|
|
}
|