encoder.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Package code93 can create Code93 barcodes
  2. package code93
  3. import (
  4. "errors"
  5. "strings"
  6. "notabug.org/makenotabuggreatagain/barcode"
  7. "notabug.org/makenotabuggreatagain/barcode/utils"
  8. )
  9. type encodeInfo struct {
  10. value int
  11. data int
  12. }
  13. const (
  14. // Special Function 1 ($)
  15. FNC1 = '\u00f1'
  16. // Special Function 2 (%)
  17. FNC2 = '\u00f2'
  18. // Special Function 3 (/)
  19. FNC3 = '\u00f3'
  20. // Special Function 4 (+)
  21. FNC4 = '\u00f4'
  22. )
  23. var encodeTable = map[rune]encodeInfo{
  24. '0': encodeInfo{0, 0x114}, '1': encodeInfo{1, 0x148}, '2': encodeInfo{2, 0x144},
  25. '3': encodeInfo{3, 0x142}, '4': encodeInfo{4, 0x128}, '5': encodeInfo{5, 0x124},
  26. '6': encodeInfo{6, 0x122}, '7': encodeInfo{7, 0x150}, '8': encodeInfo{8, 0x112},
  27. '9': encodeInfo{9, 0x10A}, 'A': encodeInfo{10, 0x1A8}, 'B': encodeInfo{11, 0x1A4},
  28. 'C': encodeInfo{12, 0x1A2}, 'D': encodeInfo{13, 0x194}, 'E': encodeInfo{14, 0x192},
  29. 'F': encodeInfo{15, 0x18A}, 'G': encodeInfo{16, 0x168}, 'H': encodeInfo{17, 0x164},
  30. 'I': encodeInfo{18, 0x162}, 'J': encodeInfo{19, 0x134}, 'K': encodeInfo{20, 0x11A},
  31. 'L': encodeInfo{21, 0x158}, 'M': encodeInfo{22, 0x14C}, 'N': encodeInfo{23, 0x146},
  32. 'O': encodeInfo{24, 0x12C}, 'P': encodeInfo{25, 0x116}, 'Q': encodeInfo{26, 0x1B4},
  33. 'R': encodeInfo{27, 0x1B2}, 'S': encodeInfo{28, 0x1AC}, 'T': encodeInfo{29, 0x1A6},
  34. 'U': encodeInfo{30, 0x196}, 'V': encodeInfo{31, 0x19A}, 'W': encodeInfo{32, 0x16C},
  35. 'X': encodeInfo{33, 0x166}, 'Y': encodeInfo{34, 0x136}, 'Z': encodeInfo{35, 0x13A},
  36. '-': encodeInfo{36, 0x12E}, '.': encodeInfo{37, 0x1D4}, ' ': encodeInfo{38, 0x1D2},
  37. '$': encodeInfo{39, 0x1CA}, '/': encodeInfo{40, 0x16E}, '+': encodeInfo{41, 0x176},
  38. '%': encodeInfo{42, 0x1AE}, FNC1: encodeInfo{43, 0x126}, FNC2: encodeInfo{44, 0x1DA},
  39. FNC3: encodeInfo{45, 0x1D6}, FNC4: encodeInfo{46, 0x132}, '*': encodeInfo{47, 0x15E},
  40. }
  41. var extendedTable = []string{
  42. "\u00f2U", "\u00f1A", "\u00f1B", "\u00f1C", "\u00f1D", "\u00f1E", "\u00f1F", "\u00f1G",
  43. "\u00f1H", "\u00f1I", "\u00f1J", "\u00f1K", "\u00f1L", "\u00f1M", "\u00f1N", "\u00f1O",
  44. "\u00f1P", "\u00f1Q", "\u00f1R", "\u00f1S", "\u00f1T", "\u00f1U", "\u00f1V", "\u00f1W",
  45. "\u00f1X", "\u00f1Y", "\u00f1Z", "\u00f2A", "\u00f2B", "\u00f2C", "\u00f2D", "\u00f2E",
  46. " ", "\u00f3A", "\u00f3B", "\u00f3C", "\u00f3D", "\u00f3E", "\u00f3F", "\u00f3G",
  47. "\u00f3H", "\u00f3I", "\u00f3J", "\u00f3K", "\u00f3L", "-", ".", "\u00f3O",
  48. "0", "1", "2", "3", "4", "5", "6", "7",
  49. "8", "9", "\u00f3Z", "\u00f2F", "\u00f2G", "\u00f2H", "\u00f2I", "\u00f2J",
  50. "\u00f2V", "A", "B", "C", "D", "E", "F", "G",
  51. "H", "I", "J", "K", "L", "M", "N", "O",
  52. "P", "Q", "R", "S", "T", "U", "V", "W",
  53. "X", "Y", "Z", "\u00f2K", "\u00f2L", "\u00f2M", "\u00f2N", "\u00f2O",
  54. "\u00f2W", "\u00f4A", "\u00f4B", "\u00f4C", "\u00f4D", "\u00f4E", "\u00f4F", "\u00f4G",
  55. "\u00f4H", "\u00f4I", "\u00f4J", "\u00f4K", "\u00f4L", "\u00f4M", "\u00f4N", "\u00f4O",
  56. "\u00f4P", "\u00f4Q", "\u00f4R", "\u00f4S", "\u00f4T", "\u00f4U", "\u00f4V", "\u00f4W",
  57. "\u00f4X", "\u00f4Y", "\u00f4Z", "\u00f2P", "\u00f2Q", "\u00f2R", "\u00f2S", "\u00f2T",
  58. }
  59. func prepare(content string) (string, error) {
  60. result := ""
  61. for _, r := range content {
  62. if r > 127 {
  63. return "", errors.New("Only ASCII strings can be encoded")
  64. }
  65. result += extendedTable[int(r)]
  66. }
  67. return result, nil
  68. }
  69. // Encode returns a code93 barcode for the given content
  70. // if includeChecksum is set to true, two checksum characters are calculated and added to the content
  71. func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) {
  72. if fullASCIIMode {
  73. var err error
  74. content, err = prepare(content)
  75. if err != nil {
  76. return nil, err
  77. }
  78. } else if strings.ContainsRune(content, '*') {
  79. return nil, errors.New("invalid data! content may not contain '*'")
  80. }
  81. data := content + string(getChecksum(content, 20))
  82. data += string(getChecksum(data, 15))
  83. data = "*" + data + "*"
  84. result := new(utils.BitList)
  85. for _, r := range data {
  86. info, ok := encodeTable[r]
  87. if !ok {
  88. return nil, errors.New("invalid data!")
  89. }
  90. result.AddBits(info.data, 9)
  91. }
  92. result.AddBit(true)
  93. return utils.New1DCode(barcode.TypeCode93, content, result), nil
  94. }
  95. func getChecksum(content string, maxWeight int) rune {
  96. weight := 1
  97. total := 0
  98. data := []rune(content)
  99. for i := len(data) - 1; i >= 0; i-- {
  100. r := data[i]
  101. info, ok := encodeTable[r]
  102. if !ok {
  103. return ' '
  104. }
  105. total += info.value * weight
  106. if weight++; weight > maxWeight {
  107. weight = 1
  108. }
  109. }
  110. total = total % 47
  111. for r, info := range encodeTable {
  112. if info.value == total {
  113. return r
  114. }
  115. }
  116. return ' '
  117. }