git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/barcode/aztec/token.go (about) 1 package aztec 2 3 import ( 4 "fmt" 5 6 "git.sr.ht/~pingoo/stdx/barcode/utils" 7 ) 8 9 type token interface { 10 fmt.Stringer 11 prev() token 12 appendTo(bits *utils.BitList, text []byte) 13 } 14 15 type simpleToken struct { 16 token 17 value int 18 bitCount byte 19 } 20 21 type binaryShiftToken struct { 22 token 23 bShiftStart int 24 bShiftByteCnt int 25 } 26 27 func newSimpleToken(prev token, value int, bitCount byte) token { 28 return &simpleToken{prev, value, bitCount} 29 } 30 func newShiftToken(prev token, bShiftStart int, bShiftCnt int) token { 31 return &binaryShiftToken{prev, bShiftStart, bShiftCnt} 32 } 33 34 func (st *simpleToken) prev() token { 35 return st.token 36 } 37 func (st *simpleToken) appendTo(bits *utils.BitList, text []byte) { 38 bits.AddBits(st.value, st.bitCount) 39 } 40 func (st *simpleToken) String() string { 41 value := st.value & ((1 << st.bitCount) - 1) 42 value |= 1 << st.bitCount 43 return "<" + fmt.Sprintf("%b", value)[1:] + ">" 44 } 45 46 func (bst *binaryShiftToken) prev() token { 47 return bst.token 48 } 49 func (bst *binaryShiftToken) appendTo(bits *utils.BitList, text []byte) { 50 for i := 0; i < bst.bShiftByteCnt; i++ { 51 if i == 0 || (i == 31 && bst.bShiftByteCnt <= 62) { 52 // We need a header before the first character, and before 53 // character 31 when the total byte code is <= 62 54 bits.AddBits(31, 5) // BINARY_SHIFT 55 if bst.bShiftByteCnt > 62 { 56 bits.AddBits(bst.bShiftByteCnt-31, 16) 57 } else if i == 0 { 58 // 1 <= binaryShiftByteCode <= 62 59 if bst.bShiftByteCnt < 31 { 60 bits.AddBits(bst.bShiftByteCnt, 5) 61 } else { 62 bits.AddBits(31, 5) 63 } 64 } else { 65 // 32 <= binaryShiftCount <= 62 and i == 31 66 bits.AddBits(bst.bShiftByteCnt-31, 5) 67 } 68 } 69 bits.AddByte(text[bst.bShiftStart+i]) 70 } 71 } 72 73 func (bst *binaryShiftToken) String() string { 74 return fmt.Sprintf("<%d::%d>", bst.bShiftStart, (bst.bShiftStart + bst.bShiftByteCnt - 1)) 75 }