barcode.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. // https://github.com/alsey/wxbarcode
  3. // 最后一位显示 _ 问题
  4. // https://github.com/alsey/wxbarcode/issues/2
  5. // //ok some type of shift is nessecary if (shifter != -1) { result.push(shifter); result.push(codeValue(chr1));//把这里的chr2改成chr1即可。 }
  6. **/
  7. !(function(){
  8. var CHAR_TILDE = 126;
  9. var CODE_FNC1 = 102;
  10. var SET_STARTA = 103;
  11. var SET_STARTB = 104;
  12. var SET_STARTC = 105;
  13. var SET_SHIFT = 98;
  14. var SET_CODEA = 101;
  15. var SET_CODEB = 100;
  16. var SET_STOP = 106;
  17. var REPLACE_CODES = {
  18. CHAR_TILDE: CODE_FNC1 //~ corresponds to FNC1 in GS1-128 standard
  19. }
  20. var CODESET = {
  21. ANY: 1,
  22. AB: 2,
  23. A: 3,
  24. B: 4,
  25. C: 5
  26. };
  27. function getBytes(str) {
  28. var bytes = [];
  29. for (var i = 0; i < str.length; i++) {
  30. bytes.push(str.charCodeAt(i));
  31. }
  32. return bytes;
  33. }
  34. exports.code128 = function (ctx, text, width, height) {
  35. width = parseInt(width);
  36. height = parseInt(height);
  37. var codes = stringToCode128(text);
  38. var g = new Graphics(ctx, width, height);
  39. var barWeight = g.area.width / ((codes.length - 3) * 11 + 35);
  40. var x = g.area.left;
  41. var y = g.area.top;
  42. for (var i = 0; i < codes.length; i++) {
  43. var c = codes[i];
  44. //two bars at a time: 1 black and 1 white
  45. for (var bar = 0; bar < 8; bar += 2) {
  46. var barW = PATTERNS[c][bar] * barWeight;
  47. // var barH = height - y - this.border;
  48. var barH = height - y;
  49. var spcW = PATTERNS[c][bar + 1] * barWeight;
  50. //no need to draw if 0 width
  51. if (barW > 0) {
  52. g.fillFgRect(x, y, barW, barH);
  53. }
  54. x += barW + spcW;
  55. }
  56. }
  57. ctx.draw();
  58. }
  59. function stringToCode128(text) {
  60. var barc = {
  61. currcs: CODESET.C
  62. };
  63. var bytes = getBytes(text);
  64. //decide starting codeset
  65. var index = bytes[0] == CHAR_TILDE ? 1 : 0;
  66. var csa1 = bytes.length > 0 ? codeSetAllowedFor(bytes[index++]) : CODESET.AB;
  67. var csa2 = bytes.length > 0 ? codeSetAllowedFor(bytes[index++]) : CODESET.AB;
  68. barc.currcs = getBestStartSet(csa1, csa2);
  69. barc.currcs = perhapsCodeC(bytes, barc.currcs);
  70. //if no codeset changes this will end up with bytes.length+3
  71. //start, checksum and stop
  72. var codes = new Array();
  73. switch (barc.currcs) {
  74. case CODESET.A:
  75. codes.push(SET_STARTA);
  76. break;
  77. case CODESET.B:
  78. codes.push(SET_STARTB);
  79. break;
  80. default:
  81. codes.push(SET_STARTC);
  82. break;
  83. }
  84. for (var i = 0; i < bytes.length; i++) {
  85. var b1 = bytes[i]; //get the first of a pair
  86. //should we translate/replace
  87. if (b1 in REPLACE_CODES) {
  88. codes.push(REPLACE_CODES[b1]);
  89. i++ //jump to next
  90. b1 = bytes[i];
  91. }
  92. //get the next in the pair if possible
  93. var b2 = bytes.length > (i + 1) ? bytes[i + 1] : -1;
  94. codes = codes.concat(codesForChar(b1, b2, barc.currcs));
  95. //code C takes 2 chars each time
  96. if (barc.currcs == CODESET.C) i++;
  97. }
  98. //calculate checksum according to Code 128 standards
  99. var checksum = codes[0];
  100. for (var weight = 1; weight < codes.length; weight++) {
  101. checksum += (weight * codes[weight]);
  102. }
  103. codes.push(checksum % 103);
  104. codes.push(SET_STOP);
  105. //encoding should now be complete
  106. return codes;
  107. function getBestStartSet(csa1, csa2) {
  108. //tries to figure out the best codeset
  109. //to start with to get the most compact code
  110. var vote = 0;
  111. vote += csa1 == CODESET.A ? 1 : 0;
  112. vote += csa1 == CODESET.B ? -1 : 0;
  113. vote += csa2 == CODESET.A ? 1 : 0;
  114. vote += csa2 == CODESET.B ? -1 : 0;
  115. //tie goes to B due to my own predudices
  116. return vote > 0 ? CODESET.A : CODESET.B;
  117. }
  118. function perhapsCodeC(bytes, codeset) {
  119. for (var i = 0; i < bytes.length; i++) {
  120. var b = bytes[i]
  121. if ((b < 48 || b > 57) && b != CHAR_TILDE)
  122. return codeset;
  123. }
  124. return CODESET.C;
  125. }
  126. //chr1 is current byte
  127. //chr2 is the next byte to process. looks ahead.
  128. function codesForChar(chr1, chr2, currcs) {
  129. var result = [];
  130. var shifter = -1;
  131. if (charCompatible(chr1, currcs)) {
  132. if (currcs == CODESET.C) {
  133. if (chr2 == -1) {
  134. shifter = SET_CODEB;
  135. currcs = CODESET.B;
  136. }
  137. else if ((chr2 != -1) && !charCompatible(chr2, currcs)) {
  138. //need to check ahead as well
  139. if (charCompatible(chr2, CODESET.A)) {
  140. shifter = SET_CODEA;
  141. currcs = CODESET.A;
  142. }
  143. else {
  144. shifter = SET_CODEB;
  145. currcs = CODESET.B;
  146. }
  147. }
  148. }
  149. }
  150. else {
  151. //if there is a next char AND that next char is also not compatible
  152. if ((chr2 != -1) && !charCompatible(chr2, currcs)) {
  153. //need to switch code sets
  154. switch (currcs) {
  155. case CODESET.A:
  156. shifter = SET_CODEB;
  157. currcs = CODESET.B;
  158. break;
  159. case CODESET.B:
  160. shifter = SET_CODEA;
  161. currcs = CODESET.A;
  162. break;
  163. }
  164. }
  165. else {
  166. //no need to shift code sets, a temporary SHIFT will suffice
  167. shifter = SET_SHIFT;
  168. }
  169. }
  170. //ok some type of shift is nessecary
  171. if (shifter != -1) {
  172. result.push(shifter);
  173. result.push(codeValue(chr1));
  174. }
  175. else {
  176. if (currcs == CODESET.C) {
  177. //include next as well
  178. result.push(codeValue(chr1, chr2));
  179. }
  180. else {
  181. result.push(codeValue(chr1));
  182. }
  183. }
  184. barc.currcs = currcs;
  185. return result;
  186. }
  187. }
  188. //reduce the ascii code to fit into the Code128 char table
  189. function codeValue(chr1, chr2) {
  190. if (typeof chr2 == "undefined") {
  191. return chr1 >= 32 ? chr1 - 32 : chr1 + 64;
  192. }
  193. else {
  194. return parseInt(String.fromCharCode(chr1) + String.fromCharCode(chr2));
  195. }
  196. }
  197. function charCompatible(chr, codeset) {
  198. var csa = codeSetAllowedFor(chr);
  199. if (csa == CODESET.ANY) return true;
  200. //if we need to change from current
  201. if (csa == CODESET.AB) return true;
  202. if (csa == CODESET.A && codeset == CODESET.A) return true;
  203. if (csa == CODESET.B && codeset == CODESET.B) return true;
  204. return false;
  205. }
  206. function codeSetAllowedFor(chr) {
  207. if (chr >= 48 && chr <= 57) {
  208. //0-9
  209. return CODESET.ANY;
  210. }
  211. else if (chr >= 32 && chr <= 95) {
  212. //0-9 A-Z
  213. return CODESET.AB;
  214. }
  215. else {
  216. //if non printable
  217. return chr < 32 ? CODESET.A : CODESET.B;
  218. }
  219. }
  220. var Graphics = function(ctx, width, height) {
  221. this.width = width;
  222. this.height = height;
  223. this.quiet = Math.round(this.width / 40);
  224. this.border_size = 0;
  225. this.padding_width = 0;
  226. this.area = {
  227. width : width - this.padding_width * 2 - this.quiet * 2,
  228. height: height - this.border_size * 2,
  229. top : this.border_size - 4,
  230. left : this.padding_width + this.quiet
  231. };
  232. this.ctx = ctx;
  233. this.fg = "#000000";
  234. this.bg = "#ffffff";
  235. // fill background
  236. this.fillBgRect(0,0, width, height);
  237. // fill center to create border
  238. this.fillBgRect(0, this.border_size, width, height - this.border_size * 2);
  239. }
  240. //use native color
  241. Graphics.prototype._fillRect = function(x, y, width, height, color) {
  242. this.ctx.setFillStyle(color)
  243. this.ctx.fillRect(x, y, width, height)
  244. }
  245. Graphics.prototype.fillFgRect = function(x,y, width, height) {
  246. this._fillRect(x, y, width, height, this.fg);
  247. }
  248. Graphics.prototype.fillBgRect = function(x,y, width, height) {
  249. this._fillRect(x, y, width, height, this.bg);
  250. }
  251. var PATTERNS = [
  252. [2, 1, 2, 2, 2, 2, 0, 0], // 0
  253. [2, 2, 2, 1, 2, 2, 0, 0], // 1
  254. [2, 2, 2, 2, 2, 1, 0, 0], // 2
  255. [1, 2, 1, 2, 2, 3, 0, 0], // 3
  256. [1, 2, 1, 3, 2, 2, 0, 0], // 4
  257. [1, 3, 1, 2, 2, 2, 0, 0], // 5
  258. [1, 2, 2, 2, 1, 3, 0, 0], // 6
  259. [1, 2, 2, 3, 1, 2, 0, 0], // 7
  260. [1, 3, 2, 2, 1, 2, 0, 0], // 8
  261. [2, 2, 1, 2, 1, 3, 0, 0], // 9
  262. [2, 2, 1, 3, 1, 2, 0, 0], // 10
  263. [2, 3, 1, 2, 1, 2, 0, 0], // 11
  264. [1, 1, 2, 2, 3, 2, 0, 0], // 12
  265. [1, 2, 2, 1, 3, 2, 0, 0], // 13
  266. [1, 2, 2, 2, 3, 1, 0, 0], // 14
  267. [1, 1, 3, 2, 2, 2, 0, 0], // 15
  268. [1, 2, 3, 1, 2, 2, 0, 0], // 16
  269. [1, 2, 3, 2, 2, 1, 0, 0], // 17
  270. [2, 2, 3, 2, 1, 1, 0, 0], // 18
  271. [2, 2, 1, 1, 3, 2, 0, 0], // 19
  272. [2, 2, 1, 2, 3, 1, 0, 0], // 20
  273. [2, 1, 3, 2, 1, 2, 0, 0], // 21
  274. [2, 2, 3, 1, 1, 2, 0, 0], // 22
  275. [3, 1, 2, 1, 3, 1, 0, 0], // 23
  276. [3, 1, 1, 2, 2, 2, 0, 0], // 24
  277. [3, 2, 1, 1, 2, 2, 0, 0], // 25
  278. [3, 2, 1, 2, 2, 1, 0, 0], // 26
  279. [3, 1, 2, 2, 1, 2, 0, 0], // 27
  280. [3, 2, 2, 1, 1, 2, 0, 0], // 28
  281. [3, 2, 2, 2, 1, 1, 0, 0], // 29
  282. [2, 1, 2, 1, 2, 3, 0, 0], // 30
  283. [2, 1, 2, 3, 2, 1, 0, 0], // 31
  284. [2, 3, 2, 1, 2, 1, 0, 0], // 32
  285. [1, 1, 1, 3, 2, 3, 0, 0], // 33
  286. [1, 3, 1, 1, 2, 3, 0, 0], // 34
  287. [1, 3, 1, 3, 2, 1, 0, 0], // 35
  288. [1, 1, 2, 3, 1, 3, 0, 0], // 36
  289. [1, 3, 2, 1, 1, 3, 0, 0], // 37
  290. [1, 3, 2, 3, 1, 1, 0, 0], // 38
  291. [2, 1, 1, 3, 1, 3, 0, 0], // 39
  292. [2, 3, 1, 1, 1, 3, 0, 0], // 40
  293. [2, 3, 1, 3, 1, 1, 0, 0], // 41
  294. [1, 1, 2, 1, 3, 3, 0, 0], // 42
  295. [1, 1, 2, 3, 3, 1, 0, 0], // 43
  296. [1, 3, 2, 1, 3, 1, 0, 0], // 44
  297. [1, 1, 3, 1, 2, 3, 0, 0], // 45
  298. [1, 1, 3, 3, 2, 1, 0, 0], // 46
  299. [1, 3, 3, 1, 2, 1, 0, 0], // 47
  300. [3, 1, 3, 1, 2, 1, 0, 0], // 48
  301. [2, 1, 1, 3, 3, 1, 0, 0], // 49
  302. [2, 3, 1, 1, 3, 1, 0, 0], // 50
  303. [2, 1, 3, 1, 1, 3, 0, 0], // 51
  304. [2, 1, 3, 3, 1, 1, 0, 0], // 52
  305. [2, 1, 3, 1, 3, 1, 0, 0], // 53
  306. [3, 1, 1, 1, 2, 3, 0, 0], // 54
  307. [3, 1, 1, 3, 2, 1, 0, 0], // 55
  308. [3, 3, 1, 1, 2, 1, 0, 0], // 56
  309. [3, 1, 2, 1, 1, 3, 0, 0], // 57
  310. [3, 1, 2, 3, 1, 1, 0, 0], // 58
  311. [3, 3, 2, 1, 1, 1, 0, 0], // 59
  312. [3, 1, 4, 1, 1, 1, 0, 0], // 60
  313. [2, 2, 1, 4, 1, 1, 0, 0], // 61
  314. [4, 3, 1, 1, 1, 1, 0, 0], // 62
  315. [1, 1, 1, 2, 2, 4, 0, 0], // 63
  316. [1, 1, 1, 4, 2, 2, 0, 0], // 64
  317. [1, 2, 1, 1, 2, 4, 0, 0], // 65
  318. [1, 2, 1, 4, 2, 1, 0, 0], // 66
  319. [1, 4, 1, 1, 2, 2, 0, 0], // 67
  320. [1, 4, 1, 2, 2, 1, 0, 0], // 68
  321. [1, 1, 2, 2, 1, 4, 0, 0], // 69
  322. [1, 1, 2, 4, 1, 2, 0, 0], // 70
  323. [1, 2, 2, 1, 1, 4, 0, 0], // 71
  324. [1, 2, 2, 4, 1, 1, 0, 0], // 72
  325. [1, 4, 2, 1, 1, 2, 0, 0], // 73
  326. [1, 4, 2, 2, 1, 1, 0, 0], // 74
  327. [2, 4, 1, 2, 1, 1, 0, 0], // 75
  328. [2, 2, 1, 1, 1, 4, 0, 0], // 76
  329. [4, 1, 3, 1, 1, 1, 0, 0], // 77
  330. [2, 4, 1, 1, 1, 2, 0, 0], // 78
  331. [1, 3, 4, 1, 1, 1, 0, 0], // 79
  332. [1, 1, 1, 2, 4, 2, 0, 0], // 80
  333. [1, 2, 1, 1, 4, 2, 0, 0], // 81
  334. [1, 2, 1, 2, 4, 1, 0, 0], // 82
  335. [1, 1, 4, 2, 1, 2, 0, 0], // 83
  336. [1, 2, 4, 1, 1, 2, 0, 0], // 84
  337. [1, 2, 4, 2, 1, 1, 0, 0], // 85
  338. [4, 1, 1, 2, 1, 2, 0, 0], // 86
  339. [4, 2, 1, 1, 1, 2, 0, 0], // 87
  340. [4, 2, 1, 2, 1, 1, 0, 0], // 88
  341. [2, 1, 2, 1, 4, 1, 0, 0], // 89
  342. [2, 1, 4, 1, 2, 1, 0, 0], // 90
  343. [4, 1, 2, 1, 2, 1, 0, 0], // 91
  344. [1, 1, 1, 1, 4, 3, 0, 0], // 92
  345. [1, 1, 1, 3, 4, 1, 0, 0], // 93
  346. [1, 3, 1, 1, 4, 1, 0, 0], // 94
  347. [1, 1, 4, 1, 1, 3, 0, 0], // 95
  348. [1, 1, 4, 3, 1, 1, 0, 0], // 96
  349. [4, 1, 1, 1, 1, 3, 0, 0], // 97
  350. [4, 1, 1, 3, 1, 1, 0, 0], // 98
  351. [1, 1, 3, 1, 4, 1, 0, 0], // 99
  352. [1, 1, 4, 1, 3, 1, 0, 0], // 100
  353. [3, 1, 1, 1, 4, 1, 0, 0], // 101
  354. [4, 1, 1, 1, 3, 1, 0, 0], // 102
  355. [2, 1, 1, 4, 1, 2, 0, 0], // 103
  356. [2, 1, 1, 2, 1, 4, 0, 0], // 104
  357. [2, 1, 1, 2, 3, 2, 0, 0], // 105
  358. [2, 3, 3, 1, 1, 1, 2, 0] // 106
  359. ]
  360. })();