0

What I have here is a JS program that generates Code39 barcode using display HTML and CSS to display the white and black lines. The JS file is fine, but I need this code to generate a portion of barcode for a backslash. The problem is, regular Code39 is all included in the arrays and functions of the script, but extended Code39 is not, and that’s the portion that includes the backslash. (The end result I’m trying to achieve is to type in the server path to a document and have it display in barcode.)

This may help explain a little more about Code39: http://en.wikipedia.org/wiki/Code_39

Okay, so here’s what I’ve done: In the barcode.html file, on line 19 after , I’ve entered a backslash. Whatever is typed here will display under the barcode in between two asterisks and the barcode will coincide with the numbers below when the page is loaded. (I have a dummy pattern of all black attached to the backslash presently). In the code39.js file, on lines 357 and 358, I’ve added the backslash with the other characters (using two backslashes of course). I’ve also added the same to the arrays below on lines 399 and 408, but I still can’t get JS to recognize the backslash, it just keeps spitting out a “0” on the barcode, which is the result I get when a value is entered that is not specified in the script. Any and all help is appreciated!

barcode.html file:

<!DOCTYPE html PUBLIC>
<html>
<head>
    <title>Barcode</title>
    <script type="text/javascript" src="code39.js"></script>
    <style type="text/css">
    #barcode {font-weight: normal; font-style: normal; line-height:normal; sans-serif; font-size: 12pt}
    </style>
</head>
<body>

Input : 123456<br/>
Barcode : Code39<br/>
Check Digit : 1 (On)<br/><br/>

Output : <br/><br/>

<div id="externalbox" style="width:5in">
<div id="inputdata" >\</div>
</div>

<br />

<script type="text/javascript">
/* <![CDATA[ */
  function get_object(id) {
   var object = null;
   if (document.layers) {
    object = document.layers[id];
   } else if (document.all) {
    object = document.all[id];
   } else if (document.getElementById) {
    object = document.getElementById(id);
   }
   return object;
  }
get_object("inputdata").innerHTML=DrawCode39Barcode(get_object("inputdata").innerHTML,1);
/* ]]> */
</script>

</body>
</html>

code39.js script file:

