pdf-labels 1.0.1 → 2.0.1

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 (50) hide show
  1. data/History.txt +9 -1
  2. data/LICENCE +0 -0
  3. data/Manifest.txt +32 -31
  4. data/Rakefile +2 -2
  5. data/fonts/Code128.afm +275 -0
  6. data/fonts/Code128.pfb +0 -0
  7. data/fonts/Code128.ttf +0 -0
  8. data/fonts/Code2of5interleaved.afm +275 -0
  9. data/fonts/Code2of5interleaved.pfb +0 -0
  10. data/fonts/Code2of5interleaved.ttf +0 -0
  11. data/fonts/Code3de9.afm +275 -0
  12. data/fonts/Code3de9.pfb +0 -0
  13. data/fonts/Code3de9.ttf +0 -0
  14. data/fonts/CodeDatamatrix.afm +275 -0
  15. data/fonts/CodeDatamatrix.pfb +0 -0
  16. data/fonts/CodeDatamatrix.ttf +0 -0
  17. data/fonts/CodeEAN13.afm +275 -0
  18. data/fonts/CodeEAN13.pfb +0 -0
  19. data/fonts/CodeEAN13.ttf +0 -0
  20. data/fonts/CodePDF417.afm +275 -0
  21. data/fonts/CodePDF417.pfb +0 -0
  22. data/fonts/CodePDF417.ttf +0 -0
  23. data/init.rb +2 -0
  24. data/lib/pdf/label.rb +12 -0
  25. data/lib/pdf/label/alias.rb +11 -0
  26. data/lib/pdf/label/batch.rb +276 -0
  27. data/lib/pdf/label/glabel_template.rb +38 -0
  28. data/lib/pdf/label/label.rb +55 -0
  29. data/lib/pdf/label/layout.rb +15 -0
  30. data/lib/pdf/label/length_node.rb +52 -0
  31. data/lib/pdf/label/markup.rb +26 -0
  32. data/lib/pdf/label/template.rb +39 -0
  33. data/pdf-labels-patch-to-r62.txt +98 -0
  34. data/templates/avery-us-templates.xml +0 -0
  35. data/templates/glabels-2.0.dtd +0 -0
  36. data/test/test_pdf_label_page.rb +76 -18
  37. data/vendor/pdf/writer.rb +4 -1
  38. data/vendor/pdf/writer/object/font.rb +4 -4
  39. data/vendor/pdf/writer/object/viewerpreferences.rb +2 -2
  40. data/vendor/pdf_writer_font_patch.diff +35 -0
  41. metadata +84 -66
  42. data/lib/alias.rb +0 -8
  43. data/lib/glabel_template.rb +0 -36
  44. data/lib/label.rb +0 -52
  45. data/lib/layout.rb +0 -13
  46. data/lib/length_node.rb +0 -47
  47. data/lib/markup.rb +0 -25
  48. data/lib/pdf_label_page.rb +0 -171
  49. data/lib/pdf_labels.rb +0 -3
  50. data/lib/template.rb +0 -37
