Class: ZplRender::Barcode::Code128

Inherits:
Object
  • Object
show all
Defined in:
lib/zpl_render/barcode/code128.rb

Overview

Code 128 encoder implementing ZPL ^BC semantics, including the Zebra subset invocation characters (>0 .. >;) and modes N, U, A and D.

Produces the exact module sequence defined by ISO/IEC 15417: each symbol is 6 elements (11 modules), the stop pattern is 7 elements (13 modules), with a mod-103 check symbol before the stop.

Defined Under Namespace

Classes: Result

Constant Summary collapse

PATTERNS =

Element widths for symbol values 0..105 (each entry: 3 bars and 3 spaces, alternating, starting with a bar).

%w[
  212222 222122 222221 121223 121322 131222 122213 122312 132212 221213
  221312 231212 112232 122132 122231 113222 123122 123221 223211 221132
  221231 213212 223112 312131 311222 321122 321221 312212 322112 322211
  212123 212321 232121 111323 131123 131321 112313 132113 132311 211313
  231113 231311 112133 112331 132131 113123 113321 133121 313121 211331
  231131 213113 213311 213131 311123 311321 331121 312113 312311 332111
  314111 221411 431111 111224 111422 121124 121421 141122 141221 112214
  112412 122114 122411 142112 142211 241211 221114 413111 241112 134111
  111242 121142 121241 114212 124112 124211 411212 421112 421211 212141
  214121 412121 111143 111341 131141 114113 114311 411113 411311 113141
  114131 311141 411131 211412 211214 211232
].freeze
STOP_PATTERN =
"2331112"
CODE_C =
99
CODE_B =
100
CODE_A =
101
SHIFT =
98
FNC1 =
102
FNC2 =
97
FNC3 =
96
FNC4_A =

FNC4 shares values with CODE A/B depending on active subset

101
FNC4_B =
100
START_A =
103
START_B =
104
START_C =
105

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, mode:, ucc_check: false) ⇒ Code128

Returns a new instance of Code128.



49
50
51
52
53
# File 'lib/zpl_render/barcode/code128.rb', line 49

def initialize(data, mode:, ucc_check: false)
  @data = data
  @mode = mode
  @ucc_check = ucc_check
end

Class Method Details

.encode(data, mode: "N", ucc_check: false) ⇒ Object

data: raw ^FD string. mode: "N", "U", "A", "D". ucc_check: bool (^BC e param)



45
46
47
# File 'lib/zpl_render/barcode/code128.rb', line 45

def self.encode(data, mode: "N", ucc_check: false)
  new(data.to_s, mode: mode.to_s.upcase, ucc_check: ucc_check).encode
end

.modules(values) ⇒ Object

Expand the symbol value list into a module-width string, e.g. "2122..." alternating bar/space widths, including check symbol and stop.



67
68
69
70
71
72
# File 'lib/zpl_render/barcode/code128.rb', line 67

def self.modules(values)
  sum = values.first
  values.each_with_index { |v, i| sum += v * i unless i.zero? }
  check = sum % 103
  (values + [check]).map { |v| PATTERNS.fetch(v) }.join + STOP_PATTERN
end

Instance Method Details

#encodeObject



55
56
57
58
59
60
61
62
63
# File 'lib/zpl_render/barcode/code128.rb', line 55

def encode
  tokens = tokenize(@data)
  case @mode
  when "U" then encode_ucc(tokens)
  when "A" then encode_auto(tokens, gs1: false)
  when "D" then encode_auto(strip_gs1_decorations(tokens), gs1: true)
  else          encode_literal(tokens)
  end
end