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