Parcourir la source

BUG#1954 code sync

TonyKang il y a 6 ans
Parent
commit
ebaa23f0fc
1 fichiers modifiés avec 59 ajouts et 38 suppressions
  1. 59 38
      modules/reports/util/rpt_pdf_util.js

+ 59 - 38
modules/reports/util/rpt_pdf_util.js

@@ -5,11 +5,12 @@
  * Created by zhang on 2017/8/14.
  * Created by zhang on 2017/8/14.
  */
  */
 
 
-let pdf = require('pdfkit');
-let fs = require('fs');
-let jpcCmnHelper = require('../rpt_component/helper/jpc_helper_common');
-let DPI = jpcCmnHelper.getScreenDPI()[0];
-let JV = require('../rpt_component/jpc_value_define');
+const pdf = require('pdfkit');
+const PDF_SCALE = 0.75;
+const fs = require('fs');
+const jpcCmnHelper = require('../rpt_component/helper/jpc_helper_common');
+const DPI = jpcCmnHelper.getScreenDPI()[0] * PDF_SCALE;
+const JV = require('../rpt_component/jpc_value_define');
 const uuidV1 = require('uuid/v1');
 const uuidV1 = require('uuid/v1');
 
 
 let fontUtil = require('./rpt_font_util');
 let fontUtil = require('./rpt_font_util');
@@ -84,7 +85,7 @@ function export_pdf_file (pageData, paperSize, fName, callback) {
 
 
     function private_drawCell(cell, fonts, styles, controls, mergedBand) {
     function private_drawCell(cell, fonts, styles, controls, mergedBand) {
         doc.save();
         doc.save();
-        //doc.translate(0.5,0.5);
+        // doc.translate(0.5,0.5); //跟H5的canvas不同,不需要这样切换
         let style = styles[cell[JV.PROP_STYLE]];
         let style = styles[cell[JV.PROP_STYLE]];
         if (style) {
         if (style) {
             let isNeedMergeBand = private_chkIfInMergedBand(mergedBand, cell);
             let isNeedMergeBand = private_chkIfInMergedBand(mergedBand, cell);
@@ -106,10 +107,12 @@ function export_pdf_file (pageData, paperSize, fName, callback) {
                 destStyle = styles[mergedBand[JV.PROP_STYLE][JV.PROP_ID]];
                 destStyle = styles[mergedBand[JV.PROP_STYLE][JV.PROP_ID]];
             }
             }
         }
         }
-        doc.moveTo(cell[JV.PROP_AREA][startP[0]] + offsetX, cell[JV.PROP_AREA][startP[1]] + offsetY);
+        // doc.moveTo(cell[JV.PROP_AREA][startP[0]] + offsetX, cell[JV.PROP_AREA][startP[1]] + offsetY);
+        doc.moveTo( (cell[JV.PROP_AREA][startP[0]] + offsetX) * PDF_SCALE, (cell[JV.PROP_AREA][startP[1]] + offsetY) * PDF_SCALE);
         if (destStyle[styleBorderDest] && parseFloat(destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT]) !== 0) {
         if (destStyle[styleBorderDest] && parseFloat(destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT]) !== 0) {
             doc.lineWidth(1.0 * destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT]);
             doc.lineWidth(1.0 * destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT]);
-            doc.lineTo(cell[JV.PROP_AREA][destP[0]] + offsetX, cell[JV.PROP_AREA][destP[1]] + offsetY);
+            // doc.lineTo(cell[JV.PROP_AREA][destP[0]] + offsetX, cell[JV.PROP_AREA][destP[1]] + offsetY);
+            doc.lineTo((cell[JV.PROP_AREA][destP[0]] + offsetX) * PDF_SCALE, (cell[JV.PROP_AREA][destP[1]] + offsetY) * PDF_SCALE);
             doc.strokeColor(destStyle[styleBorderDest][JV.PROP_COLOR]);
             doc.strokeColor(destStyle[styleBorderDest][JV.PROP_COLOR]);
         }
         }
         doc.stroke();
         doc.stroke();
@@ -162,34 +165,39 @@ function export_pdf_file (pageData, paperSize, fName, callback) {
             }
             }
         };
         };
         inner_setupControl(area, dftFontHeight, output);
         inner_setupControl(area, dftFontHeight, output);
-        let w = area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - area[JV.IDX_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
+        let validAreaTxtWidth = area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - area[JV.IDX_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
+        let validTxtLines = 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));
         if (parseInt(font.FontAngle) !== 0) {
         if (parseInt(font.FontAngle) !== 0) {
-            w = area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] - area[JV.IDX_TOP] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
+            validAreaTxtWidth = area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] - area[JV.IDX_TOP] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
+            validTxtLines = Math.floor((area[JV.IDX_RIGHT] - area[JV.IDX_LEFT]) / (dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] + 4));
         }
         }
 
 
