From 29d3fcfa989cfa55a371faf6c929953e62b3075e Mon Sep 17 00:00:00 2001 From: boombuler Date: Wed, 11 Dec 2013 20:26:35 +0100 Subject: [PATCH] added unicode support for qr codes and some minor fixes --- qr/alphanumeric.go | 37 +++++++++++++++++++++++++------------ qr/automatic.go | 5 ++++- qr/encoder.go | 6 ++++++ qr/utf8.go | 26 ++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 qr/utf8.go diff --git a/qr/alphanumeric.go b/qr/alphanumeric.go index 749a0fa..7ce5b4d 100644 --- a/qr/alphanumeric.go +++ b/qr/alphanumeric.go @@ -4,14 +4,25 @@ import ( "errors" "fmt" "github.com/boombuler/barcode" + "strings" ) -var alphaNumericTable map[byte]int = map[byte]int{ - '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, - 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, - 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, - 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35, ' ': 36, '$': 37, '%': 38, '*': 39, - '+': 40, '-': 41, '.': 42, '/': 43, ':': 44, +const charSet string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:" + +func stringToAlphaIdx(content string) <-chan int { + result := make(chan int) + go func() { + for _, r := range content { + idx := strings.IndexRune(charSet, r) + result <- idx + if idx < 0 { + break + } + } + close(result) + }() + + return result } func encodeAlphaNumeric(content string, ecl ErrorCorrectionLevel) (*barcode.BitList, *versionInfo, error) { @@ -30,20 +41,22 @@ func encodeAlphaNumeric(content string, ecl ErrorCorrectionLevel) (*barcode.BitL res.AddBits(int(alphaNumericMode), 4) res.AddBits(len(content), vi.charCountBits(alphaNumericMode)) + encoder := stringToAlphaIdx(content) + for idx := 0; idx < len(content)/2; idx++ { - c1, ok1 := alphaNumericTable[content[idx*2]] - c2, ok2 := alphaNumericTable[content[(idx*2)+1]] - if !ok1 || !ok2 { + c1 := <-encoder + c2 := <-encoder + if c1 < 0 || c2 < 0 { return nil, nil, fmt.Errorf("\"%s\" can not be encoded as %s", content, AlphaNumeric) } res.AddBits(c1*45+c2, 11) } if contentLenIsOdd { - c1, ok := alphaNumericTable[content[len(content)-1]] - if !ok { + c := <-encoder + if c < 0 { return nil, nil, fmt.Errorf("\"%s\" can not be encoded as %s", content, AlphaNumeric) } - res.AddBits(c1, 6) + res.AddBits(c, 6) } addPaddingAndTerminator(res, vi) diff --git a/qr/automatic.go b/qr/automatic.go index c82bd38..e1be37a 100644 --- a/qr/automatic.go +++ b/qr/automatic.go @@ -14,6 +14,9 @@ func encodeAuto(content string, ecl ErrorCorrectionLevel) (*barcode.BitList, *ve if bits != nil && vi != nil { return bits, vi, nil } - + bits, vi, _ = Unicode.getEncoder()(content, ecl) + if bits != nil && vi != nil { + return bits, vi, nil + } return nil, nil, fmt.Errorf("No encoding found to encode \"%s\"", content) } diff --git a/qr/encoder.go b/qr/encoder.go index 4b7d4ab..da18951 100644 --- a/qr/encoder.go +++ b/qr/encoder.go @@ -18,6 +18,8 @@ const ( Numeric // Encode only uppercase letters, numbers and [Space], $, %, *, +, -, ., /, : AlphaNumeric + // Encodes string as utf-8 + Unicode ) func (e Encoding) getEncoder() encodeFn { @@ -28,6 +30,8 @@ func (e Encoding) getEncoder() encodeFn { return encodeNumeric case AlphaNumeric: return encodeAlphaNumeric + case Unicode: + return encodeUnicode } return nil } @@ -40,6 +44,8 @@ func (e Encoding) String() string { return "Numeric" case AlphaNumeric: return "AlphaNumeric" + case Unicode: + return "Unicode" } return "" } diff --git a/qr/utf8.go b/qr/utf8.go new file mode 100644 index 0000000..eb4ad71 --- /dev/null +++ b/qr/utf8.go @@ -0,0 +1,26 @@ +package qr + +import ( + "errors" + "github.com/boombuler/barcode" +) + +func encodeUnicode(content string, ecl ErrorCorrectionLevel) (*barcode.BitList, *versionInfo, error) { + data := []byte(content) + + vi := findSmallestVersionInfo(ecl, byteMode, len(data)*8) + if vi == nil { + return nil, nil, errors.New("To much data to encode") + } + + // It's not correct to add the unicode bytes to the result directly but most readers can't handle the + // required ECI header... + res := new(barcode.BitList) + res.AddBits(int(byteMode), 4) + res.AddBits(len(content), vi.charCountBits(byteMode)) + for _, b := range data { + res.AddByte(b) + } + addPaddingAndTerminator(res, vi) + return res, vi, nil +}