Refactor to use ColorScheme instead of 'depth' approach

This commit is contained in:
zhaori96 2024-07-31 16:11:52 -03:00
parent e966fa7106
commit 30509e0dd7
16 changed files with 83 additions and 81 deletions

View File

@ -13,11 +13,11 @@ type aztecCode struct {
*utils.BitList
size int
content []byte
depth int
color barcode.ColorScheme
}
func newAztecCode(size int, depth int) *aztecCode {
return &aztecCode{utils.NewBitList(size * size), size, nil, 16}
func newAztecCode(size int, color barcode.ColorScheme) *aztecCode {
return &aztecCode{utils.NewBitList(size * size), size, nil, barcode.ColorScheme16}
}
func (c *aztecCode) Content() string {
@ -29,7 +29,7 @@ func (c *aztecCode) Metadata() barcode.Metadata {
}
func (c *aztecCode) ColorModel() color.Model {
return utils.ColorModel(c.depth)
return c.color.Model
}
func (c *aztecCode) Bounds() image.Rectangle {
@ -38,9 +38,9 @@ func (c *aztecCode) Bounds() image.Rectangle {
func (c *aztecCode) At(x, y int) color.Color {
if c.GetBit(x*c.size + y) {
return utils.BlackColor(c.depth)
return c.color.Foreground
}
return utils.WhiteColor(c.depth)
return c.color.Background
}
func (c *aztecCode) set(x, y int) {

View File

@ -124,11 +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 EncodeWithDepth(data, minECCPercent, userSpecifiedLayers, 16)
return EncodeWithColor(data, minECCPercent, userSpecifiedLayers, barcode.ColorScheme16)
}
// Encode returns an aztec barcode with the given content
func EncodeWithDepth(data []byte, minECCPercent int, userSpecifiedLayers int, depth int) (barcode.Barcode, error) {
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
@ -220,7 +220,7 @@ func EncodeWithDepth(data []byte, minECCPercent int, userSpecifiedLayers int, de
alignmentMap[origCenter+i] = center + newOffset + 1
}
}
code := newAztecCode(matrixSize, depth)
code := newAztecCode(matrixSize, color)
code.content = data
// draw data bits

View File

@ -33,7 +33,7 @@ var encodingTable = map[rune][]bool{
}
// Encode creates a codabar barcode for the given content
func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) {
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)
@ -45,10 +45,10 @@ func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) {
}
resBits.AddBit(encodingTable[r]...)
}
return utils.New1DCodeWithDepth(barcode.TypeCodabar, content, resBits, depth), 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 EncodeWithDepth(content, 16)
return EncodeWithColor(content, barcode.ColorScheme16)
}

View File

@ -156,7 +156,7 @@ func getCodeIndexList(content []rune) *utils.BitList {
}
// Encode creates a Code 128 barcode for the given content
func EncodeWithDepth(content string, depth int) (barcode.BarcodeIntCS, error) {
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))
@ -180,12 +180,12 @@ func EncodeWithDepth(content string, depth int) (barcode.BarcodeIntCS, error) {
sum = sum % 103
result.AddBit(encodingTable[sum]...)
result.AddBit(encodingTable[stopSymbol]...)
return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeCode128, content, result, sum, depth), 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 EncodeWithDepth(content, 16)
return EncodeWithColor(content, barcode.ColorScheme16)
}
func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {

View File

@ -113,7 +113,7 @@ func prepare(content string) (string, error) {
// 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 EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, depth int) (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)
@ -148,11 +148,11 @@ func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, d
if err != nil {
checkSum = 0
}
return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeCode39, content, result, int(checkSum), depth), 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 EncodeWithDepth(content, includeChecksum, fullASCIIMode, 16)
return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
}

View File

