1
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
// Encoding documentation:
|
||||
// https://en.wikipedia.org/wiki/Code_93#Detailed_outline
|
||||
|
||||
import { SYMBOLS, BINARIES, MULTI_SYMBOLS } from './constants';
|
||||
import Barcode from "../Barcode.js";
|
||||
|
||||
class CODE93 extends Barcode {
|
||||
constructor(data, options){
|
||||
super(data, options);
|
||||
}
|
||||
|
||||
valid(){
|
||||
return /^[0-9A-Z\-. $/+%]+$/.test(this.data);
|
||||
}
|
||||
|
||||
encode(){
|
||||
const symbols = this.data
|
||||
.split('')
|
||||
.flatMap(c => MULTI_SYMBOLS[c] || c);
|
||||
const encoded = symbols
|
||||
.map(s => CODE93.getEncoding(s))
|
||||
.join('');
|
||||
|
||||
// Compute checksum symbols
|
||||
const csumC = CODE93.checksum(symbols, 20);
|
||||
const csumK = CODE93.checksum(symbols.concat(csumC), 15);
|
||||
|
||||
return {
|
||||
text: this.text,
|
||||
data:
|
||||
// Add the start bits
|
||||
CODE93.getEncoding('\xff') +
|
||||
// Add the encoded bits
|
||||
encoded +
|
||||
// Add the checksum
|
||||
CODE93.getEncoding(csumC) + CODE93.getEncoding(csumK) +
|
||||
// Add the stop bits
|
||||
CODE93.getEncoding('\xff') +
|
||||
// Add the termination bit
|
||||
'1'
|
||||
};
|
||||
}
|
||||
|
||||
// Get the binary encoding of a symbol
|
||||
static getEncoding(symbol) {
|
||||
return BINARIES[CODE93.symbolValue(symbol)];
|
||||
}
|
||||
|
||||
// Get the symbol for a symbol value
|
||||
static getSymbol(symbolValue) {
|
||||
return SYMBOLS[symbolValue];
|
||||
}
|
||||
|
||||
// Get the symbol value of a symbol
|
||||
static symbolValue(symbol) {
|
||||
return SYMBOLS.indexOf(symbol);
|
||||
}
|
||||
|
||||
// Calculate a checksum symbol
|
||||
static checksum(symbols, maxWeight) {
|
||||
const csum = symbols
|
||||
.slice()
|
||||
.reverse()
|
||||
.reduce((sum, symbol, idx) => {
|
||||
const weight = (idx % maxWeight) + 1;
|
||||
return sum + (CODE93.symbolValue(symbol) * weight);
|
||||
}, 0);
|
||||
|
||||
return CODE93.getSymbol(csum % 47);
|
||||
}
|
||||
}
|
||||
|
||||
export default CODE93;
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// Encoding documentation
|
||||
// https://en.wikipedia.org/wiki/Code_93#Full_ASCII_Code_93
|
||||
|
||||
import CODE93 from './CODE93.js';
|
||||
|
||||
class CODE93FullASCII extends CODE93 {
|
||||
constructor(data, options) {
|
||||
super(data, options);
|
||||
}
|
||||
|
||||
valid() {
|
||||
return /^[\x00-\x7f]+$/.test(this.data);
|
||||
}
|
||||
}
|
||||
|
||||
export default CODE93FullASCII;
|
||||
@@ -0,0 +1,123 @@
|
||||
// The position in the array is the (checksum) value
|
||||
export const SYMBOLS = [
|
||||
'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',
|
||||
'-', '.', ' ', '$',
|
||||
'/', '+', '%',
|
||||
// Only used for csum and multi-symbols character encodings
|
||||
'($)', '(%)', '(/)', '(+)',
|
||||
// Start/Stop
|
||||
'\xff',
|
||||
];
|
||||
|
||||
// Order matches SYMBOLS array
|
||||
export const BINARIES = [
|
||||
'100010100', '101001000', '101000100', '101000010',
|
||||
'100101000', '100100100', '100100010', '101010000',
|
||||
'100010010', '100001010', '110101000', '110100100',
|
||||
'110100010', '110010100', '110010010', '110001010',
|
||||
'101101000', '101100100', '101100010', '100110100',
|
||||
'100011010', '101011000', '101001100', '101000110',
|
||||
'100101100', '100010110', '110110100', '110110010',
|
||||
'110101100', '110100110', '110010110', '110011010',
|
||||
'101101100', '101100110', '100110110', '100111010',
|
||||
'100101110', '111010100', '111010010', '111001010',
|
||||
'101101110', '101110110', '110101110', '100100110',
|
||||
'111011010', '111010110', '100110010', '101011110',
|
||||
];
|
||||
|
||||
// Multi-symbol characters (Full ASCII Code 93)
|
||||
export const MULTI_SYMBOLS = {
|
||||
'\x00': ['(%)', 'U'],
|
||||
'\x01': ['($)', 'A'],
|
||||
'\x02': ['($)', 'B'],
|
||||
'\x03': ['($)', 'C'],
|
||||
'\x04': ['($)', 'D'],
|
||||
'\x05': ['($)', 'E'],
|
||||
'\x06': ['($)', 'F'],
|
||||
'\x07': ['($)', 'G'],
|
||||
'\x08': ['($)', 'H'],
|
||||
'\x09': ['($)', 'I'],
|
||||
'\x0a': ['($)', 'J'],
|
||||
'\x0b': ['($)', 'K'],
|
||||
'\x0c': ['($)', 'L'],
|
||||
'\x0d': ['($)', 'M'],
|
||||
'\x0e': ['($)', 'N'],
|
||||
'\x0f': ['($)', 'O'],
|
||||
'\x10': ['($)', 'P'],
|
||||
'\x11': ['($)', 'Q'],
|
||||
'\x12': ['($)', 'R'],
|
||||
'\x13': ['($)', 'S'],
|
||||
'\x14': ['($)', 'T'],
|
||||
'\x15': ['($)', 'U'],
|
||||
'\x16': ['($)', 'V'],
|
||||
'\x17': ['($)', 'W'],
|
||||
'\x18': ['($)', 'X'],
|
||||
'\x19': ['($)', 'Y'],
|
||||
'\x1a': ['($)', 'Z'],
|
||||
'\x1b': ['(%)', 'A'],
|
||||
'\x1c': ['(%)', 'B'],
|
||||
'\x1d': ['(%)', 'C'],
|
||||
'\x1e': ['(%)', 'D'],
|
||||
'\x1f': ['(%)', 'E'],
|
||||
'!': ['(/)', 'A'],
|
||||
'"': ['(/)', 'B'],
|
||||
'#': ['(/)', 'C'],
|
||||
'&': ['(/)', 'F'],
|
||||
'\'': ['(/)', 'G'],
|
||||
'(': ['(/)', 'H'],
|
||||
')': ['(/)', 'I'],
|
||||
'*': ['(/)', 'J'],
|
||||
',': ['(/)', 'L'],
|
||||
':': ['(/)', 'Z'],
|
||||
';': ['(%)', 'F'],
|
||||
'<': ['(%)', 'G'],
|
||||
'=': ['(%)', 'H'],
|
||||
'>': ['(%)', 'I'],
|
||||
'?': ['(%)', 'J'],
|
||||
'@': ['(%)', 'V'],
|
||||
'[': ['(%)', 'K'],
|
||||
'\\': ['(%)', 'L'],
|
||||
']': ['(%)', 'M'],
|
||||
'^': ['(%)', 'N'],
|
||||
'_': ['(%)', 'O'],
|
||||
'`': ['(%)', 'W'],
|
||||
'a': ['(+)', 'A'],
|
||||
'b': ['(+)', 'B'],
|
||||
'c': ['(+)', 'C'],
|
||||
'd': ['(+)', 'D'],
|
||||
'e': ['(+)', 'E'],
|
||||
'f': ['(+)', 'F'],
|
||||
'g': ['(+)', 'G'],
|
||||
'h': ['(+)', 'H'],
|
||||
'i': ['(+)', 'I'],
|
||||
'j': ['(+)', 'J'],
|
||||
'k': ['(+)', 'K'],
|
||||
'l': ['(+)', 'L'],
|
||||
'm': ['(+)', 'M'],
|
||||
'n': ['(+)', 'N'],
|
||||
'o': ['(+)', 'O'],
|
||||
'p': ['(+)', 'P'],
|
||||
'q': ['(+)', 'Q'],
|
||||
'r': ['(+)', 'R'],
|
||||
's': ['(+)', 'S'],
|
||||
't': ['(+)', 'T'],
|
||||
'u': ['(+)', 'U'],
|
||||
'v': ['(+)', 'V'],
|
||||
'w': ['(+)', 'W'],
|
||||
'x': ['(+)', 'X'],
|
||||
'y': ['(+)', 'Y'],
|
||||
'z': ['(+)', 'Z'],
|
||||
'{': ['(%)', 'P'],
|
||||
'|': ['(%)', 'Q'],
|
||||
'}': ['(%)', 'R'],
|
||||
'~': ['(%)', 'S'],
|
||||
'\x7f': ['(%)', 'T'],
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
import CODE93 from './CODE93.js';
|
||||
import CODE93FullASCII from './CODE93FullASCII.js';
|
||||
|
||||
export {CODE93, CODE93FullASCII};
|
||||
Reference in New Issue
Block a user