barby 0.1.2 → 0.2.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.
Files changed (37) hide show
  1. data/bin/barby +40 -0
  2. data/lib/barby.rb +3 -0
  3. data/lib/barby/barcode.rb +6 -0
  4. data/lib/barby/barcode/code_128.rb +16 -3
  5. data/lib/barby/barcode/code_25.rb +161 -0
  6. data/lib/barby/barcode/code_25_interleaved.rb +73 -0
  7. data/lib/barby/barcode/code_93.rb +225 -0
  8. data/lib/barby/barcode/qr_code.rb +93 -0
  9. data/lib/barby/outputter.rb +56 -2
  10. data/lib/barby/outputter/ascii_outputter.rb +20 -1
  11. data/lib/barby/outputter/cairo_outputter.rb +38 -7
  12. data/lib/barby/outputter/pdfwriter_outputter.rb +44 -35
  13. data/lib/barby/outputter/png_outputter.rb +48 -16
  14. data/lib/barby/outputter/prawn_outputter.rb +100 -0
  15. data/lib/barby/outputter/rmagick_outputter.rb +48 -20
  16. data/lib/barby/vendor.rb +3 -0
  17. data/vendor/rqrcode/CHANGELOG +21 -0
  18. data/vendor/rqrcode/COPYING +19 -0
  19. data/vendor/rqrcode/README +98 -0
  20. data/vendor/rqrcode/Rakefile +65 -0
  21. data/vendor/rqrcode/lib/rqrcode.rb +13 -0
  22. data/vendor/rqrcode/lib/rqrcode/core_ext.rb +5 -0
  23. data/vendor/rqrcode/lib/rqrcode/core_ext/array.rb +5 -0
  24. data/vendor/rqrcode/lib/rqrcode/core_ext/array/behavior.rb +9 -0
  25. data/vendor/rqrcode/lib/rqrcode/core_ext/integer.rb +5 -0
  26. data/vendor/rqrcode/lib/rqrcode/core_ext/integer/bitwise.rb +11 -0
  27. data/vendor/rqrcode/lib/rqrcode/qrcode.rb +4 -0
  28. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_8bit_byte.rb +35 -0
  29. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_bit_buffer.rb +56 -0
  30. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_code.rb +421 -0
  31. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_math.rb +63 -0
  32. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_polynomial.rb +78 -0
  33. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_rs_block.rb +134 -0
  34. data/vendor/rqrcode/lib/rqrcode/qrcode/qr_util.rb +254 -0
  35. data/vendor/rqrcode/test/runtest.rb +78 -0
  36. data/vendor/rqrcode/test/test_data.rb +21 -0
  37. metadata +86 -44
@@ -0,0 +1,100 @@
1
+ require 'barby/outputter'
2
+ $: << '/home/toretore/Projects/tmp/prawn/lib'
3
+ require 'prawn'
4
+
5
+ module Barby
6
+
7
+ class PrawnOutputter < Outputter
8
+
9
+ register :to_pdf, :annotate_pdf
10
+
11
+
12
+ def to_pdf(opts={})
13
+ opts = options(opts)
14
+ annotate_pdf(Prawn::Document.new(opts[:document]), opts).render
15
+ end
16
+
17
+
18
+ def annotate_pdf(pdf, opts={})
19
+ opts = options(opts)
20
+ xpos, ypos, height, xdim = opts[:x], opts[:y], opts[:height], opts[:xdim]
21
+ ydim = opts[:ydim] || xdim
22
+ orig_xpos = xpos
23
+
24
+ if barcode.two_dimensional?
25
+ boolean_groups.reverse_each do |groups|
26
+ groups.each do |bar,amount|
27
+ if bar
28
+ pdf.move_to(xpos, ypos)
29
+ pdf.line_to(xpos, ypos+ydim)
30
+ pdf.line_to(xpos+(xdim*amount), ypos+ydim)
31
+ pdf.line_to(xpos+(xdim*amount), ypos)
32
+ pdf.line_to(xpos, ypos)
33
+ pdf.fill
34
+ end
35
+ xpos += (xdim*amount)
36
+ end
37
+ xpos = orig_xpos
38
+ ypos += ydim
39
+ end
40
+ else
41
+ boolean_groups.each do |bar,amount|
42
+ if bar
43
+ pdf.move_to(xpos, ypos)
44
+ pdf.line_to(xpos, ypos+height)
45
+ pdf.line_to(xpos+(xdim*amount), ypos+height)
46
+ pdf.line_to(xpos+(xdim*amount), ypos)
47
+ pdf.line_to(xpos, ypos)
48
+ pdf.fill
49
+ end
50
+ xpos += (xdim*amount)
51
+ end
52
+ end
53
+
54
+ pdf
55
+ end
56
+
57
+
58
+ private
59
+
60
+ def default_options
61
+ @default_options ||= {
62
+ :margin => 5,
63
+ :height => 100,
64
+ :xdim => 1
65
+ }
66
+ end
67
+
68
+ def options(opts={})
69
+ doc_opts = opts.delete(:document) || {}
70
+ opts = default_options.merge(opts)
71
+ opts[:x] ||= opts[:margin]
72
+ opts[:y] ||= opts[:margin]
73
+ opts[:document] = document_options(opts, doc_opts)
74
+ opts
75
+ end
76
+
77
+ def document_options(opts, doc_opts)
78
+ o = doc_opts.dup
79
+ #o[:page_size] ||= page_size(opts[:xdim], opts[:height], opts[:margin])
80
+ #%w(left right top bottom).each{|s| o[:"#{s}_margin"] ||= opts[:margin] }
81
+ o[:page_size] ||= 'A4' #Prawn doesn't currently support custom page sizes
82
+ o
83
+ end
84
+
85
+ def page_size(xdim, height, margin)
86
+ [width(xdim,margin), height(height,margin)]
87
+ end
88
+
89
+ def width(xdim, margin)
90
+ (xdim * encoding.length) + (margin * 2)
91
+ end
92
+
93
+ def height(height, margin)
94
+ height + (margin * 2)
95
+ end
96
+
97
+
98
+ end
99
+
100
+ end
@@ -5,11 +5,13 @@ module Barby
5
5
 
