2013-12-15 20:56:38 +00:00
|
|
|
package code128
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/boombuler/barcode"
|
|
|
|
"github.com/boombuler/barcode/utils"
|
2013-12-16 13:10:24 +00:00
|
|
|
"strings"
|
2013-12-15 20:56:38 +00:00
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
func strToRunes(str string) []rune {
|
|
|
|
result := make([]rune, utf8.RuneCountInString(str))
|
|
|
|
i := 0
|
|
|
|
for _, r := range str {
|
|
|
|
result[i] = r
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2013-12-16 13:10:24 +00:00
|
|
|
func shouldUseCTable(nextRunes []rune, curEncoding byte) bool {
|
|
|
|
requiredDigits := 4
|
|
|
|
if curEncoding == startCSymbol {
|
|
|
|
requiredDigits = 2
|
|
|
|
}
|
|
|
|
if len(nextRunes) < requiredDigits {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < requiredDigits; i++ {
|
|
|
|
if nextRunes[i] < '0' || nextRunes[i] > '9' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCodeIndexList(content []rune) *utils.BitList {
|
|
|
|
result := new(utils.BitList)
|
|
|
|
curEncoding := byte(0)
|
|
|
|
for i := 0; i < len(content); i++ {
|
|
|
|
|
|
|
|
if shouldUseCTable(content[i:], curEncoding) {
|
|
|
|
if curEncoding != startCSymbol {
|
|
|
|
result.AddByte(startCSymbol)
|
|
|
|
curEncoding = startCSymbol
|
|
|
|
}
|
|
|
|
idx := (content[i] - '0') * 10
|
|
|
|
i++
|
|
|
|
idx = idx + (content[i] - '0')
|
|
|
|
|
|
|
|
result.AddByte(byte(idx))
|
|
|
|
} else {
|
|
|
|
if curEncoding != startBSymbol {
|
|
|
|
result.AddByte(startBSymbol)
|
|
|
|
curEncoding = startBSymbol
|
|
|
|
}
|
|
|
|
idx := strings.IndexRune(bTable, content[i])
|
|
|
|
if idx < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
result.AddByte(byte(idx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println(result.GetBytes())
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2013-12-15 20:56:38 +00:00
|
|
|
func Encode(content string) (barcode.Barcode, error) {
|
|
|
|
contentRunes := strToRunes(content)
|
2013-12-16 13:10:24 +00:00
|
|
|
if len(contentRunes) < 0 || len(contentRunes) > 80 {
|
|
|
|
return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
|
|
|
|
}
|
|
|
|
idxList := getCodeIndexList(contentRunes)
|
2013-12-15 20:56:38 +00:00
|
|
|
|
|
|
|
if idxList == nil {
|
|
|
|
return nil, fmt.Errorf("\"%s\" could not be encoded", content)
|
|
|
|
}
|
|
|
|
|
2013-12-16 13:10:24 +00:00
|
|
|
result := new(utils.BitList)
|
2013-12-15 20:56:38 +00:00
|
|
|
sum := 0
|
|
|
|
for i, idx := range idxList.GetBytes() {
|
|
|
|
if i == 0 {
|
|
|
|
sum = int(idx)
|
|
|
|
} else {
|
|
|
|
sum += i * int(idx)
|
|
|
|
}
|
2013-12-16 13:10:24 +00:00
|
|
|
result.AddBit(encodingTable[idx]...)
|
2013-12-15 20:56:38 +00:00
|
|
|
}
|
2013-12-16 13:10:24 +00:00
|
|
|
result.AddBit(encodingTable[sum%103]...)
|
|
|
|
result.AddBit(encodingTable[stopSymbol]...)
|
|
|
|
return utils.New1DCode("Code 128", content, result), nil
|
2013-12-15 20:56:38 +00:00
|
|
|
}
|