@pisell/utils 3.0.6 → 3.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. package/es/arrayUtils.d.ts +23 -26
  2. package/es/arrayUtils.js +157 -97
  3. package/es/common/index.d.ts +5 -0
  4. package/es/common/index.js +5 -0
  5. package/es/common/stateHelper.d.ts +2 -6
  6. package/es/common/stateHelper.js +24 -22
  7. package/es/common/types.d.ts +10 -13
  8. package/es/common/types.js +1 -0
  9. package/es/componentStorage.d.ts +25 -27
  10. package/es/componentStorage.js +130 -94
  11. package/es/constants/index.js +9 -7
  12. package/es/date.js +53 -36
  13. package/es/document.js +67 -43
  14. package/es/escPosPrinter/EscPosPrinter.js +490 -353
  15. package/es/escPosPrinter/index.d.ts +9 -9
  16. package/es/escPosPrinter/index.js +52 -37
  17. package/es/firebase/config/index.js +54 -0
  18. package/es/firebase/index.js +232 -0
  19. package/es/format.d.ts +15 -12
  20. package/es/format.js +156 -89
  21. package/es/image.js +60 -48
  22. package/es/index.d.ts +24 -39
  23. package/es/index.js +35 -40
  24. package/es/jsBridge/index.d.ts +4 -8
  25. package/es/jsBridge/index.js +55 -56
  26. package/es/jsBridge/regDeviceApi.js +126 -87
  27. package/es/jsBridge/types.d.ts +115 -95
  28. package/es/jsBridge/types.js +1 -0
  29. package/es/locales.d.ts +8 -11
  30. package/es/locales.js +96 -77
  31. package/es/log.d.ts +6 -11
  32. package/es/log.js +59 -37
  33. package/es/miniRedux.d.ts +13 -20
  34. package/es/miniRedux.js +76 -42
  35. package/es/number-input/hooks/index.d.ts +4 -0
  36. package/es/number-input/hooks/index.js +5 -0
  37. package/es/number-input/hooks/useNumberInputState.d.ts +2 -6
  38. package/es/number-input/hooks/useNumberInputState.js +128 -71
  39. package/es/number-input/index.d.ts +14 -0
  40. package/es/number-input/index.js +21 -0
  41. package/es/number-input/types.d.ts +88 -88
  42. package/es/number-input/types.js +1 -0
  43. package/es/number-input/utils/formatter.d.ts +10 -13
  44. package/es/number-input/utils/formatter.js +77 -51
  45. package/es/number-input/utils/index.d.ts +7 -0
  46. package/es/number-input/utils/index.js +8 -0
  47. package/es/number-input/utils/validation.d.ts +2 -7
  48. package/es/number-input/utils/validation.js +66 -51
  49. package/es/otherUtils.d.ts +9 -12
  50. package/es/otherUtils.js +129 -71
  51. package/es/platform.d.ts +9 -12
  52. package/es/platform.js +85 -61
  53. package/es/select/hooks/index.d.ts +5 -0
  54. package/es/select/hooks/index.js +6 -0
  55. package/es/select/hooks/useDebouncedSearch.d.ts +1 -4
  56. package/es/select/hooks/useDebouncedSearch.js +53 -39
  57. package/es/select/hooks/useSelectState.d.ts +7 -7
  58. package/es/select/hooks/useSelectState.js +113 -56
  59. package/es/select/index.d.ts +8 -7
  60. package/es/select/index.js +13 -0
  61. package/es/select/types.d.ts +211 -211
  62. package/es/select/types.js +1 -0
  63. package/es/select/utils/filterOptions.d.ts +6 -7
  64. package/es/select/utils/filterOptions.js +31 -26
  65. package/es/select/utils/helpers.d.ts +9 -10
  66. package/es/select/utils/helpers.js +52 -42
  67. package/es/select/utils/index.d.ts +7 -4
  68. package/es/select/utils/index.js +8 -0
  69. package/es/select/utils/sortOptions.d.ts +5 -6
  70. package/es/select/utils/sortOptions.js +31 -19
  71. package/es/select/utils/tagAggregation.d.ts +6 -6
  72. package/es/select/utils/tagAggregation.js +47 -34
  73. package/es/text-input/hooks/index.d.ts +4 -0
  74. package/es/text-input/hooks/index.js +5 -0
  75. package/es/text-input/hooks/useTextInputState.d.ts +2 -6
  76. package/es/text-input/hooks/useTextInputState.js +126 -69
  77. package/es/text-input/index.d.ts +14 -0
  78. package/es/text-input/index.js +20 -0
  79. package/es/text-input/types.d.ts +48 -48
  80. package/es/text-input/types.js +1 -0
  81. package/es/text-input/utils/index.d.ts +5 -0
  82. package/es/text-input/utils/index.js +7 -0
  83. package/es/text-input/utils/validation.d.ts +2 -7
  84. package/es/text-input/utils/validation.js +70 -50
  85. package/es/typeUtils.js +84 -61
  86. package/es/walletValidity.d.ts +42 -44
  87. package/es/walletValidity.js +557 -396
  88. package/es/weighing.d.ts +45 -48
  89. package/es/weighing.js +106 -83
  90. package/lib/arrayUtils.d.ts +23 -26
  91. package/lib/arrayUtils.js +111 -101
  92. package/lib/common/index.d.ts +5 -0
  93. package/lib/common/index.js +29 -0
  94. package/lib/common/stateHelper.d.ts +2 -6
  95. package/lib/common/stateHelper.js +32 -21
  96. package/lib/common/types.d.ts +10 -13
  97. package/lib/common/types.js +17 -0
  98. package/lib/componentStorage.d.ts +25 -27
  99. package/lib/componentStorage.js +126 -96
  100. package/lib/constants/index.js +36 -7
  101. package/lib/date.js +65 -43
  102. package/lib/document.js +62 -46
  103. package/lib/escPosPrinter/EscPosPrinter.js +403 -327
  104. package/lib/escPosPrinter/index.d.ts +9 -9
  105. package/lib/escPosPrinter/index.js +84 -38
  106. package/lib/firebase/config/index.js +79 -0
  107. package/lib/firebase/index.js +180 -0
  108. package/lib/format.d.ts +15 -12
  109. package/lib/format.js +133 -91
  110. package/lib/image.js +74 -48
  111. package/lib/index.d.ts +24 -39
  112. package/lib/index.js +80 -137
  113. package/lib/jsBridge/index.d.ts +4 -8
  114. package/lib/jsBridge/index.js +76 -57
  115. package/lib/jsBridge/regDeviceApi.js +133 -86
  116. package/lib/jsBridge/types.d.ts +115 -95
  117. package/lib/jsBridge/types.js +17 -0
  118. package/lib/locales.d.ts +8 -11
  119. package/lib/locales.js +90 -79
  120. package/lib/log.d.ts +6 -11
  121. package/lib/log.js +61 -37
  122. package/lib/miniRedux.d.ts +13 -20
  123. package/lib/miniRedux.js +89 -43
  124. package/lib/number-input/hooks/index.d.ts +4 -0
  125. package/lib/number-input/hooks/index.js +29 -0
  126. package/lib/number-input/hooks/useNumberInputState.d.ts +2 -6
  127. package/lib/number-input/hooks/useNumberInputState.js +81 -71
  128. package/lib/number-input/index.d.ts +14 -0
  129. package/lib/number-input/index.js +41 -0
  130. package/lib/number-input/types.d.ts +88 -88
  131. package/lib/number-input/types.js +17 -0
  132. package/lib/number-input/utils/formatter.d.ts +10 -13
  133. package/lib/number-input/utils/formatter.js +49 -52
  134. package/lib/number-input/utils/index.d.ts +7 -0
  135. package/lib/number-input/utils/index.js +39 -0
  136. package/lib/number-input/utils/validation.d.ts +2 -7
  137. package/lib/number-input/utils/validation.js +52 -50
  138. package/lib/otherUtils.d.ts +9 -12
  139. package/lib/otherUtils.js +98 -86
  140. package/lib/platform.d.ts +9 -12
  141. package/lib/platform.js +123 -68
  142. package/lib/select/hooks/index.d.ts +5 -0
  143. package/lib/select/hooks/index.js +34 -0
  144. package/lib/select/hooks/useDebouncedSearch.d.ts +1 -4
  145. package/lib/select/hooks/useDebouncedSearch.js +41 -40
  146. package/lib/select/hooks/useSelectState.d.ts +7 -7
  147. package/lib/select/hooks/useSelectState.js +93 -58
  148. package/lib/select/index.d.ts +8 -7
  149. package/lib/select/index.js +53 -0
  150. package/lib/select/types.d.ts +211 -211
  151. package/lib/select/types.js +17 -0
  152. package/lib/select/utils/filterOptions.d.ts +6 -7
  153. package/lib/select/utils/filterOptions.js +37 -26
  154. package/lib/select/utils/helpers.d.ts +9 -10
  155. package/lib/select/utils/helpers.js +41 -45
  156. package/lib/select/utils/index.d.ts +7 -4
  157. package/lib/select/utils/index.js +48 -0
  158. package/lib/select/utils/sortOptions.d.ts +5 -6
  159. package/lib/select/utils/sortOptions.js +39 -18
  160. package/lib/select/utils/tagAggregation.d.ts +6 -6
  161. package/lib/select/utils/tagAggregation.js +41 -33
  162. package/lib/text-input/hooks/index.d.ts +4 -0
  163. package/lib/text-input/hooks/index.js +29 -0
  164. package/lib/text-input/hooks/useTextInputState.d.ts +2 -6
  165. package/lib/text-input/hooks/useTextInputState.js +79 -69
  166. package/lib/text-input/index.d.ts +14 -0
  167. package/lib/text-input/index.js +35 -0
  168. package/lib/text-input/types.d.ts +48 -48
  169. package/lib/text-input/types.js +17 -0
  170. package/lib/text-input/utils/index.d.ts +5 -0
  171. package/lib/text-input/utils/index.js +32 -0
  172. package/lib/text-input/utils/validation.d.ts +2 -7
  173. package/lib/text-input/utils/validation.js +58 -49
  174. package/lib/typeUtils.js +73 -68
  175. package/lib/walletValidity.d.ts +42 -44
  176. package/lib/walletValidity.js +393 -410
  177. package/lib/weighing.d.ts +45 -48
  178. package/lib/weighing.js +149 -104
  179. package/package.json +1 -1
  180. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js +0 -27
  181. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js +0 -12
  182. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js +0 -25
  183. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPrimitive.js +0 -14
  184. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPropertyKey.js +0 -9
  185. package/es/_virtual/_@oxc-project_runtime@0.122.0/helpers/typeof.js +0 -11
  186. package/es/date.d.ts +0 -31
  187. package/es/document.d.ts +0 -16
  188. package/es/image.d.ts +0 -14
  189. package/es/typeUtils.d.ts +0 -45
  190. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js +0 -27
  191. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js +0 -12
  192. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js +0 -25
  193. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPrimitive.js +0 -14
  194. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/toPropertyKey.js +0 -9
  195. package/lib/_virtual/_@oxc-project_runtime@0.122.0/helpers/typeof.js +0 -16
  196. package/lib/_virtual/_rolldown/runtime.js +0 -23
  197. package/lib/date.d.ts +0 -31
  198. package/lib/document.d.ts +0 -16
  199. package/lib/image.d.ts +0 -14
  200. package/lib/typeUtils.d.ts +0 -45
