Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

barcode.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. BARS = [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,23311120];
  2. START_BASE = 38
  3. STOP = 106 //BARS[STOP]==23311120 (manually added a zero at the end)
  4. var fromType128 = {
  5. A: function(charCode) {
  6. if (charCode>=0 && charCode<32)
  7. return charCode+64;
  8. if (charCode>=32 && charCode<96)
  9. return charCode-32;
  10. return charCode;
  11. },
  12. B: function(charCode) {
  13. if (charCode>=32 && charCode<128)
  14. return charCode-32;
  15. return charCode;
  16. },
  17. C: function(charCode) {
  18. return charCode;
  19. }
  20. };
  21. function code128(code, barcodeType) {
  22. if (arguments.length<2)
  23. barcodeType = code128Detect(code);
  24. if (barcodeType=='C' && code.length%2==1)
  25. code = '0'+code;
  26. var a = parseBarcode128(code, barcodeType);
  27. return bar2html128(a.join('')) ;//+ '<label>' + code + '</label>';
  28. }
  29. function code128Detect(code) {
  30. if (/^[0-9]+$/.test(code)) return 'C';
  31. if (/[a-z]/.test(code)) return 'B';
  32. return 'A';
  33. }
  34. function parseBarcode128(barcode, barcodeType) {
  35. var bars = [];
  36. bars.add = function(nr) {
  37. var nrCode = BARS[nr];
  38. this.check = this.length==0 ? nr : this.check + nr*this.length;
  39. this.push( nrCode || format("UNDEFINED: %1->%2", nr, nrCode) );
  40. };
  41. bars.add(START_BASE + barcodeType.charCodeAt(0));
  42. for(var i=0; i<barcode.length; i++)
  43. {
  44. var code = barcodeType=='C' ? +barcode.substr(i++, 2) : barcode.charCodeAt(i);
  45. converted = fromType128[barcodeType](code);
  46. if (isNaN(converted) || converted<0 || converted>106)
  47. throw new Error(format("Unrecognized character (%1) at position %2 in code '%3'.", code, i, barcode));
  48. bars.add( converted );
  49. }
  50. bars.push(BARS[bars.check % 103], BARS[STOP]);
  51. return bars;
  52. }
  53. function format(c){
  54. var d=arguments;
  55. var e= new RegExp("%([1-"+(arguments.length-1)+"])","g");
  56. return(c+"").replace(e,function(a,b){return d[b]})
  57. }
  58. function bar2html128(s) {
  59. for(var pos=0, sb=[]; pos<s.length; pos+=2)
  60. {
  61. sb.push('<div class="bar' + s.charAt(pos) + ' space' + s.charAt(pos+1) + '"></div>');
  62. }
  63. return sb.join('');
  64. }