class Prawn::QRCode::Renderer

Renderer is responsible for actually rendering a QR code to pdf @since 0.5.0

Constants

RENDER_OPTS

Attributes

debug[R]
qr_code[RW]
stroke[R]

Public Class Methods

new(qr_code, **options) click to toggle source

creates a new renderer for the given QR code

@param qr_code [RQRCode::QRCode] QR code to render @param [Hash] options additional options @option options [Float] :dot size of a dot in pt (1/72 in) @option options [Array] :pos Two-element array containing the position at which the QR-Code should be rendered. Defaults to [0,cursor] @option options [bool] :stroke whether to draw bounds around the QR Code. Defaults to true. @option options [string] :foreground_color 6-digit hex string specifying foreground color; default: '000000' @option options [string] :background_color 6-digit hex string specifying background color; default: 'FFFFFF' @option options [string] :stroke_color 6-digit hex string specifying stroke color; default: '000000' @option options [integer] :margin number of modules as margin around QRcode (default: 4) @option options [float] :extent overall width/height of QR code in pt (1/72 in) @option options [bool] :debug render a coordinate grid around the QRCode if true (uses Prawn#stroke_axis) @option options [symbol] :align alignment within the current bounding box. Valid values are :left, :right, and :center. If set

this option overrides the horizontal positioning specified in :pos. Defaults to nil.

Options :dot and :extent are mutually exclusive.

@raise [QRCodeError] if both extent and dot are specified.

# File lib/prawn/qrcode.rb, line 124
def initialize(qr_code, **options)
  raise QRCodeError, 'Specify either :dot or :extent, not both' if options.key?(:dot) && options.key?(:extent)

  @stroke = true
  @qr_code = qr_code
  options.select { |k, _v| RENDER_OPTS.include?(k) }.each { |k, v| send("#{k}=", v) }
end

Public Instance Methods

align(bounding_box) click to toggle source
# File lib/prawn/qrcode.rb, line 165
def align(bounding_box)
  rlim = bounding_box.right
  case @align
  when :center
    @pos[0] = (rlim / 2) - (extent / 2)
  when :right
    @pos[0] = rlim - extent
  when :left
    @pos[0] = 0
  end
end
background_color() click to toggle source
# File lib/prawn/qrcode.rb, line 144
def background_color
  @background_color ||= 'FFFFFF'
end
dot() click to toggle source
# File lib/prawn/qrcode.rb, line 132
def dot
  @dot ||= Prawn::QRCode.dotsize(qr_code, @extent, margin) if defined?(@extent)
  @dot ||= DEFAULT_DOTSIZE unless defined?(@extent)
  @dot
end
extent() click to toggle source
# File lib/prawn/qrcode.rb, line 156
def extent
  @extent ||= (2 * margin + qr_code.modules.length) * dot
  @extent
end
foreground_color() click to toggle source
# File lib/prawn/qrcode.rb, line 140
def foreground_color
  @foreground_color ||= '000000'
end
margin() click to toggle source
# File lib/prawn/qrcode.rb, line 152
def margin
  @margin ||= 4
end
margin_size() click to toggle source
# File lib/prawn/qrcode.rb, line 161
def margin_size
  margin * dot
end
render(pdf) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/prawn/qrcode.rb, line 178
def render(pdf)
  pdf.fill_color background_color

  pos(pdf) # make sure the @pos attribute is set before calling align
  align(pdf.bounds)
  
  pdf.bounding_box(pos(pdf), width: extent, height: extent) do |_box|
    pdf.fill_color foreground_color
    margin_dist = margin * dot

    m = qr_code.modules

    pos_y = margin_dist + m.length * dot

    m.each_with_index do |row, index|
      pos_x = margin_dist
      dark_col = 0

      row.each_index do |col|
        pdf.move_to [pos_x, pos_y]
        if qr_code.qrcode.checked?(index, col)
          dark_col += 1
        else
          if dark_col > 0
            dark_col_extent = dark_col * dot
            pdf.fill { pdf.rectangle([pos_x - dark_col_extent, pos_y], dark_col_extent, dot) }
            dark_col = 0
          end
        end
        pos_x += dot
      end

      pdf.fill { pdf.rectangle([pos_x - dark_col * dot, pos_y], dot * dark_col, dot) } if dark_col > 0

      pos_y -= dot
    end

    if stroke
      pdf.fill_color stroke_color
      pdf.stroke_bounds
    end
    pdf.stroke_axis(at: [-1, -1], negative_axes_length: 0, color: '0C0C0C', step_length: 50) if debug
  end
end
stroke_color() click to toggle source
# File lib/prawn/qrcode.rb, line 148
def stroke_color
  @stroke_color ||= '000000'
end

Private Instance Methods

pos(pdf) click to toggle source
# File lib/prawn/qrcode.rb, line 227
def pos(pdf)
  @pos ||= [0, pdf.cursor]
end