highlevel.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package pdf417
  2. import (
  3. "errors"
  4. "math/big"
  5. "github.com/boombuler/barcode/utils"
  6. )
  7. type encodingMode byte
  8. type subMode byte
  9. const (
  10. encText encodingMode = iota
  11. encNumeric
  12. encBinary
  13. subUpper subMode = iota
  14. subLower
  15. subMixed
  16. subPunct
  17. latch_to_text = 900
  18. latch_to_byte_padded = 901
  19. latch_to_numeric = 902
  20. latch_to_byte = 924
  21. shift_to_byte = 913
  22. min_numeric_count = 13
  23. )
  24. var (
  25. mixedMap map[rune]int
  26. punctMap map[rune]int
  27. )
  28. func init() {
  29. mixedMap = make(map[rune]int)
  30. mixedRaw := []rune{
  31. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 38, 13, 9, 44, 58,
  32. 35, 45, 46, 36, 47, 43, 37, 42, 61, 94, 0, 32, 0, 0, 0,
  33. }
  34. for idx, ch := range mixedRaw {
  35. if ch > 0 {
  36. mixedMap[ch] = idx
  37. }
  38. }
  39. punctMap = make(map[rune]int)
  40. punctRaw := []rune{
  41. 59, 60, 62, 64, 91, 92, 93, 95, 96, 126, 33, 13, 9, 44, 58,
  42. 10, 45, 46, 36, 47, 34, 124, 42, 40, 41, 63, 123, 125, 39, 0,
  43. }
  44. for idx, ch := range punctRaw {
  45. if ch > 0 {
  46. punctMap[ch] = idx
  47. }
  48. }
  49. }
  50. func determineConsecutiveDigitCount(data []rune) int {
  51. cnt := 0
  52. for _, r := range data {
  53. if utils.RuneToInt(r) == -1 {
  54. break
  55. }
  56. cnt++
  57. }
  58. return cnt
  59. }
  60. func encodeNumeric(digits []rune) ([]int, error) {
  61. digitCount := len(digits)
  62. chunkCount := digitCount / 44
  63. if digitCount%44 != 0 {
  64. chunkCount++
  65. }
  66. codeWords := []int{}
  67. for i := 0; i < chunkCount; i++ {
  68. start := i * 44
  69. end := start + 44
  70. if end > digitCount {
  71. end = digitCount
  72. }
  73. chunk := digits[start:end]
  74. chunkNum := big.NewInt(0)
  75. _, ok := chunkNum.SetString("1"+string(chunk), 10)
  76. if !ok {
  77. return nil, errors.New("Failed converting: " + string(chunk))
  78. }
  79. cws := []int{}
  80. for chunkNum.Cmp(big.NewInt(0)) > 0 {
  81. newChunk, cw := chunkNum.DivMod(chunkNum, big.NewInt(900), big.NewInt(0))
  82. chunkNum = newChunk
  83. cws = append([]int{int(cw.Int64())}, cws...)
  84. }
  85. codeWords = append(codeWords, cws...)
  86. }
  87. return codeWords, nil
  88. }
  89. func determineConsecutiveTextCount(msg []rune) int {
  90. result := 0
  91. isText := func(ch rune) bool {
  92. return ch == '\t' || ch == '\n' || ch == '\r' || (ch >= 32 && ch <= 126)
  93. }
  94. for i, ch := range msg {
  95. numericCount := determineConsecutiveDigitCount(msg[i:])
  96. if numericCount >= min_numeric_count || (numericCount == 0 && !isText(ch)) {
  97. break
  98. }
  99. result++
  100. }
  101. return result
  102. }
  103. func encodeText(text []rune, submode subMode) (subMode, []int) {
  104. isAlphaUpper := func(ch rune) bool {
  105. return ch == ' ' || (ch >= 'A' && ch <= 'Z')
  106. }
  107. isAlphaLower := func(ch rune) bool {
  108. return ch == ' ' || (ch >= 'a' && ch <= 'z')
  109. }
  110. isMixed := func(ch rune) bool {
  111. _, ok := mixedMap[ch]
  112. return ok
  113. }
  114. isPunctuation := func(ch rune) bool {
  115. _, ok := punctMap[ch]
  116. return ok
  117. }
  118. idx := 0
  119. var tmp []int
  120. for idx < len(text) {
  121. ch := text[idx]
  122. switch submode {
  123. case subUpper:
  124. if isAlphaUpper(ch) {
  125. if ch == ' ' {
  126. tmp = append(tmp, 26) //space
  127. } else {
  128. tmp = append(tmp, int(ch-'A'))
  129. }
  130. } else {
  131. if isAlphaLower(ch) {
  132. submode = subLower
  133. tmp = append(tmp, 27) // lower latch
  134. continue
  135. } else if isMixed(ch) {
  136. submode = subMixed
  137. tmp = append(tmp, 28) // mixed latch
  138. continue
  139. } else {
  140. tmp = append(tmp, 29) // punctuation switch
  141. tmp = append(tmp, punctMap[ch])
  142. break
  143. }
  144. }
  145. break
  146. case subLower:
  147. if isAlphaLower(ch) {
  148. if ch == ' ' {
  149. tmp = append(tmp, 26) //space
  150. } else {
  151. tmp = append(tmp, int(ch-'a'))
  152. }
  153. } else {
  154. if isAlphaUpper(ch) {
  155. tmp = append(tmp, 27) //upper switch
  156. tmp = append(tmp, int(ch-'A'))
  157. break
  158. } else if isMixed(ch) {
  159. submode = subMixed
  160. tmp = append(tmp, 28) //mixed latch
  161. continue
  162. } else {
  163. tmp = append(tmp, 29) //punctuation switch
  164. tmp = append(tmp, punctMap[ch])
  165. break
  166. }
  167. }
  168. break
  169. case subMixed:
  170. if isMixed(ch) {
  171. tmp = append(tmp, mixedMap[ch])
  172. } else {
  173. if isAlphaUpper(ch) {
  174. submode = subUpper
  175. tmp = append(tmp, 28) //upper latch
  176. continue
  177. } else if isAlphaLower(ch) {
  178. submode = subLower
  179. tmp = append(tmp, 27) //lower latch
  180. continue
  181. } else {
  182. if idx+1 < len(text) {
  183. next := text[idx+1]
  184. if isPunctuation(next) {
  185. submode = subPunct
  186. tmp = append(tmp, 25) //punctuation latch
  187. continue
  188. }
  189. }
  190. tmp = append(tmp, 29) //punctuation switch
  191. tmp = append(tmp, punctMap[ch])
  192. }
  193. }
  194. break
  195. default: //subPunct
  196. if isPunctuation(ch) {
  197. tmp = append(tmp, punctMap[ch])
  198. } else {
  199. submode = subUpper
  200. tmp = append(tmp, 29) //upper latch
  201. continue
  202. }
  203. }
  204. idx++
  205. }
  206. h := 0
  207. result := []int{}
  208. for i, val := range tmp {
  209. if i%2 != 0 {
  210. h = (h * 30) + val
  211. result = append(result, h)
  212. } else {
  213. h = val
  214. }
  215. }
  216. if len(tmp)%2 != 0 {
  217. result = append(result, (h*30)+29)
  218. }
  219. return submode, result
  220. }
  221. func determineConsecutiveBinaryCount(msg []byte) int {
  222. result := 0
  223. for i, _ := range msg {
  224. numericCount := determineConsecutiveDigitCount([]rune(string(msg[i:])))
  225. if numericCount >= min_numeric_count {
  226. break
  227. }
  228. textCount := determineConsecutiveTextCount([]rune(string(msg[i:])))
  229. if textCount > 5 {
  230. break
  231. }
  232. result++
  233. }
  234. return result
  235. }
  236. func encodeBinary(data []byte, startmode encodingMode) []int {
  237. result := []int{}
  238. count := len(data)
  239. if count == 1 && startmode == encText {
  240. result = append(result, shift_to_byte)
  241. } else if (count % 6) == 0 {
  242. result = append(result, latch_to_byte)
  243. } else {
  244. result = append(result, latch_to_byte_padded)
  245. }
  246. idx := 0
  247. // Encode sixpacks
  248. if count >= 6 {
  249. words := make([]int, 5)
  250. for (count - idx) >= 6 {
  251. var t int64 = 0
  252. for i := 0; i < 6; i++ {
  253. t = t << 8
  254. t += int64(data[idx+i])
  255. }
  256. for i := 0; i < 5; i++ {
  257. words[4-i] = int(t % 900)
  258. t = t / 900
  259. }
  260. result = append(result, words...)
  261. idx += 6
  262. }
  263. }
  264. //Encode rest (remaining n<5 bytes if any)
  265. for i := idx; i < count; i++ {
  266. result = append(result, int(data[i]&0xff))
  267. }
  268. return result
  269. }
  270. func highlevelEncode(dataStr string) ([]int, error) {
  271. encodingMode := encText
  272. textSubMode := subUpper
  273. result := []int{}
  274. data := []byte(dataStr)
  275. for len(data) > 0 {
  276. numericCount := determineConsecutiveDigitCount([]rune(string(data)))
  277. if numericCount >= min_numeric_count || numericCount == len(data) {
  278. result = append(result, latch_to_numeric)
  279. encodingMode = encNumeric
  280. textSubMode = subUpper
  281. numData, err := encodeNumeric([]rune(string(data[:numericCount])))
  282. if err != nil {
  283. return nil, err
  284. }
  285. result = append(result, numData...)
  286. data = data[numericCount:]
  287. } else {
  288. textCount := determineConsecutiveTextCount([]rune(string(data)))
  289. if textCount >= 5 || textCount == len(data) {
  290. if encodingMode != encText {
  291. result = append(result, latch_to_text)
  292. encodingMode = encText
  293. textSubMode = subUpper
  294. }
  295. var txtData []int
  296. textSubMode, txtData = encodeText([]rune(string(data[:textCount])), textSubMode)
  297. result = append(result, txtData...)
  298. data = data[textCount:]
  299. } else {
  300. binaryCount := determineConsecutiveBinaryCount(data)
  301. if binaryCount == 0 {
  302. binaryCount = 1
  303. }
  304. bytes := data[:binaryCount]
  305. if len(bytes) != 1 || encodingMode != encText {
  306. encodingMode = encBinary
  307. textSubMode = subUpper
  308. }
  309. byteData := encodeBinary(bytes, encodingMode)
  310. result = append(result, byteData...)
  311. data = data[binaryCount:]
  312. }
  313. }
  314. }
  315. return result, nil
  316. }