git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/barcode/aztec/azteccode.go (about)

     1  package aztec
     2  
     3  import (
     4  	"bytes"
     5  	"image"
     6  	"image/color"
     7  
     8  	"git.sr.ht/~pingoo/stdx/barcode"
     9  	"git.sr.ht/~pingoo/stdx/barcode/utils"
    10  )
    11  
    12  type aztecCode struct {
    13  	*utils.BitList
    14  	size    int
    15  	content []byte
    16  }
    17  
    18  func newAztecCode(size int) *aztecCode {
    19  	return &aztecCode{utils.NewBitList(size * size), size, nil}
    20  }
    21  
    22  func (c *aztecCode) Content() string {
    23  	return string(c.content)
    24  }
    25  
    26  func (c *aztecCode) Metadata() barcode.Metadata {
    27  	return barcode.Metadata{barcode.TypeAztec, 2}
    28  }
    29  
    30  func (c *aztecCode) ColorModel() color.Model {
    31  	return color.Gray16Model
    32  }
    33  
    34  func (c *aztecCode) Bounds() image.Rectangle {
    35  	return image.Rect(0, 0, c.size, c.size)
    36  }
    37  
    38  func (c *aztecCode) At(x, y int) color.Color {
    39  	if c.GetBit(x*c.size + y) {
    40  		return color.Black
    41  	}
    42  	return color.White
    43  }
    44  
    45  func (c *aztecCode) set(x, y int) {
    46  	c.SetBit(x*c.size+y, true)
    47  }
    48  
    49  func (c *aztecCode) string() string {
    50  	buf := new(bytes.Buffer)
    51  	for y := 0; y < c.size; y++ {
    52  		for x := 0; x < c.size; x++ {
    53  			if c.GetBit(x*c.size + y) {
    54  				buf.WriteString("X ")
    55  			} else {
    56  				buf.WriteString("  ")
    57  			}
    58  		}
    59  		buf.WriteRune('\n')
    60  	}
    61  	return buf.String()
    62  }