rqrcode_core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 86bf286f36fb7acec89e8a8ac63c9b3db53924419765d73299af06b2d5af6e6b
4
+ data.tar.gz: 63f29d43c5442598324647c876f7d93e60a1039812396aa2697e8066b06f1621
5
+ SHA512:
6
+ metadata.gz: 0f529986cdb960c2f01446b2b25ef89225399bc5005bb91294112d9a361ceed33a1d72d3a5f5f6acb15ea9334286dfb91ae9349cc56d2aad66b055745d91c56c
7
+ data.tar.gz: 150efe0ebfaffcdd317983bbcef042653c96421c2b42f78b919f2fd87b361ffd6b256660e95b76afbb096c63ca5985726688c07648ad26efc28ffedd487af851
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.devcontainer/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rqrcode-base.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rqrcode_core (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.11.3)
10
+ rake (10.5.0)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 2.0)
17
+ minitest (~> 5.0)
18
+ rake (~> 10.0)
19
+ rqrcode_core!
20
+
21
+ BUNDLED WITH
22
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2008 Duncan Robertson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ [![Codeship Status for whomwah/rqrcode_core](https://app.codeship.com/projects/d2dc8c80-6e88-0137-bf46-6a53d28fd6d4/status?branch=master)](https://app.codeship.com/projects/347347)
2
+
3
+ # RQRCodeCore
4
+
5
+ `rqrcode_core` is a library for encoding QR codes in pure Ruby. It has a simple interface with all the standard qrcode options. It was originally adapted in 2008 from a Javascript library by [Kazuhiko Arase](https://github.com/kazuhikoarase).
6
+
7
+ Features:
8
+
9
+ * `rqrcode_core` is a ruby only library. It requires no native libraries. Just Ruby!
10
+ * It is an encoding library. You can't decode QR codes with it.
11
+ * The interface is simple and assumes you just want to encode a string into a QR code.
12
+ * QR code is trademarked by Denso Wave inc.
13
+
14
+ `rqrcode_core` is the basis of the popular `rqrcode` gem [https://github.com/whomwah/rqrcode]. This gem allows you to generate different renderings of your QR code, including `png`, `svg` and `ansi`.
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'rqrcode_core'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install rqrcode_core
31
+
32
+ ## Basic Usage
33
+
34
+ ```ruby
35
+ $ require 'rqrcode_core'
36
+ $ qr = RQRCodeCore::QRCode.new('my string to generate', size: 4, level: :h)
37
+ $ puts qr.to_s
38
+ ```
39
+
40
+ Output:
41
+
42
+ ```
43
+ xxxxxxx x x x x x xx xxxxxxx
44
+ x x xxx xxxxxx xxx x x
45
+ x xxx x xxxxx x xx x xxx x
46
+ ... etc
47
+ ```
48
+
49
+ ## Doing your own rendering
50
+
51
+ ```ruby
52
+ require 'rqrcode_core'
53
+
54
+ qr = RQRCodeCore::QRCode.new('my string to generate', size: 4, level: :h)
55
+ qr.modules.each do |row|
56
+ row.each do |col|
57
+ print col ? '#' : ' '
58
+ end
59
+
60
+ print "\n"
61
+ end
62
+ ```
63
+
64
+ ### Options
65
+
66
+ The library expects a string to be parsed in, other args are optional.
67
+
68
+ ```
69
+ string - the string you wish to encode
70
+
71
+ size - the size of the qrcode (default 4)
72
+
73
+ level - the error correction level, can be:
74
+ * Level :l 7% of code can be restored
75
+ * Level :m 15% of code can be restored
76
+ * Level :q 25% of code can be restored
77
+ * Level :h 30% of code can be restored (default :h)
78
+
79
+ mode - the mode of the qrcode (defaults to alphanumeric or byte_8bit, depending on the input data):
80
+ * :number
81
+ * :alphanumeric
82
+ * :byte_8bit
83
+ * :kanji
84
+ ```
85
+
86
+ #### Example
87
+
88
+ ```
89
+ qr = RQRCodeCore::QRCode.new('hello world', size: 1, level: :m, mode: :alphanumeric)
90
+ ```
91
+
92
+ ## Development
93
+
94
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
95
+
96
+ ## Contributing
97
+
98
+ Bug reports and pull requests are welcome on GitHub at https://github.com/whomwah/rqrcode_core.
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rqrcode_core"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RQRCodeCore
4
+ require "rqrcode_core/core_ext"
5
+ require "rqrcode_core/qrcode"
6
+ require "rqrcode_core/version"
7
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rqrcode_core/core_ext/array'
4
+ require 'rqrcode_core/core_ext/integer'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rqrcode_core/core_ext/array/behavior'
4
+
5
+ class Array #:nodoc: all
6
+ include RQRCodeCore::CoreExtensions::Array::Behavior
7
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RQRCodeCore
4
+ module CoreExtensions #:nodoc: all
5
+ module Array
6
+ module Behavior
7
+ def extract_options!
8
+ last.is_a?(::Hash) ? pop : {}
9
+ end unless [].respond_to?(:extract_options!)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rqrcode_core/core_ext/integer/bitwise'
4
+
5
+ class Integer #:nodoc: all
6
+ include RQRCodeCore::CoreExtensions::Integer::Bitwise
7
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RQRCodeCore
4
+ module CoreExtensions #:nodoc: all
5
+ module Integer
6
+ module Bitwise
7
+ def rszf(count)
8
+ # zero fill right shift
9
+ (self >> count) & ((2 ** ((self.size * 8) - count))-1)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rqrcode_core/qrcode/qr_8bit_byte'
4
+ require 'rqrcode_core/qrcode/qr_alphanumeric'
5
+ require 'rqrcode_core/qrcode/qr_bit_buffer'
6
+ require 'rqrcode_core/qrcode/qr_code'
7
+ require 'rqrcode_core/qrcode/qr_math'
8
+ require 'rqrcode_core/qrcode/qr_numeric'
9
+ require 'rqrcode_core/qrcode/qr_polynomial'
10
+ require 'rqrcode_core/qrcode/qr_rs_block'
11
+ require 'rqrcode_core/qrcode/qr_util'
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RQRCodeCore
4
+ class QR8bitByte
5
+ attr_reader :mode
6
+
7
+ def initialize( data )
8
+ @mode = QRMODE[:mode_8bit_byte]
9
+ @data = data;
10
+ end
11
+
12
+
13
+ def get_length
14
+ @data.bytesize
15
+ end
16
+
17
+
18
+ def write( buffer)
19
+ buffer.byte_encoding_start(get_length)
20
+ @data.each_byte do |b|
21
+ buffer.put(b, 8)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RQRCodeCore
4
+ ALPHANUMERIC = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ','$','%','*','+','-','.','/',':']
5
+
6
+ class QRAlphanumeric
7
+ attr_reader :mode
8
+
9
+ def initialize( data )
10
+ @mode = QRMODE[:mode_alpha_numk]
11
+
12
+ raise QRCodeArgumentError, "Not a alpha numeric uppercase string `#{data}`" unless QRAlphanumeric.valid_data?(data)
13
+
14
+ @data = data;
15
+ end
16
+
17
+
18
+ def get_length
19
+ @data.size
20
+ end
21
+
22
+ def self.valid_data? data
23
+ data.each_char do |s|
24
+ return false if ALPHANUMERIC.index(s).nil?
25
+ end
26
+ true
27
+ end
28
+
29
+
30
+ def write( buffer)
31
+ buffer.alphanumeric_encoding_start(get_length)
32
+
33
+ (@data.size).times do |i|
34
+ if i % 2 == 0
35
+ if i == (@data.size - 1)
36
+ value = ALPHANUMERIC.index(@data[i])
37
+ buffer.put( value, 6 )
38
+ else
39
+ value = (ALPHANUMERIC.index(@data[i]) * 45) + ALPHANUMERIC.index(@data[i+1])
40
+ buffer.put( value, 11 )
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RQRCodeCore
4
+ class QRBitBuffer
5
+ attr_reader :buffer
6
+
7
+ PAD0 = 0xEC
8
+ PAD1 = 0x11
9
+
10
+ def initialize(version)
11
+ @version = version
12
+ @buffer = []
13
+ @length = 0
14
+ end
15
+
16
+
17
+ def get( index )
18
+ buf_index = (index / 8).floor
19
+ (( (@buffer[buf_index]).rszf(7 - index % 8)) & 1) == 1
20
+ end
21
+
22
+
23
+ def put( num, length )
24
+ ( 0...length ).each do |i|
25
+ put_bit((((num).rszf(length - i - 1)) & 1) == 1)
26
+ end
27
+ end
28
+
29
+
30
+ def get_length_in_bits
31
+ @length
32
+ end
33
+
34
+
35
+ def put_bit( bit )
36
+ buf_index = ( @length / 8 ).floor
37
+ if @buffer.size <= buf_index
38
+ @buffer << 0
39
+ end
40
+
41
+ if bit
42
+ @buffer[buf_index] |= ((0x80).rszf(@length % 8))
43
+ end
44
+
45
+ @length += 1
46
+ end
47
+
48
+ def byte_encoding_start(length)
49
+
50
+ put( QRMODE[:mode_8bit_byte], 4 )
51
+ put(length, QRUtil.get_length_in_bits(QRMODE[:mode_8bit_byte], @version))
52
+
53
+ end
54
+
55
+ def alphanumeric_encoding_start(length)
56
+
57
+ put( QRMODE[:mode_alpha_numk], 4 )
58
+ put(length, QRUtil.get_length_in_bits(QRMODE[:mode_alpha_numk], @version))
59
+
60
+ end
61
+
62
+ def numeric_encoding_start(length)
63
+
64
+ put( QRMODE[:mode_number], 4 )
65
+ put(length, QRUtil.get_length_in_bits(QRMODE[:mode_number], @version))
66
+
67
+ end
68
+
69
+ def pad_until(prefered_size)
70
+ # Align on byte
71
+ while get_length_in_bits % 8 != 0
72
+ put_bit( false )
73
+ end
74
+
75
+ # Pad with padding code words
76
+ while get_length_in_bits < prefered_size
77
+ put( QRBitBuffer::PAD0, 8 )
78
+ put( QRBitBuffer::PAD1, 8 ) if get_length_in_bits < prefered_size
79
+ end
80
+ end
81
+
82
+ def end_of_message(max_data_bits)
83
+ put( 0, 4 ) unless get_length_in_bits+4 > max_data_bits
84
+ end
85
+ end
86
+ end