TonyKang 5 år sedan
förälder
incheckning
a7d2db451c

+ 2 - 2
modules/reports/rpt_component/helper/jpc_helper_common.js

@@ -133,7 +133,7 @@ let JpcCommonHelper = {
         }
         return rst;
     },
-    getStringLinesInArea: function(area, strVal, pdfDoc, chnW, otherW) {
+    getStringLinesInArea: function(area, strVal, chnW, otherW) {
         //备注: 因后台的pdf kit判断字符串长度与前端的不一样,需要做些调整,不一次性地判断字符串长度。
         //      分2种字符:中文与非中文,按照各种字符的数量分别乘以相关一个字符的宽度再累计。
         //      另判断行数还不能直接用总长度除以宽度来计算,因每一行都会有不同的余量,所以得一行行走过来判断。
@@ -157,7 +157,7 @@ let JpcCommonHelper = {
         return rst;
         //备注: 其实是想用canvas的,但node canvas装起来麻烦,暂时用PDF Kit来顶用一下,以后有更好的再换
     },
-    splitString: function (area, strVal, pdfDoc, chnW, otherW) {
+    splitString: function (area, strVal, chnW, otherW) {
         let rst = [];
         if (strVal) {
             let areaWidth = area[JV.PROP_RIGHT] - area[JV.PROP_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] - 1;

+ 20 - 12
modules/reports/rpt_component/jpc_flow_tab.js

@@ -9,10 +9,11 @@ let JpcDiscreteHelper = require('./helper/jpc_helper_discrete');
 let JpcTextHelper = require('./helper/jpc_helper_text');
 let JpcCommonOutputHelper = require('./helper/jpc_helper_common_output');
 let JpcAreaHelper = require('./helper/jpc_helper_area');
-let PDFKit = require('pdfkit');
-let fontUtil = require('../util/rpt_font_util');
+// let PDFKit = require('pdfkit');
+// let fontUtil = require('../util/rpt_font_util');
 let fsUtil = require("../../../public/fsUtil");
 let strUtil = require("../../../public/stringUtil");
+let fontWidthMap = require('./helper/jpc_helper_font_width');
 
 
 let JpcFlowTabSrv = function(){};
@@ -339,7 +340,7 @@ JpcFlowTabSrv.prototype.createNew = function(){
             let adHocAutoHeightStyleStr = [];
             let reg1 = new RegExp('\n\r','g'), reg2 = new RegExp('\r\n','g'),
                 reg3 = new RegExp('\n','g'), reg4 = new RegExp('\r','g');
-            let doc = new PDFKit({autoFirstPage: false});
+            // let doc = new PDFKit({autoFirstPage: false});
             let fonts = defProperties.fonts;
             let fontCache = {};
             let private_get_font = function (fontKey) {
@@ -391,19 +392,26 @@ JpcFlowTabSrv.prototype.createNew = function(){
                         let values = value.split('|');
                         if (values.length > rst) rst = values.length;
                         let font = private_get_font(tab_field[JV.PROP_FONT]);
+                        // if (font) {
+                        //     let fontFile = __dirname.slice(0, __dirname.length - 14) + '/util/pdf_base_files/' + fontUtil.getActualFont(font[JV.FONT_PROPS[0]], (font[JV.FONT_PROPS[3]] === 'T'), (font[JV.FONT_PROPS[4]] === 'T')) + '.ttf';
+                        //     doc.font(fontFile);
+                        //     doc.fontSize(parseInt(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]]));
+                        // } else {
+                        //     doc.font(__dirname.slice(0, __dirname.length - 14) + '/util/pdf_base_files/Smart.ttf');
+                        //     doc.fontSize(12);
+                        // }
+                        let chkFontName = '宋体';
+                        let chkFontHeight = 12;
                         if (font) {
-                            let fontFile = __dirname.slice(0, __dirname.length - 14) + '/util/pdf_base_files/' + fontUtil.getActualFont(font[JV.FONT_PROPS[0]], (font[JV.FONT_PROPS[3]] === 'T'), (font[JV.FONT_PROPS[4]] === 'T')) + '.ttf';
-                            doc.font(fontFile);
-                            doc.fontSize(parseInt(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]]));
-                        } else {
-                            doc.font(__dirname.slice(0, __dirname.length - 14) + '/util/pdf_base_files/Smart.ttf');
-                            doc.fontSize(12);
+                            chkFontName = font[JV.FONT_PROPS[0]];
+                            chkFontHeight = font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]];
                         }
                         let hasSplitStr = false, splitStrArr = [];
                         let accAmt = 0;
-                        let chnW = doc.widthOfString('一'), otherW = doc.widthOfString('_');
+                        // let chnW = doc.widthOfString('一'), otherW = doc.widthOfString('_');
+                        let chnW = fontWidthMap.getFontWidth(chkFontName, chkFontHeight, '宽'), otherW = fontWidthMap.getFontWidth(chkFontName, chkFontHeight, '窄');
                         for (let i = 0; i < values.length; i++) {
-                            let amt = JpcCommonHelper.getStringLinesInArea(area, values[i], doc, chnW, otherW);
+                            let amt = JpcCommonHelper.getStringLinesInArea(area, values[i], chnW, otherW);
                             accAmt += amt;
                             if (amt > 1) {
                                 hasSplitStr = true;
@@ -418,7 +426,7 @@ JpcFlowTabSrv.prototype.createNew = function(){
                                 if (splitStrArr.indexOf(i) < 0) {
                                     newValArr.push(values[i]);
                                 } else {
-                                    newValArr = newValArr.concat(JpcCommonHelper.splitString(area, values[i], doc, chnW, otherW));
+                                    newValArr = newValArr.concat(JpcCommonHelper.splitString(area, values[i], chnW, otherW));
                                 }
                             }
                             JpcFieldHelper.setValue(data_field, theRecIdx, newValArr.join('|'));

+ 19 - 15
modules/reports/util/rpt_svg_util.js

@@ -4,10 +4,11 @@
  */
 
 let JV = require('../rpt_component/jpc_value_define');
-let pdf = require('pdfkit');
+// let pdf = require('pdfkit');
 let jpcCmnHelper = require('../rpt_component/helper/jpc_helper_common');
 let SCREEN_DPI = jpcCmnHelper.getScreenDPI();
-let fontUtil = require('./rpt_font_util');
+// let fontUtil = require('./rpt_font_util');
+let fontWidthMap = require('../rpt_component/helper/jpc_helper_font_width');
 
 module.exports = {
     exportSvgStr: function (pagesData, offsetX, offsetY) {
@@ -16,7 +17,7 @@ module.exports = {
             fonts = pagesData[JV.NODE_FONT_COLLECTION],
             controls = pagesData[JV.NODE_CONTROL_COLLECTION]
         ;
-        let pdf_doc = new pdf({autoFirstPage: false});
+        // let pdf_doc = new pdf({autoFirstPage: false});
         for (let idx = 0; idx < pagesData.items.length; idx++) {
             let page = pagesData.items[idx];
             let svgPageArr = [], pixelSize = getPixelSize(pagesData);
@@ -24,7 +25,7 @@ module.exports = {
             let adjustY = 0.5 * ((idx + 1) % 2);
             // let cnt = 0;
             for (let cell of page.cells) {
-                svgPageArr.push(buildCellSvg(cell, fonts, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND], offsetX, offsetY, adjustY, pdf_doc));
+                svgPageArr.push(buildCellSvg(cell, fonts, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND], offsetX, offsetY, adjustY));
                 // cnt++;
                 // console.log(cnt);
             }
@@ -55,7 +56,7 @@ function getActualBorderStyle(cell, styles, mergeBorderStyle, pageBorderArea, bo
     return rst;
 }
 
-function buildCellSvg(cell, fonts, styles, controls, pageMergeBorder, rptMergeBorder, offsetX, offsetY, adjustY, pdf_doc) {
+function buildCellSvg(cell, fonts, styles, controls, pageMergeBorder, rptMergeBorder, offsetX, offsetY, adjustY) {
     let rst = [];
     let style = styles[cell[JV.PROP_STYLE]];
     let mergeBandStyle = null;
@@ -101,18 +102,18 @@ function buildCellSvg(cell, fonts, styles, controls, pageMergeBorder, rptMergeBo
                 "' style='stroke:rgb(0,0,0);stroke-width:" + bottomBS[JV.PROP_LINE_WEIGHT] +"'/>")
         }
     }
-    buildText(rst, cell, font, controls[cell[JV.PROP_CONTROL]], offsetX, offsetY, adjustY, pdf_doc);
+    buildText(rst, cell, font, controls[cell[JV.PROP_CONTROL]], offsetX, offsetY, adjustY);
 
     return rst.join("");
 }
 
-function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, pdf_doc) {
+function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY) {
     let orgFontHeight = parseInt(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]]);
     let fontWeight = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === 'T')?"bold":"normal";
     let fontStyle = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === 'T')?"italic":"normal";
     let dftFontBold = font[JV.FONT_PROPS[3]];
     let dftFontItalic = font[JV.FONT_PROPS[4]];
-    let fontFile = __dirname + '/pdf_base_files/' + fontUtil.getActualFont(font[JV.FONT_PROPS[0]], (dftFontBold === 'T'), (dftFontItalic === 'T')) + '.ttf';
+    // let fontFile = __dirname + '/pdf_base_files/' + fontUtil.getActualFont(font[JV.FONT_PROPS[0]], (dftFontBold === 'T'), (dftFontItalic === 'T')) + '.ttf';
     let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]) + offsetX + 0.5,
         right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]) + offsetX + 0.5,
         top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]) + offsetY + adjustY,