@ -74,7 +74,7 @@ func prepare(content string) (string, error) {
return result, nil
}
func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, depth int) (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)
@ -102,13 +102,13 @@ func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, d
}
result.AddBit(true)
return utils.New1DCodeWithDepth(barcode.TypeCode93, content, result, depth), 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 EncodeWithDepth(content, includeChecksum, fullASCIIMode, 16)
return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
}
func getChecksum(content string, maxWeight int) rune {

View File

@ -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)
@ -11,15 +13,15 @@ type codeLayout struct {
matrix *utils.BitList
occupy *utils.BitList
size *dmCodeSize
depth int
color barcode.ColorScheme
}
func newCodeLayout(size *dmCodeSize, depth int) *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.depth = depth
result.color = color
return result
}
@ -161,7 +163,7 @@ func (l *codeLayout) SetValues(data []byte) {
}
func (l *codeLayout) Merge() *datamatrixCode {
result := newDataMatrixCodeWithDepth(l.size, l.depth)
result := newDataMatrixCodeWithColor(l.size, l.color)
//dotted horizontal lines
for r := 0; r < l.size.Rows; r += (l.size.RegionRows() + 2) {

View File

@ -12,15 +12,15 @@ type datamatrixCode struct {
*utils.BitList
*dmCodeSize
content string
depth int
color barcode.ColorScheme
}
func newDataMatrixCodeWithDepth(size *dmCodeSize, depth int) *datamatrixCode {
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", depth}
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, "", 16}
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", barcode.ColorScheme16}
}
func (c *datamatrixCode) Content() string {
@ -32,7 +32,7 @@ func (c *datamatrixCode) Metadata() barcode.Metadata {
}
func (c *datamatrixCode) ColorModel() color.Model {
return utils.ColorModel(c.depth)
return c.color.Model
}
func (c *datamatrixCode) Bounds() image.Rectangle {
@ -41,9 +41,9 @@ func (c *datamatrixCode) Bounds() image.Rectangle {
func (c *datamatrixCode) At(x, y int) color.Color {
if c.get(x, y) {
return utils.BlackColor(c.depth)
return c.color.Foreground
}
return utils.WhiteColor(c.depth)
return c.color.Background
}
func (c *datamatrixCode) get(x, y int) bool {

View File

@ -8,7 +8,7 @@ import (
)
// Encode returns a Datamatrix barcode for the given content
func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) {
func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) {
data := encodeText(content)
var size *dmCodeSize
@ -23,7 +23,7 @@ func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) {
}
data = addPadding(data, size.DataCodewords())
data = ec.calcECC(data, size)
code := render(data, size, depth)
code := render(data, size, color)
if code != nil {
code.content = content
return code, nil
@ -33,11 +33,11 @@ func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) {
// Encode returns a Datamatrix barcode for the given content
func Encode(content string) (barcode.Barcode, error) {
return EncodeWithDepth(content, 16)
return EncodeWithColor(content, barcode.ColorScheme16)
}
func render(data []byte, size *dmCodeSize, depth int) *datamatrixCode {
cl := newCodeLayout(size, depth)
func render(data []byte, size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode {
cl := newCodeLayout(size, color)
cl.SetValues(data)
@ -74,8 +74,8 @@ 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
}

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
func EncodeWithDepth(code string, depth int) (barcode.BarcodeIntCS, error) {
func EncodeWithColor(code string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
var checkSum int
if len(code) == 7 || len(code) == 12 {
code += string(calcCheckNum(code))
@ -175,12 +175,12 @@ func EncodeWithDepth(code string, depth int) (barcode.BarcodeIntCS, error) {
if len(code) == 8 {
result := encodeEAN8(code)
if result != nil {
return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeEAN8, code, result, checkSum, depth), nil
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeEAN8, code, result, checkSum, color), nil
}
} else if len(code) == 13 {
result := encodeEAN13(code)
if result != nil {
return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeEAN13, code, result, checkSum, depth), nil
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeEAN13, code, result, checkSum, color), nil
}
}
return nil, errors.New("invalid ean code data")
@ -188,5 +188,5 @@ func EncodeWithDepth(code string, depth int) (barcode.BarcodeIntCS, error) {
// Encode returns a EAN 8 or EAN 13 barcode for the given code
func Encode(code string) (barcode.BarcodeIntCS, error) {
return EncodeWithDepth(code, 16)
return EncodeWithColor(code, barcode.ColorScheme16)
}

View File

@ -15,7 +15,7 @@ const (
// Encodes the given data as PDF417 barcode.
// securityLevel should be between 0 and 8. The higher the number, the more
// additional error-correction codes are added.
func EncodeWithDepth(data string, securityLevel byte, depth int) (barcode.Barcode, error) {
func EncodeWithColor(data string, securityLevel byte, color barcode.ColorScheme) (barcode.Barcode, error) {
if securityLevel >= 9 {
return nil, fmt.Errorf("Invalid security level %d", securityLevel)
}
@ -34,7 +34,7 @@ func EncodeWithDepth(data string, securityLevel byte, depth int) (barcode.Barcod
barcode := new(pdfBarcode)
barcode.data = data
barcode.depth = depth
barcode.color = color
codeWords, err := encodeData(dataWords, columns, sl)
if err != nil {
@ -75,7 +75,7 @@ func EncodeWithDepth(data string, securityLevel byte, depth int) (barcode.Barcod
// securityLevel should be between 0 and 8. The higher the number, the more
// additional error-correction codes are added.
func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
return EncodeWithDepth(data, securityLevel, 16)
return EncodeWithColor(data, securityLevel, barcode.ColorScheme16)
}
func encodeData(dataWords []int, columns int, sl securitylevel) ([]int, error) {

View File

@ -12,7 +12,7 @@ type pdfBarcode struct {
data string
width int
code *utils.BitList
depth int
color barcode.ColorScheme
}
func (c *pdfBarcode) Metadata() barcode.Metadata {
@ -24,7 +24,7 @@ func (c *pdfBarcode) Content() string {
}
func (c *pdfBarcode) ColorModel() color.Model {
return utils.ColorModel(c.depth)
return c.color.Model
}
func (c *pdfBarcode) Bounds() image.Rectangle {
@ -35,7 +35,7 @@ func (c *pdfBarcode) Bounds() image.Rectangle {
func (c *pdfBarcode) At(x, y int) color.Color {
if c.code.GetBit((y/moduleHeight)*c.width + x) {
return utils.BlackColor(c.depth)
return c.color.Foreground
}
return utils.WhiteColor(c.depth)
return c.color.Background
}

View File

@ -55,7 +55,7 @@ func (e Encoding) String() string {
}
// Encode returns a QR barcode with the given content, error correction level and uses the given encoding
func EncodeWithDepth(content string, level ErrorCorrectionLevel, mode Encoding, depth int) (barcode.Barcode, error) {
func EncodeWithColor(content string, level ErrorCorrectionLevel, mode Encoding, color barcode.ColorScheme) (barcode.Barcode, error) {
bits, vi, err := mode.getEncoder()(content, level)
if err != nil {
return nil, err
@ -63,23 +63,23 @@ func EncodeWithDepth(content string, level ErrorCorrectionLevel, mode Encoding,
blocks := splitToBlocks(bits.IterateBytes(), vi)
data := blocks.interleave(vi)
result := render(data, vi, depth)
result := render(data, vi, color)
result.content = content
return result, nil
}
func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) {
return EncodeWithDepth(content, level, mode, 16)
return EncodeWithColor(content, level, mode, barcode.ColorScheme16)
}
func render(data []byte, vi *versionInfo, depth int) *qrcode {
func render(data []byte, vi *versionInfo, color barcode.ColorScheme) *qrcode {
dim := vi.modulWidth()
results := make([]*qrcode, 8)
for i := 0; i < 8; i++ {
results[i] = newBarCodeWithDepth(dim, depth)
results[i] = newBarCodeWithColor(dim, color)
}
occupied := newBarCodeWithDepth(dim, depth)
occupied := newBarCodeWithColor(dim, color)
setAll := func(x int, y int, val bool) {
occupied.Set(x, y, true)

View File

@ -13,7 +13,7 @@ type qrcode struct {
dimension int
data *utils.BitList
content string
depth int
color barcode.ColorScheme
}
func (qr *qrcode) Content() string {
@ -25,7 +25,7 @@ func (qr *qrcode) Metadata() barcode.Metadata {
}
func (qr *qrcode) ColorModel() color.Model {
return utils.ColorModel(qr.depth)
return qr.color.Model
}
func (qr *qrcode) Bounds() image.Rectangle {
@ -34,9 +34,9 @@ func (qr *qrcode) Bounds() image.Rectangle {
func (qr *qrcode) At(x, y int) color.Color {
if qr.Get(x, y) {
return utils.BlackColor(qr.depth)
return qr.color.Foreground
}
return utils.WhiteColor(qr.depth)
return qr.color.Background
}
func (qr *qrcode) Get(x, y int) bool {
@ -159,14 +159,14 @@ func (qr *qrcode) calcPenaltyRule4() uint {
return uint(math.Min(floor, ceil) * 10)
}
func newBarCodeWithDepth(dim int, depth int) *qrcode {
func newBarCodeWithColor(dim int, color barcode.ColorScheme) *qrcode {
res := new(qrcode)
res.dimension = dim
res.data = utils.NewBitList(dim * dim)
res.depth = depth
res.color = color
return res
}
func newBarcode(dim int) *qrcode {
return newBarCodeWithDepth(dim, 16)
return newBarCodeWithColor(dim, barcode.ColorScheme16)
}

View File

@ -78,8 +78,8 @@ func AddCheckSum(content string) (string, error) {
return content + string(utils.IntToRune(sum%10)), nil
}
// Encode creates a codabar barcode for the given content and depth
func EncodeWithDepth(content string, interleaved bool, depth int) (barcode.Barcode, error) {
// Encode creates a codabar barcode for the given content and color scheme
func EncodeWithColor(content string, interleaved bool, color barcode.ColorScheme) (barcode.Barcode, error) {
if content == "" {
return nil, errors.New("content is empty")
}
@ -131,13 +131,13 @@ func EncodeWithDepth(content string, interleaved bool, depth int) (barcode.Barco
resBits.AddBit(mode.end...)
if interleaved {
return utils.New1DCodeWithDepth(barcode.Type2of5Interleaved, content, resBits, depth), nil
return utils.New1DCodeWithColor(barcode.Type2of5Interleaved, content, resBits, color), nil
} else {
return utils.New1DCodeWithDepth(barcode.Type2of5, content, resBits, depth), nil
return utils.New1DCodeWithColor(barcode.Type2of5, content, resBits, color), nil
}
}
// Encode creates a codabar barcode for the given content
func Encode(content string, interleaved bool) (barcode.Barcode, error) {
return EncodeWithDepth(content, interleaved, 16)
return EncodeWithColor(content, interleaved, barcode.ColorScheme16)
}

View File

@ -12,7 +12,7 @@ type base1DCode struct {
*BitList
kind string
content string
depth int
color barcode.ColorScheme
}
type base1DCodeIntCS struct {
@ -29,7 +29,7 @@ func (c *base1DCode) Metadata() barcode.Metadata {
}
func (c *base1DCode) ColorModel() color.Model {
return ColorModel(c.depth)
return c.color.Model
}
func (c *base1DCode) Bounds() image.Rectangle {
@ -38,9 +38,9 @@ func (c *base1DCode) Bounds() image.Rectangle {
func (c *base1DCode) At(x, y int) color.Color {
if c.GetBit(x) {
return BlackColor(c.depth)
return c.color.Foreground
}
return WhiteColor(c.depth)
return c.color.Background
}
func (c *base1DCodeIntCS) CheckSum() int {
@ -49,20 +49,20 @@ func (c *base1DCodeIntCS) CheckSum() int {
// New1DCodeIntCheckSum creates a new 1D barcode where the bars are represented by the bits in the bars BitList
func New1DCodeIntCheckSum(codeKind, content string, bars *BitList, checksum int) barcode.BarcodeIntCS {
return &base1DCodeIntCS{base1DCode{bars, codeKind, content, 16}, checksum}
return &base1DCodeIntCS{base1DCode{bars, codeKind, content, barcode.ColorScheme16}, checksum}
}
// New1DCodeIntCheckSum creates a new 1D barcode where the bars are represented by the bits in the bars BitList
func New1DCodeIntCheckSumWithDepth(codeKind, content string, bars *BitList, checksum int, depth int) barcode.BarcodeIntCS {
return &base1DCodeIntCS{base1DCode{bars, codeKind, content, depth}, checksum}
func New1DCodeIntCheckSumWithColor(codeKind, content string, bars *BitList, checksum int, color barcode.ColorScheme) barcode.BarcodeIntCS {
return &base1DCodeIntCS{base1DCode{bars, codeKind, content, color}, 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, 16}
return &base1DCode{bars, codeKind, content, barcode.ColorScheme16}
}
// New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList
func New1DCodeWithDepth(codeKind, content string, bars *BitList, depth int) barcode.Barcode {
return &base1DCode{bars, codeKind, content, depth}
func New1DCodeWithColor(codeKind, content string, bars *BitList, color barcode.ColorScheme) barcode.Barcode {
return &base1DCode{bars, codeKind, content, color}
}