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

This commit is contained in:
MarcinZiabek
2023-01-09 11:59:06 +01:00
parent 9dbed14559
commit 641cc6dee2
6 changed files with 55 additions and 5 deletions
@@ -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");
});
}
}
}
@@ -17,4 +17,13 @@
<ProjectReference Include="..\QuestPDF\QuestPDF.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources\FontContent.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<EmbeddedResource Include="Resources\FontEmbeddedResource.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
</Project>
Binary file not shown.
+12 -5
View File
@@ -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)
@@ -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