Encode creates a codabar barcode for the given content and color scheme
(content string, interleaved bool, color barcode.ColorScheme)
| 80 | |
| 81 | // Encode creates a codabar barcode for the given content and color scheme |
| 82 | func EncodeWithColor(content string, interleaved bool, color barcode.ColorScheme) (barcode.Barcode, error) { |
| 83 | if content == "" { |
| 84 | return nil, errors.New("content is empty") |
| 85 | } |
| 86 | |
| 87 | if interleaved && len(content)%2 == 1 { |
| 88 | return nil, errors.New("can only encode even number of digits in interleaved mode") |
| 89 | } |
| 90 | |
| 91 | mode := modes[interleaved] |
| 92 | resBits := new(utils.BitList) |
| 93 | resBits.AddBit(mode.start...) |
| 94 | |
| 95 | var lastRune *rune |
| 96 | for _, r := range content { |
| 97 | var a, b pattern |
| 98 | if interleaved { |
| 99 | if lastRune == nil { |
| 100 | lastRune = new(rune) |
| 101 | *lastRune = r |
| 102 | continue |
| 103 | } else { |
| 104 | var o1, o2 bool |
| 105 | a, o1 = encodingTable[*lastRune] |
| 106 | b, o2 = encodingTable[r] |
| 107 | if !o1 || !o2 { |
| 108 | return nil, fmt.Errorf("can not encode \"%s\"", content) |
| 109 | } |
| 110 | lastRune = nil |
| 111 | } |
| 112 | } else { |
| 113 | var ok bool |
| 114 | a, ok = encodingTable[r] |
| 115 | if !ok { |
| 116 | return nil, fmt.Errorf("can not encode \"%s\"", content) |
| 117 | } |
| 118 | b = nonInterleavedSpace |
| 119 | } |
| 120 | |
| 121 | for i := 0; i < patternWidth; i++ { |
| 122 | for x := 0; x < mode.widths[a[i]]; x++ { |
| 123 | resBits.AddBit(true) |
| 124 | } |
| 125 | for x := 0; x < mode.widths[b[i]]; x++ { |
| 126 | resBits.AddBit(false) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | resBits.AddBit(mode.end...) |
| 132 | |
| 133 | if interleaved { |
| 134 | return utils.New1DCodeWithColor(barcode.Type2of5Interleaved, content, resBits, color), nil |
| 135 | } else { |
| 136 | return utils.New1DCodeWithColor(barcode.Type2of5, content, resBits, color), nil |
| 137 | } |
| 138 | } |
| 139 |
no test coverage detected