@@ -1,8 +0,0 @@
1
- require 'xml/mapping'
2
- require 'length_node'
3
-
4
- class Alias
5
- include XML::Mapping
6
- text_node :name, "@name"
7
- end
8
-
@@ -1,36 +0,0 @@
1
- require 'template'
2
-
3
- class GlabelsTemplate
4
- include XML::Mapping
5
-
6
- hash_node :templates, "Template", "@name", :class => Template, :default_value => Hash.new
7
-
8
- def find_all_templates
9
- return @t unless @t.nil?
10
- @t = []
11
- templates.each {|t|
12
- @t << "#{t[1].name}"
13
- t[1].alias.each {|a|
14
- @t << "#{a[1].name}"
15
- }
16
- }
17
- return @t
18
- end
19
-
20
-
21
- def find_template(t_name)
22
- return find_all_with_templates if t_name == :all
23
- if t = templates[t_name]
24
- return t
25
- else
26
- templates.each { |t|
27
- if t[1].alias[t_name]
28
- return t[1]
29
- end
30
- }
31
- end
32
- return nil
33
- end
34
-
35
- end
36
-
@@ -1,52 +0,0 @@
1
- require 'length_node'
2
- require 'layout'
3
- require 'markup'
4
-
5
- class Label
6
- include XML::Mapping
7
- attr_accessor :shape
8
- numeric_node :id, "@id"
9
- array_node :markupMargins, "Markup-margin", :class => MarkupMargin, :default_value => nil
10
- array_node :markupLines, "Markup-line", :class => MarkupLine, :default_value => nil
11
- array_node :markupCircles, "Markup-circle", :class => MarkupCircle, :default_value => nil
12
-
13
- array_node :layouts, "Layout", :class => Layout
14
-
15
- def markups
16
- @markups = Hash.new
17
- @markups = @markups.merge @markupMargins
18
- @markups = @markups.merge @markupLines
19
- @markups = @markups.merge @markupCircles
20
- @markups
21
- end
22
-
23
- end
24
-
25
-
26
-
27
- class LabelRectangle < Label
28
- length_node :width, "@width"
29
- length_node :height, "@height"
30
- length_node :round, "@round", :default_value => "0 pt"
31
- length_node :waste, "@waste", :default_value => "0 pt"
32
- length_node :x_waste, "@x_waste", :default_value => "0 pt"
33
- length_node :y_waste, "@y_waste", :default_value => "0 pt"
34
- @kind = "Rectangle"
35
-
36
- end
37
-
38
- class LabelRound < Label
39
- length_node :radius, "@radius"
40
- length_node :waste, "@radius", :default_value => "0 pt"
41
- @kind = "Round"
42
- end
43
-
44
- class LabelCD < Label
45
- length_node :radius, "@radius"
46
- length_node :hole, "@hole"
47
- length_node :width, "@width", :default_value => ""
48
- length_node :height, "@height", :default_value => ""
49
- length_node :waste, "@waste", :default_value => ""
50
- @kind = "CD"
51
- end
52
-
@@ -1,13 +0,0 @@
1
- require 'length_node'
2
-
3
-
4
- class Layout
5
- include XML::Mapping
6
- numeric_node :nx, "@nx"
7
- numeric_node :ny, "@ny"
8
- length_node :x0, "@x0", :default_value => "0 pt"
9
- length_node :y0, "@y0", :default_value => "0 pt"
10
- length_node :dx, "@dx", :default_value => "0 pt"
11
- length_node :dy, "@dy", :default_value => "0 pt"
12
- end
13
-
@@ -1,47 +0,0 @@
1
- require 'xml/mapping/base'
2
- class Length
3
- attr_accessor :value, :unit, :number
4
-
5
- def initialize(value)
6
- @value = value
7
- @number = value.match(/[\d\.]*/)[0].to_f
8
- @unit = value.delete("#{number}").strip
9
- end
10
-
11
- #Return the numeric portion as a Points
12
- def as_pts
13
- if @unit =~ /pt/
14
- return @number
15
- elsif @unit =~ /in/
16
- return @number * 72 #72.270
17
- elsif @unit =~ /mm/
18
- return @number * 2.83464566929134
19
- elsif @unit =~ /cm/
20
- return @number * 28.3464566929134
21
- elsif @unit =~ /pc/
22
- return 1.0 * @number / 12
23
- elsif @unit == ''
24
- return @number
25
- else
26
- raise "Unit #{unit} unknown"
27
- end
28
- end
29
- end
30
-
31
- class LengthNode < XML::Mapping::SingleAttributeNode
32
- def initialize_impl(path)
33
- @path = XML::XXPath.new(path)
34
- end
35
-
36
- def extract_attr_value(xml)
37
- @value = default_when_xpath_err{@path.first(xml).text}
38
- Length.new(@value)
39
- end
40
-
41
- def set_attr_value(xml, value)
42
- raise "Not a Length: #{value}" unless Length===value
43
- @path.first(xml,:ensure_created=>true).text = value.value
44
- end
45
- end
46
-
47
- XML::Mapping.add_node_class LengthNode
@@ -1,25 +0,0 @@
1
- require 'length_node'
2
-
3
-
4
- class MarkupMargin
5
- include XML::Mapping
6
- length_node :size, "@size"
7
-
8
- end
9
-
10
- class MarkupLine
11
- include XML::Mapping
12
- length_node :x1, "@x1"
13
- length_node :y1, "@y1"
14
- length_node :x2, "@x2"
15
- length_node :y2, "@y2"
16
- end
17
-
18
- class MarkupCircle
19
- include XML::Mapping
20
- length_node :x0, "@x0"
21
- length_node :y0, "@y0"
22
- length_node :radius, "@radius"
23
- end
24
-
25
-
@@ -1,171 +0,0 @@
1
- $: << File.expand_path(File.dirname(__FILE__) + "/../vendor")
2
- require 'xml/mapping'
3
- require 'glabel_template'
4
- require 'pdf/writer'
5
- #--- require 'breakpoint'
6
-
7
- class PDFLabelPage
8
- attr_accessor :gt, :template, :label, :pdf
9
- @@gt = nil
10
- def initialize(template_name, pdf_opts = {})
11
- @@gt || PDFLabelPage.load_template_set
12
- unless @template = @@gt.find_template(template_name)
13
- raise "Template not found!"
14
- end
15
- #if the template specifies the paper type, and the user didn't use it.
16
- if @template.size && !pdf_opts.has_key?(:paper)
17
- pdf_opts[:paper] = @template.size.gsub(/^.*-/,'')
18
- end
19
- #TODO figure out how to cope with multiple label types on a page
20
- @label = @template.labels["0"]
21
- #TODO figure out how to handle multiple layouts
22
- @layout = @label.layouts[0]
23
- @pdf = PDF::Writer.new(pdf_opts)
24
- @pdf.margins_pt(0, 0, 0, 0)
25
- end
26
-
27
- def PDFLabelPage.load_template_set(template_set_file=nil)
28
- template_set_file ||= File.expand_path(File.dirname(__FILE__) + "/../templates/avery-us-templates.xml")
29
- @@gt = GlabelsTemplate.load_from_file(template_set_file)
30
- end
31
-
32
- def PDFLabelPage.all_template_names
33
- @@gt || PDFLabelPage.load_template_set
34
- @@gt.find_all_templates
35
- end
36
-
37
- =begin rdoc
38
- add_label takes an argument hash.
39
- [: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
40
- [:x & :y] The (x,y) coordinates on the page to print the text. Ignored if position is specified.
41
- [:text] What you want to print in the label. Defaults to the (x,y) of the top left corner of the label.
42
- [:use_margin] If the label has a markupMargin, setting this argument to true will respect that margin when writing text. Defaults to true.
43
- [:justification] Values can be :left, :right, :center, :full. Defaults to :left
44
- [:offset_x, offset_y] If your printer doesn't want to print with out margins you can define these values to fine tune printout.
45
- =end
46
- def add_label(options = {})
47
- if position = options[:position]
48
- label_x, label_y = position_to_x_y(position)
49
- elsif((label_x = options[:x]) && (label_y = options[:y]))
50
- else
51
- label_x, label_y = position_to_x_y(0)
52
- end
53
- #line wrap margin
54
- label_width = label_x + @label.width.as_pts
55
-
56
- if (use_margin = options[:use_margin]).nil?
57
- use_margin = true
58
- end
59
- if use_margin
60
- @label.markupMargins.each {|margin|
61
- label_x = label_x + margin.size.as_pts
62
- label_y = label_y - margin.size.as_pts
63
- label_width = label_width - margin.size.as_pts
64
- }
65
- end
66
-
67
- if offset = options[:offset_x]
68
- label_x = label_x + offset
69
- label_width = label_width + offset
70
- end
71
- if offset = options[:offset_y]
72
- label_y = label_y + offset
73
- end
74
- text = options[:text] || "[#{label_x / 72}, #{label_y / 72}]"
75
- arg_hash = {:justification => (options[:justification] || :left),
76
- :font_size => (options[:font_size] || 12)}
77
-
78
- arg_hash = arg_hash.merge :absolute_left => label_x,
79
- :absolute_right => label_width
80
- @pdf.y = label_y
81
- @pdf.text(text,arg_hash)
82
- end
83
- =begin rdoc
84
- You can add the same text to many labels this way, takes all the arguments of add_label, but must have position instead of x,y. Requires count.
85
- [:count] - Number of labels to print
86
- =end
87
- def add_many_labels(options = {})
88
- if (options[:x] || options[:y]) && !options[:position]
89
- raise "Can't use X,Y with add_many_labels, you must use position"
90
- end
91
- if !options[:position]
92
- options[:position] = 0
93
- end
94
- raise "Count required" unless options[:count]
95
- count = options[:count]
96
- count.times do
97
- add_label(options)
98
- options[:position] = options[:position] + 1
99
- end
100
- end
101
- =begin rdoc
102
- To facilitate aligning a printer we give a method that prints the outlines of the labels
103
- =end
104
- def draw_boxes(write_coord = true, draw_markups = true)
105
- @layout.nx.times do |x|
106
- @layout.ny.times do |y|
107
- box_x, box_y = get_x_y(x, y)
108
- @pdf.rounded_rectangle(box_x,
109
- box_y,
110
- @label.width.as_pts,
111
- @label.height.as_pts,
112
- @label.round.as_pts).stroke
113
- if write_coord
114
- text = "#{box_x / 72}, #{box_y / 72}, #{@label.width.number}, #{label.height.number}"
115
- add_label(:x => box_x, :y => box_y, :text => text)
116
- end
117
-
118
- if draw_markups
119
- @label.markupMargins.each {|margin|
120
- size = margin.size.as_pts
121
- @pdf.rounded_rectangle(box_x + size,
122
- box_y - margin.size.as_pts,
123
- @label.width.as_pts - 2*size,
124
- @label.height.as_pts - 2*size,
125
- @label.round.as_pts).stroke
126
- }
127
- @label.markupLines.each {|line|
128
- @pdf.line(box_x + line.x1.as_pts,
129
- box_y + line.y1.as_pts,
130
- box_x + line.x2.as_pts,
131
- box_y + line.y2.as_pts).stroke
132
- }
133
- =begin TODO Draw cirles
134
- @label.markupCircles.each {|cicle|
135
- @pdf.
136
- =end
137
- end
138
-
139
- end
140
- end
141
- end
142
-
143
- def save_as(file_name)
144
- @pdf.save_as(file_name)
145
- end
146
-
147
- protected
148
-
149
- =begin rdoc
150
- Position is top to bottom, left to right, starting at 1 and ending at the end of the page
151
- =end
152
- def position_to_x_y(position)
153
- x = (position * 1.0 / @layout.ny).floor
154
- y = position % @layout.ny
155
- return get_x_y(x, y)
156
- end
157
-
158
- def get_x_y(x, y)
159
- label_y = @pdf.absolute_top_margin
160
- label_y = label_y + @pdf.top_margin
161
- label_y = label_y - @layout.y0.as_pts
162
- label_y = label_y - y * @layout.dy.as_pts
163
-
164
- label_x = @pdf.absolute_left_margin
165
- label_x = label_x - @pdf.left_margin
166
- label_x = label_x + @layout.x0.as_pts
167
- label_x = label_x + x * @layout.dx.as_pts
168
-
169
- return label_x, label_y
170
- end
171
- end
@@ -1,3 +0,0 @@
1
- class Pdflabels
2
- VERSION = '1.0.1'
3
- end
@@ -1,37 +0,0 @@
1
- require 'alias'
2
- require 'label'
3
- require 'markup'
4
-
5
- class Template
6
- include XML::Mapping
7
- attr_accessor :labels
8
-
9
- text_node :name, "@name"
10
- text_node :size, "@size"
11
- text_node :description, "@description", :default_value => ""
12
- text_node :_description, "@_description", :default_value => ""
13
- length_node :width, "@width", :default_value => nil
14
- length_node :height, "@height", :default_value => nil
15
-
16
- #TODO this could be cleaner, but I'm not sure how yet
17
- hash_node :labelRectangles, "Label-rectangle", "@id", :class => LabelRectangle, :default_value => nil
18
- hash_node :labelRounds, "Label-round", "@id", :class => LabelRound, :default_value => nil
19
- hash_node :labelCDs, "Label-cd", "@id", :class => LabelCD, :default_value => nil
20
-
21
- hash_node :alias, "Alias", "@name", :class => Alias, :default_value => Hash.new
22
-
23
- def initialize
24
-
25
- end
26
-
27
- def labels
28
- @labels = Hash.new
29
- @labels = @labels.merge @labelRectangles
30
- @labels = @labels.merge @labelRounds
31
- @labels = @labels.merge @labelCDs
32
- @labels
33
- end
34
-
35
- end
36
-
37
-