rpt_svg_util.js 15 KB

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