zint 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ pkg/*
2
+ doc/*
3
+ *.gem
4
+ .bundle
5
+ .yardoc/*
6
+ out.png
7
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in zint.gemspec
4
+ gemspec
@@ -0,0 +1,47 @@
1
+ = Zint
2
+
3
+ -----------
4
+
5
+ This is a Ruby FFI wrapper for the Zint barcode generation library (http://www.zint.org.uk/zintSite). You must have Zint installed on your system for this gem to work at all. Note that this is an alpha release and just gets the essentials done. fork the code and add tests of more barcode coverage.
6
+
7
+
8
+ == Install
9
+
10
+ As mentioned, you must install the zint project to be able to use this gem. Once that dependency is satisfied, then you can just do the usual gem install.
11
+
12
+ gem install zint
13
+
14
+
15
+ == Usage
16
+
17
+ Summary
18
+
19
+ require 'zint'
20
+ # Create a code-128 barcode
21
+ bc = Zint::Barcode.new("test")
22
+ bc.print! # file is now on relative path "out.png"
23
+ bc.path = "my_bc.png"
24
+ bc.print! # file is now on relative path "my_bc.png"
25
+ bc.path = "my_bc.svg"
26
+ bc.print!
27
+ bc.print! # file is now on relative path "my_bc.svg"
28
+
29
+ # create a qrcode
30
+ qrcode = Zint::Barcode.new("test", Zint::BARCODE_QRCODE)
31
+ qrcode.path = "qrcode.png"
32
+ qrcode.print! # file is now on relative path "qrcode.png"
33
+ qrcode_string = qrcode.buffer!
34
+ puts qrcode_string
35
+
36
+ #QRCode with more options, specifically set a high error correction capacity
37
+ qrcode = Zint::QRCode.new("test", Zint::QRCode::ECC_H)
38
+ qrcode.path = "qrcode.ecc_h.png"
39
+ qrcode.print! # file is now on relative path "qrcode.ecc_h.png"
40
+ # encode barcode as text
41
+ qrcode.path = "qrcode.ecc_h.txt"
42
+ puts qrcode.buffer!
43
+
44
+
45
+
46
+
47
+ TODO: Add more documentation
@@ -0,0 +1,15 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ require 'yard'
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << 'lib' << 'test'
9
+ t.pattern = 'test/*_test.rb'
10
+ t.verbose = false
11
+ end
12
+
13
+ YARD::Rake::YardocTask.new do |t|
14
+ t.files = ['lib/**/*.rb', "README.rdoc"] # optional
15
+ end
@@ -0,0 +1,11 @@
1
+ require 'ffi'
2
+ require 'tmpdir'
3
+ require 'zint/version'
4
+ require 'zint/zint_symbol'
5
+ require 'zint/wrapper'
6
+ require 'zint/barcode'
7
+ require 'zint/qrcode'
8
+ require 'zint/upc'
9
+ require 'zint/ean'
10
+ require 'zint/data_matrix'
11
+ require 'zint/exception'
@@ -0,0 +1,62 @@
1
+ module Zint
2
+ # A base class to represent the barcode being encoded
3
+ class Barcode
4
+ # The encoded value of the barcode
5
+ attr_accessor :value
6
+ # The type of barcode generated (e.g. UPC, QRCode, Data Matrix)
7
+ attr_accessor :bctype
8
+ # The output file path
9
+ attr_reader :path
10
+ # Access for the underlying FFI ManagedStruct of the Zint C struct
11
+ attr_reader :zint_symbol
12
+
13
+ def initialize(value, bctype=Zint::BARCODE_CODE128, &options)
14
+ options ||={}
15
+ @zint_symbol = Zint::Wrapper.create(bctype)
16
+ @bctype = bctype
17
+ @value = value
18
+ @encoded = false
19
+ if options[:path]
20
+ @path = options[:path]
21
+ @zint_symbol[:outfile]= @path
22
+ else
23
+ @path = File.join(Dir.pwd, "out.png")
24
+ @zint_symbol[:outfile]= @path
25
+ end
26
+ end
27
+
28
+ def path=(fspath)
29
+ @path = fspath
30
+ @zint_symbol[:outfile] = @path
31
+ @encoded = false
32
+ return @path
33
+ end
34
+
35
+ def encode!
36
+ return if @encoded
37
+ err = Zint::Wrapper.zint_encode(@zint_symbol, @value, 0)
38
+ if Zint::ERR[:err]
39
+ raise Zint::Exception.new(@zint_symbol[:errtxt])
40
+ end
41
+ @encoded = true
42
+ end
43
+
44
+ def print!
45
+ encode!
46
+ err = Zint::Wrapper.zint_print(@zint_symbol, 0)
47
+ if Zint::ERR[:err]
48
+ raise Zint::Exception.new(@zint_symbol[:errtxt])
49
+ end
50
+ end
51
+
52
+ def buffer!
53
+ tmp = File.join(Dir.tmpdir, File.basename(@path))
54
+ @zint_symbol[:outfile] = tmp
55
+ print!
56
+ @zint_symbol[:outfile] = @path
57
+ buffer = File.read(tmp)
58
+ File.unlink(tmp)
59
+ return buffer
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,8 @@
1
+ module Zint
2
+ class DataMatrix < Barcode
3
+ def initialize(value)
4
+ super(value, Zint::BARCODE_DATAMATRIX)
5
+ @zint_symbol[:option_2] = Zint::DM_SQUARE
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Zint
2
+ class Ean < Barcode
3
+ def initialize(value)
4
+ super(value, Zint::BARCODE_EANX)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module Zint
2
+ class Exception < ::Exception
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ module Zint
2
+ class QRCode < Barcode
3
+ ECC_L = 1
4
+ ECC_M = 2
5
+ ECC_Q = 3
6
+ ECC_H = 4
7
+
8
+ def initialize(value, ecc=ECC_L)
9
+ super(value, Zint::BARCODE_QRCODE)
10
+ @zint_symbol[:option_1] = ecc
11
+ return self
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Zint
2
+ class Upc < Barcode
3
+ def initialize(value)
4
+ super(value, Zint::BARCODE_UPCA)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Zint
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,160 @@
1
+ module Zint
2
+
3
+ # Symbologies
4
+ BARCODE_CODE11 = 1
5
+ BARCODE_C25MATRIX = 2
6
+ BARCODE_C25INTER = 3
7
+ BARCODE_C25IATA = 4
8
+ BARCODE_C25LOGIC = 6
9
+ BARCODE_C25IND = 7
10
+ BARCODE_CODE39 = 8
11
+ BARCODE_EXCODE39 = 9
12
+ BARCODE_EANX = 13
13
+ BARCODE_EAN128 = 16
14
+ BARCODE_CODABAR = 18
15
+ BARCODE_CODE128 = 20
16
+ BARCODE_DPLEIT = 21
17
+ BARCODE_DPIDENT = 22
18
+ BARCODE_CODE16K = 23
19
+ BARCODE_CODE49 = 24
20
+ BARCODE_CODE93 = 25
21
+ BARCODE_FLAT = 28
22
+ BARCODE_RSS14 = 29
23
+ BARCODE_RSS_LTD = 30
24
+ BARCODE_RSS_EXP = 31
25
+ BARCODE_TELEPEN = 32
26
+ BARCODE_UPCA = 34
27
+ BARCODE_UPCE = 37
28
+ BARCODE_POSTNET = 40
29
+ BARCODE_MSI_PLESSEY = 47
30
+ BARCODE_FIM = 49
31
+ BARCODE_LOGMARS = 50
32
+ BARCODE_PHARMA = 51
33
+ BARCODE_PZN = 52
34
+ BARCODE_PHARMA_TWO = 53
35
+ BARCODE_PDF417 = 55
36
+ BARCODE_PDF417TRUNC = 56
37
+ BARCODE_MAXICODE = 57
38
+ BARCODE_QRCODE = 58
39
+ BARCODE_CODE128B = 60
40
+ BARCODE_AUSPOST = 63
41
+ BARCODE_AUSREPLY = 66
42
+ BARCODE_AUSROUTE = 67
43
+ BARCODE_AUSREDIRECT = 68
44
+ BARCODE_ISBNX = 69
45
+ BARCODE_RM4SCC = 70
46
+ BARCODE_DATAMATRIX = 71
47
+ BARCODE_EAN14 = 72
48
+ BARCODE_CODABLOCKF = 74
49
+ BARCODE_NVE18 = 75
50
+ BARCODE_JAPANPOST = 76
51
+ BARCODE_KOREAPOST = 77
52
+ BARCODE_RSS14STACK = 79
53
+ BARCODE_RSS14STACK_OMNI = 80
54
+ BARCODE_RSS_EXPSTACK = 81
55
+ BARCODE_PLANET = 82
56
+ BARCODE_MICROPDF417 = 84
57
+ BARCODE_ONECODE = 85
58
+ BARCODE_PLESSEY = 86
59
+ BARCODE_TELEPEN_NUM = 87
60
+ BARCODE_ITF14 = 89
61
+ BARCODE_KIX = 90
62
+ BARCODE_AZTEC = 92
63
+ BARCODE_DAFT = 93
64
+ BARCODE_MICROQR = 97
65
+ BARCODE_HIBC_128 = 98
66
+ BARCODE_HIBC_39 = 99
67
+ BARCODE_HIBC_DM = 102
68
+ BARCODE_HIBC_QR = 104
69
+ BARCODE_HIBC_PDF = 106
70
+ BARCODE_HIBC_MICPDF = 108
71
+ BARCODE_HIBC_BLOCKF = 110
72
+ BARCODE_HIBC_AZTEC = 112
73
+ BARCODE_AZRUNE = 128
74
+ BARCODE_CODE32 = 129
75
+ BARCODE_EANX_CC = 130
76
+ BARCODE_EAN128_CC = 131
77
+ BARCODE_RSS14_CC = 132
78
+ BARCODE_RSS_LTD_CC = 133
79
+ BARCODE_RSS_EXP_CC = 134
80
+ BARCODE_UPCA_CC = 135
81
+ BARCODE_UPCE_CC = 136
82
+ BARCODE_RSS14STACK_CC = 137
83
+ BARCODE_RSS14_OMNI_CC = 138
84
+ BARCODE_RSS_EXPSTACK_CC = 139
85
+ BARCODE_CHANNEL = 140
86
+ BARCODE_CODEONE = 141
87
+ BARCODE_GRIDMATRIX = 142
88
+
89
+ # barcode output options
90
+ BARCODE_NO_ASCII = 1
91
+ BARCODE_BIND = 2
92
+ BARCODE_BOX = 4
93
+
94
+ # Even thought this an option, it foobar's
95
+ # Ruby's $stdout handle. Don't use it.
96
+ # BARCODE_STDOUT = 8
97
+ BARCODE_READER_INIT = 16
98
+
99
+ # input options
100
+ DATA_MODE = 0
101
+ UNICODE_MODE = 1
102
+ GS1_MODE = 2
103
+ KANJI_MODE = 3
104
+ SJIS_MODE = 4
105
+ DM_SQUARE = 100
106
+
107
+
108
+ # error codes
109
+ WARN = {:warn_invalid_option => 2 }
110
+ ERR = {:error_too_long => 5 ,
111
+ :error_invalid_data => 6 ,
112
+ :error_invalid_check => 7 ,
113
+ :error_invalid_option => 8 ,
114
+ :error_encoding_problem => 9 ,
115
+ :error_file_access => 10,
116
+ :error_memory => 11}
117
+
118
+ # This is the FFI wrapper on the Zint C library. You should not use
119
+ # this directly. Instead refer to Zint::Barcode and its decendents
120
+ module Wrapper
121
+ extend FFI::Library
122
+ ffi_lib "libzint"
123
+
124
+ #error codes
125
+ enum :err, [:warn_invalid_option, 2,
126
+ :error_too_long, 5,
127
+ :error_invalid_data, 6,
128
+ :error_invalid_check, 7,
129
+ :error_invalid_option, 8,
130
+ :error_encoding_problem, 9,
131
+ :error_file_access, 10,
132
+ :error_memory, 11
133
+ ]
134
+
135
+ # some typedef aliases, for better method signatures
136
+ typedef :pointer, :zint_symbol
137
+ typedef :int32, :string_max_length
138
+ typedef :int32, :rotate_angle
139
+ typedef :string, :input
140
+
141
+ attach_function(:zint_create, :ZBarcode_Create, [], :zint_symbol)
142
+ attach_function(:zint_clear, :ZBarcode_Clear, [:zint_symbol], :void)
143
+ attach_function(:zint_delete, :ZBarcode_Delete, [:zint_symbol], :void)
144
+ attach_function(:zint_encode, :ZBarcode_Encode,
145
+ [:zint_symbol, :input, :string_max_length],
146
+ :err)
147
+ attach_function(:zint_print, :ZBarcode_Print,
148
+ [:zint_symbol, :rotate_angle],
149
+ :err)
150
+ attach_function(:zint_encode_and_print, :ZBarcode_Encode_and_Print,
151
+ [:zint_symbol, :input, :string_max_length,
152
+ :rotate_angle], :err)
153
+
154
+ def self.create(bctype=BARCODE_CODE128)
155
+ bc = Zint::ZintSymbol.new(self.zint_create())
156
+ bc[:symbology] = bctype
157
+ return bc
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,36 @@
1
+ module Zint
2
+ # An FFI wrapper for the Zint C library zint_symbol struct.
3
+ # You should not use this directly, use the Zint::Barcode or
4
+ # its decendents instead.
5
+ class ZintSymbol < FFI::ManagedStruct
6
+ layout :symbology, :int32,
7
+ :height, :int32,
8
+ :whitespace_width , :int32,
9
+ :border_width , :int32,
10
+ :output_options , :int32,
11
+ :fgcolour, [:char, 10],
12
+ :bgcolour, [:char, 10],
13
+ :outfile, [:char, 256],
14
+ :scale, :float,
15
+ :option_1, :int32,
16
+ :option_2, :int32,
17
+ :option_3, :int32,
18
+ :show_hrt, :int32,
19
+ :input_mode, :int32,
20
+ :text, [:uchar, 128],
21
+ :rows, :int32,
22
+ :width, :int32,
23
+ :primary, [:char, 128],
24
+ :encoded_data , [:uchar, 178*143],
25
+ :row_height, [:int32, 178],
26
+ :errtxt, [:char, 100],
27
+ :bitmap, :pointer,
28
+ :bitmap_width, :int32,
29
+ :bitmap_height, :int32
30
+
31
+ # release method required for FFI managed structs
32
+ def self.release ptr
33
+ Zint.zint_delete(ptr)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,128 @@
1
+ %!PS-Adobe-3.0 EPSF-3.0
2
+ %%Creator: Zint 2.4.0
3
+ %%Title: http://rubygems.org/gems/zint
4
+ %%Pages: 0
5
+ %%BoundingBox: 0 0 354 59
6
+ %%EndComments
7
+ /TL { setlinewidth moveto lineto stroke } bind def
8
+ /TC { moveto 0 360 arc 360 0 arcn fill } bind def
9
+ /TH { 0 setlinewidth moveto lineto lineto lineto lineto lineto closepath fill } bind def
10
+ /TB { 2 copy } bind def
11
+ /TR { newpath 4 1 roll exch moveto 1 index 0 rlineto 0 exch rlineto neg 0 rlineto closepath fill } bind def
12
+ /TE { pop pop } bind def
13
+ newpath
14
+ 0.00 0.00 0.00 setrgbcolor
15
+ 1.00 1.00 1.00 setrgbcolor
16
+ 59.00 0.00 TB 0.00 354.00 TR
17
+ TE
18
+ 0.00 0.00 0.00 setrgbcolor
19
+ 50.00 9.00 TB 0.00 2.00 TR
20
+ TB 3.00 1.00 TR
21
+ TB 6.00 1.00 TR
22
+ TB 11.00 1.00 TR
23
+ TB 14.00 2.00 TR
24
+ TB 20.00 1.00 TR
25
+ TB 22.00 1.00 TR
26
+ TB 25.00 4.00 TR
27
+ TB 30.00 1.00 TR
28
+ TB 33.00 1.00 TR
29
+ TB 36.00 4.00 TR
30
+ TB 41.00 1.00 TR
31
+ TB 44.00 1.00 TR
32
+ TB 46.00 1.00 TR
33
+ TB 49.00 4.00 TR
34
+ TB 55.00 3.00 TR
35
+ TB 60.00 1.00 TR
36
+ TB 63.00 2.00 TR
37
+ TB 66.00 1.00 TR
38
+ TB 68.00 3.00 TR
39
+ TB 73.00 2.00 TR
40
+ TB 77.00 1.00 TR
41
+ TB 79.00 3.00 TR
42
+ TB 84.00 2.00 TR
43
+ TB 88.00 1.00 TR
44
+ TB 91.00 1.00 TR
45
+ TB 94.00 4.00 TR
46
+ TB 99.00 1.00 TR
47
+ TB 102.00 4.00 TR
48
+ TB 108.00 1.00 TR
49
+ TB 110.00 1.00 TR
50
+ TB 113.00 1.00 TR
51
+ TB 118.00 2.00 TR
52
+ TB 121.00 2.00 TR
53
+ TB 124.00 2.00 TR
54
+ TB 127.00 4.00 TR
55
+ TB 132.00 1.00 TR
56
+ TB 135.00 2.00 TR
57
+ TB 138.00 1.00 TR
58
+ TB 143.00 1.00 TR
59
+ TB 145.00 2.00 TR
60
+ TB 149.00 1.00 TR
61
+ TB 154.00 4.00 TR
62
+ TB 159.00 3.00 TR
63
+ TB 163.00 1.00 TR
64
+ TB 165.00 1.00 TR
65
+ TB 167.00 4.00 TR
66
+ TB 173.00 1.00 TR
67
+ TB 176.00 1.00 TR
68
+ TB 179.00 2.00 TR
69
+ TB 183.00 3.00 TR
70
+ TB 187.00 1.00 TR
71
+ TB 191.00 4.00 TR
72
+ TB 196.00 1.00 TR
73
+ TB 198.00 1.00 TR
74
+ TB 201.00 1.00 TR
75
+ TB 204.00 4.00 TR
76
+ TB 209.00 1.00 TR
77
+ TB 212.00 2.00 TR
78
+ TB 215.00 1.00 TR
79
+ TB 220.00 1.00 TR
80
+ TB 222.00 3.00 TR
81
+ TB 227.00 2.00 TR
82
+ TB 231.00 1.00 TR
83
+ TB 234.00 2.00 TR
84
+ TB 237.00 1.00 TR
85
+ TB 242.00 1.00 TR
86
+ TB 244.00 2.00 TR
87
+ TB 248.00 1.00 TR
88
+ TB 253.00 4.00 TR
89
+ TB 258.00 3.00 TR
90
+ TB 262.00 1.00 TR
91
+ TB 264.00 1.00 TR
92
+ TB 266.00 4.00 TR
93
+ TB 272.00 1.00 TR
94
+ TB 275.00 1.00 TR
95
+ TB 277.00 3.00 TR
96
+ TB 282.00 2.00 TR
97
+ TB 286.00 2.00 TR
98
+ TB 289.00 4.00 TR
99
+ TB 294.00 2.00 TR
100
+ TB 297.00 1.00 TR
101
+ TB 302.00 2.00 TR
102
+ TB 305.00 1.00 TR
103
+ TB 308.00 2.00 TR
104
+ TB 314.00 1.00 TR
105
+ TB 316.00 1.00 TR
106
+ TB 319.00 1.00 TR
107
+ TB 322.00 4.00 TR
108
+ TB 327.00 1.00 TR
109
+ TB 330.00 4.00 TR
110
+ TB 335.00 1.00 TR
111
+ TB 337.00 1.00 TR
112
+ TB 341.00 2.00 TR
113
+ TB 346.00 3.00 TR
114
+ TB 350.00 1.00 TR
115
+ TB 352.00 2.00 TR
116
+ TE
117
+ 0.00 0.00 0.00 setrgbcolor
118
+ matrix currentmatrix
119
+ /Helvetica findfont
120
+ 8.00 scalefont setfont
121
+ 0 0 moveto 177.00 0.50 translate 0.00 rotate 0 0 moveto
122
+ (http://rubygems.org/gems/zint) stringwidth
123
+ pop
124
+ -2 div 0 rmoveto
125
+ (http://rubygems.org/gems/zint) show
126
+ setmatrix
127
+
128
+ showpage