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

Exception: WidthCannotBeNegative #173

Open
mnadeem-mercedes-hpp opened this issue Sep 25, 2024 · 5 comments
Open

Exception: WidthCannotBeNegative #173

mnadeem-mercedes-hpp opened this issue Sep 25, 2024 · 5 comments
Labels
investigation Under investigation

Comments

@mnadeem-mercedes-hpp
Copy link

width += descriptor.GlyphIndexToWidth(glyphIndex);

The integer width gets negative once the value of width tries to go above 2147483647 (i.e. maximum integer value).

I have a string with a length of a little over 9.8 million characters and when I try to measure width for that string it throws following exception

Exception Message: WidthCannotBeNegative

Stack:
at PdfSharp.Fonts.Internal.FontHelper.MeasureString(CodePointGlyphIndexPair[] codeRun, XFont font) at PdfSharp.Drawing.XGraphics.MeasureString(String text, XFont font, XStringFormat stringFormat) at PdfSharp.Drawing.XGraphics.MeasureString(String text, XFont font)

@ThomasHoevel
Copy link
Member

What's the point of measuring 9.8 million characters? Do you want to write them all in a single line?
That amount of text fills about 30,000 regular pages and is not normally rendered in a single line.

@ThomasHoevel ThomasHoevel added the investigation Under investigation label Sep 25, 2024
@mnadeem-mercedes-hpp
Copy link
Author

In our implementation, the width from MeasureString method only matters as long as it is less than PageSize.Width otherwise the string is broken into multiple lines that can fit into the PageSize.Width.

@ThomasHoevel
Copy link
Member

If the text length is more than e.g. 1000 characters, your code could (and IMHO should) start line-breaking without calling MeasureString.
Should increase the performance of the code, too.

I don't think we should change MeasureString from "int" to "longint", as this would make the code slower.

@mnadeem-mercedes-hpp
Copy link
Author

I agree - there's no point in changing MeasureString to longint, however uint or ushort can be considered, just to avoid going negative?

@ThomasHoevel
Copy link
Member

With uint we risk having an undetected overflow at around 19 million characters.
We'll think about it.

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