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 multiline text cell width and add nowrap option #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 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 @@ -80,7 +80,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 @@ -134,7 +134,11 @@ def text_box(extra_options={})
# Returns the width of +text+ under the given text options.
#
def styled_width_of(text)
@pdf.width_of(text, @text_options)
if text.empty?
0
else
text.lines.collect{|line|@pdf.width_of(line, @text_options)}.max
end
end

private
Expand Down