barcode.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2014 Johannes Mittendorfer (http://johannes-mittendorfer.com)
  3. * Licensed under the MIT License (LICENSE.txt).
  4. *
  5. * Version 2.1.1
  6. * Build 2014-10-07
  7. */
  8. var EAN13, pluginName;
  9. pluginName = null;
  10. "use strict";
  11. EAN13 = (function() {
  12. EAN13.prototype.settings = {};
  13. EAN13.prototype.init = function() {
  14. var checkDigit, code;
  15. if (this.number.length === 12) {
  16. checkDigit = this.generateCheckDigit(this.number);
  17. this.number += checkDigit;
  18. }
  19. if (this.number.length === 13) {
  20. if (this.validate()) {
  21. this.settings.onValid.call();
  22. } else {
  23. this.settings.onInvalid.call();
  24. }
  25. code = this.getCode();
  26. return this.draw(code);
  27. } else {
  28. return this.settings.onError.call();
  29. }
  30. };
  31. EAN13.prototype.getCode = function() {
  32. var c_encoding, code, countries, i, parts, raw_number, x, y, z;
  33. x = ["0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"];
  34. y = ["0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111"];
  35. z = ["1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100"];
  36. countries = ["xxxxxx", "xxyxyy", "xxyyxy", "xxyyyx", "xyxxyy", "xyyxxy", "xyyyxx", "xyxyxy", "xyxyyx", "xyyxyx"];
  37. code = "";
  38. c_encoding = countries[parseInt(this.number.substr(0, 1), 10)].split("");
  39. raw_number = this.number.substr(1);
  40. parts = raw_number.split("");
  41. i = 0;
  42. while (i < 6) {
  43. if (c_encoding[i] === "x") {
  44. code += x[parts[i]];
  45. } else {
  46. code += y[parts[i]];
  47. }
  48. i++;
  49. }
  50. i = 6;
  51. while (i < 12) {
  52. code += z[parts[i]];
  53. i++;
  54. }
  55. return code;
  56. };
  57. EAN13.prototype.clear = function(context) {
  58. return context.clearRect(0, 0, this.settings.width, this.settings.height);
  59. };
  60. EAN13.prototype.draw = function(code) {
  61. var border_height, chars, context, height, i, item_width, layout, left, lines, offset, prefix, width, x, _i, _ref;
  62. layout = {
  63. prefix_offset: 0.06,
  64. font_stretch: 0.073,
  65. border_line_height_number: 0.9,
  66. border_line_height: 1,
  67. line_height: 0.9,
  68. font_size: 0.15,
  69. font_y: 1.03,
  70. text_offset: 4.5
  71. };
  72. width = (this.settings.prefix ? this.settings.width - (this.settings.width * layout.prefix_offset) : this.settings.width);
  73. if (this.settings.number) {
  74. border_height = layout.border_line_height_number * this.settings.height;
  75. height = layout.line_height * border_height;
  76. } else {
  77. border_height = layout.border_line_height * this.settings.height;
  78. height = border_height;
  79. }
  80. item_width = width / 95;
  81. if (this.id) {
  82. context = wx.createCanvasContext(this.id, this.ctx);
  83. this.clear(context);
  84. context.setFillStyle(this.settings.color);
  85. left = this.settings.number && this.settings.prefix ? this.settings.width * layout.prefix_offset : 0;
  86. lines = code.split("");
  87. context.fillRect(left, 0, item_width, border_height);
  88. left = left + item_width * 2;
  89. context.fillRect(left, 0, item_width, border_height);
  90. left = left + item_width;
  91. i = 0;
  92. while (i < 42) {
  93. if (lines[i] === "1") {
  94. context.fillRect(left, 0, Math.floor(item_width) + 1, height);
  95. }
  96. left = left + item_width;
  97. i++;
  98. }
  99. left = left + item_width;
  100. context.fillRect(left, 0, item_width, border_height);
  101. left = left + item_width * 2;
  102. context.fillRect(left, 0, item_width, border_height);
  103. left = left + item_width * 2;
  104. i = 42;
  105. while (i < 84) {
  106. if (lines[i] === "1") {
  107. context.fillRect(left, 0, Math.floor(item_width) + 1, height);
  108. }
  109. left = left + item_width;
  110. i++;
  111. }
  112. context.fillRect(left, 0, item_width, border_height);
  113. left = left + item_width * 2;
  114. context.fillRect(left, 0, item_width, border_height);
  115. if (this.settings.number) {
  116. context.setFontSize(layout.font_size * height + "px monospace");
  117. prefix = this.number.substr(0, 1);
  118. if (this.settings.prefix) {
  119. context.fillText(prefix, 0, border_height * layout.font_y);
  120. }
  121. offset = item_width * layout.text_offset + (this.settings.prefix ? layout.prefix_offset * this.settings.width : 0);
  122. chars = this.number.substr(1, 6).split("");
  123. chars.forEach(function(value, key) {
  124. context.fillText(value, offset, border_height * layout.font_y);
  125. return offset += layout.font_stretch * width;
  126. });
  127. offset = 49 * item_width + (this.settings.prefix ? layout.prefix_offset * this.settings.width : 0) + layout.text_offset;
  128. chars = this.number.substr(7).split("")
  129. chars.forEach(function(value, key) {
  130. context.fillText(value, offset, border_height * layout.font_y);
  131. return offset += layout.font_stretch * width;
  132. });
  133. }
  134. if (this.settings.debug) {
  135. for (x = _i = 0, _ref = item_width * 2; _ref > 0 ? _i <= width : _i >= width; x = _i += _ref) {
  136. context.beginPath();
  137. context.rect(x, height * 0.4, item_width, height * 0.1);
  138. context.setFillStyle('red');
  139. context.fill();
  140. }
  141. }
  142. context.draw()
  143. return this.settings.onSuccess.call();
  144. } else {
  145. return this.settings.onError.call();
  146. }
  147. };
  148. EAN13.prototype.generateCheckDigit = function(number) {
  149. var chars, counter;
  150. counter = 0;
  151. chars = number.split("");
  152. chars.forEach(function(value, key) {
  153. if (key % 2 === 0) {
  154. return counter += parseInt(value, 10);
  155. } else {
  156. return counter += 3 * parseInt(value, 10);
  157. }
  158. });
  159. return 10 - (counter % 10) % 10;
  160. };
  161. EAN13.prototype.validate = function() {
  162. return parseInt(this.number.slice(-1), 10) === this.generateCheckDigit(this.number.slice(0, -1));
  163. };
  164. function EAN13(id, number, options, ctx) {
  165. var option;
  166. this.id = id;
  167. this.number = number;
  168. this.ctx = ctx
  169. this.settings = {
  170. width: 200,
  171. height: 100,
  172. number: true,
  173. prefix: true,
  174. color: "black",
  175. debug: false,
  176. onValid: function() {},
  177. onInvalid: function() {},
  178. onSuccess: function() {},
  179. onError: function() {}
  180. };
  181. if (options) {
  182. for (option in options) {
  183. this.settings[option] = options[option];
  184. }
  185. }
  186. this._name = pluginName;
  187. this.init();
  188. }
  189. return EAN13;
  190. })();
  191. if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
  192. module.exports = EAN13;
  193. }