jpc_helper_common.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. let JV = require('../jpc_value_define');
  2. let stringUtil = require('../../../../public/stringUtil');
  3. let JpcCommonHelper = {
  4. commonConstant: {},
  5. getResultByID: function (KeyID, collectionList) {
  6. let rst = null;
  7. if (KeyID) {
  8. for (let i = 0; i < collectionList.length; i++) {
  9. let collection = collectionList[i];
  10. if (collection && collection instanceof Array) {
  11. for (let j = 0; j < collection.length; j++) {
  12. if (collection[j][JV.PROP_ID] === KeyID) {
  13. rst = collection[j];
  14. break;
  15. }
  16. }
  17. if (rst) break;
  18. }
  19. }
  20. }
  21. return rst;
  22. },
  23. getFont: function(fontName, dftFonts, rptTpl) {
  24. let me = this, list = [];
  25. if (rptTpl) list.push(rptTpl[JV.NODE_FONT_COLLECTION]);
  26. list.push(dftFonts);
  27. return me.getResultByID(fontName, list);
  28. },
  29. getStyle: function(styleName, dftStyles, rptTpl) {
  30. let me = this, list = [];
  31. if (rptTpl) list.push(rptTpl[JV.NODE_STYLE_COLLECTION]);
  32. list.push(dftStyles);
  33. return me.getResultByID(styleName, list);
  34. },
  35. getControl: function(controlName, dftControls, rptTpl) {
  36. let me = this, list = [];
  37. if (rptTpl) list.push(rptTpl[JV.NODE_CONTROL_COLLECTION]);
  38. list.push(dftControls);
  39. return me.getResultByID(controlName, list);
  40. },
  41. getLayoutAlignment: function(alignStr) {
  42. let rst = JV.LAYOUT.indexOf(alignStr);
  43. if (rst < 0) rst = JV.LAYOUT_FULFILL;
  44. return rst;
  45. },
  46. getPosCalculationType: function (typeStr) {
  47. let rst = JV.CAL_TYPE.indexOf(typeStr);
  48. if (rst < 0) rst = JV.CAL_TYPE_ABSTRACT;
  49. return rst;
  50. },
  51. getBoolean: function(bStr) {
  52. let rst = false;
  53. if (bStr !== null && bStr !== undefined) {
  54. let valType = typeof(bStr);
  55. if (valType === "boolean") {
  56. rst = bStr;
  57. } else if (valType === "string") {
  58. let tS = bStr.toUpperCase();
  59. rst = (tS === "T" || tS === "TRUE" || tS === "YES" || tS === "Y");
  60. } else if (valType === "number") {
  61. rst = (bStr === 1);
  62. }
  63. }
  64. return rst;
  65. },
  66. getScreenDPI: function() {
  67. let me = this, arrDPI = [];
  68. if (!me.commonConstant.resolution) {
  69. arrDPI = [96,96];
  70. // arrDPI = [100,100];
  71. me.commonConstant.resolution = arrDPI;
  72. } else {
  73. arrDPI = me.commonConstant.resolution;
  74. }
  75. return arrDPI;
  76. },
  77. getUnitFactor: function(rptTpl) {
  78. let me = this;
  79. return me.translateUnit(rptTpl[JV.NODE_MAIN_INFO][JV.PROP_UNITS]);
  80. },
  81. translateUnit: function(unitStr) {
  82. let me = this, rst = 1.0;
  83. if (unitStr) {
  84. let resolution = me.getScreenDPI();
  85. if (JV.MEASUREMENT.PIXEL.indexOf(unitStr) >= 0) {
  86. rst = 1.0;
  87. } else if (JV.MEASUREMENT.CM.indexOf(unitStr) >= 0) {
  88. rst = 1.0 * resolution[0] / 2.54;
  89. } else if (JV.MEASUREMENT.INCH.indexOf(unitStr) >= 0) {
  90. rst = 1.0 * resolution[0];
  91. }
  92. }
  93. return rst;
  94. },
  95. getPageSize: function (rptTpl) {
  96. let size = null;
  97. let sizeStr = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE];
  98. let sizeIdx = JV.PAGES_SIZE_STR.indexOf(sizeStr);
  99. if (sizeIdx >= 0) {
  100. size = JV.PAGES_SIZE[sizeIdx].slice(0);
  101. } else if (sizeStr === JV.PAGE_SELF_DEFINE) {
  102. //
  103. } else {
  104. size = JV.SIZE_A4.slice(0);
  105. }
  106. let page_orientation = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION];
  107. if (page_orientation === JV.ORIENTATION_LANDSCAPE || page_orientation === JV.ORIENTATION_LANDSCAPE_CHN) {
  108. //swap x,y
  109. let tmp = size[0];
  110. size[0] = size[1];
  111. size[1] = tmp;
  112. }
  113. return size;
  114. },
  115. getReportArea: function(rptTpl, unitFactor) {
  116. let me = this, resolution = me.getScreenDPI(), rst = [], size = me.getPageSize(rptTpl);
  117. size[0] = resolution[0] * size[0];
  118. size[1] = resolution[0] * size[1];
  119. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]);
  120. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]);
  121. rst.push(size[0] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT]);
  122. rst.push(size[1] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]);
  123. return rst;
  124. },
  125. getSegIdxByPageIdx: function(page, page_seg_map) {
  126. let rst = -1;
  127. for (let pIdx = 0; pIdx < page_seg_map.length; pIdx++) {
  128. if (page_seg_map[pIdx][0] === page) {
  129. rst = page_seg_map[pIdx][1];
  130. break;
  131. }
  132. }
  133. return rst;
  134. },
  135. getStringLinesInArea: function(area, strVal, chnW, otherW) {
  136. //备注: 因后台的pdf kit判断字符串长度与前端的不一样,需要做些调整,不一次性地判断字符串长度。
  137. // 分2种字符:中文与非中文,按照各种字符的数量分别乘以相关一个字符的宽度再累计。
  138. // 另判断行数还不能直接用总长度除以宽度来计算,因每一行都会有不同的余量,所以得一行行走过来判断。
  139. let rst = 0;
  140. if (strVal) {
  141. let areaWidth = area[JV.PROP_RIGHT] - area[JV.PROP_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] - 1;
  142. let txtWidth = 0, currentW = 0;
  143. for (let sIdx = 0; sIdx < strVal.length; sIdx++) {
  144. currentW = (strVal.charCodeAt(sIdx) > 127)?chnW:otherW;
  145. txtWidth += currentW;
  146. if (txtWidth > areaWidth) {
  147. rst++;
  148. txtWidth = currentW;
  149. }
  150. if (sIdx === strVal.length - 1) {
  151. rst++;
  152. }
  153. }
  154. }
  155. if (rst === 0) rst = 1; //即使是空字符串,也得有一行啊
  156. return rst;
  157. //备注: 其实是想用canvas的,但node canvas装起来麻烦,暂时用PDF Kit来顶用一下,以后有更好的再换
  158. },
  159. splitString: function (area, strVal, chnW, otherW) {
  160. let rst = [];
  161. if (strVal) {
  162. let areaWidth = area[JV.PROP_RIGHT] - area[JV.PROP_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] - 1;
  163. let preSIdx = 0, txtWidth = 0;
  164. let currentW = 0;
  165. for (let sIdx = 0; sIdx < strVal.length; sIdx++) {
  166. currentW = (strVal.charCodeAt(sIdx) > 127)?chnW:otherW;
  167. txtWidth += currentW;
  168. if (txtWidth > areaWidth) {
  169. if (preSIdx < sIdx) {
  170. rst.push(strVal.substr(preSIdx, sIdx - preSIdx));
  171. preSIdx = sIdx;
  172. } else {
  173. rst.push(strVal.substr(preSIdx, 1));
  174. preSIdx = sIdx + 1;
  175. }
  176. txtWidth = currentW;
  177. }
  178. if (sIdx === strVal.length - 1) {
  179. rst.push(strVal.substr(preSIdx, strVal.length - preSIdx));
  180. }
  181. }
  182. }
  183. if (rst.length === 0) rst.push(''); //什么都没有,也得整个空串
  184. return rst;
  185. }
  186. };
  187. module.exports = JpcCommonHelper;