syc-barcode 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ = Barcode
2
+ Creating barcodes (at the moment only Interleave2of5).
3
+
4
+ == Usage
5
+ Create the barcode string and the barcode graphics data
6
+ i2o5 = Interleave2of5("01199")
7
+ code = i2o5.encode
8
+ barcode = i2o5.barcode
9
+ pdf = Prawn::Document.new
10
+ barcode.to_pdf(pdf)
11
+
12
+ The barcode can be used to create a graphical representation of the barcode.
13
+
14
+ == Licencse
15
+ Barcode is published under the MIT license
@@ -0,0 +1,19 @@
1
+ require_relative 'visual'
2
+
3
+ # Barcode is the base class for barcodes it requires subclasses to implement
4
+ # #encode and #barcode
5
+ class Barcode
6
+
7
+ include Visual
8
+
9
+ # Encodes the string into a barcode
10
+ def encode
11
+ raise "needs to be implemented by sub class"
12
+ end
13
+
14
+ # Creates the coordinates for a barcode
15
+ def barcode
16
+ raise "needs to be implemented by sub class"
17
+ end
18
+
19
+ end
@@ -0,0 +1,24 @@
1
+ # Checked attributes are checked for validity
2
+ module CheckedAttributes
3
+
4
+ # Includes base
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ # Creates the attr_checked method
10
+ module ClassMethods
11
+
12
+ # attr_checked evaluates whether the value is valid
13
+ def attr_checked(attribute, &validation)
14
+ define_method "#{attribute}=" do |value|
15
+ raise ArgumentError unless validation.call(value)
16
+ instance_variable_set("@#{attribute}", value)
17
+ end
18
+
19
+ define_method attribute do
20
+ instance_variable_get "@#{attribute}"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,120 @@
1
+ require_relative 'checked_attributes'
2
+ require_relative 'barcode'
3
+
4
+ # Creates a Interleave 2 of 5 barcode
5
+ class Interleave2of5 < Barcode
6
+ include CheckedAttributes
7
+
8
+ # Holds the options for an Interleave 2 of 5 barcode
9
+ OPTIONS = {
10
+ x: 0,
11
+ y: 0,
12
+ width: 1,
13
+ factor: 2,
14
+ quiet: 20,
15
+ bearer: :none, # possible selections :none, :frame, :rule
16
+ bearer_width: 1,
17
+ top_margin: 10,
18
+ bottom_margin: 10,
19
+ height: 50
20
+ }
21
+
22
+ # Symbology the translates numbers to barcode symbology
23
+ SYMBOLOGY = {
24
+ start: "0000",
25
+ stop: "100",
26
+ "0" => "00110",
27
+ "1" => "10001",
28
+ "2" => "01001",
29
+ "3" => "11000",
30
+ "4" => "00101",
31
+ "5" => "10100",
32
+ "6" => "01100",
33
+ "7" => "00011",
34
+ "8" => "10010",
35
+ "9" => "01010"
36
+ }
37
+
38
+ attr_checked :value do
39
+ |v| v.scan(/\D/).empty?
40
+ end
41
+
42
+ attr_accessor :check, :options
43
+ attr_reader :code, :encodable, :graph
44
+
45
+ # Takes a value and whether a check digit is required
46
+ def initialize(value, check=true)
47
+ raise ArgumentError unless value.scan(/\D/).empty?
48
+ @value = value
49
+ @check = check
50
+ end
51
+
52
+ # Prints the values of Interleave2of5
53
+ def to_s
54
+ "value: #{@value}\n"+
55
+ "options: #{@options.inspect}\n"+
56
+ "encodable: #{@encodable ? @encodable : '#encode not run yet'}\n"+
57
+ "code: #{@code}\n"
58
+ end
59
+
60
+ # Encodes the value string to a barcode sequence consistent of 0 and 1
61
+ def encode
62
+ @code = ""
63
+ add_check_digit_and_ensure_even_digit_size
64
+ 0.step(@encodable.size - 1, 2) do |i|
65
+ first = SYMBOLOGY[@encodable[i]]
66
+ second = SYMBOLOGY[@encodable[i+1]]
67
+ @code += 0.upto(4).map { |j| first[j] + second[j] }.join
68
+ end
69
+ @code = SYMBOLOGY[:start] + @code + SYMBOLOGY[:stop]
70
+ self
71
+ end
72
+
73
+ # Creates the barcode coordinates for a visual representation
74
+ def barcode(options={})
75
+ @options = OPTIONS.merge(options)
76
+
77
+ y_offset = @options[:bearer] != :none ? @options[:bearer_width] : 0
78
+ x_offset = @options[:bearer] == :frame ? @options[:bearer_width] : 0
79
+
80
+ @graph = {
81
+ total_width: 2 * x_offset + 2 * @options[:quiet],
82
+ total_height: @options[:height] + 2 * y_offset + @options[:top_margin] +
83
+ @options[:bottom_margin],
84
+ x: @options[:x] + @options[:quiet] + x_offset,
85
+ y: @options[:y] + @options[:bottom_margin] + y_offset,
86
+ height: @options[:height],
87
+ bars: [],
88
+ graph_width: 0
89
+ }
90
+
91
+ x = @graph[:x]
92
+
93
+ @code.each_char.with_index do |v, i|
94
+ width = @options[:width]
95
+ if v == "1"
96
+ width = width * @options[:factor]
97
+ end
98
+ @graph[:bars] << { x: x, width: width } if i.even?
99
+ x += width
100
+ @graph[:graph_width] += width
101
+ end
102
+
103
+ @graph[:total_width] += @graph[:graph_width]
104
+ @graph
105
+ end
106
+
107
+ private
108
+
109
+ # Adds a check digit if check is true and ensures even digit size
110
+ def add_check_digit_and_ensure_even_digit_size
111
+ reverse_value = @value.split('').reverse
112
+ @check_digit = 10 - reverse_value.each_with_index.collect do |v, i|
113
+ i.even? ? v.to_i * 3 : v.to_i
114
+ end.inject(:+) % 10 if @check
115
+ @encodable = @value + (@check ? @check_digit.to_s : "")
116
+ @encodable = "0" + @encodable if @encodable.size.odd?
117
+ @encodable
118
+ end
119
+
120
+ end
@@ -0,0 +1,41 @@
1
+ require 'prawn'
2
+
3
+ # Methods to visualize a barcode
4
+ module Visual
5
+
6
+ # Adds a barcode to a pdf file
7
+ def to_pdf(pdf, options={})
8
+ pdf.undash
9
+ barcode(options)
10
+ @graph[:bars].each do |b|
11
+ pdf.move_to(b[:x], @graph[:y])
12
+ pdf.line_to(b[:x], @graph[:y] + @graph[:height])
13
+ pdf.line_to(b[:x] + b[:width], @graph[:y] + @graph[:height])
14
+ pdf.line_to(b[:x] + b[:width], @graph[:y])
15
+ pdf.line_to(b[:x], @graph[:y])
16
+ pdf.fill
17
+ end
18
+ if @options[:bearer] == :frame
19
+ pdf.move_to(@options[:x], @graph[:y])
20
+ pdf.line_to(@options[:x],
21
+ @graph[:y] + @graph[:height])
22
+ pdf.line_to(@options[:x] + 2 * @options[:quiet] + @graph[:graph_width],
23
+ @graph[:y] + @graph[:height])
24
+ pdf.line_to(@options[:x] + 2 * @options[:quiet] + @graph[:graph_width],
25
+ @graph[:y])
26
+ pdf.line_to(@options[:x],
27
+ @graph[:y])
28
+ elsif @options[:bearer] == :rule
29
+ pdf.move_to(@options[:x],
30
+ @options[:y] + @options[:bottom_margin] + @graph[:height])
31
+ pdf.line_to(@options[:x] + 2 * @options[:quiet] + @graph[:graph_width],
32
+ @options[:y] + @options[:bottom_margin] + @graph[:height])
33
+ pdf.move_to(@options[:x] + 2 * @options[:quiet] + @graph[:graph_width],
34
+ @options[:y] + @options[:bottom_margin])
35
+ pdf.line_to(@options[:x],
36
+ @options[:y] + @options[:bottom_margin])
37
+ end
38
+ @graph
39
+ end
40
+
41
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syc-barcode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pierre Sugar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: prawn
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! "= Barcode\nCreating barcodes (at the moment only Interleave2of5).
63
+ \n\n== Usage\nCreate the barcode string and the barcode graphics data\n i2o5
64
+ = Interleave2of5(\"01199\")\n code = i2o5.encode\n barcode = i2o5.barcode\n
65
+ \ pdf = Prawn::Document.new\n barcode.to_pdf(pdf)\n\nThe barcode can be used
66
+ to create a graphical representation of the barcode.\n\n== Licencse\nBarcode is
67
+ published under the MIT license\n"
68
+ email: pierre@sugaryourcoffee.de
69
+ executables: []
70
+ extensions: []
71
+ extra_rdoc_files:
72
+ - README.rdoc
73
+ files:
74
+ - lib/barcode.rb
75
+ - lib/checked_attributes.rb
76
+ - lib/interleave2of5.rb
77
+ - lib/visual.rb
78
+ - README.rdoc
79
+ homepage: http://syc.dyndns.org/drupal
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options:
83
+ - --title
84
+ - sycbarcode
85
+ - --main
86
+ - README.rdoc
87
+ - -ri
88
+ require_paths:
89
+ - lib
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 1.8.25
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Simple barcode creator
109
+ test_files: []