shared.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getTotalWidthOfEncodings = exports.calculateEncodingAttributes = exports.getBarcodePadding = exports.getEncodingHeight = exports.getMaximumHeightOfEncodings = undefined;
  6. var _merge = require("../help/merge.js");
  7. var _merge2 = _interopRequireDefault(_merge);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function getEncodingHeight(encoding, options) {
  10. return options.height + (options.displayValue && encoding.text.length > 0 ? options.fontSize + options.textMargin : 0) + options.marginTop + options.marginBottom;
  11. }
  12. function getBarcodePadding(textWidth, barcodeWidth, options) {
  13. if (options.displayValue && barcodeWidth < textWidth) {
  14. if (options.textAlign == "center") {
  15. return Math.floor((textWidth - barcodeWidth) / 2);
  16. } else if (options.textAlign == "left") {
  17. return 0;
  18. } else if (options.textAlign == "right") {
  19. return Math.floor(textWidth - barcodeWidth);
  20. }
  21. }
  22. return 0;
  23. }
  24. function calculateEncodingAttributes(encodings, barcodeOptions, context) {
  25. for (var i = 0; i < encodings.length; i++) {
  26. var encoding = encodings[i];
  27. var options = (0, _merge2.default)(barcodeOptions, encoding.options);
  28. // Calculate the width of the encoding
  29. var textWidth;
  30. if (options.displayValue) {
  31. textWidth = messureText(encoding.text, options, context);
  32. } else {
  33. textWidth = 0;
  34. }
  35. var barcodeWidth = encoding.data.length * options.width;
  36. encoding.width = Math.ceil(Math.max(textWidth, barcodeWidth));
  37. encoding.height = getEncodingHeight(encoding, options);
  38. encoding.barcodePadding = getBarcodePadding(textWidth, barcodeWidth, options);
  39. }
  40. }
  41. function getTotalWidthOfEncodings(encodings) {
  42. var totalWidth = 0;
  43. for (var i = 0; i < encodings.length; i++) {
  44. totalWidth += encodings[i].width;
  45. }
  46. return totalWidth;
  47. }
  48. function getMaximumHeightOfEncodings(encodings) {
  49. var maxHeight = 0;
  50. for (var i = 0; i < encodings.length; i++) {
  51. if (encodings[i].height > maxHeight) {
  52. maxHeight = encodings[i].height;
  53. }
  54. }
  55. return maxHeight;
  56. }
  57. function messureText(string, options, context) {
  58. var ctx;
  59. if (context) {
  60. ctx = context;
  61. } else if (typeof document !== "undefined") {
  62. ctx = document.createElement("canvas").getContext("2d");
  63. } else {
  64. // If the text cannot be messured we will return 0.
  65. // This will make some barcode with big text render incorrectly
  66. return 0;
  67. }
  68. ctx.font = options.fontOptions + " " + options.fontSize + "px " + options.font;
  69. // Calculate the width of the encoding
  70. var measureTextResult = ctx.measureText(string);
  71. if (!measureTextResult) {
  72. // Some implementations don't implement measureText and return undefined.
  73. // If the text cannot be measured we will return 0.
  74. // This will make some barcode with big text render incorrectly
  75. return 0;
  76. }
  77. var size = measureTextResult.width;
  78. return size;
  79. }
  80. exports.getMaximumHeightOfEncodings = getMaximumHeightOfEncodings;
  81. exports.getEncodingHeight = getEncodingHeight;
  82. exports.getBarcodePadding = getBarcodePadding;
  83. exports.calculateEncodingAttributes = calculateEncodingAttributes;
  84. exports.getTotalWidthOfEncodings = getTotalWidthOfEncodings;