Skip to content

Padding is read from wrong box when creating new page #1121

@mojavelinux

Description

@mojavelinux

When creating a new page, the padding is read from the margin box instead of the current bounding box. This can cause the indent function to effectively remove the padding twice, leading to a left margin that migrates further and further to the left.

I reported a similar problem in prawn-table. See prawnpdf/prawn-table#114

What it comes down to is that the bounding_box and indent functions don't play well together. Here's a script that reproduces the problem:

Prawn::Document.generate 'test.pdf' do
  initial_absolute_left = bounds.absolute_left
  text 'paragraph'
  bounding_box [0, cursor], width: bounds.width do
    text 'paragraph in bounding box'
    bounds.move_past_bottom
    text 'paragraph in bounding box'
  end
  indent 20 do
    bounds.move_past_bottom
  end
  if bounds.absolute_left != initial_absolute_left
    warn 'bounds were not properly restored after call to indent'
  end
  text 'paragraph'
end

The problematic logic is here:

prawn/lib/prawn/document.rb

Lines 723 to 731 in c5842a2

# This check 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

I believe the following logic will resolve the problem:

# update bounding box if not flowing from the previous page
unless @bounding_box && @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

Not only does it fix the example scenario I provided, the change also passes all tests in Asciidoctor PDF (which uses bounding boxes extensively).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions