You are here

function barcode_code39_encode in Barcode 6.2

Same name and namespace in other branches
  1. 7.2 plugins/code39.inc \barcode_code39_encode()
1 call to barcode_code39_encode()
barcode_code39_barcode in plugins/code39.inc
@file Barcode plugin Code 39 (aka USD-3, 3 of 9): U.S. Government and military use, required for DoD applications

File

plugins/code39.inc, line 101
Barcode plugin Code 39 (aka USD-3, 3 of 9): U.S. Government and military use, required for DoD applications

Code

function barcode_code39_encode($barnumber, $checkdigit = FALSE) {
  $enc_table = array(
    "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",
  );
  $mfc_str = "";
  $widebar = str_pad("", $settings->n2w, "1", STR_PAD_LEFT);
  $widespc = str_pad("", $settings->n2w, "0", STR_PAD_LEFT);
  if ($checkdigit == TRUE) {
    $arr_key = array_keys($enc_table);
    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 = $enc_table[$barnumber[$i]];
    $bar = TRUE;
    for ($j = 0; $j < strlen($tmp); $j++) {
      if ($tmp[$j] == 'N' && $bar) {
        $mfc_str .= '1';
      }
      elseif ($tmp[$j] == 'N' && !$bar) {
        $mfc_str .= '0';
      }
      elseif ($tmp[$j] == 'W' && $bar) {
        $mfc_str .= $widebar;
      }
      elseif ($tmp[$j] == 'W' && !$bar) {
        $mfc_str .= $widespc;
      }
      $bar = !$bar;
    }
    $mfc_str .= '0';
  }
  return $mfc_str;
}