label_factory 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/label_factory.gemspec +2 -2
- data/lib/label_factory/batch/base.rb +78 -109
- data/lib/label_factory/label/base.rb +41 -0
- data/lib/label_factory/layout/base.rb +21 -0
- data/lib/label_factory/template/base.rb +5 -2
- data/lib/label_factory/template/glabel.rb +12 -1
- data/lib/label_factory/version.rb +1 -1
- data/lib/label_factory.rb +6 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9c16bbab3b34b02f8fbc33edc8e9d48257f1dcf
|
4
|
+
data.tar.gz: 3b689fdae9d62fb06dc48c6727cc413e736165b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efa0d682190ab551bc63221384ce20ae43d3d9671a4a2c31c34bfc6ad96b458b0e76798569ef802ecb1f170fe07ad50f5b215487569ebd566a0033db3dd5cc3b
|
7
|
+
data.tar.gz: f772a583a1cd5130b693148e119b5a4f67f6b104fb12730ecf0e62a67701e5e1b6a2abc6e3d65142ca2ce41308d4bbeb09025491268aec09d27a19e33be71cec
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# LabelFactory
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/label_factory.png)](https://rubygems.org/gems/label_factory)[![Code Climate](https://codeclimate.com/github/eventioz/label_factory.png)](https://codeclimate.com/github/eventioz/label_factory)[![Dependency Status](https://gemnasium.com/eventioz/label_factory.png)](https://gemnasium.com/eventioz/label_factory)
|
2
3
|
|
3
4
|
create pdf labels with pure ruby
|
4
5
|
|
data/label_factory.gemspec
CHANGED
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ['xeroice@gmail.com']
|
11
11
|
spec.description = %q{pdf label creation in ruby}
|
12
12
|
spec.summary = %q{http://github.com/eventioz/label_factory}
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
13
|
+
spec.homepage = %q{http://github.com/eventioz/label_factory}
|
14
|
+
spec.license = %q{MIT}
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
@@ -2,64 +2,63 @@ module LabelFactory
|
|
2
2
|
module Batch
|
3
3
|
class Base
|
4
4
|
|
5
|
-
|
6
5
|
DEFAULTS = { justification: :left, font_size: 12, font_type: 'Helvetica' }
|
7
6
|
attr_accessor :template, :label, :pdf, :manual_new_page
|
8
|
-
attr_reader :labels_per_page
|
9
7
|
|
10
8
|
@@gt = nil
|
11
9
|
|
10
|
+
class << self
|
11
|
+
def gt
|
12
|
+
@@gt || self.load_template_set
|
13
|
+
end
|
14
|
+
|
15
|
+
def load_template_set(template_set_file = nil)
|
16
|
+
template_set_file ||= File.join(TEMPLATES_PATH, Template::Base::DEFAULT)
|
17
|
+
@@gt = Template::Glabel.load_from_file(template_set_file)
|
18
|
+
end
|
19
|
+
|
20
|
+
def all_template_names
|
21
|
+
gt.all_avaliable_templates
|
22
|
+
end
|
23
|
+
|
24
|
+
def all_templates
|
25
|
+
gt.templates.values
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
12
30
|
def initialize(template_name, pdf_opts = {})
|
13
31
|
|
14
32
|
@template = gt.find_template(template_name)
|
15
33
|
|
16
34
|
if @template
|
35
|
+
@label = @template.label
|
17
36
|
|
18
|
-
#if the template specifies the paper type, and the user didn't use it.
|
19
|
-
pdf_opts[:paper] = @template.size.gsub(/^.*-/,'') if @template.size && !pdf_opts.has_key?(:paper)
|
20
|
-
|
21
|
-
@label = @template.labels['0']
|
22
37
|
@layout = @label.layouts.first
|
23
|
-
@labels_per_page = [ @layout.nx, @layout.ny ].reduce(:*)
|
24
|
-
@zero_based_labels_per_page = @labels_per_page - 1
|
25
38
|
|
26
|
-
|
27
|
-
font_dir = pdf_opts.delete(:font_dir)
|
28
|
-
PDF::Writer::FONT_PATH << font_dir if font_dir && ! PDF::Writer::FONT_PATH.include?( font_dir )
|
29
|
-
# set afm_dir if needed
|
30
|
-
afm_dir = pdf_opts.delete(:afm_dir)
|
31
|
-
PDF::Writer::FontMetrics::METRICS_PATH << afm_dir if afm_dir && ! PDF::Writer::FontMetrics::METRICS_PATH.include?( font_dir )
|
32
|
-
|
33
|
-
@pdf = PDF::Writer.new(pdf_opts)
|
39
|
+
@pdf = PDF::Writer.new(set_options(pdf_opts))
|
34
40
|
|
35
41
|
@pdf.margins_pt(0, 0, 0, 0)
|
36
42
|
|
37
43
|
else
|
38
44
|
raise 'Template not found!'
|
39
45
|
end
|
40
|
-
end
|
41
46
|
|
42
|
-
def self.gt
|
43
|
-
@@gt || self.load_template_set
|
44
47
|
end
|
45
48
|
|
46
49
|
def gt
|
47
50
|
self.class.gt
|
48
51
|
end
|
49
52
|
|
50
|
-
def
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
gt.all_avaliable_templates
|
57
|
-
end
|
53
|
+
def set_options(opts = {})
|
54
|
+
opts[:paper] = @template.size.gsub(/^.*-/,'') if @template.size && ! opts[:paper]
|
55
|
+
# set font_dir if needed
|
56
|
+
set_fonts(opts.delete(:font_dir)) if opts[:font_dir]
|
57
|
+
# set afm_dir if needed
|
58
|
+
set_afm_fonts(opts.delete(:afm_dir)) if opts[:afm_dir]
|
58
59
|
|
59
|
-
|
60
|
-
gt.templates.values
|
60
|
+
opts
|
61
61
|
end
|
62
|
-
|
63
62
|
=begin rdoc
|
64
63
|
add_label takes an argument hash.
|
65
64
|
[:position] Which label slot to print. Positions are top to bottom, left to right so position 1 is the label in the top lefthand corner. Defaults to 0
|
@@ -69,6 +68,13 @@ module LabelFactory
|
|
69
68
|
[:justification] Values can be :left, :right, :center, :full. Defaults to :left
|
70
69
|
[:offset_x, offset_y] If your printer doesn't want to print with out margins you can define these values to fine tune printout.
|
71
70
|
=end
|
71
|
+
def set_fonts(font_dir = nil)
|
72
|
+
PDF::Writer::FONT_PATH << font_dir if font_dir && ! PDF::Writer::FONT_PATH.include?( font_dir )
|
73
|
+
end
|
74
|
+
|
75
|
+
def set_afm_fonts(afm_dir = nil)
|
76
|
+
PDF::Writer::FontMetrics::METRICS_PATH << afm_dir if afm_dir && ! PDF::Writer::FontMetrics::METRICS_PATH.include?( afm_dir )
|
77
|
+
end
|
72
78
|
|
73
79
|
def add_label(text, options = {})
|
74
80
|
unless options.delete(:skip)
|
@@ -124,49 +130,10 @@ module LabelFactory
|
|
124
130
|
end
|
125
131
|
end
|
126
132
|
|
127
|
-
=begin rdoc
|
128
|
-
To facilitate aligning a printer we give a method that prints the outlines of the labels
|
129
|
-
=end
|
130
|
-
def do_draw_boxes(write_coord = true, draw_markups = true)
|
131
|
-
@layout.nx.times do |x|
|
132
|
-
@layout.ny.times do |y|
|
133
|
-
box_x, box_y = get_x_y(x, y)
|
134
|
-
@pdf.rounded_rectangle(box_x,
|
135
|
-
box_y,
|
136
|
-
@label.width.as_pts,
|
137
|
-
@label.height.as_pts,
|
138
|
-
@label.round.as_pts).stroke
|
139
|
-
if write_coord
|
140
|
-
text = "#{box_x / 72}, #{box_y / 72}, #{@label.width.number}, #{label.height.number}"
|
141
|
-
add_label(text, x: box_x, y: box_y )
|
142
|
-
end
|
143
|
-
|
144
|
-
if draw_markups
|
145
|
-
@label.markupMargins.each do |margin|
|
146
|
-
size = margin.size.as_pts
|
147
|
-
@pdf.rounded_rectangle(box_x + size,
|
148
|
-
box_y - margin.size.as_pts,
|
149
|
-
@label.width.as_pts - 2*size,
|
150
|
-
@label.height.as_pts - 2*size,
|
151
|
-
@label.round.as_pts).stroke
|
152
|
-
end
|
153
|
-
@label.markupLines.each do |line|
|
154
|
-
@pdf.line(box_x + line.x1.as_pts,
|
155
|
-
box_y + line.y1.as_pts,
|
156
|
-
box_x + line.x2.as_pts,
|
157
|
-
box_y + line.y2.as_pts).stroke
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
private :do_draw_boxes
|
165
|
-
|
166
133
|
def draw_boxes(write_coord = true, draw_markups = true)
|
167
134
|
pdf.open_object do |heading|
|
168
135
|
pdf.save_state
|
169
|
-
do_draw_boxes(write_coord, draw_markups)
|
136
|
+
do_draw_boxes!(write_coord, draw_markups)
|
170
137
|
pdf.restore_state
|
171
138
|
pdf.close_object
|
172
139
|
pdf.add_object(heading, :all_pages)
|
@@ -177,63 +144,65 @@ module LabelFactory
|
|
177
144
|
@pdf.save_as(file_name)
|
178
145
|
end
|
179
146
|
|
180
|
-
|
147
|
+
private
|
148
|
+
=begin rdoc
|
149
|
+
To facilitate aligning a printer we give a method that prints the outlines of the labels
|
150
|
+
=end
|
151
|
+
def do_draw_boxes!(write_coord = true, draw_markups = true)
|
152
|
+
@layout.nx.times do |x|
|
153
|
+
@layout.ny.times do |y|
|
154
|
+
box_x, box_y = get_x_y(x, y)
|
155
|
+
@pdf.rounded_rectangle(box_x, box_y, @label.width.as_pts, @label.height.as_pts, @label.round.as_pts).stroke
|
156
|
+
add_label('', x: box_x, y: box_y ) if write_coord
|
157
|
+
@label.draw_markups!(@pdf, box_x, box_y) if draw_markups
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
181
161
|
|
182
162
|
=begin rdoc
|
183
163
|
Position is top to bottom, left to right, starting at 1 and ending at the end of the page
|
184
164
|
=end
|
185
|
-
def position_to_x_y(
|
186
|
-
|
187
|
-
|
165
|
+
def position_to_x_y(options = {})
|
166
|
+
position = options[:position]
|
167
|
+
if position && position > @layout.labels_base_per_page
|
168
|
+
position = position % @layout.labels_per_page
|
169
|
+
@pdf.new_page if position.zero? && !manual_new_page
|
170
|
+
end
|
171
|
+
x = options[:x] || @layout.get_x(position)
|
172
|
+
y = options[:y] || @layout.get_y(position)
|
173
|
+
x += options[:offset_x] if options[:offset_x]
|
174
|
+
y += options[:offset_y] if options[:offset_y]
|
188
175
|
get_x_y(x, y)
|
189
176
|
end
|
190
177
|
|
191
178
|
def get_x_y(x, y)
|
192
|
-
|
193
|
-
|
194
|
-
label_y -= @layout.y0.as_pts
|
195
|
-
label_y -= [y, @layout.dy.as_pts].reduce(:*)
|
179
|
+
[ get_label_x_position(x), get_label_y_position(y) ]
|
180
|
+
end
|
196
181
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
label_x += [x, @layout.dx.as_pts ].reduce(:*)
|
182
|
+
def get_label_x_position(x)
|
183
|
+
get_left_label_position + [ x, @layout.as_pts(:dx) ].reduce(:*)
|
184
|
+
end
|
201
185
|
|
202
|
-
|
186
|
+
def get_label_y_position(y)
|
187
|
+
get_top_label_position - [ y, @layout.as_pts(:dy) ].reduce(:*)
|
203
188
|
end
|
204
189
|
|
205
|
-
def
|
206
|
-
|
207
|
-
|
208
|
-
if position > @zero_based_labels_per_page
|
209
|
-
position = position % @labels_per_page
|
210
|
-
# if remainder is zero, we're dealing with the first label of a new page
|
211
|
-
@pdf.new_page if ( position.zero? && manual_new_page.nil? )
|
212
|
-
end
|
213
|
-
label_x, label_y = position_to_x_y(position)
|
214
|
-
else
|
215
|
-
label_x, label_y = position_to_x_y(0) unless ( label_x = options[:x] ) && ( label_y = options[:y] )
|
216
|
-
end
|
190
|
+
def get_left_label_position
|
191
|
+
[ @pdf.absolute_left_margin, @pdf.left_margin ].reduce(:-) + @layout.as_pts(:x0)
|
192
|
+
end
|
217
193
|
|
218
|
-
|
194
|
+
def get_top_label_position
|
195
|
+
[ @pdf.absolute_top_margin, @pdf.top_margin ].reduce(:+) - @layout.as_pts(:y0)
|
196
|
+
end
|
219
197
|
|
220
|
-
|
221
|
-
@label.markupMargins.each do |margin|
|
222
|
-
label_x += margin.size.as_pts
|
223
|
-
label_y -= margin.size.as_pts
|
224
|
-
label_width -= margin.size.as_pts
|
225
|
-
end
|
226
|
-
end
|
198
|
+
def setup_add_label_options(options)
|
227
199
|
|
228
|
-
|
229
|
-
label_x += options[:offset_x]
|
230
|
-
label_width += options[:offset_x]
|
231
|
-
end
|
200
|
+
label_x, label_y = position_to_x_y(options)
|
232
201
|
|
233
|
-
label_y += options[:offset_y] if options[:offset_y]
|
234
202
|
|
235
|
-
|
203
|
+
label_x, label_y, label_width = @label.without_margins(label_x, label_y, options[:offset_x]) unless options[:use_margin]
|
236
204
|
|
205
|
+
[ label_x, label_y, label_width ]
|
237
206
|
end
|
238
207
|
|
239
208
|
def setup(label_x, label_width, options)
|
@@ -17,6 +17,47 @@ module LabelFactory
|
|
17
17
|
@markups ||= [@markupMargins, @markupLines, @markupCircles ].reduce(:merge)
|
18
18
|
end
|
19
19
|
|
20
|
+
def draw_markups!(pdf_instance = nil, box_x, box_y)
|
21
|
+
if pdf_instance
|
22
|
+
draw_margins!(pdf_instance, box_x, box_y)
|
23
|
+
draw_lines!(pdf_instance, box_x, box_y)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def without_margins(x, y, offset_x = nil)
|
28
|
+
width = x + self.width.as_pts
|
29
|
+
width += offset_x if offset_x
|
30
|
+
@markupMargins.each do |margin|
|
31
|
+
x += margin.size.as_pts
|
32
|
+
y -= margin.size.as_pts
|
33
|
+
width -= margin.size.as_pts
|
34
|
+
end
|
35
|
+
[x, y, width]
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def draw_margins!(pdf_instance = nil, box_x, box_y)
|
41
|
+
@markupMargins.each do |margin|
|
42
|
+
pdf_instance.rounded_rectangle( box_x + margin.size.as_pts, box_y - margin.size.as_pts,
|
43
|
+
width_size(margin), height_size(margin), round.as_pts ).stroke
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def draw_lines!(pdf_instance = nil, box_x, box_y)
|
48
|
+
@markupLines.each do |line|
|
49
|
+
pdf_instance.line(box_x + line.x1.as_pts, box_y + line.y1.as_pts,
|
50
|
+
box_x + line.x2.as_pts, box_y + line.y2.as_pts).stroke
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def width_size(margin)
|
55
|
+
width.as_pts - [2,margin.size.as_pts].reduce(:*)
|
56
|
+
end
|
57
|
+
|
58
|
+
def height_size(margin)
|
59
|
+
height.as_pts - [2, margin.size.as_pts].reduce(:*)
|
60
|
+
end
|
20
61
|
end
|
21
62
|
end
|
22
63
|
end
|
@@ -11,6 +11,27 @@ module LabelFactory
|
|
11
11
|
length_node :y0, '@y0', default_value: '0 pt'
|
12
12
|
length_node :dx, '@dx', default_value: '0 pt'
|
13
13
|
length_node :dy, '@dy', default_value: '0 pt'
|
14
|
+
|
15
|
+
def labels_per_page
|
16
|
+
[nx, ny].reduce(:*)
|
17
|
+
end
|
18
|
+
|
19
|
+
def labels_base_per_page
|
20
|
+
labels_per_page - 1
|
21
|
+
end
|
22
|
+
|
23
|
+
def as_pts(attr = nil)
|
24
|
+
self.public_send(attr.to_s).as_pts if attr
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_x(position = 0)
|
28
|
+
(position * 1.0 / ny).floor
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_y(position = 0)
|
32
|
+
position % ny
|
33
|
+
end
|
34
|
+
|
14
35
|
end
|
15
36
|
end
|
16
37
|
end
|
@@ -25,8 +25,19 @@ module LabelFactory
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def all_avaliable_templates
|
28
|
-
|
28
|
+
all_templates_names + all_templates_alias_names
|
29
29
|
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def all_templates_names
|
34
|
+
templates.values.map(&:name)
|
35
|
+
end
|
36
|
+
|
37
|
+
def all_templates_alias_names
|
38
|
+
templates.values.map(&:alias).map(&:keys).flatten
|
39
|
+
end
|
40
|
+
|
30
41
|
end
|
31
42
|
end
|
32
43
|
end
|
data/lib/label_factory.rb
CHANGED
@@ -25,22 +25,17 @@ module LabelFactory
|
|
25
25
|
end
|
26
26
|
|
27
27
|
module Label
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
autoload :Round, File.join(LABEL_PATH, 'round')
|
32
|
-
autoload :Rectangle, File.join(LABEL_PATH, 'rectangle')
|
28
|
+
%w(base alias cd round rectangle).each do |lib|
|
29
|
+
autoload lib.capitalize.to_sym, File.join(LABEL_PATH, lib)
|
30
|
+
end
|
33
31
|
end
|
34
32
|
|
35
33
|
module Layout
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
autoload :Circle, File.join(LAYOUT_PATH, 'circle')
|
40
|
-
autoload :Length, File.join(LAYOUT_PATH, 'length')
|
34
|
+
%w(base margin line circle length).each do |lib|
|
35
|
+
autoload lib.capitalize.to_sym, File.join(LAYOUT_PATH, lib)
|
36
|
+
end
|
41
37
|
end
|
42
38
|
|
43
|
-
|
44
39
|
require File.join(LIBRARY_PATH, 'version')
|
45
40
|
require File.join(UTIL_PATH, 'length_node')
|
46
41
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: label_factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gagoar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: metaskills-pdf-writer
|