rdpl 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -19,3 +19,4 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ tags
@@ -1,6 +1,106 @@
1
1
  = rdpl
2
2
 
3
- Description goes here.
3
+ Rdpl is a simple gem that abstracts *some* of the DPL (Datamax Programming Language)(TM) concepts. Its main purpose is to simplify the creation of labels to be printed using the Datamax(TM) series of printers.
4
+
5
+ It uses a very simple approach to send jobs to the printer. Basically, the printer has to be configured inside CUPS (Rdpl will use <tt>lpr</tt> to print), so it won't work on systems where CUPS is absent.
6
+
7
+ == Jobs
8
+
9
+ Print jobs are represented through instances of <tt>Rdpl::Job</tt>. A job may contain several labels to be printed and must receive the printer's cups id.
10
+
11
+ job = Rdpl::Job.new :printer => "some_printer_id"
12
+ job.print
13
+
14
+ Take a look at the project's rdoc for available options.
15
+
16
+ == Labels
17
+
18
+ Labels are represented through instances of <tt>Rdpl::Label</tt>. A label may contain several elements, which may be text, barcodes, lines, boxes or other graphical elements.
19
+
20
+ job = Rdpl::Job.new :printer => "some_printer_id"
21
+ label = Rdpl::Label.new(:quantity => 3, :dot_size => 12)
22
+ label << "some text"
23
+ label << Rdpl::Barcode.new :data => "123456"
24
+ job << label
25
+ job.print
26
+
27
+ Labels also accept a block when adding new lines, barcodes, boxes or bitmapped text:
28
+
29
+ label = Label.new
30
+ label.add_line do |line|
31
+ line.horizontal_width = 12.2
32
+ line.vertical_width = 14.3
33
+ line.row_position = 23.4
34
+ line.column_position = 24.5
35
+ end
36
+
37
+ It'll always yield a new instance of the given element to the block, so you can specify the element's properties. The other available methods to add new elements to the label are <tt>add_barcode</tt>, <tt>add_box</tt> and <tt>add_bitmapped_text</tt>.
38
+
39
+ Take a look at the project's rdoc for available options.
40
+
41
+ == Elements
42
+
43
+ You can add elements to the labels. Elements may be text, barcodes, lines, boxes or text printed with internal bitmapped fonts. Elements may have a rotation angle, row and column positions and so on. Each kind of element may have specific options, check the docs.
44
+
45
+ == Barcodes
46
+
47
+ Barcodes may be created through the <tt>Rdpl::Element::Barcode</tt> class.
48
+
49
+ barcode = Rdpl::Barcode.new(
50
+ :rotation => 4,
51
+ :font_id => Rdpl::Barcode::CODE_128,
52
+ :data => 'SOME DATA 12345',
53
+ :height => 123,
54
+ :wide_bar_multiplier => 3,
55
+ :narrow_bar_multiplier => 4,
56
+ :row_position => 123,
57
+ :column_position => 234
58
+ )
59
+
60
+ Take a look at the project's rdoc for available options.
61
+
62
+ == Boxes
63
+
64
+ Boxes may be created through the <tt>Rdpl::Element::Box</tt> class.
65
+
66
+ box = Rdpl::Box.new(
67
+ :horizontal_width => 12.2,
68
+ :vertical_width => 14.3,
69
+ :row_position => 23.4,
70
+ :column_position => 24.5,
71
+ :bottom_and_top_thickness => 34.6,
72
+ :sides_thickness => 45.6
73
+ )
74
+
75
+ Take a look at the project's rdoc for available options.
76
+
77
+ == Lines
78
+
79
+ Lines may be created through the <tt>Rdpl::Element::Line</tt> class.
80
+
81
+ line = Rdpl::Line.new(
82
+ :horizontal_width => 12.2,
83
+ :vertical_width => 14.3,
84
+ :row_position => 23.4,
85
+ :column_position => 24.5
86
+ )
87
+
88
+ Take a look at the project's rdoc for available options.
89
+
90
+ == Text in Bitmapped fonts
91
+
92
+ Text elements printed using internal bitmapped fonts may be created through the <tt>Rdpl::Element::BitmappedText</tt> class.
93
+
94
+ text = Rdpl::BitmappedText.new(
95
+ :font_id => 2,
96
+ :width_multiplier => 2,
97
+ :height_multiplier => 3,
98
+ :row_position => 20,
99
+ :column_position => 30,
100
+ :data => 'HEY LOOK AT ME'
101
+ )
102
+
103
+ Take a look at the project's rdoc for available options.
4
104
 
5
105
  == Note on Patches/Pull Requests
