You are here

function BARCODE::_c39Encode in Barcode 6

1 call to BARCODE::_c39Encode()
BARCODE::_c39Barcode in ./barcode.inc.php

File

./barcode.inc.php, line 408

Class

BARCODE

Code

function _c39Encode($barnumber, $checkdigit = false) {
  $encTable = array(
    "0" => "NNNWWNWNN",
    "1" => "WNNWNNNNW",
    "2" => "NNWWNNNNW",
    "3" => "WNWWNNNNN",
    "4" => "NNNWWNNNW",
    "5" => "WNNWWNNNN",
    "6" => "NNWWWNNNN",
    "7" => "NNNWNNWNW",
    "8" => "WNNWNNWNN",
    "9" => "NNWWNNWNN",
    "A" => "NNWWNNWNN",
    "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",
  );
  $mfcStr = "";
  $widebar = str_pad("", $this->_n2w, "1", STR_PAD_LEFT);
  $widespc = str_pad("", $this->_n2w, "0", STR_PAD_LEFT);
  if ($checkdigit == true) {
    $arr_key = array_keys($encTable);
    for ($i = 0; $i < strlen($barnumber); $i++) {
      $num = $barnumber[$i];
      if (preg_match("/[A-Z]+/", $num)) {
        $num = ord($num) - 55;
      }
      elseif ($num == '-') {
        $num = 36;
      }
      elseif ($num == '.') {
        $num = 37;
      }
      elseif ($num == ' ') {
        $num = 38;
      }
      elseif ($num == '$') {
        $num = 39;
      }
      elseif ($num == '/') {
        $num = 40;
      }
      elseif ($num == '+') {
        $num = 41;
      }
      elseif ($num == '%') {
        $num = 42;
      }
      elseif ($num == '*') {
        $num = 43;
      }
      $sum += $num;
    }
    $barnumber .= trim($arr_key[(int) ($sum % 43)]);
  }
  $barnumber = "*" . $barnumber . "*";
  for ($i = 0; $i < strlen($barnumber); $i++) {
    $tmp = $encTable[$barnumber[$i]];
    $bar = true;
    for ($j = 0; $j < strlen($tmp); $j++) {
      if ($tmp[$j] == 'N' && $bar) {
        $mfcStr .= '1';
      }
      else {
        if ($tmp[$j] == 'N' && !$bar) {
          $mfcStr .= '0';
        }
        else {
          if ($tmp[$j] == 'W' && $bar) {
            $mfcStr .= $widebar;
          }
          else {
            if ($tmp[$j] == 'W' && !$bar) {
              $mfcStr .= $widespc;
            }
          }
        }
      }
      $bar = !$bar;
    }
    $mfcStr .= '0';
  }
  return $mfcStr;
}