EAN.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. var _createClass = function() {
  3. function defineProperties(target, props) {
  4. for (var i = 0; i < props.length; i++) {
  5. var descriptor = props[i];
  6. descriptor.enumerable = descriptor.enumerable || false;
  7. descriptor.configurable = true;
  8. if ("value" in descriptor) descriptor.writable = true;
  9. Object.defineProperty(target, descriptor.key, descriptor);
  10. }
  11. }
  12. return function(Constructor, protoProps, staticProps) {
  13. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  14. if (staticProps) defineProperties(Constructor, staticProps);
  15. return Constructor;
  16. };
  17. }();
  18. import _constants from './constants'
  19. import _encoder2 from './encoder'
  20. import _Barcode3 from '../Barcode.js'
  21. function _interopRequireDefault(obj) {
  22. return obj && obj.__esModule ? obj : {
  23. default: obj
  24. };
  25. }
  26. function _classCallCheck(instance, Constructor) {
  27. if (!(instance instanceof Constructor)) {
  28. throw new TypeError("Cannot call a class as a function");
  29. }
  30. }
  31. function _possibleConstructorReturn(self, call) {
  32. if (!self) {
  33. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  34. }
  35. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  36. }
  37. function _inherits(subClass, superClass) {
  38. if (typeof superClass !== "function" && superClass !== null) {
  39. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  40. }
  41. subClass.prototype = Object.create(superClass && superClass.prototype, {
  42. constructor: {
  43. value: subClass,
  44. enumerable: false,
  45. writable: true,
  46. configurable: true
  47. }
  48. });
  49. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  50. superClass;
  51. }
  52. // Base class for EAN8 & EAN13
  53. var EAN = function(_Barcode) {
  54. _inherits(EAN, _Barcode);
  55. function EAN(data, options) {
  56. _classCallCheck(this, EAN);
  57. // Make sure the font is not bigger than the space between the guard bars
  58. var _this = _possibleConstructorReturn(this, (EAN.__proto__ || Object.getPrototypeOf(EAN)).call(this, data,
  59. options));
  60. _this.fontSize = !options.flat && options.fontSize > options.width * 10 ? options.width * 10 : options
  61. .fontSize;
  62. // Make the guard bars go down half the way of the text
  63. _this.guardHeight = options.height + _this.fontSize / 2 + options.textMargin;
  64. return _this;
  65. }
  66. _createClass(EAN, [{
  67. key: 'encode',
  68. value: function encode() {
  69. return this.options.flat ? this.encodeFlat() : this.encodeGuarded();
  70. }
  71. }, {
  72. key: 'leftText',
  73. value: function leftText(from, to) {
  74. return this.text.substr(from, to);
  75. }
  76. }, {
  77. key: 'leftEncode',
  78. value: function leftEncode(data, structure) {
  79. return (0, _encoder2)(data, structure);
  80. }
  81. }, {
  82. key: 'rightText',
  83. value: function rightText(from, to) {
  84. return this.text.substr(from, to);
  85. }
  86. }, {
  87. key: 'rightEncode',
  88. value: function rightEncode(data, structure) {
  89. return (0, _encoder2)(data, structure);
  90. }
  91. }, {
  92. key: 'encodeGuarded',
  93. value: function encodeGuarded() {
  94. var textOptions = {
  95. fontSize: this.fontSize
  96. };
  97. var guardOptions = {
  98. height: this.guardHeight
  99. };
  100. return [{
  101. data: _constants.SIDE_BIN,
  102. options: guardOptions
  103. }, {
  104. data: this.leftEncode(),
  105. text: this.leftText(),
  106. options: textOptions
  107. }, {
  108. data: _constants.MIDDLE_BIN,
  109. options: guardOptions
  110. }, {
  111. data: this.rightEncode(),
  112. text: this.rightText(),
  113. options: textOptions
  114. }, {
  115. data: _constants.SIDE_BIN,
  116. options: guardOptions
  117. }];
  118. }
  119. }, {
  120. key: 'encodeFlat',
  121. value: function encodeFlat() {
  122. var data = [_constants.SIDE_BIN, this.leftEncode(), _constants.MIDDLE_BIN, this
  123. .rightEncode(), _constants.SIDE_BIN
  124. ];
  125. return {
  126. data: data.join(''),
  127. text: this.text
  128. };
  129. }
  130. }]);
  131. return EAN;
  132. }(_Barcode3);
  133. export default EAN;