jpc_helper_common.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // getBrowerScreenDPI: function() {
  77. // let me = this, arrDPI = [];
  78. // if (!me.commonConstant.resolution) {
  79. // if (window) {
  80. // if (window.screen.deviceXDPI != undefined) {
  81. // arrDPI.push(window.screen.deviceXDPI);
  82. // arrDPI.push(window.screen.deviceYDPI);
  83. // } else {
  84. // let tmpNode = document.createElement("DIV");
  85. // tmpNode.style.cssText = "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
  86. // document.body.appendChild(tmpNode);
  87. // arrDPI.push(parseInt(tmpNode.offsetWidth));
  88. // arrDPI.push(parseInt(tmpNode.offsetHeight));
  89. // tmpNode.parentNode.removeChild(tmpNode);
  90. // }
  91. // } else {
  92. // arrDPI = [96,96];
  93. // }
  94. // me.commonConstant.resolution = arrDPI;
  95. // } else {
  96. // arrDPI = me.commonConstant.resolution;
  97. // }
  98. // return arrDPI;
  99. // },
  100. getUnitFactor: function(rptTpl) {
  101. let me = this;
  102. return me.translateUnit(rptTpl[JV.NODE_MAIN_INFO][JV.PROP_UNITS]);
  103. },
  104. translateUnit: function(unitStr) {
  105. let me = this, rst = 1.0;
  106. if (unitStr) {
  107. let resolution = me.getScreenDPI();
  108. if (JV.MEASUREMENT.PIXEL.indexOf(unitStr) >= 0) {
  109. rst = 1.0;
  110. } else if (JV.MEASUREMENT.CM.indexOf(unitStr) >= 0) {
  111. rst = 1.0 * resolution[0] / 2.54;
  112. } else if (JV.MEASUREMENT.INCH.indexOf(unitStr) >= 0) {
  113. rst = 1.0 * resolution[0];
  114. }
  115. }
  116. return rst;
  117. },
  118. getPageSize: function (rptTpl) {
  119. let size = null;
  120. let sizeStr = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE];
  121. let sizeIdx = JV.PAGES_SIZE_STR.indexOf(sizeStr);
  122. if (sizeIdx >= 0) {
  123. size = JV.PAGES_SIZE[sizeIdx].slice(0);
  124. } else if (sizeStr === JV.PAGE_SELF_DEFINE) {
  125. //
  126. } else {
  127. size = JV.SIZE_A4.slice(0);
  128. }
  129. let page_orientation = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION];
  130. if (page_orientation === JV.ORIENTATION_LANDSCAPE || page_orientation === JV.ORIENTATION_LANDSCAPE_CHN) {
  131. //swap x,y
  132. let tmp = size[0];
  133. size[0] = size[1];
  134. size[1] = tmp;
  135. }
  136. return size;
  137. },
  138. getReportArea: function(rptTpl, unitFactor) {
  139. let me = this, resolution = me.getScreenDPI(), rst = [], size = me.getPageSize(rptTpl);
  140. size[0] = resolution[0] * size[0];
  141. size[1] = resolution[0] * size[1];
  142. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]);
  143. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]);
  144. rst.push(size[0] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT]);
  145. rst.push(size[1] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]);
  146. return rst;
  147. },
  148. getSegIdxByPageIdx: function(page, page_seg_map) {
  149. let rst = -1;
  150. for (let pIdx = 0; pIdx < page_seg_map.length; pIdx++) {
  151. if (page_seg_map[pIdx][0] === page) {
  152. rst = page_seg_map[pIdx][1];
  153. break;
  154. }
  155. }
  156. return rst;
  157. },
  158. getStringLinesInArea: function(area, strVal, pdfDoc) {
  159. 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;
  160. let txtWidth = pdfDoc.widthOfString(strVal);
  161. let rst = parseInt(txtWidth / areaWidth);
  162. if (txtWidth % areaWidth > 0) {
  163. rst++;
  164. }
  165. if (rst === 0) rst = 1; //即使是空字符串,也得有一行啊
  166. return rst;
  167. //备注: 其实是想用canvas的,但node canvas装起来麻烦,暂时用PDF Kit来顶用一下,以后换新方法再用
  168. },
  169. splitString: function (area, strVal, pdfDoc) {
  170. let rst = [];
  171. 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;
  172. let preSIdx = 0, txtWidth = 0;
  173. for (let sIdx = 1; sIdx <= strVal.length; sIdx++) {
  174. txtWidth = pdfDoc.widthOfString(strVal.substr(preSIdx, sIdx - preSIdx));
  175. if (txtWidth > areaWidth) {
  176. rst.push(strVal.substr(preSIdx, sIdx - preSIdx - 1));
  177. preSIdx = sIdx - 1;
  178. }
  179. if (sIdx === strVal.length) {
  180. rst.push(strVal.substr(preSIdx, sIdx - preSIdx));
  181. }
  182. }
  183. return rst;
  184. }
  185. };
  186. module.exports = JpcCommonHelper;