6
6
 
7
7
  #Renders images from barcodes using RMagick
8
+ #
9
+ #Registers the to_png, to_gif, to_jpg and to_image methods
8
10
  class RmagickOutputter < Outputter
9
11
 
10
12
  register :to_png, :to_gif, :to_jpg, :to_image
11
13
 
12
- attr_accessor :height, :xdim, :margin
14
+ attr_accessor :height, :xdim, :ydim, :margin
13
15
 
14
16
 
15
17
  #Returns a string containing a PNG image
@@ -29,28 +31,54 @@ module Barby
29
31
 
30
32
  #Returns an instance of Magick::Image
31
33
  def to_image(opts={})
32
- opts.each{|k,v| send("#{k}=", v) if respond_to?("#{k}=") }
33
- canvas = Magick::Image.new(full_width, full_height)
34
- bars = Magick::Draw.new
35
-
36
- x = margin
37
- y = margin
38
- booleans.each do |bar|
39
- if bar
40
- bars.rectangle(x, y, x+(xdim-1), y+(height-1))
34
+ with_options opts do
35
+ canvas = Magick::Image.new(full_width, full_height)
36
+ bars = Magick::Draw.new
37
+
38
+ x = margin
39
+ y = margin
40
+
41
+ if barcode.two_dimensional?
42
+ encoding.each do |line|
43
+ line.split(//).map{|c| c == '1' }.each do |bar|
44
+ if bar
45
+ bars.rectangle(x, y, x+(xdim-1), y+(ydim-1))
46
+ end
47
+ x += xdim
48
+ end
49
+ x = margin
50
+ y += ydim
51
+ end
52
+ else
53
+ booleans.each do |bar|
54
+ if bar
55
+ bars.rectangle(x, y, x+(xdim-1), y+(height-1))
56
+ end
57
+ x += xdim
58
+ end
41
59
  end
42
- x += xdim
43
- end
44
60
 
45
- bars.draw(canvas)
61
+ bars.draw(canvas)
46
62
 
47
- canvas
63
+ canvas
64
+ end
48
65
  end
49
66
 
50
67
 
51
68
  #The height of the barcode in px
69
+ #For 2D barcodes this is the number of "lines" * ydim
52
70
  def height
53
- @height || 100
71
+ barcode.two_dimensional? ? (ydim * encoding.length) : (@height || 100)
72
+ end
73
+
74
+ #The width of the barcode in px
75
+ def width
76
+ length * xdim
77
+ end
78
+
79
+ #Number of modules (xdims) on the x axis
80
+ def length
81
+ barcode.two_dimensional? ? encoding.first.length : encoding.length
54
82
  end
55
83
 
56
84
  #X dimension. 1X == 1px
@@ -58,16 +86,16 @@ module Barby
58
86
  @xdim || 1
59
87
  end
60
88
 
89
+ #Y dimension. Only for 2D codes
90
+ def ydim
91
+ @ydim || xdim
92
+ end
93
+
61
94
  #The margin of each edge surrounding the barcode in pixels
62
95
  def margin
63
96
  @margin || 10
64
97
  end
65
98
 
66
- #The width of the barcode in px
67
- def width
68
- barcode.encoding.length * xdim
69
- end
70
-
71
99
  #The full width of the image. This is the width of the
72
100
  #barcode + the left and right margin
73
101
  def full_width
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/../../vendor/*/lib"].each do |d|
2
+ $: << File.expand_path(d)
3
+ end
@@ -0,0 +1,21 @@
1
+ *0.3.0* (Feb 28th, 2008)
2
+
3
+ * added more documentation
4
+ * changed to_console to to_s (what was I thinking)
5
+ * add more tests
6
+
7
+ *0.2.1* (Feb 24th, 2008)
8
+
9
+ * small changes to rakefile and readme
10
+ * small change to to_console method
11
+ * make console_count method private (use modules.size instead)
12
+ * slowy updating rdocs
13
+
14
+ *0.2.0* (Feb 23rd, 2008)
15
+
16
+ * Split files up [DR]
17
+ * added rdoc comment ... more to do there
18
+
19
+ *0.1.0* (Feb 22rd, 2008)
20
+
21
+ * 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.0"
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,5 @@
1
+ Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].sort.each do |path|
2
+ filename = File.basename(path)
3
+ require "rqrcode/core_ext/#{filename}"
4
+ end
5
+
@@ -0,0 +1,5 @@
1
+ require 'rqrcode/core_ext/array/behavior'
2
+
3
+ class Array #:nodoc:
4
+ include CoreExtensions::Array::Behavior
5
+ end