Class: HexaPDF::Extras::GraphicObject::QRCode

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/extras/graphic_object/qr_code.rb

Overview

Generates a QR code and renders it using simple PDF canvas graphics.

It implements the HexaPDF graphic object interface and can therefore easily be used via the :qrcode name:

#>pdf-canvas100
canvas.draw(:qrcode, data: 'hello', size: 100)

This class relies on rqrcode_core to generate the QR code from the given data. All options of rqrcode_core are supported, have a look at their documentation to see the allowed values.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQRCode

Creates a QRCode object.



127
128
129
130
131
132
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 127

def initialize
  @data = @size = @code_size = @max_code_size = @level = @mode = nil
  @at = [0, 0]
  @dark_color = 'black'
  @light_color = nil
end

Instance Attribute Details

#atObject

The position of the bottom-left corner of the QR code.

Default: [0, 0].

Examples:

#>pdf-canvas100
canvas.draw(:qrcode, data: 'test', size: 30)
canvas.draw(:qrcode, data: 'test', size: 20, at: [50, 50])


39
40
41
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 39

def at
  @at
end

#code_sizeObject

The code size of the the QR code (normally called ‘version’).

This is directly passed to rqrcode_core as the size argument.

Default: nil (i.e. let rqrcode_core decide)

Examples:

#>pdf-canvas100
canvas.draw(:qrcode, data: 'test', size: 100, code_size: 10)


88
89
90
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 88

def code_size
  @code_size
end

#dark_colorObject

The color for the dark QR code modules (‘pixels’)

Default: ‘black’.

Examples:

#>pdf-canvas100
canvas.draw(:qrcode, data: 'test', size: 100, dark_color: 'green')


59
60
61
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 59

def dark_color
  @dark_color
end

#dataObject

The data for which the QR code should be generated.

This is directly passed to rqrcode_core as the data argument.

Default: none



76
77
78
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 76

def data
  @data
end

#levelObject

The error correction level of the QR code.

This is directly passed to rqrcode_core as the level argument.

Default: nil (i.e. let rqrcode_core decide)

Examples:

#>pdf-canvas100
canvas.draw(:qrcode, data: 'test', size: 100, level: :l)


112
113
114
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 112

def level
  @level
end

#light_colorObject

The color for the light QR code modules (‘pixels’).

Default: none (i.e. they are not drawn).

Examples:

#>pdf-canvas100
canvas.draw(:qrcode, data: 'test', size: 100, light_color: 'yellow')


69
70
71
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 69

def light_color
  @light_color
end

#max_code_sizeObject

The maximum code size of the QR code.

This is directly passed to rqrcode_core as the max_size argument.

Default: nil (i.e. let rqrcode_core decide)

Examples:

#>pdf-canvas100
canvas.draw(:qrcode, data: 't'*100, size: 100, max_code_size: 10)


100
101
102
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 100

def max_code_size
  @max_code_size
end

#modeObject

The mode of the QR code, i.e. which data it holds.

This is directly passed to rqrcode_core as the mode argument.

Default: nil (i.e. let rqrcode_core decide)

Examples:

#>pdf-canvas100
canvas.draw(:qrcode, data: 'test', size: 100, mode: :kanji)


124
125
126
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 124

def mode
  @mode
end

#sizeObject

The size of the whole rendered QR code.

Default: none

Examples:

#>pdf-canvas100
canvas.draw(:qrcode, data: 'test', size: 80, at: [10, 10])


49
50
51
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 49

def size
  @size
end

Class Method Details

.configure(**kwargs) ⇒ Object

Creates and configures a new QRCode drawing support object.

See #configure for the allowed keyword arguments.



26
27
28
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 26

def self.configure(**kwargs)
  new.configure(**kwargs)
end

Instance Method Details

#configure(at: nil, size: nil, dark_color: nil, light_color: nil, data: nil, code_size: nil, max_code_size: nil, level: nil, mode: nil) ⇒ Object

Configures the QRCode object and returns self.

The following arguments are allowed:

:at

The position of the bottom-left corner.

:size

The size of the whole rendered QR code.

:dark_color

The color used for the dark QR code modules (‘pixels’).

:light_color

The color used for the light QR code modules (‘pixels’).

:data

The data for the QR code.

:code_size

The code size of the QR code.

:max_code_size

The maximum code size of the QR code.

:level

The error correction level of the QR code

:mode

The mode of the of the QR code.

Any arguments not specified are not modified and retain their old value, see the attribute methods for the inital default values.



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 150

def configure(at: nil, size: nil, dark_color: nil, light_color: nil,
              data: nil, code_size: nil, max_code_size: nil, level: nil, mode: nil)
  @at = at if at
  @size = size if size
  @dark_color = dark_color if dark_color
  @light_color = light_color if light_color
  @data = data if data
  @code_size = code_size if code_size
  @max_code_size = max_code_size if max_code_size
  @level = level if level
  @mode = mode if mode
  self
end

#draw(canvas) ⇒ Object

Draws the QRCode object onto the given Canvas, with the bottom-left corner at the position specified by #at and the size specified by #size.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/hexapdf/extras/graphic_object/qr_code.rb', line 166

def draw(canvas)
  qrcode = RQRCodeCore::QRCode.new(data, size: code_size, max_size: max_code_size,
                                   level: level, mode: mode)

  canvas.save_graphics_state do
    canvas.translate(*at)
    canvas.fill_color(light_color).rectangle(0, 0, size, size).fill if light_color

    module_count = qrcode.modules.size
    module_size = size.to_f / module_count
    y_start = size - module_size / 2
    canvas.line_cap_style(:butt)
    canvas.line_width(module_size)
    canvas.stroke_color(dark_color)

    qrcode.modules.each_with_index do |row, row_index|
      pattern = [0]
      last_is_dark = row[0]
      row.each do |col|
        if col == last_is_dark
          pattern[-1] += module_size
        else
          last_is_dark = !last_is_dark
          pattern << module_size
        end
      end
      pattern.unshift(0) unless row[0]

      canvas.line_dash_pattern(pattern)
      canvas.line(0, y_start - module_size * row_index,
                  size, y_start - module_size * row_index).
        stroke
    end
  end
end