Class: Prawn::Document::GridBox

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

Overview

A Box is a class that represents a bounded area of a page. A Grid object has methods that allow easy access to the coordinates of its corners, which can be plugged into most existing prawnmethods.

Direct Known Subclasses

MultiBox

Experimental API collapse

Experimental API collapse

Constructor Details

#initialize(pdf, rows, columns) ⇒ GridBox

Returns a new instance of GridBox.



115
116
117
118
119
# File 'lib/prawn/grid.rb', line 115

def initialize(pdf, rows, columns)
  @pdf = pdf
  @rows = rows
  @columns = columns
end

Instance Attribute Details

#pdfObject (readonly)

Returns the value of attribute pdf.



113
114
115
# File 'lib/prawn/grid.rb', line 113

def pdf
  @pdf
end

Instance Method Details

#bottomObject

y-coordinate of the bottom



164
165
166
# File 'lib/prawn/grid.rb', line 164

def bottom
  @bottom ||= top - height
end

#bottom_leftObject

x,y coordinates of bottom left corner



179
180
181
# File 'lib/prawn/grid.rb', line 179

def bottom_left
  [left, bottom]
end

#bottom_rightObject

x,y coordinates of bottom right corner



184
185
186
# File 'lib/prawn/grid.rb', line 184

def bottom_right
  [right, bottom]
end

#bounding_box(&blk) ⇒ Object

Creates a standard bounding box based on the grid box.



189
190
191
# File 'lib/prawn/grid.rb', line 189

def bounding_box(&blk)
  pdf.bounding_box(top_left, width: width, height: height, &blk)
end

#gutterObject

Width of the gutter



144
145
146
# File 'lib/prawn/grid.rb', line 144

def gutter
  grid.gutter.to_f
end

#heightObject

Height of a box



139
140
141
# File 'lib/prawn/grid.rb', line 139

def height
  grid.row_height.to_f
end

#leftObject

x-coordinate of left side



149
150
151
# File 'lib/prawn/grid.rb', line 149

def left
  @left ||= (width + grid.column_gutter) * @columns.to_f
end

#nameObject

Mostly diagnostic method that outputs the name of a box as col_num, row_num



124
125
126
# File 'lib/prawn/grid.rb', line 124

def name
  "#{@rows},#{@columns}"
end

#rightObject

x-coordinate of right side



154
155
156
# File 'lib/prawn/grid.rb', line 154

def right
  @right ||= left + width
end

#show(grid_color = 'CCCCCC') ⇒ Object

Diagnostic method



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/prawn/grid.rb', line 194

def show(grid_color = 'CCCCCC')
  bounding_box do
    original_stroke_color = pdf.stroke_color

    pdf.stroke_color = grid_color
    pdf.text name
    pdf.stroke_bounds

    pdf.stroke_color = original_stroke_color
  end
end

#topObject

y-coordinate of the top



159
160
161
# File 'lib/prawn/grid.rb', line 159

def top
  @top ||= total_height - ((height + grid.row_gutter) * @rows.to_f)
end

#top_leftObject

x,y coordinates of top left corner



169
170
171
# File 'lib/prawn/grid.rb', line 169

def top_left
  [left, top]
end

#top_rightObject

x,y coordinates of top right corner



174
175
176
# File 'lib/prawn/grid.rb', line 174

def top_right
  [right, top]
end

#total_heightObject

:nodoc



129
130
131
# File 'lib/prawn/grid.rb', line 129

def total_height
  pdf.bounds.height.to_f
end

#widthObject

Width of a box



134
135
136
# File 'lib/prawn/grid.rb', line 134

def width
  grid.column_width.to_f
end