diff --git a/lib/prawn/text/formatted/box.rb b/lib/prawn/text/formatted/box.rb index 16b6b3a4f..ceb02dfee 100644 --- a/lib/prawn/text/formatted/box.rb +++ b/lib/prawn/text/formatted/box.rb @@ -226,7 +226,7 @@ def render(flags = {}) if @rotate != 0 && @inked render_rotated(text) else - wrap(text) + wrap(text, epsilon: -0.000001) end @inked = false end @@ -550,7 +550,7 @@ def shrink_to_fit(text) loop do if @disable_wrap_by_char && @font_size > @min_font_size begin - wrap(text) + wrap(text, epsilon: 0.000001) rescue Errors::CannotFit # Ignore errors while we can still attempt smaller # font sizes. diff --git a/lib/prawn/text/formatted/line_wrap.rb b/lib/prawn/text/formatted/line_wrap.rb index eac14dff3..858e78342 100644 --- a/lib/prawn/text/formatted/line_wrap.rb +++ b/lib/prawn/text/formatted/line_wrap.rb @@ -46,7 +46,7 @@ def wrap_line(options) fragment.lstrip! if first_fragment_on_this_line?(fragment) next if empty_line?(fragment) - unless apply_font_settings_and_add_fragment_to_line(fragment) + unless apply_font_settings_and_add_fragment_to_line(fragment, epsilon: options[:epsilon]) break end end @@ -74,10 +74,10 @@ def next_string_newline? @arranger.preview_next_string == "\n" end - def apply_font_settings_and_add_fragment_to_line(fragment) + def apply_font_settings_and_add_fragment_to_line(fragment, epsilon: 0.0) result = nil @arranger.apply_font_settings do - result = add_fragment_to_line(fragment) + result = add_fragment_to_line(fragment, epsilon: epsilon) end result end @@ -85,7 +85,7 @@ def apply_font_settings_and_add_fragment_to_line(fragment) # returns true if all text was printed without running into the end of # the line # - def add_fragment_to_line(fragment) + def add_fragment_to_line(fragment, epsilon: 0.0) case fragment when '' true @@ -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 + epsilon @accumulated_width += segment_width shy = soft_hyphen(segment.encoding) if segment[-1] == shy diff --git a/lib/prawn/text/formatted/wrap.rb b/lib/prawn/text/formatted/wrap.rb index 81b4fdac5..45b0b7872 100644 --- a/lib/prawn/text/formatted/wrap.rb +++ b/lib/prawn/text/formatted/wrap.rb @@ -44,7 +44,7 @@ def initialize(_array, options) # # Returns any formatted text that was not printed # - def wrap(array) # :nodoc: + def wrap(array, epsilon: 0.0) # :nodoc: initialize_wrap(array) stop = false @@ -57,7 +57,8 @@ def wrap(array) # :nodoc: kerning: @kerning, width: available_width, arranger: @arranger, - disable_wrap_by_char: @disable_wrap_by_char + disable_wrap_by_char: @disable_wrap_by_char, + epsilon: epsilon ) if enough_height_for_this_line? diff --git a/spec/prawn/text/box_spec.rb b/spec/prawn/text/box_spec.rb index 6234d07d8..489fbd46f 100644 --- a/spec/prawn/text/box_spec.rb +++ b/spec/prawn/text/box_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'spec_helper' +require 'byebug' describe Prawn::Text::Box do let(:pdf) { create_pdf } @@ -830,6 +831,24 @@ ) end + it 'displays the entire text when the box is just big enough' do + text = 'Two words' + size = 12.0 + text_box = described_class.new( + text, + options.merge( + size: size, + width: 56.328, # pdf.width_of(text, size: size), + height: 12.0, # pdf.height_of(text, size: size), + overflow: :shrink_to_fit, + disable_wrap_by_char: true + ) + ) + + overflow = text_box.render + expect(overflow).to eq('') + end + it 'displays the entire text' do text_box.render expect(text_box.text.tr("\n", ' ')).to eq(text.strip)