prawn-qrcode 0.4.0 → 0.5.0

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
2
  SHA256:
3
- metadata.gz: 669642c6d67bd24eb6d51a546511924ad8e23005ce126a509835e6dcc9799108
4
- data.tar.gz: dbd5b4d89edcadbe82fb94fc2c1fa3e964f01e067a473132c7f7c91d6b8179c1
3
+ metadata.gz: f7bf1bcff14157602b378498911ee03a4677120443b7fe10e7941fdebe88719c
4
+ data.tar.gz: 88a8704a23314b70e6582ebd0bbe261072a2decea03a97c2392db67b18f68ab1
5
5
  SHA512:
6
- metadata.gz: dcf9f09766c9a1d72f7c064b0f6d0e30154e3f362dd4050972ab230979e4703e5da1d8fb53e8614caa18fe5f215b2e5908ff0c368621bdbd1de669463f9a83a7
7
- data.tar.gz: 2bf546a8b09abd35384ec753d84abae8b2b9efb245529e119d94d3bc1556cdc925847e7679f7983dac633e764aabda94c1eee2a3f2c48732a1297ce25d097a5b
6
+ metadata.gz: '0598b21b285c7d77d553f4b5ecf1960b08b2e61476f68ad162a6d34f5f35d4fe33f307af9af97e9e89051f7ed021a47f520644f6d976c989cfdb764daa83d7a7'
7
+ data.tar.gz: ed8cd446792d019665fc1f8bbf2812cc633d5100d333ae49cfc85824d4974f4afb4b27d4245d9ee63095d221fa1f18640c830ca2fdd66c746d128e4b6aabadce
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,14 +34,14 @@ 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, :dot=>2.8)
44
+ render_qr_code(qrcode, dot: 2.8)
45
45
  render_file("qr3.pdf")
46
46
  end
