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

Calculate cell width of multi-line cells correctly #91

Open
wants to merge 3 commits 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
4 changes: 3 additions & 1 deletion lib/prawn/table/cell/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def text_box(extra_options={})
#
def styled_width_of(text)
options = @text_options.reject { |k| k == :style }
with_font { @pdf.width_of(text, options) }
with_font do
text.lines.map { |line| @pdf.width_of(line, options) }.max || 0
end
end

private
Expand Down
20 changes: 20 additions & 0 deletions spec/cell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ def cell(options={})
expect(c.width).to eq @pdf.width_of("text") + c.padding[1] + c.padding[3]
end

it "should be calculated for empty text" do
c = cell(:content => "")
expect(c.width).to eq c.padding[1] + c.padding[3]
end

it "should be calculated for multiline text" do
c = cell(:content => "text\na\nb")
expect(c.width).to eq @pdf.width_of("text") + c.padding[1] + c.padding[3]
end

it "should be overridden by manual :width" do
c = cell(:content => "text", :width => 400)
expect(c.width).to eq 400
Expand Down Expand Up @@ -133,6 +143,16 @@ def cell(options={})
expect(c.content_width).to eq @pdf.width_of("text")
end

it "content_width should exclude padding for empty text" do
c = cell(:content => "", :padding => 10)
expect(c.content_width).to eq 0
end

it "content_width should exclude padding with multiple lines" do
c = cell(:content => "text\na\nb", :padding => 10)
expect(c.content_width).to eq @pdf.width_of("text")
end

it "content_width should exclude padding even with manual :width" do
c = cell(:content => "text", :padding => 10, :width => 400)
expect(c.content_width).to be_within(0.01).of(380)
Expand Down