added basic code128 support

This commit is contained in:
boombuler 2013-12-15 21:56:38 +01:00
parent 987cf1bccc
commit a4167688a0
5 changed files with 221 additions and 0 deletions

21
code128/bencoder.go Normal file
View File

@ -0,0 +1,21 @@
package code128
import (
"github.com/boombuler/barcode/utils"
"strings"
)
const bTable = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
func encodeBTable(content []rune) *utils.BitList {
result := new(utils.BitList)
result.AddByte(startBSymbol)
for _, r := range content {
idx := strings.IndexRune(bTable, r)
if idx < 0 {
return nil
}
result.AddByte(byte(idx))
}
return result
}

36
code128/code.go Normal file
View File

@ -0,0 +1,36 @@
package code128
import (
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/utils"
"image"
"image/color"
)
type code struct {
*utils.BitList
content string
}
func (c *code) Content() string {
return c.content
}
func (c *code) Metadata() barcode.Metadata {
return barcode.Metadata{"Code 128", 1}
}
func (c *code) ColorModel() color.Model {
return color.Gray16Model
}
func (c *code) Bounds() image.Rectangle {
return image.Rect(0, 0, c.Len(), 1)
}
func (c *code) At(x, y int) color.Color {
if c.GetBit(x) {
return color.Black
}
return color.White
}

41
code128/encode.go Normal file
View File

@ -0,0 +1,41 @@
package code128
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/utils"
"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
}
func Encode(content string) (barcode.Barcode, error) {
contentRunes := strToRunes(content)
idxList := encodeBTable(contentRunes)
if idxList == nil {
return nil, fmt.Errorf("\"%s\" could not be encoded", content)
}
result := &code{new(utils.BitList), content}
sum := 0
for i, idx := range idxList.GetBytes() {
if i == 0 {
sum = int(idx)
} else {
sum += i * int(idx)
}
result.AddRange(encodingTable[idx])
}
result.AddRange(encodingTable[sum%103])
result.AddRange(encodingTable[stopSymbol])
return result, nil
}

116
code128/encodingtable.go Normal file
View File

