tsc.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. var app = getApp();
  2. var encode = require("./encoding.js");
  3. var jpPrinter = {    
  4. createNew: function() {      
  5. var jpPrinter = {};
  6. var data = "";
  7. var command = []
  8. jpPrinter.name = "蓝牙打印机";
  9. jpPrinter.init = function() {};
  10. jpPrinter.addCommand = function(content) { //将指令转成数组装起
  11. var code = new encode.TextEncoder(
  12. 'gb18030', {
  13. NONSTANDARD_allowLegacyEncoding: true
  14. }).encode(content)
  15. for (var i = 0; i < code.length; ++i) {
  16. command.push(code[i])
  17. }
  18. }
  19. jpPrinter.setSize = function(pageWidght, pageHeight) { //设置页面大小
  20. data = "SIZE " + pageWidght.toString() + " mm" + "," + pageHeight.toString() + " mm" + "\r\n";
  21. jpPrinter.addCommand(data)
  22. };
  23. jpPrinter.setSpeed = function(printSpeed) { //设置打印机速度
  24. data = "SPEED " + printSpeed.toString() + "\r\n";
  25. jpPrinter.addCommand(data)
  26. };
  27. jpPrinter.setDensity = function(printDensity) { //设置打印机浓度
  28. data = "DENSITY " + printDensity.toString() + "\r\n";
  29. jpPrinter.addCommand(data)
  30. };
  31. jpPrinter.setGap = function(printGap) { //传感器
  32. data = "GAP " + printGap.toString() + " mm,0 mm\r\n";
  33. jpPrinter.addCommand(data)
  34. };
  35. jpPrinter.setBline = function(printBline) { //黑标纸
  36. data = "BLINE " + printBline.toString() + " mm,0 mm\r\n";
  37. jpPrinter.addCommand(data)
  38. };
  39. jpPrinter.setCountry = function(country) { //选择国际字符集
  40. /*
  41. 001:USA
  42. 002:French
  43. 003:Latin America
  44. 034:Spanish
  45. 039:Italian
  46. 044:United Kingdom
  47. 046:Swedish
  48. 047:Norwegian
  49. 049:German
  50. */
  51. data = "COUNTRY " + country + "\r\n";
  52. jpPrinter.addCommand(data)
  53. };
  54. jpPrinter.setCodepage = function(codepage) { //选择国际代码页
  55. /*
  56. 8-bit codepage 字符集代表
  57. 437:United States
  58. 850:Multilingual
  59. 852:Slavic
  60. 860:Portuguese
  61. 863:Canadian/French
  62. 865:Nordic
  63. Windows code page
  64. 1250:Central Europe
  65. 1252:Latin I
  66. 1253:Greek
  67. 1254:Turkish
  68. 以下代码页仅限于 12×24 dot 英数字体
  69. WestEurope:WestEurope
  70. Greek:Greek
  71. Hebrew:Hebrew
  72. EastEurope:EastEurope
  73. Iran:Iran
  74. IranII:IranII
  75. Latvian:Latvian
  76. Arabic:Arabic
  77. Vietnam:Vietnam
  78. Uygur:Uygur
  79. Thai:Thai
  80. 1252:Latin I
  81. 1257:WPC1257
  82. 1251:WPC1251
  83. 866:Cyrillic
  84. 858:PC858
  85. 747:PC747
  86. 864:PC864
  87. 1001:PC100
  88. */
  89. data = "CODEPAGE " + codepage + "\r\n";
  90. jpPrinter.addCommand(data)
  91. }
  92. jpPrinter.setCls = function() { //清除打印机缓存
  93. data = "CLS\r\n";
  94. jpPrinter.addCommand(data)
  95. };
  96. jpPrinter.setFeed = function(feed) { //将纸向前推出n
  97. data = "FEED " + feed + "\r\n";
  98. jpPrinter.addCommand(data)
  99. };
  100. jpPrinter.setBackFeed = function(backup) { //将纸向后回拉n
  101. data = "BACKFEED " + backup + "\r\n";
  102. jpPrinter.addCommand(data)
  103. }
  104. jpPrinter.setDirection = function(direction) { //设置打印方向,参考编程手册
  105. data = "DIRECTION " + direction + "\r\n";
  106. jpPrinter.addCommand(data)
  107. };
  108. jpPrinter.setReference = function(x, y) { //设置坐标原点,与打印方向有关
  109. data = "REFERENCE " + x + "," + y + "\r\n";
  110. jpPrinter.addCommand(data)
  111. };
  112. jpPrinter.setFromfeed = function() { //根据Size进一张标签纸
  113. data = "FORMFEED\r\n";
  114. jpPrinter.addCommand(data)
  115. };
  116. jpPrinter.setHome = function() { //根据Size找到下一张标签纸的位置
  117. data = "HOME\r\n";
  118. jpPrinter.addCommand(data)
  119. };
  120. jpPrinter.setSound = function(level, interval) { //控制蜂鸣器
  121. data = "SOUND " + level + "," + interval + "\r\n";
  122. jpPrinter.addCommand(data)
  123. };
  124. jpPrinter.setLimitfeed = function(limit) { // 检测垂直间距
  125. data = "LIMITFEED " + limit + "\r\n";
  126. jpPrinter.addCommand(data)
  127. };
  128. jpPrinter.setBar = function(x, y, width, height) { //绘制线条
  129. data = "BAR " + x + "," + y + "," + width + "," + height + "\r\n"
  130. jpPrinter.addCommand(data)
  131. };
  132. jpPrinter.setBox = function(x_start, y_start, x_end, y_end, thickness) { //绘制方框
  133. data = "BOX " + x_start + "," + y_start + "," + x_end + "," + y_end + "," + thickness + "\r\n";
  134. jpPrinter.addCommand(data)
  135. };
  136. jpPrinter.setErase = function(x_start, y_start, x_width, y_height) { //清除指定区域的数据
  137. data = "ERASE " + x_start + "," + y_start + "," + x_width + "," + y_height + "\r\n";
  138. jpPrinter.addCommand(data)
  139. };
  140. jpPrinter.setReverse = function(x_start, y_start, x_width, y_height) { //将指定的区域反相打印
  141. data = "REVERSE " + x_start + "," + y_start + "," + x_width + "," + y_height + "\r\n";
  142. jpPrinter.addCommand(data)
  143. };
  144. jpPrinter.setText = function(x, y, font, x_, y_, str) { //打印文字
  145. data = "TEXT " + x + "," + y + ",\"" + font + "\"," + 0 + "," + x_ + "," + y_ + "," + "\"" + str + "\"\r\n"
  146. jpPrinter.addCommand(data)
  147. };
  148. jpPrinter.setQR = function(x, y, level, width, mode, content) { //打印二维码
  149. data = "QRCODE " + x + "," + y + "," + level + "," + width + "," + mode + "," + 0 + ",\"" + content + "\"\r\n"
  150. jpPrinter.addCommand(data)
  151. };
  152. jpPrinter.setBarCode = function(x, y, codetype, height, readable, narrow, wide, content) { //打印条形码
  153. data = "BARCODE " + x + "," + y + ",\"" + codetype + "\"," + height + "," + readable + "," + 0 + "," + narrow + "," + wide + ",\"" + content + "\"\r\n"
  154. jpPrinter.addCommand(data)
  155. };
  156. jpPrinter.setBitmap = function(x, y, mode, res) { //添加图片,res为画布参数
  157. console.log(res)
  158. var width = parseInt((res.width + 7) / 8 * 8 / 8)
  159. var height = res.height;
  160. var time = 1;
  161. var temp = res.data.length - width * 32;
  162. var pointList = []
  163. var inverted_Data = []
  164. var correct_Data = []
  165. console.log(width + "--" + height)
  166. data = "BITMAP " + x + "," + y + "," + width + "," + height + "," + mode + ","
  167. jpPrinter.addCommand(data)
  168. for (var i = 0; i < height; ++i) {
  169. console.log(temp)
  170. for (var j = 0; j < width; ++j) {
  171. for (var k = 0; k < 32; k += 4) {
  172. if (res.data[temp] == 0 && res.data[temp + 1] == 0 && res.data[temp + 2] == 0 && res.data[temp + 3] == 0) {
  173. pointList.push(1)
  174. } else {
  175. pointList.push(0)
  176. }
  177. temp += 4
  178. }
  179. }
  180. time++
  181. temp = res.data.length - width * 32 * time
  182. }
  183. for (var i = 0; i < pointList.length; i += 8) {
  184. var p = pointList[i] * 128 + pointList[i + 1] * 64 + pointList[i + 2] * 32 + pointList[i + 3] * 16 + pointList[i + 4] * 8 + pointList[i + 5] * 4 + pointList[i + 6] * 2 + pointList[i + 7]
  185. inverted_Data.push(p)
  186. correct_Data.push(p)
  187. }
  188. for (var i = height; i > 0; i--) {
  189. for (var j = 0; j < width; ++j) {
  190. correct_Data[(height - i - 1) * width + j] = inverted_Data[i * width + j]
  191. }
  192. }
  193. for (var i = 0; i < correct_Data.length; ++i) {
  194. command.push(correct_Data[i])
  195. }
  196. }
  197. jpPrinter.setPagePrint = function() { //打印页面
  198. data = "PRINT 1,1\r\n"
  199. jpPrinter.addCommand(data)
  200. };
  201. //获取打印数据
  202. jpPrinter.getData = function() {
  203. return command;
  204. };  
  205. return jpPrinter; 
  206. }
  207. };
  208. module.exports.jpPrinter = jpPrinter;