Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. const BufferHelper = require("./buffer-helper");
  2. // const fs = require('fs');
  3. // const Jimp = require("jimp");
  4. // const request = require('request');
  5. const iconv = require("iconv-lite");
  6. // const Deasync = require("deasync");
  7. const Buffer = require('buffer').Buffer
  8. var exchange_text_with_times = exports.exchange_text_with_times = function exchange_text_with_times(text, times, options){
  9. times = (times === undefined ? 1 : times);
  10. var bytes = new BufferHelper();
  11. var text_buffer = exchange_text(text, options)
  12. for(var i=0;i<times;i++){
  13. bytes.concat(text_buffer);
  14. }
  15. return bytes.toBuffer()
  16. }
  17. var exchange_text = exports.exchange_text = function exchange_text(text, options){
  18. options = options || {
  19. beep: false,
  20. cut: true,
  21. tailingLine: true,
  22. encoding: 'UTF8',
  23. }
  24. // 初始化打印机
  25. var init_printer_bytes = new Buffer([27, 64]);
  26. // 读取设备状态
  27. var check_state_bytes = new Buffer([29, 153]);
  28. // 返回字符 1D 99 XX FF
  29. // 其中XX的每位表示的意义
  30. // 0 0:有纸 1:缺纸
  31. // 1 0:合盖 1:开盖
  32. // 2 0:温度正常 1:机芯过热
  33. // 3 0:电量正常 1:电量低
  34. // 4 0:未打印状态 1:打印状态
  35. // 5 未定义
  36. // 6 未定义
  37. // 7 未定义
  38. // 正常打印模式
  39. var init_bytes = new Buffer([27, 33, 0]);
  40. // 放大字体
  41. //<M></M> 中号字体(2倍高)
  42. //<D></D> 中号字体(2倍宽)
  43. //<B></B> 大号字体(2倍高宽)
  44. //<C></C> 居中
  45. //<CM></CM> 中号字体居中
  46. //<CD></CD> 中号字体居中
  47. //<CB></CB> 大号字体居中
  48. //<QR></QR> 二维码图片地址
  49. //<QRI></QRI> 二维码图片地址
  50. //<PCN></PCN> 公众号
  51. // var m_start_bytes = new Buffer([27, 50, 27, 97, 0, 29, 33, 1]);
  52. // var m_end_bytes = new Buffer([29, 33, 0]);
  53. // var b_start_bytes = new Buffer([27, 50, 27, 97, 0, 29, 33, 17]);
  54. // var b_end_bytes = new Buffer([29, 33, 0]);
  55. var c_start_bytes = new Buffer([27, 97, 1]);
  56. var c_end_bytes = new Buffer([]); // [ 27, 97, 0 ];
  57. // var d_start_bytes = new Buffer([27, 50, 27, 97, 0, 29, 33, 16]);
  58. // var d_end_bytes = new Buffer([29, 33, 0]);
  59. // var cm_start_bytes = new Buffer([27, 50, 29, 33, 1, 27, 97, 1]);
  60. // var cm_end_bytes = new Buffer([29, 33, 0]);
  61. // var cd_start_bytes = new Buffer([27, 50, 29, 33, 16, 27, 97, 1]);
  62. // var cd_end_bytes = new Buffer([29, 33, 0]);
  63. // var cb_start_bytes = new Buffer([27, 50, 29, 33, 17, 27, 97, 1]);
  64. // var cb_end_bytes = new Buffer([29, 33, 0]);
  65. var reset_bytes = new Buffer([27, 97, 0, 29, 33, 0, 27, 50]);
  66. //针对针式调整后
  67. var m_start_bytes = new Buffer([27, 33, 16, 28, 33, 8]);
  68. var m_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  69. var b_start_bytes = new Buffer([27, 33, 48, 28, 33, 12]);
  70. var b_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  71. var cm_start_bytes = new Buffer([27, 97, 1, 27, 33, 16, 28, 33, 8]);
  72. var cm_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  73. var cb_start_bytes = new Buffer([27, 97, 1, 27, 33, 48, 28, 33, 12]);
  74. var cb_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  75. var cd_start_bytes = new Buffer([27, 97, 1, 27, 33, 32, 28, 33, 4]);
  76. var cd_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  77. var d_start_bytes = new Buffer([27, 33, 32, 28, 33, 4]);
  78. var d_end_bytes = new Buffer([27, 33, 0, 28, 33, 0]);
  79. // 居中
  80. var align_center_bytes = new Buffer([27, 97, 1]);
  81. var align_left_bytes = new Buffer([27, 97, 0]);
  82. // 设置行间距 默认值为8即1mm
  83. var default_space_bytes = new Buffer([27, 50]);
  84. var none_space_bytes = new Buffer([27, 51, 0]);
  85. var b_space_bytes = new Buffer([27, 51, 120]);
  86. // 切纸
  87. var cut_bytes = new Buffer([27, 105]);
  88. // 弹钱箱
  89. var moneybox_bytes = new Buffer([27, 112, 7]);
  90. // 蜂鸣 { 27, 66, 次数, 时长 * 50ms }
  91. var beep_bytes = new Buffer([ 27, 66, 3, 2 ])
  92. var bytes = new BufferHelper();
  93. bytes.concat(init_printer_bytes);
  94. bytes.concat(default_space_bytes);
  95. var temp = "";
  96. for (var i = 0; i < text.length; i++)
  97. {
  98. var ch = text[i];
  99. var index;
  100. if (ch == '<')
  101. {
  102. bytes.concat(iconv.encode(temp, options.encoding));
  103. temp = "";
  104. if (text.substring(i, i+3) == "<M>")
  105. {
  106. bytes.concat(m_start_bytes); i += 2;
  107. }
  108. else if (text.substring(i, i+4) == "</M>")
  109. {
  110. bytes.concat(m_end_bytes); i += 3;
  111. }
  112. else if (text.substring(i, i+3) == "<B>")
  113. {
  114. bytes.concat(b_start_bytes); i += 2;
  115. }
  116. else if (text.substring(i, i+4) == "</B>")
  117. {
  118. bytes.concat(b_end_bytes); i += 3;
  119. }
  120. else if (text.substring(i, i+3) == "<D>")
  121. {
  122. bytes.concat(d_start_bytes); i += 2;
  123. }
  124. else if (text.substring(i, i+4) == "</D>")
  125. {
  126. bytes.concat(d_end_bytes); i += 3;
  127. }
  128. else if (text.substring(i, i+3) == "<C>")
  129. {
  130. bytes.concat(c_start_bytes); i += 2;
  131. }
  132. else if (text.substring(i, i+4) == "</C>")
  133. {
  134. bytes.concat(c_end_bytes); i += 3;
  135. }
  136. else if (text.substring(i, i+4) == "<CM>")
  137. {
  138. bytes.concat(cm_start_bytes); i += 3;
  139. }
  140. else if (text.substring(i, i+5) == "</CM>")
  141. {
  142. bytes.concat(cm_end_bytes); i += 4;
  143. }
  144. else if (text.substring(i, i+4) == "<CD>")
  145. {
  146. bytes.concat(cd_start_bytes); i += 3;
  147. }
  148. else if (text.substring(i, i+5) == "</CD>")
  149. {
  150. bytes.concat(cd_end_bytes); i += 4;
  151. }
  152. else if (text.substring(i, i+4) == "<CB>")
  153. {
  154. bytes.concat(cb_start_bytes); i += 3;
  155. }
  156. else if (text.substring(i, i+5) == "</CB>")
  157. {
  158. bytes.concat(cb_end_bytes); i += 4;
  159. }
  160. else if (text.substring(i, i+4) == "<QR>")
  161. {
  162. index = text.indexOf("</QR>", i + 4);
  163. if (index != -1)
  164. {
  165. var url = text.substring(i + 4, index);
  166. bytes.concat(align_center_bytes);
  167. bytes.concat(none_space_bytes);
  168. bytes.concat(change_image_url_to_bytes(url));
  169. bytes.concat(default_space_bytes);
  170. bytes.concat(align_left_bytes);
  171. i = index + 4;
  172. }
  173. else
  174. {
  175. temp = temp + ch;
  176. }
  177. }
  178. else if (text.substring(i, i+5) == "<QRI>")
  179. {
  180. index = text.indexOf("</QRI>", i + 5);
  181. if (index != -1)
  182. {
  183. var url = text.substring(i + 5, index);
  184. bytes.concat(align_center_bytes);
  185. bytes.concat(none_space_bytes);
  186. bytes.concat(change_image_url_to_bytes(url));
  187. bytes.concat(default_space_bytes);
  188. bytes.concat(align_left_bytes);
  189. i = index + 5;
  190. }
  191. else
  192. {
  193. temp = temp + ch;
  194. }
  195. }
  196. else if (text.substring(i, i+5) == "<PCN>")
  197. {
  198. index = text.indexOf("</PCN>", i + 5);
  199. if (index != -1)
  200. {
  201. var url = "http://open.weixin.qq.com/qr/code/?username=" + text.substring(i + 5, index);
  202. bytes.concat(align_center_bytes);
  203. bytes.concat(none_space_bytes);
  204. bytes.concat(change_image_url_to_bytes(url));
  205. bytes.concat(default_space_bytes);
  206. bytes.concat(align_left_bytes);
  207. i = index + 5;
  208. }
  209. else
  210. {
  211. temp = temp + ch;
  212. }
  213. }
  214. }
  215. else if(ch == "\n"){
  216. temp = temp + ch;
  217. bytes.concat(iconv.encode(temp, options.encoding));
  218. bytes.concat(reset_bytes);
  219. temp = "";
  220. }
  221. else
  222. {
  223. temp = temp + ch;
  224. }
  225. }
  226. if (temp.length > 0)
  227. {
  228. bytes.concat(iconv.encode(temp, options.encoding));
  229. }
  230. var line_bytes = new Buffer([10, 10, 10, 10, 10])
  231. if (options.tailingLine){
  232. bytes.concat(line_bytes);
  233. }
  234. if (options.cut){
  235. bytes.concat(cut_bytes);
  236. }
  237. if (options.beep){
  238. bytes.concat(beep_bytes)
  239. }
  240. return bytes.toBuffer();
  241. }
  242. var change_image_url_to_bytes = exports.change_image_url_to_bytes = function change_image_url_to_bytes(url, options){
  243. options = options||{
  244. encoding: 'UTF8'
  245. };
  246. var buffer = new Buffer(iconv.encode(url, options.encoding));
  247. // var buffer = new Buffer(0);
  248. // var sync = true;
  249. // Jimp.read(url, function (err, img) {
  250. // if(err) {
  251. // console.error(err.message)
  252. // }else{
  253. // img.resize(250, 250).quality(60).greyscale();
  254. // buffer = exchange_image(img, 130);
  255. // }
  256. // sync = false
  257. // });
  258. // while(sync){ Deasync.sleep(100)}
  259. return buffer;
  260. }
  261. // var download_image = exports.download_image = function download_image(url, path){
  262. // try{
  263. // request(url).pipe(fs.createWriteStream(path));
  264. // return true
  265. // }catch(e){
  266. // return false
  267. // }
  268. // }
  269. // var exchange_image = exports.exchange_image = function exchange_image(img, threshold){
  270. // var hex;
  271. // var nl = img.bitmap.width % 256;
  272. // var nh = parseInt(img.bitmap.width / 256);
  273. // var bytes = new BufferHelper();
  274. // // data
  275. // var data = new Buffer([0, 0, 0]);
  276. // var line = new Buffer([10]);
  277. // for (var i = 0; i < parseInt(img.bitmap.height / 24) + 1; i++)
  278. // {
  279. // // ESC * m nL nH 点阵图
  280. // var header = new Buffer([27, 42, 33, nl, nh]);
  281. // bytes.concat(header);
  282. // for (var j = 0; j < img.bitmap.width; j++)
  283. // {
  284. // data[0] = data[1] = data[2] = 0; // Clear to Zero.
  285. // for (var k = 0; k < 24; k++)
  286. // {
  287. // if (((i * 24) + k) < img.bitmap.height) // if within the BMP size
  288. // {
  289. // hex = img.getPixelColor(j, (i * 24) + k);
  290. // if (Jimp.intToRGBA(hex).r <= threshold)
  291. // {
  292. // data[parseInt(k / 8)] += (128 >> (k % 8));
  293. // }
  294. // }
  295. // }
  296. // var dit = new Buffer([data[0], data[1], data[2]])
  297. // bytes.concat(dit);
  298. // }
  299. // bytes.concat(line);
  300. // } // data
  301. // return bytes.toBuffer();
  302. // }