@ -0,0 +1,116 @@
package code128
var encodingTable [107][]bool = [107][]bool{
[]bool{true, true, false, true, true, false, false, true, true, false, false},
[]bool{true, true, false, false, true, true, false, true, true, false, false},
[]bool{true, true, false, false, true, true, false, false, true, true, false},
[]bool{true, false, false, true, false, false, true, true, false, false, false},
[]bool{true, false, false, true, false, false, false, true, true, false, false},
[]bool{true, false, false, false, true, false, false, true, true, false, false},
[]bool{true, false, false, true, true, false, false, true, false, false, false},
[]bool{true, false, false, true, true, false, false, false, true, false, false},
[]bool{true, false, false, false, true, true, false, false, true, false, false},
[]bool{true, true, false, false, true, false, false, true, false, false, false},
[]bool{true, true, false, false, true, false, false, false, true, false, false},
[]bool{true, true, false, false, false, true, false, false, true, false, false},
[]bool{true, false, true, true, false, false, true, true, true, false, false},
[]bool{true, false, false, true, true, false, true, true, true, false, false},
[]bool{true, false, false, true, true, false, false, true, true, true, false},
[]bool{true, false, true, true, true, false, false, true, true, false, false},
[]bool{true, false, false, true, true, true, false, true, true, false, false},
[]bool{true, false, false, true, true, true, false, false, true, true, false},
[]bool{true, true, false, false, true, true, true, false, false, true, false},
[]bool{true, true, false, false, true, false, true, true, true, false, false},
[]bool{true, true, false, false, true, false, false, true, true, true, false},
[]bool{true, true, false, true, true, true, false, false, true, false, false},
[]bool{true, true, false, false, true, true, true, false, true, false, false},
[]bool{true, true, true, false, true, true, false, true, true, true, false},
[]bool{true, true, true, false, true, false, false, true, true, false, false},
[]bool{true, true, true, false, false, true, false, true, true, false, false},
[]bool{true, true, true, false, false, true, false, false, true, true, false},
[]bool{true, true, true, false, true, true, false, false, true, false, false},
[]bool{true, true, true, false, false, true, true, false, true, false, false},
[]bool{true, true, true, false, false, true, true, false, false, true, false},
[]bool{true, true, false, true, true, false, true, true, false, false, false},
[]bool{true, true, false, true, true, false, false, false, true, true, false},
[]bool{true, true, false, false, false, true, true, false, true, true, false},
[]bool{true, false, true, false, false, false, true, true, false, false, false},
[]bool{true, false, false, false, true, false, true, true, false, false, false},
[]bool{true, false, false, false, true, false, false, false, true, true, false},
[]bool{true, false, true, true, false, false, false, true, false, false, false},
[]bool{true, false, false, false, true, true, false, true, false, false, false},
[]bool{true, false, false, false, true, true, false, false, false, true, false},
[]bool{true, true, false, true, false, false, false, true, false, false, false},
[]bool{true, true, false, false, false, true, false, true, false, false, false},
[]bool{true, true, false, false, false, true, false, false, false, true, false},
[]bool{true, false, true, true, false, true, true, true, false, false, false},
[]bool{true, false, true, true, false, false, false, true, true, true, false},
[]bool{true, false, false, false, true, true, false, true, true, true, false},
[]bool{true, false, true, true, true, false, true, true, false, false, false},
[]bool{true, false, true, true, true, false, false, false, true, true, false},
[]bool{true, false, false, false, true, true, true, false, true, true, false},
[]bool{true, true, true, false, true, true, true, false, true, true, false},
[]bool{true, true, false, true, false, false, false, true, true, true, false},
[]bool{true, true, false, false, false, true, false, true, true, true, false},
[]bool{true, true, false, true, true, true, false, true, false, false, false},
[]bool{true, true, false, true, true, true, false, false, false, true, false},
[]bool{true, true, false, true, true, true, false, true, true, true, false},
[]bool{true, true, true, false, true, false, true, true, false, false, false},
[]bool{true, true, true, false, true, false, false, false, true, true, false},
[]bool{true, true, true, false, false, false, true, false, true, true, false},
[]bool{true, true, true, false, true, true, false, true, false, false, false},
[]bool{true, true, true, false, true, true, false, false, false, true, false},
[]bool{true, true, true, false, false, false, true, true, false, true, false},
[]bool{true, true, true, false, true, true, true, true, false, true, false},
[]bool{true, true, false, false, true, false, false, false, false, true, false},
[]bool{true, true, true, true, false, false, false, true, false, true, false},
[]bool{true, false, true, false, false, true, true, false, false, false, false},
[]bool{true, false, true, false, false, false, false, true, true, false, false},
[]bool{true, false, false, true, false, true, true, false, false, false, false},
[]bool{true, false, false, true, false, false, false, false, true, true, false},
[]bool{true, false, false, false, false, true, false, true, true, false, false},
[]bool{true, false, false, false, false, true, false, false, true, true, false},
[]bool{true, false, true, true, false, false, true, false, false, false, false},
[]bool{true, false, true, true, false, false, false, false, true, false, false},
[]bool{true, false, false, true, true, false, true, false, false, false, false},
[]bool{true, false, false, true, true, false, false, false, false, true, false},
[]bool{true, false, false, false, false, true, true, false, true, false, false},
[]bool{true, false, false, false, false, true, true, false, false, true, false},
[]bool{true, true, false, false, false, false, true, false, false, true, false},
[]bool{true, true, false, false, true, false, true, false, false, false, false},
[]bool{true, true, true, true, false, true, true, true, false, true, false},
[]bool{true, true, false, false, false, false, true, false, true, false, false},
[]bool{true, false, false, false, true, true, true, true, false, true, false},
[]bool{true, false, true, false, false, true, true, true, true, false, false},
[]bool{true, false, false, true, false, true, true, true, true, false, false},
[]bool{true, false, false, true, false, false, true, true, true, true, false},
[]bool{true, false, true, true, true, true, false, false, true, false, false},
[]bool{true, false, false, true, true, true, true, false, true, false, false},
[]bool{true, false, false, true, true, true, true, false, false, true, false},
[]bool{true, true, true, true, false, true, false, false, true, false, false},
[]bool{true, true, true, true, false, false, true, false, true, false, false},
[]bool{true, true, true, true, false, false, true, false, false, true, false},
[]bool{true, true, false, true, true, false, true, true, true, true, false},
[]bool{true, true, false, true, true, true, true, false, true, true, false},
[]bool{true, true, true, true, false, true, true, false, true, true, false},
[]bool{true, false, true, false, true, true, true, true, false, false, false},
[]bool{true, false, true, false, false, false, true, true, true, true, false},
[]bool{true, false, false, false, true, false, true, true, true, true, false},
[]bool{true, false, true, true, true, true, false, true, false, false, false},
[]bool{true, false, true, true, true, true, false, false, false, true, false},
[]bool{true, true, true, true, false, true, false, true, false, false, false},
[]bool{true, true, true, true, false, true, false, false, false, true, false},
[]bool{true, false, true, true, true, false, true, true, true, true, false},
[]bool{true, false, true, true, true, true, false, true, true, true, false},
[]bool{true, true, true, false, true, false, true, true, true, true, false},
[]bool{true, true, true, true, false, true, false, true, true, true, false},
[]bool{true, true, false, true, false, false, false, false, true, false, false},
[]bool{true, true, false, true, false, false, true, false, false, false, false},
[]bool{true, true, false, true, false, false, true, true, true, false, false},
[]bool{true, true, false, false, false, true, true, true, false, true, false, true, true},
}
const startASymbol byte = 103
const startBSymbol byte = 104
const startCSymbol byte = 105
const stopSymbol byte = 106

View File

@ -79,6 +79,13 @@ func (bl *BitList) AddBits(b int, count byte) {
} }
} }
// appends all bits in the bool-slice to the end of the list
func (bl *BitList) AddRange(bits []bool) {
for _, b := range bits {
bl.AddBit(b)
}
}
// returns all bits of the BitList as a []byte // returns all bits of the BitList as a []byte
func (bl *BitList) GetBytes() []byte { func (bl *BitList) GetBytes() []byte {
len := bl.count >> 3 len := bl.count >> 3