Skip to content

Commit

Permalink
Merge pull request #84 from zhaori96/allow-other-depths
Browse files Browse the repository at this point in the history
Allow other color schemes and models for barcodes.
  • Loading branch information
boombuler authored Aug 3, 2024
2 parents 83789df + d79eb87 commit ca3e24f
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 71 deletions.
15 changes: 10 additions & 5 deletions aztec/azteccode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ type aztecCode struct {
*utils.BitList
size int
content []byte
color barcode.ColorScheme
}

func newAztecCode(size int) *aztecCode {
return &aztecCode{utils.NewBitList(size * size), size, nil}
func newAztecCode(size int, color barcode.ColorScheme) *aztecCode {
return &aztecCode{utils.NewBitList(size * size), size, nil, barcode.ColorScheme16}
}

func (c *aztecCode) Content() string {
Expand All @@ -28,7 +29,11 @@ func (c *aztecCode) Metadata() barcode.Metadata {
}

func (c *aztecCode) ColorModel() color.Model {
return color.Gray16Model
return c.color.Model
}

func (c *aztecCode) ColorScheme() barcode.ColorScheme {
return c.color
}

func (c *aztecCode) Bounds() image.Rectangle {
Expand All @@ -37,9 +42,9 @@ func (c *aztecCode) Bounds() image.Rectangle {

func (c *aztecCode) At(x, y int) color.Color {
if c.GetBit(x*c.size + y) {
return color.Black
return c.color.Foreground
}
return color.White
return c.color.Background
}

func (c *aztecCode) set(x, y int) {
Expand Down
7 changes: 6 additions & 1 deletion aztec/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ func drawBullsEye(matrix *aztecCode, center, size int) {

// Encode returns an aztec barcode with the given content
func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Barcode, error) {
return EncodeWithColor(data, minECCPercent, userSpecifiedLayers, barcode.ColorScheme16)
}

// Encode returns an aztec barcode with the given content and color scheme
func EncodeWithColor(data []byte, minECCPercent int, userSpecifiedLayers int, color barcode.ColorScheme) (barcode.Barcode, error) {
bits := highlevelEncode(data)
eccBits := ((bits.Len() * minECCPercent) / 100) + 11
totalSizeBits := bits.Len() + eccBits
Expand Down Expand Up @@ -215,7 +220,7 @@ func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Ba
alignmentMap[origCenter+i] = center + newOffset + 1
}
}
code := newAztecCode(matrixSize)
code := newAztecCode(matrixSize, color)
code.content = data

// draw data bits
Expand Down
8 changes: 7 additions & 1 deletion barcode.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package barcode

import "image"
import (
"image"
)

const (
TypeAztec = "Aztec"
Expand Down Expand Up @@ -40,3 +42,7 @@ type BarcodeIntCS interface {
Barcode
CheckSum() int
}

type BarcodeColor interface {
ColorScheme() ColorScheme
}
11 changes: 8 additions & 3 deletions codabar/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ var encodingTable = map[rune][]bool{
'D': []bool{true, false, true, false, false, true, true, false, false, true},
}

// Encode creates a codabar barcode for the given content
func Encode(content string) (barcode.Barcode, error) {
// Encode creates a codabar barcode for the given content and color scheme
func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) {
checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`)
if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" {
return nil, fmt.Errorf("can not encode \"%s\"", content)
Expand All @@ -45,5 +45,10 @@ func Encode(content string) (barcode.Barcode, error) {
}
resBits.AddBit(encodingTable[r]...)
}
return utils.New1DCode(barcode.TypeCodabar, content, resBits), nil
return utils.New1DCodeWithColor(barcode.TypeCodabar, content, resBits, color), nil
}

// Encode creates a codabar barcode for the given content
func Encode(content string) (barcode.Barcode, error) {
return EncodeWithColor(content, barcode.ColorScheme16)
}
17 changes: 13 additions & 4 deletions code128/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ func getCodeIndexList(content []rune) *utils.BitList {
return result
}

// Encode creates a Code 128 barcode for the given content
func Encode(content string) (barcode.BarcodeIntCS, error) {
// Encode creates a Code 128 barcode for the given content and color scheme
func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
contentRunes := strToRunes(content)
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))
Expand All @@ -180,10 +180,19 @@ func Encode(content string) (barcode.BarcodeIntCS, error) {
sum = sum % 103
result.AddBit(encodingTable[sum]...)
result.AddBit(encodingTable[stopSymbol]...)
return utils.New1DCodeIntCheckSum(barcode.TypeCode128, content, result, sum), nil
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode128, content, result, sum, color), nil
}

// Encode creates a Code 128 barcode for the given content
func Encode(content string) (barcode.BarcodeIntCS, error) {
return EncodeWithColor(content, barcode.ColorScheme16)
}

func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {
return EncodeWithoutChecksumWithColor(content, barcode.ColorScheme16)
}

func EncodeWithoutChecksumWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) {
contentRunes := strToRunes(content)
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))
Expand All @@ -199,5 +208,5 @@ func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {
result.AddBit(encodingTable[idx]...)
}
result.AddBit(encodingTable[stopSymbol]...)
return utils.New1DCode(barcode.TypeCode128, content, result), nil
return utils.New1DCodeWithColor(barcode.TypeCode128, content, result, color), nil
}
12 changes: 9 additions & 3 deletions code39/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func prepare(content string) (string, error) {
return result, nil
}

// Encode returns a code39 barcode for the given content
// Encode returns a code39 barcode for the given content and color scheme
// 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.BarcodeIntCS, error) {
func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
if fullASCIIMode {
var err error
content, err = prepare(content)
Expand Down Expand Up @@ -148,5 +148,11 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
if err != nil {
checkSum = 0
}
return utils.New1DCodeIntCheckSum(barcode.TypeCode39, content, result, int(checkSum)), nil
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode39, content, result, int(checkSum), color), nil
}

// 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
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) {
return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
}
12 changes: 9 additions & 3 deletions code93/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func prepare(content string) (string, error) {
return result, nil
}

// Encode returns a code93 barcode for the given content
// Encode returns a code93 barcode for the given content and color scheme
// if includeChecksum is set to true, two checksum characters are calculated and added to the content
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) {
func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, color barcode.ColorScheme) (barcode.Barcode, error) {
if fullASCIIMode {
var err error
content, err = prepare(content)
Expand Down Expand Up @@ -104,7 +104,13 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
}
result.AddBit(true)

return utils.New1DCode(barcode.TypeCode93, content, result), nil
return utils.New1DCodeWithColor(barcode.TypeCode93, content, result, color), nil
}

// Encode returns a code93 barcode for the given content
// if includeChecksum is set to true, two checksum characters are calculated and added to the content
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) {
return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
}

func getChecksum(content string, maxWeight int) rune {
Expand Down
39 changes: 39 additions & 0 deletions color_scheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package barcode

import "image/color"

// ColorScheme defines a structure for color schemes used in barcode rendering.
// It includes the color model, background color, and foreground color.
type ColorScheme struct {
Model color.Model // Color model to be used (e.g., grayscale, RGB, RGBA)
Background color.Color // Color of the background
Foreground color.Color // Color of the foreground (e.g., bars in a barcode)
}

// ColorScheme8 represents a color scheme with 8-bit grayscale colors.
var ColorScheme8 = ColorScheme{
Model: color.GrayModel,
Background: color.Gray{Y: 255},
Foreground: color.Gray{Y: 0},
}

// ColorScheme16 represents a color scheme with 16-bit grayscale colors.
var ColorScheme16 = ColorScheme{
Model: color.Gray16Model,
Background: color.White,
Foreground: color.Black,
}

// ColorScheme24 represents a color scheme with 24-bit RGB colors.
var ColorScheme24 = ColorScheme{
Model: color.RGBAModel,
Background: color.RGBA{255, 255, 255, 255},
Foreground: color.RGBA{0, 0, 0, 255},
}

// ColorScheme32 represents a color scheme with 32-bit RGBA colors, which is similar to ColorScheme24 but typically includes alpha for transparency.
var ColorScheme32 = ColorScheme{
Model: color.RGBAModel,
Background: color.RGBA{255, 255, 255, 255},
Foreground: color.RGBA{0, 0, 0, 255},
}
10 changes: 7 additions & 3 deletions datamatrix/codelayout.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package datamatrix

import (
"github.com/boombuler/barcode/utils"
"strconv"

"github.com/boombuler/barcode"
"github.com/boombuler/barcode/utils"
)

type setValFunc func(byte)
Expand All @@ -11,13 +13,15 @@ type codeLayout struct {
matrix *utils.BitList
occupy *utils.BitList
size *dmCodeSize
color barcode.ColorScheme
}

func newCodeLayout(size *dmCodeSize) *codeLayout {
func newCodeLayout(size *dmCodeSize, color barcode.ColorScheme) *codeLayout {
result := new(codeLayout)
result.matrix = utils.NewBitList(size.MatrixColumns() * size.MatrixRows())
result.occupy = utils.NewBitList(size.MatrixColumns() * size.MatrixRows())
result.size = size
result.color = color
return result
}

Expand Down Expand Up @@ -159,7 +163,7 @@ func (l *codeLayout) SetValues(data []byte) {
}

func (l *codeLayout) Merge() *datamatrixCode {
result := newDataMatrixCode(l.size)
result := newDataMatrixCodeWithColor(l.size, l.color)

//dotted horizontal lines
for r := 0; r < l.size.Rows; r += (l.size.RegionRows() + 2) {
Expand Down
17 changes: 13 additions & 4 deletions datamatrix/datamatrixcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ type datamatrixCode struct {
*utils.BitList
*dmCodeSize
content string
color barcode.ColorScheme
}

func newDataMatrixCodeWithColor(size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode {
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", color}
}

func newDataMatrixCode(size *dmCodeSize) *datamatrixCode {
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, ""}
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", barcode.ColorScheme16}
}

func (c *datamatrixCode) Content() string {
Expand All @@ -27,7 +32,11 @@ func (c *datamatrixCode) Metadata() barcode.Metadata {
}

func (c *datamatrixCode) ColorModel() color.Model {
return color.Gray16Model
return c.color.Model
}

func (c *datamatrixCode) ColorScheme() barcode.ColorScheme {
return c.color
}

func (c *datamatrixCode) Bounds() image.Rectangle {
Expand All @@ -36,9 +45,9 @@ func (c *datamatrixCode) Bounds() image.Rectangle {

func (c *datamatrixCode) At(x, y int) color.Color {
if c.get(x, y) {
return color.Black
return c.color.Foreground
}
return color.White
return c.color.Background
}

func (c *datamatrixCode) get(x, y int) bool {
Expand Down
21 changes: 13 additions & 8 deletions datamatrix/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/boombuler/barcode"
)

// Encode returns a Datamatrix barcode for the given content
func Encode(content string) (barcode.Barcode, error) {
// Encode returns a Datamatrix barcode for the given content and color scheme
func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) {
data := encodeText(content)

var size *dmCodeSize
Expand All @@ -23,16 +23,21 @@ func Encode(content string) (barcode.Barcode, error) {
}
data = addPadding(data, size.DataCodewords())
data = ec.calcECC(data, size)
code := render(data, size)
code := render(data, size, color)
if code != nil {
code.content = content
return code, nil
}
return nil, errors.New("unable to render barcode")
}

func render(data []byte, size *dmCodeSize) *datamatrixCode {
cl := newCodeLayout(size)
// Encode returns a Datamatrix barcode for the given content
func Encode(content string) (barcode.Barcode, error) {
return EncodeWithColor(content, barcode.ColorScheme16)
}

func render(data []byte, size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode {
cl := newCodeLayout(size, color)

cl.SetValues(data)

Expand Down Expand Up @@ -69,11 +74,11 @@ func addPadding(data []byte, toCount int) []byte {
}
for len(data) < toCount {
R := ((149 * (len(data) + 1)) % 253) + 1
tmp := 129 + R;
if (tmp > 254) {
tmp := 129 + R
if tmp > 254 {
tmp = tmp - 254
}

data = append(data, byte(tmp))
}
return data
Expand Down
13 changes: 9 additions & 4 deletions ean/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func encodeEAN13(code string) *utils.BitList {
return result
}

// Encode returns a EAN 8 or EAN 13 barcode for the given code
func Encode(code string) (barcode.BarcodeIntCS, error) {
// Encode returns a EAN 8 or EAN 13 barcode for the given code and color scheme
func EncodeWithColor(code string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
var checkSum int
if len(code) == 7 || len(code) == 12 {
code += string(calcCheckNum(code))
Expand All @@ -175,13 +175,18 @@ func Encode(code string) (barcode.BarcodeIntCS, error) {
if len(code) == 8 {
result := encodeEAN8(code)
if result != nil {
return utils.New1DCodeIntCheckSum(barcode.TypeEAN8, code, result, checkSum), nil
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeEAN8, code, result, checkSum, color), nil
}
} else if len(code) == 13 {
result := encodeEAN13(code)
if result != nil {
return utils.New1DCodeIntCheckSum(barcode.TypeEAN13, code, result, checkSum), nil
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeEAN13, code, result, checkSum, color), nil
}
}
return nil, errors.New("invalid ean code data")
}

// Encode returns a EAN 8 or EAN 13 barcode for the given code
func Encode(code string) (barcode.BarcodeIntCS, error) {
return EncodeWithColor(code, barcode.ColorScheme16)
}
Loading

0 comments on commit ca3e24f

Please sign in to comment.