1
|
|
|
import { SIDE_BIN, MIDDLE_BIN } from './constants'; |
2
|
|
|
import encode from './encoder'; |
3
|
|
|
import Barcode from '../Barcode'; |
4
|
|
|
|
5
|
|
|
// Base class for EAN8 & EAN13 |
6
|
|
|
class EAN extends Barcode { |
7
|
|
|
|
8
|
|
|
constructor(data, options) { |
9
|
|
|
super(data, options); |
10
|
|
|
|
11
|
|
|
// Make sure the font is not bigger than the space between the guard bars |
12
|
|
|
this.fontSize = !options.flat && options.fontSize > options.width * 10 |
13
|
|
|
? options.width * 10 |
14
|
|
|
: options.fontSize; |
15
|
|
|
|
16
|
|
|
// Make the guard bars go down half the way of the text |
17
|
|
|
this.guardHeight = options.height + this.fontSize / 2 + options.textMargin; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
encode() { |
21
|
|
|
return this.options.flat |
22
|
|
|
? this.encodeFlat() |
23
|
|
|
: this.encodeGuarded(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
leftText(from, to) { |
27
|
|
|
return this.text.substr(from, to); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
leftEncode(data, structure) { |
31
|
|
|
return encode(data, structure); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
rightText(from, to) { |
35
|
|
|
return this.text.substr(from, to); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
rightEncode(data, structure) { |
39
|
|
|
return encode(data, structure); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
encodeGuarded() { |
43
|
|
|
const textOptions = { fontSize: this.fontSize }; |
44
|
|
|
const guardOptions = { height: this.guardHeight }; |
45
|
|
|
|
46
|
|
|
return [ |
47
|
|
|
{ data: SIDE_BIN, options: guardOptions }, |
48
|
|
|
{ data: this.leftEncode(), text: this.leftText(), options: textOptions }, |
49
|
|
|
{ data: MIDDLE_BIN, options: guardOptions }, |
50
|
|
|
{ data: this.rightEncode(), text: this.rightText(), options: textOptions }, |
51
|
|
|
{ data: SIDE_BIN, options: guardOptions }, |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
encodeFlat() { |
56
|
|
|
const data = [ |
57
|
|
|
SIDE_BIN, |
58
|
|
|
this.leftEncode(), |
59
|
|
|
MIDDLE_BIN, |
60
|
|
|
this.rightEncode(), |
61
|
|
|
SIDE_BIN |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
return { |
65
|
|
|
data: data.join(''), |
66
|
|
|
text: this.text |
67
|
|
|
}; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
export default EAN; |
73
|
|
|
|