barby 0.1.1 → 0.1.2
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.
- data/lib/barby/outputter/cairo_outputter.rb +154 -0
- metadata +37 -43
@@ -0,0 +1,154 @@
|
|
1
|
+
require 'cairo'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
module Barby
|
5
|
+
class CairoOutputter < Outputter
|
6
|
+
|
7
|
+
register :render_to_cairo_context
|
8
|
+
register :to_png
|
9
|
+
|
10
|
+
if Cairo.const_defined?(:PSSurface)
|
11
|
+
register :to_ps
|
12
|
+
register :to_eps if Cairo::PSSurface.method_defined?(:eps=)
|
13
|
+
end
|
14
|
+
|
15
|
+
register :to_pdf if Cairo.const_defined?(:PDFSurface)
|
16
|
+
register :to_svg if Cairo.const_defined?(:SVGSurface)
|
17
|
+
|
18
|
+
attr_writer :x, :y, :xdim, :height, :margin
|
19
|
+
|
20
|
+
|
21
|
+
def render_to_cairo_context(context, options={})
|
22
|
+
if context.respond_to?(:have_current_point?) and
|
23
|
+
context.have_current_point?
|
24
|
+
current_x, current_y = context.current_point
|
25
|
+
else
|
26
|
+
current_x = x(options) || margin(options)
|
27
|
+
current_y = y(options) || margin(options)
|
28
|
+
end
|
29
|
+
|
30
|
+
_xdim = xdim(options)
|
31
|
+
_height = height(options)
|
32
|
+
context.save do
|
33
|
+
context.set_source_color(:black)
|
34
|
+
context.fill do
|
35
|
+
barcode.encoding.scan(/(?:0+|1+)/).each do |codes|
|
36
|
+
current_width = _xdim * codes.size
|
37
|
+
if codes[0] == ?1
|
38
|
+
context.rectangle(current_x, current_y, current_width, _height)
|
39
|
+
end
|
40
|
+
current_x += current_width
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def to_png(options={})
|
50
|
+
output_to_string_io do |io|
|
51
|
+
Cairo::ImageSurface.new(options[:format],
|
52
|
+
full_width(options),
|
53
|
+
full_height(options)) do |surface|
|
54
|
+
render(surface, options)
|
55
|
+
surface.write_to_png(io)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def to_ps(options={})
|
62
|
+
output_to_string_io do |io|
|
63
|
+
Cairo::PSSurface.new(io,
|
64
|
+
full_width(options),
|
65
|
+
full_height(options)) do |surface|
|
66
|
+
surface.eps = options[:eps] if surface.respond_to?(:eps=)
|
67
|
+
render(surface, options)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
def to_eps(options={})
|
74
|
+
to_ps(options.merge(:eps => true))
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
def to_pdf(options={})
|
79
|
+
output_to_string_io do |io|
|
80
|
+
Cairo::PDFSurface.new(io,
|
81
|
+
full_width(options),
|
82
|
+
full_height(options)) do |surface|
|
83
|
+
render(surface, options)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
def to_svg(options={})
|
90
|
+
output_to_string_io do |io|
|
91
|
+
Cairo::SVGSurface.new(io,
|
92
|
+
full_width(options),
|
93
|
+
full_height(options)) do |surface|
|
94
|
+
render(surface, options)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
def x(options={})
|
101
|
+
@x || options[:x]
|
102
|
+
end
|
103
|
+
|
104
|
+
def y(options={})
|
105
|
+
@y || options[:y]
|
106
|
+
end
|
107
|
+
|
108
|
+
def width(options={})
|
109
|
+
barcode.encoding.length * xdim(options)
|
110
|
+
end
|
111
|
+
|
112
|
+
def height(options={})
|
113
|
+
@height || options[:height] || 50
|
114
|
+
end
|
115
|
+
|
116
|
+
def full_width(options={})
|
117
|
+
width(options) + (margin(options) * 2)
|
118
|
+
end
|
119
|
+
|
120
|
+
def full_height(options={})
|
121
|
+
height(options) + (margin(options) * 2)
|
122
|
+
end
|
123
|
+
|
124
|
+
def xdim(options={})
|
125
|
+
@xdim || options[:xdim] || 1
|
126
|
+
end
|
127
|
+
|
128
|
+
def margin(options={})
|
129
|
+
@margin || options[:margin] || 10
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
def output_to_string_io
|
136
|
+
io = StringIO.new
|
137
|
+
yield(io)
|
138
|
+
io.rewind
|
139
|
+
io.read
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
def render(surface, options)
|
144
|
+
context = Cairo::Context.new(surface)
|
145
|
+
yield(context) if block_given?
|
146
|
+
context.set_source_color(options[:background] || :white)
|
147
|
+
context.paint
|
148
|
+
render_to_cairo_context(context, options)
|
149
|
+
context
|
150
|
+
end
|
151
|
+
|
152
|
+
|
153
|
+
end
|
154
|
+
end
|
metadata
CHANGED
@@ -1,70 +1,64 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
2
4
|
name: barby
|
3
5
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
6
|
+
version: 0.1.2
|
7
|
+
date: 2008-04-27 00:00:00 +02:00
|
8
|
+
summary: A pure-Ruby barcode generator
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: toredarell@gmail.com
|
12
|
+
homepage: http://tore.darell.no/
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: barby
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
5
25
|
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
6
29
|
authors:
|
7
30
|
- Tore Darell
|
8
|
-
autorequire: barby
|
9
|
-
bindir: bin
|
10
|
-
cert_chain: []
|
11
|
-
|
12
|
-
date: 2008-04-25 00:00:00 +02:00
|
13
|
-
default_executable:
|
14
|
-
dependencies: []
|
15
|
-
|
16
|
-
description:
|
17
|
-
email: toredarell@gmail.com
|
18
|
-
executables: []
|
19
|
-
|
20
|
-
extensions: []
|
21
|
-
|
22
|
-
extra_rdoc_files:
|
23
|
-
- README
|
24
31
|
files:
|
25
32
|
- lib/barby.rb
|
26
33
|
- lib/barby
|
27
34
|
- lib/barby/barcode.rb
|
28
35
|
- lib/barby/barcode
|
36
|
+
- lib/barby/outputter.rb
|
37
|
+
- lib/barby/outputter
|
38
|
+
- lib/barby/version.rb
|
29
39
|
- lib/barby/barcode/bookland.rb
|
30
40
|
- lib/barby/barcode/code_128.rb
|
31
41
|
- lib/barby/barcode/code_39.rb
|
32
42
|
- lib/barby/barcode/ean_13.rb
|
33
43
|
- lib/barby/barcode/ean_8.rb
|
34
44
|
- lib/barby/barcode/gs1_128.rb
|
35
|
-
- lib/barby/outputter.rb
|
36
|
-
- lib/barby/outputter
|
37
45
|
- lib/barby/outputter/ascii_outputter.rb
|
38
46
|
- lib/barby/outputter/pdfwriter_outputter.rb
|
39
47
|
- lib/barby/outputter/rmagick_outputter.rb
|
48
|
+
- lib/barby/outputter/cairo_outputter.rb
|
40
49
|
- lib/barby/outputter/png_outputter.rb
|
41
|
-
- lib/barby/version.rb
|
42
50
|
- README
|
43
|
-
|
44
|
-
|
45
|
-
post_install_message:
|
51
|
+
test_files: []
|
52
|
+
|
46
53
|
rdoc_options: []
|
47
54
|
|
48
|
-
|
49
|
-
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
version: "0"
|
55
|
-
version:
|
56
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
-
requirements:
|
58
|
-
- - ">="
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
version: "0"
|
61
|
-
version:
|
55
|
+
extra_rdoc_files:
|
56
|
+
- README
|
57
|
+
executables: []
|
58
|
+
|
59
|
+
extensions: []
|
60
|
+
|
62
61
|
requirements: []
|
63
62
|
|
64
|
-
|
65
|
-
rubygems_version: 1.1.1
|
66
|
-
signing_key:
|
67
|
-
specification_version: 2
|
68
|
-
summary: A pure-Ruby barcode generator
|
69
|
-
test_files: []
|
63
|
+
dependencies: []
|
70
64
|
|