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

Fresh work on nowrap cells #149

Open
wants to merge 5 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
22 changes: 18 additions & 4 deletions lib/prawn/table/cell/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Text < Cell

TextOptions = [:inline_format, :kerning, :size, :align, :valign,
:rotate, :rotate_around, :leading, :single_line, :skip_encoding,
:overflow, :min_font_size]
:overflow, :min_font_size, :nowrap]

TextOptions.each do |option|
define_method("#{option}=") { |v| @text_options[option] = v }
Expand Down Expand Up @@ -48,7 +48,13 @@ def font_style=(style)
# from the final width if the text is long.
#
def natural_content_width
@natural_content_width ||= [styled_width_of(@content), @pdf.bounds.width].min
if nowrap == :whitespace
@natural_content_width ||= [@content.split(/\s/).compact.map { |word| styled_width_of(word) }.max || 0, @pdf.bounds.width || 0].min
else
@natural_content_width ||= [styled_width_of(@content), @pdf.bounds.width].min
end

@natural_content_width
end

# Returns the natural height of this block of text, wrapped to the
Expand Down Expand Up @@ -80,7 +86,7 @@ def set_width_constraints
# sure we have enough width to be at least one character wide. This is
# a bit of a hack, but it should work well enough.
unless defined?(@min_width) && @min_width
min_content_width = [natural_content_width, styled_width_of_single_character].min
min_content_width = nowrap ? natural_content_width : [natural_content_width, styled_width_of_single_character].min
@min_width = padding_left + padding_right + min_content_width
super
end
Expand Down Expand Up @@ -135,7 +141,15 @@ 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
@pdf.width_of(text, options)
if text.empty?
0
else
text.lines.map { |line| @pdf.width_of(line, options) }.max
end
end
end

private
Expand Down
27 changes: 27 additions & 0 deletions manual/table/nowrap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# encoding: utf-8
#
# Columns specified as nowrap will always fit on to one line forcing others to
# resize as appropriate. If you want to allow wrapping, but ensure it's never
# in the middle of a word use <code>:whitespace</code> as the <code>:nowrap</code> option's value.
#
require File.expand_path(File.join(File.dirname(__FILE__),
%w[.. example_helper]))

filename = File.basename(__FILE__).gsub('.rb', '.pdf')
Prawn::ManualBuilder::Example.generate(filename) do
text "Normal widths:"
table([["Blah " * 10, "Blah " * 10]])
move_down 20

text "Nowrap widths:"
table([[make_cell(:content => "Blah " * 11, :nowrap => true), "Blah " * 12]])
move_down 20

text "Wide content without nowrap:"
table([[("wordword" * 7 + " word" * 4), "word " * 10]])
move_down 20

text "Wide content with whitespace nowrap:"
table([[make_cell(:content => ("wordword" * 7 + " word" * 4), :nowrap => :whitespace), "word " * 10]])
move_down 20
end
1 change: 1 addition & 0 deletions manual/table/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
s.example "cell_borders_and_bg"
s.example "cell_border_lines"
s.example "cell_text"
s.example "nowrap"
s.example "image_cells"
s.example "span"
s.example "before_rendering_page"
Expand Down
15 changes: 15 additions & 0 deletions spec/cell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ def cell(options={})
(5 * @pdf.height_of("text"))
end

it "should have a width that can fit @content without wrapping" do
c = cell(:content => "text", :padding => 10, :nowrap => true)
min_content_width = c.min_width - c.padding[1] - c.padding[3]

expect(@pdf.height_of("text", :width => min_content_width)).to eq(@pdf.height_of("text"))
end

it "should have a width that can fit @content without wrapping even if it's multiline" do
c = cell(:content => "text\nlonger", :padding => 10, :nowrap => true)
min_content_width = c.min_width - c.padding[1] - c.padding[3]

expect(@pdf.height_of("text\nlonger", :width => min_content_width)).to be <
(3 * @pdf.height_of("text"))
end

it "should defer min_width's evaluation of padding" do
c = cell(:content => "text", :padding => 100)
c.padding = 0
Expand Down