moved checksum function to its own interface

its not needed that all barcodes have an integer checksum
so it should not be needed in every implementation.
This commit is contained in:
Florian Sundermann 2017-01-26 08:20:38 +01:00
parent fe0f26ff6d
commit b066487b3e
11 changed files with 57 additions and 34 deletions

View File

@ -42,10 +42,6 @@ func (c *aztecCode) At(x, y int) color.Color {
return color.White return color.White
} }
func (c *aztecCode) CheckSum() int {
return 0
}
func (c *aztecCode) set(x, y int) { func (c *aztecCode) set(x, y int) {
c.SetBit(x*c.size+y, true) c.SetBit(x*c.size+y, true)
} }

View File

@ -17,5 +17,11 @@ type Barcode interface {
Metadata() Metadata Metadata() Metadata
// the data that was encoded in this barcode // the data that was encoded in this barcode
Content() string Content() string
}
// Additional interface that some barcodes might implement to provide
// the value of its checksum.
type BarcodeIntCS interface {
Barcode
CheckSum() int CheckSum() int
} }

View File

@ -45,5 +45,5 @@ func Encode(content string) (barcode.Barcode, error) {
} }
resBits.AddBit(encodingTable[r]...) resBits.AddBit(encodingTable[r]...)
} }
return utils.New1DCode("Codabar", content, resBits, 0), nil return utils.New1DCode("Codabar", content, resBits), nil
} }

View File

@ -103,7 +103,7 @@ func getCodeIndexList(content []rune) *utils.BitList {
} }
// Encode creates a Code 128 barcode for the given content // Encode creates a Code 128 barcode for the given content
func Encode(content string) (barcode.Barcode, error) { func Encode(content string) (barcode.BarcodeIntCS, error) {
contentRunes := strToRunes(content) contentRunes := strToRunes(content)
if len(contentRunes) <= 0 || len(contentRunes) > 80 { 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)) return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
@ -124,7 +124,8 @@ func Encode(content string) (barcode.Barcode, error) {
} }
result.AddBit(encodingTable[idx]...) result.AddBit(encodingTable[idx]...)
} }
result.AddBit(encodingTable[sum%103]...) sum = sum % 103
result.AddBit(encodingTable[sum]...)
result.AddBit(encodingTable[stopSymbol]...) result.AddBit(encodingTable[stopSymbol]...)
return utils.New1DCode("Code 128", content, result, sum%103), nil return utils.New1DCodeIntCheckSum("Code 128", content, result, sum), nil
} }

View File

@ -113,7 +113,7 @@ func prepare(content string) (string, error) {
// Encode returns a code39 barcode for the given content // Encode returns a code39 barcode for the given content
// if includeChecksum is set to true, a checksum character is calculated and added to the content // if includeChecksum is set to true, a checksum character is calculated and added to the content
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) { func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) {
if fullASCIIMode { if fullASCIIMode {
var err error var err error
content, err = prepare(content) content, err = prepare(content)
@ -148,5 +148,5 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
if err != nil { if err != nil {
checkSum = 0 checkSum = 0
} }
return utils.New1DCode("Code 39", content, result, int(checkSum)), nil return utils.New1DCodeIntCheckSum("Code 39", content, result, int(checkSum)), nil
} }

View File

@ -40,10 +40,6 @@ func (c *datamatrixCode) At(x, y int) color.Color {
return color.White return color.White
} }
func (c *datamatrixCode) CheckSum() int {
return 0
}
func (c *datamatrixCode) get(x, y int) bool { func (c *datamatrixCode) get(x, y int) bool {
return c.GetBit(x*c.Rows + y) return c.GetBit(x*c.Rows + y)
} }

View File

