/**
 * Created by Tony on 2017/11/2.
 */
let JpcCanvasOutput = {
    offsetX: 10,
    offsetY: 10,
    scaleFactor: 1,
    cleanCanvas: function (canvas) {
        let ctx = canvas.getContext("2d");
        ctx.save();
        ctx.fillStyle="white";
        ctx.clearRect(0,0, canvas.width, canvas.height);
        ctx.restore();
    },
    resetFonts: function(rptFonts) {
        let me = JpcCanvasOutput;
        if (me.scaleFactor !== 1) {
            for (let key in rptFonts) {
                rptFonts[key]["FontHeight"] = me.scaleFactor * rptFonts[key]["FontHeight"];
            }
        }
    },
    drawToCanvas : function(pageObj, canvas, pageIdx) {
        let me = this;
        let ctx = canvas.getContext("2d");
        let floatReg = /^(-?\d+)(\.\d+)?$/;

        function private_setupAreaH(area, type, fontAngle, dftFontHeight, outputPoint) {
            let lType = type;
            if (type !== "left" && type !== "right" && type !== "center") lType = "left";
            switch (lType) {
                case "left":
                    if (fontAngle === JV.VERTICAL_ANGLE_INT) {
                        outputPoint[1] = 1 * area[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
                    } else if (fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
                        outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
                    } else outputPoint[0] = 1 * area[JV.IDX_LEFT] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
                    ctx.textAlign="start";
                    break;
                case "right":
                    if (fontAngle === JV.VERTICAL_ANGLE_INT) {
                        outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
                    } else if (fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
                        outputPoint[1] = 1 * area[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
                    } else outputPoint[0] = 1 * area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
                    ctx.textAlign="end";
                    break;
                case "center":
                    if (fontAngle === JV.VERTICAL_ANGLE_INT || fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
                        outputPoint[1] = (1 * area[JV.IDX_TOP] + 1 * area[JV.IDX_BOTTOM]) / 2;
                    } else outputPoint[0] = (1 * area[JV.IDX_LEFT] + 1 * area[JV.IDX_RIGHT]) / 2;
                    ctx.textAlign="center";
                    break;
            }
        }
        function private_setupAreaV(area, type, fontAngle, fontName, dftFontHeight, outputPoint) {
            let lType = type;
            if (type !== "top" && type !== "bottom" && type !== "center") lType = "top";
            switch (lType) {
                case "top":
                    if (fontAngle === JV.VERTICAL_ANGLE_INT) {
                        outputPoint[0] = 1 * area[JV.IDX_RIGHT] - dftFontHeight - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
                    } else if (fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
                        outputPoint[0] = 1 * area[JV.IDX_LEFT] + dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
                    } else outputPoint[1] = 1 * area[JV.IDX_TOP] + dftFontHeight + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
                    break;
                case "bottom":
                    if (fontAngle === JV.VERTICAL_ANGLE_INT) {
                        outputPoint[0] = 1 * area[JV.IDX_LEFT] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
                    } else if (fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
                        outputPoint[0] = 1 * area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
                    } else {
                        outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM];
                        if (fontName === "宋体") outputPoint[1] = outputPoint[1] - 1;
                    }
                    break;
                case "center":
                    if (fontAngle === JV.VERTICAL_ANGLE_INT) {
                        outputPoint[0] = (1 * area[JV.IDX_LEFT] + 1 * area[JV.IDX_RIGHT] - dftFontHeight) / 2;
                    } else if (fontAngle === JV.ANTI_VERTICAL_ANGLE_INT) {
                        outputPoint[0] = (1 * area[JV.IDX_LEFT] + 1 * area[JV.IDX_RIGHT] + dftFontHeight) / 2;
                    } else {
                        outputPoint[1] = (1 * area[JV.IDX_TOP] + 1 * area[JV.IDX_BOTTOM] + dftFontHeight) / 2;
                        if (fontName === "宋体") outputPoint[1] = outputPoint[1] - 1;
                    }
                    break;
            }
        }
        function private_splitString(strVal, areaWidth, ctx) {
            let rst = [];
            if (strVal) {
                let preSIdx = 0, txtWidth = 0;
                let currentW = 0;
                let chnW = ctx.measureText('一').width, otherW = ctx.measureText('_').width;
                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 private_drawText(val, area, font, control) {
            let dftFontHeight = 12;
            let output = [];
            if (font) {
                dftFontHeight = parseFloat(font[JV.FONT_PROPS[1]]);
                let dftOthers = "";
                let dftFontBold = font[JV.FONT_PROPS[3]];
                if (dftFontBold && dftFontBold === 'T') {
                    dftOthers = "bold " + dftOthers ;
                }
                let dftFontItalic = font[JV.FONT_PROPS[4]];
                if (dftFontItalic && dftFontItalic === 'T') {
                    dftOthers = dftOthers + "italic ";
                }
                ctx.font = dftOthers + dftFontHeight + "px " + font[JV.PROP_NAME];
            }
            let inner_setupControl = function (inArea, inFontHeight, inOutput) {
                if (control) {
                    private_setupAreaH(inArea, control.Horizon, parseInt(font.FontAngle), inFontHeight, inOutput);
                    private_setupAreaV(inArea, control.Vertical, parseInt(font.FontAngle), font.Name, inFontHeight, inOutput);
                } else {
                    private_setupAreaH(inArea, "left", parseInt(font.FontAngle), inFontHeight, inOutput);
                    private_setupAreaV(inArea, "bottom", parseInt(font.FontAngle), font.Name, inFontHeight, inOutput);
                }
            };
            inner_setupControl(area, dftFontHeight, output);
            let validAreaTxtWidth = area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - area[JV.IDX_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
            //let validAreaTxtHeight = area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] - area[JV.IDX_TOP] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
            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) {
                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(underLineVal) {
                //A. 暂不支持角度; B. 坐标已经translate
                //1. 计算下划线的相关坐标
                let width = ctx.measureText(underLineVal).width;
                if (width > area[JV.IDX_RIGHT] - area[JV.IDX_LEFT]) {
                    width = area[JV.IDX_RIGHT] - area[JV.IDX_LEFT];
                }
                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 = 0, startY = 0, endX = width, endY = startY;
                if (control.Horizon === "left") {
                    // 无变化;
                } else if (control.Horizon === "right") {
                    startX = Math.round(startX - width);
                } else {
                    startX = Math.round(startX - width / 2);
                }
                endX = Math.round(startX + width);

                if (control.Vertical === "top") {
                    startY += JV.OUTPUT_OFFSET[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM];
                } else if (control.Vertical === "bottom") {
                    // startY = Math.round(startY);
                    startY += JV.OUTPUT_OFFSET[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM];
                } else {
                    startY = Math.round(height / 2) - JV.OUTPUT_OFFSET[JV.IDX_TOP] - JV.OUTPUT_OFFSET[JV.IDX_BOTTOM];
                    // startY += JV.OUTPUT_OFFSET[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.IDX_BOTTOM];
                }
                endY = Math.round(startY);
                //2. 画线
                // ctx.save();
                if ( output[1] !== Math.round(output[1])) {
                    ctx.translate(0,0.5);
                }
                ctx.beginPath();
                ctx.moveTo(startX, startY);
                ctx.lineWidth = 1;
                ctx.strokeStyle = "BLACK";
                ctx.lineTo(endX, endY);
                ctx.stroke();
                // ctx.restore();
            }

            ctx.save();
            ctx.translate(output[0], output[1]);
            // if (font[JV.FONT_PROPS[5]] === 'T' && parseInt(font.FontAngle) === 0) {
            //     private_drawUnderline(val);
            // }
            if (font.FontAngle === JV.VERTICAL_ANGLE) {
                ctx.rotate(Math.PI/2);
            } else if (font.FontAngle === JV.ANTI_VERTICAL_ANGLE) {
                ctx.rotate(-Math.PI/2);
            }
            if (font.FontAngle === JV.WATER_MARK_ANGLE) {
                ctx.rotate(Math.PI/4);
                ctx.fillStyle = font.FontColor;
            } else if (font.FontAngle === JV.ANTI_WATER_MARK_ANGLE) {
                ctx.rotate(-Math.PI/4);
                ctx.fillStyle = font.FontColor;
            }
            if (validAreaTxtWidth >= ctx.measureText(val).width ||
                (control && control.Shrink !== 'T' && validTxtLines < private_splitString(val, validAreaTxtWidth, ctx)) ) {
                if (font[JV.FONT_PROPS[5]] === 'T' && parseInt(font.FontAngle) === 0) {
                    private_drawUnderline(val);
                }
                ctx.fillText(val, 0, 0);
            } else {
                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));
                    lines = (lines === 0)?1:lines;
                    let actLines = private_splitString(val, validAreaTxtWidth, ctx);
                    if (actLines.length > lines && dftFontHeight >= 6) {
                        dftFontHeight--;
                        ctx.font = "" + dftFontHeight + "px " + font[JV.PROP_NAME];
                    } else {
                        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);
                            ctx.translate(-output[0], -output[1]);
                            inner_setupControl(newArea, dftFontHeight, output);
                            ctx.translate(output[0], output[1]);
                            if (font[JV.FONT_PROPS[5]] === 'T' && parseInt(font.FontAngle) === 0) {
                                private_drawUnderline(actLines[lIdx]);
                            }
                            ctx.fillText(actLines[lIdx], 0, 0);
                        }
                        break;
                    }
                }
            }
            ctx.restore();
        }
        function _splitValues(cell, control, orgValues, ctx2D) {
            //根据control的 自动折行 及 缩放优先 这俩属性 来分解cell value
            if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_WRAP]] === 'T' && control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_SHRINK_FIRST]] !== 'T') {
                let vals = [];
                let validAreaTxtWidth = cell[JV.PROP_AREA][JV.PROP_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - cell[JV.PROP_AREA][JV.PROP_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
                for (let val of orgValues) {
                    let actW = ctx2D.measureText(val).width;
                    if (actW > validAreaTxtWidth) {
                        vals = vals.concat(private_splitString(val, validAreaTxtWidth, ctx2D));
                    } else {
                        vals.push(val);
                    }
                }
                if (vals.length > orgValues.length) {
                    for (let idx = 0; idx < vals.length; idx++) {
                        orgValues[idx] = vals[idx];
                    }
                }
            }
        }
        function private_drawCellText(cell, fonts, controls) {
            if (cell[JV.PROP_VALUE] !== null && cell[JV.PROP_VALUE] !== undefined) {
                let values = ("" + cell[JV.PROP_VALUE]).split('|');
                let font = null;
                if (typeof cell[JV.PROP_FONT] === "string") {
                    font = fonts[cell[JV.PROP_FONT]];
                } else {
                    font = cell[JV.PROP_FONT];
                }
                //let control = controls[cell[JV.PROP_CONTROL]];
                let control = null;
                if (typeof cell[JV.PROP_CONTROL] === "string") {
                    control = controls[cell[JV.PROP_CONTROL]];
                } else {
                    control = cell[JV.PROP_CONTROL];
                }
                if (control.ShowZero === "F") {
                    if (typeof cell[JV.PROP_VALUE] === "string") {
                        if (floatReg.test(cell[JV.PROP_VALUE]) && parseFloat(cell[JV.PROP_VALUE]) === 0.0) {
                            values = [""];
                        }
                    } else if (parseFloat(cell[JV.PROP_VALUE]) === 0.0) {
                        values = [""];
                    }
                }

                if (font) {
                    let dftFontHeight = parseFloat(font[JV.FONT_PROPS[1]]);
                    let dftOthers = "";
                    let dftFontBold = font[JV.FONT_PROPS[3]];
                    if (dftFontBold && dftFontBold === 'T') {
                        dftOthers = "bold " + dftOthers ;
                    }
                    let dftFontItalic = font[JV.FONT_PROPS[4]];
                    if (dftFontItalic && dftFontItalic === 'T') {
                        dftOthers = dftOthers + "italic ";
                    }
                    dftFontHeight = me.scaleFactor * dftFontHeight;
                    ctx.font = dftOthers + dftFontHeight + "px " + font[JV.PROP_NAME];
                }
                _splitValues(cell, control, values, ctx);

                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] + me.offsetX, cell[JV.PROP_AREA][JV.PROP_TOP] + me.offsetY, cell[JV.PROP_AREA][JV.PROP_RIGHT] + me.offsetX, cell[JV.PROP_AREA][JV.PROP_BOTTOM] + me.offsetY];
                for (let i = 0; i < values.length; i++) {
                    area[JV.IDX_TOP] = cell[JV.PROP_AREA][JV.PROP_TOP] + i * (height / values.length) + me.offsetY;
                    area[JV.IDX_BOTTOM] = cell[JV.PROP_AREA][JV.PROP_TOP] + (i + 1) * (height / values.length) + me.offsetY;
                    if (values[i] === null || values[i] === undefined || values[i] === 'null') {
                        values[i] = "";
                    }
                    private_drawText(values[i], area, font, control);
                }
            }
        }
        function private_drawLine(cell, ctx, style, styleBorderDest, startP, destP, mergedBand, styles, isNeedMergeBand) {
            ctx.beginPath();
            let destStyle = style;
            if (mergedBand) {
                if (isNeedMergeBand && parseFloat(mergedBand[styleBorderDest]) === parseFloat(cell[JV.PROP_AREA][styleBorderDest])) {
                    destStyle = styles[mergedBand[JV.PROP_STYLE][JV.PROP_ID]];
                }
            }
            ctx.moveTo(cell[JV.PROP_AREA][startP[0]] + me.offsetX, cell[JV.PROP_AREA][startP[1]] + me.offsetY);
            if (destStyle[styleBorderDest] && parseFloat(destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT]) !== 0.0) {
                ctx.lineWidth = 1.0 * destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT];
                ctx.strokeStyle = destStyle[styleBorderDest][JV.PROP_COLOR];
                ctx.lineTo(cell[JV.PROP_AREA][destP[0]] + me.offsetX, cell[JV.PROP_AREA][destP[1]] + me.offsetY);
            }
            ctx.stroke();
        }
        function private_chkIfInMergedBand(mergedBand, cell) {
            let rst = false;
            if (mergedBand && cell) {
                rst = mergedBand[JV.PROP_TOP] <= cell[JV.PROP_AREA][JV.PROP_TOP] && mergedBand[JV.PROP_BOTTOM] >= cell[JV.PROP_AREA][JV.PROP_BOTTOM] &&
                    mergedBand[JV.PROP_LEFT] <= cell[JV.PROP_AREA][JV.PROP_LEFT] && mergedBand[JV.PROP_RIGHT] >= cell[JV.PROP_AREA][JV.PROP_RIGHT];
            }
            return rst;
        }
        function private_drawCell(cell, fonts, styles, controls, mergedBand) {
            ctx.save();
            ctx.translate(0.5,0.5);
            let style = styles[cell[JV.PROP_STYLE]];
            if (style) {
                let isNeedMergeBand = private_chkIfInMergedBand(mergedBand, cell);
                private_drawLine(cell, ctx, style, JV.PROP_TOP, [JV.PROP_LEFT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_TOP], mergedBand, styles, isNeedMergeBand);
                private_drawLine(cell, ctx, style, JV.PROP_RIGHT, [JV.PROP_RIGHT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_BOTTOM], mergedBand, styles, isNeedMergeBand);
                private_drawLine(cell, ctx, style, JV.PROP_BOTTOM, [JV.PROP_RIGHT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_BOTTOM], mergedBand, styles, isNeedMergeBand);
                private_drawLine(cell, ctx, style, JV.PROP_LEFT, [JV.PROP_LEFT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_TOP], mergedBand, styles, isNeedMergeBand);
            }
            private_drawCellText(cell, fonts, controls);
            ctx.restore();
        }
        function getIniPageMergeBorder(mergedBand) {
            let rst = {};
            if (mergedBand) {
                rst[JV.PROP_LEFT] = mergedBand[JV.PROP_LEFT];
                rst[JV.PROP_RIGHT] = mergedBand[JV.PROP_RIGHT];
                rst[JV.PROP_TOP] = mergedBand[JV.PROP_TOP];
                rst[JV.PROP_BOTTOM] = mergedBand[JV.PROP_BOTTOM];
                rst[JV.PROP_STYLE] = mergedBand[JV.PROP_STYLE];
            }
            return rst;
        }
        function private_resetArea(area) {
            area[JV.PROP_LEFT] = me.scaleFactor * area[JV.PROP_LEFT];
            area[JV.PROP_RIGHT] = me.scaleFactor * area[JV.PROP_RIGHT];
            area[JV.PROP_TOP] = me.scaleFactor * area[JV.PROP_TOP];
            area[JV.PROP_BOTTOM] = me.scaleFactor * area[JV.PROP_BOTTOM];
        }
        function private_resetCellFont(cell) {
            if (typeof cell.font !== "string") {
                cell.font["FontHeight"] = me.scaleFactor * cell.font["FontHeight"];
            }
        }

        if (pageObj && pageObj.items.length > 0 && canvas && pageObj.items.length >= pageIdx) {
            let page = pageObj.items[pageIdx - 1],
                fonts = pageObj[JV.NODE_FONT_COLLECTION],
                styles = pageObj[JV.NODE_STYLE_COLLECTION],
                controls = pageObj[JV.NODE_CONTROL_COLLECTION],
                mergedBand = pageObj[JV.BAND_PROP_MERGE_BAND];
            let newPageMergeBand = getIniPageMergeBorder(mergedBand);
            if (page[JV.PROP_PAGE_MERGE_BORDER]) {
                newPageMergeBand[JV.PROP_LEFT] = page[JV.PROP_PAGE_MERGE_BORDER][JV.PROP_LEFT];
                newPageMergeBand[JV.PROP_RIGHT] = page[JV.PROP_PAGE_MERGE_BORDER][JV.PROP_RIGHT];
                newPageMergeBand[JV.PROP_TOP] = page[JV.PROP_PAGE_MERGE_BORDER][JV.PROP_TOP];
                newPageMergeBand[JV.PROP_BOTTOM] = page[JV.PROP_PAGE_MERGE_BORDER][JV.PROP_BOTTOM];
            }
            private_resetArea(newPageMergeBand);
            for (let j = 0; j < page.cells.length; j++) {
                let cell = page.cells[j];
                if (me.scaleFactor !== 1) {
                    private_resetCellFont(cell);
                    private_resetArea(cell[JV.PROP_AREA]);
                }
                private_drawCell(cell, fonts, styles, controls, newPageMergeBand);
            }
        }
    },
    drawPageBorder: function(rptTpl, canvas, resolution) {
        let me = this;
        let size = me.getReportSizeInPixel(rptTpl, resolution);

        let ctx = canvas.getContext("2d");
        ctx.save();
        ctx.beginPath();
        ctx.translate(0.5,0.5);
        ctx.lineWidth = 1;
        ctx.moveTo(me.offsetX, me.offsetY);
        ctx.lineTo(size[0] + me.offsetX, me.offsetY);
        ctx.lineTo(size[0] + me.offsetX, size[1] + me.offsetY);
        ctx.lineTo(me.offsetX, size[1] + me.offsetY);
        ctx.lineTo(me.offsetX, me.offsetY);
        ctx.stroke();
        ctx.restore();

        ctx.fillStyle="black";
        ctx.fillRect(size[0] + me.offsetX,10 + me.offsetY,10,size[1]);
        ctx.fillRect(10 + me.offsetX,size[1] + me.offsetY,size[0],10);
    },
    getReportSizeInPixel: function(rptTpl, resolution) {
        let rst = [8.27, 11.69];
        if (rptTpl && rptTpl[JV.NODE_PAGE_INFO] && rptTpl[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE]) {
            rst = rptTpl[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE].slice(0);
        }
        rst[0] = Math.round(resolution[0] * rst[0]);
        rst[1] = Math.round(resolution[0] * rst[1]);
        return rst;
    }
};