TonyKang 7 anni fa
parent
commit
4760cb8613
1 ha cambiato i file con 136 aggiunte e 39 eliminazioni
  1. 136 39
      modules/reports/util/rpt_svg_util.js

+ 136 - 39
modules/reports/util/rpt_svg_util.js

@@ -4,63 +4,129 @@
  */
 
 let JV = require('../rpt_component/jpc_value_define');
-const SCREEN_DPI = [96,96];
+let pdf = require('pdfkit');
+let jpcCmnHelper = require('../rpt_component/helper/jpc_helper_common');
+let SCREEN_DPI = jpcCmnHelper.getScreenDPI();
 
 module.exports = {
-    exportSvgStr: function (pagesData, callback) {
+    exportSvgStr: function (pagesData, offsetX, offsetY) {
+        let rst = [];
         let styles = pagesData[JV.NODE_STYLE_COLLECTION],
             fonts = pagesData[JV.NODE_FONT_COLLECTION],
             controls = pagesData[JV.NODE_CONTROL_COLLECTION]
         ;
-        for (let page of pagesData.items) {
+        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);
             svgPageArr.push("<svg width='" + pixelSize[0] + "' height='" + pixelSize[1] + "'>");
+            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]));
+                svgPageArr.push(buildCellSvg(cell, fonts, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND], offsetX, offsetY, adjustY, pdf_doc));
+                // cnt++;
+                // console.log(cnt);
             }
             svgPageArr.push("</svg>");
+            rst.push(svgPageArr);
         }
+        return rst;
     }
 }
 
