diff --git a/CHANGELOG.md b/CHANGELOG.md index b5a12aafa..e9d264fa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## Unreleased +### Apply correct margin box when creating or switching pages + +When creating a new page or returning to a page previously created, compute the margin box correctly. + +(Dan Allen, [#1122](https://github.com/prawnpdf/prawn/pull/1022)) + ### Look for glyph in correct font Take the font style into account when looking for a glyph and fallback fonts are enabled. diff --git a/lib/prawn/document.rb b/lib/prawn/document.rb index 439667d5a..4c25c9959 100644 --- a/lib/prawn/document.rb +++ b/lib/prawn/document.rb @@ -723,15 +723,17 @@ def generate_margin_box (page.margins[:top] + page.margins[:bottom]) ) - # This check maintains indentation settings across page breaks + # update bounding box if not flowing from the previous page + unless @bounding_box&.parent + old_margin_box = @bounding_box + @bounding_box = @margin_box + end + + # maintains indentation settings across page breaks if old_margin_box @margin_box.add_left_padding(old_margin_box.total_left_padding) @margin_box.add_right_padding(old_margin_box.total_right_padding) end - - # we must update bounding box if not flowing from the previous page - # - @bounding_box = @margin_box unless @bounding_box&.parent end def apply_margin_options(options) diff --git a/spec/prawn/document_spec.rb b/spec/prawn/document_spec.rb index 3b649629e..029c044c7 100644 --- a/spec/prawn/document_spec.rb +++ b/spec/prawn/document_spec.rb @@ -126,6 +126,19 @@ def self.format(string) expect(pdf.instance_variable_get(:@background)).to eq(filename) end end + + it 'retains the current margin and padding' do + initial_absolute_left = pdf.bounds.absolute_left + + pdf.bounding_box [0, pdf.cursor], width: pdf.bounds.width do + pdf.bounds.move_past_bottom + end + pdf.indent 20 do + pdf.bounds.move_past_bottom + end + + expect(pdf.bounds.absolute_left).to eq(initial_absolute_left) + end end describe '#float' do