shared.js 2.6 KB

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