jmcnevin-rghost_barcode 0.8.5 → 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,248 @@
1
+
2
+
3
+ RGhost::Config::GS[:extensions] << File.join(File.dirname(__FILE__),'ps')
4
+ RGhost::Config::GS[:preload] << :barcode
5
+
6
+ class RGhost::Document
7
+
8
+ #Facade for RGhost::Barcode::Isbn
9
+ def barcode_isbn(barcode_string,options={})
10
+ set RGhost::Barcode::Isbn.new(barcode_string,options)
11
+ end
12
+
13
+ #Facade for RGhost::Barcode::Code11
14
+ def barcode_code11(barcode_string,options={})
15
+ set RGhost::Barcode::Code11.new(barcode_string,options)
16
+ end
17
+
18
+ #Facade for RGhost::Barcode::Code2of5
19
+ def barcode_code2of5(barcode_string,options={})
20
+ set RGhost::Barcode::Code2of5.new(barcode_string,options)
21
+ end
22
+
23
+ #Facade for RGhost::Barcode::Ean8
24
+ def barcode_ean8(barcode_string,options={})
25
+ set RGhost::Barcode::Ean8.new(barcode_string,options)
26
+ end
27
+
28
+ #Facade for RGhost::Barcode::Postnet
29
+ def barcode_postnet(barcode_string,options={})
30
+ set RGhost::Barcode::Postnet.new(barcode_string,options)
31
+ end
32
+
33
+ #Facade for RGhost::Barcode::Rssexpanded
34
+ def barcode_rssexpanded(barcode_string,options={})
35
+ set RGhost::Barcode::Rssexpanded.new(barcode_string,options)
36
+ end
37
+
38
+ #Facade for RGhost::Barcode::Auspost
39
+ def barcode_auspost(barcode_string,options={})
40
+ set RGhost::Barcode::Auspost.new(barcode_string,options)
41
+ end
42
+
43
+ #Facade for RGhost::Barcode::Code39
44
+ def barcode_code39(barcode_string,options={})
45
+ set RGhost::Barcode::Code39.new(barcode_string,options)
46
+ end
47
+
48
+ #Facade for RGhost::Barcode::Upce
49
+ def barcode_upce(barcode_string,options={})
50
+ set RGhost::Barcode::Upce.new(barcode_string,options)
51
+ end
52
+
53
+ #Facade for RGhost::Barcode::Azteccode
54
+ def barcode_azteccode(barcode_string,options={})
55
+ set RGhost::Barcode::Azteccode.new(barcode_string,options)
56
+ end
57
+
58
+ #Facade for RGhost::Barcode::Ean5
59
+ def barcode_ean5(barcode_string,options={})
60
+ set RGhost::Barcode::Ean5.new(barcode_string,options)
61
+ end
62
+
63
+ #Facade for RGhost::Barcode::Plessey
64
+ def barcode_plessey(barcode_string,options={})
65
+ set RGhost::Barcode::Plessey.new(barcode_string,options)
66
+ end
67
+
68
+ #Facade for RGhost::Barcode::Raw
69
+ def barcode_raw(barcode_string,options={})
70
+ set RGhost::Barcode::Raw.new(barcode_string,options)
71
+ end
72
+
73
+ #Facade for RGhost::Barcode::Rss14
74
+ def barcode_rss14(barcode_string,options={})
75
+ set RGhost::Barcode::Rss14.new(barcode_string,options)
76
+ end
77
+
78
+ #Facade for RGhost::Barcode::Royalmail
79
+ def barcode_royalmail(barcode_string,options={})
80
+ set RGhost::Barcode::Royalmail.new(barcode_string,options)
81
+ end
82
+
83
+ #Facade for RGhost::Barcode::Datamatrix
84
+ def barcode_datamatrix(barcode_string,options={})
85
+ set RGhost::Barcode::Datamatrix.new(barcode_string,options)
86
+ end
87
+
88
+ #Facade for RGhost::Barcode::Code128
89
+ def barcode_code128(barcode_string,options={})
90
+ set RGhost::Barcode::Code128.new(barcode_string,options)
91
+ end
92
+
93
+ #Facade for RGhost::Barcode::Upca
94
+ def barcode_upca(barcode_string,options={})
95
+ set RGhost::Barcode::Upca.new(barcode_string,options)
96
+ end
97
+
98
+ #Facade for RGhost::Barcode::Qrcode
99
+ def barcode_qrcode(barcode_string,options={})
100
+ set RGhost::Barcode::Qrcode.new(barcode_string,options)
101
+ end
102
+
103
+ #Facade for RGhost::Barcode::Maxicode
104
+ def barcode_maxicode(barcode_string,options={})
105
+ set RGhost::Barcode::Maxicode.new(barcode_string,options)
106
+ end
107
+
108
+ #Facade for RGhost::Barcode::Rationalizedcodabar
109
+ def barcode_rationalizedcodabar(barcode_string,options={})
110
+ set RGhost::Barcode::Rationalizedcodabar.new(barcode_string,options)
111
+ end
112
+
113
+ #Facade for RGhost::Barcode::Code93
114
+ def barcode_code93(barcode_string,options={})
115
+ set RGhost::Barcode::Code93.new(barcode_string,options)
116
+ end
117
+
118
+ #Facade for RGhost::Barcode::Msi
119
+ def barcode_msi(barcode_string,options={})
120
+ set RGhost::Barcode::Msi.new(barcode_string,options)
121
+ end
122
+
123
+ #Facade for RGhost::Barcode::Rsslimited
124
+ def barcode_rsslimited(barcode_string,options={})
125
+ set RGhost::Barcode::Rsslimited.new(barcode_string,options)
126
+ end
127
+
128
+ #Facade for RGhost::Barcode::Ean13
129
+ def barcode_ean13(barcode_string,options={})
130
+ set RGhost::Barcode::Ean13.new(barcode_string,options)
131
+ end
132
+
133
+ #Facade for RGhost::Barcode::Onecode
134
+ def barcode_onecode(barcode_string,options={})
135
+ set RGhost::Barcode::Onecode.new(barcode_string,options)
136
+ end
137
+
138
+ #Facade for RGhost::Barcode::Ean2
139
+ def barcode_ean2(barcode_string,options={})
140
+ set RGhost::Barcode::Ean2.new(barcode_string,options)
141
+ end
142
+
143
+ #Facade for RGhost::Barcode::Interleaved2of5
144
+ def barcode_interleaved2of5(barcode_string,options={})
145
+ set RGhost::Barcode::Interleaved2of5.new(barcode_string,options)
146
+ end
147
+
148
+ #Facade for RGhost::Barcode::Symbol
149
+ def barcode_symbol(barcode_string,options={})
150
+ set RGhost::Barcode::Symbol.new(barcode_string,options)
151
+ end
152
+
153
+ #Facade for RGhost::Barcode::Pdf417
154
+ def barcode_pdf417(barcode_string,options={})
155
+ set RGhost::Barcode::Pdf417.new(barcode_string,options)
156
+ end
157
+
158
+ #Facade for RGhost::Barcode::Kix
159
+ def barcode_kix(barcode_string,options={})
160
+ set RGhost::Barcode::Kix.new(barcode_string,options)
161
+ end
162
+
163
+ #Facade for RGhost::Barcode::Pharmacode
164
+ def barcode_pharmacode(barcode_string,options={})
165
+ set RGhost::Barcode::Pharmacode.new(barcode_string,options)
166
+ end
167
+
168
+
169
+ end
170
+
171
+ module RGhost::Config
172
+ BARCODE_EXAMPLES={
173
+ 'plessey' => ['01234ABCD',{ :text => {:size => 10}}],
174
+ 'isbn' => ['978-1-56592-479',{:text => {:size => 10}, :guard=>{:whitespace => true }}],
175
+ 'rsslimited' => ['00978186074271',{:height => 1 , :text => {:size => 10}}],
176
+ 'pdf417' => ['^453^178^121^239',{:text => {:size => 10}, :columns => 2, :rows=> 10}],
177
+ 'royalmail' => ['LE28HS9Z',{:text => {:size => 10}}],
178
+ 'ean13' => ['977147396801',{ :guard=>{:whitespace => true }}],
179
+ 'code11' => ['0123456789',{:text => {:size => 10}}],
180
+ 'symbol' => ['fima',{:text => {:size => 10}}],
181
+ 'qrcode' => ['000100000010000000001100010101100110000110000',{:text => {:size => 10}}],
182
+ 'msi' => ['0123456789',{:text => {:size => 10}}],
183
+ 'upca' => ['78858101497',{:text => {:size => 10}}],
184
+ 'pharmacode' => ['117480',{:text => {:size => 10}}],
185
+ 'kix' => ['1231FZ13XHS',{:text => {:size => 10}}],
186
+ 'datamatrix' => ['^142^164^186',{:columns => 32, :rows=> 32}],
187
+ 'rssexpanded' => ['000000010011001010100001000000010000',{:text => {:size => 10}}],
188
+ 'ean2' => ['05',{:text => {:size => 10}}],
189
+ 'rationalizedcodabar' => ['A0123456789B',{:text => {:size => 10}}],
190
+ 'upce' => ['0123456',{:text => {:size => 10}}],
191
+ 'ean5' => ['90200',{:text => {:size => 10}}],
192
+ 'code2of5' => ['01234567',{:text => {:size => 10}}],
193
+ 'code93' => ['THIS IS CODE 93',{:text => {:size => 10}}],
194
+ 'postnet' => ['012345',{:text => {:size => 10}}],
195
+ 'maxicode' => ['^059^042^041^059^040^03001^02996152382802^029840^029001^0291Z00004951^029UPSN^02906X610^029159^0291234567^0291^0471^029^029Y^029634 ALPHA DR^029PITTSBURGH^029PA^030^062^004^063', {:mode=> 2, :text => {:size => 10}}],
196
+ 'raw' => ['331132131313411122131311333213114131131221323',{:text => {:size => 10}}],
197
+ 'onecode' => ['0123456709498765432101234567891',{:text => {:size => 10}}],
198
+ 'code39' => ['THIS IS CODE 39',{:text => {:size => 10}}],
199
+ 'auspost' => ['5956439111ABA 9',{:text => {:size => 10}}],
200
+ 'azteccode' => ['00100111001000000101001101111000010100111100101000000110',{:text => {:size => 10}, :format=> :compact}],
201
+ 'ean8' => ['01335583',{:text => {:size => 10}}],
202
+ 'interleaved2of5' => ['24012345678905',{
203
+ :text => {:size => 10, :offset => [0,-10], :enable => [:text, :check, :checkintext] },
204
+ :border => {:width => 4, :left => 15, :right => 15, :show => true},
205
+ :height => 2
206
+ }],
207
+ 'code128' => ['^104^102Count^0990123456789^101!',{:text => {:size => 10}}],
208
+ 'rss14' => ['24012345678905',{:text => {:size => 10}}]
209
+
210
+
211
+ }
212
+ #Generates catalog of barcodes.
213
+ #
214
+ # RGhost::Config.barcode_examples.render :pdf, :filename => "/tmp/barcode_catalog.pdf"
215
+ #
216
+ def self.barcode_examples() #(options={:text => {:size => 10}})
217
+ d=RGhost::Document.new :paper => [15,8], :margin_left => 2
218
+ #d.set RGhost::Barcode::Code39.new("ASDHAJSDF", :x => 1, :y => 5, :color => 'FF0000', :border =>{:color => '00FF00'}, :text =>{:size => 10, :color => '553297' }, :height => 1, :width => 4.545)
219
+ #d.barcode_isbn("978-1-56592-479", :x => 1, :y => 5, :color => 'FF0000', :border =>{:color => '00FF00'}, :text =>{:size => 10, :color => '553297' }, :height => 1, :width => 4.545)
220
+ #d.barcode_isbn("978-1-56592-479", :x => 1, :y => 10, :text =>{:size => 10})
221
+ d.define_template :barcode_template, File.join(File.dirname(__FILE__),'ps',"barcode_template.eps")
222
+ d.before_page_create do
223
+ use_template :barcode_template
224
+ end
225
+ RGhost::Config::BARCODE_EXAMPLES.each do |k,v|
226
+
227
+ d.show "Barcode Name: #{k}"
228
+ d.jump_rows 6
229
+ #opt=v[1][:text][:size]=10
230
+ opt=v[1]
231
+ d.send "barcode_#{k}".to_sym,v[0],opt.dup
232
+ d.jump_rows 2
233
+ d.show "Code String: #{v[0]}"
234
+ d.next_row
235
+ d.text("Options: #{opt.inspect}",false)
236
+ d.next_page
237
+ end
238
+ license=%Q{<big>Barcode Writer License</big><br/>Anybody is free to use and develop this code as they see fit, under the terms of the permissive MIT/X-Consortium license(http://postscriptbarcode.googlecode.com/svn/trunk/LICENSE), which requires that developers attribute any derivative code to this project in both the source code and documentation. As such I take no liability for its use or misuse.<br/><br/>Thanks a lot Terry!<br/> <i>Shairon Toledo</i> }
239
+ d.text license
240
+ d.next_row
241
+ d.show "RGhost::VERSION #{RGhost::VERSION::STRING}" , :align => :page_right, :tag => :b
242
+ d.next_row
243
+ d.show "RGhost::Barcode::VERSION #{RGhost::Barcode::VERSION::STRING}" , :align => :page_right, :tag => :b
244
+ d
245
+ end
246
+
247
+ end
248
+
@@ -0,0 +1,210 @@
1
+ module RGhost::Barcode
2
+
3
+ class Parameter < RGhost::PsObject
4
+
5
+ def initialize(options=nil)
6
+ @options=options
7
+ @params=[]
8
+ make() if options
9
+ end
10
+ def map
11
+ @params.join(" ")
12
+ end
13
+
14
+ def add(key,value)
15
+ @params << "#{key}=#{value}" if value
16
+ end
17
+
18
+ def add_color(key,value)
19
+ add(key, value.to_s.gsub(/[^0-9A-F]/,'') )
20
+ end
21
+ def add_single(entry)
22
+ @params << entry
23
+ end
24
+
25
+ #using the framework unit
26
+ def add_with_unit(key,value)
27
+
28
+
29
+
30
+ v=if RGhost::Config::GS[:unit] == RGhost::Units::Cm
31
+ (value.to_f/2.545)
32
+ end
33
+ add(key, v || value )
34
+ end
35
+
36
+ def inspect
37
+ "#{self.class} - #{map}"
38
+ end
39
+ def to_s
40
+ map
41
+ end
42
+ def make() end #prototype
43
+
44
+
45
+ end
46
+
47
+
48
+ #Enables properties of text. It's used on the contructor of the class RGhost::Barcode::Base(parent of all barcodes) by Hash :text.
49
+ #Between parenthesis the original parameter.
50
+ #* <tt>:size</tt> - (textsize) - size of text in points.
51
+ #* <tt>:font</tt> - (textfont) - name of font, see the font catalog.
52
+ #* <tt>:offset</tt> - (textxoffset, textyoffset) - Used as array. Example :offset => [10,8], textxoffset[0] and textyoffset[1]
53
+ #* <tt>:x and :y</tt> - (textxalign, textyalign) - Align of X and Y. Example :x => 0.2, :y => 5
54
+ #* <tt>:color</tt> - (textcolor) - Color of text. Only rgb hex. Example: :color => 'FF0000'
55
+ #* <tt>:enable </tt> Array
56
+ #Enables
57
+ #* <tt>:text</tt> - (includetext) - Show human readable text for data in symbol.
58
+ #* <tt>:check</tt> - (includecheck) - Generate check digit(s), where relevant.
59
+ #* <tt>:checkintext</tt> - (includecheckintext) - Generate check digit(s), where relevant.
60
+ #Example:
61
+ # doc=Document.new
62
+ # doc.barcode_interleaved2of5('0123456789', :text => {:size => 10, :offset => [0,-10], :enable => [:text, :check, :checkintext] })
63
+ class Text < RGhost::Barcode::Parameter
64
+
65
+ def make
66
+ add_single("includetext")
67
+ @options.each do |key,value|
68
+ case key
69
+ when :enable then value.each{|item| add_single("include#{item}") }
70
+ when :offset
71
+ add("textxoffset", value[0])
72
+ add("textyoffset", value[1])
73
+ when :color then add_color("textcolor", value)
74
+ when :x, :y then add("text#{key}align",value)
75
+ else
76
+ add("text#{key}",value)
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ #Display whitespace guards or the specified width and height, in points. (Most useful for EANs). It's used on the contructor of the class RGhost::Barcode::Base(parent of all barcodes) by Hash :guard.
83
+ #Between parenthesis the original parameter.
84
+ #* <tt>:whitespace</tt>(guardwhitespace) - Enabled by true. Example: :whitespace => true
85
+ #* <tt>:height and :width </tt>(guardwidth and guardheight)
86
+ #Amount of whitespace to guard to left and right of the symbol and vertical position of the guard symbols, in points.
87
+ #* <tt>:left,right,:left_y, :right_y </tt>(guardleftpos, guardrightpos, guardleftypos and guardrightypos)
88
+ #
89
+ #Example:
90
+ # doc=Document.new
91
+ # doc.barcode_ean13('977147396801', :guard=>{:whitespace => true, :width => 0.2 })
92
+ class Guard < RGhost::Barcode::Parameter
93
+ def make
94
+ #
95
+ @options.each do |key,value|
96
+ case key
97
+ when :left,:right,:left_y, :right_y then add("guard#{key.to_s.gsub(/_/,'')}pos",value)
98
+ when :whitespace then add_single("guardwhitespace")
99
+ else
100
+ add("guard#{key}",value)
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+
107
+ end
108
+ #Display border properties. It's used on the contructor of the class RGhost::Barcode::Base(parent of all barcodes) by Hash :border.
109
+ #Between parenthesis the original parameter.
110
+ #* <tt>:show</tt>(showborder) - Enabled by true. Example: :show => true
111
+ #* <tt>:width</tt>(borderwidth) - Width of border in points.
112
+ #* <tt>:left, :right, :top, :bottom</tt>( borderleft, borderright, bordertop, borderbottom) - specifies the width of the each side of border.
113
+ #* <tt>:color</tt>(textcolor) - Color of text. Only rgb hex. Example: :color => 'FF0000'
114
+ #
115
+ #Example:
116
+ # doc=Document.new
117
+ # doc.barcode_interleaved2of5('0123456789', :border => {:width => 4, :left => 15, :right => 15, :show => true} )
118
+ class Border < RGhost::Barcode::Parameter
119
+
120
+ def make
121
+
122
+ @options.each do |key,value|
123
+ case key
124
+ when :color then add_color("bordercolor", value)
125
+ when :show then add_single("showborder")
126
+ else
127
+ add("border#{key}",value)
128
+ end
129
+ end
130
+
131
+ end
132
+
133
+
134
+ end
135
+
136
+ #Base of all kinds of barcodes. It's used to creates barcode. For more details about options see http://groups.google.com/group/postscriptbarcode/web/Options
137
+ #
138
+ #==Options
139
+ #Facades
140
+ #* <tt>:text</tt> RGhost::Barcode::Text
141
+ #* <tt>:guard</tt> RGhost::Barcode::Guard
142
+ #* <tt>:border</tt> RGhost::Barcode::Border
143
+ #Options. Between parenthesis the original parameter.
144
+ #* <tt>:scale</tt> Scale in Array([x,y]). Example for twice the original size, :scale => [2,2]
145
+ #* <tt>:rotate</tt> Rotate angle.
146
+ #* <tt>:color</tt>(barcolor) Foreground color.
147
+ #* <tt>:background</tt>(backgroundcolor)
148
+ #* <tt>:x and :y</tt> Position. RGhost::Config::GS[:unit] will be used. Default :current_row and :limit_left
149
+ #* <tt>:width and :height</tt>(width and height). Width and height of barcode, RGhost::Config::GS[:unit] will be used.
150
+ #
151
+ #Example:
152
+ # doc=Document.new
153
+ # doc.barcode_interleaved2of5('0123456789',
154
+ # :text => {:size => 10, :offset => [0,-10], :enable => [:text, :check, :checkintext] },
155
+ # :border => {:width => 4, :left => 15, :right => 15, :show => true},
156
+ # :height => 2
157
+ # )
158
+ #Without options
159
+ # doc=Document.new
160
+ # doc.barcode_code39('0123456789')
161
+ class Base <RGhost::Barcode::Parameter
162
+
163
+ attr_reader :text, :guard, :border ,:point
164
+
165
+ def initialize(barcode_string,options={})
166
+ options||={}
167
+
168
+ @options=options.dup
169
+ @options[:x]||= :limit_left
170
+ @options[:y]||= :current_row
171
+ @barcode=barcode_string
172
+ @text = RGhost::Barcode::Text.new(@options[:text])
173
+
174
+ @guard = RGhost::Barcode::Guard.new(@options[:guard])
175
+ @border = RGhost::Barcode::Border.new(@options[:border])
176
+ @point=RGhost::Cursor.translate(@options)
177
+ @rotate=RGhost::Cursor.rotate @options[:rotate]
178
+ @scale=RGhost::Scale.new @options[:scale][0],@options[:scale][1] if @options[:scale]
179
+ [:scale, :enable, :text,:guard,:border, :x, :y, :rotate].each{|v| @options.delete v}
180
+ super(@options)
181
+ @self_name=self.class.to_s.downcase.split(/::/).last
182
+ # RGhost::Config::GS[:preload] << @self_name
183
+ end
184
+
185
+ def make
186
+
187
+ [:x,:y].each{|v| @options.delete v}
188
+
189
+ @options.each do |key,value|
190
+ case key
191
+ when :color then add_color("barcolor", value)
192
+ when :background then add_color("backgroundcolor", value)
193
+ when :width, :height then add_with_unit(key,value)
194
+ #when :inkspread then add(key,value)
195
+ else add(key,value)
196
+ end
197
+ end
198
+
199
+ end
200
+
201
+ def ps
202
+ formated_params=[ @text.map, @border.map, @guard.map, self.map].join(" ")
203
+
204
+ "gsave newpath #{@point.ps} #{@rotate} #{@scale} 0 0 moveto (#{@barcode}) (#{formated_params}) #{@self_name} closepath grestore"
205
+ end
206
+
207
+
208
+ end
209
+
210
+ end