dimensions.go 920 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package pdf417
  2. import "math"
  3. const (
  4. minCols = 2
  5. maxCols = 30
  6. maxRows = 30
  7. minRows = 2
  8. moduleHeight = 2
  9. preferred_ratio = 3.0
  10. )
  11. func calculateNumberOfRows(m, k, c int) int {
  12. r := ((m + 1 + k) / c) + 1
  13. if c*r >= (m + 1 + k + c) {
  14. r--
  15. }
  16. return r
  17. }
  18. func calcDimensions(dataWords, eccWords int) (cols, rows int) {
  19. ratio := 0.0
  20. cols = 0
  21. rows = 0
  22. for c := minCols; c <= maxCols; c++ {
  23. r := calculateNumberOfRows(dataWords, eccWords, c)
  24. if r < minRows {
  25. break
  26. }
  27. if r > maxRows {
  28. continue
  29. }
  30. newRatio := float64(17*cols+69) / float64(rows*moduleHeight)
  31. if rows != 0 && math.Abs(newRatio-preferred_ratio) > math.Abs(ratio-preferred_ratio) {
  32. continue
  33. }
  34. ratio = newRatio
  35. cols = c
  36. rows = r
  37. }
  38. if rows == 0 {
  39. r := calculateNumberOfRows(dataWords, eccWords, minCols)
  40. if r < minRows {
  41. rows = minRows
  42. cols = minCols
  43. }
  44. }
  45. return
  46. }