From a4167688a00307c174a67ed149b08388a6d0666e Mon Sep 17 00:00:00 2001 From: boombuler Date: Sun, 15 Dec 2013 21:56:38 +0100 Subject: [PATCH] added basic code128 support --- code128/bencoder.go | 21 +++++++ code128/code.go | 36 ++++++++++++ code128/encode.go | 41 ++++++++++++++ code128/encodingtable.go | 116 +++++++++++++++++++++++++++++++++++++++ utils/bitlist.go | 7 +++ 5 files changed, 221 insertions(+) create mode 100644 code128/bencoder.go create mode 100644 code128/code.go create mode 100644 code128/encode.go create mode 100644 code128/encodingtable.go diff --git a/code128/bencoder.go b/code128/bencoder.go new file mode 100644 index 0000000..cee8874 --- /dev/null +++ b/code128/bencoder.go @@ -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 +} diff --git a/code128/code.go b/code128/code.go new file mode 100644 index 0000000..e2dbb8c --- /dev/null +++ b/code128/code.go @@ -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 +} diff --git a/code128/encode.go b/code128/encode.go new file mode 100644 index 0000000..0e70375 --- /dev/null +++ b/code128/encode.go @@ -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 +} diff --git a/code128/encodingtable.go b/code128/encodingtable.go new file mode 100644 index 0000000..a94ad6f --- /dev/null +++ b/code128/encodingtable.go @@ -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 diff --git a/utils/bitlist.go b/utils/bitlist.go index f6ef268..2780720 100644 --- a/utils/bitlist.go +++ b/utils/bitlist.go @@ -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 func (bl *BitList) GetBytes() []byte { len := bl.count >> 3