zebra-zpl 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,72 +1,124 @@
1
- require "zebra/zpl/printable"
2
-
3
- module Zebra
4
- module Zpl
5
- class Barcode
6
- include Printable
7
-
8
- class InvalidRatioError < StandardError; end
9
- class InvalidNarrowBarWidthError < StandardError; end
10
- class InvalidWideBarWidthError < StandardError; end
11
-
12
- attr_accessor :height
13
- attr_reader :type, :ratio, :narrow_bar_width, :wide_bar_width
14
- attr_writer :print_human_readable_code
15
-
16
- def width=(value)
17
- @width = value || 0
18
- end
19
-
20
- def width
21
- @width || @narrow_bar_width || @wide_bar_width || 0
22
- end
23
-
24
- def type=(type)
25
- BarcodeType.validate_barcode_type(type)
26
- @type = type
27
- end
28
-
29
- def ratio=(value)
30
- raise InvalidRatioError unless value.to_f >= 2.0 && value.to_f <= 3.0
31
- @ratio = value
32
- end
33
-
34
- def ratio
35
- if !@wide_bar_width.nil? && !@narrow_bar_width.nil?
36
- (@wide_bar_width.to_f / @narrow_bar_width.to_f).round(1)
37
- else
38
- @ratio
39
- end
40
- end
41
-
42
- def narrow_bar_width=(value)
43
- raise InvalidNarrowBarWidthError unless (1..10).include?(value.to_i)
44
- @narrow_bar_width = value
45
- end
46
-
47
- def wide_bar_width=(value)
48
- raise InvalidWideBarWidthError unless (2..30).include?(value.to_i)
49
- @wide_bar_width = value
50
- end
51
-
52
- def print_human_readable_code
53
- @print_human_readable_code || false
54
- end
55
-
56
- def to_zpl
57
- check_attributes
58
- human_readable = print_human_readable_code ? "Y" : "N"
59
- "^FW#{rotation}^FO#{x},#{y}^BY#{width},#{ratio},#{height}^B#{type}#{rotation},,#{human_readable}^FD#{data}^FS"
60
- end
61
-
62
- private
63
-
64
- def check_attributes
65
- super
66
- raise MissingAttributeError.new("the barcode type to be used is not given") unless @type
67
- raise MissingAttributeError.new("the height to be used is not given") unless @height
68
- raise MissingAttributeError.new("the width to be used is not given") unless @width || @narrow_bar_width || @wide_bar_width
69
- end
70
- end
71
- end
72
- end
1
+ require "zebra/zpl/printable"
2
+
3
+ module Zebra
4
+ module Zpl
5
+ class Barcode
6
+ include Printable
7
+
8
+ class InvalidRatioError < StandardError; end
9
+ class InvalidNarrowBarWidthError < StandardError; end
10
+ class InvalidWideBarWidthError < StandardError; end
11
+
12
+ attr_accessor :height, :mode
13
+ attr_reader :type, :ratio, :narrow_bar_width, :wide_bar_width
14
+ attr_writer :print_human_readable_code, :print_text_above
15
+
16
+ def width=(value)
17
+ @width = value || 0
18
+ end
19
+
20
+ def width
21
+ @width || @narrow_bar_width || @wide_bar_width || 0
22
+ end
23
+
24
+ def type=(type)
25
+ BarcodeType.validate_barcode_type(type)
26
+ @type = type
27
+ end
28
+
29
+ def ratio=(value)
30
+ raise InvalidRatioError unless value.to_f >= 2.0 && value.to_f <= 3.0
31
+ @ratio = value
32
+ end
33
+
34
+ def ratio
35
+ if !@wide_bar_width.nil? && !@narrow_bar_width.nil?
36
+ (@wide_bar_width.to_f / @narrow_bar_width.to_f).round(1)
37
+ else
38
+ @ratio
39
+ end
40
+ end
41
+
42
+ def narrow_bar_width=(value)
43
+ raise InvalidNarrowBarWidthError unless (1..10).include?(value.to_i)
44
+ @narrow_bar_width = value
45
+ end
46
+
47
+ def wide_bar_width=(value)
48
+ raise InvalidWideBarWidthError unless (2..30).include?(value.to_i)
49
+ @wide_bar_width = value
50
+ end
51
+
52
+ def print_human_readable_code
53
+ @print_human_readable_code || false
54
+ end
55
+
56
+ def print_text_above
57
+ @print_text_above || false
58
+ end
59
+
60
+ def to_zpl
61
+ check_attributes
62
+
63
+ barcode = case type
64
+ when BarcodeType::CODE_39
65
+ # Code 39 Bar Code: ^B3o,e,h,f,g
66
+ "^B3#{rotation},,,#{interpretation_line},#{interpretation_line_above}"
67
+ when BarcodeType::CODE_93
68
+ # Code 93 Bar Code: ^BAo,h,f,g,e
69
+ "^BA#{rotation},,#{interpretation_line},#{interpretation_line_above}"
70
+ when BarcodeType::CODE_128_AUTO
71
+ # Code 128 Bar Code: ^BCo,h,f,g,e,m
72
+ "^BC#{rotation},,#{interpretation_line},#{interpretation_line_above},,#{mode}"
73
+ when BarcodeType::CODABAR
74
+ # ANSI Codabar Bar Code: ^BKo,e,h,f,g,k,l
75
+ "^BK#{rotation},,,#{interpretation_line},#{interpretation_line_above}"
76
+ when BarcodeType::CODE_AZTEC
77
+ # Aztec Bar Code Parameters: ^B0a,b,c,d,e,f,g
78
+ "^B0#{rotation},6"
79
+ when BarcodeType::CODE_AZTEC_PARAMS
80
+ # Aztec Bar Code Parameters: ^BOa,b,c,d,e,f,g
81
+ "^BO#{rotation},6"
82
+ when BarcodeType::CODE_UPS_MAXICODE
83
+ # UPS MaxiCode Bar Code: ^BDm,n,t
84
+ "^BD"
85
+ when BarcodeType::CODE_QR
86
+ # QR Code Bar Code: ^BQa,b,c,d,e
87
+ "^BQ#{rotation}"
88
+ when BarcodeType::CODE_UPCA
89
+ # UPC-A Bar Code: ^BUo,h,f,g,e
90
+ when BarcodeType::CODE_UPCE
91
+ # UPC-E Bar Code: ^B9o,h,f,g,e
92
+ "^B9#{rotation},,#{interpretation_line},#{interpretation_line_above}"
93
+ when BarcodeType::CODE_EAN8
94
+ # EAN-8 Bar Code: ^B8o,h,f,g
95
+ "^B8#{rotation},,#{interpretation_line},#{interpretation_line_above}"
96
+ when BarcodeType::CODE_EAN13
97
+ # EAN-13 Bar Code: ^BEo,h,f,g
98
+ "^BE#{rotation},,#{interpretation_line},#{interpretation_line_above}"
99
+ else
100
+ raise BarcodeType::InvalidBarcodeTypeError
101
+ end
102
+
103
+ "^FW#{rotation}^FO#{x},#{y}^BY#{width},#{ratio},#{height}#{barcode}^FD#{data}^FS"
104
+ end
105
+
106
+ private
107
+
108
+ def check_attributes
109
+ super
110
+ raise MissingAttributeError.new("the barcode type to be used is not given") unless @type
111
+ raise MissingAttributeError.new("the height to be used is not given") unless @height
112
+ raise MissingAttributeError.new("the width to be used is not given") unless @width || @narrow_bar_width || @wide_bar_width
113
+ end
114
+
115
+ def interpretation_line
116
+ print_human_readable_code ? "Y" : "N"
117
+ end
118
+
119
+ def interpretation_line_above
120
+ print_text_above ? "Y" : "N"
121
+ end
122
+ end
123
+ end
124
+ end
@@ -1,37 +1,38 @@
1
- module Zebra
2
- module Zpl
3
- module BarcodeType
4
- class InvalidBarcodeTypeError < StandardError; end
5
-
6
- CODE_39 = "3"
7
- CODE_93 = "A"
8
- CODE_128_AUTO = "C"
9
- CODABAR = "K"
10
- CODE_AZTEC = "0"
11
- CODE_AZTEC_PARAMS = "O"
12
- CODE_UPS_MAXICODE = "D"
13
- CODE_QR = "Q"
14
- CODE_UPCA = "U"
15
- CODE_UPCE = "9"
16
- CODE_EAN13 = "E"
17
-
18
- # Legacy (EPL) bar code suffixes
19
- # CODE_39 = "3"
20
- # CODE_39_CHECK_DIGIT = "3C"
21
- # CODE_93 = "9"
22
- # CODE_128_AUTO = "1"
23
- # CODE_128_A = "1A"
24
- # CODE_128_B = "1B"
25
- # CODE_128_C = "1C"
26
- # CODABAR = "K"
27
-
28
- def self.valid_barcode_type?(type)
29
- %w(3 A C K 0 O D Q U 9 E).include? type
30
- end
31
-
32
- def self.validate_barcode_type(type)
33
- raise InvalidBarcodeTypeError unless valid_barcode_type?(type)
34
- end
35
- end
36
- end
37
- end
1
+ module Zebra
2
+ module Zpl
3
+ module BarcodeType
4
+ class InvalidBarcodeTypeError < StandardError; end
5
+
6
+ CODE_39 = "3"
7
+ CODE_93 = "A"
8
+ CODE_128_AUTO = "C"
9
+ CODABAR = "K"
10
+ CODE_AZTEC = "0"
11
+ CODE_AZTEC_PARAMS = "O"
12
+ CODE_UPS_MAXICODE = "D"
13
+ CODE_QR = "Q"
14
+ CODE_UPCA = "U"
15
+ CODE_UPCE = "9"
16
+ CODE_EAN8 = "8"
17
+ CODE_EAN13 = "E"
18
+
19
+ # Legacy (EPL) bar code suffixes
20
+ # CODE_39 = "3"
21
+ # CODE_39_CHECK_DIGIT = "3C"
22
+ # CODE_93 = "9"
23
+ # CODE_128_AUTO = "1"
24
+ # CODE_128_A = "1A"
25
+ # CODE_128_B = "1B"
26
+ # CODE_128_C = "1C"
27
+ # CODABAR = "K"
28
+
29
+ def self.valid_barcode_type?(type)
30
+ %w(3 A C K 0 O D Q U 9 8 E).include? type
31
+ end
32
+
33
+ def self.validate_barcode_type(type)
34
+ raise InvalidBarcodeTypeError unless valid_barcode_type?(type)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,47 +1,48 @@
1
- module Zebra
2
- module Zpl
3
- module FontSize
4
- class InvalidFontSizeError < StandardError; end
5
-
6
- SIZE_0 = 12 # tiny
7
- SIZE_1 = 17 # 6pt
8
- SIZE_2 = 22 # 8pt
9
- SIZE_3 = 28 # 10pt
10
- SIZE_4 = 33 # 12pt
11
- SIZE_5 = 44 # 16pt
12
- SIZE_6 = 67 # 24pt
13
- SIZE_7 = 100 # 32pt
14
- SIZE_8 = 111 # 40pt
15
- SIZE_9 = 133 # 48pt
16
-
17
- def self.valid_font_size?(font_size)
18
- (0..32000).include?(font_size.to_i)
19
- end
20
-
21
- def self.validate_font_size(font_size)
22
- raise InvalidFontSizeError unless valid_font_size?(font_size)
23
- end
24
- end
25
-
26
- module FontType
27
- class InvalidFontTypeError < StandardError; end
28
-
29
- TYPE_0 = "0" # 6pt
30
- TYPE_CD = "CD" # 6pt
31
- TYPE_A = "A" # 6pt
32
- TYPE_B = "B" # 6pt
33
- TYPE_E = "E" # 6pt
34
- TYPE_F = "F" # 6pt
35
- TYPE_G = "G" # 6pt
36
- TYPE_H = "H" # 6pt
37
-
38
- def self.valid_font_type?(font_type)
39
- ["0", "CD", "A", "B", "E", "F", "G", "H"].include?(font_type)
40
- end
41
-
42
- def self.validate_font_type(font_type)
43
- raise InvalidFontTypeError unless valid_font_type?(font_type)
44
- end
45
- end
46
- end
47
- end
1
+ module Zebra
2
+ module Zpl
3
+ module FontSize
4
+ class InvalidFontSizeError < StandardError; end
5
+
6
+ SIZE_0 = 12 # tiny
7
+ SIZE_1 = 17 # 6pt
8
+ SIZE_2 = 22 # 8pt
9
+ SIZE_3 = 28 # 10pt
10
+ SIZE_4 = 33 # 12pt
11
+ SIZE_5 = 44 # 16pt
12
+ SIZE_6 = 67 # 24pt
13
+ SIZE_7 = 100 # 32pt
14
+ SIZE_8 = 111 # 40pt
15
+ SIZE_9 = 133 # 48pt
16
+
17
+ def self.valid_font_size?(font_size)
18
+ (0..32000).include?(font_size.to_i)
19
+ end
20
+
21
+ def self.validate_font_size(font_size)
22
+ raise InvalidFontSizeError unless valid_font_size?(font_size)
23
+ end
24
+ end
25
+
26
+ module FontType
27
+ class InvalidFontTypeError < StandardError; end
28
+
29
+ TYPE_0 = "0" # 6pt
30
+ TYPE_CD = "CD" # 6pt
31
+ TYPE_A = "A" # 6pt
32
+ TYPE_B = "B" # 6pt
33
+ TYPE_D = "D" # 6pt
34
+ TYPE_E = "E" # 6pt
35
+ TYPE_F = "F" # 6pt
36
+ TYPE_G = "G" # 6pt
37
+ TYPE_H = "H" # 6pt
38
+
39
+ def self.valid_font_type?(font_type)
40
+ ["0", "CD", "A", "B", "D", "E", "F", "G", "H"].include?(font_type)
41
+ end
42
+
43
+ def self.validate_font_type(font_type)
44
+ raise InvalidFontTypeError unless valid_font_type?(font_type)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,91 +1,93 @@
1
- require "zebra/zpl/printable"
2
-
3
- module Zebra
4
- module Zpl
5
- class Graphic
6
- include Printable
7
-
8
- class InvalidGraphicType < StandardError; end
9
- class InvalidLineThickness < StandardError; end
10
- class InvalidColorError < StandardError; end
11
- class InvalidOrientationError < StandardError; end
12
- class InvalidRoundingDegree < StandardError; end
13
- class InvalidSymbolType < StandardError; end
14
-
15
- attr_reader :graphic_type, :graphic_width, :graphic_height, :line_thickness, :color, :orientation, :rounding_degree, :symbol_type
16
-
17
- BOX = "B"
18
- CIRCLE = "C"
19
- DIAGONAL = "D"
20
- ELLIPSE = "E"
21
- SYMBOL = "S"
22
-
23
- def graphic_type=(type)
24
- raise InvalidGraphicType unless %w(E B D C S).include? type
25
- @graphic_type = type
26
- end
27
-
28
- def graphic_width=(width)
29
- @graphic_width = width
30
- end
31
-
32
- def graphic_height=(height)
33
- @graphic_height = height
34
- end
35
-
36
- def line_thickness=(thickness)
37
- raise InvalidLineThickness unless thickness.nil? || thickness.to_i.to_s == thickness.to_s
38
- @line_thickness = thickness
39
- end
40
-
41
- def color=(value)
42
- raise InvalidColorError unless %w[B W].include?(value&.upcase)
43
- @color = value
44
- end
45
-
46
- def orientation=(value)
47
- raise InvalidOrientationError unless %w[R L].include?(value&.upcase)
48
- @orientation = value
49
- end
50
-
51
- def rounding_degree=(value)
52
- raise InvalidRoundingDegree unless (0..8).include?(value.to_i)
53
- @rounding_degree = value
54
- end
55
-
56
- def symbol_type=(value)
57
- raise InvalidSymbolType unless %w[A B C D E].include?(value.upcase)
58
- @symbol_type = value
59
- end
60
-
61
- def to_zpl
62
- check_attributes
63
- graphic = case graphic_type
64
- when "B"
65
- "B#{graphic_width},#{graphic_height},#{line_thickness},#{color},#{rounding_degree}"
66
- when "C"
67
- "C#{graphic_width},#{line_thickness},#{color}"
68
- when "D"
69
- "D#{graphic_width},#{graphic_height},#{line_thickness},#{color},#{orientation}"
70
- when "E"
71
- "E#{graphic_width},#{graphic_height},#{line_thickness},#{color}"
72
- when "S"
73
- sym = !symbol_type.nil? ? "^FD#{symbol_type}" : ''
74
- "S,#{graphic_height},#{graphic_width}#{sym}"
75
- end
76
- "^FW#{rotation}^FO#{x},#{y}^G#{graphic}^FS"
77
- end
78
-
79
- private
80
-
81
- def has_data?
82
- false
83
- end
84
-
85
- def check_attributes
86
- super
87
- raise InvalidGraphicType if @graphic_type.nil?
88
- end
89
- end
90
- end
91
- end
1
+ require "zebra/zpl/printable"
2
+
3
+ module Zebra
4
+ module Zpl
5
+ class Graphic
6
+ include Printable
7
+
8
+ class InvalidGraphicType < StandardError; end
9
+ class InvalidLineThickness < StandardError; end
10
+ class InvalidColorError < StandardError; end
11
+ class InvalidOrientationError < StandardError; end
12
+ class InvalidRoundingDegree < StandardError; end
13
+ class InvalidSymbolType < StandardError; end
14
+
15
+ attr_reader :graphic_type, :graphic_width, :graphic_height, :line_thickness, :color, :orientation, :rounding_degree, :symbol_type
16
+
17
+ BOX = "B"
18
+ CIRCLE = "C"
19
+ DIAGONAL = "D"
20
+ ELLIPSE = "E"
21
+ SYMBOL = "S"
22
+
23
+ def graphic_type=(type)
24
+ raise InvalidGraphicType unless %w(E B D C S).include? type
25
+ @graphic_type = type
26
+ end
27
+
28
+ def graphic_width=(width)
29
+ @graphic_width = width
30
+ end
31
+
32
+ def graphic_height=(height)
33
+ @graphic_height = height
34
+ end
35
+
36
+ def line_thickness=(thickness)
37
+ raise InvalidLineThickness unless thickness.nil? || thickness.to_i.to_s == thickness.to_s
38
+ @line_thickness = thickness
39
+ end
40
+
41
+ def color=(value)
42
+ raise InvalidColorError unless %w[B W].include?(value&.upcase)
43
+ @color = value
44
+ end
45
+
46
+ def orientation=(value)
47
+ raise InvalidOrientationError unless %w[R L].include?(value&.upcase)
48
+ @orientation = value
49
+ end
50
+
51
+ def rounding_degree=(value)
52
+ raise InvalidRoundingDegree unless (0..8).include?(value.to_i)
53
+ @rounding_degree = value
54
+ end
55
+
56
+ def symbol_type=(value)
57
+ raise InvalidSymbolType unless %w[A B C D E].include?(value.upcase)
58
+ @symbol_type = value
59
+ end
60
+
61
+ def to_zpl
62
+ check_attributes
63
+ graphic = case graphic_type
64
+ when BOX
65
+ "^GB#{graphic_width},#{graphic_height},#{line_thickness},#{color},#{rounding_degree}"
66
+ when CIRCLE
67
+ "^GC#{graphic_width},#{line_thickness},#{color}"
68
+ when DIAGONAL
69
+ "^GD#{graphic_width},#{graphic_height},#{line_thickness},#{color},#{orientation}"
70
+ when ELLIPSE
71
+ "^GE#{graphic_width},#{graphic_height},#{line_thickness},#{color}"
72
+ when SYMBOL
73
+ sym = !symbol_type.nil? ? "^FD#{symbol_type}" : ''
74
+ "^GS,#{graphic_height},#{graphic_width}#{sym}"
75
+ else
76
+ raise InvalidGraphicType
77
+ end
78
+ "^FW#{rotation}^FO#{x},#{y}#{graphic}^FS"
79
+ end
80
+
81
+ private
82
+
83
+ def has_data?
84
+ false
85
+ end
86
+
87
+ def check_attributes
88
+ super
89
+ raise InvalidGraphicType if @graphic_type.nil?
90
+ end
91
+ end
92
+ end
93
+ end