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

Improve min_width calculation using longest word #98

Open
wants to merge 1 commit 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
11 changes: 5 additions & 6 deletions lib/prawn/table/cell/text.rb
Original file line number Diff line number Diff line change
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 = [natural_content_width, styled_width_of_longest_word].min
@min_width = padding_left + padding_right + min_content_width
super
end
Expand Down Expand Up @@ -140,14 +140,13 @@ def styled_width_of(text)

private

# Returns the greatest possible width of any single character
# Returns the greatest possible width of the longest word in content
# under the given text options.
# (We use this to determine the minimum width of a table cell)
# (Although we currently determine this by measuring "M", it should really
# use whichever character is widest under the current font)
#
def styled_width_of_single_character
styled_width_of("M")
def styled_width_of_longest_word
longest_word = @content.split(" ").max_by(&:length)
styled_width_of(longest_word)
end
end
end
Expand Down