Encode creates a Code 128 barcode for the given content and color scheme
(content string, color barcode.ColorScheme)
| 157 | |
| 158 | // Encode creates a Code 128 barcode for the given content and color scheme |
| 159 | func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) { |
| 160 | contentRunes := strToRunes(content) |
| 161 | if len(contentRunes) <= 0 || len(contentRunes) > 80 { |
| 162 | return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes)) |
| 163 | } |
| 164 | idxList := getCodeIndexList(contentRunes) |
| 165 | |
| 166 | if idxList == nil { |
| 167 | return nil, fmt.Errorf("\"%s\" could not be encoded", content) |
| 168 | } |
| 169 | |
| 170 | result := new(utils.BitList) |
| 171 | sum := 0 |
| 172 | for i, idx := range idxList.GetBytes() { |
| 173 | if i == 0 { |
| 174 | sum = int(idx) |
| 175 | } else { |
| 176 | sum += i * int(idx) |
| 177 | } |
| 178 | result.AddBit(encodingTable[idx]...) |
| 179 | } |
| 180 | sum = sum % 103 |
| 181 | result.AddBit(encodingTable[sum]...) |
| 182 | result.AddBit(encodingTable[stopSymbol]...) |
| 183 | return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode128, content, result, sum, color), nil |
| 184 | } |
| 185 | |
| 186 | // Encode creates a Code 128 barcode for the given content |
| 187 | func Encode(content string) (barcode.BarcodeIntCS, error) { |
no test coverage detected