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

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

Open
mojavelinux opened this issue Jul 25, 2019 · 1 comment · May be fixed by #1122
Open

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

mojavelinux opened this issue Jul 25, 2019 · 1 comment · May be fixed by #1122

Comments

@mojavelinux
Copy link
Contributor

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

@mojavelinux
Copy link
Contributor Author

The same problem occurs in go_to_page. In fact, it can happen anytime generate_margin_box is called, given the right conditions.

mojavelinux added a commit to mojavelinux/prawn that referenced this issue Aug 25, 2020
mojavelinux added a commit to mojavelinux/prawn that referenced this issue Aug 25, 2020
mojavelinux added a commit to mojavelinux/prawn that referenced this issue Jan 24, 2022
mojavelinux pushed a commit to mojavelinux/prawn that referenced this issue Jan 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant