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

restore current column when float is called inside a column box #1267

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
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