From f4b64589141f9c3c1914e0f211cb3faed0aae281 Mon Sep 17 00:00:00 2001 From: Janik Rabe Date: Mon, 14 Aug 2017 23:57:58 +0300 Subject: [PATCH 1/3] Calculate cell width of multi-line cells correctly Ensures that the optimal cell width is calculated correctly when the column's content contains more than one line. Fixes #49 --- lib/prawn/table/cell/text.rb | 8 +++++++- spec/cell_spec.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/prawn/table/cell/text.rb b/lib/prawn/table/cell/text.rb index ff28789..91a3c59 100644 --- a/lib/prawn/table/cell/text.rb +++ b/lib/prawn/table/cell/text.rb @@ -135,7 +135,13 @@ def text_box(extra_options={}) # def styled_width_of(text) options = @text_options.reject { |k| k == :style } - with_font { @pdf.width_of(text, options) } + if text.empty? + 0 + else + with_font do + text.lines.map { |line| @pdf.width_of(line, options) }.max + end + end end private diff --git a/spec/cell_spec.rb b/spec/cell_spec.rb index 4cbecff..1c50aa1 100644 --- a/spec/cell_spec.rb +++ b/spec/cell_spec.rb @@ -104,6 +104,11 @@ def cell(options={}) expect(c.width).to eq @pdf.width_of("text") + 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 +138,11 @@ def cell(options={}) expect(c.content_width).to eq @pdf.width_of("text") 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) From eaf910475e6873dae4e4706fe4459e63d2d3aeb0 Mon Sep 17 00:00:00 2001 From: Janik Rabe Date: Sun, 20 Aug 2017 19:11:25 +0300 Subject: [PATCH 2/3] Remove unnecessary conditional from #styled_width_of --- lib/prawn/table/cell/text.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/prawn/table/cell/text.rb b/lib/prawn/table/cell/text.rb index 91a3c59..b4ca538 100644 --- a/lib/prawn/table/cell/text.rb +++ b/lib/prawn/table/cell/text.rb @@ -135,12 +135,8 @@ def text_box(extra_options={}) # def styled_width_of(text) options = @text_options.reject { |k| k == :style } - if text.empty? - 0 - else - with_font do - text.lines.map { |line| @pdf.width_of(line, options) }.max - end + with_font do + text.lines.map { |line| @pdf.width_of(line, options) }.max || 0 end end From 345fb3780b0f539b93077e0b7ab71c0864bd0d04 Mon Sep 17 00:00:00 2001 From: Janik Rabe Date: Sun, 20 Aug 2017 19:11:46 +0300 Subject: [PATCH 3/3] Add specs for table cells with no text --- spec/cell_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/cell_spec.rb b/spec/cell_spec.rb index 1c50aa1..fa445a9 100644 --- a/spec/cell_spec.rb +++ b/spec/cell_spec.rb @@ -104,6 +104,11 @@ 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] @@ -138,6 +143,11 @@ 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")