lib/prawn/table/cell/text.rb in prawn-1.0.0.rc1 vs lib/prawn/table/cell/text.rb in prawn-1.0.0.rc2
- old
+ new
@@ -45,19 +45,19 @@
# Returns the width of this text with no wrapping. This will be far off
# from the final width if the text is long.
#
def natural_content_width
- [styled_width_of(@content), @pdf.bounds.width].min
+ @natural_content_width ||= [styled_width_of(@content), @pdf.bounds.width].min
end
# Returns the natural height of this block of text, wrapped to the
# preset width.
#
def natural_content_height
with_font do
- b = text_box(:width => content_width + FPTolerance)
+ b = text_box(:width => spanned_content_width + FPTolerance)
b.render(:dry_run => true)
b.height + b.line_gap
end
end
@@ -65,24 +65,26 @@
#
def draw_content
with_font do
@pdf.move_down((@pdf.font.line_gap + @pdf.font.descender)/2)
with_text_color do
- text_box(:width => content_width + FPTolerance,
- :height => content_height + FPTolerance,
+ text_box(:width => spanned_content_width + FPTolerance,
+ :height => spanned_content_height + FPTolerance,
:at => [0, @pdf.cursor]).render
end
end
end
def set_width_constraints
# Sets a reasonable minimum width. If the cell has any content, make
# 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.
- min_content_width = [natural_content_width, styled_width_of("M")].min
- @min_width ||= padding_left + padding_right + min_content_width
- super
+ unless @min_width
+ min_content_width = [natural_content_width, styled_width_of_single_character].min
+ @min_width = padding_left + padding_right + min_content_width
+ super
+ end
end
protected
def with_font
@@ -95,25 +97,32 @@
yield
end
end
def with_text_color
- old_color = @pdf.fill_color || '000000'
- @pdf.fill_color(@text_color) if @text_color
- yield
- ensure
- @pdf.fill_color(old_color)
+ if @text_color
+ begin
+ old_color = @pdf.fill_color || '000000'
+ @pdf.fill_color(@text_color)
+ yield
+ ensure
+ @pdf.fill_color(old_color)
+ end
+ else
+ yield
+ end
end
def text_box(extra_options={})
if @text_options[:inline_format]
options = @text_options.dup
options.delete(:inline_format)
+ options.merge!(extra_options)
+ options[:document] = @pdf
array = ::Prawn::Text::Formatted::Parser.to_array(@content)
- ::Prawn::Text::Formatted::Box.new(array,
- options.merge(extra_options).merge(:document => @pdf))
+ ::Prawn::Text::Formatted::Box.new(array, options)
else
::Prawn::Text::Box.new(@content, @text_options.merge(extra_options).
merge(:document => @pdf))
end
end
@@ -122,9 +131,22 @@
#
def styled_width_of(text)
@pdf.width_of(text, @text_options)
end
+ private
+
+ # Returns the greatest possible width of any single character
+ # 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
+ key = (@text_options[:style] == :bold) ? :bold_char_width : :plain_char_width
+ cache = Thread.current[key] ||= {}
+ cache[@pdf.font] ||= styled_width_of("M")
+ end
end
end
end
end