encoder.go 3.3 KB

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