@@ -148,9 +149,10 @@ function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, pdf_
     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];
     let inner_draw_text = function (textValue) {
         let dftFontHeight = orgFontHeight;
-        pdf_doc.font(fontFile);
-        pdf_doc.fontSize(dftFontHeight);
-        let actLines = private_splitString(textValue, (area[JV.PROP_RIGHT] - area[JV.PROP_LEFT]), pdf_doc);
+        // pdf_doc.font(fontFile);
+        // pdf_doc.fontSize(dftFontHeight);
+        // let actLines = private_splitString(textValue, (area[JV.PROP_RIGHT] - area[JV.PROP_LEFT]), pdf_doc);
+        let actLines = private_splitStringEx(textValue, (area[JV.PROP_RIGHT] - area[JV.PROP_LEFT]), font[JV.FONT_PROPS[0]], dftFontHeight);
         function inner_build_text(innerTxt, innerArea) {
             let innerDftFontHeight = (dftFontHeight * 3 / 4); //SVG的字体与canvas的字体大小的切换, 不用考虑取整
             if (control) {
@@ -177,10 +179,11 @@ function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, pdf_
             while (true) {
                 if (dftFontHeight > 6) {
                     dftFontHeight--;
-                    pdf_doc.fontSize(dftFontHeight);
+                    // pdf_doc.fontSize(dftFontHeight);
                     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));
                     lines = (lines === 0)?1:lines;
-                    actLines = private_splitString(textValue, (area[JV.PROP_RIGHT] - area[JV.PROP_LEFT]), pdf_doc);
+                    // actLines = private_splitString(textValue, (area[JV.PROP_RIGHT] - area[JV.PROP_LEFT]), pdf_doc);
+                    let actLines = private_splitStringEx(textValue, (area[JV.PROP_RIGHT] - area[JV.PROP_LEFT]), font[JV.FONT_PROPS[0]], dftFontHeight);
                     if (lines >= actLines.length) {
                         let aH = dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP] + 4;
                         if ((aH * actLines.length) < (area[JV.IDX_BOTTOM] - area[JV.IDX_TOP]) && (control && control.Vertical !== 'top')) {
@@ -250,12 +253,13 @@ function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, pdf_
     //*/
 }
 
-function private_splitString(strVal, areaWidth, doc) {
+function private_splitStringEx(strVal, areaWidth, chkFontName, chkFontHeight) {
     let rst = [];
     if (strVal) {
         let preSIdx = 0, txtWidth = 0;
         let currentW = 0;
-        let chnW = doc.widthOfString('一'), otherW = doc.widthOfString('_');
+        // let chnW = doc.widthOfString('一'), otherW = doc.widthOfString('_');
+        let chnW = fontWidthMap.getFontWidth(chkFontName, chkFontHeight, '宽'), otherW = fontWidthMap.getFontWidth(chkFontName, chkFontHeight, '窄');
         for (let sIdx = 0; sIdx < strVal.length; sIdx++) {
             currentW = (strVal.charCodeAt(sIdx) > 127)?chnW:otherW;
             txtWidth += currentW;