Skip to content

Commit ec213f0

Browse files
committed
fix word-width
1 parent 6121630 commit ec213f0

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

lib/adapter/esc.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict'
22

3-
const wordWidth = require('word-width')
43
const iconv = require('iconv-lite')
54
const escpos = require('escpos')
65
const qrcodeGenerator = require('../qrcode')
7-
const { wordWrap, sliceText } = require('../utils')
6+
const { wordWrap, sliceText, calWords } = require('../utils')
87
const _ = escpos.command
98

109
/**
@@ -30,7 +29,7 @@ escpos.Printer.prototype.tableCustomExt = function (data, encoding) {
3029
cellWidth = obj.cols
3130
}
3231

33-
var textLenghth = wordWidth(obj.text)
32+
var textLenghth = calWords(obj.text)
3433

3534
// If text is too wide go to next line
3635
if (cellWidth < textLenghth || (obj.chunks && obj.chunks.length)) {
@@ -57,7 +56,7 @@ escpos.Printer.prototype.tableCustomExt = function (data, encoding) {
5756
}
5857

5958
if (obj.align === 'CENTER') {
60-
const spaces = (cellWidth - wordWidth(obj.text.trim())) / 2
59+
const spaces = (cellWidth - calWords(obj.text.trim())) / 2
6160
for (let j = 0; j < spaces; j++) {
6261
lineStr += ' '
6362
}
@@ -67,15 +66,15 @@ escpos.Printer.prototype.tableCustomExt = function (data, encoding) {
6766
lineStr += ' '
6867
}
6968
} else if (obj.align === 'RIGHT') {
70-
const spaces = cellWidth - wordWidth(obj.text.trim())
69+
const spaces = cellWidth - calWords(obj.text.trim())
7170
for (let j = 0; j < spaces; j++) {
7271
lineStr += ' '
7372
}
7473
if (obj.text !== '') { lineStr += obj.text.trim() }
7574
} else {
7675
if (obj.text !== '') { lineStr += obj.text.trim() }
7776

78-
const spaces = cellWidth - wordWidth(obj.text.trim())
77+
const spaces = cellWidth - calWords(obj.text.trim())
7978
for (let j = 0; j < spaces; j++) {
8079
lineStr += ' '
8180
}

lib/utils.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
'use strict'
22

33
const wordWith = require('word-width')
4-
const isChinese = require('is-chinese')
4+
5+
const calWords = function (text) {
6+
const pins = ["’", "√"]
7+
let len = wordWith(text)
8+
for(let i=0;i<text.length;i++) {
9+
if (pins.includes(text[i])) {
10+
len ++
11+
}
12+
}
13+
return len
14+
}
15+
16+
exports.calWords = calWords
517

618
exports.converter = function (...args) {
719
const key = args.join('-')
@@ -30,14 +42,10 @@ exports.converter = function (...args) {
3042
return valueMap[key]
3143
}
3244

33-
exports.calWords = function (text) {
34-
return wordWith(text)
35-
}
36-
3745
const wordWrap = function (text, width = 1) {
3846
let chunk = []
3947
let lineStr = ''
40-
let tokenRe = /.+?(\b|$)/g
48+
let tokenRe = /.+?(\s|$)/g
4149
let words = text.match(tokenRe)
4250

4351
if (width <= 0) {
@@ -51,8 +59,8 @@ const wordWrap = function (text, width = 1) {
5159
for (let index = 0; index < words.length; index++) {
5260
let word = words[index]
5361
let preAppendStr = lineStr + word
54-
let chunkSize = wordWith(preAppendStr)
55-
let wordSize = wordWith(word)
62+
let chunkSize = calWords(preAppendStr)
63+
let wordSize = calWords(word)
5664

5765
if (chunkSize < width) {
5866
lineStr += word
@@ -70,11 +78,11 @@ const wordWrap = function (text, width = 1) {
7078
} else {
7179
let slices = sliceText(word, width)
7280
if (slices.length === 1) {
73-
wordWith(slices[0]) < width ? lineStr += slices[0] : chunk.push(slices[0])
81+
calWords(slices[0]) < width ? lineStr += slices[0] : chunk.push(slices[0])
7482
} else if (slices.length > 1) {
7583
chunk = chunk.concat(slices.slice(0, slices.length - 1))
7684
let last = slices[slices.length - 1]
77-
wordWith(last) < width ? lineStr += last : chunk.push(last)
85+
calWords(last) < width ? lineStr += last : chunk.push(last)
7886
}
7987
}
8088
}
@@ -93,9 +101,9 @@ const sliceText = function (text, width = 1) {
93101
let preAppendStr = lineStr + char
94102
if (width === 1) {
95103
char.length && chunk.push(char)
96-
} else if (wordWith(preAppendStr) < width) {
104+
} else if (calWords(preAppendStr) < width) {
97105
lineStr += char
98-
} else if (wordWith(preAppendStr) === width) {
106+
} else if (calWords(preAppendStr) === width) {
99107
preAppendStr.length && chunk.push(preAppendStr)
100108
lineStr = ''
101109
} else {

0 commit comments

Comments
 (0)