Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MeasureString error #198

Open
carlos-FG opened this issue Oct 29, 2024 · 3 comments
Open

MeasureString error #198

carlos-FG opened this issue Oct 29, 2024 · 3 comments
Labels
investigation Under investigation

Comments

@carlos-FG
Copy link

carlos-FG commented Oct 29, 2024

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
Copy link
Member

ThomasHoevel commented Oct 30, 2024

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.

@ThomasHoevel ThomasHoevel added the Cannot Reproduce https://xkcd.com/583/ label Oct 30, 2024
@carlos-FG
Copy link
Author

carlos-FG commented Oct 30, 2024 via email

@ThomasHoevel
Copy link
Member

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.

@ThomasHoevel ThomasHoevel added investigation Under investigation and removed Cannot Reproduce https://xkcd.com/583/ labels Oct 31, 2024
@ThomasHoevel ThomasHoevel reopened this Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigation Under investigation
Projects
None yet
Development

No branches or pull requests

2 participants