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.

Experimental API collapse

Experimental API collapse

Constructor Details

#initialize(pdf, rows, columns) ⇒ GridBox

Returns a new instance of GridBox.


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

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

Instance Attribute Details

#pdfObject (readonly)

Returns the value of attribute pdf


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

def pdf
  @pdf
end

Instance Method Details

#bottomObject

y-coordinate of the bottom


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

def bottom
  @bottom ||= top - height
end

#bottom_leftObject

x,y coordinates of bottom left corner


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

def bottom_left
  [left, bottom]
end

#bottom_rightObject

x,y coordinates of bottom right corner


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

def bottom_right
  [right, bottom]
end

#bounding_box(&blk) ⇒ Object

Creates a standard bounding box based on the grid box.


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

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

#gutterObject

Width of the gutter


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

def gutter
  grid.gutter.to_f
end

#heightObject

Height of a box


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

def height
  grid.row_height.to_f
end

#leftObject

x-coordinate of left side


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

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


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

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

#rightObject

x-coordinate of right side


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

def right
  @right ||= left + width
end

#show(grid_color = 'CCCCCC') ⇒ Object

Diagnostic method


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

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


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

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

#top_leftObject

x,y coordinates of top left corner


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

def top_left
  [left, top]
end

#top_rightObject

x,y coordinates of top right corner


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

def top_right
  [right, top]
end

#total_heightObject

:nodoc


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

def total_height
  pdf.bounds.height.to_f
end

#widthObject

Width of a box


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

def width
  grid.column_width.to_f
end