Class: Prawn::QRCode::Renderer

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

Overview

Renderer is responsible for actually rendering a QR code to pdf

Since:

  • 0.5.0

Constant Summary collapse

RENDER_OPTS =

Since:

  • 0.5.0

%I[dot pos stroke foreground_color background_color stroke_color margin align debug extent].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qr_code, **options) ⇒ Renderer

creates a new renderer for the given QR code

Options :dot and :extent are mutually exclusive.

Parameters:

  • qr_code (RQRCode::QRCode)

    QR code to render

  • options (Hash)

    additional options

Options Hash (**options):

  • :dot (Float)

    size of a dot in pt (1/72 in)

  • :pos (Array)

    Two-element array containing the position at which the QR-Code should be rendered. Defaults to [0,cursor]

  • :stroke (bool)

    whether to draw bounds around the QR Code. Defaults to true.

  • :foreground_color (string)

    6-digit hex string specifying foreground color; default: ‘000000’

  • :background_color (string)

    6-digit hex string specifying background color; default: ‘FFFFFF’

  • :stroke_color (string)

    6-digit hex string specifying stroke color; default: ‘000000’

  • :margin (integer)

    number of modules as margin around QRcode (default: 4)

  • :extent (float)

    overall width/height of QR code in pt (1/72 in)

  • :debug (bool)

    render a coordinate grid around the QRCode if true (uses Prawn#stroke_axis)

  • :align (symbol)

    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.

Raises:

  • (QRCodeError)

    if both extent and dot are specified.

Since:

  • 0.5.0



124
125
126
127
128
129
130
# 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

Instance Attribute Details

#qr_codeObject

Since:

  • 0.5.0



100
101
102
# File 'lib/prawn/qrcode.rb', line 100

def qr_code
  @qr_code
end

#strokeObject (readonly)

Since:

  • 0.5.0



138
139
140
# File 'lib/prawn/qrcode.rb', line 138

def stroke
  @stroke
end

Instance Method Details

#align(bounding_box) ⇒ Object

Since:

  • 0.5.0



165
166
167
168
169
170
171
172
173
174
175
# 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_colorObject

Since:

  • 0.5.0



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

def background_color
  @background_color ||= 'FFFFFF'
end

#dotObject

Since:

  • 0.5.0



132
133
134
135
136
# 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

#extentObject

Since:

  • 0.5.0



156
157
158
159
# File 'lib/prawn/qrcode.rb', line 156

def extent
  @extent ||= (2 * margin + qr_code.modules.length) * dot
  @extent
end

#foreground_colorObject

Since:

  • 0.5.0



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

def foreground_color
  @foreground_color ||= '000000'
end

#marginObject

Since:

  • 0.5.0



152
153
154
# File 'lib/prawn/qrcode.rb', line 152

def margin
  @margin ||= 4
end

#margin_sizeObject

Since:

  • 0.5.0



161
162
163
# File 'lib/prawn/qrcode.rb', line 161

def margin_size
  margin * dot
end

#render(pdf) ⇒ Object

rubocop:disable Metrics/AbcSize

Since:

  • 0.5.0



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# 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_colorObject

Since:

  • 0.5.0



148
149
150
# File 'lib/prawn/qrcode.rb', line 148

def stroke_color
  @stroke_color ||= '000000'
end