Method: Prawn::Document#checkbox

Defined in:
lib/barkest_core/extensions/prawn_document_extensions.rb

#checkbox(x, y, options = { }) ⇒ Object

Creates a small box that can be used as a checkbox.

The x and y parameters are absolute positions for the checkbox.

Valid options:

checked

If set, the box will be marked with an X.

stroke_width

The line width to draw the checkbox with. Defaults to 0.5.

size

The size of the checkbox. Defaults to 8.0.

rounded

Set to true to have rounded corners. Defaults to false.

color

The color to draw the checkbox in. Defaults to ‘000000’ (black).

label

An optional label to follow the checkbox.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/barkest_core/extensions/prawn_document_extensions.rb', line 217

def checkbox(x, y, options = { })
  options = { stroke_width: 0.5, size: 8, rounded: false, color: '000000' }.merge(options || {})
  stored_line_width = line_width
  stored_color = stroke_color
  self.line_width = options[:stroke_width]
  self.stroke_color = options[:color]
  if options[:rounded]
    stroke_rounded_rectangle [x, y], options[:size], options[:size], options[:size] * 0.2
  else
    stroke_rectangle [x, y], options[:size], options[:size]
  end
  if options[:checked]
    self.stroke_color = '000000'
    self.line_width = options[:size] * 0.125
    offset = options[:size] * 0.1
    stroke_line [x + offset, y - offset], [x + options[:size] - offset, y - options[:size] + offset]
    stroke_line [x + offset, y - options[:size] + offset], [x + options[:size] - offset, y - offset]
  end
  self.line_width = stored_line_width
  self.stroke_color = stored_color
  unless options[:label].blank?
    text_box options[:label], at: [x + options[:size] + 2.pt, y]
  end
end