Skip to content

Commit e6cf988

Browse files
committed
Fresh work on nowrap cells
Based almost entirely on: prawnpdf#50 Implements the wrapping, adds no more failures (than the current master fails on 3.0.4 anyway), but adds new tests and documentation.
1 parent 38b5bdb commit e6cf988

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

lib/prawn/table/cell/text.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Text < Cell
1717

1818
TextOptions = [:inline_format, :kerning, :size, :align, :valign,
1919
:rotate, :rotate_around, :leading, :single_line, :skip_encoding,
20-
:overflow, :min_font_size]
20+
:overflow, :min_font_size, :nowrap]
2121

2222
TextOptions.each do |option|
2323
define_method("#{option}=") { |v| @text_options[option] = v }
@@ -80,7 +80,7 @@ def set_width_constraints
8080
# sure we have enough width to be at least one character wide. This is
8181
# a bit of a hack, but it should work well enough.
8282
unless defined?(@min_width) && @min_width
83-
min_content_width = [natural_content_width, styled_width_of_single_character].min
83+
min_content_width = nowrap ? natural_content_width : [natural_content_width, styled_width_of_single_character].min
8484
@min_width = padding_left + padding_right + min_content_width
8585
super
8686
end
@@ -135,7 +135,15 @@ def text_box(extra_options={})
135135
#
136136
def styled_width_of(text)
137137
options = @text_options.reject { |k| k == :style }
138-
with_font { @pdf.width_of(text, options) }
138+
139+
with_font do
140+
@pdf.width_of(text, options)
141+
if text.empty?
142+
0
143+
else
144+
text.lines.map { |line| @pdf.width_of(line, options) }.max
145+
end
146+
end
139147
end
140148

141149
private

manual/table/nowrap.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# encoding: utf-8
2+
#
3+
# Columns specified as nowrap will always fit on to one line forcing others to
4+
# resize as appropriate.
5+
#
6+
require File.expand_path(File.join(File.dirname(__FILE__),
7+
%w[.. example_helper]))
8+
9+
filename = File.basename(__FILE__).gsub('.rb', '.pdf')
10+
Prawn::ManualBuilder::Example.generate(filename) do
11+
text "Normal widths:"
12+
table([["Blah " * 10, "Blah " * 10]])
13+
move_down 20
14+
15+
text "Nowrap widths:"
16+
table([[cell(:content => "Blah " * 10, :nowrap => true), "Blah " * 10]])
17+
move_down 20
18+
end

manual/table/table.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
s.example "cell_borders_and_bg"
2525
s.example "cell_border_lines"
2626
s.example "cell_text"
27+
s.example "nowrap"
2728
s.example "image_cells"
2829
s.example "span"
2930
s.example "before_rendering_page"

spec/cell_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,21 @@ def cell(options={})
154154
(5 * @pdf.height_of("text"))
155155
end
156156

157+
it "should have a width that can fit @content without wrapping" do
158+
c = cell(:content => "text", :padding => 10, :nowrap => true)
159+
min_content_width = c.min_width - c.padding[1] - c.padding[3]
160+
161+
expect(@pdf.height_of("text", :width => min_content_width)).to eq(@pdf.height_of("text"))
162+
end
163+
164+
it "should have a width that can fit @content without wrapping even if it's multiline" do
165+
c = cell(:content => "text\nlonger", :padding => 10, :nowrap => true)
166+
min_content_width = c.min_width - c.padding[1] - c.padding[3]
167+
168+
expect(@pdf.height_of("text\nlonger", :width => min_content_width)).to be <
169+
(3 * @pdf.height_of("text"))
170+
end
171+
157172
it "should defer min_width's evaluation of padding" do
158173
c = cell(:content => "text", :padding => 100)
159174
c.padding = 0

0 commit comments

Comments
 (0)