6
106
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -1,4 +1,5 @@
1
1
  module Rdpl
2
+ # Represents a barcode to be printed.
2
3
  class Barcode
3
4
  include Element
4
5
 
@@ -12,11 +13,13 @@ module Rdpl
12
13
 
13
14
  DEFAULT_HEIGHT = 25
14
15
 
16
+ # Sets the barcode height. Valid values go from 0 to 999.
15
17
  def height=(height)
16
18
  raise InvalidBarcodeHeightError unless valid_height_range.include?(height)
17
19
  @height = height
18
20
  end
19
21
 
22
+ # Returns the barcode's height. Defaults to 25.
20
23
  def height
21
24
  @height || DEFAULT_HEIGHT
22
25
  end
@@ -1,4 +1,5 @@
1
1
  module Rdpl
2
+ # Represents some text printed using an internal bitmapped font.
2
3
  class BitmappedText
3
4
  include Element
4
5
 
@@ -13,10 +13,28 @@ module Rdpl
13
13
  class FixedValueError < StandardError; end
14
14
  class InvalidAssigmentError < StandardError; end
15
15
 
16
+ # Initializes a new element. Available options are:
17
+ # * <tt>:rotation</tt> the element's rotation.
18
+ # * <tt>:font_id</tt> the element's font id. Can go from 'a' to 'z' or 'A' to 'Z' for barcodes or from '0' to '9' for bitmapped fonts.
19
+ # * <tt>:data</tt> the data to be printed or encoded, usually some text.
20
+ # * <tt>:height</tt> the element's height. Can go from 0 to 999 for barcodes or any other value for another kind of element.
21
+ # * <tt>:row_position</tt> the vertical position of the element inside the label, in hundredths of an inch or tenths of a millimeter.
22
+ # * <tt>:column_position</tt> the horizontal position of the element inside the label, in hundredths of an inch or tenths of a millimeter.
23
+ # * <tt>:bottom_and_top_thickness</tt> defines the thickness of bottom and top box edges. Used only for <tt>Rdpl::Element::Box</tt>.
24
+ # * <tt>:sides_thickness</tt> defines the thickness of the box sides. Used only for <tt>Rdpl::Element::Box</tt>.
25
+ # * <tt>:width_multiplier</tt>
26
+ # * <tt>:height_multiplier</tt>
16
27
  def initialize(options = {})
17
28
  options.each_pair { |option, value| self.send "#{option}=", value }
18
29
  end
19
30
 
31
+ # Sets the element's rotation angle. Can be one of the following constants:
32
+ # * <tt>Rdpl::Element::ROTATION_0_DEGREES</tt>
33
+ # * <tt>Rdpl::Element::ROTATION_90_DEGREES</tt>
34
+ # * <tt>Rdpl::Element::ROTATION_180_DEGREES</tt>
35
+ # * <tt>Rdpl::Element::ROTATION_270_DEGREES</tt>
36
+ #
37
+ # Raises <tt>Rdpl::Element::InvalidRotationError</tt> if passed a value different from these constants.
20
38
  def rotation=(rotation)
21
39
  raise InvalidRotationError, rotation unless valid_rotation_range.include?(rotation)
22
40
  @rotation = rotation
@@ -28,12 +46,12 @@ module Rdpl
28
46
 
29
47
  # Sets the element font type. Available types are:
30
48
  #
31
- # Type Interpretation
32
- # 0-9 Font
33
- # A-T Barcode with human readable text
34
- # a-z Barcode without human readable text
35
- # X Line, box, polygon, circle
36
- # Y Image
49
+ # * Type Interpretation
50
+ # * 0-9 Font
51
+ # * A-T Barcode with human readable text
52
+ # * a-z Barcode without human readable text
53
+ # * X Line, box, polygon, circle
54
+ # * Y Image
37
55
  def font_id=(font_id)
38
56
  raise InvalidFontIdError unless valid_font_id_ranges.any? { |range| range.include? font_id }
39
57
  @font_id = font_id
@@ -49,27 +67,29 @@ module Rdpl
49
67
  @width_multiplier = multiplier
50
68
  end
51
69
 
70
+ # Returns the specified width multiplier. Defaults to 1.
52
71
  def width_multiplier
53
72
  @width_multiplier || 1
54
73
  end
55
74
 
75
+ # Valid values goes from 1 to 9 and A to O (base 25)
56
76
  def height_multiplier=(multiplier)
57
77
  raise InvalidHeightMultiplierError, multiplier unless valid_width_or_height_multiplier?(multiplier)
58
78
  @height_multiplier = multiplier
59
79
  end
60
80
 
