Code 128 C Barcode Generator in Javascript (Compatible with Google Docs Spreadsheet)

Dec 10, 2012 00:46

Based on the demo code given in How to Create Barcodes in Google Docs

Yes, it's ugly, and no, I don't know how javascript actually works. I've never learnt it properly and I've only learned by trial and error, so it should go without saying that this code is offered without warranty.

This code requires the input length to be even. If the input string length is odd, a "0" will be appended to the beginning. If you need a more flexible generator, try http://www.barcode-generator.org/ or http://zanstra.com/my/Barcode.html.

function Barcode128C(Code,BarHeight)
{
if(BarHeight==null) BarHeight = 2;
var BarASCIIBin=[11011001100, 11001101100, 11001100110, 10010011000, 10010001100,
10001001100, 10011001000, 10011000100, 10001100100, 11001001000,
11001000100, 11000100100, 10110011100, 10011011100, 10011001110,
10111001100, 10011101100, 10011100110, 11001110010, 11001011100,
11001001110, 11011100100, 11001110100, 11101101110, 11101001100,
11100101100, 11100100110, 11101100100, 11100110100, 11100110010,
11011011000, 11011000110, 11000110110, 10100011000, 10001011000,
10001000110, 10110001000, 10001101000, 10001100010, 11010001000,
11000101000, 11000100010, 10110111000, 10110001110, 10001101110,
10111011000, 10111000110, 10001110110, 11101110110, 11010001110,
11000101110, 11011101000, 11011100010, 11011101110, 11101011000,
11101000110, 11100010110, 11101101000, 11101100010, 11100011010,
11101111010, 11001000010, 11110001010, 10100110000, 10100001100,
10010110000, 10010000110, 10000101100, 10000100110, 10110010000,
10110000100, 10011010000, 10011000010, 10000110100, 10000110010,
11000010010, 11001010000, 11110111010, 11000010100, 10001111010,
10100111100, 10010111100, 10010011110, 10111100100, 10011110100,
10011110010, 11110100100, 11110010100, 11110010010, 11011011110,
11011110110, 11110110110, 10101111000, 10100011110, 10001011110,
10111101000, 10111100010, 11110101000, 11110100010, 10111011110,
10111101110, 11101011110, 11110101110];

var BarSource = Code;
var r = BarSource.length;
if(r % 2 == 1)
{
BarSource = "0" + BarSource
}
else
{
BarSource = "" + BarSource
}

var BarCS = 2;
var BarBin = "";
for(var f = 0; f < r; f = f + 2)
{
var BarChunk = BarSource.slice(f, f + 2);
BarBin = BarBin + BarASCIIBin[Number(BarChunk)];
BarCS = BarCS + Number(BarChunk) * (f + 2) / 2
}

BarBin = "11010011100" + BarBin + BarASCIIBin[Number(BarCS%103)] + "1100011101011";
var k = BarBin.length;
var BarDots = "";
for(var f = 0; f < k; f = f + 2)
{
var BarDot = Number(BarBin.slice(f,f+2));
if(BarDot == 0) // Blank (00)
{
BarDots = BarDots + String.fromCharCode(32);
}
if(BarDot == 1) // Right (01)
{
BarDots = BarDots + String.fromCharCode(9616);
}
if(BarDot == 10) // Left (10)
{
BarDots = BarDots + String.fromCharCode(9612);
}
if(BarDot == 11) // Full (11)
{
BarDots = BarDots + String.fromCharCode(9608);
}
}
var BarOut = ""
for(var f = 0; f < BarHeight; f++)
{
BarOut = BarOut + BarDots + "\n";
}
return BarOut;
}
Previous post Next post
Up