diff --git a/lib/prawn/text.rb b/lib/prawn/text.rb index 49097f42f..b535eeb2e 100644 --- a/lib/prawn/text.rb +++ b/lib/prawn/text.rb @@ -382,7 +382,13 @@ def fill_formatted_text_box(text, options) @all_text_printed = box.everything_printed? self.y -= box.height - self.y -= box.line_gap + box.leading if @final_gap + + # If there's no remaining_text we don't really want to treat this line + # in a special way, we printed everything we wanted so the special + # single_line logic should not be triggered here. + if @final_gap || (options[:single_line] && !@all_text_printed) + self.y -= box.line_gap + box.leading + end remaining_text end diff --git a/spec/prawn/text_spec.rb b/spec/prawn/text_spec.rb index 391d62c17..dfba20b62 100644 --- a/spec/prawn/text_spec.rb +++ b/spec/prawn/text_spec.rb @@ -491,6 +491,44 @@ end end + describe 'when :final_gap is set to false, there should still be ' \ + 'a gap between the first line and the rest' do + it 'spaces the first and the second line correctly' do + text = 'hello ' * 30 + original_y = pdf.y + # First rendering with no indentation so we know what the correct + # render height should be + pdf.text(text, final_gap: false) + height_with_no_indentation = original_y - pdf.y + y_between_paragraphs = pdf.y + # The indentation size doesn't matter, it's about triggering + # a different path in the code + pdf.text(text, indent_paragraphs: 1, final_gap: false) + height_with_indentation = y_between_paragraphs - pdf.y + expect(height_with_indentation).to be_within(0.0001) + .of(height_with_no_indentation) + end + end + + describe 'single line height with no final_gap should not depend on '\ + 'indentation' do + it 'does not affect the height of a single line' do + text = 'hello' + original_y = pdf.y + # First rendering with no indentation so we know what the correct + # render height should be + pdf.text(text, final_gap: false) + height_with_no_indentation = original_y - pdf.y + y_between_paragraphs = pdf.y + # The indentation size doesn't matter, it's about triggering + # a different path in the code + pdf.text(text, indent_paragraphs: 1, final_gap: false) + height_with_indentation = y_between_paragraphs - pdf.y + expect(height_with_indentation).to be_within(0.0001) + .of(height_with_no_indentation) + end + end + describe 'when wrap to new page, and first line of new page' \ ' is not the start of a new paragraph, that line should' \ ' not be indented' do