Skip to content

Commit a8163fa

Browse files
committed
优化文字长度计算
1 parent 468fd7a commit a8163fa

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

lib/adapter/esc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ escpos.Printer.prototype.tableCustomExt = function (data, encoding) {
6060
lineStr += ' '.repeat(obj.left)
6161

6262
if (/CENTER/i.test(obj.align)) {
63-
const spaces = (cellWidth - calWords(obj.text.trim())) / 2
63+
const len = cellWidth - calWords(obj.text.trim())
64+
const spaces = len / 2
6465
for (let j = 0; j < spaces; j++) {
6566
lineStr += ' '
6667
}
6768
if (obj.text !== '') { lineStr += obj.text.trim() }
6869

69-
for (let j = 0; j < spaces - 1; j++) {
70+
for (let j = 0; j < len - Math.ceil(spaces); j++) {
7071
lineStr += ' '
7172
}
7273
} else if (/RIGHT/i.test(obj.align)) {

lib/utils.js

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

3-
const wordWith = require('word-width')
3+
const eaw = require('eaw')
44

55
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
6+
return eaw.getWidth(text)
147
}
158

169
exports.calWords = calWords

package-lock.json

Lines changed: 12 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@posprint/command-builder",
3-
"version": "0.0.40",
3+
"version": "0.0.42",
44
"description": "JavaScript library that implements the ESC/POS and TSC protocol to buffer.",
55
"main": "index.js",
66
"repository": {
@@ -23,12 +23,11 @@
2323
"README.md"
2424
],
2525
"dependencies": {
26+
"eaw": "^0.1.5",
2627
"escpos": "3.0.0-alpha.3",
2728
"is-chinese": "^1.2.9",
2829
"joi": "14.3.1",
29-
"qrcode": "^1.4.4",
30-
"word-width": "1.0.1",
31-
"word-wrap": "1.2.3"
30+
"qrcode": "^1.4.4"
3231
},
3332
"devDependencies": {
3433
"coveralls": "^3.1.0",

0 commit comments

Comments
 (0)