Skip to content

Commit

Permalink
fix float precision bug in text box rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
unterkoefler committed Mar 2, 2023
1 parent 7d4f6b8 commit b319393
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/prawn/text/formatted/line_wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 + FLOAT_PRECISION_DELTA
@accumulated_width += segment_width
shy = soft_hyphen(segment.encoding)
if segment[-1] == shy
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/text/formatted/wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def enough_height_for_this_line?
@descender + @line_height + @leading
end
require_relatived_total_height = @baseline_y.abs + diff
if require_relatived_total_height > @height + 0.0001
if require_relatived_total_height > @height + FLOAT_PRECISION_DELTA
# no room for the full height of this line
@arranger.repack_unretrieved
false
Expand Down
4 changes: 4 additions & 0 deletions lib/prawn/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
# This is free software. Please see the LICENSE and COPYING files for details.

module Prawn
# Useful for comparing floats for equality to avoid precision errors.
# Based on pdf-core output precision: https://github.com/prawnpdf/pdf-core/blob/1021ffc94b9d8d37854ef5199d3cbaf220bb5ab2/lib/pdf/core/pdf_object.rb#L16
FLOAT_PRECISION_DELTA = 1e-6

# Throughout the Prawn codebase, repeated calculations which can benefit from
# caching are made.
# In some cases, caching and reusing results can not only save CPU cycles but
Expand Down

0 comments on commit b319393

Please sign in to comment.