rpt_print.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /**
  2. * Created by Tony on 2018/4/24.
  3. */
  4. let rptPrintHelper = {
  5. preview: async function () {
  6. if (zTreeOprObj.checkedRptTplNodes && zTreeOprObj.checkedRptTplNodes.length > 0) {
  7. let refRptTplIds = [];
  8. rptControlObj.getTplIdsCommon(refRptTplIds, null);
  9. let params = rptControlObj.creatCommonExportParam(refRptTplIds);
  10. await rptCustomObj.getCustomSelect(params);
  11. let chkNodes = zTreeOprObj.treeObj.getCheckedNodes(true);
  12. if (chkNodes.length > 0) {
  13. delete params.orientation; // 打印时有勾选的话,不需要提供方向
  14. }
  15. if (COMMON_WATER_MARK_PIC_DATA === null || COMMON_WATER_MARK_PIC_DATA === '') {
  16. params.needWaterMark = true;
  17. } else {
  18. params.needWaterMark = false;
  19. }
  20. CommonAjax.postXsrfEx("/tender/report_api/getMultiReports", params, 60000, true, getCookie('csrfToken'),
  21. function(result){
  22. const signatureRelArr = [];
  23. for (const signatureRel of result.signatureRelInfo) {
  24. signatureRelArr.push(JSON.parse(signatureRel.rel_content));
  25. }
  26. if (params.needWaterMark) COMMON_WATER_MARK_PIC_DATA = result.waterMarkStr;
  27. for (let idx = 0; idx < result.data.length; idx++) {
  28. let singleSignatureRelArr = [];
  29. for (let rIdx = 0; rIdx < result.signatureRelInfo.length; rIdx++) {
  30. if (result.signatureRelInfo[rIdx].rpt_id === refRptTplIds[idx]) {
  31. singleSignatureRelArr = signatureRelArr[rIdx]; // 有些报表可能没有签名
  32. break;
  33. }
  34. }
  35. // rptSignatureHelper.mergeSignature(result.data[idx], ROLE_REL_LIST);
  36. // let pageRst = result.data;
  37. // STAGE_AUDIT = result.stageAudit;
  38. // STAGE_AUDIT_ORG = result.stageAuditOrg;
  39. // STAGE_FLOW = result.stageFlow;
  40. // //stageAuditOrg
  41. // if (result.signatureRelInfo && result.signatureRelInfo.length > 0) {
  42. // CURRENT_ROLE_REL_ID = result.signatureRelInfo[0].id;
  43. // ROLE_REL_LIST = me._parseRoleRelList(result.signatureRelInfo[0].rel_content);
  44. // rptSignatureHelper.originalRoleRelList = me._parseRoleRelList(result.signatureRelInfo[0].rel_content);
  45. // if (current_stage_status === 3) {
  46. // rptSignatureHelper.mergeSignDate(pageRst, ROLE_REL_LIST);
  47. // rptSignatureHelper.mergeSignature(pageRst, ROLE_REL_LIST);
  48. // rptSignatureHelper.mergeSignAudit(pageRst, ROLE_REL_LIST, STAGE_AUDIT);
  49. // }
  50. // } else {
  51. // CURRENT_ROLE_REL_ID = -1;
  52. // ROLE_REL_LIST = [];
  53. // }
  54. if (current_stage_status === 3) {
  55. rptSignatureHelper.mergeSignDate(result.data[idx], singleSignatureRelArr, false);
  56. rptSignatureHelper.mergeSignature(result.data[idx], singleSignatureRelArr);
  57. rptSignatureHelper.mergeSignAudit(result.data[idx], singleSignatureRelArr, result.stageAudit);
  58. }
  59. }
  60. sessionStorage.multiRptsData = JSON.stringify(result.data);
  61. sessionStorage.pageSize = rptControlObj.getCurrentPageSize();
  62. sessionStorage.orientation = rptControlObj.getCurrentOrientation();
  63. sessionStorage.scaleFactor = 1;
  64. sessionStorage.closeWaterMark = getCloseWatermark();
  65. sessionStorage.waterMarkStr = COMMON_WATER_MARK_PIC_DATA;
  66. if (sessionStorage.pageSize === 'A3') {
  67. window.open('/printReport/A3');
  68. } else {
  69. window.open('/printReport/A4');
  70. }
  71. },
  72. function(failRst){
  73. sessionStorage.currentPageData = null;
  74. console.log(failRst);
  75. },
  76. function(exceptionRst){
  77. sessionStorage.currentPageData = null;
  78. console.log(exceptionRst);
  79. }
  80. );
  81. } else {
  82. //不可能的branch
  83. }
  84. },
  85. previewSvgData: function() {
  86. //
  87. },
  88. buildSvgArr: function (pagesData, actAreaArr, offsetX, offsetY, closeWaterMark) {
  89. let styles = pagesData[JV.NODE_STYLE_COLLECTION],
  90. fonts = pagesData[JV.NODE_FONT_COLLECTION],
  91. controls = pagesData[JV.NODE_CONTROL_COLLECTION]
  92. ;
  93. let rst = [];
  94. let canvas = document.getElementById("chkCanvas");
  95. let pixelSize = getPixelSize(pagesData);
  96. let isHtoV = false;
  97. if (pixelSize[0] > pixelSize[1]) {
  98. // changeHtoV(pagesData, pixelSize);
  99. isHtoV = true;
  100. }
  101. for (let idx = 0; idx < pagesData.items.length; idx++) {
  102. let page = pagesData.items[idx];
  103. let actAreaOffsetX = 0, actAreaOffsetY = 0;
  104. if (actAreaArr) {
  105. actAreaOffsetX = actAreaArr[idx].Left;
  106. actAreaOffsetY = actAreaArr[idx].Top;
  107. if (pixelSize[0] > pixelSize[1]) {
  108. }
  109. }
  110. let svgPageArr = [];
  111. console.log(pixelSize);
  112. svgPageArr.push("<svg width='" + pixelSize[0] + "' height='" + pixelSize[1] + "'>");
  113. // let adjustY = 0.5 * ((idx + 1) % 2);
  114. let adjustY = 0.5;
  115. for (let cell of page.cells) {
  116. svgPageArr.push(buildCellSvg(cell, fonts, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND],
  117. offsetX - actAreaOffsetX, offsetY - actAreaOffsetY, adjustY, canvas, isHtoV, pixelSize, actAreaArr[idx]));
  118. }
  119. // 计量有电子签名,要单独处理
  120. for (let cell of page.signature_cells) {
  121. svgPageArr.push(buildSignatureCellSvg(cell, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND],
  122. offsetX - actAreaOffsetX, offsetY - actAreaOffsetY, adjustY, canvas, isHtoV, pixelSize, actAreaArr[idx]));
  123. }
  124. if (closeWaterMark === 0) {
  125. for (let cell of page.watermark_cells) {
  126. cell.pic = sessionStorage.waterMarkStr;
  127. svgPageArr.push(buildSignatureCellSvg(cell, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND],
  128. offsetX - actAreaOffsetX, offsetY - actAreaOffsetY, adjustY, canvas, isHtoV, pixelSize, actAreaArr[idx]));
  129. }
  130. }
  131. // 计量有电子签名日期,在处理上与cells一样
  132. for (let cell of page.signature_date_cells) {
  133. svgPageArr.push(buildCellSvg(cell, fonts, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND],
  134. offsetX - actAreaOffsetX, offsetY - actAreaOffsetY, adjustY, canvas, isHtoV, pixelSize, actAreaArr[idx]));
  135. }
  136. // 计量有电子签名审核意见,在处理上与cells一样
  137. for (let cell of page.signature_audit_cells) {
  138. svgPageArr.push(buildCellSvg(cell, fonts, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND],
  139. offsetX - actAreaOffsetX, offsetY - actAreaOffsetY, adjustY, canvas, isHtoV, pixelSize, actAreaArr[idx]));
  140. }
  141. svgPageArr.push("</svg>");
  142. rst.push(svgPageArr);
  143. }
  144. return rst;
  145. }
  146. };
  147. function getActualBorderStyle(cell, styles, mergeBorderStyle, pageBorderArea, borderStr) {
  148. let rst = styles[cell[JV.PROP_STYLE]][borderStr];
  149. if (rst && parseFloat(rst[JV.PROP_LINE_WEIGHT]) === 1) {
  150. rst[JV.PROP_LINE_WEIGHT] = 0.5;
  151. }
  152. if (mergeBorderStyle) {
  153. if (parseFloat(cell[JV.PROP_AREA][borderStr]) === parseFloat(pageBorderArea[borderStr])) {
  154. if (borderStr === JV.PROP_LEFT || borderStr === JV.PROP_RIGHT) {
  155. if (parseFloat(cell[JV.PROP_AREA][JV.PROP_TOP]) >= parseFloat(pageBorderArea[JV.PROP_TOP]) &&
  156. parseFloat(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) <= parseFloat(pageBorderArea[JV.PROP_BOTTOM])) {
  157. rst = mergeBorderStyle[borderStr];
  158. }
  159. } else if (borderStr === JV.PROP_TOP || borderStr === JV.PROP_BOTTOM) {
  160. if (parseFloat(cell[JV.PROP_AREA][JV.PROP_LEFT]) >= parseFloat(pageBorderArea[JV.PROP_LEFT]) &&
  161. parseFloat(cell[JV.PROP_AREA][JV.PROP_RIGHT]) <= parseFloat(pageBorderArea[JV.PROP_RIGHT])) {
  162. rst = mergeBorderStyle[borderStr];
  163. }
  164. }
  165. }
  166. }
  167. return rst;
  168. }
  169. function buildSignatureCellSvg(cell, styles, controls, pageMergeBorder, rptMergeBorder, offsetX, offsetY, adjustY, canvas, isHtoV, pixelSize, actArea) {
  170. let rst = [];
  171. let style = styles[cell[JV.PROP_STYLE]];
  172. let mergeBandStyle = null;
  173. if (rptMergeBorder) {
  174. mergeBandStyle = styles[rptMergeBorder[JV.PROP_STYLE][JV.PROP_ID]];
  175. }
  176. let font = cell[JV.PROP_FONT];
  177. if (typeof font === 'string') {
  178. font = fonts[cell[JV.PROP_FONT]];
  179. }
  180. let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]) + offsetX + 0.5,
  181. right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]) + offsetX + 0.5,
  182. top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]) + offsetY + adjustY,
  183. bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY
  184. ;
  185. let HtoVStr = "";
  186. if (isHtoV) {
  187. // HtoVStr = ` transform="translate(`+ (actArea.Bottom - actArea.Top + 5) + `,` + (actArea.Left - actArea.Top ) + `) rotate(90)"`;
  188. //引用了padding后,top坐标不用考虑offset了
  189. HtoVStr = ` transform="translate(${(actArea.Bottom - actArea.Top + 2)},0) rotate(90)"`;
  190. }
  191. if (style) {
  192. let leftBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_LEFT);
  193. // if (style[JV.PROP_LEFT] && parseFloat(style[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) > 0) {
  194. if (leftBS && parseFloat(leftBS[JV.PROP_LINE_WEIGHT]) > 0) {
  195. rst.push("<line x1='" + left + "' y1='" + top +
  196. "' x2='" + left + "' y2='" + bottom +
  197. "' style='stroke:rgb(0,0,0);stroke-width:" + leftBS[JV.PROP_LINE_WEIGHT] + "'" + HtoVStr + "/>")
  198. }
  199. let rightBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_RIGHT);
  200. // if (style[JV.PROP_RIGHT] && parseFloat(style[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) > 0) {
  201. if (rightBS && parseFloat(rightBS[JV.PROP_LINE_WEIGHT]) > 0) {
  202. rst.push("<line x1='" + right + "' y1='" + top +
  203. "' x2='" + right + "' y2='" + bottom +
  204. "' style='stroke:rgb(0,0,0);stroke-width:" + rightBS[JV.PROP_LINE_WEIGHT] +"'" + HtoVStr + "/>")
  205. }
  206. let topBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_TOP);
  207. // if (style[JV.PROP_TOP] && parseFloat(style[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) > 0) {
  208. if (topBS && parseFloat(topBS[JV.PROP_LINE_WEIGHT]) > 0) {
  209. rst.push("<line x1='" + left + "' y1='" + top +
  210. "' x2='" + right + "' y2='" + top +
  211. "' style='stroke:rgb(0,0,0);stroke-width:" + topBS[JV.PROP_LINE_WEIGHT] +"'" + HtoVStr + "/>")
  212. }
  213. let bottomBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_BOTTOM);
  214. // if (style[JV.PROP_BOTTOM] && parseFloat(style[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) > 0) {
  215. if (bottomBS && parseFloat(bottomBS[JV.PROP_LINE_WEIGHT]) > 0) {
  216. rst.push("<line x1='" + left + "' y1='" + bottom +
  217. "' x2='" + right + "' y2='" + bottom +
  218. "' style='stroke:rgb(0,0,0);stroke-width:" + bottomBS[JV.PROP_LINE_WEIGHT] +"'" + HtoVStr + "/>")
  219. }
  220. }
  221. let control = cell[JV.PROP_CONTROL];
  222. if (typeof control === 'string') {
  223. control = controls[cell[JV.PROP_CONTROL]];
  224. }
  225. if (cell.pic || cell.path) {
  226. buildImage(rst, cell, control, offsetX, offsetY, adjustY, isHtoV, HtoVStr);
  227. }
  228. return rst.join("");
  229. }
  230. function buildCellSvg(cell, fonts, styles, controls, pageMergeBorder, rptMergeBorder, offsetX, offsetY, adjustY, canvas, isHtoV, pixelSize, actArea) {
  231. let rst = [];
  232. let style = styles[cell[JV.PROP_STYLE]];
  233. let mergeBandStyle = null;
  234. if (rptMergeBorder) {
  235. mergeBandStyle = styles[rptMergeBorder[JV.PROP_STYLE][JV.PROP_ID]];
  236. }
  237. let font = cell[JV.PROP_FONT];
  238. if (typeof font === 'string') {
  239. font = fonts[cell[JV.PROP_FONT]];
  240. }
  241. let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]) + offsetX + 0.5,
  242. right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]) + offsetX + 0.5,
  243. top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]) + offsetY + adjustY,
  244. bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY
  245. ;
  246. let HtoVStr = "";
  247. if (isHtoV) {
  248. // HtoVStr = ` transform="translate(`+ (actArea.Bottom - actArea.Top + 5) + `,` + (actArea.Left - actArea.Top ) + `) rotate(90)"`;
  249. //引用了padding后,top坐标不用考虑offset了
  250. HtoVStr = ` transform="translate(${(actArea.Bottom - actArea.Top + 2)},0) rotate(90)"`;
  251. }
  252. if (style) {
  253. let leftBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_LEFT);
  254. // if (style[JV.PROP_LEFT] && parseFloat(style[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) > 0) {
  255. if (leftBS && parseFloat(leftBS[JV.PROP_LINE_WEIGHT]) > 0) {
  256. rst.push("<line x1='" + left + "' y1='" + top +
  257. "' x2='" + left + "' y2='" + bottom +
  258. "' style='stroke:rgb(0,0,0);stroke-width:" + leftBS[JV.PROP_LINE_WEIGHT] + "'" + HtoVStr + "/>")
  259. }
  260. let rightBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_RIGHT);
  261. // if (style[JV.PROP_RIGHT] && parseFloat(style[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) > 0) {
  262. if (rightBS && parseFloat(rightBS[JV.PROP_LINE_WEIGHT]) > 0) {
  263. rst.push("<line x1='" + right + "' y1='" + top +
  264. "' x2='" + right + "' y2='" + bottom +
  265. "' style='stroke:rgb(0,0,0);stroke-width:" + rightBS[JV.PROP_LINE_WEIGHT] +"'" + HtoVStr + "/>")
  266. }
  267. let topBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_TOP);
  268. // if (style[JV.PROP_TOP] && parseFloat(style[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) > 0) {
  269. if (topBS && parseFloat(topBS[JV.PROP_LINE_WEIGHT]) > 0) {
  270. rst.push("<line x1='" + left + "' y1='" + top +
  271. "' x2='" + right + "' y2='" + top +
  272. "' style='stroke:rgb(0,0,0);stroke-width:" + topBS[JV.PROP_LINE_WEIGHT] +"'" + HtoVStr + "/>")
  273. }
  274. let bottomBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_BOTTOM);
  275. // if (style[JV.PROP_BOTTOM] && parseFloat(style[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) > 0) {
  276. if (bottomBS && parseFloat(bottomBS[JV.PROP_LINE_WEIGHT]) > 0) {
  277. rst.push("<line x1='" + left + "' y1='" + bottom +
  278. "' x2='" + right + "' y2='" + bottom +
  279. "' style='stroke:rgb(0,0,0);stroke-width:" + bottomBS[JV.PROP_LINE_WEIGHT] +"'" + HtoVStr + "/>")
  280. }
  281. }
  282. let control = cell[JV.PROP_CONTROL];
  283. if (typeof control === 'string') {
  284. control = controls[cell[JV.PROP_CONTROL]];
  285. }
  286. buildText(rst, cell, font, control, offsetX, offsetY, adjustY, canvas, isHtoV, HtoVStr);
  287. return rst.join("");
  288. }
  289. function buildImage(destRst, cell, control, offsetX, offsetY, adjustY, isHtoV, HtoVStr) {
  290. let href = '';
  291. if (cell.pic) {
  292. href = 'href="' + cell.pic +'"';
  293. } else {
  294. href = 'xlink:href="' + cell.path +'"';
  295. }
  296. const area = getProperSignatureArea(cell, control, offsetX, offsetY);
  297. if (cell.signature_name === JV.SIGNATURE_NAME_DUMMY) {
  298. area[0] = area[0] + 1; // Left
  299. area[1] = area[1] + 1; // Top
  300. area[2] = area[2] - 1; // Right
  301. area[3] = area[3] - 1; // Bottom
  302. }
  303. destRst.push('<image x="' + area[0] + '" y="' + area[1] + '" width="' + (area[2] - area[0]) + '" height="' + (area[3] - area[1]) + '" ');
  304. destRst.push(href + HtoVStr + ' />');
  305. }
  306. function getProperSignatureArea(cell, control, offsetX, offsetY) {
  307. // 约定默认长宽比例是2:1,图片分辨率是600*300
  308. const rst = [0, 0, 0, 0]; // left, top, right, bottom
  309. if (cell && cell[JV.PROP_AREA]) {
  310. let width = cell[JV.PROP_AREA][JV.PROP_RIGHT] - cell[JV.PROP_AREA][JV.PROP_LEFT],
  311. height = cell[JV.PROP_AREA][JV.PROP_BOTTOM] - cell[JV.PROP_AREA][JV.PROP_TOP];
  312. if (width > height * 2) {
  313. width = height * 2;
  314. } else {
  315. height = width / 2;
  316. }
  317. switch (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]]) {
  318. case 'left':
  319. rst[0] = cell[JV.PROP_AREA][JV.PROP_LEFT];
  320. rst[1] = cell[JV.PROP_AREA][JV.PROP_TOP];
  321. rst[2] = rst[0] + width;
  322. rst[3] = rst[1] + height;
  323. break;
  324. case 'right':
  325. rst[2] = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  326. rst[3] = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  327. rst[0] = rst[2] - width;
  328. rst[1] = rst[3] - height;
  329. break;
  330. default:
  331. //center
  332. rst[0] = (cell[JV.PROP_AREA][JV.PROP_LEFT] + cell[JV.PROP_AREA][JV.PROP_RIGHT] - width) / 2;
  333. rst[1] = cell[JV.PROP_AREA][JV.PROP_TOP];
  334. rst[2] = rst[0] + width;
  335. rst[3] = rst[1] + height;
  336. break;
  337. }
  338. }
  339. rst[0] = rst[0] + offsetX;
  340. rst[2] = rst[2] + offsetX;
  341. rst[1] = rst[1] + offsetY;
  342. rst[3] = rst[3] + offsetY;
  343. return rst;
  344. }
  345. function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, canvas, isHtoV, HtoVStr) {
  346. let orgFontHeight = parseInt(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]]);
  347. let fontWeight = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === 'T')?"bold":"normal";
  348. let fontStyle = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === 'T')?"italic":"normal";
  349. let fontUnderline = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] === 'T')?"underline":"normal";
  350. let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]) + offsetX + 0.5,
  351. right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]) + offsetX + 0.5,
  352. top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]) + offsetY + adjustY,
  353. bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY,
  354. x = left, y = top,
  355. text_anchor = "start"
  356. ;
  357. let value = cell[JV.PROP_VALUE];
  358. if (value === undefined || value === null) {
  359. value = "";
  360. }
  361. let values = null;
  362. if (typeof value === "string") {
  363. values = value.split("|");
  364. } else {
  365. values = [value];
  366. }
  367. // let stepHeight = (parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) - parseInt(cell[JV.PROP_AREA][JV.PROP_TOP])) / values.length;
  368. if (control) {
  369. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "left") {
  370. text_anchor = "start";
  371. x = left + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
  372. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "right") {
  373. text_anchor = "end";
  374. x = right - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
  375. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "center") {
  376. text_anchor = "middle";
  377. x = Math.round((left + right) / 2);
  378. }
  379. }
  380. let area = [0,0,0,0];
  381. area[JV.IDX_TOP] = top;
  382. area[JV.IDX_BOTTOM] = bottom;
  383. area[JV.IDX_LEFT] = left;
  384. area[JV.IDX_RIGHT] = right;
  385. let height = bottom - top;
  386. let ctx = canvas.getContext("2d");
  387. let inner_draw_text = function (textValue) {
  388. let dftFontHeight = orgFontHeight;
  389. ctx.font = ((font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === 'T')?"bold ":"") + ((font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === 'T')?"italic":"") + dftFontHeight + "px " + font[JV.PROP_NAME];
  390. function inner_build_text(innerTxt, innerArea) {
  391. let innerDftFontHeight = (dftFontHeight * 3 / 4); //SVG的字体与canvas的字体大小的切换, 不用考虑取整
  392. if (control) {
  393. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "top") {
  394. y = innerArea[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_TOP] + orgFontHeight;
  395. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "bottom") {
  396. y = innerArea[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.IDX_BOTTOM];
  397. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "center") {
  398. y = Math.round((innerArea[JV.IDX_TOP] + innerArea[JV.IDX_BOTTOM] + innerDftFontHeight) / 2 );
  399. }
  400. } else {
  401. y = innerArea[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_TOP];
  402. }
  403. if (font[JV.PROP_NAME] === "宋体") {
  404. y--;
  405. }
  406. destRst.push("<text style='fill:black;font-family:" + font[JV.PROP_NAME] +
  407. ";font-weight:" + fontWeight +
  408. ";font-style:" + fontStyle +
  409. ";text-decoration:" + fontUnderline +
  410. // ";text-decoration:normal" +
  411. ";font-size:" + innerDftFontHeight + "pt' x='" +
  412. x +"' y='" + y + "' text-anchor='" + text_anchor + "' xml:space='preserve'" + HtoVStr + ">" + innerTxt + "</text>");
  413. }
  414. let actLines = private_splitString(textValue, (area[JV.IDX_RIGHT] - area[JV.IDX_LEFT] - JV.OUTPUT_OFFSET[JV.IDX_LEFT] - JV.OUTPUT_OFFSET[JV.IDX_RIGHT]), ctx);
  415. let validTxtLines = Math.floor((area[JV.IDX_BOTTOM] - area[JV.IDX_TOP]) / (dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP] + 4));
  416. if (actLines.length === 1 || (control && control.Shrink !== 'T' && validTxtLines < actLines)) {
  417. inner_build_text(textValue, area);
  418. } else {
  419. while (true) {
  420. let lines = Math.floor((area[JV.IDX_BOTTOM] - area[JV.IDX_TOP]) / (dftFontHeight + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.IDX_TOP] + 4));
  421. lines = (lines === 0 || (control.Shrink === 'T' && control.ShrinkFirst === 'T'))?1:lines;
  422. actLines = private_splitString(textValue, (area[JV.IDX_RIGHT] - area[JV.IDX_LEFT] - JV.OUTPUT_OFFSET[JV.IDX_LEFT] - JV.OUTPUT_OFFSET[JV.IDX_RIGHT]), ctx);
  423. if (actLines.length > lines && dftFontHeight >= 6) {
  424. dftFontHeight--;
  425. ctx.font = ((font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === 'T')?"bold ":"") + ((font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === 'T')?"italic":"") + dftFontHeight + "px " + font[JV.PROP_NAME];
  426. } else {
  427. let aH = dftFontHeight + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.IDX_TOP] + 4;
  428. if ((aH * actLines.length) < (area[JV.IDX_BOTTOM] - area[JV.IDX_TOP]) && (control && control.Vertical !== 'top')) {
  429. if (control.Vertical === 'bottom') {
  430. area[JV.IDX_TOP] = area[JV.IDX_BOTTOM] - (aH * actLines.length);
  431. } else {
  432. area[JV.IDX_TOP] = (area[JV.IDX_TOP] + area[JV.IDX_BOTTOM]) / 2 - (aH * actLines.length) / 2
  433. area[JV.IDX_BOTTOM] = area[JV.IDX_TOP] + (aH * actLines.length);
  434. }
  435. }
  436. let newArea = [], baseTop = area[JV.IDX_TOP];
  437. for (let ai = 0; ai < area.length; ai++) {
  438. newArea[ai] = area[ai];
  439. }
  440. for (let lIdx = 0; lIdx < actLines.length; lIdx++) {
  441. newArea[JV.IDX_TOP] = Math.round(aH * lIdx + baseTop);
  442. newArea[JV.IDX_BOTTOM] = Math.round(aH * (lIdx + 1) + baseTop);
  443. inner_build_text(actLines[lIdx], newArea);
  444. }
  445. break;
  446. }
  447. }
  448. }
  449. };
  450. let ah = height;
  451. let restTopH = 0, restBottomH = 0;
  452. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_CLOSE_OUTPUT]] === 'T') {
  453. ah = (parseFloat(font[JV.FONT_PROPS[1]]) + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]) * values.length;
  454. let restH = height - ah;
  455. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === 'center') {
  456. restTopH = restH / 2;
  457. restBottomH = restH / 2;
  458. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === 'bottom') {
  459. restBottomH = restH;
  460. } else {
  461. // restTopH = restH;
  462. restTopH = JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP] + 8; // 备注:打印比别的方式多加些空隙
  463. }
  464. }
  465. for (let vidx = 0; vidx < values.length; vidx++) {
  466. // area[JV.IDX_TOP] = top + vidx * (height / values.length);
  467. // area[JV.IDX_BOTTOM] = top + (vidx + 1) * (height / values.length);
  468. area[JV.IDX_TOP] = top + vidx * (ah / values.length) + restTopH;
  469. area[JV.IDX_BOTTOM] = top + (vidx + 1) * (ah / values.length) + restTopH;
  470. inner_draw_text(values[vidx]);
  471. }
  472. }
  473. function private_splitString(strVal, areaWidth, ctx) {
  474. let rst = [];
  475. if (strVal) {
  476. let preSIdx = 0, txtWidth = 0;
  477. let currentW = 0;
  478. let chnW = ctx.measureText('一').width, otherW = ctx.measureText('_').width;
  479. for (let sIdx = 0; sIdx < strVal.length; sIdx++) {
  480. currentW = (strVal.charCodeAt(sIdx) > 127)?chnW:otherW;
  481. txtWidth += currentW;
  482. if (txtWidth > areaWidth) {
  483. if (preSIdx < sIdx) {
  484. rst.push(strVal.substr(preSIdx, sIdx - preSIdx));
  485. preSIdx = sIdx;
  486. } else {
  487. rst.push(strVal.substr(preSIdx, 1));
  488. preSIdx = sIdx + 1;
  489. }
  490. txtWidth = currentW;
  491. }
  492. if (sIdx === strVal.length - 1) {
  493. rst.push(strVal.substr(preSIdx, strVal.length - preSIdx));
  494. }
  495. }
  496. }
  497. if (rst.length === 0) rst.push(''); //什么都没有,也得整个空串
  498. return rst;
  499. }
  500. function getPixelSize(pagesData) {
  501. let rst = [793,1122];
  502. let SCREEN_DPI = [96,96];
  503. if (pagesData[JV.NODE_PAGE_INFO] && pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE]) {
  504. rst[0] = Math.round(SCREEN_DPI[0] * pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0]);
  505. rst[1] = Math.round(SCREEN_DPI[1] * pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]);
  506. }
  507. return rst;
  508. }