Class: Prawn::Table::Cell::Text

Inherits:
Prawn::Table::Cell show all
Defined in:
lib/prawn/table/cell/text.rb

Overview

A Cell that contains text. Has some limited options to set font family, size, and style.

Constant Summary collapse

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

Constants inherited from Prawn::Table::Cell

FPTolerance

Instance Attribute Summary collapse

Attributes inherited from Prawn::Table::Cell

#background_color, #border_colors, #border_lines, #border_widths, #borders, #colspan, #content, #dummy_cells, #height, #padding, #rowspan

Instance Method Summary collapse

Methods inherited from Prawn::Table::Cell

#avg_spanned_min_width, #border_bottom_color, #border_bottom_color=, #border_bottom_line, #border_bottom_line=, #border_bottom_width, #border_bottom_width=, #border_color=, #border_left_color, #border_left_color=, #border_left_line, #border_left_line=, #border_left_width, #border_left_width=, #border_line=, #border_right_color, #border_right_color=, #border_right_line, #border_right_line=, #border_right_width, #border_right_width=, #border_top_color, #border_top_color=, #border_top_line, #border_top_line=, #border_top_width, #border_top_width=, #border_width=, #content_height, #content_width, #draw, #draw_background, #draw_borders, #draw_bounded_content, draw_cells, #height_ignoring_span, make, #max_width, #max_width_ignoring_span, #min_width, #min_width_ignoring_span, #padding_bottom, #padding_bottom=, #padding_left, #padding_left=, #padding_right, #padding_right=, #padding_top, #padding_top=, #relative_x, #relative_y, #spanned_content_height, #spanned_content_width, #style, #width, #width=, #width_ignoring_span, #x, #x=, #y, #y=

Constructor Details

#initialize(pdf, point, options = {}) ⇒ Text

Returns a new instance of Text.



29
30
31
32
# File 'lib/prawn/table/cell/text.rb', line 29

def initialize(pdf, point, options={})
  @text_options = {}
  super
end

Instance Attribute Details

#fontObject

Returns the font that will be used to draw this cell.



36
37
38
# File 'lib/prawn/table/cell/text.rb', line 36

def font
  with_font { @pdf.font }
end

#text_color=(value) ⇒ Object (writeonly)

Sets the attribute text_color

Parameters:

  • value

    the value to set the attribute text_color to.



27
28
29
# File 'lib/prawn/table/cell/text.rb', line 27

def text_color=(value)
  @text_color = value
end

Instance Method Details

#draw_contentObject

Draws the text content into its bounding box.



67
68
69
70
71
72
73
74
75
76
# File 'lib/prawn/table/cell/text.rb', line 67

def draw_content
  with_font do
    @pdf.move_down((@pdf.font.line_gap + @pdf.font.descender)/2)
    with_text_color do
      text_box(:width => spanned_content_width + FPTolerance,
               :height => spanned_content_height + FPTolerance,
               :at => [0, @pdf.cursor]).render
    end
  end
end

#font_style=(style) ⇒ Object

Sets the style of the font in use. Equivalent to the Text::Box style option, but we already have a style method.



43
44
45
# File 'lib/prawn/table/cell/text.rb', line 43

def font_style=(style)
  @text_options[:style] = style
end

#natural_content_heightObject

Returns the natural height of this block of text, wrapped to the preset width.



57
58
59
60
61
62
63
# File 'lib/prawn/table/cell/text.rb', line 57

def natural_content_height
  with_font do
    b = text_box(:width => spanned_content_width + FPTolerance)
    b.render(:dry_run => true)
    b.height + b.line_gap
  end
end

#natural_content_widthObject

Returns the width of this text with no wrapping. This will be far off from the final width if the text is long.



50
51
52
# File 'lib/prawn/table/cell/text.rb', line 50

def natural_content_width
  @natural_content_width ||= [styled_width_of(@content), @pdf.bounds.width].min
end

#set_width_constraintsObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/prawn/table/cell/text.rb', line 78

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.
  unless defined?(@min_width) && @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