61
- # Valid values goes from 1 to 9 and A to O (base 25)
81
+ # Returns the specified height multiplier. Defaults to 1.
62
82
  def height_multiplier
63
83
  @height_multiplier || 1
64
84
  end
65
85
 
66
- # Used only by barcode and smooth/scaleble fonts, but has to be present as 000
86
+ # Used only by barcode and smooth/scalable fonts, but has to be present as 000
67
87
  # in other elements.
68
88
  def formatted_height
69
89
  '000'
70
90
  end
71
91
 
72
- # Interpreted in hundredths of an inch or tenths of millimeters, depending on
92
+ # Interpreted in hundreths of an inch or tenths of millimeters, depending on
73
93
  # the measurement used in the printing job.
74
94
  def row_position=(position)
75
95
  @row_position = position
@@ -1,4 +1,5 @@
1
1
  module Rdpl
2
+ # Represents a line to be printed in the label.
2
3
  class Line
3
4
  DEFAULT_CHARACTER = 'l'
4
5
 
@@ -5,23 +5,25 @@ module Rdpl
5
5
  include Element
6
6
  include Graphic
7
7
 
8
+ # Returns the element's vertical width. Defaults to 0.
8
9
  def vertical_width
9
10
  @vertical_width || 0
10
11
  end
11
12
 
13
+ # Returns the element's horizontal width. Defaults to 0.
12
14
  def horizontal_width
13
15
  @horizontal_width || 0
14
16
  end
15
17
 
16
- def width_multiplier=(multiplier)
18
+ def width_multiplier=(multiplier) # :nodoc:
17
19
  raise FixedValueError, 'for lines width multiplier is fixed to 1'
18
20
  end
19
21
 
20
- def height_multiplier=(multiplier)
22
+ def height_multiplier=(multiplier) # :nodoc:
21
23
  raise FixedValueError, 'for lines height multiplier is fixed to 1'
22
24
  end
23
25
 
24
- def font_id
26
+ def font_id # :nodoc:
25
27
  'X'
26
28
  end
27
29
 
data/lib/job.rb CHANGED
@@ -1,6 +1,13 @@
1
1
  require 'tempfile'
2
2
 
3
3
  module Rdpl
4
+ # A +Job+ instance represents a print job to be sent to the printer. A print job
5
+ # may contain a list of instances of Rdpl::Label.
6
+ #
7
+ # The default process to send jobs to the printer is configuring the printer in
8
+ # cups and passing the printer's cups identifier to the constructor. Rdpl will use
9
+ # this id and issue something like +lpr -P cups_id some_temp_file+. This makes this lib
10
+ # unusable if you're not on some kind of *nix box.
4
11
  class Job
5
12
  attr_reader :labels, :printer, :state
6
13
  attr_writer :sensor
@@ -8,6 +15,17 @@ module Rdpl
8
15
  include Commandable
9
16
  include Enumerable
10
17
 
18
+ # Creates a new instance of Rdpl::Job
19
+ #
20
+ # Example:
21
+ #
22
+ # job = Rdpl::Job.new :printer => "some_cups_id"
23
+ #
24
+ # The possible options are:
25
+ #
26
+ # * <tt>:printer</tt> the cups id of the printer to be used. This option is required.
27
+ # * <tt>:sensor</tt> the type of sensor to be used. Can be one of <tt>Rdpl::Sensor::REFLEXIVE</tt> or <tt>Rdpl::Sensor::EDGE</tt>. This is optional and defaults to <tt>Rdpl::Sensor::EDGE</tt>
28
+ # * <tt>:measurement</tt> the measurement system to be used. Can be one of :inches or :metric. For metric, mm will be used as unit. This is optional and defaults to <tt>:inches</tt>.
11
29
  def initialize(options = {})
12
30
  initialize_options options
13
31
  @contents = ''
@@ -15,18 +33,26 @@ module Rdpl
15
33
  @labels = []
16
34
  end
17
35
 
36
+ # Yields each <tt>Rdpl::Label</tt> instance contained in this job.
18
37
  def each
19
38
  @labels.each { |label| yield label }
20
39
  end
21
40
 
41
+ # Returns the current sensor type. If no <tt>:sensor</tt> option was specified, defaults
42
+ # to <tt>Rdpl::Sensor::EDGE</tt>.
22
43
  def sensor; @sensor ||= Sensor::EDGE; end
23
44
 
45
+ # Returns the current measurement system in use.
24
46
  def measurement; @measurement ||= :inches; end
25
47
 
48
+ # Returns <tt>true</tt> if the current measurement system is <tt>:inches</tt>.
26
49
  def in?; measurement == :inches; end
