jpc_helper_common.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. let JV = require('../jpc_value_define');
  2. let JpcCommonHelper = {
  3. commonConstant: {},
  4. getResultByID: function (KeyID, collectionList) {
  5. let rst = null;
  6. if (KeyID) {
  7. for (let i = 0; i < collectionList.length; i++) {
  8. let collection = collectionList[i];
  9. if (collection && collection instanceof Array) {
  10. for (let j = 0; j < collection.length; j++) {
  11. if (collection[j][JV.PROP_ID] === KeyID) {
  12. rst = collection[j];
  13. break;
  14. }
  15. }
  16. if (rst) break;
  17. }
  18. }
  19. }
  20. return rst;
  21. },
  22. getFont: function(fontName, dftFonts, rptTpl) {
  23. let me = this, list = [];
  24. if (rptTpl) list.push(rptTpl[JV.NODE_FONT_COLLECTION]);
  25. list.push(dftFonts);
  26. return me.getResultByID(fontName, list);
  27. },
  28. getStyle: function(styleName, dftStyles, rptTpl) {
  29. let me = this, list = [];
  30. if (rptTpl) list.push(rptTpl[JV.NODE_STYLE_COLLECTION]);
  31. list.push(dftStyles);
  32. return me.getResultByID(styleName, list);
  33. },
  34. getControl: function(controlName, dftControls, rptTpl) {
  35. let me = this, list = [];
  36. if (rptTpl) list.push(rptTpl[JV.NODE_CONTROL_COLLECTION]);
  37. list.push(dftControls);
  38. return me.getResultByID(controlName, list);
  39. },
  40. getLayoutAlignment: function(alignStr) {
  41. let rst = JV.LAYOUT.indexOf(alignStr);
  42. if (rst < 0) rst = JV.LAYOUT_FULFILL;
  43. return rst;
  44. },
  45. getPosCalculationType: function (typeStr) {
  46. let rst = JV.CAL_TYPE.indexOf(typeStr);
  47. if (rst < 0) rst = JV.CAL_TYPE_ABSTRACT;
  48. return rst;
  49. },
  50. getBoolean: function(bStr) {
  51. let rst = false;
  52. if (bStr !== null && bStr !== undefined) {
  53. let valType = typeof(bStr);
  54. if (valType === "boolean") {
  55. rst = bStr;
  56. } else if (valType === "string") {
  57. let tS = bStr.toUpperCase();
  58. rst = (tS === "T" || tS === "TRUE" || tS === "YES" || tS === "Y");
  59. } else if (valType === "number") {
  60. rst = (bStr === 1);
  61. }
  62. }
  63. return rst;
  64. },
  65. getScreenDPI: function() {
  66. let me = this, arrDPI = [];
  67. if (!me.commonConstant.resolution) {
  68. arrDPI = [96,96];
  69. // arrDPI = [100,100];
  70. me.commonConstant.resolution = arrDPI;
  71. } else {
  72. arrDPI = me.commonConstant.resolution;
  73. }
  74. return arrDPI;
  75. },
  76. getUnitFactor: function(rptTpl) {
  77. let me = this;
  78. return me.translateUnit(rptTpl[JV.NODE_MAIN_INFO][JV.PROP_UNITS]);
  79. },
  80. translateUnit: function(unitStr) {
  81. let me = this, rst = 1.0;
  82. if (unitStr) {
  83. let resolution = me.getScreenDPI();
  84. if (JV.MEASUREMENT.PIXEL.indexOf(unitStr) >= 0) {
  85. rst = 1.0;
  86. } else if (JV.MEASUREMENT.CM.indexOf(unitStr) >= 0) {
  87. rst = 1.0 * resolution[0] / 2.54;
  88. } else if (JV.MEASUREMENT.INCH.indexOf(unitStr) >= 0) {
  89. rst = 1.0 * resolution[0];
  90. }
  91. }
  92. return rst;
  93. },
  94. getPageSize: function (rptTpl) {
  95. let size = null;
  96. let sizeStr = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE];
  97. let sizeIdx = JV.PAGES_SIZE_STR.indexOf(sizeStr);
  98. if (sizeIdx >= 0) {
  99. size = JV.PAGES_SIZE[sizeIdx].slice(0);
  100. } else if (sizeStr === JV.PAGE_SELF_DEFINE) {
  101. //
  102. } else {
  103. size = JV.SIZE_A4.slice(0);
  104. }
  105. let page_orientation = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION];
  106. if (page_orientation === JV.ORIENTATION_LANDSCAPE || page_orientation === JV.ORIENTATION_LANDSCAPE_CHN) {
  107. //swap x,y
  108. let tmp = size[0];
  109. size[0] = size[1];
  110. size[1] = tmp;
  111. }
  112. return size;
  113. },
  114. getReportArea: function(rptTpl, unitFactor) {
  115. let me = this, resolution = me.getScreenDPI(), rst = [], size = me.getPageSize(rptTpl);
  116. size[0] = resolution[0] * size[0];
  117. size[1] = resolution[0] * size[1];
  118. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]);
  119. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]);
  120. rst.push(size[0] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT]);
  121. rst.push(size[1] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]);
  122. return rst;
  123. },
  124. getSegIdxByPageIdx: function(page, page_seg_map) {
  125. let rst = -1;
  126. for (let pIdx = 0; pIdx < page_seg_map.length; pIdx++) {
  127. if (page_seg_map[pIdx][0] === page) {
  128. rst = page_seg_map[pIdx][1];
  129. break;
  130. }
  131. }
  132. return rst;
  133. },
  134. getStringLinesInArea: function(area, strVal, pdfDoc) {
  135. let areaWidth = area[JV.PROP_RIGHT] - area[JV.PROP_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] - 2;
  136. let txtWidth = pdfDoc.widthOfString(strVal);
  137. let rst = parseInt(txtWidth / areaWidth);
  138. if (txtWidth % areaWidth > 0) {
  139. rst++;
  140. }
  141. if (rst === 0) rst = 1; //即使是空字符串,也得有一行啊
  142. return rst;
  143. //备注: 其实是想用canvas的,但node canvas装起来麻烦,暂时用PDF Kit来顶用一下,以后换新方法再用
  144. },
  145. splitString: function (area, strVal, pdfDoc) {
  146. let rst = [];
  147. let areaWidth = area[JV.PROP_RIGHT] - area[JV.PROP_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] - 2;
  148. let preSIdx = 0, txtWidth = 0;
  149. for (let sIdx = 1; sIdx <= strVal.length; sIdx++) {
  150. txtWidth = pdfDoc.widthOfString(strVal.substr(preSIdx, sIdx - preSIdx));
  151. if (txtWidth > areaWidth) {
  152. rst.push(strVal.substr(preSIdx, sIdx - preSIdx - 1));
  153. preSIdx = sIdx - 1;
  154. }
  155. if (sIdx === strVal.length) {
  156. rst.push(strVal.substr(preSIdx, sIdx - preSIdx));
  157. }
  158. }
  159. return rst;
  160. }
  161. };
  162. module.exports = JpcCommonHelper;