ststeiger / PdfSharpCore

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

`Console.Error` gets clogged up when accessing Fonts #374

Closed RenderMichael closed 12 months ago

RenderMichael commented 1 year ago

Whenever I try to use a font, Console.Error gets filled up with irrelevant error messages about fonts failing to load. This is because whenever the font resolver is accessed, all supported fonts are loaded and, when some of them fail, they log to error

https://github.com/ststeiger/PdfSharpCore/blob/2d31ef51b9e931dee9a4d293c59f20399f8c7705/PdfSharpCore/Utils/FontResolver.cs#L115-L130

This is unfortunate, as it clogs up Console.Error for me. I know PdfSharpCore isn't necessarily one-for-one, but PdfSharp for framework doesn't do this and this is the only thing hampering our migration. Is there anything that can be done?

Note this only happens on windows for me, not Ubuntu The following is a test to check for this behavior (note fonts should not be accessed before this test otherwise it doesn't work)

[Fact]
public void LoadingFontsDoesNotLogToError()
{
    var errorStream = new MemoryStream();
    var errorWriter = new StreamWriter(errorStream);
    Console.SetError(errorWriter);
    var font = new XFont("Arial", 20, XFontStyle.BoldItalic);
    errorWriter.Flush();
    Assert.Empty(errorStream.ToArray());
}
RenderMichael commented 1 year ago

For reference, this is what error looks like for me on Windows

System.IO.EndOfStreamException: End of stream reached with 27424 byte left to read.
   at SixLabors.Fonts.BigEndianBinaryReader.ReadInternal(Byte[] data, Int32 size)
   at SixLabors.Fonts.BigEndianBinaryReader.ReadString(Int32 bytesToRead, Encoding encoding)
   at SixLabors.Fonts.Utilities.StringLoader.LoadValue(BigEndianBinaryReader reader)
   at SixLabors.Fonts.Tables.General.Name.NameTable.Load(BigEndianBinaryReader reader)
   at SixLabors.Fonts.Tables.General.Name.NameTable.Load(FontReader fontReader)
   at SixLabors.Fonts.Tables.TableLoader.Load[TTable](FontReader reader)
   at SixLabors.Fonts.FontReader.TryGetTable[TTableType]()
   at SixLabors.Fonts.FontReader.GetTable[TTableType]()
   at SixLabors.Fonts.FontDescription.LoadDescription(FontReader reader)
   at SixLabors.Fonts.FontDescription.LoadDescription(String path)
   at PdfSharpCore.Utils.FontResolver.FontFileInfo.Load(String path)
   at PdfSharpCore.Utils.FontResolver.SetupFontsFiles(String[] sSupportedFonts)
System.IO.EndOfStreamException: End of stream reached with 27424 byte left to read.
   at SixLabors.Fonts.BigEndianBinaryReader.ReadInternal(Byte[] data, Int32 size)
   at SixLabors.Fonts.BigEndianBinaryReader.ReadString(Int32 bytesToRead, Encoding encoding)
   at SixLabors.Fonts.Utilities.StringLoader.LoadValue(BigEndianBinaryReader reader)
   at SixLabors.Fonts.Tables.General.Name.NameTable.Load(BigEndianBinaryReader reader)
   at SixLabors.Fonts.Tables.General.Name.NameTable.Load(FontReader fontReader)
   at SixLabors.Fonts.Tables.TableLoader.Load[TTable](FontReader reader)
   at SixLabors.Fonts.FontReader.TryGetTable[TTableType]()
   at SixLabors.Fonts.FontReader.GetTable[TTableType]()
   at SixLabors.Fonts.FontDescription.LoadDescription(FontReader reader)
   at SixLabors.Fonts.FontDescription.LoadDescription(String path)
   at PdfSharpCore.Utils.FontResolver.FontFileInfo.Load(String path)
   at PdfSharpCore.Utils.FontResolver.SetupFontsFiles(String[] sSupportedFonts)
System.IO.EndOfStreamException: End of stream reached with 27424 byte left to read.
   at SixLabors.Fonts.BigEndianBinaryReader.ReadInternal(Byte[] data, Int32 size)
   at SixLabors.Fonts.BigEndianBinaryReader.ReadString(Int32 bytesToRead, Encoding encoding)
   at SixLabors.Fonts.Utilities.StringLoader.LoadValue(BigEndianBinaryReader reader)
   at SixLabors.Fonts.Tables.General.Name.NameTable.Load(BigEndianBinaryReader reader)
   at SixLabors.Fonts.Tables.General.Name.NameTable.Load(FontReader fontReader)
   at SixLabors.Fonts.Tables.TableLoader.Load[TTable](FontReader reader)
   at SixLabors.Fonts.FontReader.TryGetTable[TTableType]()
   at SixLabors.Fonts.FontReader.GetTable[TTableType]()
   at SixLabors.Fonts.FontDescription.LoadDescription(FontReader reader)
   at SixLabors.Fonts.FontDescription.LoadDescription(String path)
   at PdfSharpCore.Utils.FontResolver.FontFileInfo.Load(String path)
   at PdfSharpCore.Utils.FontResolver.SetupFontsFiles(String[] sSupportedFonts)
System.IO.EndOfStreamException: End of stream reached with 27424 byte left to read.
   at SixLabors.Fonts.BigEndianBinaryReader.ReadInternal(Byte[] data, Int32 size)
   at SixLabors.Fonts.BigEndianBinaryReader.ReadString(Int32 bytesToRead, Encoding encoding)
   at SixLabors.Fonts.Utilities.StringLoader.LoadValue(BigEndianBinaryReader reader)
   at SixLabors.Fonts.Tables.General.Name.NameTable.Load(BigEndianBinaryReader reader)
   at SixLabors.Fonts.Tables.General.Name.NameTable.Load(FontReader fontReader)
   at SixLabors.Fonts.Tables.TableLoader.Load[TTable](FontReader reader)
   at SixLabors.Fonts.FontReader.TryGetTable[TTableType]()
   at SixLabors.Fonts.FontReader.GetTable[TTableType]()
   at SixLabors.Fonts.FontDescription.LoadDescription(FontReader reader)
   at SixLabors.Fonts.FontDescription.LoadDescription(String path)
   at PdfSharpCore.Utils.FontResolver.FontFileInfo.Load(String path)
   at PdfSharpCore.Utils.FontResolver.SetupFontsFiles(String[] sSupportedFonts)
RenderMichael commented 1 year ago

I can submit a PR with a fix if we come to a conclusion about one.