encode.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 nextRunes[i] < '0' || nextRunes[i] > '9' {
  29. return false
  30. }
  31. }
  32. return true
  33. }
  34. func getCodeIndexList(content []rune) *utils.BitList {
  35. result := new(utils.BitList)
  36. curEncoding := byte(0)
  37. for i := 0; i < len(content); i++ {
  38. if shouldUseCTable(content[i:], curEncoding) {
  39. if curEncoding != startCSymbol {
  40. if curEncoding == byte(0) {
  41. result.AddByte(startCSymbol)
  42. } else {
  43. result.AddByte(codeCSymbol)
  44. }
  45. curEncoding = startCSymbol
  46. }
  47. idx := (content[i] - '0') * 10
  48. i++
  49. idx = idx + (content[i] - '0')
  50. result.AddByte(byte(idx))
  51. } else {
  52. if curEncoding != startBSymbol {
  53. if curEncoding == byte(0) {
  54. result.AddByte(startBSymbol)
  55. } else {
  56. result.AddByte(codeBSymbol)
  57. }
  58. curEncoding = startBSymbol
  59. }
  60. var idx int
  61. switch content[i] {
  62. case FNC1:
  63. idx = 102
  64. break
  65. case FNC2:
  66. idx = 97
  67. break
  68. case FNC3:
  69. idx = 96
  70. break
  71. case FNC4:
  72. idx = 100
  73. break
  74. default:
  75. idx = strings.IndexRune(bTable, content[i])
  76. break
  77. }
  78. if idx < 0 {
  79. return nil
  80. }
  81. result.AddByte(byte(idx))
  82. }
  83. }
  84. return result
  85. }
  86. // Encode creates a Code 128 barcode for the given content
  87. func Encode(content string) (barcode.Barcode, error) {
  88. contentRunes := strToRunes(content)
  89. if len(contentRunes) <= 0 || len(contentRunes) > 80 {
  90. return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
  91. }
  92. idxList := getCodeIndexList(contentRunes)
  93. if idxList == nil {
  94. return nil, fmt.Errorf("\"%s\" could not be encoded", content)
  95. }
  96. result := new(utils.BitList)
  97. sum := 0
  98. for i, idx := range idxList.GetBytes() {
  99. if i == 0 {
  100. sum = int(idx)
  101. } else {
  102. sum += i * int(idx)
  103. }
  104. result.AddBit(encodingTable[idx]...)
  105. }
  106. result.AddBit(encodingTable[sum%103]...)
  107. result.AddBit(encodingTable[stopSymbol]...)
  108. return utils.New1DCode("Code 128", content, result, sum%103), nil
  109. }