encode.go 2.6 KB

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