Explorar o código

Fixed: loading fonts from embedded resource via the FontManager.RegisterFontFromEmbeddedResource method

MarcinZiabek %!s(int64=2) %!d(string=hai) anos
pai
achega
641cc6dee2

+ 32 - 0
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<ArgumentException>(() =>
+            {
+                FontManager.RegisterFontFromEmbeddedResource("QuestPDF.UnitTests.WrongPath.ttf");
+            });
+        }
+    }
+}

+ 9 - 0
Source/QuestPDF.UnitTests/QuestPDF.UnitTests.csproj

@@ -17,4 +17,13 @@
       <ProjectReference Include="..\QuestPDF\QuestPDF.csproj" />
       <ProjectReference Include="..\QuestPDF\QuestPDF.csproj" />
     </ItemGroup>
     </ItemGroup>
 
 
+    <ItemGroup>
+      <Content Include="Resources\FontContent.ttf">
+        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+      </Content>
+      <EmbeddedResource Include="Resources\FontEmbeddedResource.ttf">
+        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+      </EmbeddedResource>
+    </ItemGroup>
+
 </Project>
 </Project>

BIN=BIN
Source/QuestPDF.UnitTests/Resources/FontContent.ttf


BIN=BIN
Source/QuestPDF.UnitTests/Resources/FontEmbeddedResource.ttf


+ 12 - 5
Source/QuestPDF/Drawing/FontManager.cs

@@ -64,7 +64,11 @@ namespace QuestPDF.Drawing
         
         
         public static void RegisterFontFromEmbeddedResource(string pathName)
         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);
             RegisterFont(stream);
         }
         }
         
         
@@ -88,10 +92,13 @@ namespace QuestPDF.Drawing
                 "Lato-ThinItalic.ttf"
                 "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)
         internal static SKPaint ColorToPaint(this string color)

+ 2 - 0
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
 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)
 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
 Maintenance: Updated SkiaSharp dependency to 2.88.3
+
+Fixed in version 2022.12.1: loading fonts from embedded resource via the FontManager.RegisterFontFromEmbeddedResource method