Class: Prawn::Graphics::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/graphics/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

:width

The width in PDF points of the cell.

:border

The border line width. If omitted, no border will be drawn.

:horizontal_padding

The horizontal padding in PDF points

:vertical_padding

The vertical padding in PDF points

:padding

Overrides both horizontal and vertical padding

:border_style

One of :all, :no_top, :no_bottom, :sides

:align

One of :left, :right, :center



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/prawn/graphics/cell.rb', line 52

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

  @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.



71
72
73
# File 'lib/prawn/graphics/cell.rb', line 71

def align
  @align
end

#background_colorObject

Returns the value of attribute background_color.



71
72
73
# File 'lib/prawn/graphics/cell.rb', line 71

def background_color
  @background_color
end

#border_styleObject

Returns the value of attribute border_style.



71
72
73
# File 'lib/prawn/graphics/cell.rb', line 71

def border_style
  @border_style
end

#border_widthObject

Returns the value of attribute border_width.



71
72
73
# File 'lib/prawn/graphics/cell.rb', line 71

def border_width
  @border_width
end

#borders=(value) ⇒ Object

Sets the attribute borders

Parameters:

  • value

    the value to set the attribute borders to.



71
72
73
# File 'lib/prawn/graphics/cell.rb', line 71

def borders=(value)
  @borders = value
end

#documentObject

Returns the value of attribute document.



71
72
73
# File 'lib/prawn/graphics/cell.rb', line 71

def document
  @document
end

#heightObject

The height of the cell in PDF points



98
99
100
# File 'lib/prawn/graphics/cell.rb', line 98

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

#horizontal_paddingObject

Returns the value of attribute horizontal_padding.



71
72
73
# File 'lib/prawn/graphics/cell.rb', line 71

def horizontal_padding
  @horizontal_padding
end

#pointObject

Returns the value of attribute point.



71
72
73
# File 'lib/prawn/graphics/cell.rb', line 71

def point
  @point
end

#vertical_paddingObject

Returns the value of attribute vertical_padding.



71
72
73
# File 'lib/prawn/graphics/cell.rb', line 71

def vertical_padding
  @vertical_padding
end

#widthObject

The width of the cell in PDF points



91
92
93
94
# File 'lib/prawn/graphics/cell.rb', line 91

def width
  @width || (@document.font.metrics.string_width(@text,
    @document.font.size)) + 2*@horizontal_padding
end

Instance Method Details

#drawObject

Draws the cell onto the PDF document



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
# File 'lib/prawn/graphics/cell.rb', line 110

def draw
  rel_point = @point                
  
  if @background_color    
    @document.mask(:fill_color) do
      @document.fill_color @background_color  
      h  = borders.include?(:bottom) ? 
        height - border_width : height + border_width / 2.0
      @document.fill_rectangle [rel_point[0] + border_width / 2.0, 
                                rel_point[1] - border_width / 2.0 ], 
          width - border_width, h  
    end
  end

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

      if borders.include?(:left)
        @document.stroke_line [rel_point[0], rel_point[1] + (@border_width / 2.0)], 
          [rel_point[0], rel_point[1] - height - @border_width / 2.0 ]
      end

      if borders.include?(:right)
        @document.stroke_line( 
          [rel_point[0] + width, rel_point[1] + (@border_width / 2.0)],
          [rel_point[0] + width, rel_point[1] - height - @border_width / 2.0] )
      end

      if borders.include?(:top)
        @document.stroke_line(
          [ rel_point[0] + @border_width / 2.0, rel_point[1] ], 
          [ rel_point[0] - @border_width / 2.0 + width, rel_point[1] ])
      end

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

    end
    
    borders

  end

  @document.bounding_box( [@point[0] + @horizontal_padding, 
                           @point[1] - @vertical_padding], 
                          :width   => text_area_width,
                          :height  => height - @vertical_padding) do
    @document.move_up @document.font.line_gap
    @document.text @text, :align => @align
  end
end

#text_area_heightObject

The height of the text area excluding the vertical padding



104
105
106
# File 'lib/prawn/graphics/cell.rb', line 104

def text_area_height
  @document.font.height_of(@text, :line_width => text_area_width) 
end

#text_area_widthObject

The width of the text area excluding the horizonal padding



85
86
87
# File 'lib/prawn/graphics/cell.rb', line 85

def text_area_width
  width - 2*@horizontal_padding
end

#to_sObject

Returns the cell’s text as a string.



79
80
81
# File 'lib/prawn/graphics/cell.rb', line 79

def to_s
  @text
end