27
50
 
51
+ # Returns <tt>true</tt> if the current measurement system is <tt>:metric</tt>.
28
52
  def mm?; measurement == :metric; end
29
53
 
54
+ # Adds a new <tt>Rdpl::Label</tt> to be printed in this job. After a label in added,
55
+ # the job will insert a <tt>FEED</tt> command (<tt>STX F <CR><LF></tt>).
30
56
  def <<(label)
31
57
  @labels << label
32
58
  label.job = self
@@ -35,10 +61,17 @@ module Rdpl
35
61
  end
36
62
  alias :add_label :<<
37
63
 
64
+ # Dumps the contents to be printed to a string.
38
65
  def dump; @contents.dup; end
39
66
 
67
+ # Insert a <tt>FEED</t> command (<tt>STX F <CR><LF></tt>).
40
68
  def feed; command FEED; end
41
69
 
70
+ # Sends the job's contents to the printer. The destination will be the cups
71
+ # printer id informed in the job's creation.
72
+ #
73
+ # The printing process is very simple, Rdpl will create a temp file and issue a <tt>lpr</tt> system
74
+ # command using the cups printer id and this file.
42
75
  def print
43
76
  tempfile = Tempfile.new 'datamax_label'
44
77
  tempfile << dump
@@ -1,4 +1,5 @@
1
1
  module Rdpl
2
+ # Represents a label to be printed. Labels must me included in an instance of <tt>Rdpl::Job</tt>.
2
3
  class Label
3
4
  attr_reader :state, :quantity
4
5
  attr_writer :job
@@ -10,38 +11,60 @@ module Rdpl
10
11
  DEFAULT_DOT_SIZE = 11
11
12
  DEFAULT_HEAT = 14
12
13
 
14
+ # Creates a new instance of <tt>Rdpl::Label</tt>
15
+ #
16
+ # Available options are:
17
+ #
18
+ # * <tt>:heat</tt> the heat setting to be used when printing this label. This is optional and defaults to 14.
19
+ # * <tt>:dot_size</tt> the dot size to be used when printing this label. This is optional and defaults to 11.
20
+ # * <tt>:start_of_print</tt> the inicial position to start printing the label. This is optional and has no default value, since this setting depends on the printer model.
21
+ # * <tt>:quantity</tt> the number of copies of this label to be printed.
13
22
  def initialize(options = {})
14
23
  @contents = ''
15
24
  start options
16
25
  end
17
26
 
27
+ # Dumps this label's contents to a string.
18
28
  def dump
19
29
  @contents.dup
20
30
  end
21
31
 
32
+ # Closes this label for edition. This appends DPL specific commands to the label,
33
+ # so the printer knows where the printing must end and where another label is started.
22
34
  def end!
23
35
  self << formatted_quantity unless quantity.nil?
24
36
  self << FINISH
25
37
  self.state = :finished
26
38
  end
27
39
 
28
- def [](arg)
40
+ def [](arg) # :nodoc:
29
41
  @contents[arg]
30
42
  end
31
43
 
44
+ # Adds a new element to this label.
45
+ # An element can be one of the following:
46
+ # * <tt>Rdpl::Barcode</tt>
47
+ # * <tt>Rdpl::BitmappedText</tt>
48
+ # * <tt>Rdpl::Box</tt>
49
+ # * <tt>Rdpl::Line</tt>
50
+ # * A simple string containing DPL commands
32
51
  def <<(arg)
33
52
  raise EndedElementError unless state == :open
34
53
  @contents << arg.to_s << NEW_LINE
35
54
  end
36
55
 
56
+ # Returns the current dot size.
37
57
  def dot_size
38
58
  @dot_size || DEFAULT_DOT_SIZE
39
59
  end
40
60
 
61
+ # Returns the current heat setting to be used when printing.
41
62
  def heat
42
63
  @heat || DEFAULT_HEAT
43
64
  end
44
65
 
66
+ # Returns <tt>true</tt> if this label's job is setted to use millimeters as measurement unit.
67
+ # I the label does not have a job yet, it will return <tt>false</tt>.
45
68
  def mm?
46
69
  @job ? @job.mm? : false
47
70
  end
@@ -72,9 +95,9 @@ module Rdpl
72
95
  def start(options = {})
73
96
  self.state = :open
74
97
  command START
98
+ options.each_pair { |option, value| self.send("#{option}=", value) }
75
99
  self << formatted_heat
76
100
  self << formatted_dot_size
77
- options.each_pair { |option, value| self.send("#{option}=", value) }
78
101
  end
79
102
 
