-
Notifications
You must be signed in to change notification settings - Fork 689
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
fix float precision bug in text box rendering #1293
base: master
Are you sure you want to change the base?
fix float precision bug in text box rendering #1293
Conversation
@@ -101,7 +101,7 @@ def add_fragment_to_line(fragment) | |||
@document.width_of(segment, kerning: @kerning) | |||
end | |||
|
|||
if @accumulated_width + segment_width <= @width | |||
if @accumulated_width + segment_width <= @width + 0.00001 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a test that demonstrates validity of this fix?
Also could you please explain the value of that number? Why can't it be smaller/bigger?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit adds a test that fails, but it does so by manually inserting the necessary rounding errors, so I didn't add it to this pr. Because this bug is so rare, I don't know a good way to add a test for it without changing the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose the value 0.00001
arbitrarily. Seems like it's big enough fix the bug and small enough to not matter visually. The ruby docs for float link to this page which uses 0.0001
as a delta. Happy to update to that instead if you think that would be better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI In the geom2d gem which heavily uses floating point calculations I have implemented special comparison methods, see https://github.com/gettalong/geom2d/blob/master/lib/geom2d/utils.rb#L24-L34. The precision to use is arbitrarily set.
For Prawn I would probably choose something that reflects the output precision used in https://github.com/prawnpdf/pdf-core/blob/master/lib/pdf/core/pdf_object.rb#L16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @gettalong!
@pointlessone I updated to a value based on the precision and also updated one other place doing a similar comparison to use the same value
44b871f
to
b319393
Compare
I still don't quite understand the root cause of the issue. |
When we render a text box with shrink_to_fit enabled, we call In The bug occurs specifically when the LHS Now, The assumption being violated here is that subsequent calls to Also, regardless, |
fixes #1292