47
47
  ```
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
data/lib/prawn/qrcode.rb CHANGED
@@ -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,109 +21,208 @@ 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
- # +:margin+:: Size of margin around code in QR-Code modules/dots, Default to 4
46
- # +:align+:: Optional alignment within the current bounding box. Valid values are :left, :right, and :center. If set
47
- # This option overrides the horizontal positioning specified in :pos. Defaults to nil.
48
- # +: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
49
44
  #
50
- def print_qr_code(content, level: :m, dot: DEFAULT_DOTSIZE, pos: [0,cursor], stroke: true, margin: 4, **options)
51
- qr_version = 0
52
- dot_size = dot
53
-
54
- begin
55
- qr_version += 1
56
- qr_code = RQRCode::QRCode.new(content, size: qr_version, level: level)
57
- dot = options[:extent] / (2*margin + qr_code.modules.length) if options[:extent]
58
-
59
- render_qr_code(qr_code, dot: dot, pos: pos, stroke: stroke, margin: margin, **options)
60
- rescue RQRCodeCore::QRCodeRunTimeError
61
- if qr_version < 40
62
- retry
63
- else
64
- raise
65
- end
66
- 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, stroke: stroke, margin: margin, **options)
67
78
  end
68
79
 
69
- # Renders a prepared QR Code (RQRCode::QRCode) object.
80
+ # Renders a prepared QR code (RQRCode::QRCode) int the pdf.
81
+ # @since 0.5.0
70
82
  #
71
- # 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
72
85
  #
73
- # *options:: Named optional parameters
74
- # +:extent+:: Size of QR Code given in pt (1 pt == 1/72 in)
75
- # +:pos+:: Two-element array containing the position at which the QR-Code should be rendered. Defaults to [0,cursor]
76
- # +:dot+:: Size of QR Code module/dot. Calculated from extent or defaulting to 1pt
77
- # +:stroke+:: boolean value whether to draw bounds around the QR Code. Defaults to true.
78
- # +:margin+:: Size of margin around code in QR-Code modules/dots, Default to 4
79
- # +:align+:: Optional alignment within the current bounding box. Valid values are :left, :right, and :center. If set
80
- # This option overrides the horizontal positioning specified in :pos. Defaults to nil.
81
- # +:debug+:: Optional boolean, renders a coordinate grid around the QRCode if true (uses Prawn#stroke_axis)
86
+ # @see Renderer
82
87
  #
83
- def render_qr_code(qr_code, dot: DEFAULT_DOTSIZE, pos: [0,cursor], stroke: true, foreground_color: '000000', background_color: 'FFFFFF', stroke_color: '000000', margin: 4, **options)
84
- extent = extent || (2*margin + qr_code.modules.length) * dot
85
-
86
- case options[:align]
87
- when :center
88
- pos[0] = (@bounding_box.right / 2) - (extent / 2)
89
- when :right
90
- pos[0] = @bounding_box.right - extent
91
- when :left
92
- 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) }
93
130
  end
94
131
 
95
- fill_color background_color
96
-
97
- bounding_box(pos, width: extent, height: extent) do |_box|
98
- fill_color foreground_color
99
- pos_y = margin * dot + qr_code.modules.length * dot
100
-
101
- qr_code.modules.each_index do |row|
102
- pos_x = margin * dot
103
- dark_col = 0
104
- qr_code.modules.each_index do |col|
105
- move_to [pos_x, pos_y]
106
- if qr_code.qrcode.checked?(row, col)
107
- dark_col += 1
108
- else
109
- if dark_col > 0
110
- fill { rectangle([pos_x - dark_col * dot, pos_y], dot * dark_col, dot) }
111
- 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
+ @point[0] = (rlim / 2) - (extent / 2)
170
+ when :right
171
+ @point[0] = rlim - extent
172
+ when :left
173
+ @point[0] = 0
174
+ end
175
+ end
176
+
177
+ # rubocop:disable Metrics/AbcSize
178
+ def render(pdf)
179
+ pdf.fill_color background_color
180
+
181
+ pdf.bounding_box(pos(pdf), width: extent, height: extent) do |_box|
182
+ pdf.fill_color foreground_color
183
+ margin_dist = margin * dot
184
+
185
+ m = qr_code.modules
186
+
187
+ pos_y = margin_dist + m.length * dot
188
+
189
+ m.each_with_index do |row, index|
190
+ pos_x = margin_dist
191
+ dark_col = 0
192
+
193
+ row.each_index do |col|
194
+ pdf.move_to [pos_x, pos_y]
195
+ if qr_code.qrcode.checked?(index, col)
196
+ dark_col += 1
197
+ else
198
+ if dark_col > 0
199
+ dark_col_extent = dark_col * dot
200
+ pdf.fill { pdf.rectangle([pos_x - dark_col_extent, pos_y], dark_col_extent, dot) }
201
+ dark_col = 0
202
+ end
112
203
  end
204
+ pos_x += dot
113
205
  end
114
- pos_x += dot
206
+
207
+ pdf.fill { pdf.rectangle([pos_x - dark_col * dot, pos_y], dot * dark_col, dot) } if dark_col > 0
208
+
209
+ pos_y -= dot
115
210
  end
116
- if dark_col > 0
117
- fill { rectangle([pos_x - dark_col * dot, pos_y], dot * dark_col, dot) }
211
+
212
+ if stroke
213
+ pdf.fill_color stroke_color
214
+ pdf.stroke_bounds
118
215
  end
119
- pos_y -= dot
216
+ pdf.stroke_axis(at: [-1, -1], negative_axes_length: 0, color: '0C0C0C', step_length: 50) if debug
120
217
  end
218
+ end
121
219
 
122
- if stroke
123
- fill_color stroke_color
124
- stroke_bounds
125
- end
126
- stroke_axis(at: [-1,-1], negative_axes_length: 0, color: '0C0C0C', step_length: 50) if options[:debug]
220
+ private
221
+
222
+ attr_reader :debug
223
+
224
+ def pos(pdf)
225
+ @pos ||= [0, pdf.cursor]
127
226
  end
128
227
  end
129
228
  end
@@ -0,0 +1,28 @@
1
+ require_relative 'table/cell'
2
+
3
+ module Prawn
4
+ module QRCode
5
+ module Table
6
+ # create a Prawn::Table::Cell instacne that renders QR codes as table cell
7
+ # @since 0.5.0
8
+ #
9
+ # @param [Hash] options for creating table cell
10
+ # @option [String] :content string content to render as QR code
11
+ # @option [RQRCode::QRCode] :qr_code qr_code object to render
12
+ # @option [Prawn::QRCode::Renderer] :renderer initialized renderer (contains qr_code)
13
+ #
14
+ # The table cell will create a QRCode and Renderer on demand, all necessary options will be passed through
15
+ #
16
+ # @see Prawn::QRCode.min_qrcode
17
+ # @see Prawn::QRCode::Renderer
18
+ # @see Prawn::Table::Cell
19
+ #
20
+ # @return [Prawn::QRCode::Table::Cell] table cell instance for Prawn::Table
21
+ def make_qrcode_cell(**options)
22
+ Prawn::QRCode::Table::Cell.new(self, [0, cursor], options)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ Prawn::Document.extensions << Prawn::QRCode::Table
@@ -0,0 +1,45 @@
1
+ require 'prawn/table/cell'
2
+
3
+ module Prawn
4
+ module QRCode
5
+ module Table
6
+
7
+ # QRCode is a table cell that renders a QR code inside a table.
8
+ # Most users will create a table via the PDF DSL method make_qr_code_cell.
9
+ class Cell < Prawn::Table::Cell
10
+ QR_OPTIONS = %I[content qr_code renderer level mode extent pos dot stroke margin align].freeze
11
+ CELL_OPTS = %I[padding borders border_widths border_colors border_lines colspan rowspan at].freeze
12
+
13
+ QR_OPTIONS.each { |attr| attr_writer attr }
14
+
15
+ def initialize(pdf, pos, **options)
16
+ super(pdf, pos, options.select { |k, _| CELL_OPTS.include?(k) })
17
+ @margin = 4
18
+ @options = options.reject { |k, _| CELL_OPTS.include?(k) }
19
+ @options.each { |k, v| send("#{k}=", v) }
20
+ end
21
+
22
+ def natural_content_width
23
+ renderer.extent
24
+ end
25
+
26
+ def natural_content_height
27
+ renderer.extent
28
+ end
29
+
30
+ def draw_content
31
+ renderer.render(@pdf)
32
+ end
33
+
34
+ def renderer
35
+ @renderer ||= Prawn::QRCode::Renderer.new(qr_code, **@options)
36
+ end
37
+
38
+ def qr_code
39
+ @qr_code = Prawn::QRCode.min_qrcode(content, **@options) unless defined?(@qr_code)
40
+ @qr_code
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,21 @@
1
+ #
2
+ # Copyright 2010 - 2019 Jens Hausherr
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
1
17
  module Prawn
2
18
  module QRCode
3
- VERSION = "0.4.0"
19
+ VERSION = '0.5.0'.freeze
4
20
  end
5
21
  end
data/prawn-qrcode.gemspec CHANGED
@@ -1,5 +1,4 @@
1
- # coding: utf-8
2
- # Copyright 2011-2017 Jens Hausherr
1
+ # Copyright 2011 - 2019 Jens Hausherr
3
2
  #
4
3
  # Licensed under the Apache License, Version 2.0 (the "License");
5
4
  # you may not use this file except in compliance with the License.
@@ -13,28 +12,28 @@
13
12
  # See the License for the specific language governing permissions and
14
13
  # limitations under the License.
15
14
 
16
- lib = File.expand_path('../lib', __FILE__)
15
+ lib = File.expand_path('lib', __dir__)
17
16
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
18
17
  require 'prawn/qrcode/version'
19
18
 
20
19
  Gem::Specification.new do |spec|
21
- spec.name = "prawn-qrcode"
20
+ spec.name = 'prawn-qrcode'
22
21
  spec.version = Prawn::QRCode::VERSION
23
22
  spec.platform = Gem::Platform::RUBY
24
- spec.summary = "Print QR Codes in PDF"
25
- spec.licenses = [ 'Apache License 2.0' ]
26
- spec.files = Dir.glob("{examples,lib}/**/**/*") +
27
- ["Rakefile", "prawn-qrcode.gemspec"]
28
- spec.require_path = "lib"
29
- spec.required_ruby_version = '>= 2.0.0'
30
- spec.required_rubygems_version = ">= 1.3.6"
23
+ spec.summary = 'Print QR Codes in PDF'
24
+ spec.licenses = ['Apache License 2.0']
25
+ spec.files = Dir.glob('{examples,lib,test}/**/**/*') +
26
+ ['Rakefile', 'prawn-qrcode.gemspec']
27
+ spec.require_path = 'lib'
28
+ spec.required_ruby_version = '>= 2.2.0'
29
+ spec.required_rubygems_version = '>= 1.3.6'
31
30
 
32
- spec.extra_rdoc_files = %w{README.md LICENSE}
33
- spec.rdoc_options << '--title' << 'Prawn/QRCode Documentation' <<
34
- '--main' << 'README.md' << '-q'
35
- spec.authors = ["Jens Hausherr"]
36
- spec.email = ["jabbrwcky@gmail.com"]
37
- spec.homepage = "http://github.com/jabbrwcky/prawn-qrcode"
31
+ spec.extra_rdoc_files = %w[README.md LICENSE]
32
+ spec.rdoc_options << '--title' << 'Prawn/QRCode Documentation' \
33
+ '--main' << 'README.md' << '-q'
34
+ spec.authors = ['Jens Hausherr']
35
+ spec.email = ['jabbrwcky@gmail.com']
36
+ spec.homepage = 'http://github.com/jabbrwcky/prawn-qrcode'
38
37
 
39
38
  spec.description = <<END_DESC
40
39
  Prawn/QRCode simplifies the generation and rendering of QRCodes in Prawn PDF documents.
@@ -43,7 +42,9 @@ END_DESC
43
42
  spec.add_dependency('prawn', '>=1')
44
43
  spec.add_dependency('rqrcode', '>=1.0.0')
45
44
 
46
- spec.add_development_dependency "bundler", "~> 1.13"
47
- spec.add_development_dependency "rake", "~> 10.0"
48
- #spec.add_development_dependency "rspec", "~> 3.0"
45
+ spec.add_development_dependency 'bundler', '~> 2.0'
46
+ spec.add_development_dependency 'minitest', '~> 5.12', '>= 5.12.2'
47
+ spec.add_development_dependency 'prawn-table', '~> 0.2.2'
48
+ spec.add_development_dependency 'rake', '~> 13.0'
49
+ spec.add_development_dependency 'rubocop', '~>0.66.0'
49
50
  end
@@ -0,0 +1,11 @@
1
+ require 'minitest/autorun'
2
+ require 'prawn/qrcode'
3
+
4
+ class TestMinQRCode < Minitest::Test
5
+ def test_dot_size_float
6
+ qrcode = Prawn::QRCode.min_qrcode('foobar')
7
+ assert(qrcode)
8
+ dot = Prawn::QRCode.dotsize(qrcode, 72)
9
+ assert_in_delta(2.5, dot, 0.05)
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/prawn/qrcode.rb'
3
+
4
+ class TestRenderer < Minitest::Test
5
+ def setup
6
+ @qrcode = Prawn::QRCode.min_qrcode('https://gituhb.com/jabbrwcky/prawn-qrcode')
7
+ end
8
+
9
+ def test_renderer_defaults
10
+ r = Prawn::QRCode::Renderer.new(@qrcode)
11
+
12
+ assert(r.stroke)
13
+ assert_equal(Prawn::QRCode::DEFAULT_DOTSIZE, r.dot)
14
+ assert_equal('000000', r.foreground_color)
15
+ assert_equal('FFFFFF', r.background_color)
16
+ assert_equal(4, r.margin)
17
+ assert_equal(37.0, r.extent)
18
+ end
19
+
20
+ def test_renderer_extent
21
+ r = Prawn::QRCode::Renderer.new(@qrcode, extent: 72)
22
+ assert_in_delta(1.9, 0.05, r.extent)
23
+ end
24
+
25
+ def test_conflicting_dotsize_and_extent
26
+ assert_raises(Prawn::QRCode::QRCodeError) { Prawn::QRCode::Renderer.new(@qrcode, dot: 3, extent: 72) }
27
+ end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-qrcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Hausherr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-13 00:00:00.000000000 Z
11
+ date: 2019-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prawn
@@ -44,28 +44,76 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.13'
47
+ version: '2.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.13'
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.12'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 5.12.2
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '5.12'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 5.12.2
75
+ - !ruby/object:Gem::Dependency
76
+ name: prawn-table
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.2.2
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 0.2.2
55
89
  - !ruby/object:Gem::Dependency
56
90
  name: rake
57
91
  requirement: !ruby/object:Gem::Requirement
58
92
  requirements:
59
93
  - - "~>"
60
94
  - !ruby/object:Gem::Version
61
- version: '10.0'
95
+ version: '13.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '13.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.66.0
62
110
  type: :development
63
111
  prerelease: false
64
112
  version_requirements: !ruby/object:Gem::Requirement
65
113
  requirements:
66
114
  - - "~>"
67
115
  - !ruby/object:Gem::Version
68
- version: '10.0'
116
+ version: 0.66.0
69
117
  description: " Prawn/QRCode simplifies the generation and rendering of QRCodes in
70
118
  Prawn PDF documents.\n"
71
119
  email:
@@ -82,9 +130,14 @@ files:
82
130
  - examples/autosize_qrcode.rb
83
131
  - examples/dotsize_qrcode.rb
84
132
  - examples/prepared_qrcode.rb
133
+ - examples/table_qrcode.rb
85
134
  - lib/prawn/qrcode.rb
135
+ - lib/prawn/qrcode/table.rb
136
+ - lib/prawn/qrcode/table/cell.rb
86
137
  - lib/prawn/qrcode/version.rb
87
138
  - prawn-qrcode.gemspec
139
+ - test/test_minqrcode.rb
140
+ - test/test_renderer.rb
88
141
  homepage: http://github.com/jabbrwcky/prawn-qrcode
89
142
  licenses:
90
143
  - Apache License 2.0
@@ -92,8 +145,7 @@ metadata: {}
92
145
  post_install_message:
93
146
  rdoc_options:
94
147
  - "--title"
95
- - Prawn/QRCode Documentation
96
- - "--main"
148
+ - Prawn/QRCode Documentation--main
97
149
  - README.md
98
150
  - "-q"
99
151
  require_paths:
@@ -102,14 +154,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
102
154
  requirements:
103
155
  - - ">="
104
156
  - !ruby/object:Gem::Version
105
- version: 2.0.0
157
+ version: 2.2.0
106
158
  required_rubygems_version: !ruby/object:Gem::Requirement
107
159
  requirements:
108
160
  - - ">="
109
161
  - !ruby/object:Gem::Version
110
162
  version: 1.3.6
111
163
  requirements: []
112
- rubygems_version: 3.0.3
164
+ rubygems_version: 3.0.6
113
165
  signing_key:
114
166
  specification_version: 4
115
167
  summary: Print QR Codes in PDF