barcode/codabar/encoder.go

55 lines
2.4 KiB
Go
Raw Normal View History

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},
'+': []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},
'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
func EncodeWithDepth(content string, depth int) (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
}
return utils.New1DCodeWithDepth(barcode.TypeCodabar, content, resBits, depth), nil
}
// Encode creates a codabar barcode for the given content
func Encode(content string) (barcode.Barcode, error) {
return EncodeWithDepth(content, 16)
2013-12-13 17:43:18 +00:00
}