/*
 * Code39 barcode
 */

        function DrawCode39Barcode(data,
                           checkDigit)
        {
            return DrawHTMLBarcode_Code39(data,checkDigit,"yes","in", 0,3,1,3,"bottom","center", "","black","white"); 
        }

        function DrawHTMLBarcode_Code39(data,
                            checkDigit,
                            humanReadable,
                            units,
                            minBarWidth,
                            width,height,
                            barWidthRatio,
                            textLocation,
                            textAlignment,
                            textStyle,
                            foreColor,
                            backColor)
        {
            return DrawBarcode_Code39(data,
                         checkDigit,
                         humanReadable,
                         units,
                         minBarWidth,
                         width,height,
                         barWidthRatio,
                         textLocation,
                         textAlignment,
                         textStyle,
                         foreColor,
                         backColor,
                         "html");
        }

            function DrawBarcode_Code39(data,
                            checkDigit,
                            humanReadable,
                            units,
                            minBarWidth,
                            width,height,
                            barWidthRatio,
                            textLocation,
                            textAlignment,
                            textStyle,
                            foreColor,
                            backColor,
                            mode)
        {

              if (foreColor==undefined)
                foreColor="black";
              if (backColor==undefined)
                backColor="white";

              if (textLocation==undefined)
                textLocation="bottom";
              else if (textLocation!="bottom" && textLocation!="top")
                textLocation="bottom";
              if (textAlignment==undefined)
                textAlignment="center";
              else if (textAlignment!="center" && textAlignment!="left" && textAlignment!="right")
                textAlignment="center";
              if (textStyle==undefined)
                textStyle="";
              if (barWidthRatio==undefined)
                barWidthRatio=3;              
              if (height==undefined)
                height=1;
              else if (height<=0 || height >15)
                height=1;
              if (width==undefined)
                width=3;
              else if (width<=0 || width >15)
                width=3;
              if (minBarWidth==undefined)
                  minBarWidth=0;
              else if (minBarWidth<0 || minBarWidth >2)
                  minBarWidth=0;
              if (units==undefined)
                units="in";
              else if (units!="in" && units !="cm")
                units="in";
              if (humanReadable==undefined)
                humanReadable="yes";
              else if (humanReadable!="yes" && humanReadable !="no")
                humanReadable="yes";

              var encodedData=EncodeCode39(data,checkDigit);    
                    var humanReadableText = ConnectCode_Encode_Code39(data,checkDigit);
                var encodedLength = 0;
                    var thinLength = 0;
                    var thickLength = 0.0;
                    var totalLength = 0.0;
                    var incrementWidth = 0.0;
                    var swing = 1;
              var result="";
              var barWidth=0;
              var thickWidth=0.0;
              var svg;
                    if (barWidthRatio >= 2 && barWidthRatio <= 3)
                    {
                    }
                    else
                        barWidthRatio = 3;

                    for (x = 0; x < encodedData.length; x++)
                    {
                        if (encodedData.substr(x,1) == 't')
                        {
                            thinLength++;
                            encodedLength++;
                        }
                        else if (encodedData.substr(x,1) == 'w')
                        {
                            thickLength = thickLength + barWidthRatio;
                            encodedLength = encodedLength + 3;
                        }
                    }
                    totalLength = totalLength + thinLength + thickLength;

                    if (minBarWidth > 0)
                    {
                        barWidth = minBarWidth.toFixed(2);
                        width=barWidth * totalLength;
                    }
                    else
                        barWidth = (width / totalLength).toFixed(2);

                    thickWidth = barWidth * 3;
                    if (barWidthRatio >= 2 && barWidthRatio <= 3.0)
                    {
                        thickWidth = barWidth * barWidthRatio;
                    }

              if (mode=="html")
              {
                  if (textAlignment=='center')
                      result='<div style="text-align:center">';
                  else if (textAlignment=='left')
                      result='<div style="text-align:left;">';
                  else if (textAlignment=='right')
                      result='<div style="text-align:right;">';

                  var humanSpan="";
                  if (humanReadable=='yes' && textLocation=='top')
                  {
                    if (textStyle=='')
                        humanSpan='<span style="font-family : arial; font-size:12pt">'+humanReadableText+'</span><br />';
                    else
                        humanSpan='<span style='+textStyle+'>'+humanReadableText+'</span><br />';
                  }
                  result=result+humanSpan;
              }

                    for (x = 0; x < encodedData.length; x++)
                    {
                        var brush;
                        if (swing == 0)
                            brush = backColor;
                        else
                            brush = foreColor;

                        if (encodedData.substr(x,1) == 't')
                        {
                  if (mode=="html")
                    result=result
                         +'<span style="border-left:'
                         +barWidth
                         +units
                         +' solid ' 
                         +brush
                         +';height:'
                         +height
                         +units+';display:inline-block;"></span>';
                    incrementWidth = incrementWidth + barWidth;
                }
                        else if (encodedData.substr(x,1) == 'w')
                        {
                  if (mode=="html")
                    result=result
                         +'<span style="border-left :'
                         +thickWidth
                         +units+' solid ' 
                         +brush
                         +';height:'
                         +height
                         +units+';display:inline-block;"></span>';
                        incrementWidth = incrementWidth + thickWidth;
                }

                        if (swing == 0)
                            swing = 1;
                        else
                            swing = 0;
                    }

              if (mode=="html")
              {
                  var humanSpan="";
                  if (humanReadable=='yes' && textLocation=='bottom')
                  {
                    if (textStyle=='')
                        humanSpan='<br /><span style="font-family : arial; font-size:12pt">'+humanReadableText+'</span>';
                    else
                        humanSpan='<br /><span style='+textStyle+'>'+humanReadableText+'</span>';
                  }
                  result=result+humanSpan+"</div>";
              }
              return result;    
        }

            function EncodeCode39(data,checkDigit)
            {
                var fontOutput = ConnectCode_Encode_Code39(data,checkDigit);
                var output = "";
                var pattern = "";
                for (x = 0; x < fontOutput.length; x++)
                {
                    switch (fontOutput.substr(x,1))
                    {
                        case '1':
                            pattern = "wttwttttwt";
                            break;
                        case '2':
                            pattern = "ttwwttttwt";
                            break;
                        case '3':
                            pattern = "wtwwtttttt";
                            break;
                        case '4':
                            pattern = "tttwwtttwt";
                            break;
                        case '5':
                            pattern = "wttwwttttt";
                            break;
                        case '6':
                            pattern = "ttwwwttttt";
                            break;
                        case '7':
                            pattern = "tttwttwtwt";
                            break;
                        case '8':
                            pattern = "wttwttwttt";
                            break;
                        case '9':
                            pattern = "ttwwttwttt";
                            break;
                        case '0':
                            pattern = "tttwwtwttt";
                            break;
                        case 'A':
                            pattern = "wttttwttwt";
                            break;
                        case 'B':
                            pattern = "ttwttwttwt";
                            break;
                        case 'C':
                            pattern = "wtwttwtttt";
                            break;
                        case 'D':
                            pattern = "ttttwwttwt";
                            break;
                        case 'E':
                            pattern = "wtttwwtttt";
                            break;
                        case 'F':
                            pattern = "ttwtwwtttt";
                            break;
                        case 'G':
                            pattern = "tttttwwtwt";
                            break;
                        case 'H':
                            pattern = "wttttwwttt";
                            break;
                        case 'I':
                            pattern = "ttwttwwttt";
                            break;
                        case 'J':
                            pattern = "ttttwwwttt";
                            break;
                        case 'K':
                            pattern = "wttttttwwt";
                            break;
                        case 'L':
                            pattern = "ttwttttwwt";
                            break;
                        case 'M':
                            pattern = "wtwttttwtt";
                            break;
                        case 'N':
                            pattern = "ttttwttwwt";
                            break;
                        case 'O':
                            pattern = "wtttwttwtt";
                            break;
                        case 'P':
                            pattern = "ttwtwttwtt";
                            break;
                        case 'Q':
                            pattern = "ttttttwwwt";
                            break;
                        case 'R':
                            pattern = "wtttttwwtt";
                            break;
                        case 'S':
                            pattern = "ttwtttwwtt";
                            break;
                        case 'T':
                            pattern = "ttttwtwwtt";
                            break;
                        case 'U':
                            pattern = "wwttttttwt";
                            break;
                        case 'V':
                            pattern = "twwtttttwt";
                            break;
                        case 'W':
                            pattern = "wwwttttttt";
                            break;
                        case 'X':
                            pattern = "twttwtttwt";
                            break;
                        case 'Y':
                            pattern = "wwttwttttt";
                            break;
                        case 'Z':
                            pattern = "twwtwttttt";
                            break;
                        case '-':
                            pattern = "twttttwtwt";
                            break;
                        case '.':
                            pattern = "wwttttwttt";
                            break;
                        case ' ':
                            pattern = "twwtttwttt";
                            break;
                        case '*':
                            pattern = "twttwtwttt";
                            break;
                        case '$':
                            pattern = "twtwtwtttt";
                            break;
                        case '/':
                            pattern = "twtwtttwtt";
                            break;
                        case '+':
                            pattern = "twtttwtwtt";
                            break;
                        case '%':
                            pattern = "tttwtwtwtt";
                            break;
                        case '\\':
                            pattern = "tttttttttt";
                            break;
                default : break;
                    }
                    output=output+pattern;
                }
                return output;
            }

        function ConnectCode_Encode_Code39(data,checkDigit)
        {
            var Result="";
            var cd="";
            var filtereddata="";
            filtereddata = filterInput(data);
            var filteredlength = filtereddata.length;
            if (checkDigit==1)
            {
                if (filteredlength > 254)
                {
                    filtereddata = filtereddata.substr(0,254);
                }
                cd = generateCheckDigit(filtereddata);
            }
            else
            {
                if (filteredlength > 255)
                {
                    filtereddata = filtereddata.substr(0,255);
                }
            }
            Result = "*" + filtereddata+cd+"*";
              Result=html_decode(html_escape(Result));  
            return Result;
        }

        function getCode39Character(inputdecimal) {
            var CODE39MAP=new Array("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","-","."," ","$",
                            "/","+","%","\\");
            return CODE39MAP[inputdecimal];
        }

        function getCode39Value(inputchar) {
            var CODE39MAP=new Array("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","-","."," ","$",
                            "/","+","%","\\");
            var RVal=-1;
            for (i=0;i<43;i++)
            {
                if (inputchar==CODE39MAP[i])
                {
                    RVal=i;
                }
            }
            return RVal;
        }

        function filterInput(data)
        {
            var Result="";
            var datalength=data.length;
            for (x=0;x<datalength;x++)
            {
                if (getCode39Value(data.substr(x,1)) != -1)
                {
                    Result = Result + data.substr(x,1);
                }
            }
            return Result;
        }

        function generateCheckDigit(data)
        {
            var Result="";
            var datalength=data.length;
            var sumValue=0;
            for (x=0;x<datalength;x++)
            {
                sumValue=sumValue+getCode39Value(data.substr(x,1));
            }
            sumValue=sumValue % 43;
            return getCode39Character(sumValue);
        }

        function html_escape(data)
        {
            var Result="";
            for (x=0;x<data.length;x++)
            {
                Result=Result+"&#"+data.charCodeAt(x).toString()+";";
            }
            return Result;
        }

        function html_decode(str) {
            var ta=document.createElement("textarea");
              ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
              return ta.value;
        }
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • Can you reduce your code to a smaller (minimal) case? – Halcyon Jan 16 '14 at 17:00
  • If you enter, for example, "123456" in line 19 of barcode.html and load the page, it will output a barcode representing those numbers as well as *123456* below the barcode. I need this JS to recognize a backslash as an output character as well. It doesn't recognize the backslash presently, just outputs a zero. – user3203479 Jan 16 '14 at 17:03
  • I included all the code so you are able to see the output. Sorry, just trying to be thorough. – user3203479 Jan 16 '14 at 17:04
  • Here's a JSFiddle: http://jsfiddle.net/8yZqG/ what is the expected output? It outputs a barcode and `*0*` – Halcyon Jan 16 '14 at 17:06
  • Okay. Enter 123456 in line 10 of that in place of the backslash and see the result. – user3203479 Jan 16 '14 at 17:08

2 Answers2

2

Your problem is in your getCode39Value() function.

for (i = 0; i < 43; i++) {

You're using for i < 43, when you actually have 44 objects in the array. You should use CODE39MAP.length instead:

function getCode39Value(inputchar) {
    var CODE39MAP = new Array("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", "-", ".", " ", "$", "/", "+", "%", "\\");
    var RVal = -1;
    for (i = 0; i < CODE39MAP.length; i++) {
        if (inputchar == CODE39MAP[i]) {
            RVal = i;
        }
    }
    return RVal;
}

http://jsfiddle.net/remus/GSFVG/

brandonscript
  • 68,675
  • 32
  • 163
  • 220
1

getCode39Value is bad because of the off-by-one. This implementation also works:

// moved outsite the function, it's a static map.
var CODE39MAP=new Array("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","-","."," ","$",
                    "/","+","%","\\");

function getCode39Value(inputchar) {
    return CODE39MAP.indexOf(inputchar);
}

indexOf already returns -1 if it's not found.

Halcyon
  • 57,230
  • 10
  • 89
  • 128