1
|
|
|
// Encoding documentation: |
2
|
|
|
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode |
3
|
|
|
|
4
|
|
|
import EANencoder from './ean_encoder.js'; |
5
|
|
|
import Barcode from "../Barcode.js"; |
6
|
|
|
|
7
|
|
|
class EAN13 extends Barcode{ |
8
|
|
|
constructor(data, options){ |
9
|
|
|
// Add checksum if it does not exist |
10
|
|
|
if(data.search(/^[0-9]{12}$/) !== -1){ |
11
|
|
|
data += checksum(data); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
super(data, options); |
15
|
|
|
|
16
|
|
|
// Make sure the font is not bigger than the space between the guard bars (not used when flat=true) |
17
|
|
|
this.fontSize = options.width * 10; |
18
|
|
|
|
19
|
|
|
// Make the guard bars go down half the way of the text |
20
|
|
|
this.guardHeight = options.height + this.fontSize / 2 + options.textMargin; |
21
|
|
|
|
22
|
|
|
// Adds a last character to the end of the barcode |
23
|
|
|
this.lastChar = options.lastChar; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
valid(){ |
27
|
|
|
return this.data.search(/^[0-9]{13}$/) !== -1 && |
28
|
|
|
this.data[12] == checksum(this.data); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
encode(){ |
32
|
|
|
if(this.options.flat){ |
33
|
|
|
return this.flatEncoding(); |
34
|
|
|
} |
35
|
|
|
else{ |
36
|
|
|
return this.guardedEncoding(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Define the EAN-13 structure |
41
|
|
|
getStructure(){ |
42
|
|
|
return [ |
43
|
|
|
"LLLLLL", |
44
|
|
|
"LLGLGG", |
45
|
|
|
"LLGGLG", |
46
|
|
|
"LLGGGL", |
47
|
|
|
"LGLLGG", |
48
|
|
|
"LGGLLG", |
49
|
|
|
"LGGGLL", |
50
|
|
|
"LGLGLG", |
51
|
|
|
"LGLGGL", |
52
|
|
|
"LGGLGL" |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// The "standard" way of printing EAN13 barcodes with guard bars |
57
|
|
|
guardedEncoding(){ |
58
|
|
|
var encoder = new EANencoder(); |
59
|
|
|
var result = []; |
60
|
|
|
|
61
|
|
|
var structure = this.getStructure()[this.data[0]]; |
62
|
|
|
|
63
|
|
|
// Get the string to be encoded on the left side of the EAN code |
64
|
|
|
var leftSide = this.data.substr(1, 6); |
65
|
|
|
|
66
|
|
|
// Get the string to be encoded on the right side of the EAN code |
67
|
|
|
var rightSide = this.data.substr(7, 6); |
68
|
|
|
|
69
|
|
|
// Add the first digigt |
70
|
|
|
if(this.options.displayValue){ |
71
|
|
|
result.push({ |
72
|
|
|
data: "000000000000", |
73
|
|
|
text: this.text.substr(0, 1), |
74
|
|
|
options: {textAlign: "left", fontSize: this.fontSize} |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Add the guard bars |
79
|
|
|
result.push({ |
80
|
|
|
data: "101", |
81
|
|
|
options: {height: this.guardHeight} |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
// Add the left side |
85
|
|
|
result.push({ |
86
|
|
|
data: encoder.encode(leftSide, structure), |
87
|
|
|
text: this.text.substr(1, 6), |
88
|
|
|
options: {fontSize: this.fontSize} |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
// Add the middle bits |
92
|
|
|
result.push({ |
93
|
|
|
data: "01010", |
94
|
|
|
options: {height: this.guardHeight} |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
// Add the right side |
98
|
|
|
result.push({ |
99
|
|
|
data: encoder.encode(rightSide, "RRRRRR"), |
100
|
|
|
text: this.text.substr(7, 6), |
101
|
|
|
options: {fontSize: this.fontSize} |
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
// Add the end bits |
105
|
|
|
result.push({ |
106
|
|
|
data: "101", |
107
|
|
|
options: {height: this.guardHeight} |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
if(this.options.lastChar && this.options.displayValue){ |
111
|
|
|
result.push({data: "00"}); |
112
|
|
|
|
113
|
|
|
result.push({ |
114
|
|
|
data: "00000", |
115
|
|
|
text: this.options.lastChar, |
116
|
|
|
options: {fontSize: this.fontSize} |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
return result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
flatEncoding(){ |
123
|
|
|
var encoder = new EANencoder(); |
124
|
|
|
var result = ""; |
125
|
|
|
|
126
|
|
|
var structure = this.getStructure()[this.data[0]]; |
127
|
|
|
|
128
|
|
|
result += "101"; |
129
|
|
|
result += encoder.encode(this.data.substr(1, 6), structure); |
130
|
|
|
result += "01010"; |
131
|
|
|
result += encoder.encode(this.data.substr(7, 6), "RRRRRR"); |
132
|
|
|
result += "101"; |
133
|
|
|
|
134
|
|
|
return { |
135
|
|
|
data: result, |
136
|
|
|
text: this.text |
137
|
|
|
}; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Calulate the checksum digit |
142
|
|
|
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit |
143
|
|
|
function checksum(number){ |
144
|
|
|
var result = 0; |
145
|
|
|
|
146
|
|
|
var i; |
147
|
|
|
for(i = 0; i < 12; i += 2){ |
148
|
|
|
result += parseInt(number[i]); |
149
|
|
|
} |
150
|
|
|
for(i = 1; i < 12; i += 2){ |
151
|
|
|
result += parseInt(number[i]) * 3; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return (10 - (result % 10)) % 10; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
export default EAN13; |
158
|
|
|
|