tsc.js 7.0 KB

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