Only need flat encoding.
parent
1f596995e2
commit
50517444aa
|
|
@ -1,27 +1,12 @@
|
||||||
//import { SIDE_BIN, MIDDLE_BIN } from './constants';
|
import { SIDE_BIN, MIDDLE_BIN } from './constants';
|
||||||
|
|
||||||
const Barcode = require("cards.Barcode.js");
|
|
||||||
const encode = require("cards.encode.js");
|
const encode = require("cards.encode.js");
|
||||||
|
const Barcode = require("cards.Barcode.js");
|
||||||
|
|
||||||
// Base class for EAN8 & EAN13
|
// Base class for EAN8 & EAN13
|
||||||
class EAN extends Barcode {
|
class EAN extends Barcode {
|
||||||
|
|
||||||
constructor(data, options) {
|
constructor(data, options) {
|
||||||
super(data, options);
|
super(data, options);
|
||||||
|
|
||||||
// Make sure the font is not bigger than the space between the guard bars
|
|
||||||
this.fontSize = !options.flat && options.fontSize > options.width * 10
|
|
||||||
? options.width * 10
|
|
||||||
: options.fontSize;
|
|
||||||
|
|
||||||
// Make the guard bars go down half the way of the text
|
|
||||||
this.guardHeight = options.height + this.fontSize / 2 + options.textMargin;
|
|
||||||
}
|
|
||||||
|
|
||||||
encode() {
|
|
||||||
return this.options.flat
|
|
||||||
? this.encodeFlat()
|
|
||||||
: this.encodeGuarded();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
leftText(from, to) {
|
leftText(from, to) {
|
||||||
|
|
@ -40,24 +25,7 @@ class EAN extends Barcode {
|
||||||
return encode(data, structure);
|
return encode(data, structure);
|
||||||
}
|
}
|
||||||
|
|
||||||
encodeGuarded() {
|
encode() {
|
||||||
const textOptions = { fontSize: this.fontSize };
|
|
||||||
const guardOptions = { height: this.guardHeight };
|
|
||||||
const SIDE_BIN = '101';
|
|
||||||
const MIDDLE_BIN = '01010';
|
|
||||||
|
|
||||||
return [
|
|
||||||
{ data: SIDE_BIN, options: guardOptions },
|
|
||||||
{ data: this.leftEncode(), text: this.leftText(), options: textOptions },
|
|
||||||
{ data: MIDDLE_BIN, options: guardOptions },
|
|
||||||
{ data: this.rightEncode(), text: this.rightText(), options: textOptions },
|
|
||||||
{ data: SIDE_BIN, options: guardOptions },
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
encodeFlat() {
|
|
||||||
const SIDE_BIN = '101';
|
|
||||||
const MIDDLE_BIN = '01010';
|
|
||||||
const data = [
|
const data = [
|
||||||
SIDE_BIN,
|
SIDE_BIN,
|
||||||
this.leftEncode(),
|
this.leftEncode(),
|
||||||
|
|
|
||||||
|
|
@ -60,33 +60,6 @@ class EAN13 extends EAN {
|
||||||
return super.rightEncode(data, 'RRRRRR');
|
return super.rightEncode(data, 'RRRRRR');
|
||||||
}
|
}
|
||||||
|
|
||||||
// The "standard" way of printing EAN13 barcodes with guard bars
|
|
||||||
encodeGuarded() {
|
|
||||||
const data = super.encodeGuarded();
|
|
||||||
|
|
||||||
// Extend data with left digit & last character
|
|
||||||
if (this.options.displayValue) {
|
|
||||||
data.unshift({
|
|
||||||
data: '000000000000',
|
|
||||||
text: this.text.substr(0, 1),
|
|
||||||
options: { textAlign: 'left', fontSize: this.fontSize }
|
|
||||||
});
|
|
||||||
|
|
||||||
if (this.options.lastChar) {
|
|
||||||
data.push({
|
|
||||||
data: '00'
|
|
||||||
});
|
|
||||||
data.push({
|
|
||||||
data: '00000',
|
|
||||||
text: this.options.lastChar,
|
|
||||||
options: { fontSize: this.fontSize }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = EAN13;
|
module.exports = EAN13;
|
||||||
|
|
@ -12,19 +12,6 @@ class UPC extends Barcode{
|
||||||
}
|
}
|
||||||
|
|
||||||
super(data, options);
|
super(data, options);
|
||||||
|
|
||||||
this.displayValue = options.displayValue;
|
|
||||||
|
|
||||||
// Make sure the font is not bigger than the space between the guard bars
|
|
||||||
if(options.fontSize > options.width * 10){
|
|
||||||
this.fontSize = options.width * 10;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
this.fontSize = options.fontSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make the guard bars go down half the way of the text
|
|
||||||
this.guardHeight = options.height + this.fontSize / 2 + options.textMargin;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
valid(){
|
valid(){
|
||||||
|
|
@ -33,15 +20,6 @@ class UPC extends Barcode{
|
||||||
}
|
}
|
||||||
|
|
||||||
encode(){
|
encode(){
|
||||||
if(this.options.flat){
|
|
||||||
return this.flatEncoding();
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return this.guardedEncoding();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
flatEncoding(){
|
|
||||||
var result = "";
|
var result = "";
|
||||||
|
|
||||||
result += "101";
|
result += "101";
|
||||||
|
|
@ -55,62 +33,6 @@ class UPC extends Barcode{
|
||||||
text: this.text
|
text: this.text
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
guardedEncoding(){
|
|
||||||
var result = [];
|
|
||||||
|
|
||||||
// Add the first digit
|
|
||||||
if(this.displayValue){
|
|
||||||
result.push({
|
|
||||||
data: "00000000",
|
|
||||||
text: this.text.substr(0, 1),
|
|
||||||
options: {textAlign: "left", fontSize: this.fontSize}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the guard bars
|
|
||||||
result.push({
|
|
||||||
data: "101" + encode(this.data[0], "L"),
|
|
||||||
options: {height: this.guardHeight}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the left side
|
|
||||||
result.push({
|
|
||||||
data: encode(this.data.substr(1, 5), "LLLLL"),
|
|
||||||
text: this.text.substr(1, 5),
|
|
||||||
options: {fontSize: this.fontSize}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the middle bits
|
|
||||||
result.push({
|
|
||||||
data: "01010",
|
|
||||||
options: {height: this.guardHeight}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the right side
|
|
||||||
result.push({
|
|
||||||
data: encode(this.data.substr(6, 5), "RRRRR"),
|
|
||||||
text: this.text.substr(6, 5),
|
|
||||||
options: {fontSize: this.fontSize}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the end bits
|
|
||||||
result.push({
|
|
||||||
data: encode(this.data[11], "R") + "101",
|
|
||||||
options: {height: this.guardHeight}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the last digit
|
|
||||||
if(this.displayValue){
|
|
||||||
result.push({
|
|
||||||
data: "00000000",
|
|
||||||
text: this.text.substr(11, 1),
|
|
||||||
options: {textAlign: "right", fontSize: this.fontSize}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calulate the checksum digit
|
// Calulate the checksum digit
|
||||||
|
|
|
||||||
|
|
@ -60,22 +60,6 @@ class UPCE extends Barcode{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.displayValue = options.displayValue;
|
|
||||||
|
|
||||||
// Make sure the font is not bigger than the space between the guard bars
|
|
||||||
if(options.fontSize > options.width * 10){
|
|
||||||
this.fontSize = options.width * 10;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
this.fontSize = options.fontSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make the guard bars go down half the way of the text
|
|
||||||
this.guardHeight = options.height + this.fontSize / 2 + options.textMargin;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
valid(){
|
valid(){
|
||||||
|
|
@ -83,15 +67,6 @@ class UPCE extends Barcode{
|
||||||
}
|
}
|
||||||
|
|
||||||
encode(){
|
encode(){
|
||||||
if(this.options.flat){
|
|
||||||
return this.flatEncoding();
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return this.guardedEncoding();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
flatEncoding(){
|
|
||||||
var result = "";
|
var result = "";
|
||||||
|
|
||||||
result += "101";
|
result += "101";
|
||||||
|
|
@ -104,49 +79,6 @@ class UPCE extends Barcode{
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
guardedEncoding(){
|
|
||||||
var result = [];
|
|
||||||
|
|
||||||
// Add the UPC-A number system digit beneath the quiet zone
|
|
||||||
if(this.displayValue){
|
|
||||||
result.push({
|
|
||||||
data: "00000000",
|
|
||||||
text: this.text[0],
|
|
||||||
options: {textAlign: "left", fontSize: this.fontSize}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the guard bars
|
|
||||||
result.push({
|
|
||||||
data: "101",
|
|
||||||
options: {height: this.guardHeight}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the 6 UPC-E digits
|
|
||||||
result.push({
|
|
||||||
data: this.encodeMiddleDigits(),
|
|
||||||
text: this.text.substring(1, 7),
|
|
||||||
options: {fontSize: this.fontSize}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the end bits
|
|
||||||
result.push({
|
|
||||||
data: "010101",
|
|
||||||
options: {height: this.guardHeight}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the UPC-A check digit beneath the quiet zone
|
|
||||||
if(this.displayValue){
|
|
||||||
result.push({
|
|
||||||
data: "00000000",
|
|
||||||
text: this.text[7],
|
|
||||||
options: {textAlign: "right", fontSize: this.fontSize}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
encodeMiddleDigits() {
|
encodeMiddleDigits() {
|
||||||
const numberSystem = this.upcA[0];
|
const numberSystem = this.upcA[0];
|
||||||
const checkDigit = this.upcA[this.upcA.length - 1];
|
const checkDigit = this.upcA[this.upcA.length - 1];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue