empira / PDFsharp

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

MeasureString error #198

Open carlos-FG opened 3 weeks ago

carlos-FG commented 3 weeks ago

When obtaining the height of a text using MeasureString and using it as the height for an XTextFormatter with multiple lines of text, the last line does not appear. Comparing with version 1.3 of PDFsharp, I found that the measurement returned by version 6 is smaller.

XFont font = new XFont("Arial", 20, XFontStyleEx.Regular); XTextFormatter tx = new XTextFormatter(gfx); double textHeight = gfx.MeasureString("hello", font).Height; // two-line text string text = "hello\nworld"; // 2 lines height textHeight *=2; tx.DrawString(text, font, XBrushes.Black, new XRect(200, 200, 50, textHeight), XStringFormats.TopLeft);

ThomasHoevel commented 3 weeks ago

Cannot replicate. Consider using the IssueSubmissionTemplate to allow us to replicate the issue.

XTextFormatter uses font.GetHeight() internally. That gives me the same value I also get from MeasureString.

image

Maybe you have to make the rectangle larger to account for rounding errors, because 5 * x may give a different value than x + x + x + x + x.

My code above draws both lines of text.

carlos-FG commented 3 weeks ago

I tested using the IssueSubmissionTemplate and was able to reproduce the issue when FailsafeFontResolver isn’t used. Since I’m working on a web server, this functionality isn’t available. I can use font.GetHeight(), which returns the correct measurement. Thank you for your insights.

De: ThomasHoevel @.> Enviado el: miércoles, 30 de octubre de 2024 8:41 Para: empira/PDFsharp @.> CC: carlos-FG @.>; Author @.> Asunto: Re: [empira/PDFsharp] MeasureString error (Issue #198)

Cannot replicate. Consider using the IssueSubmissionTemplate to allow us to replicate the issue. XTextFormatter uses font.GetHeight() internally. That gives me the same value I also get from MeasureString. image.png (view on web) https://github.com/user-attachments/assets/362722d2-a356-47a4-9438-570c4368c536 Maybe you have to make the rectangle larger to account for rounding errors, because 5 * x may give a different value than x + x + x + x + x.

ThomasHoevel commented 3 weeks ago

I can confirm that font.GetHeight() sometimes return larger values than MeasureString. GetHeight() is used by XTextFormatter and should be used to calculate the rectangle.