-        function private_drawUnderline() {
+        function private_drawUnderline(underLineVal, underLineArea) {
             //A. 暂不支持角度; B. PDF输出时,坐标没有translate
             //A. 暂不支持角度; B. PDF输出时,坐标没有translate
             let ctx = doc;
             let ctx = doc;
             //1. 计算下划线的相关坐标
             //1. 计算下划线的相关坐标
-            let width = ctx.widthOfString(val);
+            let width = ctx.widthOfString(underLineVal);
+            if (width > underLineArea[JV.IDX_RIGHT] - underLineArea[JV.IDX_LEFT]) {
+                width = underLineArea[JV.IDX_RIGHT] - underLineArea[JV.IDX_LEFT];
+            }
             let height = dftFontHeight;
             let height = dftFontHeight;
-            let startX = area[JV.IDX_LEFT], startY = area[JV.IDX_TOP], endX = area[JV.IDX_RIGHT], endY = area[JV.IDX_BOTTOM];
+            let startX = underLineArea[JV.IDX_LEFT], startY = underLineArea[JV.IDX_TOP], endX = underLineArea[JV.IDX_RIGHT], endY = underLineArea[JV.IDX_BOTTOM];
             // let startX = 0, startY = 0, endX = width, endY = startY;
             // let startX = 0, startY = 0, endX = width, endY = startY;
             if (control.Horizon === "left") {
             if (control.Horizon === "left") {
-                startX = Math.round(area[JV.IDX_LEFT] + JV.OUTPUT_OFFSET[JV.IDX_LEFT]);
+                startX = Math.round(underLineArea[JV.IDX_LEFT] + JV.OUTPUT_OFFSET[JV.IDX_LEFT]);
             } else if (control.Horizon === "right") {
             } else if (control.Horizon === "right") {
-                startX = Math.round(area[JV.IDX_RIGHT] - width - JV.OUTPUT_OFFSET[JV.IDX_RIGHT]);
+                startX = Math.round(underLineArea[JV.IDX_RIGHT] - width - JV.OUTPUT_OFFSET[JV.IDX_RIGHT]);
             } else {
             } else {
-                startX = Math.round( area[JV.IDX_LEFT] + (area[JV.IDX_RIGHT] - area[JV.IDX_LEFT] - width) / 2);
+                startX = Math.round( underLineArea[JV.IDX_LEFT] + (underLineArea[JV.IDX_RIGHT] - underLineArea[JV.IDX_LEFT] - width) / 2);
             }
             }
             endX = Math.round(startX + width);
             endX = Math.round(startX + width);
 
 
             if (control.Vertical === "top") {
             if (control.Vertical === "top") {
-                startY = Math.round(area[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM] + height);
+                startY = Math.round(underLineArea[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM] + height);
             } else if (control.Vertical === "bottom") {
             } else if (control.Vertical === "bottom") {
-                startY = Math.round(area[JV.IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM]);
+                startY = Math.round(underLineArea[JV.IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM]);
             } else {
             } else {
-                startY = Math.round( area[JV.IDX_TOP] + (area[JV.IDX_BOTTOM] - area[JV.IDX_TOP] + height) / 2) + JV.OUTPUT_OFFSET[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM];
+                startY = Math.round( underLineArea[JV.IDX_TOP] + (underLineArea[JV.IDX_BOTTOM] - underLineArea[JV.IDX_TOP] + height) / 2) + JV.OUTPUT_OFFSET[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM];
             }
             }
             endY = Math.round(startY);
             endY = Math.round(startY);
             //2. 画线
             //2. 画线
@@ -198,42 +206,50 @@ function export_pdf_file (pageData, paperSize, fName, callback) {
                 ctx.translate(0,0.5);
                 ctx.translate(0,0.5);
             }
             }
             // ctx.beginPath();
             // ctx.beginPath();
-            ctx.moveTo(startX, startY);
+            // ctx.moveTo(startX, startY);
+            ctx.moveTo(startX * PDF_SCALE, startY * PDF_SCALE);
             ctx.lineWidth(1);
             ctx.lineWidth(1);
             ctx.strokeStyle = "BLACK";
             ctx.strokeStyle = "BLACK";
-            ctx.lineTo(endX, endY);
+            // ctx.lineTo(endX, endY);
+            ctx.lineTo(endX * PDF_SCALE, endY * PDF_SCALE);
             ctx.stroke();
             ctx.stroke();
             // ctx.restore();
             // ctx.restore();
         }
         }
 
 
         let rotateOptions;
         let rotateOptions;
-        if (font[JV.FONT_PROPS[5]] === 'T' && parseInt(font.FontAngle) === 0) {
-            private_drawUnderline();
-        }
+        // if (font[JV.FONT_PROPS[5]] === 'T' && parseInt(font.FontAngle) === 0) {
+        //     private_drawUnderline(val);
+        // }
         if (parseInt(font.FontAngle) !== 0) {
         if (parseInt(font.FontAngle) !== 0) {
             if (control){
             if (control){
-                rotateOptions = private_setupAreaRotateOption(area,w,control.Vertical,dftFontHeight, output);
+                rotateOptions = private_setupAreaRotateOption(area,validAreaTxtWidth,control.Vertical,dftFontHeight, output);
             } else {
             } else {
-                rotateOptions = private_setupAreaRotateOption(area,w,"bottom",dftFontHeight, output);
+                rotateOptions = private_setupAreaRotateOption(area,validAreaTxtWidth,"bottom",dftFontHeight, output);
             }
             }
             doc.rotate(font.FontAngle,rotateOptions);
             doc.rotate(font.FontAngle,rotateOptions);
         }
         }
