From 490a7defb1414a741a3a3648291739026e7fcc5e Mon Sep 17 00:00:00 2001 From: Elias Fierke Date: Wed, 25 Dec 2024 21:08:09 +0100 Subject: [PATCH] =?UTF-8?q?[feat:]=20FontResolver=20for=20Custom=20Fonts?= =?UTF-8?q?=20with=20=C3=84=C3=96=C3=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CustomFontResolver.cs | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 CustomFontResolver.cs diff --git a/CustomFontResolver.cs b/CustomFontResolver.cs new file mode 100644 index 0000000..cb62106 --- /dev/null +++ b/CustomFontResolver.cs @@ -0,0 +1,49 @@ +using PdfSharp.Pdf; +using PdfSharp.Drawing; +using PdfSharp.Fonts; +using System.IO; +using System; + +namespace PLG_Exam; + +public class CustomFontResolver : IFontResolver +{ + private static readonly byte[] Cantarell_RegularFont; + private static readonly byte[] Cantarell_BoldFont; + + static CustomFontResolver() + { + // Schriftarten aus Dateien laden + var basePath = Path.Combine(AppContext.BaseDirectory, "resources"); + var regularPath = Path.Combine(basePath, "Cantarell-Regular.ttf"); + var boldPath = Path.Combine(basePath, "Cantarell-Bold.ttf"); + + Cantarell_RegularFont = File.ReadAllBytes(regularPath); + Cantarell_BoldFont = File.ReadAllBytes(boldPath); + } + + public string DefaultFontName => "Cantarell"; + + public byte[] GetFont(string faceName) + { + return faceName switch + { + "Cantarell-Regular" => Cantarell_RegularFont, + "Cantarell-Bold" => Cantarell_BoldFont, + _ => throw new FileNotFoundException($"Schriftart nicht gefunden: {faceName}") + }; + } + + public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic) + { + if (familyName == "Cantarell") + { + // Bold oder Regular zurückgeben + return isBold + ? new FontResolverInfo("Cantarell-Bold") + : new FontResolverInfo("Cantarell-Regular"); + } + + throw new FileNotFoundException($"Schriftart nicht gefunden: {familyName}"); + } +}