Is this how font sources are supposed to work? FindFont doesn't find the fonts

I need to get Windows fonts to work in a docker Linux container but first, before I try that, I’m seeing if I can test with embedded fonts on Windows when the font is not installed on windows. I’m running this on Windows 10, .Net Core 3.1, in Visual Studio 2019, and Aspose.PDF for .Net 22.6.0.

So, I downloaded a font which is not installed on windows and embedded it in a resource on my unit test just to try it. This didn’t work with any font I tried it with. I tried upper case, lower case, etc. for the font name. I opened the font and looked at the font name stored just to double check.

I tried the “Short Baby” font from this page since it’s unlikely to be installed already :slight_smile: This site doesn’t flood you with adverts. https://www.fontspace.com/category/ttf

    [TestMethod]
    public void LoadFontResources()
    {
        // Retrieve a list of embedded fonts
        var fontNames = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames().Where( name => name.ToLower().EndsWith( "ttf" ) );
        Stream fontStream;
        foreach ( var fontName in fontNames )
        {
            Console.WriteLine( $"Loading font {fontName}");
            // Test to see the name and ensure it is valid
            fontStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( fontName );
            Aspose.Pdf.Text.Font fontA = FontRepository.OpenFont( fontStream, FontTypes.TTF );

            fontStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( fontName );
            using ( var ms = new MemoryStream() )
            {
                fontStream.CopyTo( ms );
                var fontSource = new MemoryFontSource( ms.ToArray() );
                // Verified that the fontSource does have the array of bytes for the array loaded.
                FontRepository.Sources.Add( fontSource );

                // Didn't work here either
                //var font1 = FontRepository.FindFont( "Short Baby" );
            }
        }
        
        //FontRepository.ReloadFonts(); // Not sure if this is needed, tried with and without

        // Test to see if it worked. It does not.
        // Tried "Short Baby", "ShortBaby","shortbaby","short baby","ShortBaby-Mg2w"
        var font2 = FontRepository.FindFont( "Short Baby" );
    }

I added an OpenFont statement just to verify the font loads and the name used. I attached a debug screenshot for this example.
FontNameDebug.png (45.0 KB)

Hi @williamfa, if you set a breakpoint in the following line
Aspose.Pdf.Text.Font fontA =
Can you check the value of fontStream. Also can you verify, via breakpoint, that the value of fontA and post the output?

Thank you :vulcan_salute::nerd_face:

Hi Bob, I attached it to the first post at FontNameDebug.png. Thanks!

@williamfa

You are using the font sources correctly. Could you please share exception detail that you are facing? We will then log this issue in our issue tracking system.