Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests & docs for Datamatrix #48

Merged
merged 6 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Zebra::Zpl offers a Ruby DSL to design and print labels using the ZPL programmin
- [Text](#text)
- [Barcodes](#barcodes)
- [QR Codes](#qr-codes)
- [Data Matrix](#data-matrix)
- [Boxes](#boxes)
- [Options](#options)
- [Rotation](#elements-rotation)
Expand Down Expand Up @@ -186,6 +187,22 @@ print_job = Zebra::PrintJob.new '<your-qr-printer-name-on-cups>'
print_job.print label, '<hostname>'
```

#### Data Matrix

You can create Data Matrix elements to print using instances of the `Zebra::Zpl::Datamatrix` class. It accepts the following options:

* `position`: An array with the coordinates to place the data matrix, in dots.
* `symbol_height`: Crucial variable of the size size of the data matrix. Accepted values: 1 - label width.
* `aspect_ratio`: 1 for square, 2 for rectangular.

```ruby
datamatrix = Zebra::Zpl::Datamatrix.new(
data: 'www.github.com',
position: [50,50],
symbol_height: 5
)
```

#### Boxes

You can draw boxes in your labels:
Expand Down
7 changes: 4 additions & 3 deletions lib/zebra/zpl/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ def max_lines

def to_zpl
check_attributes
"^FW#{rotation}^CF#{font_type},#{font_size}^CI28^FO#{x},#{y}^FB#{width},#{max_lines},#{line_spacing},#{justification},#{hanging_indent}^FD#{data}^FS"
if !bold.nil?
zpl += "^FW#{rotation}^CF#{font_type},#{font_size}^CI28^FO#{x+2},#{y}^FB#{width},#{max_lines},#{line_spacing},#{justification},#{hanging_indent}^FD#{data}^FS"
zpl += "^FW#{rotation}^CF#{font_type},#{font_size}^CI28^FO#{x},#{y+2}^FB#{width},#{max_lines},#{line_spacing},#{justification},#{hanging_indent}^FD#{data}^FS"
"^FW#{rotation}^CF#{font_type},#{font_size}^CI28^FO#{x+2},#{y}^FB#{width},#{max_lines},#{line_spacing},#{justification},#{hanging_indent}^FD#{data}^FS" +
"^FW#{rotation}^CF#{font_type},#{font_size}^CI28^FO#{x},#{y+2}^FB#{width},#{max_lines},#{line_spacing},#{justification},#{hanging_indent}^FD#{data}^FS"
else
"^FW#{rotation}^CF#{font_type},#{font_size}^CI28^FO#{x},#{y}^FB#{width},#{max_lines},#{line_spacing},#{justification},#{hanging_indent}^FD#{data}^FS"
end
end

Expand Down
124 changes: 124 additions & 0 deletions spec/zebra/zpl/datamatrix_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
require 'spec_helper'

describe Zebra::Zpl::Datamatrix do
it "can be initialized with the symbol height" do
datamatrix = described_class.new symbol_height: 3
expect(datamatrix.symbol_height).to eq 3
end

it "can be initialized with a quality level" do
datamatrix = described_class.new quality: 140
expect(datamatrix.quality).to eq 140
end

it "can be initialized with a number of columns" do
datamatrix = described_class.new columns: 33
expect(datamatrix.columns).to eq 33
end

it "can be initialized with a number of rows" do
datamatrix = described_class.new rows: 42
expect(datamatrix.rows).to eq 42
end

it "can be initialized with a format" do
datamatrix = described_class.new format: 2
expect(datamatrix.format).to eq 2
end
it "can be initialized with a aspect ratio" do
datamatrix = described_class.new aspect_ratio: 2
expect(datamatrix.aspect_ratio).to eq 2
end

describe "#orientation" do
it "raises an error if the orientation not in [N I R B]" do
expect { described_class.new orientation: 'A' }.to raise_error(Zebra::Zpl::Datamatrix::InvalidOrientationError)
end
end

describe "#quality" do
it "raises an error if the quality is not one of 0, 50, 80, 100, 140, 200" do
expect { described_class.new quality: 20 }.to raise_error(Zebra::Zpl::Datamatrix::InvalidQualityFactorError)
end
end

describe "#columns" do
it "raises an error if the number of columns is out of bounds" do
expect { described_class.new columns: 0 }.to raise_error(Zebra::Zpl::Datamatrix::InvalidSizeError)
end
end

describe "#rows" do
it "raises an error if the number of rows is out of bounds" do
expect { described_class.new rows: 59 }.to raise_error(Zebra::Zpl::Datamatrix::InvalidSizeError)
end
end


describe "#to_zpl" do
let(:valid_attributes) { {
position: [50, 50],
symbol_height: 5,
data: "foobar"
}}
let(:datamatrix) { described_class.new valid_attributes }
let(:tokens) { datamatrix.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }

it "raises an error if the X position is not given" do
datamatrix = described_class.new position: [nil, 50], data: "foobar"
expect {
datamatrix.to_zpl
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
end

it "raises an error if the Y position is not given" do
datamatrix = described_class.new position: [50, nil], data: "foobar"
expect {
datamatrix.to_zpl
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
end

it "raises an error if the data to be printed was not informed" do
datamatrix.data = nil
expect {
datamatrix.to_zpl
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
end

it "raises an error if the scale factor is not given" do
valid_attributes.delete :symbol_height

expect {
datamatrix.to_zpl
}.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the symbol height to be used is not given")
end

it "contains the barcode command '^B'" do
expect(datamatrix.to_zpl).to match /\^B/
end

it "contains the X position" do
expect(tokens[2]).to eq "50"
end

it "contains the Y position" do
expect(tokens[3]).to eq "50"
end

it "contains Data Matrix code type" do
expect(tokens[4]).to eq "^BXN"
end

it "contains the symbol_height" do
expect(tokens[5]).to eq "5"
end

it "contains the quality level" do
expect(tokens[6]).to eq "200"
end

it "contains the data to be printed in the datamatrix" do
expect(tokens[8]).to eq "foobar"
end
end
end
5 changes: 5 additions & 0 deletions spec/zebra/zpl/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@

describe "#to_zpl" do
subject(:text) { described_class.new position: [100, 150], font_size: Zebra::Zpl::FontSize::SIZE_3, data: "foobar" }
subject(:text_bold) { described_class.new position: [100, 150], font_size: Zebra::Zpl::FontSize::SIZE_3, bold: true, data: "foobar" }
subject(:tokens) { text.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }

it "raises an error if the X position was not informed" do
Expand Down Expand Up @@ -125,6 +126,10 @@
expect(text.to_zpl).to eq '^FWN^CF0,28^CI28^FO100,150^FB,4,,L,^FDfoobar^FS'
end

it "contains the properly duplicated attributes in correct order for bold text" do
expect(text_bold.to_zpl).to eq '^FWN^CF0,28^CI28^FO102,150^FB,4,,L,^FDfoobar^FS^FWN^CF0,28^CI28^FO100,152^FB,4,,L,^FDfoobar^FS'
end

# it "assumes 1 as the default horizontal multipler" do
# binding.pry
# expect(text.to_zpl.split(",")[4].to_i).to eq Zebra::Zpl::HorizontalMultiplier::VALUE_1
Expand Down