barcode.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.canvas) {
  82. context = this.canvas.getContext('2d');
  83. var ratio = this.ratio || 1
  84. var canvasWidth = this.settings.width * ratio
  85. var canvasHeight = this.settings.height * ratio
  86. this.canvas.width = canvasWidth
  87. this.canvas.height = canvasHeight
  88. context.scale(ratio, ratio)
  89. // context.fillStyle = '#ffffff'
  90. context.fillRect(0, 0, this.settings.width, this.settings.height)
  91. this.clear(context);
  92. context.fillStyle = this.settings.color;
  93. left = this.settings.number && this.settings.prefix ? this.settings.width * layout.prefix_offset : 0;
  94. lines = code.split("");
  95. context.fillRect(left, 0, item_width, border_height);
  96. left = left + item_width * 2;
  97. context.fillRect(left, 0, item_width, border_height);
  98. left = left + item_width;
  99. i = 0;
  100. while (i < 42) {
  101. if (lines[i] === "1") {
  102. context.fillRect(left, 0, Math.floor(item_width) + 1, height);
  103. }
  104. left = left + item_width;
  105. i++;
  106. }
  107. left = left + item_width;
  108. context.fillRect(left, 0, item_width, border_height);
  109. left = left + item_width * 2;
  110. context.fillRect(left, 0, item_width, border_height);
  111. left = left + item_width * 2;
  112. i = 42;
  113. while (i < 84) {
  114. if (lines[i] === "1") {
  115. context.fillRect(left, 0, Math.floor(item_width) + 1, height);
  116. }
  117. left = left + item_width;
  118. i++;
  119. }
  120. context.fillRect(left, 0, item_width, border_height);
  121. left = left + item_width * 2;
  122. context.fillRect(left, 0, item_width, border_height);
  123. if (this.settings.number) {
  124. context.font = layout.font_size * height + "px monospace";
  125. prefix = this.number.substr(0, 1);
  126. if (this.settings.prefix) {
  127. context.fillText(prefix, 0, border_height * layout.font_y);
  128. }
  129. offset = item_width * layout.text_offset + (this.settings.prefix ? layout.prefix_offset * this.settings.width : 0);
  130. chars = this.number.substr(1, 6).split("");
  131. chars.forEach(function(value, key) {
  132. context.fillText(value, offset, border_height * layout.font_y);
  133. return offset += layout.font_stretch * width;
  134. });
  135. offset = 49 * item_width + (this.settings.prefix ? layout.prefix_offset * this.settings.width : 0) + layout.text_offset;
  136. chars = this.number.substr(7).split("")
  137. chars.forEach(function(value, key) {
  138. context.fillText(value, offset, border_height * layout.font_y);
  139. return offset += layout.font_stretch * width;
  140. });
  141. }
  142. if (this.settings.debug) {
  143. for (x = _i = 0, _ref = item_width * 2; _ref > 0 ? _i <= width : _i >= width; x = _i += _ref) {
  144. context.beginPath();
  145. context.rect(x, height * 0.4, item_width, height * 0.1);
  146. context.fillStyle = 'red';
  147. context.fill();
  148. }
  149. }
  150. return this.settings.onSuccess.call();
  151. } else {
  152. return this.settings.onError.call();
  153. }
  154. };
  155. EAN13.prototype.generateCheckDigit = function(number) {
  156. var chars, counter;
  157. counter = 0;
  158. chars = number.split("");
  159. chars.forEach(function(value, key) {
  160. if (key % 2 === 0) {
  161. return counter += parseInt(value, 10);
  162. } else {
  163. return counter += 3 * parseInt(value, 10);
  164. }
  165. });
  166. return 10 - (counter % 10) % 10;
  167. };
  168. EAN13.prototype.validate = function() {
  169. return parseInt(this.number.slice(-1), 10) === this.generateCheckDigit(this.number.slice(0, -1));
  170. };
  171. function EAN13(canvas, ratio, number, options) {
  172. var option;
  173. this.canvas = canvas;
  174. this.ratio = ratio;
  175. this.number = number;
  176. this.settings = {
  177. width: 200,
  178. height: 100,
  179. number: true,
  180. prefix: true,
  181. color: "black",
  182. debug: false,
  183. onValid: function() {},
  184. onInvalid: function() {},
  185. onSuccess: function() {},
  186. onError: function() {}
  187. };
  188. if (options) {
  189. for (option in options) {
  190. this.settings[option] = options[option];
  191. }
  192. }
  193. this._name = pluginName;
  194. this.init();
  195. }
  196. return EAN13;
  197. })();
  198. if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
  199. module.exports = EAN13;
  200. }