Skip to content

Chinese chars are not measured correctly #372

@AlleSchonWeg

Description

@AlleSchonWeg

Hi,
i tried to migrate from PdfSharp 1.3.0 to version 6.2.4 (PDFsharp-GDI / .Net Framework 4.8) and observed a string measurement issue.

I have this text: 一到十二月
and tried to measure it with font "Courier New" size 8.

Version 1.3.0 returns:
Size: 44,00390625;9,0625

Version 6.2.4 return:
Size: 24.00390625,9.0625

Image

--> blue rectangle is the measured size
Obviously my layout calculation isn't working anymore.
Here is my minimal repro code:

        private void button2_Click(object sender, EventArgs e)
        {
            var doc = new PdfDocument(@"C:\Daten\pdfsharp.pdf");
            var p = doc.AddPage();
            var gr = XGraphics.FromPdfPage(p);
            // var font = ConvertToXFont(new Font("Courier New", 8));
            var font = ConvertToXFont(new Font("Courier New", 8));
            var s = "一到十二月";
            var size = gr.MeasureString(s, font);
            Debug.WriteLine($"String: {s} ||| Size: {size}");
            XGraphicsPath path = new XGraphicsPath();
            path.AddString(s, new XFontFamily(font.Name2), XFontStyleEx.Regular, font.Size, new XPoint(10, 10), XStringFormats.TopLeft);
            //gr.DrawString(s, font, XBrushes.Black, 10, 10, XStringFormats.TopLeft); // not working with chinese
            gr.DrawPath(XBrushes.Black, path); 
            gr.DrawRectangle(XPens.Blue, new RectangleF(new PointF(10, 10), size.ToSizeF()));
            doc.Close();
        }
        private XFont ConvertToXFont(Font font)
        {
            //return new XFont(font.Name, font.Size, ConvertFontStyle(font.Style));
            return new XFont(font.Name, font.Size, ConvertFontStyleEx(font.Style));
        }
        private XFontStyleEx ConvertFontStyleEx(FontStyle style)
        {
            if (style.ToString().IndexOf(',') == -1)
            {
                switch (style)
                {
                    case FontStyle.Bold: return XFontStyleEx.Bold;
                    case FontStyle.Italic: return XFontStyleEx.Italic;
                    case FontStyle.Regular: return XFontStyleEx.Regular;
                    case FontStyle.Strikeout: return XFontStyleEx.Strikeout;
                    case FontStyle.Underline: return XFontStyleEx.Underline;
                }

            }
            return XFontStyleEx.Regular;
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    forumPlease use Discussions or the Support forum to ask Support questions - https://forum.pdfsharp.net/support

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions