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_widths, #borders, #content, #height, #padding

Instance Method Summary collapse

Methods inherited from Prawn::Table::Cell

#border_bottom_color, #border_bottom_color=, #border_bottom_width, #border_bottom_width=, #border_color=, #border_left_color, #border_left_color=, #border_left_width, #border_left_width=, #border_right_color, #border_right_color=, #border_right_width, #border_right_width=, #border_top_color, #border_top_color=, #border_top_width, #border_top_width=, #border_width=, #content_height, #content_width, #draw, #draw_background, #draw_borders, #draw_bounded_content, draw_cells, make, #max_width, #min_width, #padding_bottom, #padding_bottom=, #padding_left, #padding_left=, #padding_right, #padding_right=, #padding_top, #padding_top=, #style, #width, #width=, #x, #x=, #y, #y=

Constructor Details

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

Returns a new instance of Text.



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

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

Instance Attribute Details

#fontObject

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



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

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.



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

def text_color=(value)
  @text_color = value
end

Instance Method Details

#draw_contentObject

Draws the text content into its bounding box.



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

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,
               :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.



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

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.



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

def natural_content_height
  with_font do
    b = text_box(:width => 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.



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

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

#set_width_constraintsObject



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

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
end