diff options
Diffstat (limited to 'vendor/github.com/skip2/go-qrcode/encoder.go')
-rw-r--r-- | vendor/github.com/skip2/go-qrcode/encoder.go | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/vendor/github.com/skip2/go-qrcode/encoder.go b/vendor/github.com/skip2/go-qrcode/encoder.go index 713378c0..6a809cfe 100644 --- a/vendor/github.com/skip2/go-qrcode/encoder.go +++ b/vendor/github.com/skip2/go-qrcode/encoder.go @@ -172,7 +172,7 @@ func (d *dataEncoder) encode(data []byte) (*bitset.Bitset, error) { } // Classify data into unoptimised segments. - d.classifyDataModes() + highestRequiredMode := d.classifyDataModes() // Optimise segments. err := d.optimiseDataModes() @@ -180,6 +180,25 @@ func (d *dataEncoder) encode(data []byte) (*bitset.Bitset, error) { return nil, err } + // Check if a single byte encoded segment would be more efficient. + optimizedLength := 0 + for _, s := range d.optimised { + length, err := d.encodedLength(s.dataMode, len(s.data)) + if err != nil { + return nil, err + } + optimizedLength += length + } + + singleByteSegmentLength, err := d.encodedLength(highestRequiredMode, len(d.data)) + if err != nil { + return nil, err + } + + if singleByteSegmentLength <= optimizedLength { + d.optimised = []segment{segment{dataMode: highestRequiredMode, data: d.data}} + } + // Encode data. encoded := bitset.New() for _, s := range d.optimised { @@ -192,9 +211,15 @@ func (d *dataEncoder) encode(data []byte) (*bitset.Bitset, error) { // classifyDataModes classifies the raw data into unoptimised segments. // e.g. "123ZZ#!#!" => // [numeric, 3, "123"] [alphanumeric, 2, "ZZ"] [byte, 4, "#!#!"]. -func (d *dataEncoder) classifyDataModes() { +// +// Returns the highest data mode needed to encode the data. e.g. for a mixed +// numeric/alphanumeric input, the highest is alphanumeric. +// +// dataModeNone < dataModeNumeric < dataModeAlphanumeric < dataModeByte +func (d *dataEncoder) classifyDataModes() dataMode { var start int mode := dataModeNone + highestRequiredMode := mode for i, v := range d.data { newMode := dataModeNone @@ -217,9 +242,15 @@ func (d *dataEncoder) classifyDataModes() { mode = newMode } + + if newMode > highestRequiredMode { + highestRequiredMode = newMode + } } d.actual = append(d.actual, segment{dataMode: mode, data: d.data[start:len(d.data)]}) + + return highestRequiredMode } // optimiseDataModes optimises the list of segments to reduce the overall output |