123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- let JV = require('../jpc_value_define');
- let stringUtil = require('../../../../public/stringUtil');
- 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 re = /[\u4E00-\u9FA5]/g;
- let re = /[^\x00-\xff]/ig; //普通ASCII以外的字符
- let cStr = strVal.match(re);
- let chnAmt = (cStr)?cStr.length:0;
- let chnW = pdfDoc.widthOfString('一'), otherW = pdfDoc.widthOfString('_');
- //备注: 因后台的pdfDoc判断字符串长度与前端的不一样,需要做些调整,不一次性地判断字符串长度。
- // 分2种字符:中文与非中文,按照各种字符的数量分别乘以相关一个字符的宽度再累计。
- // let txtWidth = pdfDoc.widthOfString(strVal);
- let txtWidth = chnAmt * chnW + (strVal.length - chnAmt) * otherW;
- 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 re = /[\u4E00-\u9FA5]/g;
- let re = /[^\x00-\xff]/ig; //普通ASCII以外的字符
- let chnW = pdfDoc.widthOfString('一'), otherW = pdfDoc.widthOfString('_');
- 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++) {
- let tmpStr = strVal.substr(preSIdx, sIdx - preSIdx);
- let cStr = tmpStr.match(re);
- let chnAmt = (cStr)?cStr.length:0;
- // let chnAmt = tmpStr.match(re).length;
- // txtWidth = pdfDoc.widthOfString(strVal.substr(preSIdx, sIdx - preSIdx));
- txtWidth = chnAmt * chnW + (tmpStr.length - chnAmt) * otherW;
- 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;
|