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

resolves #114 position table using indent instead of bounding_box #115

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
27 changes: 12 additions & 15 deletions lib/prawn/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -672,27 +672,24 @@ def position_cells
y_positions.each_with_index { |y, i| row(i).y = y }
end

# Sets up a bounding box to position the table according to the specified
# :position option, and yields.
# Adjusts the box padding to position the table according to the specified
# :position option, then yields.
#
def with_position
x = case defined?(@position) && @position || :left
when :left then return yield
when :center then (@pdf.bounds.width - width) / 2.0
when :right then @pdf.bounds.width - width
when Numeric then @position
pad_left, pad_right = case defined?(@position) && @position || :left
when :left then [0, 0]
when :center then [(@pdf.bounds.width - width) / 2.0] * 2
when :right then [@pdf.bounds.width - width, 0]
when Numeric then [@position, 0]
else raise ArgumentError, "unknown position #{@position.inspect}"
end
dy = @pdf.bounds.absolute_top - @pdf.y
final_y = nil

@pdf.bounding_box([x, @pdf.bounds.top], :width => width) do
@pdf.move_down dy
if pad_left > 0
@pdf.indent pad_left, pad_right do
yield
end
else
yield
final_y = @pdf.y
end

@pdf.y = final_y
end

end
Expand Down
18 changes: 15 additions & 3 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -689,24 +689,36 @@
end

describe "position" do
it "should not position table if :position => :left" do
expect(@pdf).not_to receive(:indent)

@pdf.table([["foo"]], :column_widths => 500, :position => :left)
end

it "should center tables with :position => :center" do
expect(@pdf).to receive(:bounding_box).with([(@pdf.bounds.width - 500) / 2.0, anything], kind_of(Hash))
expect(@pdf).to receive(:indent).with((@pdf.bounds.width - 500) / 2.0, (@pdf.bounds.width - 500) / 2.0)

@pdf.table([["foo"]], :column_widths => 500, :position => :center)
end

it "should right-align tables with :position => :right" do
expect(@pdf).to receive(:bounding_box).with([@pdf.bounds.width - 500, anything], kind_of(Hash))
expect(@pdf).to receive(:indent).with(@pdf.bounds.width - 500, 0)

@pdf.table([["foo"]], :column_widths => 500, :position => :right)
end

it "should accept a Numeric" do
expect(@pdf).to receive(:bounding_box).with([123, anything], kind_of(Hash))
expect(@pdf).to receive(:indent).with(123, 0)

@pdf.table([["foo"]], :column_widths => 500, :position => 123)
end

it "should not position table if table width matches width of bounds" do
expect(@pdf).not_to receive(:indent)

@pdf.table([["foo"]], :column_widths => @pdf.bounds.width, :position => :center)
end

it "should raise_error an ArgumentError on unknown :position" do
expect {
@pdf.table([["foo"]], :position => :bratwurst)
Expand Down