TonyKang 6 anos atrás
pai
commit
d9ce742064
1 arquivos alterados com 101 adições e 0 exclusões
  1. 101 0
      modules/reports/util/rpt_svg_util.js

+ 101 - 0
modules/reports/util/rpt_svg_util.js

@@ -143,6 +143,78 @@ function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, pdf_
             x = Math.round((left + right) / 2);
         }
     }
+    //*/
+    let height = cell[JV.PROP_AREA][JV.PROP_BOTTOM] - cell[JV.PROP_AREA][JV.PROP_TOP];
+    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);
+        function inner_build_text(innerTxt, innerArea) {
+            let innerDftFontHeight = (dftFontHeight * 3 / 4); //SVG的字体与canvas的字体大小的切换, 不用考虑取整
+            if (control) {
+                if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "top") {
+                    y = innerArea[JV.PROP_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
+                } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "bottom") {
+                    y = innerArea[JV.PROP_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
+                } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "center") {
+                    y = Math.round((innerArea[JV.PROP_TOP] + innerArea[JV.PROP_BOTTOM] + innerDftFontHeight) / 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:" + innerDftFontHeight + "pt' x='" +
+                x +"' y='" + y + "' text-anchor='" + text_anchor + "' xml:space='preserve'>" + innerTxt + "</text>");
+        }
+        if (actLines.length === 1 || (control && control.Shrink !== 'T')) {
+            inner_build_text(textValue, area);
+        } else {
+            while (true) {
+                if (dftFontHeight > 6) {
+                    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);
+                    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')) {
+                            if (control.Vertical === 'bottom') {
+                                area[JV.IDX_TOP] = area[JV.IDX_BOTTOM] - (aH * actLines.length);
+                            } else {
+                                area[JV.IDX_TOP] = (area[JV.IDX_TOP] + area[JV.IDX_BOTTOM]) / 2 - (aH * actLines.length) / 2
+                                area[JV.IDX_BOTTOM] = area[JV.IDX_TOP] + (aH * actLines.length);
+                            }
+                        }
+                        let newArea = [], baseTop = area[JV.IDX_TOP];
+                        for (let ai = 0; ai < area.length; ai++) {
+                            newArea[ai] = area[ai];
+                        }
+                        for (let lIdx = 0; lIdx < actLines.length; lIdx++) {
+                            newArea[JV.IDX_TOP] = Math.round(aH * lIdx + baseTop);
+                            newArea[JV.IDX_BOTTOM] = Math.round(aH * (lIdx + 1) + baseTop);
+                            inner_build_text(actLines[lIdx], newArea);
+                        }
+                        break;
+                    }
+                } else {
+                    inner_build_text(textValue, area);
+                    break;
+                }
+            }
+        }
+    };
+    for (let vidx = 0; vidx < values.length; vidx++) {
+        area[JV.IDX_TOP] = cell[JV.PROP_AREA][JV.PROP_TOP] + vidx * (height / values.length);
+        area[JV.IDX_BOTTOM] = cell[JV.PROP_AREA][JV.PROP_TOP] + (vidx + 1) * (height / values.length);
+        inner_draw_text(values[vidx], area, font, control);
+    }
+    /*/
     for (let vidx = 0; vidx < values.length; vidx++) {
         //check whether need to adjust the font size
         let dftFontHeight = orgFontHeight;
@@ -175,6 +247,35 @@ function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, pdf_
             ";font-size:" + dftFontHeight + "pt' x='" +
             x +"' y='" + y + "' text-anchor='" + text_anchor + "' xml:space='preserve'>" + values[vidx] + "</text>");
     }
+    //*/
+}
+
+function private_splitString(strVal, areaWidth, doc) {
+    let rst = [];
+    if (strVal) {
+        let preSIdx = 0, txtWidth = 0;
+        let currentW = 0;
+        let chnW = doc.widthOfString('一'), otherW = doc.widthOfString('_');
+        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;
 }
 
 function getPixelSize(pagesData) {