You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is an issue when creating XFont object with a custom font resolver. I've attached an archive with the fonts that I'm trying to use. The Arial fonts in folders A and B are not the same, however, internally (when creating a XFontSource object) they are both called 'Arial' (XFontSource.FontName property). So, when the second XFont object is created it throws System.ArgumentException when adding XFontSource object to FontSourcesByName dictionary. Code below shows an example of this issue when used with the fonts from the attached archive.
using PdfSharpCore.Drawing;
using PdfSharpCore.Fonts;
namespace FontTest
{
public class Program
{
public static void Main(string[] args)
{
GlobalFontSettings.FontResolver = new FontResolver();
var printerFontsFolders = "<path_to_dir>\\Fonts";
while (true)
{
foreach (var family in Directory.GetDirectories(printerFontsFolders))
{
foreach (var file in Directory.GetFiles(family))
{
var fontName = Path.GetFileNameWithoutExtension(file);
var familyName = Path.GetFileName(family);
var fullName = $"{familyName}_{fontName}";
var font = new XFont(fullName, 16, XFontStyle.Regular, XPdfFontOptions.UnicodeDefault);
Console.WriteLine(font.Name);
}
}
}
}
}
public class FontResolver : IFontResolver
{
private Dictionary<string, byte[]> _fontCache;
public FontResolver()
{
_fontCache = new Dictionary<string, byte[]>();
var printerFontsFolders = "<path_to_dir>\\Fonts";
foreach (var family in Directory.GetDirectories(printerFontsFolders))
{
foreach (var file in Directory.GetFiles(family))
{
var data = File.ReadAllBytes(file);
var fontName = Path.GetFileNameWithoutExtension(file);
var familyName = Path.GetFileName(family);
var fullName = $"{familyName}_{fontName}";
_fontCache[fullName] = data;
}
}
}
public string DefaultFontName => "Roboto";
public byte[] GetFont(string faceName)
{
_fontCache.TryGetValue(faceName, out var data)
return data;
}
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
if (isBold && isItalic)
return new($"{familyName}-BoldItalic");
if (isItalic)
return new($"{familyName}-Italic");
if (isBold)
return new($"{familyName}-Bold");
return new(familyName);
}
}
}
There is an issue when creating XFont object with a custom font resolver. I've attached an archive with the fonts that I'm trying to use. The Arial fonts in folders A and B are not the same, however, internally (when creating a XFontSource object) they are both called 'Arial' (XFontSource.FontName property). So, when the second XFont object is created it throws System.ArgumentException when adding XFontSource object to FontSourcesByName dictionary. Code below shows an example of this issue when used with the fonts from the attached archive.
Archive with the fonts I am using:
Fonts.zip
The text was updated successfully, but these errors were encountered: