ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.04k stars 231 forks source link

System.ArgumentException is thrown when creating XFont object with a custom font resolver #417

Open Kizuto3 opened 6 months ago

Kizuto3 commented 6 months ago

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);
        }
    }
}

Archive with the fonts I am using: Fonts.zip