Skip to content

Commit

Permalink
Merge pull request #38 from stevieing/add_code39_support
Browse files Browse the repository at this point in the history
Add code39 support
  • Loading branch information
stevieing authored Oct 23, 2018
2 parents cdcf11d + 65024d6 commit 2f3e3e9
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 19 deletions.
5 changes: 0 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ Style/NumericLiterals:
Style/AsciiComments:
Enabled: false

# Blocks are used to structure tests and are part of the shoulda dsl.
# The standard BlockLength limits are too stringent for this purpose.
# Block DSL is also used in routes
# We should be careful keeping rake tasks here, as large tasks are still an issue.
# Not sure if we need to split namespaces across several files though.
Metrics/BlockLength:
Exclude:
- 'test/**/*'
Expand Down
68 changes: 55 additions & 13 deletions app/label_printer/label_printer/commands/barcode_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BarcodeFormat < Commands::Base
# qq: Number of zeros to be suppressed (optional, 00 - 20)

# 2D:
# Barcode type: Q
# Format: [ESC] XBaa; bbbb, cccc, d, ee, ff, gg, h [LF] [NUL]
# Example: XB02;0300,0145,Q,20,03,05,1
# aa: Bar code number (00 - 31)
Expand All @@ -41,6 +42,7 @@ class BarcodeFormat < Commands::Base
# h: Rotational angle of bar code 0: 0° 1: 90° 2: 180° 3: 270°

# PDF417
# Barcode type: P
# Format: [ESC] XBaa; bbbb, cccc, d, ee, ff, gg, h, iiii [LF] [NUL]
# Example: XB02,0300,0000,P,00,01,10,0,0100
# aa: Bar code number (00 - 31)
Expand All @@ -53,26 +55,66 @@ class BarcodeFormat < Commands::Base
# h: rotational angle of barcode (0 - 0, 1 - 90, 2 - 180, 3 - 270)
# iiii: Bar height (0000 - 0100 in 0.1mm units)

# CODE39
# Barcode type: 3 or B
# [ESC] XBaa; bbbb, cccc, d, e, ff, gg, hh, ii, jj, k, llll [LF] [NUL]
# Example: XB02,0300,0000,3,3,01,01,01,01,01,1,0100
# aa: Bar code number (00 - 31)
# bbbb: Print origin of X-coordinate of bar code
# cccc: Print origin of Y-coordinate of bar code
# d: Type of bar code
# e: type of check digit
# ff: Narrow bar width (01 to 99 (in dots))
# gg: Narrow space width (01 to 99 (in dots))
# hh: Wide bar width (01 to 99 (in dots))
# ii: Wide space width (01 to 99 (in dots))
# jj: Character-to-character space width (01 to 99 (in dots))
# k: rotational angle of barcode (0 - 0, 1 - 90, 2 - 180, 3 - 270)
# llll: Height of the bar code (0000 - 0100 in 0.1mm units)

prefix_accessor 'XB'

optional_attributes barcode_type: '5', type_of_check_digit: '3',
one_module_width: '02', height: '0070',
one_cell_width: '04', rotational_angle: '1',
no_of_columns: '01', bar_height: '0010'
no_of_columns: '01', bar_height: '0010',
narrow_bar_width: '01', narrow_space_width: '01',
wide_bar_width: '01',
wide_space_width: '01', char_to_char_space_width: '01'

# TODO: Modify method to stop rubocop errors.
def control_codes
standard_codes = "#{x_origin},#{y_origin},#{barcode_type}"

case barcode_type
when 'Q'
"#{standard_codes},20,#{one_cell_width},05,#{rotational_angle}"
when 'P'
"#{standard_codes},00,#{one_module_width},#{no_of_columns},"\
"0,#{bar_height}"
else
"#{standard_codes},#{type_of_check_digit},#{one_module_width},"\
"0,#{height},+0000000000,002,0,00"
end
return twod_codes if barcode_type == 'Q'
return pdf417_codes if barcode_type == 'P'
return code39_codes if barcode_type == '3' || barcode_type == 'B'

oned_codes
end

private

def standard_codes
"#{x_origin},#{y_origin},#{barcode_type}"
end

