barcode/codabar/encoder.go

50 lines
2.2 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, false, true, true, false, false, true, true},
'A': []bool{true, false, true, true, false, false, true, false, false, true},
'B': []bool{true, false, true, false, false, true, false, false, true, true},
'C': []bool{true, false, false, true, false, false, true, false, true, true},
'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
2013-12-13 17:43:18 +00:00
func Encode(content string) (barcode.Barcode, error) {
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
}
2017-06-17 21:42:17 +00:00
return utils.New1DCode(barcode.TypeCodabar, content, resBits), nil
2013-12-13 17:43:18 +00:00
}