Class: Prawn::Table::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/table/cell.rb,
lib/prawn/table/cell/text.rb,
lib/prawn/table/cell/in_table.rb,
lib/prawn/table/cell/subtable.rb

Overview

A Cell is a rectangular area of the page into which content is drawn. It has a framework for sizing itself and adding padding and simple styling. There are several standard Cell subclasses that handle things like text, Tables, and (in the future) stamps, images, and arbitrary content.

Cells are a basic building block for table support (see Prawn::Table).

Please subclass me if you want new content types! I’m designed to be very extensible. See the different standard Cell subclasses in lib/prawn/table/cell/*.rb for a template.

Direct Known Subclasses

Subtable, Text

Defined Under Namespace

Modules: InTable Classes: Subtable, Text

Constant Summary collapse

FPTolerance =

A small amount added to the bounding box width to cover over floating- point errors when round-tripping from content_width to width and back. This does not change cell positioning; it only slightly expands each cell’s bounding box width so that rounding error does not prevent a cell from rendering.

1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Sets up a cell on the document pdf, at the given x/y location point, with the given options. Cell, like Table, follows the “options set accessors” paradigm (see “Options” under the Table documentation), so any cell accessor cell.foo = :bar can be set by providing the option :foo => :bar here.



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/prawn/table/cell.rb', line 145

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

  # Set defaults; these can be changed by options
  @padding       = [5, 5, 5, 5]
  @borders       = [:top, :bottom, :left, :right]
  @border_widths = [1] * 4
  @border_colors = ['000000'] * 4

  options.each { |k, v| send("#{k}=", v) }
end

Instance Attribute Details

#background_colorObject

The background color, if any, for this cell. Specified in HTML RGB format, e.g., “ccffff”. The background is drawn under the whole cell, including any padding.



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

def background_color
  @background_color
end

#border_colorsObject (readonly)

HTML RGB-format (“ccffff”) border colors: [top, right, bottom, left].



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

def border_colors
  @border_colors
end

#border_widthsObject (readonly)

Width, in PDF points, of the cell’s borders: [top, right, bottom, left].



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

def border_widths
  @border_widths
end

#bordersObject

Specifies which borders to enable. Must be an array of zero or more of: [:left, :right, :top, :bottom].



77
78
79
# File 'lib/prawn/table/cell.rb', line 77

def borders
  @borders
end

#contentObject

Specifies the content for the cell. Must be a “cellable” object. See the “Data” section of the Prawn::Table documentation for details on cellable objects.



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

def content
  @content
end

#heightObject

Returns the cell’s height in points, inclusive of padding.



209
210
211
212
213
# File 'lib/prawn/table/cell.rb', line 209

def height
  # We can't ||= here because the FP error accumulates on the round-trip
  # from #content_height.
  @height || (content_height + padding_top + padding_bottom)
end

#paddingObject

Amount of dead space (in PDF points) inside the borders but outside the content. Padding defaults to 5pt.



54
55
56
# File 'lib/prawn/table/cell.rb', line 54

def padding
  @padding
end

Class Method Details

.make(pdf, content, options = {}) ⇒ Object

Instantiates a Cell based on the given options. The particular class of cell returned depends on the :content argument. See the Prawn::Table documentation under “Data” for allowable content types.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/prawn/table/cell.rb', line 103

def self.make(pdf, content, options={})
  at = options.delete(:at) || [0, pdf.cursor]
  content = content.to_s if content.nil? || content.kind_of?(Numeric) ||
    content.kind_of?(Date)
  
  if content.is_a?(Hash)
    options.update(content)
    content = options[:content]
  else
    options[:content] = content
  end

  case content
  when Prawn::Table::Cell
    content
  when String
    Cell::Text.new(pdf, at, options)
  when Prawn::Table
    Cell::Subtable.new(pdf, at, options)
  when Array
    subtable = Prawn::Table.new(options[:content], pdf, {})
    Cell::Subtable.new(pdf, at, options.merge(:content => subtable))
  else
    # TODO: other types of content
    raise ArgumentError, "Content type not recognized: #{content.inspect}"
  end
end

Instance Method Details

#border_bottom_colorObject



379
380
381
# File 'lib/prawn/table/cell.rb', line 379

def border_bottom_color
  @border_colors[2]
end

#border_bottom_color=(val) ⇒ Object



383
384
385
# File 'lib/prawn/table/cell.rb', line 383

def border_bottom_color=(val)
  @border_colors[2] = val
end

#border_bottom_widthObject



437
438
439
# File 'lib/prawn/table/cell.rb', line 437

def border_bottom_width
  @borders.include?(:bottom) ? @border_widths[2] : 0
end

#border_bottom_width=(val) ⇒ Object



441
442
443
# File 'lib/prawn/table/cell.rb', line 441

def border_bottom_width=(val)
  @border_widths[2] = val
end

#border_color=(color) ⇒ Object Also known as: border_colors=

Sets border colors on this cell. The argument can be one of:

  • an integer (sets all colors)

  • a two-element array [vertical, horizontal]

  • a three-element array [top, horizontal, bottom]

  • a four-element array [top, right, bottom, left]



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/prawn/table/cell.rb', line 336

def border_color=(color)
  @border_colors = case
  when color.nil?
    ["000000"] * 4
  when String === color # all colors
    [color, color, color, color]
  when color.length == 2 # vert, horiz
    [color[0], color[1], color[0], color[1]]
  when color.length == 3 # top, horiz, bottom
    [color[0], color[1], color[2], color[1]]
  when color.length == 4 # top, right, bottom, left
    [color[0], color[1], color[2], color[3]]
  else
    raise ArgumentError, ":border_color must be a string " +
      "or an array [v,h] or [t,r,b,l]"
  end
end

#border_left_colorObject



387
388
389
# File 'lib/prawn/table/cell.rb', line 387

def border_left_color
  @border_colors[3]
end

#border_left_color=(val) ⇒ Object



391
392
393
# File 'lib/prawn/table/cell.rb', line 391

def border_left_color=(val)
  @border_colors[3] = val
end

#border_left_widthObject



445
446
447
# File 'lib/prawn/table/cell.rb', line 445

def border_left_width
  @borders.include?(:left) ? @border_widths[3] : 0
end

#border_left_width=(val) ⇒ Object



449
450
451
# File 'lib/prawn/table/cell.rb', line 449

def border_left_width=(val)
  @border_widths[3] = val
end

#border_right_colorObject



371
372
373
# File 'lib/prawn/table/cell.rb', line 371

def border_right_color
  @border_colors[1]
end

#border_right_color=(val) ⇒ Object



375
376
377
# File 'lib/prawn/table/cell.rb', line 375

def border_right_color=(val)
  @border_colors[1] = val
end

#border_right_widthObject



429
430
431
# File 'lib/prawn/table/cell.rb', line 429

def border_right_width
  @borders.include?(:right) ? @border_widths[1] : 0
end

#border_right_width=(val) ⇒ Object



433
434
435
# File 'lib/prawn/table/cell.rb', line 433

def border_right_width=(val)
  @border_widths[1] = val
end

#border_top_colorObject



355
356
357
# File 'lib/prawn/table/cell.rb', line 355

def border_top_color
  @border_colors[0]
end

#border_top_color=(val) ⇒ Object



359
360
361
# File 'lib/prawn/table/cell.rb', line 359

def border_top_color=(val)
  @border_colors[0] = val
end

#border_top_widthObject



421
422
423
# File 'lib/prawn/table/cell.rb', line 421

def border_top_width
  @borders.include?(:top) ? @border_widths[0] : 0
end

#border_top_width=(val) ⇒ Object



425
426
427
# File 'lib/prawn/table/cell.rb', line 425

def border_top_width=(val)
  @border_widths[0] = val
end

#border_width=(width) ⇒ Object Also known as: border_widths=

Sets border widths on this cell. The argument can be one of:

  • an integer (sets all widths)

  • a two-element array [vertical, horizontal]

  • a three-element array [top, horizontal, bottom]

  • a four-element array [top, right, bottom, left]



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/prawn/table/cell.rb', line 402

def border_width=(width)
  @border_widths = case
  when width.nil?
    ["000000"] * 4
  when Numeric === width # all widths
    [width, width, width, width]
  when width.length == 2 # vert, horiz
    [width[0], width[1], width[0], width[1]]
  when width.length == 3 # top, horiz, bottom
    [width[0], width[1], width[2], width[1]]
  when width.length == 4 # top, right, bottom, left
    [width[0], width[1], width[2], width[3]]
  else
    raise ArgumentError, ":border_width must be a string " +
      "or an array [v,h] or [t,r,b,l]"
  end
end

#content_heightObject

Returns the height of the bare content in the cell, excluding padding.



217
218
219
220
221
222
223
# File 'lib/prawn/table/cell.rb', line 217

def content_height
  if @height # manually set
    return @height - padding_top - padding_bottom
  end
  
  natural_content_height
end

#content_widthObject

Returns the width of the bare content in the cell, excluding padding.



191
192
193
194
195
196
197
# File 'lib/prawn/table/cell.rb', line 191

def content_width
  if @width # manually set
    return @width - padding_left - padding_right
  end

  natural_content_width
end

#draw(pt = [x, y]) ⇒ Object

Draws the cell onto the document. Pass in a point [x,y] to override the location at which the cell is drawn.



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/prawn/table/cell.rb', line 236

def draw(pt=[x, y])
  set_width_constraints

  draw_background(pt)
  draw_borders(pt)
  @pdf.bounding_box([pt[0] + padding_left, pt[1] - padding_top], 
                    :width  => content_width + FPTolerance,
                    :height => content_height + FPTolerance) do
    draw_content
  end
end

#max_widthObject

If provided, the maximum width that this cell can be drawn in.



65
66
67
68
# File 'lib/prawn/table/cell.rb', line 65

def max_width
  set_width_constraints
  @max_width
end

#min_widthObject

If provided, the minimum width that this cell will permit.



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

def min_width
  set_width_constraints
  @min_width
end

#natural_content_heightObject

Returns the height this cell would naturally take on, absent constraints. Must be implemented in subclasses.

Raises:

  • (NotImplementedError)


228
229
230
231
# File 'lib/prawn/table/cell.rb', line 228

def natural_content_height
  raise NotImplementedError, 
    "subclasses must implement natural_content_height"
end

#natural_content_widthObject

Returns the width this cell would naturally take on, absent other constraints. Must be implemented in subclasses.

Raises:

  • (NotImplementedError)


202
203
204
205
# File 'lib/prawn/table/cell.rb', line 202

def natural_content_width
  raise NotImplementedError, 
    "subclasses must implement natural_content_width"
end

#padding_bottomObject



313
314
315
# File 'lib/prawn/table/cell.rb', line 313

def padding_bottom
  @padding[2]
end

#padding_bottom=(val) ⇒ Object



317
318
319
# File 'lib/prawn/table/cell.rb', line 317

def padding_bottom=(val)
  @padding[2] = val
end

#padding_leftObject



321
322
323
# File 'lib/prawn/table/cell.rb', line 321

def padding_left
  @padding[3]
end

#padding_left=(val) ⇒ Object



325
326
327
# File 'lib/prawn/table/cell.rb', line 325

def padding_left=(val)
  @padding[3] = val
end

#padding_rightObject



305
306
307
# File 'lib/prawn/table/cell.rb', line 305

def padding_right
  @padding[1]
end

#padding_right=(val) ⇒ Object



309
310
311
# File 'lib/prawn/table/cell.rb', line 309

def padding_right=(val)
  @padding[1] = val
end

#padding_topObject



297
298
299
# File 'lib/prawn/table/cell.rb', line 297

def padding_top
  @padding[0]
end

#padding_top=(val) ⇒ Object



301
302
303
# File 'lib/prawn/table/cell.rb', line 301

def padding_top=(val)
  @padding[0] = val
end

#style(options = {}, &block) ⇒ Object

Supports setting multiple properties at once.

cell.style(:padding => 0, :border_width => 2)

is the same as:

cell.padding = 0
cell.border_width = 2


167
168
169
170
171
172
173
# File 'lib/prawn/table/cell.rb', line 167

def style(options={}, &block)
  options.each { |k, v| send("#{k}=", v) }

  # The block form supports running a single block for multiple cells, as
  # in Cells#style.
  block.call(self) if block
end

#widthObject

Returns the cell’s width in points, inclusive of padding.



177
178
179
180
181
# File 'lib/prawn/table/cell.rb', line 177

def width
  # We can't ||= here because the FP error accumulates on the round-trip
  # from #content_width.
  @width || (content_width + padding_left + padding_right)
end

#width=(w) ⇒ Object

Manually sets the cell’s width, inclusive of padding.



185
186
187
# File 'lib/prawn/table/cell.rb', line 185

def width=(w)
  @width = @min_width = @max_width = w
end

#xObject

x-position of the cell within the parent bounds.



250
251
252
# File 'lib/prawn/table/cell.rb', line 250

def x
  @point[0]
end

#x=(val) ⇒ Object

Set the x-position of the cell within the parent bounds.



256
257
258
# File 'lib/prawn/table/cell.rb', line 256

def x=(val)
  @point[0] = val
end

#yObject

y-position of the cell within the parent bounds.



262
263
264
# File 'lib/prawn/table/cell.rb', line 262

def y
  @point[1]
end

#y=(val) ⇒ Object

Set the y-position of the cell within the parent bounds.



268
269
270
# File 'lib/prawn/table/cell.rb', line 268

def y=(val)
  @point[1] = val
end