encode.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Package code128 can create Code128 barcodes
  2. package code128
  3. import (
  4. "fmt"
  5. "strings"
  6. "unicode/utf8"
  7. "github.com/boombuler/barcode"
  8. "github.com/boombuler/barcode/utils"
  9. )
  10. func strToRunes(str string) []rune {
  11. result := make([]rune, utf8.RuneCountInString(str))
  12. i := 0
  13. for _, r := range str {
  14. result[i] = r
  15. i++
  16. }
  17. return result
  18. }
  19. func shouldUseCTable(nextRunes []rune, curEncoding byte) bool {
  20. requiredDigits := 4
  21. if curEncoding == startCSymbol {
  22. requiredDigits = 2
  23. }
  24. if len(nextRunes) < requiredDigits {
  25. return false
  26. }
  27. for i := 0; i < requiredDigits; i++ {
  28. if i%2 == 0 && nextRunes[i] == FNC1 {
  29. requiredDigits++
  30. if len(nextRunes) < requiredDigits {
  31. return false
  32. }
  33. continue
  34. }
  35. if nextRunes[i] < '0' || nextRunes[i] > '9' {
  36. return false
  37. }
  38. }
  39. return true
  40. }
  41. func tableContainsRune(table string, r rune) bool {
  42. return strings.ContainsRune(table, r) || r == FNC1 || r == FNC2 || r == FNC3 || r == FNC4
  43. }
  44. func shouldUseATable(nextRunes []rune, curEncoding byte) bool {
  45. nextRune := nextRunes[0]
  46. if !tableContainsRune(bTable, nextRune) || curEncoding == startASymbol {
  47. return tableContainsRune(aTable, nextRune)
  48. }
  49. if curEncoding == 0 {
  50. for _, r := range nextRunes {
  51. if tableContainsRune(abTable, r) {
  52. continue
  53. }
  54. if strings.ContainsRune(aOnlyTable, r) {
  55. return true
  56. }
  57. break
  58. }
  59. }
  60. return false
  61. }
  62. func getCodeIndexList(content []rune) *utils.BitList {
  63. result := new(utils.BitList)
  64. curEncoding := byte(0)
  65. for i := 0; i < len(content); i++ {
  66. if shouldUseCTable(content[i:], curEncoding) {
  67. if curEncoding != startCSymbol {
  68. if curEncoding == byte(0) {
  69. result.AddByte(startCSymbol)
  70. } else {
  71. result.AddByte(codeCSymbol)
  72. }
  73. curEncoding = startCSymbol
  74. }
  75. if content[i] == FNC1 {
  76. result.AddByte(102)
  77. } else {
  78. idx := (content[i] - '0') * 10
  79. i++
  80. idx = idx + (content[i] - '0')
  81. result.AddByte(byte(idx))
  82. }
  83. } else if shouldUseATable(content[i:], curEncoding) {
  84. if curEncoding != startASymbol {
  85. if curEncoding == byte(0) {
  86. result.AddByte(startASymbol)
  87. } else {
  88. result.AddByte(codeASymbol)
  89. }
  90. curEncoding = startASymbol
  91. }
  92. var idx int
  93. switch content[i] {
  94. case FNC1:
  95. idx = 102
  96. break
  97. case FNC2:
  98. idx = 97
  99. break
  100. case FNC3:
  101. idx = 96
  102. break
  103. case FNC4:
  104. idx = 101
  105. break
  106. default:
  107. idx = strings.IndexRune(aTable, content[i])
  108. break
  109. }
  110. if idx < 0 {
  111. return nil
  112. }
  113. result.AddByte(byte(idx))
  114. } else {
  115. if curEncoding != startBSymbol {
  116. if curEncoding == byte(0) {
  117. result.AddByte(startBSymbol)
  118. } else {
  119. result.AddByte(codeBSymbol)
  120. }
  121. curEncoding = startBSymbol
  122. }
  123. var idx int
  124. switch content[i] {
  125. case FNC1:
  126. idx = 102
  127. break
  128. case FNC2:
  129. idx = 97
  130. break
  131. case FNC3:
  132. idx = 96
  133. break
  134. case FNC4:
  135. idx = 100
  136. break
  137. default:
  138. idx = strings.IndexRune(bTable, content[i])
  139. break
  140. }
  141. if idx < 0 {
  142. return nil
  143. }
  144. result.AddByte(byte(idx))
  145. }
  146. }
  147. return result
  148. }
  149. // Encode creates a Code 128 barcode for the given content
  150. func Encode(content string) (barcode.BarcodeIntCS, error) {
  151. contentRunes := strToRunes(content)
  152. if len(contentRunes) <= 0 || len(contentRunes) > 80 {
  153. return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
  154. }
  155. idxList := getCodeIndexList(contentRunes)
  156. if idxList == nil {
  157. return nil, fmt.Errorf("\"%s\" could not be encoded", content)
  158. }
  159. result := new(utils.BitList)
  160. sum := 0
  161. for i, idx := range idxList.GetBytes() {
  162. if i == 0 {
  163. sum = int(idx)
  164. } else {
  165. sum += i * int(idx)
  166. }
  167. result.AddBit(encodingTable[idx]...)
  168. }
  169. sum = sum % 103
  170. result.AddBit(encodingTable[sum]...)
  171. result.AddBit(encodingTable[stopSymbol]...)
  172. return utils.New1DCodeIntCheckSum(barcode.TypeCode128, content, result, sum), nil
  173. }
  174. func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {
  175. contentRunes := strToRunes(content)
  176. if len(contentRunes) <= 0 || len(contentRunes) > 80 {
  177. return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
  178. }
  179. idxList := getCodeIndexList(contentRunes)
  180. if idxList == nil {
  181. return nil, fmt.Errorf("\"%s\" could not be encoded", content)
  182. }
  183. result := new(utils.BitList)
  184. for _, idx := range idxList.GetBytes() {
  185. result.AddBit(encodingTable[idx]...)
  186. }
  187. result.AddBit(encodingTable[stopSymbol]...)
  188. return utils.New1DCode(barcode.TypeCode128, content, result), nil
  189. }