-        if (w >= doc.widthOfString(val) || (control && control.Shrink !== 'T')) {
-            options.width = w;
-            options.height = dftFontHeight;
-            doc.text(val,output[0], output[1], options);
+        if (validAreaTxtWidth >= doc.widthOfString(val) ||
+            (control && control.Shrink !== 'T' && validTxtLines < private_splitString(val, validAreaTxtWidth, doc)) ) {
+            options.width = validAreaTxtWidth * PDF_SCALE;
+            options.height = dftFontHeight * PDF_SCALE;
+            doc.fontSize(dftFontHeight);
+            if (font[JV.FONT_PROPS[5]] === 'T' && parseInt(font.FontAngle) === 0) {
+                private_drawUnderline(val, area);
+            }
+            doc.fontSize(dftFontHeight * PDF_SCALE);
+            doc.text(val,output[0] * PDF_SCALE, output[1] * PDF_SCALE, options);
             doc.font(__dirname + '/pdf_base_files/simhei_bold_italic.ttf');
             doc.font(__dirname + '/pdf_base_files/simhei_bold_italic.ttf');
         } else {
         } else {
             while (true) {
             while (true) {
                 //*/
                 //*/
                 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));
                 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;
                 lines = (lines === 0)?1:lines;
-                let actLines = private_splitString(val, w, doc);
+                let actLines = private_splitString(val, validAreaTxtWidth, doc);
                 if (actLines.length > lines && dftFontHeight >= 6) {
                 if (actLines.length > lines && dftFontHeight >= 6) {
                     dftFontHeight--;
                     dftFontHeight--;
                     doc.fontSize(dftFontHeight);
                     doc.fontSize(dftFontHeight);
-                    options.width = w;
-                    options.height = dftFontHeight;
+                    options.width = validAreaTxtWidth * PDF_SCALE;
+                    options.height = dftFontHeight * PDF_SCALE;
                     // doc.text(val,output[0], output[1], options);
                     // doc.text(val,output[0], output[1], options);
                 } else {
                 } else {
                     let aH = dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP] + 4;
                     let aH = dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP] + 4;
@@ -249,21 +265,26 @@ function export_pdf_file (pageData, paperSize, fName, callback) {
                     for (let ai = 0; ai < area.length; ai++) {
                     for (let ai = 0; ai < area.length; ai++) {
                         newArea[ai] = area[ai];
                         newArea[ai] = area[ai];
                     }
                     }
-                    options.width = w;
-                    options.height = dftFontHeight;
+                    options.width = validAreaTxtWidth * PDF_SCALE;
+                    options.height = dftFontHeight * PDF_SCALE;
                     for (let lIdx = 0; lIdx < actLines.length; lIdx++) {
                     for (let lIdx = 0; lIdx < actLines.length; lIdx++) {
                         newArea[JV.IDX_TOP] = Math.round(aH * lIdx + baseTop);
                         newArea[JV.IDX_TOP] = Math.round(aH * lIdx + baseTop);
                         newArea[JV.IDX_BOTTOM] = Math.round(aH * (lIdx + 1) + baseTop);
                         newArea[JV.IDX_BOTTOM] = Math.round(aH * (lIdx + 1) + baseTop);
                         inner_setupControl(newArea, dftFontHeight, output);
                         inner_setupControl(newArea, dftFontHeight, output);
-                        doc.text(actLines[lIdx], output[0], output[1], options);
+                        doc.fontSize(dftFontHeight);
+                        if (font[JV.FONT_PROPS[5]] === 'T' && parseInt(font.FontAngle) === 0) {
+                            private_drawUnderline(actLines[lIdx], newArea);
+                        }
+                        doc.fontSize(dftFontHeight * PDF_SCALE);
+                        doc.text(actLines[lIdx], output[0] * PDF_SCALE, output[1] * PDF_SCALE, options);
                     }
                     }
                     break;
                     break;
                 }
                 }
                 /*/
                 /*/
                 dftFontHeight--;
                 dftFontHeight--;
                 doc.fontSize(dftFontHeight);
                 doc.fontSize(dftFontHeight);
-                if (w >= doc.widthOfString(val) || dftFontHeight < 6) {
-                    options.width = w;
+                if (validAreaTxtWidth >= doc.widthOfString(val) || dftFontHeight < 6) {
+                    options.width = validAreaTxtWidth;
                     options.height = dftFontHeight;
                     options.height = dftFontHeight;
                     doc.text(val,output[0], output[1], options);
                     doc.text(val,output[0], output[1], options);
                     doc.font(__dirname + '/pdf_base_files/simhei_bold_italic.ttf');
                     doc.font(__dirname + '/pdf_base_files/simhei_bold_italic.ttf');