diff --git a/lib/prawn/table/cell/text.rb b/lib/prawn/table/cell/text.rb index ff28789..b4ca538 100644 --- a/lib/prawn/table/cell/text.rb +++ b/lib/prawn/table/cell/text.rb @@ -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 diff --git a/spec/cell_spec.rb b/spec/cell_spec.rb index 4cbecff..fa445a9 100644 --- a/spec/cell_spec.rb +++ b/spec/cell_spec.rb @@ -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 @@ -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)