rpt_jspdf.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**
  2. * Created by Tony on 2019/9/10.
  3. */
  4. const PDF_SCALE = 0.75;
  5. // const DPI = getScreenDPI();
  6. let JpcJsPDFHelper = {
  7. doc: null,
  8. initialize: function (orientation, unit, format) {
  9. let me = this;
  10. me.doc = new jsPDF(orientation, unit, format);
  11. return me.doc;
  12. },
  13. testFont: function () {
  14. let me = this;
  15. let doc = me.initialize('p', 'pt', 'a4');
  16. console.log(me.doc.getFontList());
  17. // console.log(me.doc.getFont());
  18. // doc.setFont("SmartSimsun", "bold");
  19. // doc.setFontSize(16);
  20. // doc.text(20, 20, '小写字母总长度:' + doc.getTextWidth('abcdefghijklmnopqrstuvwxyz'));
  21. //
  22. // doc.setFont("SmartSimsun", "normal");
  23. // doc.setFontSize(16);
  24. // doc.text(20, 40, '大写字母总长度:' + doc.getTextWidth('ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
  25. //
  26. // doc.line(20, 20, 60, 20); // horizontal line
  27. // doc.line(20, 40, 60, 40); // horizontal line
  28. //
  29. // doc.save('Test.pdf');
  30. },
  31. outputAsPdf: function (pageData, paperSize, pdfName, signatureRelArr, signAuditArr) {
  32. let doc = this._createPdf(pageData, paperSize, signatureRelArr, signAuditArr);
  33. doc.save(pdfName + '.pdf');
  34. },
  35. _createPdf: function (pageData, paperSize, signatureRelArr, signAuditArr) {
  36. let me = this;
  37. let offsetX = 0;
  38. let offsetY = 0;
  39. let strReplaceReg1 = new RegExp('\n\r', 'gm'),
  40. strReplaceReg2 = new RegExp('\r\n', 'gm'),
  41. strReplaceReg3 = new RegExp('\n', 'gm'),
  42. strReplaceReg4 = new RegExp('\r', 'gm')
  43. ;
  44. // let newName = pdfName;
  45. let pageObj = pageData;
  46. let paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(paperSize);
  47. let size = JV.PAGES_SIZE[paperSizeIdx];
  48. let orientation = '';
  49. if (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] > pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]) {
  50. orientation = 'landscape';
  51. } else {
  52. orientation = 'portrait';
  53. }
  54. let doc = me.initialize(orientation, 'pt', paperSize.toLowerCase());
  55. doc.setFont("SmartSimsun", "normal"); //目前只考虑宋体
  56. function private_getIniPageMergeBorder(mergedBand) {
  57. let rst = {};
  58. rst[JV.PROP_LEFT] = mergedBand[JV.PROP_LEFT];
  59. rst[JV.PROP_RIGHT] = mergedBand[JV.PROP_RIGHT];
  60. rst[JV.PROP_TOP] = mergedBand[JV.PROP_TOP];
  61. rst[JV.PROP_BOTTOM] = mergedBand[JV.PROP_BOTTOM];
  62. rst[JV.PROP_STYLE] = mergedBand[JV.PROP_STYLE];
  63. return rst;
  64. }
  65. let newPageMergeBand = private_getIniPageMergeBorder(pageObj[JV.BAND_PROP_MERGE_BAND]);
  66. if (current_stage_status === 3) {
  67. rptSignatureHelper.mergeSignDate(pageObj, signatureRelArr, false);
  68. // rptSignatureHelper.mergeSignature(pageObj, signatureRelArr); // 这里merge的意义不大
  69. rptSignatureHelper.mergeSignAudit(pageObj, signatureRelArr, signAuditArr);
  70. }
  71. // let signatureCounter = {"allAmt": 0, "picAmt": 0};
  72. if (pageObj && pageObj.items.length > 0 ) {
  73. for(let i = 0; i < pageObj.items.length; i++) {
  74. if (i > 0) {
  75. doc.addPage(paperSize.toLowerCase(), orientation);
  76. }
  77. let ctx = doc.canvas.getContext("2d");
  78. let page = pageObj.items[i],
  79. fonts = pageObj[JV.NODE_FONT_COLLECTION],
  80. styles = pageObj[JV.NODE_STYLE_COLLECTION],
  81. controls = pageObj[JV.NODE_CONTROL_COLLECTION];
  82. if (page[JV.PROP_PAGE_MERGE_BORDER]) {
  83. newPageMergeBand[JV.PROP_LEFT] = page[JV.PROP_PAGE_MERGE_BORDER][JV.PROP_LEFT];
  84. newPageMergeBand[JV.PROP_RIGHT] = page[JV.PROP_PAGE_MERGE_BORDER][JV.PROP_RIGHT];
  85. newPageMergeBand[JV.PROP_TOP] = page[JV.PROP_PAGE_MERGE_BORDER][JV.PROP_TOP];
  86. newPageMergeBand[JV.PROP_BOTTOM] = page[JV.PROP_PAGE_MERGE_BORDER][JV.PROP_BOTTOM];
  87. }
  88. for (let j = 0; j < page.cells.length; j++) {
  89. let cell = page.cells[j];
  90. private_drawCell(doc, ctx, cell, fonts, styles, controls, newPageMergeBand);
  91. }
  92. // 计量有电子签名日期,在处理上与cells一样
  93. for (let cell of page.signature_date_cells) {
  94. private_drawCell(doc, ctx, cell, fonts, styles, controls, newPageMergeBand);
  95. }
  96. // 计量有电子签名审核意见,在处理上与cells一样
  97. for (let cell of page.signature_audit_cells) {
  98. private_drawCell(doc, ctx, cell, fonts, styles, controls, newPageMergeBand);
  99. }
  100. // 计量有电子签名,要单独处理
  101. for (let cell of page.signature_cells) {
  102. if (current_stage_status === 3) {
  103. private_drawSignature(doc, ctx, cell, styles, controls, newPageMergeBand, false, signatureRelArr);
  104. } else {
  105. private_drawSignature(doc, ctx, cell, styles, controls, newPageMergeBand, true, signatureRelArr);
  106. }
  107. }
  108. if (PAGE_SHOW['closeWatermark'] === 0) {
  109. for (let cell of page.watermark_cells) {
  110. cell.pic = COMMON_WATER_MARK_PIC_DATA;
  111. _drawWatermark(doc, ctx, cell, controls);
  112. }
  113. }
  114. }
  115. }
  116. // doc.save(newName + '.pdf');
  117. return doc;
  118. function private_chkIfInMergedBand(mergedBand, cell) {
  119. let rst = false;
  120. if (mergedBand && cell) {
  121. rst = mergedBand[JV.PROP_TOP] <= cell[JV.PROP_AREA][JV.PROP_TOP] && mergedBand[JV.PROP_BOTTOM] >= cell[JV.PROP_AREA][JV.PROP_BOTTOM] &&
  122. mergedBand[JV.PROP_LEFT] <= cell[JV.PROP_AREA][JV.PROP_LEFT] && mergedBand[JV.PROP_RIGHT] >= cell[JV.PROP_AREA][JV.PROP_RIGHT];
  123. }
  124. return rst;
  125. }
  126. function _drawWatermark(doc, ctx, cell, controls) {
  127. let control = null;
  128. if (typeof cell[JV.PROP_CONTROL] === "string") {
  129. control = controls[cell[JV.PROP_CONTROL]];
  130. } else {
  131. control = cell[JV.PROP_CONTROL];
  132. }
  133. private_drawImage(doc, ctx, cell, control, cell.pic, [1, 1, 1, 1]);
  134. }
  135. function private_drawSignature(doc, ctx, cell, styles, controls, mergedBand, onlyShowBorder, signatureRelArr) {
  136. ctx.beginPath();
  137. let style = styles[cell[JV.PROP_STYLE]];
  138. if (style) {
  139. let isNeedMergeBand = private_chkIfInMergedBand(mergedBand, cell);
  140. private_drawLine(cell, doc, ctx, style, JV.PROP_TOP, [JV.PROP_LEFT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_TOP], mergedBand, styles, isNeedMergeBand);
  141. private_drawLine(cell, doc, ctx, style, JV.PROP_RIGHT, [JV.PROP_RIGHT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_BOTTOM], mergedBand, styles, isNeedMergeBand);
  142. private_drawLine(cell, doc, ctx, style, JV.PROP_BOTTOM, [JV.PROP_RIGHT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_BOTTOM], mergedBand, styles, isNeedMergeBand);
  143. private_drawLine(cell, doc, ctx, style, JV.PROP_LEFT, [JV.PROP_LEFT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_TOP], mergedBand, styles, isNeedMergeBand);
  144. }
  145. ctx.closePath();
  146. if (!onlyShowBorder) private_drawSignatureCellText(doc, ctx, cell, controls, signatureRelArr);
  147. }
  148. function private_drawSignatureCellText(doc, ctx, cell, controls, signatureRelArr) {
  149. let control = null;
  150. if (typeof cell[JV.PROP_CONTROL] === "string") {
  151. control = controls[cell[JV.PROP_CONTROL]];
  152. } else {
  153. control = cell[JV.PROP_CONTROL];
  154. }
  155. if (cell.pic) {
  156. if (cell.signature_name === JV.SIGNATURE_NAME_DUMMY) {
  157. private_drawImage(doc, ctx, cell, control, cell.pic, [1, 1, 1, 1]);
  158. } else {
  159. private_drawImage(doc, ctx, cell, control, cell.pic);
  160. }
  161. } else {
  162. // 导出PDF时,根本不需要判断cell.path是不是null or undefined
  163. for (const signRel of signatureRelArr) {
  164. if (cell.signature_name === signRel.signature_name && signRel.sign_pic !== null && signRel.sign_pic !== undefined) {
  165. private_drawImage(doc, ctx, cell, control, signRel.sign_pic);
  166. break;
  167. }
  168. }
  169. }
  170. }
  171. function private_getProperSignatureArea(cell, control) {
  172. // 约定默认长宽比例是2:1,图片分辨率是600*300
  173. const rst = [0, 0, 0, 0]; // left, top, right, bottom
  174. if (cell && cell[JV.PROP_AREA]) {
  175. let width = cell[JV.PROP_AREA][JV.PROP_RIGHT] - cell[JV.PROP_AREA][JV.PROP_LEFT],
  176. height = cell[JV.PROP_AREA][JV.PROP_BOTTOM] - cell[JV.PROP_AREA][JV.PROP_TOP];
  177. if (width > height * 2) {
  178. width = height * 2;
  179. } else {
  180. height = width / 2;
  181. }
  182. switch (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]]) {
  183. case 'left':
  184. rst[0] = cell[JV.PROP_AREA][JV.PROP_LEFT];
  185. rst[1] = cell[JV.PROP_AREA][JV.PROP_TOP];
  186. rst[2] = rst[0] + width;
  187. rst[3] = rst[1] + height;
  188. break;
  189. case 'right':
  190. rst[2] = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  191. rst[3] = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  192. rst[0] = rst[2] - width;
  193. rst[1] = rst[3] - height;
  194. break;
  195. default:
  196. //center
  197. rst[0] = (cell[JV.PROP_AREA][JV.PROP_LEFT] + cell[JV.PROP_AREA][JV.PROP_RIGHT] - width) / 2;
  198. rst[1] = cell[JV.PROP_AREA][JV.PROP_TOP];
  199. rst[2] = rst[0] + width;
  200. rst[3] = rst[1] + height;
  201. break;
  202. }
  203. }
  204. rst[0] = rst[0] + offsetX;
  205. rst[2] = rst[2] + offsetX;
  206. rst[1] = rst[1] + offsetY;
  207. rst[3] = rst[3] + offsetY;
  208. return rst;
  209. }
  210. function private_drawImage(doc, ctx, cell, control, imageData, offsetArea) {
  211. let area = private_getProperSignatureArea(cell, control);
  212. if (offsetArea) {
  213. area[0] = area[0] + offsetArea[0]; // Left
  214. area[1] = area[1] + offsetArea[1]; // Top
  215. area[2] = area[2] - offsetArea[2]; // Right
  216. area[3] = area[3] - offsetArea[3]; // Bottom
  217. }
  218. doc.addImage(imageData, 'PNG', area[0] * PDF_SCALE, area[1] * PDF_SCALE,
  219. (area[2] - area[0]) * PDF_SCALE, (area[3] - area[1]) * PDF_SCALE);
  220. }
  221. function private_drawCell(doc, ctx, cell, fonts, styles, controls, mergedBand) {
  222. ctx.beginPath();
  223. let style = styles[cell[JV.PROP_STYLE]];
  224. if (style) {
  225. let isNeedMergeBand = private_chkIfInMergedBand(mergedBand, cell);
  226. private_drawLine(cell, doc, ctx, style, JV.PROP_TOP, [JV.PROP_LEFT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_TOP], mergedBand, styles, isNeedMergeBand);
  227. private_drawLine(cell, doc, ctx, style, JV.PROP_RIGHT, [JV.PROP_RIGHT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_BOTTOM], mergedBand, styles, isNeedMergeBand);
  228. private_drawLine(cell, doc, ctx, style, JV.PROP_BOTTOM, [JV.PROP_RIGHT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_BOTTOM], mergedBand, styles, isNeedMergeBand);
  229. private_drawLine(cell, doc, ctx, style, JV.PROP_LEFT, [JV.PROP_LEFT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_TOP], mergedBand, styles, isNeedMergeBand);
  230. }
  231. private_drawCellText(doc, ctx, cell, fonts, controls);
  232. ctx.closePath();
  233. }
  234. function private_drawLine(cell, doc, ctx, style, styleBorderDest, startP, destP, mergedBand, styles, isNeedMergeBand) {
  235. let destStyle = style;
  236. if (mergedBand) {
  237. if (isNeedMergeBand && parseFloat(mergedBand[styleBorderDest]) === parseFloat(cell[JV.PROP_AREA][styleBorderDest])) {
  238. destStyle = styles[mergedBand[JV.PROP_STYLE][JV.PROP_ID]];
  239. }
  240. }
  241. if (destStyle[styleBorderDest] && parseFloat(destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT]) !== 0) {
  242. doc.setDrawColor(destStyle[styleBorderDest][JV.PROP_COLOR]);
  243. if (parseInt(destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT]) === 2) {
  244. doc.setLineWidth(1 * destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT]);
  245. } else {
  246. doc.setLineWidth(0.1);
  247. }
  248. doc.line((cell[JV.PROP_AREA][startP[0]] + offsetX) * PDF_SCALE, (cell[JV.PROP_AREA][startP[1]] + offsetY) * PDF_SCALE,
  249. (cell[JV.PROP_AREA][destP[0]] + offsetX) * PDF_SCALE, (cell[JV.PROP_AREA][destP[1]] + offsetY) * PDF_SCALE);
  250. }
  251. }
  252. function setupDocFontByCfg(doc, fontName, font) {
  253. if (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === 'T') {
  254. doc.setFont(fontName, "bold");
  255. } else {
  256. doc.setFont(fontName, "normal");
  257. }
  258. }
  259. function _preHandleSpliter(orgVal) {
  260. let rst = orgVal;
  261. if (rst !== null && rst !== '') {
  262. rst = rst.replace(strReplaceReg1, '|').replace(strReplaceReg2, '|').replace(strReplaceReg3, '|').replace(strReplaceReg4, '|');
  263. }
  264. return rst;
  265. }
  266. function _splitValues(cell, control, orgValues, doc) {
  267. //根据control的 自动折行 及 缩放优先 这俩属性 来分解cell value
  268. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_WRAP]] === 'T' && control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_SHRINK_FIRST]] !== 'T') {
  269. let vals = [];
  270. let validAreaTxtWidth = cell[JV.PROP_AREA][JV.PROP_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - cell[JV.PROP_AREA][JV.PROP_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
  271. for (let val of orgValues) {
  272. let actW = doc.getTextWidth(val);
  273. if (actW > validAreaTxtWidth) {
  274. vals = vals.concat(private_splitString(val, validAreaTxtWidth, doc));
  275. } else {
  276. vals.push(val);
  277. }
  278. }
  279. if (vals.length > orgValues.length) {
  280. for (let idx = 0; idx < vals.length; idx++) {
  281. orgValues[idx] = vals[idx];
  282. }
  283. }
  284. }
  285. }
  286. function private_drawCellText(doc, ctx, cell, fonts, controls) {
  287. if (cell[JV.PROP_VALUE] !== undefined && cell[JV.PROP_VALUE] !== null) {
  288. // let values = ("" + cell[JV.PROP_VALUE]).split('|');
  289. let values = _preHandleSpliter("" + cell[JV.PROP_VALUE]).split('|');
  290. // let font = fonts[cell[JV.PROP_FONT]];
  291. let font = null;
  292. if (typeof cell[JV.PROP_FONT] === "string") {
  293. font = fonts[cell[JV.PROP_FONT]];
  294. } else {
  295. font = cell[JV.PROP_FONT];
  296. }
  297. if (CUST_CFG.isNarrow) {
  298. // 目前只考虑宋体及窄体
  299. if (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] === 'Arial Narrow') {
  300. setupDocFontByCfg(doc, 'Arial Narrow', font);
  301. } else {
  302. setupDocFontByCfg(doc, 'SmartSimsun', font);
  303. }
  304. } else {
  305. setupDocFontByCfg(doc, 'SmartSimsun', font);
  306. }
  307. // let control = controls[cell[JV.PROP_CONTROL]];
  308. let control = null;
  309. if (typeof cell[JV.PROP_CONTROL] === "string") {
  310. control = controls[cell[JV.PROP_CONTROL]];
  311. } else {
  312. control = cell[JV.PROP_CONTROL];
  313. }
  314. if (font) {
  315. doc.setFontSize(1 * font[JV.FONT_PROPS[1]]);
  316. }
  317. _splitValues(cell, control, values, doc);
  318. let height = cell[JV.PROP_AREA][JV.PROP_BOTTOM] - cell[JV.PROP_AREA][JV.PROP_TOP];
  319. let area = [cell[JV.PROP_AREA][JV.PROP_LEFT] + offsetX, cell[JV.PROP_AREA][JV.PROP_TOP] + offsetY, cell[JV.PROP_AREA][JV.PROP_RIGHT] + offsetX, cell[JV.PROP_AREA][JV.PROP_BOTTOM] + offsetY];
  320. let ah = height;
  321. let restTopH = 0, restBottomH = 0;
  322. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_CLOSE_OUTPUT]] === 'T') {
  323. ah = (parseFloat(font[JV.FONT_PROPS[1]]) + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]) * values.length;
  324. let restH = height - ah;
  325. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === 'center') {
  326. restTopH = restH / 2;
  327. restBottomH = restH / 2;
  328. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === 'bottom') {
  329. restBottomH = restH;
  330. } else {
  331. // restTopH = restH;
  332. restTopH = JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
  333. }
  334. }
  335. let spaceIdxArr = [];
  336. for (let i = 0; i < values.length; i++) {
  337. area[JV.IDX_TOP] = cell[JV.PROP_AREA][JV.PROP_TOP] + i * (ah / values.length) + offsetY + restTopH;
  338. area[JV.IDX_BOTTOM] = cell[JV.PROP_AREA][JV.PROP_TOP] + (i + 1) * (ah / values.length) + offsetY + restBottomH;
  339. if (values[i] === null || values[i] === undefined || values[i] === 'null') {
  340. values[i] = "";
  341. }
  342. // 因pdfkit输出空格只有浏览器的一半宽度,需要额外加空格补上,jspdf也有这个情况 -----------------------------
  343. if (typeof(values[i]) === "string") {
  344. for (let j = 0; j < values[i].length; j++) {
  345. if (values[i][j] === ' ') spaceIdxArr.push(j);
  346. }
  347. }
  348. // 但有了_splitValues()处理后,这个加额外空格好像没必要了,但是不加又不好(比如'年 月 日'这种文字) ------------
  349. for (let j = spaceIdxArr.length - 1; j >= 0; j--) {
  350. values[i] = values[i].slice(0, spaceIdxArr[j]) + ' ' + values[i].slice(spaceIdxArr[j]);
  351. }
  352. // -----------------------------
  353. private_drawText(doc, ctx, values[i], area, font, control);
  354. spaceIdxArr = [];
  355. }
  356. }
  357. }
  358. function _chkIfShinkFirst(control, actLines) {
  359. let rst = false;
  360. if (control.ShrinkFirst === 'T' && control.Wrap !== 'T' && actLines > 1) {
  361. rst = true;
  362. }
  363. return rst;
  364. }
  365. function private_drawText(doc, ctx, val, area, font, control) {
  366. let dftFontHeight = 12;
  367. let output = [];
  368. if (font) {
  369. dftFontHeight = 1 * font[JV.FONT_PROPS[1]];
  370. doc.setFontSize(dftFontHeight);
  371. }
  372. // doc.font(fontFile);
  373. let options={};
  374. let inner_setupControl = function (outVal, inArea, inFontHeight, inOutput) {
  375. if (control) {
  376. private_setupAreaH(outVal, inArea, control.Horizon, font.FontAngle, inFontHeight, inOutput, options);
  377. private_setupAreaV(outVal, inArea, control.Vertical, font.FontAngle, inFontHeight, inOutput);
  378. } else {
  379. private_setupAreaH(outVal, inArea, "left", parseInt(font.FontAngle), inFontHeight, inOutput, options);
  380. private_setupAreaV(outVal, inArea, "bottom", parseInt(font.FontAngle), inFontHeight, inOutput);
  381. }
  382. };
  383. inner_setupControl(val, area, dftFontHeight, output);
  384. let validAreaTxtWidth = area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - area[JV.IDX_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
  385. 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));
  386. if (parseInt(font.FontAngle) !== 0) {
  387. validAreaTxtWidth = area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] - area[JV.IDX_TOP] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
  388. validTxtLines = Math.floor((area[JV.IDX_RIGHT] - area[JV.IDX_LEFT]) / (dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] + 4));
  389. }
  390. function private_drawUnderline(underLineVal, underLineArea) {
  391. //A. 暂不支持角度; B. PDF输出时,坐标没有translate
  392. // let ctx = doc;
  393. //1. 计算下划线的相关坐标
  394. let width = doc.getTextWidth(underLineVal);
  395. if (width > underLineArea[JV.IDX_RIGHT] - underLineArea[JV.IDX_LEFT]) {
  396. width = underLineArea[JV.IDX_RIGHT] - underLineArea[JV.IDX_LEFT];
  397. }
  398. let height = dftFontHeight;
  399. let startX = underLineArea[JV.IDX_LEFT], startY = underLineArea[JV.IDX_TOP], endX = underLineArea[JV.IDX_RIGHT], endY = underLineArea[JV.IDX_BOTTOM];
  400. // let startX = 0, startY = 0, endX = width, endY = startY;
  401. if (control.Horizon === "left") {
  402. startX = Math.round(underLineArea[JV.IDX_LEFT] + JV.OUTPUT_OFFSET[JV.IDX_LEFT]);
  403. } else if (control.Horizon === "right") {
  404. startX = Math.round(underLineArea[JV.IDX_RIGHT] - width - JV.OUTPUT_OFFSET[JV.IDX_RIGHT]);
  405. } else {
  406. startX = Math.round( underLineArea[JV.IDX_LEFT] + (underLineArea[JV.IDX_RIGHT] - underLineArea[JV.IDX_LEFT] - width) / 2);
  407. }
  408. endX = Math.round(startX + width);
  409. if (control.Vertical === "top") {
  410. startY = Math.round(underLineArea[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM] + height);
  411. } else if (control.Vertical === "bottom") {
  412. startY = Math.round(underLineArea[JV.IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM]);
  413. } else {
  414. startY = Math.round( underLineArea[JV.IDX_TOP] + (underLineArea[JV.IDX_BOTTOM] - underLineArea[JV.IDX_TOP] + height) / 2) + JV.OUTPUT_OFFSET[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM];
  415. }
  416. endY = Math.round(startY);
  417. //2. 画线
  418. // ctx.save();
  419. if ( output[1] !== Math.round(output[1])) {
  420. ctx.translate(0,0.5);
  421. }
  422. doc.setDrawColor("BLACK");
  423. doc.setLineWidth(0.1);
  424. doc.line(startX * PDF_SCALE, startY * PDF_SCALE, endX * PDF_SCALE, endY * PDF_SCALE);
  425. }
  426. let rotateOptions;
  427. if (parseInt(font.FontAngle) !== 0) {
  428. if (control){
  429. rotateOptions = private_setupAreaRotateOption(area,validAreaTxtWidth,control.Vertical,dftFontHeight, output);
  430. } else {
  431. rotateOptions = private_setupAreaRotateOption(area,validAreaTxtWidth,"bottom",dftFontHeight, output);
  432. }
  433. doc.rotate(font.FontAngle,rotateOptions);
  434. }
  435. if (validAreaTxtWidth >= doc.getTextWidth(val) ||
  436. (control && control.Shrink !== 'T' && validTxtLines < private_splitString(val, validAreaTxtWidth, doc)) ) {
  437. options.width = validAreaTxtWidth * PDF_SCALE;
  438. options.height = dftFontHeight * PDF_SCALE;
  439. doc.setFontSize(dftFontHeight);
  440. if (font[JV.FONT_PROPS[5]] === 'T' && parseInt(font.FontAngle) === 0) {
  441. private_drawUnderline(val, area);
  442. }
  443. doc.setFontSize(dftFontHeight * PDF_SCALE);
  444. doc.text(output[0] * PDF_SCALE, output[1] * PDF_SCALE, val);
  445. } else {
  446. while (true) {
  447. let lines = 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));
  448. lines = (lines === 0 || (control.Shrink === 'T' && control.ShrinkFirst === 'T'))?1:lines;
  449. let actLines = private_splitString(val, validAreaTxtWidth, doc);
  450. // if (actLines.length > lines && dftFontHeight >= 6) {
  451. if ((actLines.length > lines || _chkIfShinkFirst(control, actLines.length)) && dftFontHeight >= 6) {
  452. dftFontHeight--;
  453. doc.setFontSize(dftFontHeight);
  454. options.width = validAreaTxtWidth * PDF_SCALE;
  455. options.height = dftFontHeight * PDF_SCALE;
  456. } else {
  457. let aH = dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP] + 4;
  458. if ((aH * actLines.length) < (area[JV.IDX_BOTTOM] - area[JV.IDX_TOP]) && (control && control.Vertical !== 'top')) {
  459. if (control.Vertical === 'bottom') {
  460. area[JV.IDX_TOP] = area[JV.IDX_BOTTOM] - (aH * actLines.length);
  461. } else {
  462. area[JV.IDX_TOP] = (area[JV.IDX_TOP] + area[JV.IDX_BOTTOM]) / 2 - (aH * actLines.length) / 2
  463. area[JV.IDX_BOTTOM] = area[JV.IDX_TOP] + (aH * actLines.length);
  464. }
  465. }
  466. let newArea = [], baseTop = area[JV.IDX_TOP];
  467. for (let ai = 0; ai < area.length; ai++) {
  468. newArea[ai] = area[ai];
  469. }
  470. options.width = validAreaTxtWidth * PDF_SCALE;
  471. options.height = dftFontHeight * PDF_SCALE;
  472. for (let lIdx = 0; lIdx < actLines.length; lIdx++) {
  473. newArea[JV.IDX_TOP] = Math.round(aH * lIdx + baseTop);
  474. newArea[JV.IDX_BOTTOM] = Math.round(aH * (lIdx + 1) + baseTop);
  475. doc.setFontSize(dftFontHeight);
  476. inner_setupControl(actLines[lIdx], newArea, dftFontHeight, output);
  477. if (font[JV.FONT_PROPS[5]] === 'T' && parseInt(font.FontAngle) === 0) {
  478. private_drawUnderline(actLines[lIdx], newArea);
  479. }
  480. doc.setFontSize(dftFontHeight * PDF_SCALE);
  481. doc.text(output[0] * PDF_SCALE, output[1] * PDF_SCALE, actLines[lIdx]);
  482. }
  483. break;
  484. }
  485. }
  486. }
  487. }
  488. function private_setupAreaH(outVal, area, type, fontAngle, dftFontHeight, outputPoint,options) {
  489. //jspdf输出方式不同(但与H5的canvas处理方式有所不同,因为没有相关alignment属性,需要自己算,而且角度还暂时不支持)-------------------------------------------
  490. let lType = type;
  491. if (type !== "left" && type !== "right" && type !== "center") lType = "left";
  492. switch (lType) {
  493. case "left":
  494. if (fontAngle === JV.VERTICAL_ANGLE_INT) {
  495. // outputPoint[1] = 1 * area[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
  496. } else if (fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
  497. // outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
  498. } else {
  499. outputPoint[0] = 1 * area[JV.IDX_LEFT] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
  500. }
  501. //ctx.textAlign="start";
  502. break;
  503. case "right":
  504. if (fontAngle === JV.VERTICAL_ANGLE_INT) {
  505. outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
  506. } else if (fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
  507. outputPoint[1] = 1 * area[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
  508. } else {
  509. let width = doc.getTextWidth(outVal);
  510. outputPoint[0] = 1 * area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - width;
  511. }
  512. //ctx.textAlign="end";
  513. break;
  514. case "center":
  515. if (fontAngle === JV.VERTICAL_ANGLE_INT || fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
  516. outputPoint[1] = (1 * area[JV.IDX_TOP] + 1 * area[JV.IDX_BOTTOM]) / 2;
  517. } else {
  518. let width = doc.getTextWidth(outVal);
  519. outputPoint[0] = (1 * area[JV.IDX_LEFT] + 1 * area[JV.IDX_RIGHT]) / 2 - (width / 2);
  520. }
  521. //ctx.textAlign="center";
  522. break;
  523. }
  524. }
  525. function private_setupAreaV(outVal, area, type, fontAngle, dftFontHeight, outputPoint) {
  526. //jspdf输出方式不同(与H5的canvas一样处理)-------------------------------------------
  527. let lType = type;
  528. if (type !== "top" && type !== "bottom" && type !== "center") lType = "top";
  529. switch (lType) {
  530. case "top":
  531. if (fontAngle === JV.VERTICAL_ANGLE_INT) {
  532. outputPoint[0] = 1 * area[JV.IDX_RIGHT] - dftFontHeight - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
  533. } else if (fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
  534. outputPoint[0] = 1 * area[JV.IDX_LEFT] + dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
  535. } else outputPoint[1] = 1 * area[JV.IDX_TOP] + dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
  536. break;
  537. case "bottom":
  538. if (fontAngle === JV.VERTICAL_ANGLE_INT) {
  539. outputPoint[0] = 1 * area[JV.IDX_LEFT] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
  540. } else if (fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
  541. outputPoint[0] = 1 * area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
  542. } else {
  543. outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
  544. // if (fontName === "宋体") outputPoint[1] = outputPoint[1] - 1;
  545. outputPoint[1] = outputPoint[1] - 1; // 宋体需要提上一个像素点
  546. }
  547. break;
  548. case "center":
  549. if (fontAngle === JV.VERTICAL_ANGLE_INT) {
  550. outputPoint[0] = (1 * area[JV.IDX_LEFT] + 1 * area[JV.IDX_RIGHT] - dftFontHeight) / 2;
  551. } else if (fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
  552. outputPoint[0] = (1 * area[JV.IDX_LEFT] + 1 * area[JV.IDX_RIGHT] + dftFontHeight) / 2;
  553. } else {
  554. outputPoint[1] = (1 * area[JV.IDX_TOP] + 1 * area[JV.IDX_BOTTOM] + dftFontHeight) / 2;
  555. // if (fontName === "宋体") outputPoint[1] = outputPoint[1] - 1;
  556. outputPoint[1] = outputPoint[1] - 1; // 宋体需要提上一个像素点
  557. }
  558. break;
  559. }
  560. }
  561. function private_splitString(strVal, areaWidth, doc) {
  562. let rst = [];
  563. if (strVal) {
  564. let preSIdx = 0, txtWidth = 0;
  565. let currentW = 0;
  566. let chnW = doc.getTextWidth('一'), otherW = doc.getTextWidth('_');
  567. for (let sIdx = 0; sIdx < strVal.length; sIdx++) {
  568. currentW = (strVal.charCodeAt(sIdx) > 127)?chnW:otherW;
  569. txtWidth += currentW;
  570. if (txtWidth > areaWidth) {
  571. if (preSIdx < sIdx) {
  572. rst.push(strVal.substr(preSIdx, sIdx - preSIdx));
  573. preSIdx = sIdx;
  574. } else {
  575. rst.push(strVal.substr(preSIdx, 1));
  576. preSIdx = sIdx + 1;
  577. }
  578. txtWidth = currentW;
  579. }
  580. if (sIdx === strVal.length - 1) {
  581. rst.push(strVal.substr(preSIdx, strVal.length - preSIdx));
  582. }
  583. }
  584. }
  585. if (rst.length === 0) rst.push(''); //什么都没有,也得整个空串
  586. return rst;
  587. }
  588. function private_setupAreaRotateOption(area,w, type="top",dftFontHeight,outputPoint){
  589. let x = (area[JV.IDX_RIGHT] - area[JV.IDX_LEFT])/2+area[JV.IDX_LEFT];
  590. let y =(area[JV.IDX_BOTTOM] - area[JV.IDX_TOP])/2+ area[JV.IDX_TOP];
  591. let rotateOptions = {origin:[x,y]};
  592. let h = area[JV.IDX_RIGHT] - area[JV.IDX_LEFT];
  593. outputPoint[0]=x-w/2+JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
  594. switch (type) {
  595. case "top":
  596. outputPoint[1] = y- h/2+ JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
  597. break;
  598. case "bottom":
  599. outputPoint[1] = y+ h/2-JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]-dftFontHeight;
  600. break;
  601. case "center":
  602. outputPoint[1] = y+dftFontHeight/2;
  603. break;
  604. }
  605. return rotateOptions;
  606. }
  607. }
  608. }