package escpos import ( "fmt" "strings" ) // QRCorrection is the QR code error correction level. Higher levels survive // more damage at the cost of a denser code. type QRCorrection byte // QRCorrection values for GS ( k function 169. const ( QRCorrectionL QRCorrection = 48 // about 7% recovery QRCorrectionM QRCorrection = 49 // about 15% recovery QRCorrectionQ QRCorrection = 50 // about 25% recovery QRCorrectionH QRCorrection = 51 // about 30% recovery ) // QR code limits. const ( MaxQRData = 7089 // bytes the printer's symbol storage accepts MinQRModuleSize = 1 MaxQRModuleSize = 16 ) // qrByteCapacity holds, for each QR version from 1 to 40, how many bytes a // code of that version holds in byte mode at correction levels L, M, Q and // H (ISO/IEC 18004). var qrByteCapacity = [40][4]int{ {17, 14, 11, 7}, {32, 26, 20, 14}, {53, 42, 32, 24}, {78, 62, 46, 34}, {106, 84, 60, 44}, {134, 106, 74, 58}, {154, 122, 86, 64}, {192, 152, 108, 84}, {230, 180, 130, 98}, {271, 213, 151, 119}, {321, 251, 177, 137}, {367, 287, 203, 155}, {425, 331, 241, 177}, {458, 362, 258, 194}, {520, 412, 292, 220}, {586, 450, 322, 250}, {644, 504, 364, 280}, {718, 560, 394, 310}, {792, 624, 442, 338}, {858, 666, 482, 382}, {929, 711, 509, 403}, {1003, 779, 565, 439}, {1091, 857, 611, 461}, {1171, 911, 661, 511}, {1273, 997, 715, 535}, {1367, 1059, 751, 593}, {1465, 1125, 805, 625}, {1528, 1190, 868, 658}, {1628, 1264, 908, 698}, {1732, 1370, 982, 742}, {1840, 1452, 1030, 790}, {1952, 1538, 1112, 842}, {2068, 1628, 1168, 898}, {2188, 1722, 1228, 958}, {2303, 1809, 1283, 983}, {2431, 1911, 1351, 1051}, {2563, 1989, 1423, 1093}, {2699, 2099, 1499, 1139}, {2809, 2213, 1579, 1219}, {2953, 2331, 1663, 1273}, } // QRModules returns how many modules wide a QR code holding n bytes at // level is at most, or 0 for a level that is not one. It assumes byte mode, // the least dense, so the printer's own code is never wider; data that // only a denser mode holds, such as thousands of digits, counts as the // largest version, 177 modules. func QRModules(n int, level QRCorrection) int { if level < QRCorrectionL || level > QRCorrectionH { return 0 } column := int(level - QRCorrectionL) for v, capacity := range qrByteCapacity { if n <= capacity[column] { return 17 + 4*(v+1) } } return 17 + 4*len(qrByteCapacity) } // ParseQRCorrection returns the correction level named by name: L, M, Q or H. func ParseQRCorrection(name string) (QRCorrection, error) { switch strings.ToUpper(strings.TrimSpace(name)) { case "L": return QRCorrectionL, nil case "M": return QRCorrectionM, nil case "Q": return QRCorrectionQ, nil case "H": return QRCorrectionH, nil } return 0, fmt.Errorf("%w: unknown QR correction level %q (want L, M, Q or H)", ErrInvalidArgument, name) } // QRCode appends a model 2 QR code holding data, printed with square modules // moduleSize dots wide. func (b *Builder) QRCode(data []byte, moduleSize int, level QRCorrection) error { switch { case len(data) == 0: return fmt.Errorf("%w: QR data is empty", ErrInvalidArgument) case len(data) > MaxQRData: return fmt.Errorf("%w: QR data is %d bytes, limit is %d", ErrInvalidArgument, len(data), MaxQRData) case moduleSize < MinQRModuleSize || moduleSize > MaxQRModuleSize: return fmt.Errorf("%w: QR module size %d must be within %d-%d", ErrInvalidArgument, moduleSize, MinQRModuleSize, MaxQRModuleSize) case level < QRCorrectionL || level > QRCorrectionH: return fmt.Errorf("%w: unknown QR correction level %d", ErrInvalidArgument, byte(level)) } // Function 165: select model 2. b.buf.Write([]byte{gs, '(', 'k', 4, 0, '1', 'A', '2', 0}) // Function 167: module size. b.buf.Write([]byte{gs, '(', 'k', 3, 0, '1', 'C', byte(moduleSize)}) // Function 169: error correction level. b.buf.Write([]byte{gs, '(', 'k', 3, 0, '1', 'E', byte(level)}) // Function 180: store data. The length covers cn, fn, m and the data. n := len(data) + 3 b.buf.Write([]byte{gs, '(', 'k', byte(n), byte(n >> 8), '1', 'P', '0'}) b.buf.Write(data) // Function 181: print the stored symbol. b.buf.Write([]byte{gs, '(', 'k', 3, 0, '1', 'Q', '0'}) return nil }