forked from juliankoehn/barcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_39.go
112 lines (95 loc) · 1.72 KB
/
code_39.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package barcode
import (
"strconv"
"strings"
)
func barcodeCode39(code string, extended, checksum bool) *barArray {
if code == "" {
return nil
}
if extended {
// code = encode_code39_ext(code)
code = encodeCode39Ext(code)
}
if checksum {
code = code + checksumCode39(code)
}
code = strings.ToUpper(code)
// add start and stop codes if they does not exists on code
if code[len(code)-1:] != "*" {
code = code + "*"
}
if code[:1] != "*" {
code = "*" + code
}
bararray := barArray{
Code: code,
MaxW: 0,
MaxH: 1,
BCode: []bCode{},
}
// avg 7 iterations
for i := 0; i < len(code); i++ {
char := string([]rune(code)[i])
chrs := chr[char]
if chrs == "" {
return nil
}
for j := 0; j < 9; j++ {
var t bool
if j%2 == 0 {
t = true
} else {
t = false
}
w := string([]rune(chr[char])[j])
wValue, _ := strconv.Atoi(w)
x := bCode{
T: t,
W: wValue,
H: 1,
P: 0,
}
bararray.BCode = append(bararray.BCode, x)
bararray.MaxW = bararray.MaxW + wValue
}
// gaps
bararray.BCode = append(bararray.BCode, bCode{
T: false,
W: 1,
H: 1,
P: 0,
})
}
// 128
bararray.MaxW += len(code)
return &bararray
}
/**
* Calculate Code 39 checksum (modulo 43)
*/
func checksumCode39(code string) string {
sum := 0
for _, r := range code {
v := chars[r]
sum += v
}
sum = sum % 43
for r, v := range chars {
if v == sum {
return string(r)
}
}
return "#"
}
// encodeCode39Ext encode a string to be used for code 39 extended mode
func encodeCode39Ext(code string) string {
var codeExt string
for _, r := range code {
if int(r) > len(encodeDictionary) {
return ""
}
codeExt = codeExt + encodeDictionary[int(r)]
}
return codeExt
}