draw1D.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Generic barcode drawing functions
  3. */
  4. var gm = require('gm');
  5. const MODE_BINARY = 0;
  6. const MODE_BARWIDTH = 1;
  7. // constructor
  8. // with defaults
  9. function Barcode1D() {
  10. this.mode = MODE_BARWIDTH;
  11. this.width = 0;
  12. this.height = 0;
  13. this.background = '#FFF';
  14. this.barcolor = '#000';
  15. this.type = 'PNG';
  16. this.offset = 0;
  17. this.modulewidth = 1;
  18. }
  19. Barcode1D.MODE_BINARY = MODE_BINARY;
  20. Barcode1D.MODE_BARWIDTH = MODE_BARWIDTH;
  21. /*
  22. * Convert binary to barwidth (static)
  23. * This is for compatiblity purposes,
  24. *
  25. * @param Sring|Array pattern - binary pattern [1,1,1,0,0,1,0]
  26. * @return Array - barwidth pattern [3,2,1,1]
  27. */
  28. Barcode1D.convertToBarwidth = function (pattern) {
  29. if (!pattern.length) {
  30. return [];
  31. }
  32. var count = 0,
  33. current = pattern[0],
  34. ret = [];
  35. for(var i = 0; i < pattern.length; i++, count++) {
  36. if (current !== (current = pattern[i])) {
  37. ret.push(count);
  38. count = 0;
  39. }
  40. }
  41. ret.push(count);
  42. return ret;
  43. }
  44. /*
  45. * set the pattern mode to bar width
  46. * @return Object (this)
  47. */
  48. Barcode1D.prototype.modeBarwidth = function () {
  49. this.mode = MODE_BARWIDTH;
  50. return this;
  51. };
  52. /*
  53. * set the pattern mode to binary
  54. * @return Object (this)
  55. */
  56. Barcode1D.prototype.modeBinary = function () {
  57. this.mode = MODE_BINARY;
  58. return this;
  59. };
  60. /*
  61. * set the type for output
  62. * @param String type - file extention of the type
  63. * @return Object (this)
  64. */
  65. Barcode1D.prototype.setType = function (type) {
  66. this.type = type;
  67. return this;
  68. };
  69. /*
  70. * set the type for output
  71. * @param Int width - width of the image
  72. * @return Object (this)
  73. */
  74. Barcode1D.prototype.setWidth = function (width) {
  75. this.width = width;
  76. return this;
  77. };
  78. /*
  79. * set the type for output
  80. * @param Int height - height of the image
  81. * @return Object (this)
  82. */
  83. Barcode1D.prototype.setHeight = function (height) {
  84. this.height = height;
  85. return this;
  86. };
  87. /*
  88. * set the pixel width of a single barcode module
  89. * also calcualtes and sets the offset
  90. * @param Int basewidth - the width of barcode if the module width was 1px
  91. * @return Object (this)
  92. */
  93. Barcode1D.prototype.setModuleWidth = function (basewidth) {
  94. // bit shift 0 is just a quick way to turn it into an integer
  95. this.modulewidth = this.width / basewidth >> 0;
  96. this.offset = this.width % basewidth / 2 >> 0;
  97. return this;
  98. };
  99. /*
  100. * Draw using 1d barwiths
  101. * @param Array pattern = Array of barwidths, alternating between black and white
  102. * @param function callback
  103. */
  104. Barcode1D.prototype.draw = function (pattern, callback) {
  105. var img = gm(this.width, this.height, this.background),
  106. pos = this.offset,
  107. draw = true,
  108. w;
  109. img.stroke(this.barcolor, 0);
  110. for (var i = 0; i < pattern.length; i++) {
  111. //console.log(pattern[i] + ' ' + this.modulewidth);
  112. w = pattern[i] * this.modulewidth;
  113. if (draw) {
  114. // subtract 1 from the x2 pos because drawRectangle adds another pixel on
  115. // to the end of it, no idea why. The positioning otherwise is fine, i.e.
  116. // you don't need it in the `w` var.
  117. img.drawRectangle(pos, 0, pos + w - 1, this.height);
  118. }
  119. pos += w;
  120. draw ^= true;
  121. }
  122. img.stream(this.type, function (err, stdout, strerr) {
  123. callback(err, stdout);
  124. });
  125. };
  126. module.exports = Barcode1D;