barcode.js 9.8 KB

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