package escpos import ( "fmt" "strings" ) // Symbology identifies a one-dimensional barcode type. Values are the GS k // function B type codes. type Symbology byte // Supported barcode symbologies. const ( UPCA Symbology = 65 UPCE Symbology = 66 EAN13 Symbology = 67 EAN8 Symbology = 68 CODE39 Symbology = 69 CODE128 Symbology = 73 ) // HRIPosition places the human-readable interpretation (the digits) relative // to a barcode. type HRIPosition byte // HRIPosition values for GS H. const ( HRINone HRIPosition = 0 HRIAbove HRIPosition = 1 HRIBelow HRIPosition = 2 HRIBoth HRIPosition = 3 ) // Barcode module width limits for GS w. const ( MinBarcodeWidth = 2 MaxBarcodeWidth = 6 ) const code39Charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -.$/+%" // ParseSymbology returns the symbology named by name, e.g. "ean13". func ParseSymbology(name string) (Symbology, error) { switch strings.ToLower(strings.TrimSpace(name)) { case "upca", "upc-a": return UPCA, nil case "upce", "upc-e": return UPCE, nil case "ean13", "ean-13", "jan13": return EAN13, nil case "ean8", "ean-8", "jan8": return EAN8, nil case "code39", "code-39": return CODE39, nil case "code128", "code-128": return CODE128, nil } return 0, fmt.Errorf("%w: unknown barcode symbology %q", ErrInvalidArgument, name) } // String returns the canonical symbology name. func (s Symbology) String() string { switch s { case UPCA: return "upca" case UPCE: return "upce" case EAN13: return "ean13" case EAN8: return "ean8" case CODE39: return "code39" case CODE128: return "code128" } return fmt.Sprintf("Symbology(%d)", byte(s)) } // ValidateBarcode reports whether data can be encoded as sym. Where data // includes the check digit, the check digit is verified. func ValidateBarcode(sym Symbology, data string) error { invalid := func(format string, args ...any) error { return fmt.Errorf("%w: %s: "+format, append([]any{ErrInvalidArgument, sym}, args...)...) } switch sym { case UPCA: if !isDigits(data) || (len(data) != 11 && len(data) != 12) { return invalid("want 11 or 12 digits") } if len(data) == 12 && !validCheckDigit(data) { return invalid("check digit is wrong") } case UPCE: switch { case !isDigits(data): return invalid("want digits only") case len(data) >= 6 && len(data) <= 8: case (len(data) == 11 || len(data) == 12) && data[0] == '0': default: return invalid("want 6-8 digits, or 11-12 digits starting with 0") } case EAN13: if !isDigits(data) || (len(data) != 12 && len(data) != 13) { return invalid("want 12 or 13 digits") } if len(data) == 13 && !validCheckDigit(data) { return invalid("check digit is wrong") } case EAN8: if !isDigits(data) || (len(data) != 7 && len(data) != 8) { return invalid("want 7 or 8 digits") } if len(data) == 8 && !validCheckDigit(data) { return invalid("check digit is wrong") } case CODE39: if len(data) < 1 || len(data) > 255 { return invalid("want 1-255 characters") } for _, r := range data { if !strings.ContainsRune(code39Charset, r) { return invalid("character %q not allowed (use 0-9, A-Z, space and -.$/+%%)", r) } } case CODE128: if len(data) < 1 { return invalid("data is empty") } for _, r := range data { if r < 0x20 || r > 0x7E { return invalid("character %q not allowed (printable ASCII only)", r) } } if len(code128Payload(data)) > 255 { return invalid("data is too long") } default: return fmt.Errorf("%w: unknown barcode symbology %d", ErrInvalidArgument, byte(sym)) } return nil } // BarcodeHeight sets the barcode height in dots (GS h n). func (b *Builder) BarcodeHeight(dots uint8) { b.buf.Write([]byte{gs, 'h', dots}) } // BarcodeWidth sets the barcode module width, [MinBarcodeWidth] to // [MaxBarcodeWidth] (GS w n). func (b *Builder) BarcodeWidth(module int) error { if module < MinBarcodeWidth || module > MaxBarcodeWidth { return fmt.Errorf("%w: barcode width %d must be within %d-%d", ErrInvalidArgument, module, MinBarcodeWidth, MaxBarcodeWidth) } b.buf.Write([]byte{gs, 'w', byte(module)}) return nil } // BarcodeHRI sets where the human-readable digits are printed (GS H n). func (b *Builder) BarcodeHRI(pos HRIPosition) { b.buf.Write([]byte{gs, 'H', byte(pos)}) } // Barcode validates data and appends a barcode (GS k m n d1...dn). CODE128 // data is printed in code set B, so callers pass plain text. func (b *Builder) Barcode(sym Symbology, data string) error { if err := ValidateBarcode(sym, data); err != nil { return err } payload := []byte(data) if sym == CODE128 { payload = code128Payload(data) } b.buf.Write([]byte{gs, 'k', byte(sym), byte(len(payload))}) b.buf.Write(payload) return nil } // code128Payload selects code set B and escapes '{', which the printer // treats as the start of a code set or function character. func code128Payload(data string) []byte { return []byte("{B" + strings.ReplaceAll(data, "{", "{{")) } func isDigits(s string) bool { if s == "" { return false } for i := range len(s) { if s[i] < '0' || s[i] > '9' { return false } } return true } // validCheckDigit verifies the GS1 mod-10 check digit in the last position of // a UPC-A, EAN-13 or EAN-8 number. func validCheckDigit(code string) bool { sum := 0 body := code[:len(code)-1] for i := range len(body) { digit := int(body[len(body)-1-i] - '0') if i%2 == 0 { digit *= 3 } sum += digit } return (10-sum%10)%10 == int(code[len(code)-1]-'0') }