diff --git a/CHANGELOG.md b/CHANGELOG.md index b5a12aafa..456133c6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ Take the font style into account when looking for a glyph and fallback fonts are (Dan Allen, [#1147](https://github.com/prawnpdf/prawn/issues/1147)) +### Restore original column after call to float + +When the `float` method is used inside of a column box, restore the original column in addition to the page number and y. + +(Dan Allen, [#1266](https://github.com/prawnpdf/prawn/issues/1266)) + ## PrawnPDF 2.4.0 ### Added support for Ruby 3 diff --git a/lib/prawn/document.rb b/lib/prawn/document.rb index 0a3942845..cc03bcc05 100644 --- a/lib/prawn/document.rb +++ b/lib/prawn/document.rb @@ -385,9 +385,11 @@ def move_cursor_to(new_y) # def float original_page = page_number + original_column = bounds.instance_variable_get :@current_column if bounds.is_a? ColumnBox original_y = y yield go_to_page(original_page) unless page_number == original_page + bounds.instance_variable_set :@current_column, original_column if original_column self.y = original_y end diff --git a/spec/prawn/document_spec.rb b/spec/prawn/document_spec.rb index 3b649629e..140375ab8 100644 --- a/spec/prawn/document_spec.rb +++ b/spec/prawn/document_spec.rb @@ -135,6 +135,18 @@ def self.format(string) expect(pdf.y).to eq(orig_y) end + it 'restores the original column' do + orig_y = pdf.y + pdf.column_box [pdf.bounds.left, pdf.cursor], width: pdf.bounds.width, columns: 2, reflow_margins: true do + pdf.float do + pdf.bounds.move_past_bottom + pdf.text 'Foo' + end + expect(pdf.y).to eq(orig_y) + expect(pdf.bounds.instance_variable_get(:@current_column)).to eq(0) + end + end + it 'teleports across pages if necessary' do pdf.float do pdf.text 'Foo' @@ -148,6 +160,24 @@ def self.format(string) expect(pages[0][:strings]).to eq(%w[Foo Baz]) expect(pages[1][:strings]).to eq(['Bar']) end + + it 'restores the original column across pages' do + orig_y = pdf.y + orig_page = pdf.page_number + pdf.column_box [pdf.bounds.left, pdf.cursor], width: pdf.bounds.width, columns: 2, reflow_margins: true do + pdf.float do + pdf.bounds.move_past_bottom + pdf.text 'Foo' + pdf.bounds.move_past_bottom + pdf.text 'Bar' + pdf.bounds.move_past_bottom + pdf.text 'Baz' + end + expect(pdf.y).to eq(orig_y) + expect(pdf.page_number).to eq(orig_page) + expect(pdf.bounds.instance_variable_get(:@current_column)).to eq(0) + end + end end describe '#start_new_page' do