rpt_svg_util.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * Created by Tony on 2018/6/27.
  3. * 报表直接打印需要
  4. */
  5. let JV = require('../rpt_component/jpc_value_define');
  6. let pdf = require('pdfkit');
  7. let jpcCmnHelper = require('../rpt_component/helper/jpc_helper_common');
  8. let SCREEN_DPI = jpcCmnHelper.getScreenDPI();
  9. let fontUtil = require('./rpt_font_util');
  10. module.exports = {
  11. exportSvgStr: function (pagesData, offsetX, offsetY) {
  12. let rst = [];
  13. let styles = pagesData[JV.NODE_STYLE_COLLECTION],
  14. fonts = pagesData[JV.NODE_FONT_COLLECTION],
  15. controls = pagesData[JV.NODE_CONTROL_COLLECTION]
  16. ;
  17. let pdf_doc = new pdf({autoFirstPage: false});
  18. for (let idx = 0; idx < pagesData.items.length; idx++) {
  19. let page = pagesData.items[idx];
  20. let svgPageArr = [], pixelSize = getPixelSize(pagesData);
  21. svgPageArr.push("<svg width='" + pixelSize[0] + "' height='" + pixelSize[1] + "'>");
  22. let adjustY = 0.5 * ((idx + 1) % 2);
  23. // let cnt = 0;
  24. for (let cell of page.cells) {
  25. svgPageArr.push(buildCellSvg(cell, fonts, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND], offsetX, offsetY, adjustY, pdf_doc));
  26. // cnt++;
  27. // console.log(cnt);
  28. }
  29. svgPageArr.push("</svg>");
  30. rst.push(svgPageArr);
  31. }
  32. return rst;
  33. }
  34. }
  35. function getActualBorderStyle(cell, styles, mergeBorderStyle, pageBorderArea, borderStr) {
  36. let rst = styles[cell[JV.PROP_STYLE]][borderStr];
  37. if (mergeBorderStyle) {
  38. if (parseFloat(cell[JV.PROP_AREA][borderStr]) === parseFloat(pageBorderArea[borderStr])) {
  39. if (borderStr === JV.PROP_LEFT || borderStr === JV.PROP_RIGHT) {
  40. if (parseFloat(cell[JV.PROP_AREA][JV.PROP_TOP]) >= parseFloat(pageBorderArea[JV.PROP_TOP]) &&
  41. parseFloat(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) <= parseFloat(pageBorderArea[JV.PROP_BOTTOM])) {
  42. rst = mergeBorderStyle[borderStr];
  43. }
  44. } else if (borderStr === JV.PROP_TOP || borderStr === JV.PROP_BOTTOM) {
  45. if (parseFloat(cell[JV.PROP_AREA][JV.PROP_LEFT]) >= parseFloat(pageBorderArea[JV.PROP_LEFT]) &&
  46. parseFloat(cell[JV.PROP_AREA][JV.PROP_RIGHT]) <= parseFloat(pageBorderArea[JV.PROP_RIGHT])) {
  47. rst = mergeBorderStyle[borderStr];
  48. }
  49. }
  50. }
  51. }
  52. return rst;
  53. }
  54. function buildCellSvg(cell, fonts, styles, controls, pageMergeBorder, rptMergeBorder, offsetX, offsetY, adjustY, pdf_doc) {
  55. let rst = [];
  56. let style = styles[cell[JV.PROP_STYLE]];
  57. let mergeBandStyle = null;
  58. if (rptMergeBorder) {
  59. mergeBandStyle = styles[rptMergeBorder[JV.PROP_STYLE][JV.PROP_ID]];
  60. }
  61. let font = cell[JV.PROP_FONT];
  62. if (typeof font === 'string') {
  63. font = fonts[cell[JV.PROP_FONT]];
  64. }
  65. let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]) + offsetX + 0.5,
  66. right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]) + offsetX + 0.5,
  67. top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]) + offsetY + adjustY,
  68. bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY
  69. ;
  70. if (style) {
  71. let leftBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_LEFT);
  72. // if (style[JV.PROP_LEFT] && parseFloat(style[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) > 0) {
  73. if (leftBS && parseFloat(leftBS[JV.PROP_LINE_WEIGHT]) > 0) {
  74. rst.push("<line x1='" + left + "' y1='" + top +
  75. "' x2='" + left + "' y2='" + bottom +
  76. "' style='stroke:rgb(0,0,0);stroke-width:" + leftBS[JV.PROP_LINE_WEIGHT] +"'/>")
  77. }
  78. let rightBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_RIGHT);
  79. // if (style[JV.PROP_RIGHT] && parseFloat(style[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) > 0) {
  80. if (rightBS && parseFloat(rightBS[JV.PROP_LINE_WEIGHT]) > 0) {
  81. rst.push("<line x1='" + right + "' y1='" + top +
  82. "' x2='" + right + "' y2='" + bottom +
  83. "' style='stroke:rgb(0,0,0);stroke-width:" + rightBS[JV.PROP_LINE_WEIGHT] +"'/>")
  84. }
  85. let topBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_TOP);
  86. // if (style[JV.PROP_TOP] && parseFloat(style[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) > 0) {
  87. if (topBS && parseFloat(topBS[JV.PROP_LINE_WEIGHT]) > 0) {
  88. rst.push("<line x1='" + left + "' y1='" + top +
  89. "' x2='" + right + "' y2='" + top +
  90. "' style='stroke:rgb(0,0,0);stroke-width:" + topBS[JV.PROP_LINE_WEIGHT] +"'/>")
  91. }
  92. let bottomBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_BOTTOM);
  93. // if (style[JV.PROP_BOTTOM] && parseFloat(style[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) > 0) {
  94. if (bottomBS && parseFloat(bottomBS[JV.PROP_LINE_WEIGHT]) > 0) {
  95. rst.push("<line x1='" + left + "' y1='" + bottom +
  96. "' x2='" + right + "' y2='" + bottom +
  97. "' style='stroke:rgb(0,0,0);stroke-width:" + bottomBS[JV.PROP_LINE_WEIGHT] +"'/>")
  98. }
  99. }
  100. buildText(rst, cell, font, controls[cell[JV.PROP_CONTROL]], offsetX, offsetY, adjustY, pdf_doc);
  101. return rst.join("");
  102. }
  103. function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, pdf_doc) {
  104. let orgFontHeight = parseInt(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]]);
  105. let fontWeight = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === 'T')?"bold":"normal";
  106. let fontStyle = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === 'T')?"italic":"normal";
  107. let dftFontBold = font[JV.FONT_PROPS[3]];
  108. let dftFontItalic = font[JV.FONT_PROPS[4]];
  109. let fontFile = __dirname + '/pdf_base_files/' + fontUtil.getActualFont(font[JV.FONT_PROPS[0]], (dftFontBold === 'T'), (dftFontItalic === 'T')) + '.ttf';
  110. let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]) + offsetX + 0.5,
  111. right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]) + offsetX + 0.5,
  112. top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]) + offsetY + adjustY,
  113. bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY,
  114. x = left, y = top,
  115. text_anchor = "start"
  116. ;
  117. let value = cell[JV.PROP_VALUE];
  118. if (!(value)) {
  119. value = "";
  120. }
  121. let values = null;
  122. if (typeof value === "string") {
  123. values = value.split("|");
  124. } else {
  125. values = [value];
  126. }
  127. let stepHeight = (parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) - parseInt(cell[JV.PROP_AREA][JV.PROP_TOP])) / values.length;
  128. if (control) {
  129. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "left") {
  130. text_anchor = "start";
  131. x = left + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
  132. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "right") {
  133. text_anchor = "end";
  134. x = right - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
  135. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "center") {
  136. text_anchor = "middle";
  137. x = Math.round((left + right) / 2);
  138. }
  139. }
  140. //*/
  141. let height = cell[JV.PROP_AREA][JV.PROP_BOTTOM] - cell[JV.PROP_AREA][JV.PROP_TOP];
  142. 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];
  143. let inner_draw_text = function (textValue) {
  144. let dftFontHeight = orgFontHeight;
  145. pdf_doc.font(fontFile);
  146. pdf_doc.fontSize(dftFontHeight);
  147. let actLines = private_splitString(textValue, (area[JV.PROP_RIGHT] - area[JV.PROP_LEFT]), pdf_doc);
  148. function inner_build_text(innerTxt, innerArea) {
  149. let innerDftFontHeight = (dftFontHeight * 3 / 4); //SVG的字体与canvas的字体大小的切换, 不用考虑取整
  150. if (control) {
  151. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "top") {
  152. y = innerArea[JV.PROP_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
  153. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "bottom") {
  154. y = innerArea[JV.PROP_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
  155. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "center") {
  156. y = Math.round((innerArea[JV.PROP_TOP] + innerArea[JV.PROP_BOTTOM] + innerDftFontHeight) / 2 );
  157. }
  158. }
  159. if (font[JV.PROP_NAME] === "宋体") {
  160. y--;
  161. }
  162. destRst.push("<text style='fill:black;font-family:" + font[JV.PROP_NAME] +
  163. ";font-weight:" + fontWeight +
  164. ";font-style:" + fontStyle +
  165. ";font-size:" + innerDftFontHeight + "pt' x='" +
  166. x +"' y='" + y + "' text-anchor='" + text_anchor + "' xml:space='preserve'>" + innerTxt + "</text>");
  167. }
  168. if (actLines.length === 1 || (control && control.Shrink !== 'T')) {
  169. inner_build_text(textValue, area);
  170. } else {
  171. while (true) {
  172. if (dftFontHeight > 6) {
  173. dftFontHeight--;
  174. pdf_doc.fontSize(dftFontHeight);
  175. 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));
  176. lines = (lines === 0)?1:lines;
  177. actLines = private_splitString(textValue, (area[JV.PROP_RIGHT] - area[JV.PROP_LEFT]), pdf_doc);
  178. if (lines >= actLines.length) {
  179. let aH = dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP] + 4;
  180. if ((aH * actLines.length) < (area[JV.IDX_BOTTOM] - area[JV.IDX_TOP]) && (control && control.Vertical !== 'top')) {
  181. if (control.Vertical === 'bottom') {
  182. area[JV.IDX_TOP] = area[JV.IDX_BOTTOM] - (aH * actLines.length);
  183. } else {
  184. area[JV.IDX_TOP] = (area[JV.IDX_TOP] + area[JV.IDX_BOTTOM]) / 2 - (aH * actLines.length) / 2
  185. area[JV.IDX_BOTTOM] = area[JV.IDX_TOP] + (aH * actLines.length);
  186. }
  187. }
  188. let newArea = [], baseTop = area[JV.IDX_TOP];
  189. for (let ai = 0; ai < area.length; ai++) {
  190. newArea[ai] = area[ai];
  191. }
  192. for (let lIdx = 0; lIdx < actLines.length; lIdx++) {
  193. newArea[JV.IDX_TOP] = Math.round(aH * lIdx + baseTop);
  194. newArea[JV.IDX_BOTTOM] = Math.round(aH * (lIdx + 1) + baseTop);
  195. inner_build_text(actLines[lIdx], newArea);
  196. }
  197. break;
  198. }
  199. } else {
  200. inner_build_text(textValue, area);
  201. break;
  202. }
  203. }
  204. }
  205. };
  206. for (let vidx = 0; vidx < values.length; vidx++) {
  207. area[JV.IDX_TOP] = cell[JV.PROP_AREA][JV.PROP_TOP] + vidx * (height / values.length);
  208. area[JV.IDX_BOTTOM] = cell[JV.PROP_AREA][JV.PROP_TOP] + (vidx + 1) * (height / values.length);
  209. inner_draw_text(values[vidx], area, font, control);
  210. }
  211. /*/
  212. for (let vidx = 0; vidx < values.length; vidx++) {
  213. //check whether need to adjust the font size
  214. let dftFontHeight = orgFontHeight;
  215. pdf_doc.font(fontFile);
  216. pdf_doc.fontSize(dftFontHeight);
  217. while ((right - left) <= pdf_doc.widthOfString(values[vidx])) {
  218. if (dftFontHeight > 6) {
  219. dftFontHeight--;
  220. pdf_doc.fontSize(dftFontHeight);
  221. } else {
  222. break;
  223. }
  224. }
  225. dftFontHeight = (dftFontHeight * 3 / 4); //SVG的字体与canvas的字体大小的切换, 不用考虑取整
  226. if (control) {
  227. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "top") {
  228. y = Math.round((top + vidx * stepHeight) + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP]);
  229. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "bottom") {
  230. y = Math.round((top + (vidx + 1) * stepHeight) - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]);
  231. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "center") {
  232. y = Math.round(((top + vidx * stepHeight) + (top + (vidx + 1) * stepHeight) + dftFontHeight) / 2 );
  233. }
  234. }
  235. if (font[JV.PROP_NAME] === "宋体") {
  236. y--;
  237. }
  238. destRst.push("<text style='fill:black;font-family:" + font[JV.PROP_NAME] +
  239. ";font-weight:" + fontWeight +
  240. ";font-style:" + fontStyle +
  241. ";font-size:" + dftFontHeight + "pt' x='" +
  242. x +"' y='" + y + "' text-anchor='" + text_anchor + "' xml:space='preserve'>" + values[vidx] + "</text>");
  243. }
  244. //*/
  245. }
  246. function private_splitString(strVal, areaWidth, doc) {
  247. let rst = [];
  248. if (strVal) {
  249. let preSIdx = 0, txtWidth = 0;
  250. let currentW = 0;
  251. let chnW = doc.widthOfString('一'), otherW = doc.widthOfString('_');
  252. for (let sIdx = 0; sIdx < strVal.length; sIdx++) {
  253. currentW = (strVal.charCodeAt(sIdx) > 127)?chnW:otherW;
  254. txtWidth += currentW;
  255. if (txtWidth > areaWidth) {
  256. if (preSIdx < sIdx) {
  257. rst.push(strVal.substr(preSIdx, sIdx - preSIdx));
  258. preSIdx = sIdx;
  259. } else {
  260. rst.push(strVal.substr(preSIdx, 1));
  261. preSIdx = sIdx + 1;
  262. }
  263. txtWidth = currentW;
  264. }
  265. if (sIdx === strVal.length - 1) {
  266. rst.push(strVal.substr(preSIdx, strVal.length - preSIdx));
  267. }
  268. }
  269. }
  270. if (rst.length === 0) rst.push(''); //什么都没有,也得整个空串
  271. return rst;
  272. }
  273. function getPixelSize(pagesData) {
  274. let rst = [793,1122];
  275. if (pagesData[JV.NODE_PAGE_INFO] && pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE]) {
  276. rst[0] = Math.round(SCREEN_DPI[0] * pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0]);
  277. rst[1] = Math.round(SCREEN_DPI[1] * pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]);
  278. }
  279. return rst;
  280. }