123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- // 获取文本字节总数
- 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 textArr = getTextRows(text.replace(/\s+/g, ' '),maxFontNums*2)
- const rows = textArr.length
- for(let i=0;i<rows;i++){
- if(align=="center"){
- const ltxt = textArr[i]
- const ltlen = ltxt.replace(/[^x00-xFF]/g,'**').length/2
- command.setText(left+((maxFontNums-ltlen)/2)*lightHeight, rowTop, "TSS24.BF2", fontSize, fontSize, ltxt)
- }
- else{
- command.setText(left, rowTop, "TSS24.BF2", fontSize, fontSize, textArr[i])
- }
- rowTop = rowTop+lightHeight
- }
- return rowTop
- }
|