EAN.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6. var _constants = require('./constants');
  7. var _encoder = require('./encoder');
  8. var _encoder2 = _interopRequireDefault(_encoder);
  9. var _Barcode2 = require('../Barcode');
  10. var _Barcode3 = _interopRequireDefault(_Barcode2);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  13. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  14. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  15. // Base class for EAN8 & EAN13
  16. var EAN = function (_Barcode) {
  17. _inherits(EAN, _Barcode);
  18. function EAN(data, options) {
  19. _classCallCheck(this, EAN);
  20. // Make sure the font is not bigger than the space between the guard bars
  21. var _this = _possibleConstructorReturn(this, (EAN.__proto__ || Object.getPrototypeOf(EAN)).call(this, data, options));
  22. _this.fontSize = !options.flat && options.fontSize > options.width * 10 ? options.width * 10 : options.fontSize;
  23. // Make the guard bars go down half the way of the text
  24. _this.guardHeight = options.height + _this.fontSize / 2 + options.textMargin;
  25. return _this;
  26. }
  27. _createClass(EAN, [{
  28. key: 'encode',
  29. value: function encode() {
  30. return this.options.flat ? this.encodeFlat() : this.encodeGuarded();
  31. }
  32. }, {
  33. key: 'leftText',
  34. value: function leftText(from, to) {
  35. return this.text.substr(from, to);
  36. }
  37. }, {
  38. key: 'leftEncode',
  39. value: function leftEncode(data, structure) {
  40. return (0, _encoder2.default)(data, structure);
  41. }
  42. }, {
  43. key: 'rightText',
  44. value: function rightText(from, to) {
  45. return this.text.substr(from, to);
  46. }
  47. }, {
  48. key: 'rightEncode',
  49. value: function rightEncode(data, structure) {
  50. return (0, _encoder2.default)(data, structure);
  51. }
  52. }, {
  53. key: 'encodeGuarded',
  54. value: function encodeGuarded() {
  55. var textOptions = { fontSize: this.fontSize };
  56. var guardOptions = { height: this.guardHeight };
  57. return [{ data: _constants.SIDE_BIN, options: guardOptions }, { data: this.leftEncode(), text: this.leftText(), options: textOptions }, { data: _constants.MIDDLE_BIN, options: guardOptions }, { data: this.rightEncode(), text: this.rightText(), options: textOptions }, { data: _constants.SIDE_BIN, options: guardOptions }];
  58. }
  59. }, {
  60. key: 'encodeFlat',
  61. value: function encodeFlat() {
  62. var data = [_constants.SIDE_BIN, this.leftEncode(), _constants.MIDDLE_BIN, this.rightEncode(), _constants.SIDE_BIN];
  63. return {
  64. data: data.join(''),
  65. text: this.text
  66. };
  67. }
  68. }]);
  69. return EAN;
  70. }(_Barcode3.default);
  71. exports.default = EAN;