Skip to content

Commit

Permalink
restore current column when float is called inside a column box
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed May 18, 2022
1 parent 7d4f6b8 commit 392b2ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/prawn/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions spec/prawn/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down

0 comments on commit 392b2ca

Please sign in to comment.