zebra-zpl 1.0.2 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +71 -0
- data/CONTRIBUTING.md +49 -0
- data/Gemfile +6 -0
- data/README.md +348 -89
- data/docs/example.rb +290 -0
- data/docs/images/barcode.png +0 -0
- data/docs/images/datamatrix.png +0 -0
- data/docs/images/earth.jpg +0 -0
- data/docs/images/graphics.png +0 -0
- data/docs/images/image.png +0 -0
- data/docs/images/image_inverted.png +0 -0
- data/docs/images/image_manipulation.png +0 -0
- data/docs/images/images.png +0 -0
- data/docs/images/justification.png +0 -0
- data/docs/images/qrcode.png +0 -0
- data/docs/images/rotation.png +0 -0
- data/docs/images/text.png +0 -0
- data/lib/zebra/print_job.rb +19 -17
- data/lib/zebra/zpl.rb +26 -18
- data/lib/zebra/zpl/barcode.rb +29 -12
- data/lib/zebra/zpl/barcode_type.rb +4 -1
- data/lib/zebra/zpl/box.rb +16 -4
- data/lib/zebra/zpl/comment.rb +15 -0
- data/lib/zebra/zpl/datamatrix.rb +76 -0
- data/lib/zebra/zpl/font.rb +1 -1
- data/lib/zebra/zpl/graphic.rb +91 -0
- data/lib/zebra/zpl/image.rb +97 -0
- data/lib/zebra/zpl/label.rb +3 -14
- data/lib/zebra/zpl/pdf417.rb +50 -0
- data/lib/zebra/zpl/qrcode.rb +6 -2
- data/lib/zebra/zpl/raw.rb +25 -0
- data/lib/zebra/zpl/text.rb +44 -20
- data/lib/zebra/zpl/version.rb +1 -1
- data/spec/fixtures/default.jpg +0 -0
- data/spec/spec_helper.rb +6 -2
- data/spec/zebra/print_job_spec.rb +13 -18
- data/spec/zebra/zpl/barcode_spec.rb +70 -63
- data/spec/zebra/zpl/box_spec.rb +27 -31
- data/spec/zebra/zpl/character_set_spec.rb +7 -7
- data/spec/zebra/zpl/comment_spec.rb +18 -0
- data/spec/zebra/zpl/datamatrix_spec.rb +124 -0
- data/spec/zebra/zpl/graphics_spec.rb +227 -0
- data/spec/zebra/zpl/image_spec.rb +146 -0
- data/spec/zebra/zpl/label_spec.rb +41 -53
- data/spec/zebra/zpl/pdf417_spec.rb +108 -0
- data/spec/zebra/zpl/qrcode_spec.rb +92 -92
- data/spec/zebra/zpl/text_spec.rb +60 -55
- data/zebra-zpl.gemspec +12 -12
- metadata +63 -27
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Zebra::Zpl::CharacterSet do
|
4
4
|
it "can be initialized with the number of data bits" do
|
5
5
|
character_set = described_class.new :number_of_data_bits => 8
|
6
|
-
character_set.number_of_data_bits.
|
6
|
+
expect(character_set.number_of_data_bits).to eq 8
|
7
7
|
end
|
8
8
|
|
9
9
|
it "raises an error when receiving an invalid number of data bits" do
|
@@ -14,7 +14,7 @@ describe Zebra::Zpl::CharacterSet do
|
|
14
14
|
|
15
15
|
it "can be initialized with the language" do
|
16
16
|
character_set = described_class.new :language => Zebra::Zpl::Language::PORTUGUESE
|
17
|
-
character_set.language.
|
17
|
+
expect(character_set.language).to eq Zebra::Zpl::Language::PORTUGUESE
|
18
18
|
end
|
19
19
|
|
20
20
|
it "raises an error with an invalid language" do
|
@@ -25,7 +25,7 @@ describe Zebra::Zpl::CharacterSet do
|
|
25
25
|
|
26
26
|
it "can be initialized with the country code" do
|
27
27
|
character_set = described_class.new :country_code => Zebra::Zpl::CountryCode::LATIN_AMERICA
|
28
|
-
character_set.country_code.
|
28
|
+
expect(character_set.country_code).to eq Zebra::Zpl::CountryCode::LATIN_AMERICA
|
29
29
|
end
|
30
30
|
|
31
31
|
it "raises an error with an invalid country code" do
|
@@ -82,19 +82,19 @@ describe Zebra::Zpl::CharacterSet do
|
|
82
82
|
end
|
83
83
|
|
84
84
|
it "begins with the command 'I'" do
|
85
|
-
character_set.to_zpl.
|
85
|
+
expect(character_set.to_zpl).to match /\AI/
|
86
86
|
end
|
87
87
|
|
88
88
|
it "contains the number of data bits" do
|
89
|
-
tokens[0].match(/I(\d)/)[1].
|
89
|
+
expect(tokens[0].match(/I(\d)/)[1]).to eq "8"
|
90
90
|
end
|
91
91
|
|
92
92
|
it "contains the language" do
|
93
|
-
tokens[1].
|
93
|
+
expect(tokens[1]).to eq Zebra::Zpl::Language::PORTUGUESE
|
94
94
|
end
|
95
95
|
|
96
96
|
it "contains the country code" do
|
97
|
-
tokens[2].
|
97
|
+
expect(tokens[2]).to eq Zebra::Zpl::CountryCode::LATIN_AMERICA
|
98
98
|
end
|
99
99
|
end
|
100
100
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Zebra::Zpl::Comment do
|
4
|
+
it "can be initialized with the data" do
|
5
|
+
comment = described_class.new data: "THIS IS A COMMENT"
|
6
|
+
expect(comment.data).to eq "THIS IS A COMMENT"
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#to_zpl" do
|
10
|
+
subject(:comment) { described_class.new attributes }
|
11
|
+
let(:attributes) {{
|
12
|
+
data: "THIS IS A COMMENT",
|
13
|
+
}}
|
14
|
+
it "contains the comment" do
|
15
|
+
expect(comment.to_zpl).to eq "^FXTHIS IS A COMMENT^FS"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Zebra::Zpl::Datamatrix do
|
4
|
+
it "can be initialized with the symbol height" do
|
5
|
+
datamatrix = described_class.new symbol_height: 3
|
6
|
+
expect(datamatrix.symbol_height).to eq 3
|
7
|
+
end
|
8
|
+
|
9
|
+
it "can be initialized with a quality level" do
|
10
|
+
datamatrix = described_class.new quality: 140
|
11
|
+
expect(datamatrix.quality).to eq 140
|
12
|
+
end
|
13
|
+
|
14
|
+
it "can be initialized with a number of columns" do
|
15
|
+
datamatrix = described_class.new columns: 33
|
16
|
+
expect(datamatrix.columns).to eq 33
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can be initialized with a number of rows" do
|
20
|
+
datamatrix = described_class.new rows: 42
|
21
|
+
expect(datamatrix.rows).to eq 42
|
22
|
+
end
|
23
|
+
|
24
|
+
it "can be initialized with a format" do
|
25
|
+
datamatrix = described_class.new format: 2
|
26
|
+
expect(datamatrix.format).to eq 2
|
27
|
+
end
|
28
|
+
it "can be initialized with a aspect ratio" do
|
29
|
+
datamatrix = described_class.new aspect_ratio: 2
|
30
|
+
expect(datamatrix.aspect_ratio).to eq 2
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#orientation" do
|
34
|
+
it "raises an error if the orientation not in [N I R B]" do
|
35
|
+
expect { described_class.new orientation: 'A' }.to raise_error(Zebra::Zpl::Datamatrix::InvalidOrientationError)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#quality" do
|
40
|
+
it "raises an error if the quality is not one of 0, 50, 80, 100, 140, 200" do
|
41
|
+
expect { described_class.new quality: 20 }.to raise_error(Zebra::Zpl::Datamatrix::InvalidQualityFactorError)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#columns" do
|
46
|
+
it "raises an error if the number of columns is out of bounds" do
|
47
|
+
expect { described_class.new columns: 0 }.to raise_error(Zebra::Zpl::Datamatrix::InvalidSizeError)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "#rows" do
|
52
|
+
it "raises an error if the number of rows is out of bounds" do
|
53
|
+
expect { described_class.new rows: 59 }.to raise_error(Zebra::Zpl::Datamatrix::InvalidSizeError)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
describe "#to_zpl" do
|
59
|
+
let(:valid_attributes) { {
|
60
|
+
position: [50, 50],
|
61
|
+
symbol_height: 5,
|
62
|
+
data: "foobar"
|
63
|
+
}}
|
64
|
+
let(:datamatrix) { described_class.new valid_attributes }
|
65
|
+
let(:tokens) { datamatrix.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
|
66
|
+
|
67
|
+
it "raises an error if the X position is not given" do
|
68
|
+
datamatrix = described_class.new position: [nil, 50], data: "foobar"
|
69
|
+
expect {
|
70
|
+
datamatrix.to_zpl
|
71
|
+
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "raises an error if the Y position is not given" do
|
75
|
+
datamatrix = described_class.new position: [50, nil], data: "foobar"
|
76
|
+
expect {
|
77
|
+
datamatrix.to_zpl
|
78
|
+
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
|
79
|
+
end
|
80
|
+
|
81
|
+
it "raises an error if the data to be printed was not informed" do
|
82
|
+
datamatrix.data = nil
|
83
|
+
expect {
|
84
|
+
datamatrix.to_zpl
|
85
|
+
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
|
86
|
+
end
|
87
|
+
|
88
|
+
it "raises an error if the scale factor is not given" do
|
89
|
+
valid_attributes.delete :symbol_height
|
90
|
+
|
91
|
+
expect {
|
92
|
+
datamatrix.to_zpl
|
93
|
+
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the symbol height to be used is not given")
|
94
|
+
end
|
95
|
+
|
96
|
+
it "contains the barcode command '^B'" do
|
97
|
+
expect(datamatrix.to_zpl).to match /\^B/
|
98
|
+
end
|
99
|
+
|
100
|
+
it "contains the X position" do
|
101
|
+
expect(tokens[2]).to eq "50"
|
102
|
+
end
|
103
|
+
|
104
|
+
it "contains the Y position" do
|
105
|
+
expect(tokens[3]).to eq "50"
|
106
|
+
end
|
107
|
+
|
108
|
+
it "contains Data Matrix code type" do
|
109
|
+
expect(tokens[6]).to eq "^BXN"
|
110
|
+
end
|
111
|
+
|
112
|
+
it "contains the symbol_height" do
|
113
|
+
expect(tokens[7]).to eq "5"
|
114
|
+
end
|
115
|
+
|
116
|
+
it "contains the quality level" do
|
117
|
+
expect(tokens[8]).to eq "200"
|
118
|
+
end
|
119
|
+
|
120
|
+
it "contains the data to be printed in the datamatrix" do
|
121
|
+
expect(tokens[10]).to eq "foobar"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,227 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Zebra::Zpl::Graphic do
|
4
|
+
it "can be initialized with graphic type" do
|
5
|
+
graphic = described_class.new graphic_type: Zebra::Zpl::Graphic::ELLIPSE
|
6
|
+
expect(graphic.graphic_type).to eq "E"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "can be initialized with a graphic width" do
|
10
|
+
graphic = described_class.new graphic_width: 30
|
11
|
+
expect(graphic.graphic_width).to eq 30
|
12
|
+
end
|
13
|
+
|
14
|
+
it "can be initialized with a graphic height" do
|
15
|
+
graphic = described_class.new graphic_height: 30
|
16
|
+
expect(graphic.graphic_height).to eq 30
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can be initialized with a line_thickness" do
|
20
|
+
graphic = described_class.new line_thickness: 3
|
21
|
+
expect(graphic.line_thickness).to eq 3
|
22
|
+
end
|
23
|
+
|
24
|
+
it "can be initialized with a color" do
|
25
|
+
graphic = described_class.new color: "W"
|
26
|
+
expect(graphic.color).to eq "W"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can be initialized with an orientation" do
|
30
|
+
graphic = described_class.new orientation: "R"
|
31
|
+
expect(graphic.orientation).to eq "R"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "can be initialized with a rounding degree" do
|
35
|
+
graphic = described_class.new rounding_degree: 2
|
36
|
+
expect(graphic.rounding_degree).to eq 2
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
describe "#orientation" do
|
42
|
+
it "raises an error if the orientation not in [N L]" do
|
43
|
+
expect { described_class.new orientation: 'A' }.to raise_error(Zebra::Zpl::Graphic::InvalidOrientationError)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#color" do
|
48
|
+
it "raises an error if the color not in [B W]" do
|
49
|
+
expect { described_class.new color: 'A' }.to raise_error(Zebra::Zpl::Graphic::InvalidColorError)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#line_thickness" do
|
54
|
+
it "raises an error if line thickness is not a number" do
|
55
|
+
expect { described_class.new line_thickness: 'A' }.to raise_error(Zebra::Zpl::Graphic::InvalidLineThickness)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#graphic_type" do
|
60
|
+
it "raises an error if the graphic type not in [E B D C S]" do
|
61
|
+
expect { described_class.new graphic_type: 'A' }.to raise_error(Zebra::Zpl::Graphic::InvalidGraphicType)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#to_zpl" do
|
66
|
+
let(:valid_attributes) { {
|
67
|
+
position: [50, 50],
|
68
|
+
graphic_width: 200,
|
69
|
+
graphic_height: 300,
|
70
|
+
line_thickness: 2,
|
71
|
+
color: "B",
|
72
|
+
orientation: "L",
|
73
|
+
rounding_degree: 3,
|
74
|
+
symbol_type: "C"
|
75
|
+
}}
|
76
|
+
let(:graphic_ellipse) { described_class.new valid_attributes.merge({graphic_type: Zebra::Zpl::Graphic::ELLIPSE}) }
|
77
|
+
let(:graphic_diagonal) { described_class.new valid_attributes.merge({graphic_type: Zebra::Zpl::Graphic::DIAGONAL})}
|
78
|
+
let(:graphic_box) { described_class.new valid_attributes.merge({graphic_type: Zebra::Zpl::Graphic::BOX}) }
|
79
|
+
let(:graphic_symbol) { described_class.new valid_attributes.merge({graphic_type: Zebra::Zpl::Graphic::SYMBOL}) }
|
80
|
+
let(:graphic_circle) { described_class.new valid_attributes.merge({graphic_type: Zebra::Zpl::Graphic::CIRCLE}) }
|
81
|
+
|
82
|
+
let(:tokens_ellipse) { graphic_ellipse.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
|
83
|
+
let(:tokens_diagonal) { graphic_diagonal.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
|
84
|
+
let(:tokens_box) { graphic_box.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
|
85
|
+
let(:tokens_symbol) { graphic_symbol.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
|
86
|
+
let(:tokens_circle) { graphic_circle.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
|
87
|
+
|
88
|
+
it "raises an error if the X position is not given" do
|
89
|
+
graphic = described_class.new position: [nil, 50], graphic_type: described_class::ELLIPSE
|
90
|
+
expect {
|
91
|
+
graphic.to_zpl
|
92
|
+
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
|
93
|
+
end
|
94
|
+
|
95
|
+
it "raises an error if the Y position is not given" do
|
96
|
+
graphic = described_class.new position: [50, nil], graphic_type: described_class::ELLIPSE
|
97
|
+
expect {
|
98
|
+
graphic.to_zpl
|
99
|
+
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
|
100
|
+
end
|
101
|
+
|
102
|
+
it "raises an error if Graphic Type is not given" do
|
103
|
+
graphic = described_class.new position: [50, 50]
|
104
|
+
expect {
|
105
|
+
graphic.to_zpl
|
106
|
+
}.to raise_error(Zebra::Zpl::Graphic::InvalidGraphicType)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "contains the X position" do
|
110
|
+
expect(tokens_ellipse[2]).to eq "50"
|
111
|
+
end
|
112
|
+
|
113
|
+
it "contains the Y position" do
|
114
|
+
expect(tokens_ellipse[3]).to eq "50"
|
115
|
+
end
|
116
|
+
|
117
|
+
#Elipse Attributes
|
118
|
+
|
119
|
+
it "ellipse contains the ellipse graphic command '^GE'" do
|
120
|
+
expect(tokens_ellipse[4]).to eq "^GE"
|
121
|
+
end
|
122
|
+
|
123
|
+
it "ellipse contains the graphic width" do
|
124
|
+
expect(tokens_ellipse[5]).to eq "200"
|
125
|
+
end
|
126
|
+
|
127
|
+
it "ellipse contains the graphic height" do
|
128
|
+
expect(tokens_ellipse[6]).to eq "300"
|
129
|
+
end
|
130
|
+
|
131
|
+
it "ellipse contains the line thickness" do
|
132
|
+
expect(tokens_ellipse[7]).to eq "2"
|
133
|
+
end
|
134
|
+
|
135
|
+
it "ellipse contains the color" do
|
136
|
+
expect(tokens_ellipse[8]).to eq "B"
|
137
|
+
end
|
138
|
+
|
139
|
+
#Box Attributes
|
140
|
+
|
141
|
+
it "box contains the box graphic command '^GB'" do
|
142
|
+
expect(tokens_box[4]).to eq "^GB"
|
143
|
+
end
|
144
|
+
|
145
|
+
it "box contains the graphic width" do
|
146
|
+
expect(tokens_box[5]).to eq "200"
|
147
|
+
end
|
148
|
+
|
149
|
+
it "box contains the graphic height" do
|
150
|
+
expect(tokens_box[6]).to eq "300"
|
151
|
+
end
|
152
|
+
|
153
|
+
it "box contains the line thickness" do
|
154
|
+
expect(tokens_box[7]).to eq "2"
|
155
|
+
end
|
156
|
+
|
157
|
+
it "box contains the color" do
|
158
|
+
expect(tokens_box[8]).to eq "B"
|
159
|
+
end
|
160
|
+
|
161
|
+
it "box contains the rounding degree" do
|
162
|
+
expect(tokens_box[9]).to eq "3"
|
163
|
+
end
|
164
|
+
|
165
|
+
#Circle Attributes
|
166
|
+
|
167
|
+
it "circle contains the circle graphic command '^GC'" do
|
168
|
+
expect(tokens_circle[4]).to eq "^GC"
|
169
|
+
end
|
170
|
+
|
171
|
+
it "circle contains the graphic width" do
|
172
|
+
expect(tokens_circle[5]).to eq "200"
|
173
|
+
end
|
174
|
+
|
175
|
+
it "circle contains the line thickness" do
|
176
|
+
expect(tokens_circle[6]).to eq "2"
|
177
|
+
end
|
178
|
+
|
179
|
+
it "circle contains the color" do
|
180
|
+
expect(tokens_circle[7]).to eq "B"
|
181
|
+
end
|
182
|
+
|
183
|
+
#Diagonal Attributes
|
184
|
+
|
185
|
+
it "diagonal contains the diagonal graphic command '^GD'" do
|
186
|
+
expect(tokens_diagonal[4]).to eq "^GD"
|
187
|
+
end
|
188
|
+
|
189
|
+
it "diagonal contains the graphic width" do
|
190
|
+
expect(tokens_diagonal[5]).to eq "200"
|
191
|
+
end
|
192
|
+
|
193
|
+
it "diagonal contains the graphic width" do
|
194
|
+
expect(tokens_diagonal[6]).to eq "300"
|
195
|
+
end
|
196
|
+
|
197
|
+
it "diagonal contains the line thickness" do
|
198
|
+
expect(tokens_diagonal[7]).to eq "2"
|
199
|
+
end
|
200
|
+
|
201
|
+
it "diagonal contains the color" do
|
202
|
+
expect(tokens_diagonal[8]).to eq "B"
|
203
|
+
end
|
204
|
+
|
205
|
+
it "diagonal contains the orientation" do
|
206
|
+
expect(tokens_diagonal[9]).to eq "L"
|
207
|
+
end
|
208
|
+
|
209
|
+
#Symbol Attributes
|
210
|
+
|
211
|
+
it "symbol contains the symbol graphic command '^GS'" do
|
212
|
+
expect(tokens_symbol[4][0..2]).to eq "^GS"
|
213
|
+
end
|
214
|
+
|
215
|
+
it "symbol contains the graphic height" do
|
216
|
+
expect(tokens_symbol[5]).to eq "300"
|
217
|
+
end
|
218
|
+
|
219
|
+
it "symbol contains the graphic width" do
|
220
|
+
expect(tokens_symbol[6]).to eq "200"
|
221
|
+
end
|
222
|
+
|
223
|
+
it "symbol contains the symbol type" do
|
224
|
+
expect(tokens_symbol[7]).to eq "^FDC"
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Zebra::Zpl::Image do
|
4
|
+
|
5
|
+
it 'can be initialized with a path' do
|
6
|
+
img = described_class.new path: 'spec/fixtures/default.jpg'
|
7
|
+
expect(img.path).to match /^(.+)\/([^\/]+)$/
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'can be initialized with dimensions (width & height)' do
|
11
|
+
img = described_class.new width: 800, height: 600
|
12
|
+
expect(img.width).to eq 800
|
13
|
+
expect(img.height).to eq 600
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'can be initialized with a rotation amount' do
|
17
|
+
img = described_class.new rotation: 90
|
18
|
+
expect(img.rotation).to eq 90
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'can be initialized with a black threshold' do
|
22
|
+
img = described_class.new black_threshold: 0.25
|
23
|
+
expect(img.black_threshold).to eq 0.25
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'can be initialized with the invert flag' do
|
27
|
+
img = described_class.new invert: true
|
28
|
+
expect(img.invert).to eq true
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'can be initialized with the compress flag' do
|
32
|
+
img = described_class.new compress: true
|
33
|
+
expect(img.compress).to eq true
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#width' do
|
37
|
+
it 'raises an error if an invalid width is given' do
|
38
|
+
expect { described_class.new width: -10 }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
|
39
|
+
expect { described_class.new width: 'abc' }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#height' do
|
44
|
+
it 'raises an error if an invalid height is given' do
|
45
|
+
expect { described_class.new height: -10 }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
|
46
|
+
expect { described_class.new height: 'abc' }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#rotation' do
|
51
|
+
it 'raises an error if an invalid rotation value is given' do
|
52
|
+
expect { described_class.new rotation: '90d' }.to raise_error(Zebra::Zpl::Image::InvalidRotationError)
|
53
|
+
expect { described_class.new rotation: 'abc' }.to raise_error(Zebra::Zpl::Image::InvalidRotationError)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#black_threshold' do
|
58
|
+
it 'raises an error if an invalid black threshold is given' do
|
59
|
+
expect { described_class.new black_threshold: -5 }.to raise_error(Zebra::Zpl::Image::InvalidThresholdError)
|
60
|
+
expect { described_class.new black_threshold: 1.1 }.to raise_error(Zebra::Zpl::Image::InvalidThresholdError)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "instance methods" do
|
65
|
+
let(:valid_attributes) { {
|
66
|
+
path: 'spec/fixtures/default.jpg',
|
67
|
+
position: [50, 50],
|
68
|
+
width: 100,
|
69
|
+
height: 150
|
70
|
+
}}
|
71
|
+
let(:image) { described_class.new valid_attributes }
|
72
|
+
|
73
|
+
describe '#source' do
|
74
|
+
it 'returns an Img2Zpl::Image object' do
|
75
|
+
expect(image.source.class).to eq Img2Zpl::Image
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'responds to ImageMagick (MiniMagick::Image) commands' do
|
79
|
+
attr = valid_attributes
|
80
|
+
attr.delete(:width)
|
81
|
+
attr.delete(:height)
|
82
|
+
img = described_class.new attr
|
83
|
+
src = img.src
|
84
|
+
expect(src.respond_to?(:resize)).to be true
|
85
|
+
expect(src.respond_to?(:trim)).to be true
|
86
|
+
expect(src.respond_to?(:crop)).to be true
|
87
|
+
expect(src.respond_to?(:flatten)).to be true
|
88
|
+
expect(src.respond_to?(:rotate)).to be true
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'properly manipulates the image with ImageMagick (MiniMagick::Image) commands' do
|
92
|
+
attr = valid_attributes
|
93
|
+
attr.delete(:width)
|
94
|
+
attr.delete(:height)
|
95
|
+
img = described_class.new attr
|
96
|
+
|
97
|
+
img.source.resize '123x'
|
98
|
+
expect(img.width).to eq 123
|
99
|
+
img.source.resize 'x321'
|
100
|
+
expect(img.height).to eq 321
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe '#to_zpl' do
|
105
|
+
let(:tokens) { image.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
|
106
|
+
|
107
|
+
it 'raises an error if the X position is not given' do
|
108
|
+
qrcode = described_class.new position: [nil, 50]
|
109
|
+
expect {
|
110
|
+
qrcode.to_zpl
|
111
|
+
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'raises an error if the Y position is not given' do
|
115
|
+
qrcode = described_class.new position: [50, nil]
|
116
|
+
expect {
|
117
|
+
qrcode.to_zpl
|
118
|
+
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'raises an error if the path is not given' do
|
122
|
+
valid_attributes.delete :path
|
123
|
+
|
124
|
+
expect {
|
125
|
+
image.to_zpl
|
126
|
+
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the path is invalid or not given")
|
127
|
+
end
|
128
|
+
|
129
|
+
it "contains the Graphics Field command '^GF'" do
|
130
|
+
expect(image.to_zpl).to match /\^GF/
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'contains the X position' do
|
134
|
+
expect(tokens[1]).to eq '50'
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'contains the Y position' do
|
138
|
+
expect(tokens[2]).to eq '50'
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'contains the properly encoded image' do
|
142
|
+
expect(tokens[3..-1].join).to eq '^GFA1079107913:::::K078Q03J07FF8P038I01JFP078I03JF8O0FCI0KFCO0FC001KFEN01FE003LFN01FE003LF8M03FF007LFCM03FF8007LFCM07FF800MFCM0IFC00MFEM0IFC00MFEL01IFE01MFEL01IFE01MFEL03JF01NFL07JF8:01NFL0KFC:01MFEK01KFE00MFEK01LF00MFEK03LF00MFEK07LF8007LFCK07LF8007LFCK0MFC003LF8K0MFC003LFK01MFE001KFEK01NFI0KFCK03NFI07JF8K07NF8I01JFL04N08J07FFCK0FC::::::N01MFE:::::::::::::::::::::::::::::::::::^FS'
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|