@ -158,7 +158,7 @@ func encodeEAN13(code string) *utils.BitList {
} }
// Encode returns a EAN 8 or EAN 13 barcode for the given code // Encode returns a EAN 8 or EAN 13 barcode for the given code
func Encode(code string) (barcode.Barcode, error) { func Encode(code string) (barcode.BarcodeIntCS, error) {
var checkSum int var checkSum int
if len(code) == 7 || len(code) == 12 { if len(code) == 7 || len(code) == 12 {
code += string(calcCheckNum(code)) code += string(calcCheckNum(code))
@ -175,12 +175,12 @@ func Encode(code string) (barcode.Barcode, error) {
if len(code) == 8 { if len(code) == 8 {
result := encodeEAN8(code) result := encodeEAN8(code)
if result != nil { if result != nil {
return utils.New1DCode("EAN 8", code, result, checkSum), nil return utils.New1DCodeIntCheckSum("EAN 8", code, result, checkSum), nil
} }
} else if len(code) == 13 { } else if len(code) == 13 {
result := encodeEAN13(code) result := encodeEAN13(code)
if result != nil { if result != nil {
return utils.New1DCode("EAN 13", code, result, checkSum), nil return utils.New1DCodeIntCheckSum("EAN 13", code, result, checkSum), nil
} }
} }
return nil, errors.New("invalid ean code data") return nil, errors.New("invalid ean code data")

View File

@ -46,10 +46,6 @@ func (qr *qrcode) Set(x, y int, val bool) {
qr.data.SetBit(x*qr.dimension+y, val) qr.data.SetBit(x*qr.dimension+y, val)
} }
func (qr *qrcode) CheckSum() int {
return 0
}
func (qr *qrcode) calcPenalty() uint { func (qr *qrcode) calcPenalty() uint {
return qr.calcPenaltyRule1() + qr.calcPenaltyRule2() + qr.calcPenaltyRule3() + qr.calcPenaltyRule4() return qr.calcPenaltyRule1() + qr.calcPenaltyRule2() + qr.calcPenaltyRule3() + qr.calcPenaltyRule4()
} }

View File

@ -16,6 +16,10 @@ type scaledBarcode struct {
rect image.Rectangle rect image.Rectangle
} }
type intCSscaledBC struct {
scaledBarcode
}
func (bc *scaledBarcode) Content() string { func (bc *scaledBarcode) Content() string {
return bc.wrapped.Content() return bc.wrapped.Content()
} }
@ -36,8 +40,11 @@ func (bc *scaledBarcode) At(x, y int) color.Color {
return bc.wrapperFunc(x, y) return bc.wrapperFunc(x, y)
} }
func (bc *scaledBarcode) CheckSum() int { func (bc *intCSscaledBC) CheckSum() int {
return bc.wrapped.CheckSum() if cs, ok := bc.wrapped.(BarcodeIntCS); ok {
return cs.CheckSum()
}
return 0
} }
// Scale returns a resized barcode with the given width and height. // Scale returns a resized barcode with the given width and height.
@ -52,6 +59,19 @@ func Scale(bc Barcode, width, height int) (Barcode, error) {
return nil, errors.New("unsupported barcode format") return nil, errors.New("unsupported barcode format")
} }
func newScaledBC(wrapped Barcode, wrapperFunc wrapFunc, rect image.Rectangle) Barcode {
result := &scaledBarcode{
wrapped: wrapped,
wrapperFunc: wrapperFunc,
rect: rect,
}
if _, ok := wrapped.(BarcodeIntCS); ok {
return &intCSscaledBC{*result}
}
return result
}
func scale2DCode(bc Barcode, width, height int) (Barcode, error) { func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
orgBounds := bc.Bounds() orgBounds := bc.Bounds()
orgWidth := orgBounds.Max.X - orgBounds.Min.X orgWidth := orgBounds.Max.X - orgBounds.Min.X
@ -77,11 +97,11 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
return bc.At(x, y) return bc.At(x, y)
} }
return &scaledBarcode{ return newScaledBC(
bc, bc,
wrap, wrap,
image.Rect(0, 0, width, height), image.Rect(0, 0, width, height),
}, nil ), nil
} }
func scale1DCode(bc Barcode, width, height int) (Barcode, error) { func scale1DCode(bc Barcode, width, height int) (Barcode, error) {
@ -106,10 +126,9 @@ func scale1DCode(bc Barcode, width, height int) (Barcode, error) {
return bc.At(x, 0) return bc.At(x, 0)
} }
return &scaledBarcode{ return newScaledBC(
bc, bc,
wrap, wrap,
image.Rect(0, 0, width, height), image.Rect(0, 0, width, height),
}, nil ), nil
} }

View File

@ -134,5 +134,5 @@ func Encode(content string, interleaved bool) (barcode.Barcode, error) {
if interleaved { if interleaved {
kindTxt = " (interleaved)" kindTxt = " (interleaved)"
} }
return utils.New1DCode("2 of 5"+kindTxt, content, resBits, -1), nil return utils.New1DCode("2 of 5"+kindTxt, content, resBits), nil
} }

View File

@ -12,6 +12,10 @@ type base1DCode struct {
*BitList *BitList
kind string kind string
content string content string
}
type base1DCodeIntCS struct {
base1DCode
checksum int checksum int
} }
@ -38,11 +42,16 @@ func (c *base1DCode) At(x, y int) color.Color {
return color.White return color.White
} }
func (c *base1DCode) CheckSum() int { func (c *base1DCodeIntCS) CheckSum() int {
return c.checksum return c.checksum
} }
// New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList // New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList
func New1DCode(codeKind, content string, bars *BitList, checksum int) barcode.Barcode { func New1DCodeIntCheckSum(codeKind, content string, bars *BitList, checksum int) barcode.BarcodeIntCS {
return &base1DCode{bars, codeKind, content, checksum} return &base1DCodeIntCS{base1DCode{bars, codeKind, content}, checksum}
}
// New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList
func New1DCode(codeKind, content string, bars *BitList) barcode.Barcode {
return &base1DCode{bars, codeKind, content}
} }