commands.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * 修改自https://github.com/song940/node-escpos/blob/master/commands.js
  3. * ESC/POS _ (Constants)
  4. */
  5. const encode = require('./encoding');
  6. var _ = {
  7. LF: [0x0a],
  8. FS: [0x1c],
  9. FF: [0x0c],
  10. GS: [0x1d],
  11. DLE: [0x10],
  12. EOT: [0x04],
  13. NUL: [0x00],
  14. ESC: [0x1b],
  15. EOL: '\n',
  16. };
  17. /**
  18. * [FEED_CONTROL_SEQUENCES Feed control sequences]
  19. * @type {Object}
  20. */
  21. _.FEED_CONTROL_SEQUENCES = {
  22. CTL_LF: [0x0a], // Print and line feed
  23. CTL_GLF: [0x4a, 0x00], // Print and feed paper (without spaces between lines)
  24. CTL_FF: [0x0c], // Form feed
  25. CTL_CR: [0x0d], // Carriage return
  26. CTL_HT: [0x09], // Horizontal tab
  27. CTL_VT: [0x0b], // Vertical tab
  28. };
  29. _.CHARACTER_SPACING = {
  30. CS_DEFAULT: [0x1b, 0x20, 0x00],
  31. CS_SET: [0x1b, 0x20]
  32. };
  33. _.LINE_SPACING = {
  34. LS_DEFAULT: [0x1b, 0x32],
  35. LS_SET: [0x1b, 0x33],
  36. LS_ZERO: [0x1b, 0x00]
  37. };
  38. /**
  39. * [HARDWARE Printer hardware]
  40. * @type {Object}
  41. */
  42. _.HARDWARE = {
  43. HW_INIT: [0x1b, 0x40], // Clear data in buffer and reset modes
  44. HW_SELECT: [0x1b, 0x3d, 0x01], // Printer select
  45. HW_RESET: [0x1b, 0x3f, 0x0a, 0x00], // Reset printer hardware
  46. };
  47. /**
  48. * [CASH_DRAWER Cash Drawer]
  49. * @type {Object}
  50. */
  51. _.CASH_DRAWER = {
  52. CD_KICK_2: [0x1b, 0x70, 0x00], // Sends a pulse to pin 2 []
  53. CD_KICK_5: [0x1b, 0x70, 0x01], // Sends a pulse to pin 5 []
  54. };
  55. /**
  56. * [MARGINS Margins sizes]
  57. * @type {Object}
  58. */
  59. _.MARGINS = {
  60. BOTTOM: [0x1b, 0x4f], // Fix bottom size
  61. LEFT: [0x1b, 0x6c], // Fix left size
  62. RIGHT: [0x1b, 0x51], // Fix right size
  63. };
  64. /**
  65. * [PAPER Paper]
  66. * @type {Object}
  67. */
  68. _.PAPER = {
  69. PAPER_FULL_CUT: [0x1d, 0x56, 0x00], // Full cut paper
  70. PAPER_PART_CUT: [0x1d, 0x56, 0x01], // Partial cut paper
  71. PAPER_CUT_A: [0x1d, 0x56, 0x41], // Partial cut paper
  72. PAPER_CUT_B: [0x1d, 0x56, 0x42], // Partial cut paper
  73. };
  74. /**
  75. * [TEXT_FORMAT Text format]
  76. * @type {Object}
  77. */
  78. _.TEXT_FORMAT = {
  79. TXT_NORMAL: [0x1b, 0x21, 0x00], // Normal text
  80. TXT_2HEIGHT: [0x1b, 0x21, 0x10], // Double height text
  81. TXT_2WIDTH: [0x1b, 0x21, 0x20], // Double width text
  82. TXT_4SQUARE: [0x1b, 0x21, 0x30], // Double width & height text
  83. TXT_UNDERL_OFF: [0x1b, 0x2d, 0x00], // Underline font OFF
  84. TXT_UNDERL_ON: [0x1b, 0x2d, 0x01], // Underline font 1-dot ON
  85. TXT_UNDERL2_ON: [0x1b, 0x2d, 0x02], // Underline font 2-dot ON
  86. TXT_BOLD_OFF: [0x1b, 0x45, 0x00], // Bold font OFF
  87. TXT_BOLD_ON: [0x1b, 0x45, 0x01], // Bold font ON
  88. TXT_ITALIC_OFF: [0x1b, 0x35], // Italic font ON
  89. TXT_ITALIC_ON: [0x1b, 0x34], // Italic font ON
  90. TXT_FONT_A: [0x1b, 0x4d, 0x00], // Font type A
  91. TXT_FONT_B: [0x1b, 0x4d, 0x01], // Font type B
  92. TXT_FONT_C: [0x1b, 0x4d, 0x02], // Font type C
  93. TXT_ALIGN_LT: [0x1b, 0x61, 0x00], // Left justification
  94. TXT_ALIGN_CT: [0x1b, 0x61, 0x01], // Centering
  95. TXT_ALIGN_RT: [0x1b, 0x61, 0x02], // Right justification
  96. };
  97. /**
  98. * [BARCODE_FORMAT Barcode format]
  99. * @type {Object}
  100. */
  101. _.BARCODE_FORMAT = {
  102. BARCODE_TXT_OFF: [0x1d, 0x48, 0x00], // HRI barcode chars OFF
  103. BARCODE_TXT_ABV: [0x1d, 0x48, 0x01], // HRI barcode chars above
  104. BARCODE_TXT_BLW: [0x1d, 0x48, 0x02], // HRI barcode chars below
  105. BARCODE_TXT_BTH: [0x1d, 0x48, 0x03], // HRI barcode chars both above and below
  106. BARCODE_FONT_A: [0x1d, 0x66, 0x00], // Font type A for HRI barcode chars
  107. BARCODE_FONT_B: [0x1d, 0x66, 0x01], // Font type B for HRI barcode chars
  108. BARCODE_HEIGHT: function(height) { // Barcode Height [1-255]
  109. return [0x1d, 0x68, height];
  110. },
  111. BARCODE_WIDTH: function(width) { // Barcode Width [2-6]
  112. return [0x1d, 0x77, width];
  113. },
  114. BARCODE_HEIGHT_DEFAULT: [0x1d, 0x68, 0x64], // Barcode height default:100
  115. BARCODE_WIDTH_DEFAULT: [0x1d, 0x77, 0x01], // Barcode width default:1
  116. BARCODE_UPC_A: [0x1d, 0x6b, 0x00], // Barcode type UPC-A
  117. BARCODE_UPC_E: [0x1d, 0x6b, 0x01], // Barcode type UPC-E
  118. BARCODE_EAN13: [0x1d, 0x6b, 0x02], // Barcode type EAN13
  119. BARCODE_EAN8: [0x1d, 0x6b, 0x03], // Barcode type EAN8
  120. BARCODE_CODE39: [0x1d, 0x6b, 0x04], // Barcode type CODE39
  121. BARCODE_ITF: [0x1d, 0x6b, 0x05], // Barcode type ITF
  122. BARCODE_NW7: [0x1d, 0x6b, 0x06], // Barcode type NW7
  123. BARCODE_CODE93: [0x1d, 0x6b, 0x48], // Barcode type CODE93
  124. BARCODE_CODE128: [0x1d, 0x6b, 0x49], // Barcode type CODE128
  125. };
  126. /**
  127. * [QRCODE_FORMAT qrcode format二维码]
  128. * @type {Object}
  129. */
  130. _.QRCODE_FORMAT = {
  131. QRCODE_SIZE: function(n) { // 设置二维码大小
  132. let data = [29, 40, 107, 3, 0, 49, 67]
  133. if (n > 15) {
  134. n = 15
  135. }
  136. if (n < 1) {
  137. n = 1
  138. }
  139. data.push(n)
  140. return data;
  141. },
  142. QRCODE_ERROR: function(n) { // 设置纠错等级
  143. /*
  144. n 功能 纠错能力
  145. 48 选择纠错等级 L 7
  146. 49 选择纠错等级 M 15
  147. 50 选择纠错等级 Q 25
  148. 51 选择纠错等级 H 30
  149. */
  150. let data = [29, 40, 107, 3, 0, 49, 69]
  151. data.push(n)
  152. return data;
  153. },
  154. QRCODE_DATA: function(content) { // 设置二维码内容
  155. let data = [29, 40, 107]
  156. var code = new encode.TextEncoder(
  157. 'gb18030', {
  158. NONSTANDARD_allowLegacyEncoding: true
  159. }).encode(content)
  160. data.push(parseInt((code.length + 3) % 256))
  161. data.push(parseInt((code.length + 3) / 256))
  162. data.push(49)
  163. data.push(80)
  164. data.push(48)
  165. for (var i = 0; i < code.length; ++i) {
  166. data.push(code[i])
  167. }
  168. return data;
  169. },
  170. QRCODE_PRINT: [29, 40, 107, 3, 0, 49, 81, 48], // 打印二维码
  171. };
  172. /**
  173. * [IMAGE_FORMAT Image format]
  174. * @type {Object}
  175. */
  176. _.IMAGE_FORMAT = {
  177. S_RASTER_N: [0x1d, 0x76, 0x30, 0x00], // Set raster image normal size
  178. S_RASTER_2W: [0x1d, 0x76, 0x30, 0x01], // Set raster image double width
  179. S_RASTER_2H: [0x1d, 0x76, 0x30, 0x02], // Set raster image double height
  180. S_RASTER_Q: [0x1d, 0x76, 0x30, 0x03], // Set raster image quadruple
  181. };
  182. /**
  183. * [BITMAP_FORMAT description]
  184. * @type {Object}
  185. */
  186. _.BITMAP_FORMAT = {
  187. BITMAP_S8: [0x1b, 0x2a, 0x00],
  188. BITMAP_D8: [0x1b, 0x2a, 0x01],
  189. BITMAP_S24: [0x1b, 0x2a, 0x20],
  190. BITMAP_D24: [0x1b, 0x2a, 0x21]
  191. };
  192. /**
  193. * [GSV0_FORMAT description]
  194. * @type {Object}
  195. */
  196. _.GSV0_FORMAT = {
  197. GSV0_NORMAL: [0x1d, 0x76, 0x30, 0x00],
  198. GSV0_DW: [0x1d, 0x76, 0x30, 0x01],
  199. GSV0_DH: [0x1d, 0x76, 0x30, 0x02],
  200. GSV0_DWDH: [0x1d, 0x76, 0x30, 0x03]
  201. };
  202. /**
  203. * [BEEP description]
  204. * @type {string}
  205. */
  206. _.BEEP = [0x1b, 0x42]; // Printer Buzzer pre hex
  207. /**
  208. * [COLOR description]
  209. * @type {Object}
  210. */
  211. _.COLOR = {
  212. 0: [0x1b, 0x72, 0x00], // black
  213. 1: [0x1b, 0x72, 0x01] // red
  214. };
  215. /**
  216. * [exports description]
  217. * @type {[type]}
  218. */
  219. module.exports = _;