arena_barby 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +29 -0
- data/bin/barby +41 -0
- data/lib/barby.rb +17 -0
- data/lib/barby/barcode.rb +116 -0
- data/lib/barby/barcode/bookland.rb +37 -0
- data/lib/barby/barcode/code_128.rb +410 -0
- data/lib/barby/barcode/code_25.rb +193 -0
- data/lib/barby/barcode/code_25_iata.rb +23 -0
- data/lib/barby/barcode/code_25_interleaved.rb +73 -0
- data/lib/barby/barcode/code_39.rb +233 -0
- data/lib/barby/barcode/code_93.rb +230 -0
- data/lib/barby/barcode/ean_13.rb +178 -0
- data/lib/barby/barcode/ean_8.rb +32 -0
- data/lib/barby/barcode/gs1_128.rb +42 -0
- data/lib/barby/barcode/pdf_417.rb +76 -0
- data/lib/barby/barcode/qr_code.rb +101 -0
- data/lib/barby/outputter.rb +127 -0
- data/lib/barby/outputter/ascii_outputter.rb +41 -0
- data/lib/barby/outputter/cairo_outputter.rb +185 -0
- data/lib/barby/outputter/pdfwriter_outputter.rb +83 -0
- data/lib/barby/outputter/png_outputter.rb +97 -0
- data/lib/barby/outputter/prawn_outputter.rb +99 -0
- data/lib/barby/outputter/rmagick_outputter.rb +126 -0
- data/lib/barby/outputter/svg_outputter.rb +225 -0
- data/lib/barby/vendor.rb +3 -0
- data/lib/barby/version.rb +9 -0
- data/vendor/Pdf417lib-java-0.91/lib/Pdf417lib.jar +0 -0
- data/vendor/Pdf417lib-java-0.91/lib/Pdf417lib.java +1471 -0
- data/vendor/rqrcode/CHANGELOG +29 -0
- data/vendor/rqrcode/COPYING +19 -0
- data/vendor/rqrcode/README +98 -0
- data/vendor/rqrcode/Rakefile +65 -0
- data/vendor/rqrcode/lib/rqrcode.rb +13 -0
- data/vendor/rqrcode/lib/rqrcode/core_ext.rb +5 -0
- data/vendor/rqrcode/lib/rqrcode/core_ext/array.rb +5 -0
- data/vendor/rqrcode/lib/rqrcode/core_ext/array/behavior.rb +9 -0
- data/vendor/rqrcode/lib/rqrcode/core_ext/integer.rb +5 -0
- data/vendor/rqrcode/lib/rqrcode/core_ext/integer/bitwise.rb +11 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode.rb +4 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_8bit_byte.rb +37 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_bit_buffer.rb +56 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_code.rb +421 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_math.rb +63 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_polynomial.rb +78 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_rs_block.rb +313 -0
- data/vendor/rqrcode/lib/rqrcode/qrcode/qr_util.rb +254 -0
- data/vendor/rqrcode/test/runtest.rb +78 -0
- data/vendor/rqrcode/test/test_data.rb +21 -0
- metadata +114 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
*0.3.2* (Mar 15th, 2009)
|
2
|
+
|
3
|
+
* Ruby 1.9 fixes by [Tore Darell http://tore.darell.no] [Chris Mowforth http://blog.99th.st]
|
4
|
+
|
5
|
+
*0.3.1* (Nov 24th, 2008)
|
6
|
+
|
7
|
+
* expanded RS block table to QRcode version 40 [Vladislav Gorodetskiy]
|
8
|
+
|
9
|
+
*0.3.0* (Feb 28th, 2008)
|
10
|
+
|
11
|
+
* added more documentation
|
12
|
+
* changed to_console to to_s (what was I thinking)
|
13
|
+
* add more tests
|
14
|
+
|
15
|
+
*0.2.1* (Feb 24th, 2008)
|
16
|
+
|
17
|
+
* small changes to rakefile and readme
|
18
|
+
* small change to to_console method
|
19
|
+
* make console_count method private (use modules.size instead)
|
20
|
+
* slowy updating rdocs
|
21
|
+
|
22
|
+
*0.2.0* (Feb 23rd, 2008)
|
23
|
+
|
24
|
+
* Split files up [DR]
|
25
|
+
* added rdoc comment ... more to do there
|
26
|
+
|
27
|
+
*0.1.0* (Feb 22rd, 2008)
|
28
|
+
|
29
|
+
* Initial Release [DR]
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2008 Duncan Robertson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
5
|
+
deal in the Software without restriction, including without limitation the
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
16
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
19
|
+
|
@@ -0,0 +1,98 @@
|
|
1
|
+
== rQRCode, Encode QRCodes
|
2
|
+
|
3
|
+
rQRCode is a library for encoding QR Codes in Ruby. It has a simple interface with all the standard qrcode options. It was adapted from the Javascript library by Kazuhiko Arase.
|
4
|
+
|
5
|
+
RubyForge Project Page http://rubyforge.org/projects/rqrcode/
|
6
|
+
|
7
|
+
== An Overview
|
8
|
+
|
9
|
+
Let's clear up some rQRCode stuff.
|
10
|
+
|
11
|
+
# rQRCode is a *standalone library*. It requires no other libraries. Just Ruby!
|
12
|
+
# It is an encoding library. You can't decode QR codes with it.
|
13
|
+
# The interface is simple and assumes you just want to encode a string into a QR code
|
14
|
+
# QR code is trademarked by Denso Wave inc
|
15
|
+
|
16
|
+
== Resources
|
17
|
+
|
18
|
+
wikipedia:: http://en.wikipedia.org/wiki/QR_Code
|
19
|
+
Denso-Wave website:: http://www.denso-wave.com/qrcode/index-e.html
|
20
|
+
kaywa:: http://qrcode.kaywa.com
|
21
|
+
|
22
|
+
|
23
|
+
== Installing
|
24
|
+
|
25
|
+
You may get the latest stable version from Rubyforge.
|
26
|
+
|
27
|
+
$ gem install rqrcode
|
28
|
+
|
29
|
+
You can also get the source from http://github.com/whomwah/rqrcode/tree/master
|
30
|
+
|
31
|
+
$ git clone git://github.com/whomwah/rqrcode.git
|
32
|
+
|
33
|
+
|
34
|
+
=== Loading rQRCode Itself
|
35
|
+
|
36
|
+
You have installed the gem already, yeah?
|
37
|
+
|
38
|
+
require 'rubygems'
|
39
|
+
require 'rqrcode'
|
40
|
+
|
41
|
+
=== Simple QRCode generation to screen
|
42
|
+
|
43
|
+
qr = RQRCode::QRCode.new( 'my string to generate', :size => 4, :level => :h )
|
44
|
+
puts qr.to_s
|
45
|
+
#
|
46
|
+
# Prints:
|
47
|
+
# xxxxxxx x x x x x xx xxxxxxx
|
48
|
+
# x x xxx xxxxxx xxx x x
|
49
|
+
# x xxx x xxxxx x xx x xxx x
|
50
|
+
# ... etc
|
51
|
+
|
52
|
+
=== Simple QRCode generation to view (RubyOnRails)
|
53
|
+
|
54
|
+
<b>Controller:</b>
|
55
|
+
@qr = RQRCode::QRCode.new( 'my string to generate', :size => 4, :level => :h )
|
56
|
+
|
57
|
+
<b>View: (minimal styling added)</b>
|
58
|
+
<style type="text/css">
|
59
|
+
table {
|
60
|
+
border-width: 0;
|
61
|
+
border-style: none;
|
62
|
+
border-color: #0000ff;
|
63
|
+
border-collapse: collapse;
|
64
|
+
}
|
65
|
+
td {
|
66
|
+
border-width: 0;
|
67
|
+
border-style: none;
|
68
|
+
border-color: #0000ff;
|
69
|
+
border-collapse: collapse;
|
70
|
+
padding: 0;
|
71
|
+
margin: 0;
|
72
|
+
width: 10px;
|
73
|
+
height: 10px;
|
74
|
+
}
|
75
|
+
td.black { background-color: #000; }
|
76
|
+
td.white { background-color: #fff; }
|
77
|
+
</style>
|
78
|
+
|
79
|
+
<table>
|
80
|
+
<% @qr.modules.each_index do |x| %>
|
81
|
+
<tr>
|
82
|
+
<% @qr.modules.each_index do |y| %>
|
83
|
+
<% if @qr.is_dark(x,y) %>
|
84
|
+
<td class="black"/>
|
85
|
+
<% else %>
|
86
|
+
<td class="white"/>
|
87
|
+
<% end %>
|
88
|
+
<% end %>
|
89
|
+
</tr>
|
90
|
+
<% end %>
|
91
|
+
</table>
|
92
|
+
|
93
|
+
== Contact
|
94
|
+
|
95
|
+
Author:: Duncan Robertson
|
96
|
+
Email:: duncan@whomwah.com
|
97
|
+
Home Page:: http://whomwah.com
|
98
|
+
License:: MIT Licence (http://www.opensource.org/licenses/mit-license.html)
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
require 'rake/testtask'
|
7
|
+
|
8
|
+
NAME = "rqrcode"
|
9
|
+
VERS = "0.3.2"
|
10
|
+
CLEAN.include ['pkg', 'rdoc']
|
11
|
+
|
12
|
+
Gem::manage_gems
|
13
|
+
|
14
|
+
spec = Gem::Specification.new do |s|
|
15
|
+
s.name = NAME
|
16
|
+
s.version = VERS
|
17
|
+
s.author = "Duncan Robertson"
|
18
|
+
s.email = "duncan@whomwah.com"
|
19
|
+
s.homepage = "http://rqrcode.rubyforge.org"
|
20
|
+
s.platform = Gem::Platform::RUBY
|
21
|
+
s.summary = "A library to encode QR Codes"
|
22
|
+
s.rubyforge_project = NAME
|
23
|
+
s.description = <<EOF
|
24
|
+
rQRCode is a library for encoding QR Codes. The simple
|
25
|
+
interface allows you to create QR Code data structures
|
26
|
+
ready to be displayed in the way you choose.
|
27
|
+
EOF
|
28
|
+
s.files = FileList["lib/**/*", "test/*"].exclude("rdoc").to_a
|
29
|
+
s.require_path = "lib"
|
30
|
+
s.has_rdoc = true
|
31
|
+
s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"]
|
32
|
+
s.test_file = "test/runtest.rb"
|
33
|
+
end
|
34
|
+
|
35
|
+
task :build_package => [:repackage]
|
36
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
37
|
+
#pkg.need_zip = true
|
38
|
+
#pkg.need_tar = true
|
39
|
+
pkg.gem_spec = spec
|
40
|
+
end
|
41
|
+
|
42
|
+
desc "Default: run unit tests."
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
desc "Run all the tests"
|
46
|
+
Rake::TestTask.new(:test) do |t|
|
47
|
+
t.libs << "lib"
|
48
|
+
t.pattern = "test/runtest.rb"
|
49
|
+
t.verbose = true
|
50
|
+
end
|
51
|
+
|
52
|
+
desc "Generate documentation for the library"
|
53
|
+
Rake::RDocTask.new("rdoc") { |rdoc|
|
54
|
+
rdoc.rdoc_dir = 'rdoc'
|
55
|
+
rdoc.title = "rQRCode Documentation"
|
56
|
+
rdoc.template = "~/bin/jamis.rb"
|
57
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
58
|
+
rdoc.main = "README"
|
59
|
+
rdoc.rdoc_files.include("README", "CHANGELOG", "COPYING", 'lib/**/*.rb')
|
60
|
+
}
|
61
|
+
|
62
|
+
desc "rdoc to rubyforge"
|
63
|
+
task :rubyforge => [:rdoc] do
|
64
|
+
sh %{/usr/bin/scp -r -p rdoc/* rubyforge:/var/www/gforge-projects/rqrcode}
|
65
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright 2008 by Duncan Robertson (duncan@whomwah.com).
|
5
|
+
# All rights reserved.
|
6
|
+
|
7
|
+
# Permission is granted for use, copying, modification, distribution,
|
8
|
+
# and distribution of modified versions of this work as long as the
|
9
|
+
# above copyright notice is included.
|
10
|
+
#++
|
11
|
+
|
12
|
+
require "rqrcode/core_ext"
|
13
|
+
require "rqrcode/qrcode"
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright 2004 by Duncan Robertson (duncan@whomwah.com).
|
5
|
+
# All rights reserved.
|
6
|
+
|
7
|
+
# Permission is granted for use, copying, modification, distribution,
|
8
|
+
# and distribution of modified versions of this work as long as the
|
9
|
+
# above copyright notice is included.
|
10
|
+
#++
|
11
|
+
|
12
|
+
module RQRCode
|
13
|
+
|
14
|
+
class QR8bitByte
|
15
|
+
attr_reader :mode
|
16
|
+
|
17
|
+
def initialize( data )
|
18
|
+
@mode = QRMODE[:mode_8bit_byte]
|
19
|
+
@data = data;
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def get_length
|
24
|
+
@data.size
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def write( buffer )
|
29
|
+
( 0...@data.size ).each do |i|
|
30
|
+
c = @data[i]
|
31
|
+
c = c.ord if c.respond_to?(:ord)#String#[] returns single-char string in 1.9, .ord gets ASCII pos
|
32
|
+
buffer.put( c, 8 )
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright 2004 by Duncan Robertson (duncan@whomwah.com).
|
5
|
+
# All rights reserved.
|
6
|
+
|
7
|
+
# Permission is granted for use, copying, modification, distribution,
|
8
|
+
# and distribution of modified versions of this work as long as the
|
9
|
+
# above copyright notice is included.
|
10
|
+
#++
|
11
|
+
|
12
|
+
module RQRCode
|
13
|
+
|
14
|
+
class QRBitBuffer
|
15
|
+
attr_reader :buffer
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@buffer = []
|
19
|
+
@length = 0
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def get( index )
|
24
|
+
buf_index = (index / 8).floor
|
25
|
+
(( (@buffer[buf_index]).rszf(7 - index % 8)) & 1) == 1
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def put( num, length )
|
30
|
+
( 0...length ).each do |i|
|
31
|
+
put_bit((((num).rszf(length - i - 1)) & 1) == 1)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def get_length_in_bits
|
37
|
+
@length
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def put_bit( bit )
|
42
|
+
buf_index = ( @length / 8 ).floor
|
43
|
+
if @buffer.size <= buf_index
|
44
|
+
@buffer << 0
|
45
|
+
end
|
46
|
+
|
47
|
+
if bit
|
48
|
+
@buffer[buf_index] |= ((0x80).rszf(@length % 8))
|
49
|
+
end
|
50
|
+
|
51
|
+
@length += 1
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,421 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright 2008 by Duncan Robertson (duncan@whomwah.com).
|
5
|
+
# All rights reserved.
|
6
|
+
|
7
|
+
# Permission is granted for use, copying, modification, distribution,
|
8
|
+
# and distribution of modified versions of this work as long as the
|
9
|
+
# above copyright notice is included.
|
10
|
+
#++
|
11
|
+
|
12
|
+
module RQRCode #:nodoc:
|
13
|
+
|
14
|
+
QRMODE = {
|
15
|
+
:mode_number => 1 << 0,
|
16
|
+
:mode_alpha_numk => 1 << 1,
|
17
|
+
:mode_8bit_byte => 1 << 2,
|
18
|
+
:mode_kanji => 1 << 3
|
19
|
+
}
|
20
|
+
|
21
|
+
QRERRORCORRECTLEVEL = {
|
22
|
+
:l => 1,
|
23
|
+
:m => 0,
|
24
|
+
:q => 3,
|
25
|
+
:h => 2
|
26
|
+
}
|
27
|
+
|
28
|
+
QRMASKPATTERN = {
|
29
|
+
:pattern000 => 0,
|
30
|
+
:pattern001 => 1,
|
31
|
+
:pattern010 => 2,
|
32
|
+
:pattern011 => 3,
|
33
|
+
:pattern100 => 4,
|
34
|
+
:pattern101 => 5,
|
35
|
+
:pattern110 => 6,
|
36
|
+
:pattern111 => 7
|
37
|
+
}
|
38
|
+
|
39
|
+
# StandardErrors
|
40
|
+
|
41
|
+
class QRCodeArgumentError < ArgumentError; end
|
42
|
+
class QRCodeRunTimeError < RuntimeError; end
|
43
|
+
|
44
|
+
# == Creation
|
45
|
+
#
|
46
|
+
# QRCode objects expect only one required constructor parameter
|
47
|
+
# and an optional hash of any other. Here's a few examples:
|
48
|
+
#
|
49
|
+
# qr = RQRCode::QRCode.new('hello world')
|
50
|
+
# qr = RQRCode::QRCode.new('hello world', :size => 1, :level => :m )
|
51
|
+
#
|
52
|
+
|
53
|
+
class QRCode
|
54
|
+
attr_reader :modules, :module_count
|
55
|
+
|
56
|
+
PAD0 = 0xEC
|
57
|
+
PAD1 = 0x11
|
58
|
+
|
59
|
+
# Expects a string to be parsed in, other args are optional
|
60
|
+
#
|
61
|
+
# # string - the string you wish to encode
|
62
|
+
# # size - the size of the qrcode (default 4)
|
63
|
+
# # level - the error correction level, can be:
|
64
|
+
# * Level :l 7% of code can be restored
|
65
|
+
# * Level :m 15% of code can be restored
|
66
|
+
# * Level :q 25% of code can be restored
|
67
|
+
# * Level :h 30% of code can be restored (default :h)
|
68
|
+
#
|
69
|
+
# qr = RQRCode::QRCode.new('hello world', :size => 1, :level => :m )
|
70
|
+
#
|
71
|
+
|
72
|
+
def initialize( *args )
|
73
|
+
raise QRCodeArgumentError unless args.first.kind_of?( String )
|
74
|
+
|
75
|
+
@data = args.shift
|
76
|
+
options = args.extract_options!
|
77
|
+
level = options[:level] || :h
|
78
|
+
@error_correct_level = QRERRORCORRECTLEVEL[ level.to_sym ]
|
79
|
+
@type_number = options[:size] || 4
|
80
|
+
@module_count = @type_number * 4 + 17
|
81
|
+
@modules = nil
|
82
|
+
@data_cache = nil
|
83
|
+
@data_list = QR8bitByte.new( @data )
|
84
|
+
|
85
|
+
self.make
|
86
|
+
end
|
87
|
+
|
88
|
+
# <tt>is_dark</tt> is called with a +col+ and +row+ parameter. This will
|
89
|
+
# return true or false based on whether that coordinate exists in the
|
90
|
+
# matrix returned. It would normally be called while iterating through
|
91
|
+
# <tt>modules</tt>. A simple example would be:
|
92
|
+
#
|
93
|
+
# instance.is_dark( 10, 10 ) => true
|
94
|
+
#
|
95
|
+
|
96
|
+
def is_dark( row, col )
|
97
|
+
if row < 0 || @module_count <= row || col < 0 || @module_count <= col
|
98
|
+
raise QRCodeRunTimeError, "#{row},#{col}"
|
99
|
+
end
|
100
|
+
@modules[row][col]
|
101
|
+
end
|
102
|
+
|
103
|
+
# This is a public method that returns the QR Code you have
|
104
|
+
# generated as a string. It will not be able to be read
|
105
|
+
# in this format by a QR Code reader, but will give you an
|
106
|
+
# idea if the final outout. It takes two optional args
|
107
|
+
# +:true+ and +:false+ which are there for you to choose
|
108
|
+
# how the output looks. Here's an example of it's use:
|
109
|
+
#
|
110
|
+
# instance.to_s =>
|
111
|
+
# xxxxxxx x x x x x xx xxxxxxx
|
112
|
+
# x x xxx xxxxxx xxx x x
|
113
|
+
# x xxx x xxxxx x xx x xxx x
|
114
|
+
#
|
115
|
+
# instance._to_s( :true => 'E', :false => 'Q') =>
|
116
|
+
# EEEEEEEQEQQEQEQQQEQEQQEEQQEEEEEEE
|
117
|
+
# EQQQQQEQQEEEQQEEEEEEQEEEQQEQQQQQE
|
118
|
+
# EQEEEQEQQEEEEEQEQQQQQQQEEQEQEEEQE
|
119
|
+
#
|
120
|
+
|
121
|
+
def to_s( *args )
|
122
|
+
options = args.extract_options!
|
123
|
+
row = options[:true] || 'x'
|
124
|
+
col = options[:false] || ' '
|
125
|
+
|
126
|
+
res = []
|
127
|
+
|
128
|
+
@modules.each_index do |c|
|
129
|
+
tmp = []
|
130
|
+
@modules.each_index do |r|
|
131
|
+
if is_dark(c,r)
|
132
|
+
tmp << row
|
133
|
+
else
|
134
|
+
tmp << col
|
135
|
+
end
|
136
|
+
end
|
137
|
+
res << tmp.join
|
138
|
+
end
|
139
|
+
res.join("\n")
|
140
|
+
end
|
141
|
+
|
142
|
+
protected
|
143
|
+
|
144
|
+
def make #:nodoc:
|
145
|
+
make_impl( false, get_best_mask_pattern )
|
146
|
+
end
|
147
|
+
|
148
|
+
private
|
149
|
+
|
150
|
+
|
151
|
+
def make_impl( test, mask_pattern ) #:nodoc:
|
152
|
+
@modules = Array.new( @module_count )
|
153
|
+
|
154
|
+
( 0...@module_count ).each do |row|
|
155
|
+
@modules[row] = Array.new( @module_count )
|
156
|
+
end
|
157
|
+
|
158
|
+
setup_position_probe_pattern( 0, 0 )
|
159
|
+
setup_position_probe_pattern( @module_count - 7, 0 )
|
160
|
+
setup_position_probe_pattern( 0, @module_count - 7 )
|
161
|
+
setup_position_adjust_pattern
|
162
|
+
setup_timing_pattern
|
163
|
+
setup_type_info( test, mask_pattern )
|
164
|
+
setup_type_number( test ) if @type_number >= 7
|
165
|
+
|
166
|
+
if @data_cache.nil?
|
167
|
+
@data_cache = QRCode.create_data(
|
168
|
+
@type_number, @error_correct_level, @data_list
|
169
|
+
)
|
170
|
+
end
|
171
|
+
|
172
|
+
map_data( @data_cache, mask_pattern )
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
def setup_position_probe_pattern( row, col ) #:nodoc:
|
177
|
+
( -1..7 ).each do |r|
|
178
|
+
next if ( row + r ) <= -1 || @module_count <= ( row + r )
|
179
|
+
( -1..7 ).each do |c|
|
180
|
+
next if ( col + c ) <= -1 || @module_count <= ( col + c )
|
181
|
+
if 0 <= r && r <= 6 && ( c == 0 || c == 6 ) || 0 <= c && c <= 6 && ( r == 0 || r == 6 ) || 2 <= r && r <= 4 && 2 <= c && c <= 4
|
182
|
+
@modules[row + r][col + c] = true;
|
183
|
+
else
|
184
|
+
@modules[row + r][col + c] = false;
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
|
191
|
+
def get_best_mask_pattern #:nodoc:
|
192
|
+
min_lost_point = 0
|
193
|
+
pattern = 0
|
194
|
+
|
195
|
+
( 0...8 ).each do |i|
|
196
|
+
make_impl( true, i )
|
197
|
+
lost_point = QRUtil.get_lost_point( self )
|
198
|
+
|
199
|
+
if i == 0 || min_lost_point > lost_point
|
200
|
+
min_lost_point = lost_point
|
201
|
+
pattern = i
|
202
|
+
end
|
203
|
+
end
|
204
|
+
pattern
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
def setup_timing_pattern #:nodoc:
|
209
|
+
( 8...@module_count - 8 ).each do |i|
|
210
|
+
@modules[i][6] = @modules[6][i] = i % 2 == 0
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
|
215
|
+
def setup_position_adjust_pattern #:nodoc:
|
216
|
+
pos = QRUtil.get_pattern_position(@type_number)
|
217
|
+
|
218
|
+
( 0...pos.size ).each do |i|
|
219
|
+
( 0...pos.size ).each do |j|
|
220
|
+
row = pos[i]
|
221
|
+
col = pos[j]
|
222
|
+
|
223
|
+
next unless @modules[row][col].nil?
|
224
|
+
|
225
|
+
( -2..2 ).each do |r|
|
226
|
+
( -2..2 ).each do |c|
|
227
|
+
if r == -2 || r == 2 || c == -2 || c == 2 || ( r == 0 && c == 0 )
|
228
|
+
@modules[row + r][col + c] = true
|
229
|
+
else
|
230
|
+
@modules[row + r][col + c] = false
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
|
239
|
+
def setup_type_number( test ) #:nodoc:
|
240
|
+
bits = QRUtil.get_bch_type_number( @type_number )
|
241
|
+
|
242
|
+
( 0...18 ).each do |i|
|
243
|
+
mod = ( !test && ( (bits >> i) & 1) == 1 )
|
244
|
+
@modules[ (i / 3).floor ][ i % 3 + @module_count - 8 - 3 ] = mod
|
245
|
+
@modules[ i % 3 + @module_count - 8 - 3 ][ (i / 3).floor ] = mod
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
|
250
|
+
def setup_type_info( test, mask_pattern ) #:nodoc:
|
251
|
+
data = (@error_correct_level << 3 | mask_pattern)
|
252
|
+
bits = QRUtil.get_bch_type_info( data )
|
253
|
+
|
254
|
+
( 0...15 ).each do |i|
|
255
|
+
mod = (!test && ( (bits >> i) & 1) == 1)
|
256
|
+
|
257
|
+
# vertical
|
258
|
+
if i < 6
|
259
|
+
@modules[i][8] = mod
|
260
|
+
elsif i < 8
|
261
|
+
@modules[ i + 1 ][8] = mod
|
262
|
+
else
|
263
|
+
@modules[ @module_count - 15 + i ][8] = mod
|
264
|
+
end
|
265
|
+
|
266
|
+
# horizontal
|
267
|
+
if i < 8
|
268
|
+
@modules[8][ @module_count - i - 1 ] = mod
|
269
|
+
elsif i < 9
|
270
|
+
@modules[8][ 15 - i - 1 + 1 ] = mod
|
271
|
+
else
|
272
|
+
@modules[8][ 15 - i - 1 ] = mod
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
# fixed module
|
277
|
+
@modules[ @module_count - 8 ][8] = !test
|
278
|
+
end
|
279
|
+
|
280
|
+
|
281
|
+
def map_data( data, mask_pattern ) #:nodoc:
|
282
|
+
inc = -1
|
283
|
+
row = @module_count - 1
|
284
|
+
bit_index = 7
|
285
|
+
byte_index = 0
|
286
|
+
|
287
|
+
( @module_count - 1 ).step( 1, -2 ) do |col|
|
288
|
+
col = col - 1 if col <= 6
|
289
|
+
|
290
|
+
while true do
|
291
|
+
( 0...2 ).each do |c|
|
292
|
+
|
293
|
+
if @modules[row][ col - c ].nil?
|
294
|
+
dark = false
|
295
|
+
if byte_index < data.size && !data[byte_index].nil?
|
296
|
+
dark = (( (data[byte_index]).rszf( bit_index ) & 1) == 1 )
|
297
|
+
end
|
298
|
+
mask = QRUtil.get_mask( mask_pattern, row, col - c )
|
299
|
+
dark = !dark if mask
|
300
|
+
@modules[row][ col - c ] = dark
|
301
|
+
bit_index -= 1
|
302
|
+
|
303
|
+
if bit_index == -1
|
304
|
+
byte_index += 1
|
305
|
+
bit_index = 7
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
row += inc
|
311
|
+
|
312
|
+
if row < 0 || @module_count <= row
|
313
|
+
row -= inc
|
314
|
+
inc = -inc
|
315
|
+
break
|
316
|
+
end
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
def QRCode.create_data( type_number, error_correct_level, data_list ) #:nodoc:
|
322
|
+
rs_blocks = QRRSBlock.get_rs_blocks( type_number, error_correct_level )
|
323
|
+
buffer = QRBitBuffer.new
|
324
|
+
|
325
|
+
data = data_list
|
326
|
+
buffer.put( data.mode, 4 )
|
327
|
+
buffer.put(
|
328
|
+
data.get_length, QRUtil.get_length_in_bits( data.mode, type_number )
|
329
|
+
)
|
330
|
+
data.write( buffer )
|
331
|
+
|
332
|
+
total_data_count = 0
|
333
|
+
( 0...rs_blocks.size ).each do |i|
|
334
|
+
total_data_count = total_data_count + rs_blocks[i].data_count
|
335
|
+
end
|
336
|
+
|
337
|
+
if buffer.get_length_in_bits > total_data_count * 8
|
338
|
+
raise QRCodeRunTimeError,
|
339
|
+
"code length overflow. (#{buffer.get_length_in_bits}>#{total_data_count})"
|
340
|
+
end
|
341
|
+
|
342
|
+
if buffer.get_length_in_bits + 4 <= total_data_count * 8
|
343
|
+
buffer.put( 0, 4 )
|
344
|
+
end
|
345
|
+
|
346
|
+
while buffer.get_length_in_bits % 8 != 0
|
347
|
+
buffer.put_bit( false )
|
348
|
+
end
|
349
|
+
|
350
|
+
while true
|
351
|
+
break if buffer.get_length_in_bits >= total_data_count * 8
|
352
|
+
buffer.put( QRCode::PAD0, 8 )
|
353
|
+
break if buffer.get_length_in_bits >= total_data_count * 8
|
354
|
+
buffer.put( QRCode::PAD1, 8 )
|
355
|
+
end
|
356
|
+
|
357
|
+
QRCode.create_bytes( buffer, rs_blocks )
|
358
|
+
end
|
359
|
+
|
360
|
+
|
361
|
+
def QRCode.create_bytes( buffer, rs_blocks ) #:nodoc:
|
362
|
+
offset = 0
|
363
|
+
max_dc_count = 0
|
364
|
+
max_ec_count = 0
|
365
|
+
dcdata = Array.new( rs_blocks.size )
|
366
|
+
ecdata = Array.new( rs_blocks.size )
|
367
|
+
|
368
|
+
( 0...rs_blocks.size ).each do |r|
|
369
|
+
dc_count = rs_blocks[r].data_count
|
370
|
+
ec_count = rs_blocks[r].total_count - dc_count
|
371
|
+
max_dc_count = [ max_dc_count, dc_count ].max
|
372
|
+
max_ec_count = [ max_ec_count, ec_count ].max
|
373
|
+
dcdata[r] = Array.new( dc_count )
|
374
|
+
|
375
|
+
( 0...dcdata[r].size ).each do |i|
|
376
|
+
dcdata[r][i] = 0xff & buffer.buffer[ i + offset ]
|
377
|
+
end
|
378
|
+
|
379
|
+
offset = offset + dc_count
|
380
|
+
rs_poly = QRUtil.get_error_correct_polynomial( ec_count )
|
381
|
+
raw_poly = QRPolynomial.new( dcdata[r], rs_poly.get_length - 1 )
|
382
|
+
mod_poly = raw_poly.mod( rs_poly )
|
383
|
+
ecdata[r] = Array.new( rs_poly.get_length - 1 )
|
384
|
+
( 0...ecdata[r].size ).each do |i|
|
385
|
+
mod_index = i + mod_poly.get_length - ecdata[r].size
|
386
|
+
ecdata[r][i] = mod_index >= 0 ? mod_poly.get( mod_index ) : 0
|
387
|
+
end
|
388
|
+
end
|
389
|
+
|
390
|
+
total_code_count = 0
|
391
|
+
( 0...rs_blocks.size ).each do |i|
|
392
|
+
total_code_count = total_code_count + rs_blocks[i].total_count
|
393
|
+
end
|
394
|
+
|
395
|
+
data = Array.new( total_code_count )
|
396
|
+
index = 0
|
397
|
+
|
398
|
+
( 0...max_dc_count ).each do |i|
|
399
|
+
( 0...rs_blocks.size ).each do |r|
|
400
|
+
if i < dcdata[r].size
|
401
|
+
index += 1
|
402
|
+
data[index-1] = dcdata[r][i]
|
403
|
+
end
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
( 0...max_ec_count ).each do |i|
|
408
|
+
( 0...rs_blocks.size ).each do |r|
|
409
|
+
if i < ecdata[r].size
|
410
|
+
index += 1
|
411
|
+
data[index-1] = ecdata[r][i]
|
412
|
+
end
|
413
|
+
end
|
414
|
+
end
|
415
|
+
|
416
|
+
data
|
417
|
+
end
|
418
|
+
|
419
|
+
end
|
420
|
+
|
421
|
+
end
|