80
103
  [:state, :heat, :dot_size, :start_of_print, :quantity].each do |method|
@@ -8,7 +8,7 @@ module Rdpl
8
8
  NEW_LINE = CR + LF
9
9
  FEED = 'F'
10
10
 
11
- module Commandable
11
+ module Commandable # :nodoc:
12
12
  def command(param)
13
13
  raise EndedElementError if self.state == :finished
14
14
  @contents << STX << param << NEW_LINE
@@ -20,8 +20,10 @@ module Rdpl
20
20
  EDGE = 'e'
21
21
  end
22
22
 
23
+ # Raised when a job is created without a printer name
23
24
  class MissingPrinterNameError < StandardError; end
24
- class EndedElementError < StandardError; end
25
+ # Raised when inserting a new command to a already closed job.
26
+ class EndedElementError < StandardError; end
25
27
  end
26
28
 
27
29
  require 'job'
@@ -0,0 +1,81 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rdpl}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["C\303\241ssio Marques"]
12
+ s.date = %q{2010-09-24}
13
+ s.description = %q{RDPL provides a way to create labels for the Datamax™ printers using plain Ruby, through an abstraction of the DPL (Datamax Programming Language)}
14
+ s.email = %q{cassiommc@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ ".rvmrc",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/elements/barcode.rb",
28
+ "lib/elements/bitmapped_text.rb",
29
+ "lib/elements/box.rb",
30
+ "lib/elements/element.rb",
31
+ "lib/elements/graphic.rb",
32
+ "lib/elements/line.rb",
33
+ "lib/elements/lines_and_boxes.rb",
34
+ "lib/job.rb",
35
+ "lib/label.rb",
36
+ "lib/rdpl.rb",
37
+ "rdpl.gemspec",
38
+ "spec/elements/barcode_spec.rb",
39
+ "spec/elements/bitmapped_text_spec.rb",
40
+ "spec/elements/box_spec.rb",
41
+ "spec/elements/line_spec.rb",
42
+ "spec/job_spec.rb",
43
+ "spec/label_spec.rb",
44
+ "spec/rdpl_spec.rb",
45
+ "spec/shared_examples/element.rb",
46
+ "spec/shared_examples/lines_and_boxes.rb",
47
+ "spec/spec.opts",
48
+ "spec/spec_helper.rb"
49
+ ]
50
+ s.homepage = %q{http://github.com/cassiomarques/rdpl}
51
+ s.rdoc_options = ["--charset=UTF-8"]
52
+ s.require_paths = ["lib"]
53
+ s.rubygems_version = %q{1.3.7}
54
+ s.summary = %q{Create Datamax™ labels using Ruby!}
55
+ s.test_files = [
56
+ "spec/elements/barcode_spec.rb",
57
+ "spec/elements/bitmapped_text_spec.rb",
58
+ "spec/elements/box_spec.rb",
59
+ "spec/elements/line_spec.rb",
60
+ "spec/job_spec.rb",
61
+ "spec/label_spec.rb",
62
+ "spec/rdpl_spec.rb",
63
+ "spec/shared_examples/element.rb",
64
+ "spec/shared_examples/lines_and_boxes.rb",
65
+ "spec/spec_helper.rb"
66
+ ]
67
+
68
+ if s.respond_to? :specification_version then
69
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
70
+ s.specification_version = 3
71
+
72
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
73
+ s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
74
+ else
75
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
76
+ end
77
+ else
78
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
79
+ end
80
+ end
81
+
@@ -114,6 +114,7 @@ describe Rdpl::Job do
114
114
  job.dump[-4..-3].should == Rdpl::STX + Rdpl::FEED
115
115
  end
116
116
  end
117
+
117
118
  describe "#print" do
118
119
  let(:temp_file) { "" }
119
120
 
@@ -66,7 +66,7 @@ describe Rdpl::Label do
66
66
  end
67
67
  end
68
68
 
69
- it "raises Rdpl::EndedElementError if it's ended andwe try to add new content" do
69
+ it "raises Rdpl::EndedElementError if it's ended and we try to add new content" do
70
70
  label = Rdpl::Label.new
71
71
  label.end!
72
72
  lambda do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdpl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - "C\xC3\xA1ssio Marques"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-21 00:00:00 -03:00
18
+ date: 2010-09-24 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -61,6 +61,7 @@ files:
61
61
  - lib/job.rb
62
62
  - lib/label.rb
63
63
  - lib/rdpl.rb
64
+ - rdpl.gemspec
64
65
  - spec/elements/barcode_spec.rb
65
66
  - spec/elements/bitmapped_text_spec.rb
66
67
  - spec/elements/box_spec.rb