Skip to content

Instantly share code, notes, and snippets.

@jozip
Last active January 20, 2019 19:06
Show Gist options
  • Save jozip/6cbe92b9e02277f41138a7b856b07b04 to your computer and use it in GitHub Desktop.
Save jozip/6cbe92b9e02277f41138a7b856b07b04 to your computer and use it in GitHub Desktop.
Unicode barcode generator in glorious javascript
// code39.js -- Unicode barcode generator
// Ported from from Trammell Hudson's original Perl script
// by Johan Persson <jzp@bahnhof.se>
//
// Original header:
// Generate code39 barcodes with unicode characters.
// This doesn't really have a purpose.
// https://trmm.net/Barcode
var patterns = {
'*': '1 1001',
'A': '011 10',
'B': '101 10',
'C': '001 11',
'D': '110 10',
'E': '010 11',
'F': '100 11',
'G': '111 00',
'H': '011 01',
'I': '101 01',
'J': '110 01',
'K': '0111 0',
'L': '1011 0',
'M': '0011 1',
'N': '1101 0',
'O': '0101 1',
'P': '1001 1',
'Q': '1110 0',
'R': '0110 1',
'S': '1010 1',
'T': '1100 1',
'U': '0 1110',
'V': '1 0110',
'W': '0 0111',
'X': '1 1010',
'Y': '0 1011',
'Z': '1 0011',
'.': '0 1101',
'-': '1 1100',
' ': '1 0101',
'0': '11 001',
'1': '01 110',
'2': '10 110',
'3': '00 111',
'4': '11 010',
'5': '01 011',
'6': '10 011',
'7': '11 100',
'8': '01 101',
'9': '10 101',
};
var bits = {
'0' : '█',
'1': '│',
' ': ' ',
};
function code39(str) {
var last = '';
return ('*' + str + '*')
.toUpperCase()
.split('')
.map(function (x) {
return (patterns[x] || patterns[' '])
.split('')
.map(function (y) {
var padded = (y === '0' && last === '0') ? ' ' + bits[y] : bits[y];
last = y;
return padded;
})
.join('');
})
.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment