prawn-qrcode 0.3.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4ada5ebe6c4c0feeb4244bc47e28680ef0ed5446
4
- data.tar.gz: d48fad676f2a61198978f4798d9f5e40e4963c91
2
+ SHA256:
3
+ metadata.gz: 3587fc7dbd82b33c463d766acd6d5d66615c6fc4b8366c9bd454dd0e2bc19de3
4
+ data.tar.gz: b2ef597a09d3f33060ed1cc329f8feebf52f22d35882760acf2f9cada65cc47b
5
5
  SHA512:
6
- metadata.gz: 8e798f5d932613fc454f45718f3472442a7b6377a6b8efa16840c179bac8f855131140386e9802741b343242e409e9323979d2676cff27d2b4bfbf2f43d6969d
7
- data.tar.gz: ded7001605bf6f5a45a4c34ee908e64a7f37deed39dd1e7b8d7013ecbd78f3a96422730254bc0b8649b8d10dfe1d90175c56343219aad9f6a21a5ccf85591903
6
+ metadata.gz: e64c1e7436259d33b6dd0949693946fbc785b114169c0cb8412f180ae99fc2bb742f58db46ee7b202e4ef534c074b192d858609c0da048dbaba6a8ba5d65b637
7
+ data.tar.gz: 0147b627f63876d455cf7e683c8efee880ab018a1c28db9740df7ace419aabc56502030d58992d1fd23d72e2b44d75bb0a18997d51b038f1664a5dc2e4a95ff5
data/README.md CHANGED
@@ -5,7 +5,7 @@ A simple extension to generate and/or render QRCodes for Prawn PDFs
5
5
  ![TravisCI Build state](https://travis-ci.org/jabbrwcky/prawn-qrcode.svg?branch=master)
6
6
  [![Gem Version](https://badge.fury.io/rb/prawn-qrcode.svg)](http://badge.fury.io/rb/prawn-qrcode)
7
7
 
8
- Prawn/QRCode is a Prawn (>= 0.11.1) extension to simplify rendering of QR Codes*.
8
+ Prawn/QRCode is a Prawn (>= 1.0.0) extension to simplify rendering of QR Codes*.
9
9
 
10
10
  (*) QR Code is registered trademark of DENSO WAVE INCORPORATED.
11
11
  See http://www.denso-wave.com/qrcode/ for more information.
@@ -13,7 +13,7 @@ Prawn/QRCode is a Prawn (>= 0.11.1) extension to simplify rendering of QR Codes*
13
13
  ## Install
14
14
 
15
15
  ```bash
16
- $ gem install prawn-qrcode
16
+ gem install prawn-qrcode
17
17
  ```
18
18
 
19
19
  ## Usage
@@ -22,7 +22,7 @@ $ gem install prawn-qrcode
22
22
  require 'prawn/qrcode'
23
23
 
24
24
  qrcode_content = "http://github.com/jabbrwcky/prawn-qrcode"
25
- qrcode = RQRCode::QRCode.new(qrcode_content, :level=>:h, :size => 5)
25
+ qrcode = RQRCode::QRCode.new(qrcode_content, level: :h, size: 5)
26
26
 
27
27
  # Render a prepared QRCode at he cursor position
28
28
  # using a default module (e.g. dot) size of 1pt or 1/72 in
@@ -34,43 +34,16 @@ end
34
34
  # Render a code for raw content and a given total code size.
35
35
  # Renders a QR Code at the cursor position measuring 1 in in width/height.
36
36
  Prawn::Document::new do
37
- print_qr_code(qrcode_content, :extent=>72)
37
+ print_qr_code(qrcode_content, extent: 72)
38
38
  render_file("qr2.pdf")
39
39
  end
40
40
 
41
41
  # Render a code for raw content with a given dot size
42
42
  Prawn::Document::new do
43
43
  # Renders a QR Code at he cursor position using a dot (module) size of 2.8/72 in (roughly 1 mm).
44
- render_qr_code(qrcode_content, :dot=>2.8)
44
+ render_qr_code(qrcode, dot: 2.8)
45
45
  render_file("qr3.pdf")
46
46
  end
47
47
  ```
48
48
 
49
49
  For a full list of examples, take a look in the `examples` folder.
50
-
51
- ## Changelog
52
-
53
- ### 0.1.0
54
- Initial Release
55
-
56
- ### 0.1.1
57
- Updated prawn dependency from exact requirement of 0.11.1 to an >=0.11.1, <0.13
58
-
59
- ### 0.1.2
60
- Integrated patch to reduce rectangle count, which leads to a overall size reduction
61
- of the generated PDF (bdurette)
62
-
63
- ### 0.2.0
64
- Integrated patch from bdurette to align QR Code within its bounding box.
65
- Adds the optional parameter :align (:left, :center, :right) to both
66
- render_qr_code() and print_qr_code()
67
-
68
- ### 0.2.1
69
- Updated prawn dependnecy spec to >= 0.11.1.
70
-
71
- ### 0.2.2
72
- Fixed default stroke and explicit conversion of extents to floats.
73
-
74
- ### 0.2.2.1
75
- Stroke param broken. Replaced with simpler evaluation.
76
-
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright 2011 Jens Hausherr
1
+ # Copyright 2011 - 2109 Jens Hausherr
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -13,16 +13,23 @@
13
13
  # limitations under the License.
14
14
 
15
15
  require 'rake'
16
- require "rubygems"
17
- require "rubygems/package_task"
18
- require "bundler/gem_tasks"
16
+ require 'rubygems'
17
+ require 'rubygems/package_task'
18
+ require 'bundler/gem_tasks'
19
+ require 'rake/testtask'
19
20
 
20
- spec = Gem::Specification.load "prawn-qrcode.gemspec"
21
+ spec = Gem::Specification.load 'prawn-qrcode.gemspec'
21
22
  Gem::PackageTask.new(spec).define do |pkg|
22
- #pkg.need_zip = true
23
+ # pkg.need_zip = true
23
24
  pkg.need_tar = true
24
25
  end
25
26
 
26
- task :default => :package
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'test'
29
+ t.libs << 'lib'
30
+ t.test_files = FileList['test/test_*.rb']
31
+ end
32
+
33
+ task :default => :test
27
34
 
28
35
  task :clean => :clobber_package
@@ -1,4 +1,4 @@
1
- # Copyright 2011 Jens Hausherr
1
+ # Copyright 2011 - 2109 Jens Hausherr
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -16,21 +16,21 @@ require 'prawn'
16
16
  require 'prawn/measurement_extensions'
17
17
  require_relative '../lib/prawn/qrcode'
18
18
 
19
- qrcode = 'https://github.com/jabbrwcky/prawn-qrcode'
19
+ data = 'https://github.com/jabbrwcky/prawn-qrcode'
20
20
 
21
21
  Prawn::Document.new(page_size: 'A4') do
22
22
  text 'Sample autosized QR-Code (with stroked bounds) Size of QRCode : 1 in (72 pt)'
23
- print_qr_code(qrcode, extent: 72)
23
+ print_qr_code(data, extent: 72)
24
24
  move_down 20
25
25
 
26
26
  text 'Sample autosized QR-Code (with and without stroked bounds) Size of QRCode : 2 in (144 pt)'
27
27
  cpos = cursor
28
- print_qr_code(qrcode, extent: 144)
29
- print_qr_code(qrcode, pos: [150, cpos], extent: 144, stroke: false)
28
+ print_qr_code(data, extent: 144)
29
+ print_qr_code(data, pos: [150, cpos], extent: 144, stroke: false)
30
30
  move_down 10
31
31
 
32
32
  text 'Sample autosized QR-Code (with stroked bounds) Size of QRCode :10 cm'
33
- print_qr_code(qrcode, extent: 10.send(:cm), stroke: true, level: :q)
33
+ print_qr_code(data, extent: 10.send(:cm), stroke: true, level: :q)
34
34
  move_down 10
35
35
  text "Quite huge, isn't it?"
36
36
  render_file('autosize.pdf')
@@ -1,4 +1,4 @@
1
- # Copyright 2011 Jens Hausherr
1
+ # Copyright 2011 - 2109 Jens Hausherr
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -39,9 +39,9 @@ Prawn::Document.new(page_size: 'A4') do
39
39
  print_qr_code(qrcode, dot: 1.mm)
40
40
  print_qr_code(qrcode, pos: [150, cpos], dot: 1.mm, stroke: false)
41
41
  font 'Courier', size: 8 do
42
- text_box "require 'prawn/measurement_extensions'\n\n" +
43
- "print_qr_code(qrcode, dot: 1.mm)\n"+
44
- "print_qr_code(qrcode, pos: [150,cpos], dot: 1.mm, stroke: false)", at: [320, cpos], height: 200, width: 220
42
+ text_box "require 'prawn/measurement_extensions'\n\n" \
43
+ "print_qr_code(qrcode, dot: 1.mm)\n" \
44
+ 'print_qr_code(qrcode, pos: [150,cpos], dot: 1.mm, stroke: false)', at: [320, cpos], height: 200, width: 220
45
45
  end
46
46
 
47
47
  move_down 30
@@ -53,8 +53,8 @@ Prawn::Document.new(page_size: 'A4') do
53
53
  cpos = cursor
54
54
  print_qr_code(qrcode, dot: 1.mm, level: :q)
55
55
  font 'Courier', size: 8 do
56
- text_box "require 'prawn/measurement_extensions'\n\n" +
57
- "print_qr_code(qrcode, dot: 1.mm, level: :q)", at: [320, cpos], height: 200, width: 220
56
+ text_box "require 'prawn/measurement_extensions'\n\n" \
57
+ 'print_qr_code(qrcode, dot: 1.mm, level: :q)', at: [320, cpos], height: 200, width: 220
58
58
  end
59
59
  render_file('dotsize.pdf')
60
60
  end
@@ -1,4 +1,4 @@
1
- # Copyright 2011 Jens Hausherr
1
+ # Copyright 2011 - 2109 Jens Hausherr
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -15,17 +15,17 @@ require 'rubygems'
15
15
  require 'prawn'
16
16
  require_relative '../lib/prawn/qrcode'
17
17
 
18
- qrcode = RQRCode::QRCode.new('https://github.com/jabbrwcky/prawn-qrcode', :size=>5)
18
+ qrcode = RQRCode::QRCode.new('https://github.com/jabbrwcky/prawn-qrcode', size: 5)
19
19
 
20
- Prawn::Document::new(:page_size => "A4") do
21
- text "Prawn QR Code sample 1: Predefined QR-Code"
20
+ Prawn::Document.new(page_size: 'A4') do
21
+ text 'Prawn QR Code sample 1: Predefined QR-Code'
22
22
  move_down 10
23
- text "Sample predefined QR-Code (with stroked bounds) Size of QRCode dots: 1pt (1/72 in)"
23
+ text 'Sample predefined QR-Code (with stroked bounds) Size of QRCode dots: 1pt (1/72 in)'
24
24
  move_down 45
25
25
  render_qr_code(qrcode)
26
26
  move_down 10
27
- text "Sample predefined QR-Code (without stroked bounds) Size of QRCode dots: 1pt (1/72 in)"
27
+ text 'Sample predefined QR-Code (without stroked bounds) Size of QRCode dots: 1pt (1/72 in)'
28
28
  move_down 45
29
- render_qr_code(qrcode, :stroke=>false)
30
- render_file("prepared.pdf")
29
+ render_qr_code(qrcode, stroke: false)
30
+ render_file('prepared.pdf')
31
31
  end
@@ -0,0 +1,35 @@
1
+ # Copyright 2011 - 2019 Jens Hausherr
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ require 'rubygems'
15
+ require 'prawn'
16
+ require 'prawn/measurement_extensions'
17
+ require 'prawn/table'
18
+ require_relative '../lib/prawn/qrcode'
19
+ require_relative '../lib/prawn/qrcode/table'
20
+
21
+ # qrcode = 'https://github.com/jabbrwcky/prawn-qrcode'
22
+
23
+ Prawn::Document.new(page_size: 'A4') do
24
+ font 'Helvetica', style: :bold do
25
+ text 'QRCode in table'
26
+ end
27
+ move_down 5.mm
28
+ cpos = cursor
29
+ qr = make_qrcode_cell(content: 'https://github.com/jabbrwcky/prawn-qrcode', extent: 72)
30
+ t = make_table([%w[URL QRCODE],
31
+ ['https://github.com/jabbrwcky/prawn-qrcode', qr]])
32
+ t.draw
33
+ move_down 20
34
+ render_file('table.pdf')
35
+ end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2010-2014 Jens Hausherr
2
+ # Copyright 2010 - 2019 Jens Hausherr
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -21,107 +21,211 @@ require 'rqrcode'
21
21
  # :main: This is an extension for Prawn::Document to simplify rendering QR Codes.
22
22
  # The module registers itself as Prawn extension upon loading.
23
23
  #
24
- # *Author*:: Jens Hausherr (mailto:jabbrwcky@googlemail.com)
25
- # *Copyright*:: Copyright (c) 2011 Jens Hausherr
26
- # *License*:: Apache License, Version 2.0
24
+ # @author Jens Hausherr <jabbrwcky@gmail.com>
25
+ # @copyright Copyright (c) 2011 -2019 Jens Hausherr
26
+ # @license Apache License, Version 2.0
27
27
  #
28
28
  module Prawn
29
29
  module QRCode
30
- # The default size for QR Code modules is 1/72 in
30
+ # DEFAULT_DOTSIZE defines the default size for QR Code modules in multiples of 1/72 in
31
31
  DEFAULT_DOTSIZE = 1.to_f
32
32
 
33
- # Prints a QR Code to the PDF document. The QR Code creation happens on the fly.
33
+ # Creates a QRCode with a minimal size to fit the data with the requested error correction level.
34
+ # @since 0.5.0
34
35
  #
35
- # content:: The string to render as content of the QR Code
36
+ # @param [string] content The string to render as content of the QR Code
37
+ # @param [Integer] qr_version Optional number of modules to use initially. Will use more if input overflows module size (Default: 0)
38
+ # @param [symbol] level Optional Error correction level to use. One of: (:l, :m, :h, :q), Defaults to :m
39
+ # @param [symbol] mode Optional mode. One of (:number, :alphanumeric, :byte_8bit, :kanji), Defaults to :alphanumeric or :byte_8bit
36
40
  #
37
- # *options:: Named optional parameters
41
+ # @return [RQRCode::QRCode] QR code that can hold the specified data with the desired error correction level
38
42
  #
39
- # +:level+:: Error correction level to use. One of: (:l,:m,:h,:q), Defaults to :m
40
- # +:extent+:: Size of QR Code given in pt (1 pt == 1/72 in)
41
- # +:pos+:: Two-element array containing the position at which the QR-Code should be rendered. Defaults to [0,cursor]
42
- # +:dot+:: Size of QR Code module/dot. Calculated from extent or defaulting to 1pt
43
- # +:stroke+:: boolean value whether to draw bounds around the QR Code.
44
- # Defaults to true.
45
- # +:align+:: Optional alignment within the current bounding box. Valid values are :left, :right, and :center. If set
46
- # This option overrides the horizontal positioning specified in :pos. Defaults to nil.
47
- # +:debug+:: Optional boolean, renders a coordinate grid around the QRCode if true (uses Prawn#stroke_axis)
43
+ # @raise [RQRCodeCore::QRCodeRunTimeError] if the data specified will not fit in the largest QR code (QR version 40) with the given error correction level
48
44
  #
49
- def print_qr_code(content, level: :m, dot: DEFAULT_DOTSIZE, pos: [0,cursor], stroke: true, **options)
50
- qr_version = 0
51
- dot_size = dot
52
-
53
- begin
54
- qr_version += 1
55
- qr_code = RQRCode::QRCode.new(content, size: qr_version, level: level)
56
- dot = options[:extent] / (8 + qr_code.modules.length) if options[:extent]
57
-
58
- render_qr_code(qr_code, dot: dot, pos: pos, stroke: stroke, **options)
59
- rescue RQRCode::QRCodeRunTimeError
60
- if qr_version < 40
61
- retry
62
- else
63
- raise
64
- end
65
- end
45
+ def self.min_qrcode(content, qr_version = 0, level: :m, mode: nil, **)
46
+ qr_version += 1
47
+ RQRCode::QRCode.new(content, size: qr_version, level: level, mode: mode)
48
+ rescue RQRCodeCore::QRCodeRunTimeError
49
+ retry if qr_version < 40
50
+ raise
51
+ end
52
+
53
+ # dotsize calculates the required dotsize for a QR code to be rendered with the given extent and the module size
54
+ # @since 0.5.0
55
+ #
56
+ # @param [RQRCode::QRCode] qr_code QR code to render
57
+ # @param [Integer/Float] extent Size of QR code given in pt (1 pt == 1/72 in)
58
+ # @param [Integer] margin Width of margin as number of modules (defaults to 4 modules)
59
+ #
60
+ # @return [Float] size of dot in pt (1/72 in)
61
+ def self.dotsize(qr_code, extent, margin = 4)
62
+ extent.to_f / (2 * margin + qr_code.modules.length).to_f
63
+ end
64
+
65
+ # Prints a QR Code to the PDF document. The QR Code creation happens on the fly.
66
+ #
67
+ # @param [string] content The string to render as content of the QR Code
68
+ # @param [symbol] level Error correction level to use. One of: (:l, :m, :h, :q), Defaults to :m
69
+ # @param [symbol] mode Optional mode. One of (:number, :alphanumeric, :byte_8bit, :kanji), Defaults to :alphanumeric or :byte_8bit
70
+ # @param [Array] pos Two-element array containing the position at which the QR-Code should be rendered. Defaults to [0,cursor]
71
+ # @param [Hash] options additional options that are passed on to Prawn::QRCode::Renderer
72
+ #
73
+ # @see Renderer
74
+ #
75
+ def print_qr_code(content, level: :m, mode: nil, pos: [0, cursor], **options)
76
+ qr_code = Prawn::QRCode.min_qrcode(content, level: level, mode: mode)
77
+ render_qr_code(qr_code, pos: pos, **options)
66
78
  end
67
79
 
68
- # Renders a prepared QR Code (RQRCode::QRCode) object.
80
+ # Renders a prepared QR code (RQRCode::QRCode) int the pdf.
81
+ # @since 0.5.0
69
82
  #
70
- # qr_code:: The QR Code (an RQRCode::QRCode) to render
83
+ # @param [RQRCode::QRCode] qr_code The QR code (an RQRCode::QRCode) to render
84
+ # @param [Hash] options additional options that are passed on to Prawn::QRCode::Renderer
71
85
  #
72
- # *options:: Named optional parameters
73
- # +:extent+:: Size of QR Code given in pt (1 pt == 1/72 in)
74
- # +:pos+:: Two-element array containing the position at which the QR-Code should be rendered. Defaults to [0,cursor]
75
- # +:dot+:: Size of QR Code module/dot. Calculated from extent or defaulting to 1pt
76
- # +:stroke+:: boolean value whether to draw bounds around the QR Code. Defaults to true.
77
- # +:align+:: Optional alignment within the current bounding box. Valid values are :left, :right, and :center. If set
78
- # This option overrides the horizontal positioning specified in :pos. Defaults to nil.
79
- # +:debug+:: Optional boolean, renders a coordinate grid around the QRCode if true (uses Prawn#stroke_axis)
86
+ # @see Renderer
80
87
  #
81
- def render_qr_code(qr_code, dot: DEFAULT_DOTSIZE, pos: [0,cursor], stroke: true, foreground_color: '000000', background_color: 'FFFFFF', stroke_color: '000000', **options)
82
- extent = extent || (8 + qr_code.modules.length) * dot
83
-
84
- case options[:align]
85
- when :center
86
- pos[0] = (@bounding_box.right / 2) - (extent / 2)
87
- when :right
88
- pos[0] = @bounding_box.right - extent
89
- when :left
90
- pos[0] = 0
88
+ def render_qr_code(qr_code, **options)
89
+ renderer = Renderer.new(qr_code, **options)
90
+ renderer.render(self)
91
+ end
92
+
93
+ # QRCodeError is raised on errors specific to Prawn::QRCode
94
+ # @since 0.5.0
95
+ class QRCodeError < StandardError; end
96
+
97
+ # Renderer is responsible for actually rendering a QR code to pdf
98
+ # @since 0.5.0
99
+ class Renderer
100
+ attr_accessor :qr_code
101
+
102
+ RENDER_OPTS = %I[dot pos stroke foreground_color background_color stroke_color margin align debug extent].freeze
103
+ RENDER_OPTS.each { |attr| attr_writer attr }
104
+
105
+ # creates a new renderer for the given QR code
106
+ #
107
+ # @param qr_code [RQRCode::QRCode] QR code to render
108
+ # @param [Hash] options additional options
109
+ # @option options [Float] :dot size of a dot in pt (1/72 in)
110
+ # @option options [Array] :pos Two-element array containing the position at which the QR-Code should be rendered. Defaults to [0,cursor]
111
+ # @option options [bool] :stroke whether to draw bounds around the QR Code. Defaults to true.
112
+ # @option options [string] :foreground_color 6-digit hex string specifying foreground color; default: '000000'
113
+ # @option options [string] :background_color 6-digit hex string specifying background color; default: 'FFFFFF'
114
+ # @option options [string] :stroke_color 6-digit hex string specifying stroke color; default: '000000'
115
+ # @option options [integer] :margin number of modules as margin around QRcode (default: 4)
116
+ # @option options [float] :extent overall width/height of QR code in pt (1/72 in)
117
+ # @option options [bool] :debug render a coordinate grid around the QRCode if true (uses Prawn#stroke_axis)
118
+ # @option options [symbol] :align alignment within the current bounding box. Valid values are :left, :right, and :center. If set
119
+ # this option overrides the horizontal positioning specified in :pos. Defaults to nil.
120
+ #
121
+ # Options :dot and :extent are mutually exclusive.
122
+ #
123
+ # @raise [QRCodeError] if both extent and dot are specified.
124
+ def initialize(qr_code, **options)
125
+ raise QRCodeError, 'Specify either :dot or :extent, not both' if options.key?(:dot) && options.key?(:extent)
126
+
127
+ @stroke = true
128
+ @qr_code = qr_code
129
+ options.select { |k, _v| RENDER_OPTS.include?(k) }.each { |k, v| send("#{k}=", v) }
91
130
  end
92
131
 
93
- fill_color background_color
94
-
95
- bounding_box(pos, width: extent, height: extent) do |_box|
96
- fill_color foreground_color
97
- pos_y = 4 * dot + qr_code.modules.length * dot
98
-
99
- qr_code.modules.each_index do |row|
100
- pos_x = 4 * dot
101
- dark_col = 0
102
- qr_code.modules.each_index do |col|
103
- move_to [pos_x, pos_y]
104
- if qr_code.dark?(row, col)
105
- dark_col += 1
106
- else
107
- if dark_col > 0
108
- fill { rectangle([pos_x - dark_col * dot, pos_y], dot * dark_col, dot) }
109
- dark_col = 0
132
+ def dot
133
+ @dot ||= Prawn::QRCode.dotsize(qr_code, @extent, margin) if defined?(@extent)
134
+ @dot ||= DEFAULT_DOTSIZE unless defined?(@extent)
135
+ @dot
136
+ end
137
+
138
+ attr_reader :stroke
139
+
140
+ def foreground_color
141
+ @foreground_color ||= '000000'
142
+ end
143
+
144
+ def background_color
145
+ @background_color ||= 'FFFFFF'
146
+ end
147
+
148
+ def stroke_color
149
+ @stroke_color ||= '000000'
150
+ end
151
+
152
+ def margin
153
+ @margin ||= 4
154
+ end
155
+
156
+ def extent
157
+ @extent ||= (2 * margin + qr_code.modules.length) * dot
158
+ @extent
159
+ end
160
+
161
+ def margin_size
162
+ margin * dot
163
+ end
164
+
165
+ def align(bounding_box)
166
+ rlim = bounding_box.right
167
+ case @align
168
+ when :center
169
+ @pos[0] = (rlim / 2) - (extent / 2)
170
+ when :right
171
+ @pos[0] = rlim - extent
172
+ when :left
173
+ @pos[0] = 0
174
+ end
175
+ end
176
+
177
+ # rubocop:disable Metrics/AbcSize
178
+ def render(pdf)
179
+ pdf.fill_color background_color
180
+
181
+ pos(pdf) # make sure the @pos attribute is set before calling align
182
+ align(pdf.bounds)
183
+
184
+ pdf.bounding_box(pos(pdf), width: extent, height: extent) do |_box|
185
+ pdf.fill_color foreground_color
186
+ margin_dist = margin * dot
187
+
188
+ m = qr_code.modules
189
+
190
+ pos_y = margin_dist + m.length * dot
191
+
192
+ m.each_with_index do |row, index|
193
+ pos_x = margin_dist
194
+ dark_col = 0
195
+
196
+ row.each_index do |col|
197
+ pdf.move_to [pos_x, pos_y]
198
+ if qr_code.qrcode.checked?(index, col)
199
+ dark_col += 1
200
+ else
201
+ if dark_col > 0
202
+ dark_col_extent = dark_col * dot
203
+ pdf.fill { pdf.rectangle([pos_x - dark_col_extent, pos_y], dark_col_extent, dot) }
204
+ dark_col = 0
205
+ end
110
206
  end
207
+ pos_x += dot
111
208
  end
112
- pos_x += dot
209
+
210
+ pdf.fill { pdf.rectangle([pos_x - dark_col * dot, pos_y], dot * dark_col, dot) } if dark_col > 0
211
+
212
+ pos_y -= dot
113
213
  end
114
- if dark_col > 0
115
- fill { rectangle([pos_x - dark_col * dot, pos_y], dot * dark_col, dot) }
214
+
215
+ if stroke
216
+ pdf.fill_color stroke_color
217
+ pdf.stroke_bounds
116
218
  end
117
- pos_y -= dot
219
+ pdf.stroke_axis(at: [-1, -1], negative_axes_length: 0, color: '0C0C0C', step_length: 50) if debug
118
220
  end
221
+ end
119
222
 
120
- if stroke
121
- fill_color stroke_color
122
- stroke_bounds
123
- end
124
- stroke_axis(at: [-1,-1], negative_axes_length: 0, color: '0C0C0C', step_length: 50) if options[:debug]
223
+ private
224
+
225
+ attr_reader :debug
226
+
227
+ def pos(pdf)
228
+ @pos ||= [0, pdf.cursor]
125
229
  end
126
230
  end
127
231
  end