jpc_helper_common.js 7.7 KB

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