encoder.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Package pdf417 can create PDF-417 barcodes
  2. package pdf417
  3. import (
  4. "fmt"
  5. "github.com/boombuler/barcode"
  6. "github.com/boombuler/barcode/utils"
  7. )
  8. const (
  9. padding_codeword = 900
  10. )
  11. // Encodes the given data as PDF417 barcode.
  12. // securityLevel should be between 0 and 8. The higher the number, the more
  13. // additional error-correction codes are added.
  14. func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
  15. if securityLevel >= 9 {
  16. return nil, fmt.Errorf("Invalid security level %d", securityLevel)
  17. }
  18. sl := securitylevel(securityLevel)
  19. dataWords, err := highlevelEncode(data)
  20. if err != nil {
  21. return nil, err
  22. }
  23. columns, rows := calcDimensions(len(dataWords), sl.ErrorCorrectionWordCount())
  24. if columns < minCols || columns > maxCols || rows < minRows || rows > maxRows {
  25. return nil, fmt.Errorf("Unable to fit data in barcode")
  26. }
  27. barcode := new(pdfBarcode)
  28. barcode.data = data
  29. codeWords, err := encodeData(dataWords, columns, sl)
  30. if err != nil {
  31. return nil, err
  32. }
  33. grid := [][]int{}
  34. for i := 0; i < len(codeWords); i += columns {
  35. grid = append(grid, codeWords[i:min(i+columns, len(codeWords))])
  36. }
  37. codes := [][]int{}
  38. for rowNum, row := range grid {
  39. table := rowNum % 3
  40. rowCodes := make([]int, 0, columns+4)
  41. rowCodes = append(rowCodes, start_word)
  42. rowCodes = append(rowCodes, getCodeword(table, getLeftCodeWord(rowNum, rows, columns, securityLevel)))
  43. for _, word := range row {
  44. rowCodes = append(rowCodes, getCodeword(table, word))
  45. }
  46. rowCodes = append(rowCodes, getCodeword(table, getRightCodeWord(rowNum, rows, columns, securityLevel)))
  47. rowCodes = append(rowCodes, stop_word)
  48. codes = append(codes, rowCodes)
  49. }
  50. barcode.code = renderBarcode(codes)
  51. barcode.width = (columns+4)*17 + 1
  52. return barcode, nil
  53. }
  54. func encodeData(dataWords []int, columns int, sl securitylevel) ([]int, error) {
  55. dataCount := len(dataWords)
  56. ecCount := sl.ErrorCorrectionWordCount()
  57. padWords := getPadding(dataCount, ecCount, columns)
  58. dataWords = append(dataWords, padWords...)
  59. length := len(dataWords) + 1
  60. dataWords = append([]int{length}, dataWords...)
  61. ecWords := sl.Compute(dataWords)
  62. return append(dataWords, ecWords...), nil
  63. }
  64. func getLeftCodeWord(rowNum int, rows int, columns int, securityLevel byte) int {
  65. tableId := rowNum % 3
  66. var x int
  67. switch tableId {
  68. case 0:
  69. x = (rows - 3) / 3
  70. case 1:
  71. x = int(securityLevel) * 3
  72. x += (rows - 1) % 3
  73. case 2:
  74. x = columns - 1
  75. }
  76. return 30*(rowNum/3) + x
  77. }
  78. func getRightCodeWord(rowNum int, rows int, columns int, securityLevel byte) int {
  79. tableId := rowNum % 3
  80. var x int
  81. switch tableId {
  82. case 0:
  83. x = columns - 1
  84. case 1:
  85. x = (rows - 1) / 3
  86. case 2:
  87. x = int(securityLevel) * 3
  88. x += (rows - 1) % 3
  89. }
  90. return 30*(rowNum/3) + x
  91. }
  92. func min(a, b int) int {
  93. if a <= b {
  94. return a
  95. }
  96. return b
  97. }
  98. func getPadding(dataCount int, ecCount int, columns int) []int {
  99. totalCount := dataCount + ecCount + 1
  100. mod := totalCount % columns
  101. padding := []int{}
  102. if mod > 0 {
  103. padCount := columns - mod
  104. padding = make([]int, padCount)
  105. for i := 0; i < padCount; i++ {
  106. padding[i] = padding_codeword
  107. }
  108. }
  109. return padding
  110. }
  111. func renderBarcode(codes [][]int) *utils.BitList {
  112. bl := new(utils.BitList)
  113. for _, row := range codes {
  114. lastIdx := len(row) - 1
  115. for i, col := range row {
  116. if i == lastIdx {
  117. bl.AddBits(col, 18)
  118. } else {
  119. bl.AddBits(col, 17)
  120. }
  121. }
  122. }
  123. return bl
  124. }