1
hao
2025-03-27 e610e1c17f62b423a717fadaaa7b139d02857793
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { SIDE_BIN, MIDDLE_BIN } from './constants';
import encode from './encoder';
import Barcode from '../Barcode';
 
// Base class for EAN8 & EAN13
class EAN extends Barcode {
 
    constructor(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) {
        return this.text.substr(from, to);
    }
 
    leftEncode(data, structure) {
        return encode(data, structure);
    }
 
    rightText(from, to) {
        return this.text.substr(from, to);
    }
 
    rightEncode(data, structure) {
        return encode(data, structure);
    }
 
    encodeGuarded() {
        const textOptions = { fontSize: this.fontSize };
        const guardOptions = { height: this.guardHeight };
 
        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 data = [
            SIDE_BIN,
            this.leftEncode(),
            MIDDLE_BIN,
            this.rightEncode(),
            SIDE_BIN
        ];
 
        return {
            data: data.join(''),
            text: this.text
        };
    }
 
}
 
export default EAN;