1
|
|
|
// Encoding documentation: |
2
|
|
|
// http://www.barcodeisland.com/ean8.phtml |
3
|
|
|
|
4
|
|
|
import EANencoder from './ean_encoder.js'; |
5
|
|
|
import Barcode from "../Barcode.js"; |
6
|
|
|
|
7
|
|
|
class EAN8 extends Barcode{ |
8
|
|
|
constructor(data, options){ |
9
|
|
|
// Add checksum if it does not exist |
10
|
|
|
if(data.search(/^[0-9]{7}$/) !== -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]{8}$/) !== -1 && |
33
|
|
|
this.data[7] == 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
|
|
|
guardedEncoding(){ |
46
|
|
|
var encoder = new EANencoder(); |
47
|
|
|
var result = []; |
48
|
|
|
|
49
|
|
|
var structure = 'LLLL'; |
50
|
|
|
|
51
|
|
|
// Get the string to be encoded on the left side of the EAN code |
52
|
|
|
var leftSide = this.data.substr(0, 4); |
53
|
|
|
|
54
|
|
|
// Get the string to be encoded on the right side of the EAN code |
55
|
|
|
var rightSide = this.data.substr(4, 4); |
56
|
|
|
|
57
|
|
|
// First guard bars |
58
|
|
|
result.push({ |
59
|
|
|
data: "101", |
60
|
|
|
options: {height: this.guardHeight} |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
// Add the left side |
64
|
|
|
result.push({ |
65
|
|
|
data: encoder.encode(leftSide, structure), |
66
|
|
|
text: this.text.substr(0, 4), |
67
|
|
|
options: {fontSize: this.fontSize} |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
// Add the middle bits |
71
|
|
|
result.push({ |
72
|
|
|
data: "01010", |
73
|
|
|
options: {height: this.guardHeight} |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
// Add the right side |
77
|
|
|
result.push({ |
78
|
|
|
data: encoder.encode(rightSide, "RRRR"), |
79
|
|
|
text: this.text.substr(4, 4), |
80
|
|
|
options: {fontSize: this.fontSize} |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
// Add the end bits |
84
|
|
|
result.push({ |
85
|
|
|
data: "101", |
86
|
|
|
options: {height: this.guardHeight} |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
return result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
flatEncoding(){ |
93
|
|
|
var encoder = new EANencoder(); |
94
|
|
|
var result = ""; |
95
|
|
|
|
96
|
|
|
var structure = 'LLLL'; |
97
|
|
|
|
98
|
|
|
result += "101"; |
99
|
|
|
result += encoder.encode(this.data.substr(0, 4), structure); |
100
|
|
|
result += "01010"; |
101
|
|
|
result += encoder.encode(this.data.substr(4, 4), "RRRR"); |
102
|
|
|
result += "101"; |
103
|
|
|
|
104
|
|
|
return { |
105
|
|
|
data: result, |
106
|
|
|
text: this.text |
107
|
|
|
}; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Calulate the checksum digit |
112
|
|
|
function checksum(number){ |
113
|
|
|
var result = 0; |
114
|
|
|
|
115
|
|
|
var i; |
116
|
|
|
for(i = 0; i < 7; i += 2){ |
117
|
|
|
result += parseInt(number[i]) * 3; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
for(i = 1; i < 7; i += 2){ |
121
|
|
|
result += parseInt(number[i]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return (10 - (result % 10)) % 10; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
export default EAN8; |
128
|
|
|
|