class Prawn::FlexibleTable::Cell

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

Attributes

align[RW]
background_color[RW]
border_color[RW]
border_style[RW]
border_width[RW]
borders[RW]
colspan[RW]
document[RW]
font_size[RW]
font_style[RW]
horizontal_padding[RW]
point[RW]
rowspan[RW]
text_color[RW]
vertical_padding[RW]

Public Class Methods

new(options={}) click to toggle source

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

Of the available options listed below, :point, :width, and :text must be provided. If you are not using the Document#f_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.

# File lib/prawn/flexible-table/cell.rb, line 31
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

  @rowspan = options[:rowspan] || 1
  @colspan = options[:colspan] || 1
end

Public Instance Methods

draw() click to toggle source

Draws the cell onto the PDF document

# File lib/prawn/flexible-table/cell.rb, line 99
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

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

        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

    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_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

    @document.mask(:fill_color) do
      @document.fill_color @text_color if @text_color
      @document.text @text, options
    end
  end
end
height() click to toggle source

The height of the cell in PDF points

# File lib/prawn/flexible-table/cell.rb, line 81
def height
  @height || text_area_height + 2*@vertical_padding
end
text_area_height() click to toggle source

The height of the text area excluding the vertical padding

# File lib/prawn/flexible-table/cell.rb, line 86
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_width() click to toggle source

The width of the text area excluding the horizonal padding

# File lib/prawn/flexible-table/cell.rb, line 71
def text_area_width
  width - 2*@horizontal_padding
end
to_s() click to toggle source

Returns the cell's text as a string.

# File lib/prawn/flexible-table/cell.rb, line 66
def to_s
  @text
end
width() click to toggle source

The width of the cell in PDF points

# File lib/prawn/flexible-table/cell.rb, line 76
def width
  @width || (@document.width_of(@text, :size => @font_size)) + 2*@horizontal_padding
end