Class: Prawn::Table::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/table/cell.rb

Overview

A cell is a special-purpose bounding box designed to flow text within a bordered area. This is used by Prawn’s Document::Table implementation but can also be used standalone for drawing text boxes via Document#cell

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Cell

Creates a new cell object. Generally used indirectly via Document#cell

Of the available options listed below, :point, :width, and :text must be provided. If you are not using the Document#cell shortcut, the :document must also be provided.

:point

Absolute [x,y] coordinate of the top-left corner of the cell.

:document

The Prawn::Document object to render on.

:text

The text to be flowed within the cell

:text_color

The color of the text to be displayed

:width

The width in PDF points of the cell.

:height

The height in PDF points of the cell.

:horizontal_padding

The horizontal padding in PDF points

:vertical_padding

The vertical padding in PDF points

:padding

Overrides both horizontal and vertical padding

:align

One of :left, :right, :center

:borders

An array of sides which should have a border. Any of :top, :left, :right, :bottom

:border_width

The border line width. Defaults to 1.

:border_style

One of :all, :no_top, :no_bottom, :sides, :none, :bottom_only. Defaults to :all.

:border_color

The color of the cell border.

:font_size

The font size for the cell text.

:font_style

The font style for the cell text.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/prawn/table/cell.rb', line 57

def initialize(options={})
  @point        = options[:point]
  @document     = options[:document]
  @text         = options[:text].to_s
  @text_color   = options[:text_color]
  @width        = options[:width]
  @height       = options[:height]
  @borders      = options[:borders]
  @border_width = options[:border_width] || 1
  @border_style = options[:border_style] || :all               
  @border_color = options[:border_color]
  @background_color = options[:background_color] 
  @align            = options[:align] || :left
  @font_size        = options[:font_size]
  @font_style       = options[:font_style]

  @horizontal_padding = options[:horizontal_padding] || 0
  @vertical_padding   = options[:vertical_padding]   || 0

  if options[:padding]
    @horizontal_padding = @vertical_padding = options[:padding]
  end
  
end

Instance Attribute Details

#alignObject

Returns the value of attribute align.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def align
  @align
end

#background_colorObject

Returns the value of attribute background_color.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def background_color
  @background_color
end

#border_colorObject

Returns the value of attribute border_color.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def border_color
  @border_color
end

#border_styleObject

Returns the value of attribute border_style.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def border_style
  @border_style
end

#border_widthObject

Returns the value of attribute border_width.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def border_width
  @border_width
end

#borders=(value) ⇒ Object

Sets the attribute borders

Parameters:

  • value

    the value to set the attribute borders to.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def borders=(value)
  @borders = value
end

#documentObject

Returns the value of attribute document.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def document
  @document
end

#font_sizeObject

Returns the value of attribute font_size.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def font_size
  @font_size
end

#font_styleObject

Returns the value of attribute font_style.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def font_style
  @font_style
end

#heightObject

The height of the cell in PDF points



108
109
110
# File 'lib/prawn/table/cell.rb', line 108

def height  
  @height || text_area_height + 2*@vertical_padding
end

#horizontal_paddingObject

Returns the value of attribute horizontal_padding.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def horizontal_padding
  @horizontal_padding
end

#pointObject

Returns the value of attribute point.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def point
  @point
end

#text_colorObject

Returns the value of attribute text_color.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def text_color
  @text_color
end

#vertical_paddingObject

Returns the value of attribute vertical_padding.



82
83
84
# File 'lib/prawn/table/cell.rb', line 82

def vertical_padding
  @vertical_padding
end

#widthObject

The width of the cell in PDF points



102
103
104
# File 'lib/prawn/table/cell.rb', line 102

def width
  @width || (@document.width_of(@text, :size => @font_size)) + 2*@horizontal_padding
end

Instance Method Details

#drawObject

Draws the cell onto the PDF document



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/prawn/table/cell.rb', line 128

def draw
  margin = @border_width / 2.0
  
  if @background_color    
    old_color = @document.fill_color || "000000"
    @document.fill_color(@background_color)
    h  = borders.include?(:bottom) ? 
    height - ( 2 * margin ) : height + margin
    @document.fill_rectangle [x, y ], width, h  

    @document.fill_color(old_color)
  end

  if @border_width > 0
    @document.mask(:line_width) do
      @document.line_width = @border_width

      @document.mask(:stroke_color) do
        @document.stroke_color @border_color if @border_color

        if borders.include?(:left)
          @document.stroke_line [x, y + margin], 
            [x, y - height - margin ]
        end

        if borders.include?(:right)
          @document.stroke_line( 
            [x + width, y + margin],
            [x + width, y - height - margin] )
        end

        if borders.include?(:top)
          @document.stroke_line(
            [ x, y ], 
            [ x + width, y ])
        end

        if borders.include?(:bottom)
          @document.stroke_line [x, y - height ],
                              [x + width, y - height]
        end
      end

    end
    
    borders

  end

  @document.bounding_box( [x + @horizontal_padding, 
                           y - @vertical_padding], 
                          :width   => text_area_width,
                          :height  => height - @vertical_padding) do
    @document.move_down((@document.font.line_gap +
                         @document.font.descender) / 2)

    options = {:align => @align, :final_gap => false}

    options[:size] = @font_size if @font_size
    options[:style] = @font_style if @font_style

    old_color = @document.fill_color || "000000"
    @document.fill_color @text_color if @text_color                        
    @document.text @text, options
    @document.fill_color old_color
  end
end

#text_area_heightObject

The height of the text area excluding the vertical padding



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/prawn/table/cell.rb', line 114

def text_area_height
  text_height = 0
  if @font_size
    @document.font_size(@font_size) do
      text_height = @document.height_of(@text, :width => text_area_width)
    end
  else
    text_height = @document.height_of(@text, :width => text_area_width)
  end
  text_height
end

#text_area_widthObject

The width of the text area excluding the horizonal padding



96
97
98
# File 'lib/prawn/table/cell.rb', line 96

def text_area_width
  width - 2*@horizontal_padding
end

#to_sObject

Returns the cell’s text as a string.



90
91
92
# File 'lib/prawn/table/cell.rb', line 90

def to_s
  @text
end