encode.go 2.7 KB

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