TonyKang 6 gadi atpakaļ
vecāks
revīzija
8d2b4efa87

+ 41 - 34
modules/reports/rpt_component/helper/jpc_helper_common.js

@@ -133,48 +133,55 @@ let JpcCommonHelper = {
         }
         return rst;
     },
-    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 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判断字符串长度与前端的不一样,需要做些调整,不一次性地判断字符串长度。
+    getStringLinesInArea: function(area, strVal, pdfDoc, chnW, otherW) {
+        //备注: 因后台的pdf kit判断字符串长度与前端的不一样,需要做些调整,不一次性地判断字符串长度。
         //      分2种字符:中文与非中文,按照各种字符的数量分别乘以相关一个字符的宽度再累计。
-        // let txtWidth = pdfDoc.widthOfString(strVal);
-        let txtWidth = chnAmt * chnW + (strVal.length - chnAmt) * otherW;
-        let rst = parseInt(txtWidth / areaWidth);
-        if (txtWidth % areaWidth > 0) {
-            rst++;
+        //      另判断行数还不能直接用总长度除以宽度来计算,因每一行都会有不同的余量,所以得一行行走过来判断。
+        let rst = 0;
+        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;
+            let txtWidth = 0, currentW = 0;
+            for (let sIdx = 0; sIdx < strVal.length; sIdx++) {
+                currentW = (strVal.charCodeAt(sIdx) > 127)?chnW:otherW;
+                txtWidth += currentW;
+                if (txtWidth > areaWidth) {
+                    rst++;
+                    txtWidth = currentW;
+                }
+                if (sIdx === strVal.length - 1) {
+                    rst++;
+                }
+            }
         }
         if (rst === 0) rst = 1; //即使是空字符串,也得有一行啊
         return rst;
-        //备注: 其实是想用canvas的,但node canvas装起来麻烦,暂时用PDF Kit来顶用一下,以后换新方法再用
+        //备注: 其实是想用canvas的,但node canvas装起来麻烦,暂时用PDF Kit来顶用一下,以后有更好的再换
     },
-    splitString: function (area, strVal, pdfDoc) {
+    splitString: function (area, strVal, pdfDoc, chnW, otherW) {
         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++) {
-            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;
-            }
-            if (sIdx === strVal.length) {
-                rst.push(strVal.substr(preSIdx, sIdx - preSIdx));
+        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;
+            let preSIdx = 0, txtWidth = 0;
+            let currentW = 0;
+            for (let sIdx = 0; sIdx < strVal.length; sIdx++) {
+                currentW = (strVal.charCodeAt(sIdx) > 127)?chnW:otherW;
+                txtWidth += currentW;
+                if (txtWidth > areaWidth) {
+                    if (preSIdx < sIdx) {
+                        rst.push(strVal.substr(preSIdx, sIdx - preSIdx));
+                        preSIdx = sIdx;
+                    } else {
+                        rst.push(strVal.substr(preSIdx, 1));
+                        preSIdx = sIdx + 1;
+                    }
+                    txtWidth = currentW;
+                }
+                if (sIdx === strVal.length - 1) {
+                    rst.push(strVal.substr(preSIdx, strVal.length - preSIdx));
+                }
             }
         }
+        if (rst.length === 0) rst.push(''); //什么都没有,也得整个空串
         return rst;
     }
 };

+ 3 - 3
modules/reports/rpt_component/jpc_flow_tab.js

@@ -343,10 +343,10 @@ JpcFlowTabSrv.prototype.createNew = function(){
                         }
                         let hasSplitStr = false, splitStrArr = [];
                         let accAmt = 0;
+                        let chnW = doc.widthOfString('一'), otherW = doc.widthOfString('_');
                         for (let i = 0; i < values.length; i++) {
-                            let amt = JpcCommonHelper.getStringLinesInArea(area, values[i], doc);
+                            let amt = JpcCommonHelper.getStringLinesInArea(area, values[i], doc, chnW, otherW);
                             accAmt += amt;
-                            // rst += amt;
                             if (amt > 1) {
                                 hasSplitStr = true;
                                 splitStrArr.push(i);
@@ -359,7 +359,7 @@ JpcFlowTabSrv.prototype.createNew = function(){
                                 if (splitStrArr.indexOf(i) < 0) {
                                     newValArr.push(values[i]);
                                 } else {
-                                    newValArr = newValArr.concat(JpcCommonHelper.splitString(area, values[i], doc));
+                                    newValArr = newValArr.concat(JpcCommonHelper.splitString(area, values[i], doc, chnW, otherW));
                                 }
                             }
                             JpcFieldHelper.setValue(data_field, theRecIdx, newValArr.join('|'));