2014-08-11 09:11:24 +00:00
|
|
|
// Package codabar can create Codabar barcodes
|
2013-12-13 17:43:18 +00:00
|
|
|
package codabar
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-11 09:11:24 +00:00
|
|
|
"regexp"
|
|
|
|
|
2013-12-13 17:43:18 +00:00
|
|
|
"github.com/boombuler/barcode"
|
|
|
|
"github.com/boombuler/barcode/utils"
|
|
|
|
)
|
|
|
|
|
2014-08-11 09:11:24 +00:00
|
|
|
var encodingTable = map[rune][]bool{
|
2013-12-13 17:43:18 +00:00
|
|
|
'0': []bool{true, false, true, false, true, false, false, true, true},
|
|
|
|
'1': []bool{true, false, true, false, true, true, false, false, true},
|
|
|
|
'2': []bool{true, false, true, false, false, true, false, true, true},
|
|
|
|
'3': []bool{true, true, false, false, true, false, true, false, true},
|
|
|
|
'4': []bool{true, false, true, true, false, true, false, false, true},
|
|
|
|
'5': []bool{true, true, false, true, false, true, false, false, true},
|
|
|
|
'6': []bool{true, false, false, true, false, true, false, true, true},
|
|
|
|
'7': []bool{true, false, false, true, false, true, true, false, true},
|
|
|
|
'8': []bool{true, false, false, true, true, false, true, false, true},
|
|
|
|
'9': []bool{true, true, false, true, false, false, true, false, true},
|
|
|
|
'-': []bool{true, false, true, false, false, true, true, false, true},
|
|
|
|
'$': []bool{true, false, true, true, false, false, true, false, true},
|
|
|
|
':': []bool{true, true, false, true, false, true, true, false, true, true},
|
|
|
|
'/': []bool{true, true, false, true, true, false, true, false, true, true},
|
|
|
|
'.': []bool{true, true, false, true, true, false, true, true, false, true},
|
2018-02-03 01:20:30 +00:00
|
|
|
'+': []bool{true, false, true, true, false, true, true, false, true, true},
|
2013-12-13 17:43:18 +00:00
|
|
|
'A': []bool{true, false, true, true, false, false, true, false, false, true},
|
2018-02-02 04:14:37 +00:00
|
|
|
'B': []bool{true, false, false, true, false, false, true, false, true, true},
|
2018-02-03 01:20:30 +00:00
|
|
|
'C': []bool{true, false, true, false, false, true, false, false, true, true},
|
2013-12-13 17:43:18 +00:00
|
|
|
'D': []bool{true, false, true, false, false, true, true, false, false, true},
|
|
|
|
}
|
|
|
|
|
2014-08-11 09:11:24 +00:00
|
|
|
// Encode creates a codabar barcode for the given content
|
2024-07-31 19:11:52 +00:00
|
|
|
func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) {
|
2013-12-13 17:43:18 +00:00
|
|
|
checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`)
|
|
|
|
if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" {
|
|
|
|
return nil, fmt.Errorf("can not encode \"%s\"", content)
|
|
|
|
}
|
|
|
|
resBits := new(utils.BitList)
|
|
|
|
for i, r := range content {
|
|
|
|
if i > 0 {
|
|
|
|
resBits.AddBit(false)
|
|
|
|
}
|
2014-08-11 09:11:24 +00:00
|
|
|
resBits.AddBit(encodingTable[r]...)
|
2013-12-13 17:43:18 +00:00
|
|
|
}
|
2024-07-31 19:11:52 +00:00
|
|
|
return utils.New1DCodeWithColor(barcode.TypeCodabar, content, resBits, color), nil
|
2024-07-24 13:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode creates a codabar barcode for the given content
|
|
|
|
func Encode(content string) (barcode.Barcode, error) {
|
2024-07-31 19:11:52 +00:00
|
|
|
return EncodeWithColor(content, barcode.ColorScheme16)
|
2013-12-13 17:43:18 +00:00
|
|
|
}
|