Class: ZplRender::Barcode::Code39

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

Overview

Code 39 encoder (ZPL ^B3). Each character is 9 elements (5 bars, 4 spaces), 3 of them wide. Start/stop character '*' is added automatically, elements are separated by a narrow inter-character gap.

Defined Under Namespace

Classes: Result

Constant Summary collapse

PATTERNS =

n = narrow, w = wide; elements alternate bar/space starting with a bar

{
  "0" => "nnnwwnwnn", "1" => "wnnwnnnnw", "2" => "nnwwnnnnw",
  "3" => "wnwwnnnnn", "4" => "nnnwwnnnw", "5" => "wnnwwnnnn",
  "6" => "nnwwwnnnn", "7" => "nnnwnnwnw", "8" => "wnnwnnwnn",
  "9" => "nnwwnnwnn", "A" => "wnnnnwnnw", "B" => "nnwnnwnnw",
  "C" => "wnwnnwnnn", "D" => "nnnnwwnnw", "E" => "wnnnwwnnn",
  "F" => "nnwnwwnnn", "G" => "nnnnnwwnw", "H" => "wnnnnwwnn",
  "I" => "nnwnnwwnn", "J" => "nnnnwwwnn", "K" => "wnnnnnnww",
  "L" => "nnwnnnnww", "M" => "wnwnnnnwn", "N" => "nnnnwnnww",
  "O" => "wnnnwnnwn", "P" => "nnwnwnnwn", "Q" => "nnnnnnwww",
  "R" => "wnnnnnwwn", "S" => "nnwnnnwwn", "T" => "nnnnwnwwn",
  "U" => "wwnnnnnnw", "V" => "nwwnnnnnw", "W" => "wwwnnnnnn",
  "X" => "nwnnwnnnw", "Y" => "wwnnwnnnn", "Z" => "nwwnwnnnn",
  "-" => "nwnnnnwnw", "." => "wwnnnnwnn", " " => "nwwnnnwnn",
  "$" => "nwnwnwnnn", "/" => "nwnwnnnwn", "+" => "nwnnnwnwn",
  "%" => "nnnwnwnwn", "*" => "nwnnwnwnn"
}.freeze
CHECK_CHARS =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"

Class Method Summary collapse

Class Method Details

.encode(data, check_digit: false) ⇒ Object

Returns elements as [[width_units, bar?], ...] where width_units is 1 (narrow) or the wide ratio, to be multiplied by the module width.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zpl_render/barcode/code39.rb', line 34

def self.encode(data, check_digit: false)
  text = data.to_s.upcase.gsub(/[^0-9A-Z\-\. $\/+%]/, "")
  payload = text.dup
  if check_digit
    sum = payload.chars.sum { |c| CHECK_CHARS.index(c) || 0 }
    payload += CHECK_CHARS[sum % 43]
  end
  full = "*#{payload}*"
  elements = []
  full.each_char.with_index do |ch, idx|
    PATTERNS.fetch(ch).each_char.with_index do |wide, i|
      elements << [wide == "w" ? :wide : :narrow, i.even?]
    end
    elements << [:narrow, false] unless idx == full.length - 1 # inter-char gap
  end
  Result.new(elements: elements, interpretation: text)
end