let JV = require('../jpc_value_define'); let JpcCommonHelper = { commonConstant: {}, getResultByID: function (KeyID, collectionList) { let rst = null; if (KeyID) { for (let i = 0; i < collectionList.length; i++) { let collection = collectionList[i]; if (collection && collection instanceof Array) { for (let j = 0; j < collection.length; j++) { if (collection[j][JV.PROP_ID] === KeyID) { rst = collection[j]; break; } } if (rst) break; } } } return rst; }, getFont: function(fontName, dftFonts, rptTpl) { let me = this, list = []; if (rptTpl) list.push(rptTpl[JV.NODE_FONT_COLLECTION]); list.push(dftFonts); return me.getResultByID(fontName, list); }, getStyle: function(styleName, dftStyles, rptTpl) { let me = this, list = []; if (rptTpl) list.push(rptTpl[JV.NODE_STYLE_COLLECTION]); list.push(dftStyles); return me.getResultByID(styleName, list); }, getControl: function(controlName, dftControls, rptTpl) { let me = this, list = []; if (rptTpl) list.push(rptTpl[JV.NODE_CONTROL_COLLECTION]); list.push(dftControls); return me.getResultByID(controlName, list); }, getLayoutAlignment: function(alignStr) { let rst = JV.LAYOUT.indexOf(alignStr); if (rst < 0) rst = JV.LAYOUT_FULFILL; return rst; }, getPosCalculationType: function (typeStr) { let rst = JV.CAL_TYPE.indexOf(typeStr); if (rst < 0) rst = JV.CAL_TYPE_ABSTRACT; return rst; }, getBoolean: function(bStr) { let rst = false; if (bStr !== null && bStr !== undefined) { let valType = typeof(bStr); if (valType === "boolean") { rst = bStr; } else if (valType === "string") { let tS = bStr.toUpperCase(); rst = (tS === "T" || tS === "TRUE" || tS === "YES" || tS === "Y"); } else if (valType === "number") { rst = (bStr === 1); } } return rst; }, getScreenDPI: function() { let me = this, arrDPI = []; if (!me.commonConstant.resolution) { arrDPI = [96,96]; // arrDPI = [100,100]; me.commonConstant.resolution = arrDPI; } else { arrDPI = me.commonConstant.resolution; } return arrDPI; }, getUnitFactor: function(rptTpl) { let me = this; return me.translateUnit(rptTpl[JV.NODE_MAIN_INFO][JV.PROP_UNITS]); }, translateUnit: function(unitStr) { let me = this, rst = 1.0; if (unitStr) { let resolution = me.getScreenDPI(); if (JV.MEASUREMENT.PIXEL.indexOf(unitStr) >= 0) { rst = 1.0; } else if (JV.MEASUREMENT.CM.indexOf(unitStr) >= 0) { rst = 1.0 * resolution[0] / 2.54; } else if (JV.MEASUREMENT.INCH.indexOf(unitStr) >= 0) { rst = 1.0 * resolution[0]; } } return rst; }, getPageSize: function (rptTpl) { let size = null; let sizeStr = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE]; let sizeIdx = JV.PAGES_SIZE_STR.indexOf(sizeStr); if (sizeIdx >= 0) { size = JV.PAGES_SIZE[sizeIdx].slice(0); } else if (sizeStr === JV.PAGE_SELF_DEFINE) { // } else { size = JV.SIZE_A4.slice(0); } let page_orientation = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION]; if (page_orientation === JV.ORIENTATION_LANDSCAPE || page_orientation === JV.ORIENTATION_LANDSCAPE_CHN) { //swap x,y let tmp = size[0]; size[0] = size[1]; size[1] = tmp; } return size; }, getReportArea: function(rptTpl, unitFactor) { let me = this, resolution = me.getScreenDPI(), rst = [], size = me.getPageSize(rptTpl); size[0] = resolution[0] * size[0]; size[1] = resolution[0] * size[1]; rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]); rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]); rst.push(size[0] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT]); rst.push(size[1] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]); return rst; }, getSegIdxByPageIdx: function(page, page_seg_map) { let rst = -1; for (let pIdx = 0; pIdx < page_seg_map.length; pIdx++) { if (page_seg_map[pIdx][0] === page) { rst = page_seg_map[pIdx][1]; break; } } 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 txtWidth = pdfDoc.widthOfString(strVal); let rst = parseInt(txtWidth / areaWidth); if (txtWidth % areaWidth > 0) { rst++; } if (rst === 0) rst = 1; //即使是空字符串,也得有一行啊 return rst; //备注: 其实是想用canvas的,但node canvas装起来麻烦,暂时用PDF Kit来顶用一下,以后换新方法再用 }, splitString: function (area, strVal, pdfDoc) { let rst = []; 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++) { txtWidth = pdfDoc.widthOfString(strVal.substr(preSIdx, sIdx - preSIdx)); 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)); } } return rst; } }; module.exports = JpcCommonHelper;