From 45ec2dd96ea0050ff3ffc943a630dcd0dc2f1397 Mon Sep 17 00:00:00 2001 From: Casper Ehrenborg Date: Mon, 30 Nov 2020 12:31:29 +0100 Subject: [PATCH] Actually use custom fonts --- PdfSharpCore/Utils/FontResolver.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/PdfSharpCore/Utils/FontResolver.cs b/PdfSharpCore/Utils/FontResolver.cs index 4474b922..dd18b6bd 100644 --- a/PdfSharpCore/Utils/FontResolver.cs +++ b/PdfSharpCore/Utils/FontResolver.cs @@ -19,7 +19,7 @@ public class FontResolver : IFontResolver private static readonly Dictionary InstalledFonts = new Dictionary(); - private static readonly string[] SSupportedFonts; + private static readonly List SSupportedFonts; public FontResolver() { @@ -28,21 +28,19 @@ public FontResolver() static FontResolver() { string fontDir; - + SSupportedFonts = new List(); bool isOSX = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX); if (isOSX) { fontDir = "/Library/Fonts/"; - SSupportedFonts = Directory.GetFiles(fontDir, "*.ttf", SearchOption.AllDirectories); - SetupFontsFiles(SSupportedFonts); + SetupFontsFiles(Directory.GetFiles(fontDir, "*.ttf", SearchOption.AllDirectories)); return; } bool isLinux = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux); if (isLinux) { - SSupportedFonts = resolveLinuxFontFiles(); - SetupFontsFiles(SSupportedFonts); + SetupFontsFiles(resolveLinuxFontFiles()); return; } @@ -50,8 +48,7 @@ static FontResolver() if (isWindows) { fontDir = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\Fonts"); - SSupportedFonts = Directory.GetFiles(fontDir, "*.ttf", SearchOption.AllDirectories); - SetupFontsFiles(SSupportedFonts); + SetupFontsFiles(Directory.GetFiles(fontDir, "*.ttf", SearchOption.AllDirectories)); return; } @@ -89,6 +86,8 @@ public static void SetupFontsFiles(string[] sSupportedFonts) string fontFamilyName = fontDescription.FontFamily(CultureInfo.InvariantCulture); Debug.WriteLine(fontPathFile); + SSupportedFonts.Add(fontPathFile); + if (tmpFontFamiliesTtfFilesDict.TryGetValue(fontFamilyName, out List familyTtfFiles)) familyTtfFiles.Add(fontPathFile); else @@ -188,7 +187,7 @@ public byte[] GetFont(string faceFileName) string ttfPathFile = ""; try { - ttfPathFile = SSupportedFonts.ToList().First(x => x.ToLower().Contains(Path.GetFileName(faceFileName).ToLower())); + ttfPathFile = SSupportedFonts.First(x => x.ToLower().Contains(Path.GetFileName(faceFileName).ToLower())); using (var ttf = File.OpenRead(ttfPathFile)) { ttf.CopyTo(ms);