@@ -1,401 +1,538 @@
1
- const ALIGN_LEFT = 0;
2
- const ALIGN_CENTER = 1;
3
- const ALIGN_RIGHT = 2;
4
- const EscPosPrinter = function(dotsPerLine) {
5
- if (dotsPerLine != 384 && dotsPerLine != 576) dotsPerLine = 384;
6
- this.widthOfColumns = new Array(6);
7
- this.dotsPerLine = dotsPerLine;
8
- this.charHSize = 1;
9
- this.orderData = "";
1
+ // Maximum columns for printing in columns function
2
+ export var MAX_COLUMNS = 6;
3
+
4
+ // Left alignment
5
+ export var ALIGN_LEFT = 0;
6
+ // Center alignment
7
+ export var ALIGN_CENTER = 1;
8
+ // Right alignment
9
+ export var ALIGN_RIGHT = 2;
10
+
11
+ // HRI above the barcode
12
+ export var HRI_POS_ABOVE = 1;
13
+ // HRI below the barcode
14
+ export var HRI_POS_BELOW = 2;
15
+ var EscPosPrinter = function EscPosPrinter(dotsPerLine) {
16
+ if (dotsPerLine != 384 && dotsPerLine != 576)
17
+ // Print width in dots. 384 for 58mm and 576 for 80mm
18
+ dotsPerLine = 384;
19
+ this.widthOfColumns = new Array(MAX_COLUMNS);
20
+ this.dotsPerLine = dotsPerLine;
21
+ this.charHSize = 1;
22
+ this.orderData = "";
10
23
  };
11
24
  function _numToHexStr(n, bytes) {
12
- var str = "";
13
- var v;
14
- for (var i = 0; i < bytes; i++) {
15
- v = n & 255;
16
- if (v < 16) str += "0" + v.toString(16);
17
- else str += v.toString(16);
18
- n >>= 8;
19
- }
20
- return str;
25
+ var str = "";
26
+ var v;
27
+ for (var i = 0; i < bytes; i++) {
28
+ v = n & 0xff;
29
+ if (v < 0x10) str += "0" + v.toString(16);else str += v.toString(16);
30
+ n >>= 8;
31
+ }
32
+ return str;
21
33
  }
22
34
  function _unicodeToUtf8(unicode) {
23
- var c1, c2, c3, c4;
24
- if (unicode < 0) return "";
25
- if (unicode <= 127) {
26
- c1 = unicode & 127;
27
- return _numToHexStr(c1, 1);
28
- }
29
- if (unicode <= 2047) {
30
- c1 = unicode >> 6 & 31 | 192;
31
- c2 = unicode & 63 | 128;
32
- return _numToHexStr(c1, 1) + _numToHexStr(c2, 1);
33
- }
34
- if (unicode <= 65535) {
35
- c1 = unicode >> 12 & 15 | 224;
36
- c2 = unicode >> 6 & 63 | 128;
37
- c3 = unicode & 63 | 128;
38
- return _numToHexStr(c1, 1) + _numToHexStr(c2, 1) + _numToHexStr(c3, 1);
39
- }
40
- if (unicode <= 1114111) {
41
- c1 = unicode >> 18 & 7 | 240;
42
- c2 = unicode >> 12 & 63 | 128;
43
- c3 = unicode >> 6 & 63 | 128;
44
- c4 = unicode & 63 | 128;
45
- return _numToHexStr(c1, 1) + _numToHexStr(c2, 1) + _numToHexStr(c3, 1) + _numToHexStr(c4, 1);
46
- }
47
- return "";
35
+ var c1, c2, c3, c4;
36
+ if (unicode < 0) return "";
37
+ if (unicode <= 0x7f) {
38
+ c1 = unicode & 0x7f;
39
+ return _numToHexStr(c1, 1);
40
+ }
41
+ if (unicode <= 0x7ff) {
42
+ c1 = unicode >> 6 & 0x1f | 0xc0;
43
+ c2 = unicode & 0x3f | 0x80;
44
+ return _numToHexStr(c1, 1) + _numToHexStr(c2, 1);
45
+ }
46
+ if (unicode <= 0xffff) {
47
+ c1 = unicode >> 12 & 0x0f | 0xe0;
48
+ c2 = unicode >> 6 & 0x3f | 0x80;
49
+ c3 = unicode & 0x3f | 0x80;
50
+ return _numToHexStr(c1, 1) + _numToHexStr(c2, 1) + _numToHexStr(c3, 1);
51
+ }
52
+ if (unicode <= 0x10ffff) {
53
+ c1 = unicode >> 18 & 0x07 | 0xf0;
54
+ c2 = unicode >> 12 & 0x3f | 0x80;
55
+ c3 = unicode >> 6 & 0x3f | 0x80;
56
+ c4 = unicode & 0x3f | 0x80;
57
+ return _numToHexStr(c1, 1) + _numToHexStr(c2, 1) + _numToHexStr(c3, 1) + _numToHexStr(c4, 1);
58
+ }
59
+ return "";
48
60
  }
49
- EscPosPrinter.prototype.clear = function() {
50
- this.orderData = "";
61
+
62
+ // Clear the generated order data.
63
+ EscPosPrinter.prototype.clear = function () {
64
+ this.orderData = "";
51
65
  };
52
- EscPosPrinter.prototype.print = function(host, sn, copies) {
53
- var that = this;
54
- return new Promise(function(resolve, reject) {
55
- var xmlobj = new XMLHttpRequest();
56
- xmlobj.timeout = 1e4;
57
- var url = "https://" + host + "/cgi-bin/print.cgi?sn=" + sn + "&copies=" + copies;
58
- xmlobj.open("POST", url, true);
59
- xmlobj.onreadystatechange = function() {
60
- if (xmlobj.readyState === XMLHttpRequest.DONE) if (xmlobj.status.toString(10) === "200") resolve(/task_id: (\d+)/.exec(xmlobj.responseText)[1] || "");
61
- else reject(xmlobj.responseText);
62
- };
63
- xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
64
- xmlobj.send(that.orderData);
65
- });
66
+
67
+ // Print the order data.
68
+ EscPosPrinter.prototype.print = function (host, sn, copies) {
69
+ var that = this;
70
+ return new Promise(function (resolve, reject) {
71
+ var xmlobj = new XMLHttpRequest();
72
+ xmlobj.timeout = 10000;
73
+ var url = "https://" + host + "/cgi-bin/print.cgi?sn=" + sn + "&copies=" + copies;
74
+ xmlobj.open("POST", url, true);
75
+ xmlobj.onreadystatechange = function () {
76
+ if (xmlobj.readyState === XMLHttpRequest.DONE) {
77
+ if (xmlobj.status.toString(10) === "200") {
78
+ var re = /task_id: (\d+)/;
79
+ var matches = re.exec(xmlobj.responseText);
80
+ resolve(matches[1] || "");
81
+ } else {
82
+ reject(xmlobj.responseText);
83
+ }
84
+ }
85
+ };
86
+ xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
87
+ xmlobj.send(that.orderData);
88
+ });
66
89
  };
67
- EscPosPrinter.prototype.queryStatus = function(host, sn, task_id) {
68
- return new Promise(function(resolve, reject) {
69
- var xmlobj = new XMLHttpRequest();
70
- xmlobj.timeout = 1e4;
71
- var url = "https://" + host + "/cgi-bin/status.cgi?sn=" + sn + "&task_id=" + task_id;
72
- xmlobj.open("POST", url, true);
73
- xmlobj.onreadystatechange = function() {
74
- if (xmlobj.readyState === XMLHttpRequest.DONE) if (xmlobj.status.toString(10) === "200") resolve(xmlobj.responseText);
75
- else reject(xmlobj.responseText);
76
- };
77
- xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
78
- xmlobj.send();
79
- });
90
+
91
+ // Query print status.
92
+ EscPosPrinter.prototype.queryStatus = function (host, sn, task_id) {
93
+ return new Promise(function (resolve, reject) {
94
+ var xmlobj = new XMLHttpRequest();
95
+ xmlobj.timeout = 10000;
96
+ var url = "https://" + host + "/cgi-bin/status.cgi?sn=" + sn + "&task_id=" + task_id;
97
+ xmlobj.open("POST", url, true);
98
+ xmlobj.onreadystatechange = function () {
99
+ if (xmlobj.readyState === XMLHttpRequest.DONE) {
100
+ if (xmlobj.status.toString(10) === "200") {
101
+ resolve(xmlobj.responseText);
102
+ } else {
103
+ reject(xmlobj.responseText);
104
+ }
105
+ }
106
+ };
107
+ xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
108
+ xmlobj.send();
109
+ });
80
110
  };
81
- EscPosPrinter.prototype.appendText = function(str) {
82
- for (var i = 0; i < str.length; i++) this.orderData += _unicodeToUtf8(str.charCodeAt(i));
111
+
112
+ //////////////////////////////////////////////////
113
+ // Basic ESC/POS Commands
114
+ //////////////////////////////////////////////////
115
+
116
+ // Append text in the order.
117
+ EscPosPrinter.prototype.appendText = function (str) {
118
+ for (var i = 0; i < str.length; i++) this.orderData += _unicodeToUtf8(str.charCodeAt(i));
83
119
  };
84
- EscPosPrinter.prototype.lineFeed = function(n) {
85
- for (var i = 0; i < n; i++) this.orderData += "0a";
120
+
121
+ // [LF] Print the contents in the buffer and feed n lines.
122
+ EscPosPrinter.prototype.lineFeed = function (n) {
123
+ for (var i = 0; i < n; i++) this.orderData += "0a";
86
124
  };
87
- EscPosPrinter.prototype.restoreDefaultSettings = function() {
88
- this.charHSize = 1;
89
- this.orderData += "1b40";
125
+
126
+ // [ESC @] Restore default settings (line spacing, print modes, etc).
127
+ EscPosPrinter.prototype.restoreDefaultSettings = function () {
128
+ this.charHSize = 1;
129
+ this.orderData += "1b40";
90
130
  };
91
- EscPosPrinter.prototype.restoreDefaultLineSpacing = function() {
92
- this.orderData += "1b32";
131
+
132
+ // [ESC 2] Restore default line spacing.
133
+ EscPosPrinter.prototype.restoreDefaultLineSpacing = function () {
134
+ this.orderData += "1b32";
93
135
  };
94
- EscPosPrinter.prototype.setLineSpacing = function(n) {
95
- if (n >= 0 && n <= 255) this.orderData += "1b33" + _numToHexStr(n, 1);
136
+
137
+ // [ESC 3] Set line spacing.
138
+ EscPosPrinter.prototype.setLineSpacing = function (n) {
139
+ if (n >= 0 && n <= 255) this.orderData += "1b33" + _numToHexStr(n, 1);
96
140
  };
97
- EscPosPrinter.prototype.setPrintModes = function(bold, double_h, double_w) {
98
- var n = 0;
99
- if (bold) n |= 8;
100
- if (double_h) n |= 16;
101
- if (double_w) n |= 32;
102
- this.charHSize = double_w ? 2 : 1;
103
- this.orderData += "1b21" + _numToHexStr(n, 1);
141
+
142
+ // [ESC !] Select print modes (double width/height or not, bold or not).
143
+ EscPosPrinter.prototype.setPrintModes = function (bold, double_h, double_w) {
144
+ var n = 0;
145
+ if (bold) n |= 8;
146
+ if (double_h) n |= 16;
147
+ if (double_w) n |= 32;
148
+ this.charHSize = double_w ? 2 : 1;
149
+ this.orderData += "1b21" + _numToHexStr(n, 1);
104
150
  };
105
- EscPosPrinter.prototype.setCharacterSize = function(h, w) {
106
- var n = 0;
107
- if (h >= 1 && h <= 8) n |= h - 1;
108
- if (w >= 1 && w <= 8) {
109
- n |= w - 1 << 4;
110
- this.charHSize = w;
111
- }
112
- this.orderData += "1d21" + _numToHexStr(n, 1);
151
+
152
+ // [GS !] Set character size (1~8 times of normal width or height).
153
+ EscPosPrinter.prototype.setCharacterSize = function (h, w) {
154
+ var n = 0;
155
+ if (h >= 1 && h <= 8) n |= h - 1;
156
+ if (w >= 1 && w <= 8) {
157
+ n |= w - 1 << 4;
158
+ this.charHSize = w;
159
+ }
160
+ this.orderData += "1d21" + _numToHexStr(n, 1);
113
161
  };
114
- EscPosPrinter.prototype.horizontalTab = function(n) {
115
- for (var i = 0; i < n; i++) this.orderData += "09";
162
+
163
+ // [HT] Jump to the next n TAB positions.
164
+ EscPosPrinter.prototype.horizontalTab = function (n) {
165
+ for (var i = 0; i < n; i++) this.orderData += "09";
116
166
  };
117
- EscPosPrinter.prototype.setAbsolutePrintPosition = function(n) {
118
- if (n >= 0 && n <= 65535) this.orderData += "1b24" + _numToHexStr(n, 2);
167
+
168
+ // [ESC $] Move to horizontal absolute position.
169
+ EscPosPrinter.prototype.setAbsolutePrintPosition = function (n) {
170
+ if (n >= 0 && n <= 65535) this.orderData += "1b24" + _numToHexStr(n, 2);
119
171
  };
120
- EscPosPrinter.prototype.setRelativePrintPosition = function(n) {
121
- if (n >= -32768 && n <= 32767) this.orderData += "1b5c" + _numToHexStr(n, 2);
172
+
173
+ // [ESC \] Move to horizontal relative position.
174
+ EscPosPrinter.prototype.setRelativePrintPosition = function (n) {
175
+ if (n >= -32768 && n <= 32767) this.orderData += "1b5c" + _numToHexStr(n, 2);
122
176
  };
123
- EscPosPrinter.prototype.setAlignment = function(n) {
124
- if (n >= 0 && n <= 2) this.orderData += "1b61" + _numToHexStr(n, 1);
177
+
178
+ // [ESC a] Set alignment.
179
+ EscPosPrinter.prototype.setAlignment = function (n) {
180
+ if (n >= 0 && n <= 2) this.orderData += "1b61" + _numToHexStr(n, 1);
125
181
  };
126
- EscPosPrinter.prototype.cutPaper = function(fullCut) {
127
- this.orderData += "1d56" + (fullCut ? "30" : "31");
182
+
183
+ // [GS V m] Cut paper.
184
+ EscPosPrinter.prototype.cutPaper = function (fullCut) {
185
+ this.orderData += "1d56" + (fullCut ? "30" : "31");
128
186
  };
129
- EscPosPrinter.prototype.postponedCutPaper = function(fullCut, n) {
130
- if (n >= 0 && n <= 255) this.orderData += "1d56" + (fullCut ? "61" : "62") + _numToHexStr(n, 1);
187
+
188
+ // [GS V m n] Postponed cut paper.
189
+ // After sending this command, the cutter will not cut until (76+n) dot lines are fed.
190
+ EscPosPrinter.prototype.postponedCutPaper = function (fullCut, n) {
191
+ if (n >= 0 && n <= 255) this.orderData += "1d56" + (fullCut ? "61" : "62") + _numToHexStr(n, 1);
131
192
  };
193
+
194
+ //////////////////////////////////////////////////
195
+ // Print in Columns
196
+ //////////////////////////////////////////////////
197
+
198
+ // Return the width of a character.
132
199
  function _widthOfChar(c) {
133
- if (c >= 32 && c <= 879 || c >= 65377 && c <= 65439) return 12;
134
- if (c == 8208 || c >= 8211 && c <= 8214 || c >= 8216 && c <= 8217 || c >= 8220 && c <= 8221 || c >= 8229 && c <= 8230 || c >= 8240 && c <= 8243 || c == 8245 || c == 8251) return 24;
135
- if (c >= 4352 && c <= 4607 || c >= 9312 && c <= 9471 || c >= 9632 && c <= 10175 || c >= 11904 && c <= 12255 || c >= 12288 && c <= 12687 || c >= 12704 && c <= 12783 || c >= 12800 && c <= 40959 || c >= 44032 && c <= 55295 || c >= 63744 && c <= 64255 || c >= 65072 && c <= 65103 || c >= 126976 && c <= 129535) return 24;
136
- if (c >= 65281 && c <= 65374 || c >= 65504 && c <= 65509) return 24;
137
- return 0;
200
+ if (c >= 0x00020 && c <= 0x0036f || c >= 0x0ff61 && c <= 0x0ff9f) return 12;
201
+ if (c == 0x02010 || c >= 0x02013 && c <= 0x02016 || c >= 0x02018 && c <= 0x02019 || c >= 0x0201c && c <= 0x0201d || c >= 0x02025 && c <= 0x02026 || c >= 0x02030 && c <= 0x02033 || c == 0x02035 || c == 0x0203b) return 24;
202
+ if (c >= 0x01100 && c <= 0x011ff || c >= 0x02460 && c <= 0x024ff || c >= 0x025a0 && c <= 0x027bf || c >= 0x02e80 && c <= 0x02fdf || c >= 0x03000 && c <= 0x0318f || c >= 0x031a0 && c <= 0x031ef || c >= 0x03200 && c <= 0x09fff || c >= 0x0ac00 && c <= 0x0d7ff || c >= 0x0f900 && c <= 0x0faff || c >= 0x0fe30 && c <= 0x0fe4f || c >= 0x1f000 && c <= 0x1f9ff) return 24;
203
+ if (c >= 0x0ff01 && c <= 0x0ff5e || c >= 0x0ffe0 && c <= 0x0ffe5) return 24;
204
+ return 0;
138
205
  }
139
- function columnWidthWithAlignment(width, alignment) {
140
- return width & 65535 | (alignment & 3) << 16;
206
+ export function columnWidthWithAlignment(width, alignment) {
207
+ return width & 0xffff | (alignment & 3) << 16;
141
208
  }
142
209
  function _columnWidth(v) {
143
- return v & 65535;
210
+ return v & 0xffff;
144
211
  }
145
212
  function _columnAlignment(v) {
146
- return v >> 16 & 3;
213
+ return v >> 16 & 3;
147
214
  }
148
- EscPosPrinter.prototype.setColumnWidths = function() {
149
- var i, remain, width, alignment;
150
- if (arguments.length == 0) return;
151
- for (i = 0; i < 6; i++) this.widthOfColumns[i] = 0;
152
- remain = this.dotsPerLine;
153
- for (i = 0; i < arguments.length; i++) {
154
- if (i == 6) return;
155
- width = _columnWidth(arguments[i]);
156
- alignment = _columnAlignment(arguments[i]);
157
- if (width == 0 || width > remain) {
158
- this.widthOfColumns[i] = columnWidthWithAlignment(remain, alignment);
159
- return;
160
- }
161
- this.widthOfColumns[i] = arguments[i];
162
- remain -= width;
163
- }
215
+
216
+ // Set the width and alignment mode for each columns.
217
+ EscPosPrinter.prototype.setColumnWidths = function /*...*/
218
+ () {
219
+ var i, remain, width, alignment;
220
+ if (arguments.length == 0) return;
221
+ for (i = 0; i < MAX_COLUMNS; i++) this.widthOfColumns[i] = 0;
222
+ remain = this.dotsPerLine; // Dots not used
223
+ for (i = 0; i < arguments.length; i++) {
224
+ if (i == MAX_COLUMNS)
225
+ // Maximum columns exceeded
226
+ return;
227
+ width = _columnWidth(arguments[i]);
228
+ alignment = _columnAlignment(arguments[i]);
229
+ if (width == 0 || width > remain) {
230
+ // Use all free dots for the last column
231
+ this.widthOfColumns[i] = columnWidthWithAlignment(remain, alignment);
232
+ return;
233
+ }
234
+ this.widthOfColumns[i] = arguments[i];
235
+ remain -= width;
236
+ }
164
237
  };
165
- EscPosPrinter.prototype.printInColumns = function() {
166
- var strcurr = new Array(6);
167
- var strrem = new Array(6);
168
- var strwidth = new Array(6);
169
- var i, j, c, w, columns, width, alignment, pos;
170
- var done;
171
- if (arguments.length == 0) return;
172
- columns = 0;
173
- for (i = 0; i < arguments.length; i++) {
174
- if (i == 6 || this.widthOfColumns[i] == 0) break;
175
- strcurr[i] = "";
176
- strrem[i] = arguments[i];
177
- columns++;
178
- }
179
- do {
180
- done = true;
181
- pos = 0;
182
- for (i = 0; i < columns; i++) {
183
- width = _columnWidth(this.widthOfColumns[i]);
184
- if (strrem[i].length == 0) {
185
- pos += width;
186
- continue;
187
- }
188
- done = false;
189
- strcurr[i] = "";
190
- strwidth[i] = 0;
191
- for (j = 0; j < strrem[i].length; j++) {
192
- c = strrem[i].charCodeAt(j);
193
- if (c == 10) {
194
- j++;
195
- break;
196
- } else {
197
- w = _widthOfChar(c);
198
- if (w == 0) {
199
- c = "?";
200
- w = 12;
201
- }
202
- w *= this.charHSize;
203
- if (strwidth[i] + w > width) break;
204
- else {
205
- strcurr[i] += String.fromCharCode(c);
206
- strwidth[i] += w;
207
- }
208
- }
209
- }
210
- if (j < strrem[i].length) strrem[i] = strrem[i].substring(j);
211
- else strrem[i] = "";
212
- alignment = _columnAlignment(this.widthOfColumns[i]);
213
- switch (alignment) {
214
- case 1:
215
- this.setAbsolutePrintPosition(pos + (width - strwidth[i]) / 2);
216
- break;
217
- case 2:
218
- this.setAbsolutePrintPosition(pos + (width - strwidth[i]));
219
- break;
220
- default:
221
- this.setAbsolutePrintPosition(pos);
222
- break;
223
- }
224
- this.appendText(strcurr[i]);
225
- pos += width;
226
- }
227
- if (!done) this.lineFeed(1);
228
- } while (!done);
238
+
239
+ // Print in columns with the current column settings.
240
+ EscPosPrinter.prototype.printInColumns = function /*...*/
241
+ () {
242
+ var strcurr = new Array(MAX_COLUMNS);
243
+ var strrem = new Array(MAX_COLUMNS);
244
+ var strwidth = new Array(MAX_COLUMNS);
245
+ var i, j, c, w, columns, width, alignment, pos;
246
+ var done;
247
+ if (arguments.length == 0) return;
248
+ columns = 0;
249
+ for (i = 0; i < arguments.length; i++) {
250
+ if (i == MAX_COLUMNS || this.widthOfColumns[i] == 0) break;
251
+ strcurr[i] = "";
252
+ strrem[i] = arguments[i];
253
+ columns++;
254
+ }
255
+ do {
256
+ done = true;
257
+ pos = 0;
258
+ for (i = 0; i < columns; i++) {
259
+ width = _columnWidth(this.widthOfColumns[i]);
260
+ if (strrem[i].length == 0) {
261
+ pos += width;
262
+ continue;
263
+ }
264
+ done = false;
265
+ strcurr[i] = "";
266
+ strwidth[i] = 0;
267
+ for (j = 0; j < strrem[i].length; j++) {
268
+ c = strrem[i].charCodeAt(j);
269
+ if (c == 0x0a) {
270
+ j++; // Drop the '\n'
271
+ break;
272
+ } else {
273
+ w = _widthOfChar(c);
274
+ if (w == 0) {
275
+ c = "?";
276
+ w = 12;
277
+ }
278
+ w *= this.charHSize;
279
+ if (strwidth[i] + w > width) {
280
+ break;
281
+ } else {
282
+ strcurr[i] += String.fromCharCode(c);
283
+ strwidth[i] += w;
284
+ }
285
+ }
286
+ }
287
+ if (j < strrem[i].length) strrem[i] = strrem[i].substring(j);else strrem[i] = "";
288
+ alignment = _columnAlignment(this.widthOfColumns[i]);
289
+ switch (alignment) {
290
+ case ALIGN_CENTER:
291
+ this.setAbsolutePrintPosition(pos + (width - strwidth[i]) / 2);
292
+ break;
293
+ case ALIGN_RIGHT:
294
+ this.setAbsolutePrintPosition(pos + (width - strwidth[i]));
295
+ break;
296
+ default:
297
+ this.setAbsolutePrintPosition(pos);
298
+ break;
299
+ }
300
+ this.appendText(strcurr[i]);
301
+ pos += width;
302
+ }
303
+ if (!done) this.lineFeed(1);
304
+ } while (!done);
229
305
  };
230
- EscPosPrinter.prototype.appendBarcode = function(hri_pos, height, module_size, barcode_type, text) {
231
- var text_length = text.length;
232
- if (text_length == 0) return;
233
- if (text_length > 255) text_length = 255;
234
- if (height < 1) height = 1;
235
- else if (height > 255) height = 255;
236
- if (module_size < 1) module_size = 1;
237
- else if (module_size > 6) module_size = 6;
238
- this.orderData += "1d48" + _numToHexStr(hri_pos & 3, 1);
239
- this.orderData += "1d6600";
240
- this.orderData += "1d68" + _numToHexStr(height, 1);
241
- this.orderData += "1d77" + _numToHexStr(module_size, 1);
242
- this.orderData += "1d6b" + _numToHexStr(barcode_type, 1) + _numToHexStr(text_length, 1);
243
- for (var i = 0; i < text_length; i++) this.orderData += _numToHexStr(text.charCodeAt(i), 1);
306
+
307
+ //////////////////////////////////////////////////
308
+ // Barcode / QR Code Printing
309
+ //////////////////////////////////////////////////
310
+
311
+ // Append barcode in the order.
312
+ EscPosPrinter.prototype.appendBarcode = function (hri_pos, height, module_size, barcode_type, text) {
313
+ var text_length = text.length;
314
+ if (text_length == 0) return;
315
+ if (text_length > 255) text_length = 255;
316
+ if (height < 1) height = 1;else if (height > 255) height = 255;
317
+ if (module_size < 1) module_size = 1;else if (module_size > 6) module_size = 6;
318
+ this.orderData += "1d48" + _numToHexStr(hri_pos & 3, 1);
319
+ this.orderData += "1d6600";
320
+ this.orderData += "1d68" + _numToHexStr(height, 1);
321
+ this.orderData += "1d77" + _numToHexStr(module_size, 1);
322
+ this.orderData += "1d6b" + _numToHexStr(barcode_type, 1) + _numToHexStr(text_length, 1);
323
+ for (var i = 0; i < text_length; i++) this.orderData += _numToHexStr(text.charCodeAt(i), 1);
244
324
  };
245
- EscPosPrinter.prototype.appendQRcode = function(module_size, ec_level, text) {
246
- var text_length = text.length;
247
- if (text_length == 0) return;
248
- if (text_length > 65535) text_length = 65535;
249
- if (module_size < 1) module_size = 1;
250
- else if (module_size > 16) module_size = 16;
251
- if (ec_level < 0) ec_level = 0;
252
- else if (ec_level > 3) ec_level = 3;
253
- this.orderData += "1d286b040031410000";
254
- this.orderData += "1d286b03003143" + _numToHexStr(module_size, 1);
255
- this.orderData += "1d286b03003145" + _numToHexStr(ec_level + 48, 1);
256
- this.orderData += "1d286b" + _numToHexStr(text_length + 3, 2) + "315030";
257
- for (var i = 0; i < text_length; i++) this.orderData += _numToHexStr(text.charCodeAt(i), 1);
258
- this.orderData += "1d286b0300315130";
325
+
326
+ // Append QR code in the order.
327
+ EscPosPrinter.prototype.appendQRcode = function (module_size, ec_level, text) {
328
+ var text_length = text.length;
329
+ if (text_length == 0) return;
330
+ if (text_length > 65535) text_length = 65535;
331
+ if (module_size < 1) module_size = 1;else if (module_size > 16) module_size = 16;
332
+ if (ec_level < 0) ec_level = 0;else if (ec_level > 3) ec_level = 3;
333
+ this.orderData += "1d286b040031410000";
334
+ this.orderData += "1d286b03003143" + _numToHexStr(module_size, 1);
335
+ this.orderData += "1d286b03003145" + _numToHexStr(ec_level + 48, 1);
336
+ this.orderData += "1d286b" + _numToHexStr(text_length + 3, 2) + "315030";
337
+ for (var i = 0; i < text_length; i++) this.orderData += _numToHexStr(text.charCodeAt(i), 1);
338
+ this.orderData += "1d286b0300315130";
259
339
  };
340
+
341
+ //////////////////////////////////////////////////
342
+ // Image Printing
343
+ //////////////////////////////////////////////////
344
+
345
+ // Grayscale to mono - diffuse dithering.
260
346
  function _diffuseDither(src_data, width, height) {
261
- if (width <= 0 || height <= 0) return null;
262
- if (src_data.length < width * height) return null;
263
- var bmwidth = width + 7 >> 3;
264
- var dst_data = new Array(bmwidth * height);
265
- var line_buffer = new Array(2 * width);
266
- var i, p, q, x, y, mask;
267
- var line1, line2, b1, b2, tmp;
268
- var err, e1, e3, e5, e7;
269
- var not_last_line;
270
- line1 = 0;
271
- line2 = 1;
272
- for (i = 0; i < width; i++) {
273
- line_buffer[i] = 0;
274
- line_buffer[width + i] = src_data[i];
275
- }
276
- for (y = 0; y < height; y++) {
277
- tmp = line1;
278
- line1 = line2;
279
- line2 = tmp;
280
- not_last_line = y < height - 1;
281
- if (not_last_line) {
282
- p = (y + 1) * width;
283
- for (i = 0; i < width; i++) {
284
- line_buffer[line2 * width + i] = src_data[p];
285
- p++;
286
- }
287
- }
288
- q = y * bmwidth;
289
- for (i = 0; i < bmwidth; i++) {
290
- dst_data[q] = 0;
291
- q++;
292
- }
293
- b1 = 0;
294
- b2 = 0;
295
- q = y * bmwidth;
296
- mask = 128;
297
- for (x = 1; x <= width; x++) {
298
- var idx = line1 * width + b1;
299
- if (line_buffer[idx] < 128) {
300
- err = line_buffer[idx];
301
- dst_data[q] |= mask;
302
- } else err = line_buffer[idx] - 255;
303
- b1++;
304
- if (mask == 1) {
305
- q++;
306
- mask = 128;
307
- } else mask >>= 1;
308
- e7 = err * 7 + 8 >> 4;
309
- e5 = err * 5 + 8 >> 4;
310
- e3 = err * 3 + 8 >> 4;
311
- e1 = err - (e7 + e5 + e3);
312
- if (x < width) line_buffer[line1 * width + b1] += e7;
313
- if (not_last_line) {
314
- line_buffer[line2 * width + b2] += e5;
315
- if (x > 1) line_buffer[line2 * width + b2 - 1] += e3;
316
- if (x < width) line_buffer[line2 * width + b2 + 1] += e1;
317
- }
318
- b2++;
319
- }
320
- }
321
- return dst_data;
347
+ if (width <= 0 || height <= 0) return null;
348
+ if (src_data.length < width * height) return null;
349
+ var bmwidth = width + 7 >> 3;
350
+ var dst_data = new Array(bmwidth * height);
351
+ var line_buffer = new Array(2 * width);
352
+ var i, p, q, x, y, mask;
353
+ var line1, line2, b1, b2, tmp;
354
+ var err, e1, e3, e5, e7;
355
+ var not_last_line;
356
+ line1 = 0;
357
+ line2 = 1;
358
+ for (i = 0; i < width; i++) {
359
+ line_buffer[i] = 0;
360
+ line_buffer[width + i] = src_data[i];
361
+ }
362
+ for (y = 0; y < height; y++) {
363
+ tmp = line1;
364
+ line1 = line2;
365
+ line2 = tmp;
366
+ not_last_line = y < height - 1;
367
+ if (not_last_line) {
368
+ p = (y + 1) * width;
369
+ for (i = 0; i < width; i++) {
370
+ line_buffer[line2 * width + i] = src_data[p];
371
+ p++;
372
+ }
373
+ }
374
+ q = y * bmwidth;
375
+ for (i = 0; i < bmwidth; i++) {
376
+ dst_data[q] = 0;
377
+ q++;
378
+ }
379
+ b1 = 0;
380
+ b2 = 0;
381
+ q = y * bmwidth;
382
+ mask = 0x80;
383
+ for (x = 1; x <= width; x++) {
384
+ var idx = line1 * width + b1;
385
+ if (line_buffer[idx] < 128) {
386
+ // Black pixel
387
+ err = line_buffer[idx];
388
+ dst_data[q] |= mask;
389
+ } else {
390
+ err = line_buffer[idx] - 255;
391
+ }
392
+ b1++;
393
+ if (mask == 1) {
394
+ q++;
395
+ mask = 0x80;
396
+ } else {
397
+ mask >>= 1;
398
+ }
399
+ e7 = err * 7 + 8 >> 4;
400
+ e5 = err * 5 + 8 >> 4;
401
+ e3 = err * 3 + 8 >> 4;
402
+ e1 = err - (e7 + e5 + e3);
403
+ if (x < width) line_buffer[line1 * width + b1] += e7;
404
+ if (not_last_line) {
405
+ line_buffer[line2 * width + b2] += e5;
406
+ if (x > 1) line_buffer[line2 * width + b2 - 1] += e3;
407
+ if (x < width) line_buffer[line2 * width + b2 + 1] += e1;
408
+ }
409
+ b2++;
410
+ }
411
+ }
412
+ return dst_data;
322
413
  }
414
+
415
+ // Grayscale to mono - threshold dithering.
323
416
  function _thresholdDither(src_data, width, height) {
324
- if (width <= 0 || height <= 0) return null;
325
- if (src_data.length < width * height) return null;
326
- var bmwidth = width + 7 >> 3;
327
- var dst_data = new Array(bmwidth * height);
328
- var p = 0, q = 0, k, x, y, mask;
329
- for (y = 0; y < height; y++) {
330
- k = q;
331
- mask = 128;
332
- for (x = 0; x < width; x++) {
333
- if (src_data[p] < 128) dst_data[k] |= mask;
334
- p++;
335
- if (mask == 1) {
336
- k++;
337
- mask = 128;
338
- } else mask >>= 1;
339
- }
340
- q += bmwidth;
341
- }
342
- return dst_data;
417
+ if (width <= 0 || height <= 0) return null;
418
+ if (src_data.length < width * height) return null;
419
+ var bmwidth = width + 7 >> 3;
420
+ var dst_data = new Array(bmwidth * height);
421
+ var p, q, k, x, y, mask;
422
+ p = 0;
423
+ q = 0;
424
+ for (y = 0; y < height; y++) {
425
+ k = q;
426
+ mask = 0x80;
427
+ for (x = 0; x < width; x++) {
428
+ if (src_data[p] < 128) {
429
+ // Black pixel
430
+ dst_data[k] |= mask;
431
+ }
432
+ p++;
433
+ if (mask == 1) {
434
+ k++;
435
+ mask = 0x80;
436
+ } else {
437
+ mask >>= 1;
438
+ }
439
+ }
440
+ q += bmwidth;
441
+ }
442
+ return dst_data;
343
443
  }
444
+
445
+ // RGB to grayscale.
344
446
  function _convertToGray(imgdata, width, height) {
345
- var gray_data = new Array(width * height);
346
- var i = 0, j = 0, x, y, r, g, b;
347
- for (y = 0; y < height; y++) for (x = 0; x < width; x++) {
348
- r = imgdata.data[j++] & 255;
349
- g = imgdata.data[j++] & 255;
350
- b = imgdata.data[j++] & 255;
351
- j++;
352
- gray_data[i++] = r * 11 + g * 16 + b * 5 >> 5 & 255;
353
- }
354
- return gray_data;
447
+ var gray_data = new Array(width * height);
448
+ var i = 0,
449
+ j = 0,
450
+ x,
451
+ y,
452
+ r,
453
+ g,
454
+ b;
455
+ for (y = 0; y < height; y++) {
456
+ for (x = 0; x < width; x++) {
457
+ r = imgdata.data[j++] & 0xff;
458
+ g = imgdata.data[j++] & 0xff;
459
+ b = imgdata.data[j++] & 0xff;
460
+ j++; // Skip the Alpha channel
461
+ gray_data[i++] = r * 11 + g * 16 + b * 5 >> 5 & 0xff;
462
+ }
463
+ }
464
+ return gray_data;
355
465
  }
356
- EscPosPrinter.prototype.appendImage = function(imgdata, dither) {
357
- var gray_data, mono_data;
358
- var w = imgdata.width;
359
- var h = imgdata.height;
360
- gray_data = _convertToGray(imgdata, w, h);
361
- if (dither == "diffuse") mono_data = _diffuseDither(gray_data, w, h);
362
- else mono_data = _thresholdDither(gray_data, w, h);
363
- w = w + 7 >> 3;
364
- this.orderData += "1d763000";
365
- this.orderData += _numToHexStr(w, 2);
366
- this.orderData += _numToHexStr(h, 2);
367
- for (var i = 0; i < mono_data.length; i++) this.orderData += _numToHexStr(mono_data[i] & 255, 1);
466
+
467
+ // Append image in the order.
468
+ EscPosPrinter.prototype.appendImage = function (imgdata, dither) {
469
+ var gray_data, mono_data;
470
+ var w = imgdata.width;
471
+ var h = imgdata.height;
472
+ gray_data = _convertToGray(imgdata, w, h);
473
+ if (dither == "diffuse") mono_data = _diffuseDither(gray_data, w, h);
474
+ /* if (dither == "threshold") */else mono_data = _thresholdDither(gray_data, w, h);
475
+ w = w + 7 >> 3;
476
+ this.orderData += "1d763000";
477
+ this.orderData += _numToHexStr(w, 2);
478
+ this.orderData += _numToHexStr(h, 2);
479
+ for (var i = 0; i < mono_data.length; i++) this.orderData += _numToHexStr(mono_data[i] & 0xff, 1);
368
480
  };
369
- EscPosPrinter.prototype.enterPageMode = function() {
370
- this.orderData += "1b4c";
481
+
482
+ //////////////////////////////////////////////////
483
+ // Page Mode Commands
484
+ //////////////////////////////////////////////////
485
+
486
+ // Enter page mode.
487
+ EscPosPrinter.prototype.enterPageMode = function () {
488
+ this.orderData += "1b4c";
371
489
  };
372
- EscPosPrinter.prototype.setPrintAreaInPageMode = function(x, y, w, h) {
373
- this.orderData += "1b57";
374
- this.orderData += _numToHexStr(x, 2);
375
- this.orderData += _numToHexStr(y, 2);
376
- this.orderData += _numToHexStr(w, 2);
377
- this.orderData += _numToHexStr(h, 2);
490
+
491
+ // Set print area in page mode.
492
+ // x, y: origin of the print area (the left-top corner of the print area)
493
+ // w, h: width and height of the print area
494
+ EscPosPrinter.prototype.setPrintAreaInPageMode = function (x, y, w, h) {
495
+ this.orderData += "1b57";
496
+ this.orderData += _numToHexStr(x, 2);
497
+ this.orderData += _numToHexStr(y, 2);
498
+ this.orderData += _numToHexStr(w, 2);
499
+ this.orderData += _numToHexStr(h, 2);
378
500
  };
379
- EscPosPrinter.prototype.setPrintDirectionInPageMode = function(dir) {
380
- if (dir >= 0 && dir <= 3) this.orderData += "1b54" + _numToHexStr(dir, 1);
501
+
502
+ // Set print direction in page mode.
503
+ // dir: 0:not rotated; 1:90-degree clockwise rotated;
504
+ // 2:180-degree clockwise rotated; 3:270-degree clockwise rotated
505
+ EscPosPrinter.prototype.setPrintDirectionInPageMode = function (dir) {
506
+ if (dir >= 0 && dir <= 3) this.orderData += "1b54" + _numToHexStr(dir, 1);
381
507
  };
382
- EscPosPrinter.prototype.setAbsolutePrintPositionInPageMode = function(n) {
383
- if (n >= 0 && n <= 65535) this.orderData += "1d24" + _numToHexStr(n, 2);
508
+
509
+ // Set absolute print position in page mode.
510
+ EscPosPrinter.prototype.setAbsolutePrintPositionInPageMode = function (n) {
511
+ if (n >= 0 && n <= 65535) this.orderData += "1d24" + _numToHexStr(n, 2);
384
512
  };
385
- EscPosPrinter.prototype.setRelativePrintPositionInPageMode = function(n) {
386
- if (n >= -32768 && n <= 32767) this.orderData += "1d5c" + _numToHexStr(n, 2);
513
+
514
+ // Set relative print position in page mode.
515
+ EscPosPrinter.prototype.setRelativePrintPositionInPageMode = function (n) {
516
+ if (n >= -32768 && n <= 32767) this.orderData += "1d5c" + _numToHexStr(n, 2);
387
517
  };
388
- EscPosPrinter.prototype.printAndExitPageMode = function() {
389
- this.orderData += "0c";
518
+
519
+ // Print contents in the buffer and exit page mode.
520
+ EscPosPrinter.prototype.printAndExitPageMode = function () {
521
+ this.orderData += "0c";
390
522
  };
391
- EscPosPrinter.prototype.printInPageMode = function() {
392
- this.orderData += "1b0c";
523
+
524
+ // Print contents in the buffer and keep in page mode.
525
+ EscPosPrinter.prototype.printInPageMode = function () {
526
+ this.orderData += "1b0c";
393
527
  };
394
- EscPosPrinter.prototype.clearInPageMode = function() {
395
- this.orderData += "18";
528
+
529
+ // Clear contents in the buffer and keep in page mode.
530
+ EscPosPrinter.prototype.clearInPageMode = function () {
531
+ this.orderData += "18";
396
532
  };
397
- EscPosPrinter.prototype.exitPageMode = function() {
398
- this.orderData += "1b53";
533
+
534
+ // Discard contents in the buffer and exit page mode.
535
+ EscPosPrinter.prototype.exitPageMode = function () {
536
+ this.orderData += "1b53";
399
537
  };
400
- //#endregion
401
- export { ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT, columnWidthWithAlignment, EscPosPrinter as default };
538
+ export default EscPosPrinter;