barcode.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. }
  130. else if ((chr2 != -1) && !charCompatible(chr2, currcs)) {
  131. //need to check ahead as well
  132. if (charCompatible(chr2, CODESET.A)) {
  133. shifter = SET_CODEA;
  134. currcs = CODESET.A;
  135. }
  136. else {
  137. shifter = SET_CODEB;
  138. currcs = CODESET.B;
  139. }
  140. }
  141. }
  142. }
  143. else {
  144. //if there is a next char AND that next char is also not compatible
  145. if ((chr2 != -1) && !charCompatible(chr2, currcs)) {
  146. //need to switch code sets
  147. switch (currcs) {
  148. case CODESET.A:
  149. shifter = SET_CODEB;
  150. currcs = CODESET.B;
  151. break;
  152. case CODESET.B:
  153. shifter = SET_CODEA;
  154. currcs = CODESET.A;
  155. break;
  156. }
  157. }
  158. else {
  159. //no need to shift code sets, a temporary SHIFT will suffice
  160. shifter = SET_SHIFT;
  161. }
  162. }
  163. //ok some type of shift is nessecary
  164. if (shifter != -1) {
  165. result.push(shifter);
  166. result.push(codeValue(chr2));
  167. }
  168. else {
  169. if (currcs == CODESET.C) {
  170. //include next as well
  171. result.push(codeValue(chr1, chr2));
  172. }
  173. else {
  174. result.push(codeValue(chr1));
  175. }
  176. }
  177. barc.currcs = currcs;
  178. return result;
  179. }
  180. }
  181. //reduce the ascii code to fit into the Code128 char table
  182. function codeValue(chr1, chr2) {
  183. if (typeof chr2 == "undefined") {
  184. return chr1 >= 32 ? chr1 - 32 : chr1 + 64;
  185. }
  186. else {
  187. return parseInt(String.fromCharCode(chr1) + String.fromCharCode(chr2));
  188. }
  189. }
  190. function charCompatible(chr, codeset) {
  191. var csa = codeSetAllowedFor(chr);
  192. if (csa == CODESET.ANY) return true;
  193. //if we need to change from current
  194. if (csa == CODESET.AB) return true;
  195. if (csa == CODESET.A && codeset == CODESET.A) return true;
  196. if (csa == CODESET.B && codeset == CODESET.B) return true;
  197. return false;
  198. }
  199. function codeSetAllowedFor(chr) {
  200. if (chr >= 48 && chr <= 57) {
  201. //0-9
  202. return CODESET.ANY;
  203. }
  204. else if (chr >= 32 && chr <= 95) {
  205. //0-9 A-Z
  206. return CODESET.AB;
  207. }
  208. else {
  209. //if non printable
  210. return chr < 32 ? CODESET.A : CODESET.B;
  211. }
  212. }
  213. var Graphics = function(ctx, width, height) {
  214. this.width = width;
  215. this.height = height;
  216. this.quiet = Math.round(this.width / 40);
  217. this.border_size = 0;
  218. this.padding_width = 0;
  219. this.area = {
  220. width : width - this.padding_width * 2 - this.quiet * 2,
  221. height: height - this.border_size * 2,
  222. top : this.border_size - 4,
  223. left : this.padding_width + this.quiet
  224. };
  225. this.ctx = ctx;
  226. this.fg = "#000000";
  227. this.bg = "#ffffff";
  228. // fill background
  229. this.fillBgRect(0,0, width, height);
  230. // fill center to create border
  231. this.fillBgRect(0, this.border_size, width, height - this.border_size * 2);
  232. }
  233. //use native color
  234. Graphics.prototype._fillRect = function(x, y, width, height, color) {
  235. this.ctx.setFillStyle(color)
  236. this.ctx.fillRect(x, y, width, height)
  237. }
  238. Graphics.prototype.fillFgRect = function(x,y, width, height) {
  239. this._fillRect(x, y, width, height, this.fg);
  240. }
  241. Graphics.prototype.fillBgRect = function(x,y, width, height) {
  242. this._fillRect(x, y, width, height, this.bg);
  243. }
  244. var PATTERNS = [
  245. [2, 1, 2, 2, 2, 2, 0, 0], // 0
  246. [2, 2, 2, 1, 2, 2, 0, 0], // 1
  247. [2, 2, 2, 2, 2, 1, 0, 0], // 2
  248. [1, 2, 1, 2, 2, 3, 0, 0], // 3
  249. [1, 2, 1, 3, 2, 2, 0, 0], // 4
  250. [1, 3, 1, 2, 2, 2, 0, 0], // 5
  251. [1, 2, 2, 2, 1, 3, 0, 0], // 6
  252. [1, 2, 2, 3, 1, 2, 0, 0], // 7
  253. [1, 3, 2, 2, 1, 2, 0, 0], // 8
  254. [2, 2, 1, 2, 1, 3, 0, 0], // 9
  255. [2, 2, 1, 3, 1, 2, 0, 0], // 10
  256. [2, 3, 1, 2, 1, 2, 0, 0], // 11
  257. [1, 1, 2, 2, 3, 2, 0, 0], // 12
  258. [1, 2, 2, 1, 3, 2, 0, 0], // 13
  259. [1, 2, 2, 2, 3, 1, 0, 0], // 14
  260. [1, 1, 3, 2, 2, 2, 0, 0], // 15
  261. [1, 2, 3, 1, 2, 2, 0, 0], // 16
  262. [1, 2, 3, 2, 2, 1, 0, 0], // 17
  263. [2, 2, 3, 2, 1, 1, 0, 0], // 18
  264. [2, 2, 1, 1, 3, 2, 0, 0], // 19
  265. [2, 2, 1, 2, 3, 1, 0, 0], // 20
  266. [2, 1, 3, 2, 1, 2, 0, 0], // 21
  267. [2, 2, 3, 1, 1, 2, 0, 0], // 22
  268. [3, 1, 2, 1, 3, 1, 0, 0], // 23
  269. [3, 1, 1, 2, 2, 2, 0, 0], // 24
  270. [3, 2, 1, 1, 2, 2, 0, 0], // 25
  271. [3, 2, 1, 2, 2, 1, 0, 0], // 26
  272. [3, 1, 2, 2, 1, 2, 0, 0], // 27
  273. [3, 2, 2, 1, 1, 2, 0, 0], // 28
  274. [3, 2, 2, 2, 1, 1, 0, 0], // 29
  275. [2, 1, 2, 1, 2, 3, 0, 0], // 30
  276. [2, 1, 2, 3, 2, 1, 0, 0], // 31
  277. [2, 3, 2, 1, 2, 1, 0, 0], // 32
  278. [1, 1, 1, 3, 2, 3, 0, 0], // 33
  279. [1, 3, 1, 1, 2, 3, 0, 0], // 34
  280. [1, 3, 1, 3, 2, 1, 0, 0], // 35
  281. [1, 1, 2, 3, 1, 3, 0, 0], // 36
  282. [1, 3, 2, 1, 1, 3, 0, 0], // 37
  283. [1, 3, 2, 3, 1, 1, 0, 0], // 38
  284. [2, 1, 1, 3, 1, 3, 0, 0], // 39
  285. [2, 3, 1, 1, 1, 3, 0, 0], // 40
  286. [2, 3, 1, 3, 1, 1, 0, 0], // 41
  287. [1, 1, 2, 1, 3, 3, 0, 0], // 42
  288. [1, 1, 2, 3, 3, 1, 0, 0], // 43
  289. [1, 3, 2, 1, 3, 1, 0, 0], // 44
  290. [1, 1, 3, 1, 2, 3, 0, 0], // 45
  291. [1, 1, 3, 3, 2, 1, 0, 0], // 46
  292. [1, 3, 3, 1, 2, 1, 0, 0], // 47
  293. [3, 1, 3, 1, 2, 1, 0, 0], // 48
  294. [2, 1, 1, 3, 3, 1, 0, 0], // 49
  295. [2, 3, 1, 1, 3, 1, 0, 0], // 50
  296. [2, 1, 3, 1, 1, 3, 0, 0], // 51
  297. [2, 1, 3, 3, 1, 1, 0, 0], // 52
  298. [2, 1, 3, 1, 3, 1, 0, 0], // 53
  299. [3, 1, 1, 1, 2, 3, 0, 0], // 54
  300. [3, 1, 1, 3, 2, 1, 0, 0], // 55
  301. [3, 3, 1, 1, 2, 1, 0, 0], // 56
  302. [3, 1, 2, 1, 1, 3, 0, 0], // 57
  303. [3, 1, 2, 3, 1, 1, 0, 0], // 58
  304. [3, 3, 2, 1, 1, 1, 0, 0], // 59
  305. [3, 1, 4, 1, 1, 1, 0, 0], // 60
  306. [2, 2, 1, 4, 1, 1, 0, 0], // 61
  307. [4, 3, 1, 1, 1, 1, 0, 0], // 62
  308. [1, 1, 1, 2, 2, 4, 0, 0], // 63
  309. [1, 1, 1, 4, 2, 2, 0, 0], // 64
  310. [1, 2, 1, 1, 2, 4, 0, 0], // 65
  311. [1, 2, 1, 4, 2, 1, 0, 0], // 66
  312. [1, 4, 1, 1, 2, 2, 0, 0], // 67
  313. [1, 4, 1, 2, 2, 1, 0, 0], // 68
  314. [1, 1, 2, 2, 1, 4, 0, 0], // 69
  315. [1, 1, 2, 4, 1, 2, 0, 0], // 70
  316. [1, 2, 2, 1, 1, 4, 0, 0], // 71
  317. [1, 2, 2, 4, 1, 1, 0, 0], // 72
  318. [1, 4, 2, 1, 1, 2, 0, 0], // 73
  319. [1, 4, 2, 2, 1, 1, 0, 0], // 74
  320. [2, 4, 1, 2, 1, 1, 0, 0], // 75
  321. [2, 2, 1, 1, 1, 4, 0, 0], // 76
  322. [4, 1, 3, 1, 1, 1, 0, 0], // 77
  323. [2, 4, 1, 1, 1, 2, 0, 0], // 78
  324. [1, 3, 4, 1, 1, 1, 0, 0], // 79
  325. [1, 1, 1, 2, 4, 2, 0, 0], // 80
  326. [1, 2, 1, 1, 4, 2, 0, 0], // 81
  327. [1, 2, 1, 2, 4, 1, 0, 0], // 82
  328. [1, 1, 4, 2, 1, 2, 0, 0], // 83
  329. [1, 2, 4, 1, 1, 2, 0, 0], // 84
  330. [1, 2, 4, 2, 1, 1, 0, 0], // 85
  331. [4, 1, 1, 2, 1, 2, 0, 0], // 86
  332. [4, 2, 1, 1, 1, 2, 0, 0], // 87
  333. [4, 2, 1, 2, 1, 1, 0, 0], // 88
  334. [2, 1, 2, 1, 4, 1, 0, 0], // 89
  335. [2, 1, 4, 1, 2, 1, 0, 0], // 90
  336. [4, 1, 2, 1, 2, 1, 0, 0], // 91
  337. [1, 1, 1, 1, 4, 3, 0, 0], // 92
  338. [1, 1, 1, 3, 4, 1, 0, 0], // 93
  339. [1, 3, 1, 1, 4, 1, 0, 0], // 94
  340. [1, 1, 4, 1, 1, 3, 0, 0], // 95
  341. [1, 1, 4, 3, 1, 1, 0, 0], // 96
  342. [4, 1, 1, 1, 1, 3, 0, 0], // 97
  343. [4, 1, 1, 3, 1, 1, 0, 0], // 98
  344. [1, 1, 3, 1, 4, 1, 0, 0], // 99
  345. [1, 1, 4, 1, 3, 1, 0, 0], // 100
  346. [3, 1, 1, 1, 4, 1, 0, 0], // 101
  347. [4, 1, 1, 1, 3, 1, 0, 0], // 102
  348. [2, 1, 1, 4, 1, 2, 0, 0], // 103
  349. [2, 1, 1, 2, 1, 4, 0, 0], // 104
  350. [2, 1, 1, 2, 3, 2, 0, 0], // 105
  351. [2, 3, 3, 1, 1, 1, 2, 0] // 106
  352. ]