浏览代码

后台计算string长度与前台计算不一致问题

TonyKang 6 年之前
父节点
当前提交
7ca7f8dfc8

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

@@ -1,4 +1,5 @@
 let JV = require('../jpc_value_define');
+let stringUtil = require('../../../../public/stringUtil');
 
 let JpcCommonHelper = {
     commonConstant: {},
@@ -134,7 +135,15 @@ let JpcCommonHelper = {
     },
     getStringLinesInArea: function(area, strVal, pdfDoc) {
         let areaWidth = area[JV.PROP_RIGHT] - area[JV.PROP_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] - 2;
-        let txtWidth = pdfDoc.widthOfString(strVal);
+        // let re = /[\u4E00-\u9FA5]/g;
+        let re = /[^\x00-\xff]/ig; //普通ASCII以外的字符
+        let cStr = strVal.match(re);
+        let chnAmt = (cStr)?cStr.length:0;
+        let chnW = pdfDoc.widthOfString('一'), otherW = pdfDoc.widthOfString('_');
+        //备注: 因后台的pdfDoc判断字符串长度与前端的不一样,需要做些调整,不一次性地判断字符串长度。
+        //      分2种字符:中文与非中文,按照各种字符的数量分别乘以相关一个字符的宽度再累计。
+        // let txtWidth = pdfDoc.widthOfString(strVal);
+        let txtWidth = chnAmt * chnW + (strVal.length - chnAmt) * otherW;
         let rst = parseInt(txtWidth / areaWidth);
         if (txtWidth % areaWidth > 0) {
             rst++;
@@ -145,10 +154,19 @@ let JpcCommonHelper = {
     },
     splitString: function (area, strVal, pdfDoc) {
         let rst = [];
+        // let re = /[\u4E00-\u9FA5]/g;
+        let re = /[^\x00-\xff]/ig; //普通ASCII以外的字符
+        let chnW = pdfDoc.widthOfString('一'), otherW = pdfDoc.widthOfString('_');
         let areaWidth = area[JV.PROP_RIGHT] - area[JV.PROP_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] - 2;
         let preSIdx = 0, txtWidth = 0;
+
         for (let sIdx = 1; sIdx <= strVal.length; sIdx++) {
-            txtWidth = pdfDoc.widthOfString(strVal.substr(preSIdx, sIdx - preSIdx));
+            let tmpStr = strVal.substr(preSIdx, sIdx - preSIdx);
+            let cStr = tmpStr.match(re);
+            let chnAmt = (cStr)?cStr.length:0;
+            // let chnAmt = tmpStr.match(re).length;
+            // txtWidth = pdfDoc.widthOfString(strVal.substr(preSIdx, sIdx - preSIdx));
+            txtWidth = chnAmt * chnW + (tmpStr.length - chnAmt) * otherW;
             if (txtWidth > areaWidth) {
                 rst.push(strVal.substr(preSIdx, sIdx - preSIdx - 1));
                 preSIdx = sIdx - 1;

+ 1 - 0
modules/reports/rpt_component/jpc_flow_tab.js

@@ -324,6 +324,7 @@ JpcFlowTabSrv.prototype.createNew = function(){
                         let value = JpcFieldHelper.getValue(data_field, theRecIdx);
                         let area = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, 1, 1, 0, 1, 0, 1, 0, false, false);
                         if (value !== null && value !== undefined) {
+                            value = value.replace('\t', '');
                             value = value.replace(reg1, '|').replace(reg2, '|').replace(reg3, '|').replace(reg4, '|');
                         } else {
                             value = '';

+ 3 - 2
test/unit/reports/test_tpl_09_1.js

@@ -26,7 +26,8 @@ let fsUtil = require("../../../public/fsUtil");
 
 let demoPrjId = - 1;
 // let demoRptId = 226;
-let demoRptId = 453; //copied
+// let demoRptId = 453; //copied
+let demoRptId = 612; //copied
 let pagesize = "A4";
 
 // let userId_Leng = "59cdf14a0034a1000ba52b97"; //小冷User Id 换成_id了 QQ号
@@ -62,7 +63,7 @@ test('测试 - 测试模板啦: ', function (t) {
         rptTplDataFacade.prepareProjectData(userId_Dft, demoPrjId, filter, function (err, msg, rawDataObj) {
             if (!err) {
                 try {
-                    fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/rptTplRawDataObject_建筑09-1表.jsp");
+                    // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/rptTplRawDataObject_建筑09-1表.jsp");
                     let tplData = rptDataUtil.assembleData(rawDataObj);
                     // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/rptTplRawDataAfterCacl_建筑09-1表.jsp");
                     // fsUtil.writeObjToFile(tplData, "D:/GitHome/ConstructionCost/tmp/rptTplAssembledData_建筑09-1表.jsp");