cleanup
This commit is contained in:
1 parent
a4167688a0
commit
9741f0ff8d
12 files changed
+143
-270
No files matched your search
@@ -1,21 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
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
|
||||
}
|
||||
+57
-6
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
@@ -17,15 +18,65 @@ func strToRunes(str string) []rune {
|
||||
return result
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func Encode(content string) (barcode.Barcode, error) {
|
||||
contentRunes := strToRunes(content)
|
||||
idxList := encodeBTable(contentRunes)
|
||||
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)
|
||||
|
||||
if idxList == nil {
|
||||
return nil, fmt.Errorf("\"%s\" could not be encoded", content)
|
||||
}
|
||||
|
||||
result := &code{new(utils.BitList), content}
|
||||
result := new(utils.BitList)
|
||||
sum := 0
|
||||
for i, idx := range idxList.GetBytes() {
|
||||
if i == 0 {
|
||||
@@ -33,9 +84,9 @@ func Encode(content string) (barcode.Barcode, error) {
|
||||
} else {
|
||||
sum += i * int(idx)
|
||||
}
|
||||
result.AddRange(encodingTable[idx])
|
||||
result.AddBit(encodingTable[idx]...)
|
||||
}
|
||||
result.AddRange(encodingTable[sum%103])
|
||||
result.AddRange(encodingTable[stopSymbol])
|
||||
return result, nil
|
||||
result.AddBit(encodingTable[sum%103]...)
|
||||
result.AddBit(encodingTable[stopSymbol]...)
|
||||
return utils.New1DCode("Code 128", content, result), nil
|
||||
}
|
||||
@@ -114,3 +114,5 @@ const startASymbol byte = 103
|
||||
const startBSymbol byte = 104
|
||||
const startCSymbol byte = 105
|
||||
const stopSymbol byte = 106
|
||||
|
||||
const bTable = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
||||
Reference in new issue
Block a user