diff --git a/Source/QuestPDF.UnitTests/FontManagerTests.cs b/Source/QuestPDF.UnitTests/FontManagerTests.cs new file mode 100644 index 0000000..37c4068 --- /dev/null +++ b/Source/QuestPDF.UnitTests/FontManagerTests.cs @@ -0,0 +1,32 @@ +using System; +using System.IO; +using NUnit.Framework; +using QuestPDF.Drawing; + +namespace QuestPDF.UnitTests +{ + public class FontManagerTests + { + [Test] + public void LoadFontFromFile() + { + using var stream = File.OpenRead("Resources/FontContent.ttf"); + FontManager.RegisterFont(stream); + } + + [Test] + public void LoadFontFromEmbeddedResource() + { + FontManager.RegisterFontFromEmbeddedResource("QuestPDF.UnitTests.Resources.FontEmbeddedResource.ttf"); + } + + [Test] + public void LoadFontFromEmbeddedResource_ShouldThrowException_WhenResourceIsNotAvailable() + { + Assert.Throws(() => + { + FontManager.RegisterFontFromEmbeddedResource("QuestPDF.UnitTests.WrongPath.ttf"); + }); + } + } +} \ No newline at end of file diff --git a/Source/QuestPDF.UnitTests/QuestPDF.UnitTests.csproj b/Source/QuestPDF.UnitTests/QuestPDF.UnitTests.csproj index 12bad7e..e062de1 100644 --- a/Source/QuestPDF.UnitTests/QuestPDF.UnitTests.csproj +++ b/Source/QuestPDF.UnitTests/QuestPDF.UnitTests.csproj @@ -17,4 +17,13 @@ + + + PreserveNewest + + + PreserveNewest + + + diff --git a/Source/QuestPDF.UnitTests/Resources/FontContent.ttf b/Source/QuestPDF.UnitTests/Resources/FontContent.ttf new file mode 100644 index 0000000..5fcfc55 Binary files /dev/null and b/Source/QuestPDF.UnitTests/Resources/FontContent.ttf differ diff --git a/Source/QuestPDF.UnitTests/Resources/FontEmbeddedResource.ttf b/Source/QuestPDF.UnitTests/Resources/FontEmbeddedResource.ttf new file mode 100644 index 0000000..5fcfc55 Binary files /dev/null and b/Source/QuestPDF.UnitTests/Resources/FontEmbeddedResource.ttf differ diff --git a/Source/QuestPDF/Drawing/FontManager.cs b/Source/QuestPDF/Drawing/FontManager.cs index 0f90b7f..4717379 100644 --- a/Source/QuestPDF/Drawing/FontManager.cs +++ b/Source/QuestPDF/Drawing/FontManager.cs @@ -64,7 +64,11 @@ namespace QuestPDF.Drawing public static void RegisterFontFromEmbeddedResource(string pathName) { - using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(pathName); + using var stream = Assembly.GetCallingAssembly().GetManifestResourceStream(pathName); + + if (stream == null) + throw new ArgumentException($"Cannot load font file from an embedded resource. Please make sure that the resource is available or the path is correct: {pathName}"); + RegisterFont(stream); } @@ -88,10 +92,13 @@ namespace QuestPDF.Drawing "Lato-ThinItalic.ttf" }; - fontFileNames - .Select(x => $"QuestPDF.Resources.DefaultFont.{x}") - .ToList() - .ForEach(RegisterFontFromEmbeddedResource); + foreach (var fileName in fontFileNames) + { + var filePath = $"QuestPDF.Resources.DefaultFont.{fileName}"; + + using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filePath); + RegisterFont(stream); + } } internal static SKPaint ColorToPaint(this string color) diff --git a/Source/QuestPDF/Resources/ReleaseNotes.txt b/Source/QuestPDF/Resources/ReleaseNotes.txt index f71102a..5559c4c 100644 --- a/Source/QuestPDF/Resources/ReleaseNotes.txt +++ b/Source/QuestPDF/Resources/ReleaseNotes.txt @@ -2,3 +2,5 @@ Feature: implemented LetterSpacing property for the Text element Improvement: the Text element API accepts now only string values, objects are not automatically converted anymore Fix: the Alignment element incorrectly limits size of its child when only one axis is set (horizontal or vertical) Maintenance: Updated SkiaSharp dependency to 2.88.3 + +Fixed in version 2022.12.1: loading fonts from embedded resource via the FontManager.RegisterFontFromEmbeddedResource method \ No newline at end of file