Skip to content

Commit

Permalink
Don't restore y-position on stretchy bboxes
Browse files Browse the repository at this point in the history
When starting a bounding box at the bottom of a page (for example, using
span starting at the end of a page), drawing some text (advancing to the
next page), and then closing out the bbox / span, Prawn would restore
the original (bottom) y-position, thus stretching the bbox to the height
of the page. This commit resolves that issue.

Closes prawnpdf#255.
  • Loading branch information
bradediger committed Jul 18, 2011
1 parent 2ff910a commit 40723eb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/prawn/document/bounding_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ def init_bounding_box(user_block, options={}, &init_block)

self.y = @bounding_box.absolute_top
user_block.call
self.y = @bounding_box.absolute_bottom unless options[:hold_position]
unless options[:hold_position] || @bounding_box.stretchy?
self.y = @bounding_box.absolute_bottom
end

created_box, @bounding_box = @bounding_box, parent_box

Expand Down
38 changes: 38 additions & 0 deletions spec/bounding_box_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,44 @@
box.height.should == 100
end

it "should advance the y-position by bbox.height by default" do
orig_y = @pdf.y
@pdf.bounding_box [0, @pdf.cursor], :width => @pdf.bounds.width,
:height => 30 do
@pdf.text "hello"
end
@pdf.y.should.be.close(orig_y - 30, 0.001)
end

it "should not advance y-position if passed :hold_position => true" do
orig_y = @pdf.y
@pdf.bounding_box [0, @pdf.cursor], :width => @pdf.bounds.width,
:hold_position => true do
@pdf.text "hello"
end
# y only advances by height of one line ("hello")
@pdf.y.should.be.close(orig_y - @pdf.height_of("hello"), 0.001)
end

it "should not advance y-position of a stretchy bbox if it would stretch " +
"the bbox further" do
bottom = @pdf.y = @pdf.margin_box.absolute_bottom
@pdf.bounding_box [0, @pdf.margin_box.top], :width => @pdf.bounds.width do
@pdf.y = bottom
@pdf.text "hello" # starts a new page
end
@pdf.page_count.should == 2

# Restoring the position (to the absolute bottom) would stretch the bbox to
# the bottom of the page, which we don't want. This should be equivalent to
# a bbox with :hold_position => true, where we only advance by the amount
# that was actually drawn.
@pdf.y.should.be.close(
@pdf.margin_box.absolute_top - @pdf.height_of("hello"),
0.001
)
end

end

describe "Indentation" do
Expand Down

0 comments on commit 40723eb

Please sign in to comment.