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

fix float precision bug in text box rendering #1293

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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