def pdf417_codes
"#{standard_codes},00,#{one_module_width},#{no_of_columns},"\
"0,#{bar_height}"
end

def twod_codes
"#{standard_codes},20,#{one_cell_width},05,#{rotational_angle}"
end

def oned_codes
"#{standard_codes},#{type_of_check_digit},#{one_module_width},"\
"0,#{height},+0000000000,002,0,00"
end

def code39_codes
"#{standard_codes},#{type_of_check_digit},#{narrow_bar_width},#{narrow_space_width},"\
"#{wide_bar_width},#{wide_space_width},#{char_to_char_space_width},"\
"#{rotational_angle},#{height}"
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion app/models/barcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
class Barcode < Drawing
store :options, accessors: %i[barcode_type one_module_width
height rotational_angle one_cell_width
type_of_check_digit no_of_columns bar_height]
type_of_check_digit no_of_columns bar_height
narrow_bar_width narrow_space_width
wide_bar_width wide_space_width
char_to_char_space_width]

validates :barcode_type, format: { with: /\A[0-9A-Z]{1}\z/, allow_blank: true }

validates :one_module_width, :one_cell_width, :no_of_columns,
:narrow_bar_width, :narrow_space_width,
:wide_bar_width, :wide_space_width,
:char_to_char_space_width,
format: { with: /\A\d{2}\z/, allow_blank: true }

validates :rotational_angle, :type_of_check_digit,
Expand Down
16 changes: 16 additions & 0 deletions spec/label_printer/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@
expect(command.control_codes).to eq('0300,0000,P,00,01,10,0,0100')
end
end

context 'CODE39' do
let(:options) {{ id: '001', x_origin: '0300', y_origin: '0000',
barcode_type: '3', type_of_check_digit: '3', narrow_bar_width: '02',
narrow_space_width: '03', wide_bar_width: '04',
wide_space_width: '05', char_to_char_space_width: '06',
rotational_angle: '1', height: '0100' }}

it 'should have appropriate control codes' do
command = LabelPrinter::Commands::BarcodeFormat.new(options)
expect(command.control_codes).to eq('0300,0000,3,3,02,03,04,05,06,1,0100')
command = LabelPrinter::Commands::BarcodeFormat.new(options.merge(barcode_type: 'B'))
expect(command.control_codes).to eq('0300,0000,B,3,02,03,04,05,06,1,0100')
end

end
end

context 'Draw Barcode' do
Expand Down
30 changes: 30 additions & 0 deletions spec/models/barcode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,36 @@
expect(build(:barcode, bar_height: '11111')).to_not be_valid
end

it 'should validate format of narrow bar width' do
expect(build(:barcode, narrow_bar_width: '01')).to be_valid
expect(build(:barcode, narrow_bar_width: 'XX')).to_not be_valid
expect(build(:barcode, narrow_bar_width: '111')).to_not be_valid
end

it 'should validate format of narrow space width' do
expect(build(:barcode, narrow_space_width: '01')).to be_valid
expect(build(:barcode, narrow_space_width: 'XX')).to_not be_valid
expect(build(:barcode, narrow_space_width: '111')).to_not be_valid
end

it 'should validate format of wide bar width' do
expect(build(:barcode, wide_bar_width: '01')).to be_valid
expect(build(:barcode, wide_bar_width: 'XX')).to_not be_valid
expect(build(:barcode, wide_bar_width: '111')).to_not be_valid
end

it 'should validate format of wide space width' do
expect(build(:barcode, wide_space_width: '01')).to be_valid
expect(build(:barcode, wide_space_width: 'XX')).to_not be_valid
expect(build(:barcode, wide_space_width: '111')).to_not be_valid
end

it 'should validate format of char to char space width' do
expect(build(:barcode, char_to_char_space_width: '01')).to be_valid
expect(build(:barcode, char_to_char_space_width: 'XX')).to_not be_valid
expect(build(:barcode, char_to_char_space_width: '111')).to_not be_valid
end

it 'should assign a placeholder id if there is a section' do
label = create(:label)

Expand Down

0 comments on commit 2f3e3e9

Please sign in to comment.