empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
398 stars 91 forks source link

Invalid Surrogate Pair Check #87

Closed leesatoh closed 3 months ago

leesatoh commented 4 months ago

Hi,

I attempted to write Japanese Kanji into a PDF using PDFsharp. When I tried to encode the character with the code 0x29C9C, PDFsharp threw an ArgumentOutOfRangeException. This seems incorrect because 0x29C9C is not within the surrogate pair range. The exception message was as follows:

System.ArgumentOutOfRangeException
  HResult=0x80131502
  Message=有効な UTF32 値には包括的に 0x000000 から 0x10ffff の間で指定し、サロゲート コードポイント値 (0x00d800 ~ 0x00dfff) を含んではいけません。
パラメーター名:utf32
  Source=mscorlib
  スタック トレース:
   場所 System.Char.ConvertFromUtf32(Int32 utf32)
   場所 PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.DrawString(String s, XFont font, XBrush brush, XRect rect, XStringFormat format)
   場所 PdfSharp.Drawing.XGraphics.DrawString(String text, XFont font, XBrush brush, XRect layoutRectangle, XStringFormat format)
   場所 PdfSharp.Drawing.Layout.XTextFormatter.DrawString(String text, XFont font, XBrush brush, XRect layoutRectangle, XStringFormat format)
   場所 TestForPDFfonts.PdfWriter.DictionaryToPdf(Dictionary`2 data, String filename, String fontname) (C:\Users\satom\source\repos\TestForPDFfonts\TestForPDFfonts\PdfWriter.cs):行 87
   場所 TestForPDFfonts.Program.Main(String[] args) (C:\Users\satom\source\repos\TestForPDFfonts\TestForPDFfonts\Program.cs):行 52449

Thanks.

TH-Soft commented 4 months ago

Any code that does not fit into 16 bits (i.e. anything beyond 0xffff) falls in the surrogate pair range. I do not see any code. Difficult for us to reproduce.

leesatoh commented 4 months ago

Hi @TH-Soft , Thanks for your reply. I provied my code. It simply outputs a word to the PDF file. This program need font file ipamjm.ttf in fonts directory. SamplePDFsharp.zip

            var data = new Dictionary<string, string>
            {

                {"MJ054956", char.ConvertFromUtf32(0x29C9C)},
            };

Thanks. lee

TH-Soft commented 4 months ago

I'll check it on Monday. The code in question has meanwhile been reworked, but was not yet published (work in progress).

ThomasHoevel commented 4 months ago

Thanks for your feedback. PDFsharp did not handle the glyph ID returned for 0x29C9C correctly, leading to that exception. Previously, we only tested surrogate pairs with some Emojis, but character 0x29C9C for that font behaves differently.

Will be fixed with 6.1.0-preview-2.

StLange commented 3 months ago

Now works with 6.1.0 preview 2.