Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Kizuto3 opened this issue Dec 26, 2023 · 0 comments

Comments

@Kizuto3
Copy link

Kizuto3 commented Dec 26, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant