algorytm.org

Implementacja w JavaScript

Baza Wiedzy
wersja offline serwisu przeznaczona na urządzenia z systemem Android
Darowizny
darowiznaWspomóż rozwój serwisu
Nagłówki RSS
Artykuły
Implementacje
Komentarze
Forum
Bookmarki






Sonda
Implementacji w jakim języku programowania poszukujesz?

Extended Code 93 - Implementacja w JavaScript
Ocena użytkownikóww: *****  / 0
SłabyŚwietny
Nadesłany przez Tomasz Lubiński, 20 grudnia 2011 21:06
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.

Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.

code93ext.js:
//Extended Code 93
//(c) 2011 by Tomasz Lubinski
//www.algorytm.org

   var special_1 = '\u0080'; // ($)
   var special_2 = '\u0081'; // (%)
   var special_3 = '\u0082'; // (/)
   var special_4 = '\u0083'; // (+)
	
   var code93signs = [
      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
      'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
      'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$',
      '/', '+', '%', special_1, special_2, special_3, special_4];

   var extendedCode93Replace = [
      "\u0081U", "\u0080A", "\u0080B", "\u0080C", "\u0080D", "\u0080E", "\u0080F", "\u0080G", "\u0080H",
      "\u0080I", "\u0080J", "\u0080K", "\u0080L", "\u0080M", "\u0080N", "\u0080O", "\u0080P", "\u0080Q",
      "\u0080R", "\u0080S", "\u0080T", "\u0080U", "\u0080V", "\u0080W", "\u0080X", "\u0080Y", "\u0080Z",
      "\u0081A", "\u0081B", "\u0081C", "\u0081D", "\u0081E", " ", "\u0082A", "\u0082B", "\u0082C", "\u0082D",
      "\u0082E", "\u0082F", "\u0082G", "\u0082H", "\u0082I", "\u0082J", "\u0082K", "\u0082L", "-", ".", 
      "\u0082O", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "\u0082Z", "\u0081F", "\u0081G", "\u0081H",
      "\u0081I", "\u0081J", "\u0081V", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
      "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "\u0081K", "\u0081L", "\u0081M", "\u0081N",
      "\u0081O", "\u0081W", "\u0083A", "\u0083B", "\u0083C", "\u0083D", "\u0083E", "\u0083F", "\u0083G",
      "\u0083H", "\u0083I", "\u0083J", "\u0083K", "\u0083L", "\u0083M", "\u0083N", "\u0083O", "\u0083P",
      "\u0083Q", "\u0083R", "\u0083S", "\u0083T", "\u0083U", "\u0083V", "\u0083W", "\u0083X", "\u0083Y", 
      "\u0083Z", "\u0081P", "\u0081Q", "\u0081R", "\u0081S", "\u0081T"
   ];
   
   var code93bars = [
      [1, 0, 0, 0, 1, 0, 1, 0, 0],
      [1, 0, 1, 0, 0, 1, 0, 0, 0],
      [1, 0, 1, 0, 0, 0, 1, 0, 0],
      [1, 0, 1, 0, 0, 0, 0, 1, 0],
      [1, 0, 0, 1, 0, 1, 0, 0, 0],
      [1, 0, 0, 1, 0, 0, 1, 0, 0],
      [1, 0, 0, 1, 0, 0, 0, 1, 0],
      [1, 0, 1, 0, 1, 0, 0, 0, 0],
      [1, 0, 0, 0, 1, 0, 0, 1, 0],
      [1, 0, 0, 0, 0, 1, 0, 1, 0],
      [1, 1, 0, 1, 0, 1, 0, 0, 0],
      [1, 1, 0, 1, 0, 0, 1, 0, 0],
      [1, 1, 0, 1, 0, 0, 0, 1, 0],
      [1, 1, 0, 0, 1, 0, 1, 0, 0],
      [1, 1, 0, 0, 1, 0, 0, 1, 0],
      [1, 1, 0, 0, 0, 1, 0, 1, 0],
      [1, 0, 1, 1, 0, 1, 0, 0, 0],
      [1, 0, 1, 1, 0, 0, 1, 0, 0],
      [1, 0, 1, 1, 0, 0, 0, 1, 0],
      [1, 0, 0, 1, 1, 0, 1, 0, 0],
      [1, 0, 0, 0, 1, 1, 0, 1, 0],
      [1, 0, 1, 0, 1, 1, 0, 0, 0],
      [1, 0, 1, 0, 0, 1, 1, 0, 0],
      [1, 0, 1, 0, 0, 0, 1, 1, 0],
      [1, 0, 0, 1, 0, 1, 1, 0, 0],
      [1, 0, 0, 0, 1, 0, 1, 1, 0],
      [1, 1, 0, 1, 1, 0, 1, 0, 0],
      [1, 1, 0, 1, 1, 0, 0, 1, 0],
      [1, 1, 0, 1, 0, 1, 1, 0, 0],
      [1, 1, 0, 1, 0, 0, 1, 1, 0],
      [1, 1, 0, 0, 1, 0, 1, 1, 0],
      [1, 1, 0, 0, 1, 1, 0, 1, 0],
      [1, 0, 1, 1, 0, 1, 1, 0, 0],
      [1, 0, 1, 1, 0, 0, 1, 1, 0],
      [1, 0, 0, 1, 1, 0, 1, 1, 0],
      [1, 0, 0, 1, 1, 1, 0, 1, 0],
      [1, 0, 0, 1, 0, 1, 1, 1, 0],
      [1, 1, 1, 0, 1, 0, 1, 0, 0],
      [1, 1, 1, 0, 1, 0, 0, 1, 0],
      [1, 1, 1, 0, 0, 1, 0, 1, 0],
      [1, 0, 1, 1, 0, 1, 1, 1, 0],
      [1, 0, 1, 1, 1, 0, 1, 1, 0],
      [1, 1, 0, 1, 0, 1, 1, 1, 0],
      [1, 0, 0, 1, 0, 0, 1, 1, 0],
      [1, 1, 1, 0, 1, 1, 0, 1, 0],
      [1, 1, 1, 0, 1, 0, 1, 1, 0],
      [1, 0, 0, 1, 1, 0, 0, 1, 0]
   ];

   var start_stop = [ 1, 0, 1, 0, 1, 1, 1, 1, 0 ];

   var termination_bar = [ 1 ];

