prawnpdf / prawn

Fast, Nimble PDF Writer for Ruby
https://prawnpdf.org
Other
4.67k stars 688 forks source link

text_box with shrink_to_fit sometimes crops end of sentence #1292

Open unterkoefler opened 1 year ago

unterkoefler commented 1 year ago

In rare cases, Text#text_box (https://www.rubydoc.info/gems/prawn/Prawn/Text#text_box-instance_method) with the overflow: :shrink_to_fit option causes part of the text to be cropped out. This is unexpected because if the text doesn't fix the box, it should be shrunk until it does, not cropped.

This occurs specifically when the width of the box is just big enough to fit the text and two float precisions errors occur. Say, for example, we want to put "Two words" in a text box of width 50.0. First, when shrink_to_fit runs to determine the font size, the width of "Two words" is computed as 49.999999.shrink_to_fit says, cool, this will fit, no need to reduce the font size or wrap the line. Then the line is actually printed and this time the width is computed as 50.00000001. This causes the line to be split and "words" is put on the second line, but if there isn't enough height, it will be cropped.

I have a commit with a spec that manually introduces these precision errors and has a failing spec here: https://github.com/unterkoefler/prawn/commit/aca004479435e74664ad348d87253dce96508d01

Happy to put up a pr to fix :)