From a453775b8e6caef7f356c540210144766d84f9e8 Mon Sep 17 00:00:00 2001 From: Stephen Inglis Date: Mon, 22 Oct 2018 14:18:22 +0100 Subject: [PATCH 1/3] Added code 39 support. --- .../label_printer/commands/barcode_format.rb | 29 +++++++++++++++++- app/models/barcode.rb | 9 +++++- spec/label_printer/commands_spec.rb | 16 ++++++++++ spec/models/barcode_spec.rb | 30 +++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) diff --git a/app/label_printer/label_printer/commands/barcode_format.rb b/app/label_printer/label_printer/commands/barcode_format.rb index 20650f6..882f12f 100644 --- a/app/label_printer/label_printer/commands/barcode_format.rb +++ b/app/label_printer/label_printer/commands/barcode_format.rb @@ -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) @@ -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) @@ -53,12 +55,33 @@ 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' def control_codes standard_codes = "#{x_origin},#{y_origin},#{barcode_type}" @@ -69,6 +92,10 @@ def control_codes when 'P' "#{standard_codes},00,#{one_module_width},#{no_of_columns},"\ "0,#{bar_height}" + when '3','B' + "#{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}" else "#{standard_codes},#{type_of_check_digit},#{one_module_width},"\ "0,#{height},+0000000000,002,0,00" diff --git a/app/models/barcode.rb b/app/models/barcode.rb index b3b9127..532c68b 100644 --- a/app/models/barcode.rb +++ b/app/models/barcode.rb @@ -7,11 +7,18 @@ 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, diff --git a/spec/label_printer/commands_spec.rb b/spec/label_printer/commands_spec.rb index d01551e..fb8c2cf 100644 --- a/spec/label_printer/commands_spec.rb +++ b/spec/label_printer/commands_spec.rb @@ -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 diff --git a/spec/models/barcode_spec.rb b/spec/models/barcode_spec.rb index 0f2c0da..7c7c213 100644 --- a/spec/models/barcode_spec.rb +++ b/spec/models/barcode_spec.rb @@ -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) From 819c39ea34a4fbdb76a763f93bd275fc430bbd91 Mon Sep 17 00:00:00 2001 From: Stephen Inglis Date: Mon, 22 Oct 2018 14:56:39 +0100 Subject: [PATCH 2/3] rubocopped. --- .rubocop.yml | 10 +++++----- .../label_printer/commands/barcode_format.rb | 4 ++-- app/models/barcode.rb | 5 ++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 1d30ab5..782ba22 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -34,11 +34,10 @@ 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/AbcSize: + Exclude: + - 'app/label_printer/label_printer/commands/barcode_format.rb' + Metrics/BlockLength: Exclude: - 'test/**/*' @@ -55,6 +54,7 @@ Metrics/MethodLength: Max: 12 Exclude: - 'db/migrate/*' + - 'app/label_printer/label_printer/commands/barcode_format.rb' Layout/IndentArray: EnforcedStyle: consistent diff --git a/app/label_printer/label_printer/commands/barcode_format.rb b/app/label_printer/label_printer/commands/barcode_format.rb index 882f12f..278d73f 100644 --- a/app/label_printer/label_printer/commands/barcode_format.rb +++ b/app/label_printer/label_printer/commands/barcode_format.rb @@ -72,7 +72,6 @@ class BarcodeFormat < Commands::Base # 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', @@ -83,6 +82,7 @@ class BarcodeFormat < Commands::Base 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}" @@ -92,7 +92,7 @@ def control_codes when 'P' "#{standard_codes},00,#{one_module_width},#{no_of_columns},"\ "0,#{bar_height}" - when '3','B' + when '3', 'B' "#{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}" diff --git a/app/models/barcode.rb b/app/models/barcode.rb index 532c68b..9ccdf0e 100644 --- a/app/models/barcode.rb +++ b/app/models/barcode.rb @@ -10,14 +10,13 @@ class Barcode < Drawing 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 - ] + 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, + :wide_bar_width, :wide_space_width, :char_to_char_space_width, format: { with: /\A\d{2}\z/, allow_blank: true } From 65024d648915f3cd564cf5029c67395d8b75c35a Mon Sep 17 00:00:00 2001 From: Stephen Inglis Date: Tue, 23 Oct 2018 10:46:39 +0100 Subject: [PATCH 3/3] Fixed rubocop issues. --- .rubocop.yml | 5 -- .../label_printer/commands/barcode_format.rb | 47 ++++++++++++------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 782ba22..5a4bfd2 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -34,10 +34,6 @@ Style/NumericLiterals: Style/AsciiComments: Enabled: false -Metrics/AbcSize: - Exclude: - - 'app/label_printer/label_printer/commands/barcode_format.rb' - Metrics/BlockLength: Exclude: - 'test/**/*' @@ -54,7 +50,6 @@ Metrics/MethodLength: Max: 12 Exclude: - 'db/migrate/*' - - 'app/label_printer/label_printer/commands/barcode_format.rb' Layout/IndentArray: EnforcedStyle: consistent diff --git a/app/label_printer/label_printer/commands/barcode_format.rb b/app/label_printer/label_printer/commands/barcode_format.rb index 278d73f..9ba219d 100644 --- a/app/label_printer/label_printer/commands/barcode_format.rb +++ b/app/label_printer/label_printer/commands/barcode_format.rb @@ -84,22 +84,37 @@ class BarcodeFormat < Commands::Base # 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}" - when '3', 'B' - "#{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}" - 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