/* Return letter index, or -1 if not correct */
function getLetterIndex(ch)
{
   for (var i=0; i<code93signs.length; i++)
   {
      if (code93signs[i] == ch)
         return i;
   }
   return -1;
}

/* Return check digit C */
function checkDigitC(code93)
{
   var sum = 0;
   var w = 0;

   for (var i=code93.length-1; i>=0; i--)
   {
      sum += (getLetterIndex(code93[i]) * ((w % 20) + 1));
      w++;
   }

   return (sum % 47);
}	
	  
/* Return check digit K */
function checkDigitK(code93)
{
   var sum = checkDigitC(code93);
   var w = 1;

   for (var i=code93.length-1; i>=0; i--)
   {
      sum += (getLetterIndex(code93[i]) * ((w % 15) + 1));
      w++;
   }

   return (sum % 47);
}
	  
/* Convert code to bars */
function convert(code93) 
{
   var bars = new Array(9*(code93.length+4) + 1);

   /* add start bars */
   for (var j=0; j<9; j++)
   {
       bars[j] = start_stop[j];
   }

   /* add character codes */
   for (var i=0; i<code93.length; i++)
   {
      var toCopy = code93bars[getLetterIndex(code93[i])];
      for (var j=0; j<9; j++)
      {
          bars[i*9 + 9 + j] = toCopy[j];
      }
   }

   /* add check digit C */
   var toCopy = code93bars[checkDigitC(code93)];
   for (var j=0; j<9; j++) 
   {
       bars[(code93.length+1)*9 + j] = toCopy[j];
   }
   
   /* add check digit K */
   var toCopy = code93bars[checkDigitK(code93)];
   for (var j=0; j<9; j++) 
   {
       bars[(code93.length+2)*9 + j] = toCopy[j];
   }
   
   /* add stop bars */
   for (var j=0; j<9; j++)
   {
       bars[(code93.length+3)*9 + j] = start_stop[j];
   }
   
   /* add termination bar */
   bars[(code93.length+4)*9] = termination_bar[0];

   return bars;
}

/* converts to code 93 set of characters */
function convertToCode93(code93ext)
{
   var result = "";

   for (var i=0; i<code93ext.length; i++)
   {
      if ((code93ext.charCodeAt(i) >= 0) && (code93ext.charCodeAt(i) <= 127))
         result += extendedCode93Replace[code93ext.charCodeAt(i)];
      else
         return null;
   }

   return result;
}

//initialize data
function generate(code93ext)
{
   var canvas = document.getElementById("canvas");
   var ctx = canvas.getContext("2d");

   /* convert to code 93 set of characters */
   var code93 = convertToCode93(code93ext);
   
   if (code93 != null)
   {
      var bars = convert(code93);
      
      /* clear canvas and set its width */
      canvas.width = bars.length*2+20;      
      
      for (i=0; i<bars.length; i++)
      {
         /* Black bar */
         if (bars[i] == 1)
         {
            ctx.fillRect(10+2*i, 10, 2, 70);
         }
      }

      ctx.fillStyle    = "rgb(0,0,0)";
      ctx.font         = "10pt Arial";
      ctx.textBaseline = "top";
      ctx.textAlign    = "center";
      ctx.fillText(code93ext, canvas.width/2, 90);
   }
   else
   {
      ctx.fillStyle    = "rgb(255,0,0)";
      ctx.font         = "10pt Arial";
      ctx.textBaseline = "top";
      ctx.textAlign    = "left";
      ctx.fillText("Nieprawidłowy", 10, 10);
      ctx.fillText("kod", 10, 25);
      ctx.fillText("Incorrect", 10, 50);
      ctx.fillText("code", 10, 65);
   }
}
Dodaj komentarz