123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- // 获取文本字节总数
- export const getBytesCount = function(str){
- var bytesCount = 0;
- if (str != null) {
- for (var i = 0; i < str.length; i++) {
- var c = str.charAt(i);
- if (c.match(/[^\x00-\xff]/ig) != null) //全角
- {
- bytesCount += 2;
- } else {
- bytesCount += 1;
- }
- }
- }
- return bytesCount;
- }
- // JS 包含中文的字符串按固定长度拆分换行算法
- export const getTextRows = function(str,iPageSize){
- var strArray = [];
- var tempStr = "";
- var iTotalLength = getBytesCount(str);
- for (var i = 0; i < str.length; i++) {
- var iCount = getBytesCount(str[i]);
- var iCountTemp = getBytesCount(tempStr);
- //iCountTemp + iCount >= iPageSize 这里可能出现两种情况:
- //长度等于 iPageSize 或 等于 iPageSize + 1(最后一个为中文字符就等于 9 + 2 情况)
- if (iCountTemp + iCount >= iPageSize) {
- tempStr += str[i];
- strArray.push(tempStr);
- tempStr = "";
- } else {
- tempStr += str[i];
- }
- }
-
- // //最后一次尾巴
- if (tempStr.length > 0){
- strArray.push(tempStr)
- }
- return strArray
- }
- // 格式化打印文本
- export const textFormat = function(command,text,maxFontNums,left,top,lightHeight,align,fontSize){
- let rowTop = top
- const maxNums = maxFontNums
- const rowHeight = lightHeight
- const textArr = getTextRows(text.replace(/\s+/g, ' '),maxNums*2)
- const rows = textArr.length
- for(let i=0;i<rows;i++){
- if(align=="center"){
- const ltxt = textArr[i]
- const ltlen = getBytesCount(ltxt)/2*fontSize
- command.setText(left+((maxNums-ltlen)/2)*rowHeight, rowTop, "TSS24.BF2", fontSize, fontSize, ltxt)
- }
- else{
- command.setText(left, rowTop, "TSS24.BF2", fontSize, fontSize, textArr[i])
- }
- rowTop = rowTop+rowHeight*fontSize+10
- }
- return rowTop
- }
- // 60 * 40 尺寸模板
- export const printTempl = function(tsc,data){
- let top = 16 // 距离顶部10点像素
- const left = 8 // 距离左边
- const lightHeight = 24 // 行高3mm,1mm = 8点
- const pageW = 60 // 页签宽度mm
- const pageH = 40 // 页签高度mm
- const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
- const leftMaxFnums = Math.floor(maxFontNums*0.6)
- let rightTop = 0
- let rightLeft = (leftMaxFnums+1)*lightHeight
- // 初始化打印机
- const command = tsc.jpPrinter.createNew()
- command.init()
- command.setSize(pageW, pageH) // 标签纸张宽高,单位mm
- command.setGap(3) // 标签上下间距,单位mm
- command.setCls() // 清除缓冲区数据
- // 经销商文字高度,是否换行
- top = textFormat(command,data.dealerName,maxFontNums,left,top,lightHeight,"center",1)
- // 数字货架名称文字
- top = top+10
- rightTop = top
- top = textFormat(command,data.shelfName,leftMaxFnums,left,top,lightHeight,"left",1)
- // 产品编码
- top = top+10
- top = textFormat(command,data.productCode,leftMaxFnums,left,top,lightHeight,"left",1)
- // 产品名称
- top = top+6
- top = textFormat(command,data.productName,leftMaxFnums,left,top,lightHeight,"left",1)
- // 打印人打印时间
- top = top+10
- top = textFormat(command,data.printDate+' '+data.printUser,leftMaxFnums,left,top,lightHeight,"left",1)
- // 货位号
- const qrtop = textFormat(command,data.shelfPlaceCode,maxFontNums-leftMaxFnums-2,rightLeft,rightTop,lightHeight,"center",2)
- // 二维码
- command.setQR(rightLeft, qrtop, "L", 5, "A", data.barCode)
- command.setPagePrint(1,data.currentInven) // 打印分数1,每个标签重发打印2次
- console.log("开始打印了",command.getData())
- return command
- }
|