Skip to content
Open

Dummy #163

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

# Prawn/QRCode

A simple extension to generate and/or render QRCodes for Prawn PDFs

![TravisCI Build state](https://travis-ci.org/jabbrwcky/prawn-qrcode.svg?branch=master)
<!---
![TravisCI Build state](https://travis-ci.org/aghos7/prawn-qrcode.svg?branch=master)
[![Gem Version](https://badge.fury.io/rb/prawn-qrcode.svg)](http://badge.fury.io/rb/prawn-qrcode)
-->

Prawn/QRCode is a Prawn (>= 0.11.1) extension to simplify rendering of QR Codes*.

Expand All @@ -21,7 +24,7 @@ $ gem install prawn-qrcode
```ruby
require 'prawn/qrcode'

qrcode_content = "http://github.com/jabbrwcky/prawn-qrcode"
qrcode_content = "http://github.com/aghos7/prawn-qrcode"
qrcode = RQRCode::QRCode.new(qrcode_content, :level=>:h, :size => 5)

# Render a prepared QRCode at he cursor position
Expand Down Expand Up @@ -74,3 +77,6 @@ Fixed default stroke and explicit conversion of extents to floats.
### 0.2.2.1
Stroke param broken. Replaced with simpler evaluation.

### 0.2.2.2
Initial rewrite

30 changes: 15 additions & 15 deletions examples/autosize_qrcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@

qrcode = 'https://github.com/jabbrwcky/prawn-qrcode'

Prawn::Document::new(:page_size => "A4") do
# Prawn::Document::new(:page_size => "A4") do

text "Sample autosized QR-Code (with stroked bounds) Size of QRCode : 1 in (72 pt)"
print_qr_code(qrcode, :extent=>72)
move_down 20
# text "Sample autosized QR-Code (with stroked bounds) Size of QRCode : 1 in (72 pt)"
# print_qr_code(qrcode, :extent=>72)
# move_down 20

text "Sample autosized QR-Code (with and without stroked bounds) Size of QRCode : 2 in (144 pt)"
cpos = cursor
print_qr_code(qrcode, :extent=>144)
print_qr_code(qrcode, :pos=>[150,cpos], :extent=>144, :stroke=>false)
move_down 10
# text "Sample autosized QR-Code (with and without stroked bounds) Size of QRCode : 2 in (144 pt)"
# cpos = cursor
# print_qr_code(qrcode, :extent=>144)
# print_qr_code(qrcode, :pos=>[150,cpos], :extent=>144, :stroke=>false)
# move_down 10

text "Sample autosized QR-Code (with stroked bounds) Size of QRCode :10 cm"
print_qr_code(qrcode, :extent=>10.send(:cm), :stroke=>true, :level=>:q)
move_down 10
text "Quite huge, isn't it?'"
render_file("autosized.pdf")
end
# text "Sample autosized QR-Code (with stroked bounds) Size of QRCode :10 cm"
# print_qr_code(qrcode, :extent=>10.send(:cm), :stroke=>true, :level=>:q)
# move_down 10
# text "Quite huge, isn't it?'"
# render_file("autosized.pdf")
# end
28 changes: 14 additions & 14 deletions examples/dotsize_qrcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@

qrcode = 'https://github.com/jabbrwcky/prawn-qrcode'

Prawn::Document::new(:page_size => "A4") do
text "Sample autosized QR-Code (with stroked bounds). Size of dots : 3mm (huge)"
print_qr_code(qrcode, :dot=>3.send(:mm))
move_down 20
# Prawn::Document::new(:page_size => "A4") do
# text "Sample autosized QR-Code (with stroked bounds). Size of dots : 3mm (huge)"
# print_qr_code(qrcode, :dot=>3.send(:mm))
# move_down 20

text "Sample QR-Code (with and without stroked bounds) using dots with size: 1 mm (~2.8pt)"
cpos = cursor
print_qr_code(qrcode, :dot=>1.send(:mm))
print_qr_code(qrcode, :pos=>[150,cpos], :dot=>1.send(:mm), :stroke=>false)
move_down 20
# text "Sample QR-Code (with and without stroked bounds) using dots with size: 1 mm (~2.8pt)"
# cpos = cursor
# print_qr_code(qrcode, :dot=>1.send(:mm))
# print_qr_code(qrcode, :pos=>[150,cpos], :dot=>1.send(:mm), :stroke=>false)
# move_down 20

text "Higher ECC Levels (may) increase module size. "+
"This QR Code uses ECC Level Q (ca. 30% of symbols can be recovered)."
print_qr_code(qrcode, :dot=>1.send(:mm), :level=>:q)
# text "Higher ECC Levels (may) increase module size. "+
# "This QR Code uses ECC Level Q (ca. 30% of symbols can be recovered)."
# print_qr_code(qrcode, :dot=>1.send(:mm), :level=>:q)

render_file("dotsize.pdf")
end
# render_file("dotsize.pdf")
# end
22 changes: 11 additions & 11 deletions examples/prepared_qrcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
require 'prawn/core'
require_relative '../lib/prawn/qrcode'

qrcode = RQRCode::QRCode.new 'https://github.com/jabbrwcky/prawn-qrcode', :size=>5
# qrcode = RQRCode::QRCode.new 'https://github.com/jabbrwcky/prawn-qrcode', :size=>5

Prawn::Document::new(:page_size => "A4") do
text "Prawn QR Code sample 1: Predefined QR-Code"
move_down 15
# Prawn::Document::new(:page_size => "A4") do
# text "Prawn QR Code sample 1: Predefined QR-Code"
# move_down 15

text "Sample predefined QR-Code (with stroked bounds) Size of QRCode dots: 1pt (1/72 in)"
render_qr_code(qrcode)
# text "Sample predefined QR-Code (with stroked bounds) Size of QRCode dots: 1pt (1/72 in)"
# render_qr_code(qrcode)

move_down 20
text "Sample predefined QR-Code (without stroked bounds) Size of QRCode dots: 1pt (1/72 in)"
render_qr_code(qrcode, :stroke=>false)
render_file("prepared.pdf")
end
# move_down 20
# text "Sample predefined QR-Code (without stroked bounds) Size of QRCode dots: 1pt (1/72 in)"
# render_qr_code(qrcode, :stroke=>false)
# render_file("prepared.pdf")
# end
187 changes: 105 additions & 82 deletions lib/prawn/qrcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,113 +26,136 @@
# *License*:: Apache License, Version 2.0
#
module QRCode

# The default size for QR Code modules is 1/72 in
DEFAULT_DOTSIZE = 1.to_f

# Prints a QR Code to the PDF document. The QR Code creation happens on the fly.
#
# content:: The string to render as content of the QR Code
#
# *options:: Named optional parameters
# Arguments:
# <tt>content/tt>:: The string to render as content of the QR Code
#
# +:level+:: Error correction level to use. One of: (:l,:m,:h,:q), Defaults to :m
# +:extent+:: Size of QR Code given in pt (1 pt == 1/72 in)
# +:pos+:: Two-element array containing the position at which the QR-Code should be rendered. Defaults to [0,cursor]
# +:dot+:: Size of QR Code module/dot. Calculated from extent or defaulting to 1pt
# +:stroke+:: boolean value whether to draw bounds around the QR Code.
# Defaults to true.
# +:align+:: Optional 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.
#
def print_qr_code(content, *options)
opt = options.extract_options!
qr_version = 0
level = opt[:level] || :m
extent = opt[:extent].to_f
dot_size = opt[:dot].to_f || DEFAULT_DOTSIZE
begin
qr_version +=1
qr_code = RQRCode::QRCode.new(content, :size=>qr_version, :level=>level)
# Options:
# <tt>:options</tt>:: Named optional parameters
# <tt>:version</tt>:: The QR Code version
# <tt>:level</tt>:: Error correction level to use. One of: (:l,:m,:h,:q), Defaults to :m
# <tt>:dot</tt>:: Size of QR Code module/dot.
# <tt>:at</tt>:: an array [x,y] with the location of the top left corner of the image.
# <tt>:position</tt>:: One of (:left, :center, :right) or an x-offset
# <tt>:vposition</tt>:: One of (:top, :center, :center) or an y-offset
# <tt>:height</tt>:: the height of the image [actual height of the image]
# <tt>:width</tt>:: the width of the image [actual width of the image]
# <tt>:scale</tt>:: scale the dimensions of the image proportionally
# <tt>:fit</tt>:: scale the dimensions of the image proportionally to fit inside [width,height]
def print_qr_code(content, options = {})
version = options[:version] || 0
level = options[:level] || :m

dot_size = extent/(8+qr_code.modules.length) if extent
render_qr_code(qr_code, opt.merge(:dot=>dot_size))
begin
version +=1
qr_code = RQRCode::QRCode.new(content, :size => version, :level=> level)
render_qr_code(qr_code, options)
rescue RQRCode::QRCodeRunTimeError
if qr_version <40
if version <40
retry
else
raise
end
end
end

# Renders a prepared QR Code (RQRCode::QRCode) object.
# Renders a QR Code (RQRCode::QRCode) object.
#
# qr_code:: The QR Code (an RQRCode::QRCode) to render
# Arguments:
# <tt>qr_code/tt>:: The QR Code (an RQRCode::QRCode) to render
#
# *options:: Named optional parameters
# +:extent+:: Size of QR Code given in pt (1 pt == 1/72 in)
# +:pos+:: Two-element array containing the position at which the QR-Code should be rendered. Defaults to [0,cursor]
# +:dot+:: Size of QR Code module/dot. Calculated from extent or defaulting to 1pt
# +:stroke+:: boolean value whether to draw bounds around the QR Code. Defaults to true.
# +:align+:: Optional 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.
def render_qr_code(qr_code, *options)
opt = options.extract_options!
dot = opt[:dot].to_f || DEFAULT_DOTSIZE
extent= opt[:extent].to_f || (8+qr_code.modules.length) * dot
stroke = true
stroke = opt[:stroke] if opt.has_key?(:stroke)
# Options:
# <tt>:options</tt>:: Named optional parameters
# <tt>:dot</tt>:: Size of QR Code module/dot.
# <tt>:at</tt>:: an array [x,y] with the location of the top left corner of the image.
# <tt>:position</tt>:: One of (:left, :center, :right) or an x-offset
# <tt>:vposition</tt>:: One of (:top, :center, :center) or an y-offset
# <tt>:height</tt>:: the height of the image [actual height of the image]
# <tt>:width</tt>:: the width of the image [actual width of the image]
# <tt>:scale</tt>:: scale the dimensions of the image proportionally
# <tt>:fit</tt>:: scale the dimensions of the image proportionally to fit inside [width,height]
def render_qr_code(qr_code, options = {})
dot_w, dot_h = qrcode_dot_size(qr_code, options)
if options[:at]
pos_x, pos_y = options[:at]
else
pos_x, pos_y = qrcode_position(qr_code.modules.first.length * dot_w, qr_code.modules.length * dot_h, options)
end

foreground_color = opt[:foreground_color] || '000000'
background_color = opt[:background_color] || 'FFFFFF'
stroke_color = opt[:stroke_color] || '000000'
qr_code.modules.each do |col|
temp_x = pos_x
col.each do |row|
if row
fill { rectangle([temp_x, pos_y], dot_w, dot_h) }
end
temp_x += dot_w
end
pos_y -= dot_h
end
end

pos = opt[:pos] ||[0, cursor]
private
def qrcode_position(w, h, options = {})
options[:vposition] ||= :top
options[:position] ||= :left

y = case options[:vposition]
when :top
bounds.top
when :center
bounds.top - (bounds.height - h) / 2.0
when :bottom
bounds.bottom + h
when Numeric
bounds.top - options[:vposition]
end

align = opt[:align]
case(align)
x = case options[:position]
when :left
bounds.left
when :center
pos[0] = (@bounding_box.right / 2) - (extent / 2)
bounds.left + (bounds.width - w)/ 2.0
when :right
pos[0] = @bounding_box.right - extent
when :left
pos[0] = 0;
bounds.right - w
when Numeric
options[:position] + bounds.left
end

stroke_color stroke_color
fill_color background_color

bounding_box pos, :width => extent, :height => extent do |box|
fill_color foreground_color

if stroke
stroke_bounds
end
return [x,y]
end

pos_y = 4*dot +qr_code.modules.length * dot
def qrcode_dot_size(qr_code, options)
qr_w = qr_code.modules.length
qr_h = qr_code.modules.first.length
w = options[:width] || qr_w
h = options[:height] || qr_h

qr_code.modules.each_index do |row|
pos_x = 4*dot
dark_col = 0
qr_code.modules.each_index do |col|
move_to [pos_x, pos_y]
if qr_code.dark?(row, col)
dark_col = dark_col+1
else
if (dark_col>0)
fill { rectangle([pos_x - dark_col*dot, pos_y], dot*dark_col, dot) }
dark_col = 0
end
end
pos_x = pos_x + dot
end
if (dark_col > 0)
fill { rectangle([pos_x - dark_col*dot, pos_y], dot*dark_col, dot) }
end
pos_y = pos_y - dot
if options[:width] && !options[:height]
wp = w / qr_w
w = qr_w * wp
h = qr_h * wp
elsif options[:height] && !options[:width]
hp = h / qr_h
w = qr_w * hp
h = qr_h * hp
elsif options[:scale]
w = qr_w * options[:scale]
h = qr_h * options[:scale]
elsif options[:fit]
bw, bh = options[:fit]
bp = bw / bh.to_f
ip = qr_w / qr_h
if ip > bp
w = bw
h = bw / ip
else
h = bh
w = bh * ip
end
end

[w/qr_w, h/qr_h]
end

end
Expand Down
4 changes: 2 additions & 2 deletions prawn-qrcode.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

Gem::Specification.new do |spec|
spec.name = "prawn-qrcode"
spec.version = "0.2.2.1"
spec.version = "0.2.2.2"
spec.platform = Gem::Platform::RUBY
spec.summary = "Print QR Codes in PDF"
spec.licenses = [ 'Apache License 2.0' ]
Expand All @@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
spec.authors = ["Jens Hausherr"]
spec.email = ["jabbrwcky@googlemail.com"]
#spec.rubyforge_project = "prawn-qrcode"
spec.homepage = "http://github.com/jabbrwcky/prawn-qrcode"
spec.homepage = "https://github.com/aghos7/prawn-qrcode"

spec.description = <<END_DESC
Prawn/QRCode simplifies the generation and rendering of QRCodes in Prawn PDF documents.
Expand Down