-function buildCellSvg(cell, fonts, styles, controls, mergeBorder) {
+function getActualBorderStyle(cell, styles, mergeBorderStyle, pageBorderArea, borderStr) {
+    let rst = styles[cell[JV.PROP_STYLE]][borderStr];
+    if (mergeBorderStyle) {
+        if (parseFloat(cell[JV.PROP_AREA][borderStr]) === parseFloat(pageBorderArea[borderStr])) {
+            if (borderStr === JV.PROP_LEFT || borderStr === JV.PROP_RIGHT) {
+                if (parseFloat(cell[JV.PROP_AREA][JV.PROP_TOP]) >= parseFloat(pageBorderArea[JV.PROP_TOP]) &&
+                    parseFloat(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) <= parseFloat(pageBorderArea[JV.PROP_BOTTOM])) {
+                    rst = mergeBorderStyle[borderStr];
+                }
+            } else if (borderStr === JV.PROP_TOP || borderStr === JV.PROP_BOTTOM) {
+                if (parseFloat(cell[JV.PROP_AREA][JV.PROP_LEFT]) >= parseFloat(pageBorderArea[JV.PROP_LEFT]) &&
+                    parseFloat(cell[JV.PROP_AREA][JV.PROP_RIGHT]) <= parseFloat(pageBorderArea[JV.PROP_RIGHT])) {
+                    rst = mergeBorderStyle[borderStr];
+                }
+            }
+        }
+    }
+    return rst;
+}
+
+function buildCellSvg(cell, fonts, styles, controls, pageMergeBorder, rptMergeBorder, offsetX, offsetY, adjustY, pdf_doc) {
     let rst = [];
     let style = styles[cell[JV.PROP_STYLE]];
+    let mergeBandStyle = null;
+    if (rptMergeBorder) {
+        mergeBandStyle = styles[rptMergeBorder[JV.PROP_STYLE][JV.PROP_ID]];
+    }
+    let font = cell[JV.PROP_FONT];
+    if (typeof font === 'string') {
+        font = fonts[cell[JV.PROP_FONT]];
+    }
+    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,
+        bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY
+    ;
     if (style) {
-        if (style[JV.PROP_LEFT] && parseFloat(style[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) > 0) {
-            rst.push("<line x1='" + cell[JV.PROP_AREA][JV.PROP_LEFT] + "' y1='" + cell[JV.PROP_AREA][JV.PROP_TOP] +
-                "' x2='" + cell[JV.PROP_AREA][JV.PROP_LEFT] + "' y2='" + cell[JV.PROP_AREA][JV.PROP_BOTTOM] +
-                "' style='stroke:rgb(0,0,0);stroke-width:1'/>")
+        let leftBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_LEFT);
+        // if (style[JV.PROP_LEFT] && parseFloat(style[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) > 0) {
+        if (leftBS && parseFloat(leftBS[JV.PROP_LINE_WEIGHT]) > 0) {
+            rst.push("<line x1='" + left + "' y1='" + top +
+                "' x2='" + left + "' y2='" + bottom +
+                "' style='stroke:rgb(0,0,0);stroke-width:" + leftBS[JV.PROP_LINE_WEIGHT] +"'/>")
         }
-        if (style[JV.PROP_RIGHT] && parseFloat(style[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) > 0) {
-            rst.push("<line x1='" + cell[JV.PROP_AREA][JV.PROP_RIGHT] + "' y1='" + cell[JV.PROP_AREA][JV.PROP_TOP] +
-                "' x2='" + cell[JV.PROP_AREA][JV.PROP_RIGHT] + "' y2='" + cell[JV.PROP_AREA][JV.PROP_BOTTOM] +
-                "' style='stroke:rgb(0,0,0);stroke-width:1'/>")
+        let rightBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_RIGHT);
+        // if (style[JV.PROP_RIGHT] && parseFloat(style[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) > 0) {
+        if (rightBS && parseFloat(rightBS[JV.PROP_LINE_WEIGHT]) > 0) {
+            rst.push("<line x1='" + right + "' y1='" + top +
+                "' x2='" + right + "' y2='" + bottom +
+                "' style='stroke:rgb(0,0,0);stroke-width:" + rightBS[JV.PROP_LINE_WEIGHT] +"'/>")
         }
-        if (style[JV.PROP_TOP] && parseFloat(style[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) > 0) {
-            rst.push("<line x1='" + cell[JV.PROP_AREA][JV.PROP_LEFT] + "' y1='" + cell[JV.PROP_AREA][JV.PROP_TOP] +
-                "' x2='" + cell[JV.PROP_AREA][JV.PROP_RIGHT] + "' y2='" + cell[JV.PROP_AREA][JV.PROP_TOP] +
-                "' style='stroke:rgb(0,0,0);stroke-width:1'/>")
+        let topBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_TOP);
+        // if (style[JV.PROP_TOP] && parseFloat(style[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) > 0) {
+        if (topBS && parseFloat(topBS[JV.PROP_LINE_WEIGHT]) > 0) {
+            rst.push("<line x1='" + left + "' y1='" + top +
+                "' x2='" + right + "' y2='" + top +
+                "' style='stroke:rgb(0,0,0);stroke-width:" + topBS[JV.PROP_LINE_WEIGHT] +"'/>")
         }
-        if (style[JV.PROP_BOTTOM] && parseFloat(style[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) > 0) {
-            rst.push("<line x1='" + cell[JV.PROP_AREA][JV.PROP_LEFT] + "' y1='" + cell[JV.PROP_AREA][JV.PROP_BOTTOM] +
-                "' x2='" + cell[JV.PROP_AREA][JV.PROP_RIGHT] + "' y2='" + cell[JV.PROP_AREA][JV.PROP_BOTTOM] +
-                "' style='stroke:rgb(0,0,0);stroke-width:1'/>")
+        let bottomBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_BOTTOM);
+        // if (style[JV.PROP_BOTTOM] && parseFloat(style[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) > 0) {
+        if (bottomBS && parseFloat(bottomBS[JV.PROP_LINE_WEIGHT]) > 0) {
+            rst.push("<line x1='" + left + "' y1='" + bottom +
+                "' x2='" + right + "' y2='" + bottom +
+                "' style='stroke:rgb(0,0,0);stroke-width:" + bottomBS[JV.PROP_LINE_WEIGHT] +"'/>")
         }
     }
-    let font = cell[JV.PROP_FONT];
-    if (typeof font === 'string') {
-        font = fonts[cell[JV.PROP_FONT]];
-    }
-    let fontsize = parseInt(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]]);
-    let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]),
-        right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]),
-        top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]),
-        bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]),
+    buildText(rst, cell, font, controls[cell[JV.PROP_CONTROL]], offsetX, offsetY, adjustY, pdf_doc);
+
+    return rst.join("");
+}
+
+function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, pdf_doc) {
+    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 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,
+        bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY,
         x = left, y = top,
         text_anchor = "start"
     ;
-    let control = controls[cell[JV.PROP_CONTROL]];
+    let value = cell[JV.PROP_VALUE];
+    if (!(value)) {
+        value = "";
+    }
+    let values = null;
+    if (typeof value === "string") {
+        values = value.split("|");
+    } else {
+        values = [value];
+    }
+    let stepHeight = (parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) - parseInt(cell[JV.PROP_AREA][JV.PROP_TOP])) / values.length;
     if (control) {
         if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "left") {
             text_anchor = "start";
@@ -72,16 +138,47 @@ function buildCellSvg(cell, fonts, styles, controls, mergeBorder) {
             text_anchor = "middle";
             x = Math.round((left + right) / 2);
         }
-        if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "top") {
-            y = top + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
-        } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "bottom") {
-            y = bottom - fontsize - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
-        } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "center") {
-            y = Math.round((top + bottom + fontsize) / 2 );
+    }
+    for (let vidx = 0; vidx < values.length; vidx++) {
+        //check whether need to adjust the font size
+        let dftFontHeight = orgFontHeight;
+        let dftFontBold = font[JV.FONT_PROPS[3]];
+        let dftFontItalic = font[JV.FONT_PROPS[4]];
+        if (dftFontBold && dftFontBold === 'T') {
+            pdf_doc.font(__dirname+'/pdf_base_files/hwxsb.ttf');
+        }else if(dftFontItalic && dftFontItalic === 'T'){
+            pdf_doc.font(__dirname+'/pdf_base_files/Smart-italic.ttf');
+        }else {
+            pdf_doc.font(__dirname+'/pdf_base_files/Smart.ttf');
+        }
+        pdf_doc.fontSize(dftFontHeight);
+        while ((right - left) <= pdf_doc.widthOfString(values[vidx])) {
+            if (dftFontHeight > 6) {
+                dftFontHeight--;
+                pdf_doc.fontSize(dftFontHeight);
+            } else {
+                break;
+            }
+        }
+        dftFontHeight = (dftFontHeight * 3 / 4); //SVG的字体与canvas的字体大小的切换, 不用考虑取整
+        if (control) {
+            if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "top") {
+                y = Math.round((top + vidx * stepHeight) + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP]);
+            } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "bottom") {
+                y = Math.round((top + (vidx + 1) * stepHeight) - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]);
+            } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "center") {
+                y = Math.round(((top + vidx * stepHeight) + (top + (vidx + 1) * stepHeight) + dftFontHeight) / 2 );
+            }
+        }
+        if (font[JV.PROP_NAME] === "宋体") {
+            y--;
         }
+        destRst.push("<text style='fill:black;font-family:" + font[JV.PROP_NAME] +
+            ";font-weight:" + fontWeight +
+            ";font-style:" + fontStyle +
+            ";font-size:" + dftFontHeight + "pt' x='" +
+            x +"' y='" + y + "' text-anchor='" + text_anchor + "' xml:space='preserve'>" + values[vidx] + "</text>");
     }
-    rst.push("<text style='fill:black;font-size:" + fontsize + "pt' x='" + x +"' y='" + y + "'>" + cell[JV.PROP_VALUE] + "</text>");
-    return rst.join();
 }
 
 function getPixelSize(pagesData) {