fix(label): 文字与二维码留 3mm,预览按像素排版
GBK 双字节字符(×、·、—)按全角算宽,长品名不再探到码上; 预览字号改用像素值,系统显示缩放不再把预览字放大、看着像压码。
This commit is contained in:
+13
-4
@@ -145,10 +145,15 @@ def label_geom(cfg):
|
||||
|
||||
|
||||
def _text_width(text, font):
|
||||
"""估算一行文字的点阵宽度:汉字≈字号,ASCII≈0.55 字号"""
|
||||
"""估算一行文字的点阵宽度:GBK 双字节(汉字/全角标点)≈字号,ASCII≈0.56 字号
|
||||
|
||||
v1.0.69:判断从「码点 > 0x2E80」改成「非 ASCII」。TSPL 用 GBK 下发,
|
||||
×、·、°、— 这些不在 0x2E80 以上,但 GBK 里是双字节全角字(× 是 A1C1),
|
||||
打印机按全角打;按半角估会低估宽度,长品名就可能顶到二维码。
|
||||
"""
|
||||
w = 0.0
|
||||
for ch in str(text or ''):
|
||||
w += font if ord(ch) > 0x2E80 else font * 0.56
|
||||
w += font if ord(ch) > 0x7F else font * 0.56
|
||||
return w
|
||||
|
||||
|
||||
@@ -232,7 +237,10 @@ def plan_label(cfg, title, fields, qr_data=''):
|
||||
else:
|
||||
warn.append('标签太小,二维码放不下(已省略);建议加大宽度/高度')
|
||||
|
||||
text_w = w - mx * 2 - (qr_w + int(round(2 * dpm)) if qr else 0)
|
||||
# 文字和二维码之间至少留 3mm:TSPL 点阵字实际宽度只能估,留够余量才不会
|
||||
# 压码(扫不出码比多折一行严重得多)。
|
||||
qr_gap = int(round(3 * dpm)) if qr else 0
|
||||
text_w = w - mx * 2 - (qr_w + qr_gap if qr else 0)
|
||||
|
||||
# ---- 字号 + 折行:从大到小试,取「不丢字、放得下」的最大字号 ----
|
||||
# 三档退让,优先保版面整齐:
|
||||
@@ -246,7 +254,8 @@ def plan_label(cfg, title, fields, qr_data=''):
|
||||
# 日期/时间/重量/数量/订单号是原子串,后面也不允许折;
|
||||
# 宁可整体降一号字,也不能把日期劈成“11-06 1”+“0:00”。
|
||||
longest = 2 if len(texts) > 2 else 0
|
||||
slack = 1.02
|
||||
# 有二维码时不留 2% 容差:宁可多折一行,也不能让文字探进码里。
|
||||
slack = 1.0 if qr else 1.02
|
||||
|
||||
def build(f, allow):
|
||||
tmp, folds = [], 0
|
||||
|
||||
@@ -60,14 +60,15 @@ def fail_reasons(p, cfg, qr):
|
||||
my = max(int(round(1.5 * dpm)), 6)
|
||||
q = p['qr']
|
||||
out = []
|
||||
gap = int(round(3 * dpm)) if q else 0
|
||||
for ln in p['lines']:
|
||||
if ln['x'] < mx - 1:
|
||||
out.append('行越左边界 x=%d' % ln['x'])
|
||||
if ln['x'] + ln['w'] > w:
|
||||
out.append('行越右纸边 x+w=%d > %d' % (ln['x'] + ln['w'], w))
|
||||
if q and ln['x'] + ln['w'] > q['x'] - 4:
|
||||
out.append('行压到二维码 x+w=%d, 码左沿=%d'
|
||||
% (ln['x'] + ln['w'], q['x']))
|
||||
if q and ln['x'] + ln['w'] > q['x'] - gap:
|
||||
out.append('行离码不足 %dmm x+w=%d, 码左沿=%d'
|
||||
% (gap // int(round(dpm)), ln['x'] + ln['w'], q['x']))
|
||||
if p['lines']:
|
||||
y0 = p['lines'][0]['y']
|
||||
yl = p['lines'][-1]['y'] + p['font']
|
||||
@@ -108,6 +109,27 @@ for (wmm, hmm) in SIZES:
|
||||
|
||||
print('版面不变量:%d 通过,%d 失败' % (ok, fail))
|
||||
|
||||
# ---- 宽度模型:GBK 双字节字符必须是全角(×、·、— 这些不是汉字但仍是双字节)----
|
||||
print('=== 宽度模型(GBK 双字节 = 全角)===')
|
||||
check('× 按全角算', L._text_width('×', 24) == 24, str(L._text_width('×', 24)))
|
||||
check('· 按全角算', L._text_width('·', 24) == 24, str(L._text_width('·', 24)))
|
||||
check('— 按全角算', L._text_width('—', 24) == 24, str(L._text_width('—', 24)))
|
||||
check('ASCII 仍按半角', abs(L._text_width('A', 24) - 24 * 0.56) < 0.01,
|
||||
str(L._text_width('A', 24)))
|
||||
|
||||
# ---- 设置页预览那组数据:文字右缘离码至少 3mm ----
|
||||
print('=== 预览示例不压码 ===')
|
||||
_p = L.plan_label({'width_mm': 60, 'height_mm': 40, 'dpi': 203}, '分拣标签',
|
||||
['客户: 张三水果批发店',
|
||||
'品名: 农夫山泉饮用天然水 550ml×24',
|
||||
'数量: 1.00 桶', '日期: 11-04 19:02'],
|
||||
qr_data='CT2026110400001234')
|
||||
_q = _p['qr']
|
||||
_gap = min(_q['x'] - (ln['x'] + ln['w']) for ln in _p['lines'])
|
||||
check('示例文字离码 ≥3mm(24 dots)', _gap >= 24, 'gap=%d' % _gap)
|
||||
check('示例字没被截断', not any('截断' in str(x) for x in _p['warnings']),
|
||||
str(_p['warnings']))
|
||||
|
||||
# ---- 再验一遍真正的 TSPL 字节:坐标在纸内、文字一个字不少 ----
|
||||
print('=== 实际 TSPL 指令校验 ===')
|
||||
for (wmm, hmm) in ((50, 50), (60, 40)):
|
||||
@@ -117,10 +139,15 @@ for (wmm, hmm) in ((50, 50), (60, 40)):
|
||||
'2026-11-22 18:30', 'CT-WEB-20261122-BR003-000123456789')
|
||||
s = data.decode('gbk', errors='replace')
|
||||
tag = 'TSPL %gx%g' % (wmm, hmm)
|
||||
# 长文字会折成多条 TEXT,拼起来才是完整内容(不能按整段子串找)
|
||||
import re
|
||||
payloads = re.findall(r'TEXT \d+,\d+,"TSS\d+\.BF2",0,1,1,"([^"]*)"', s)
|
||||
payloads += re.findall(r'QRCODE \d+,\d+,\w,\d,\w,\d,M2,S1,"([^"]*)"', s)
|
||||
joined = ''.join(payloads)
|
||||
for need in ('张三水果批发市场', '农夫山泉 550ml×24', '15.00',
|
||||
'2026-11-22 18:30', 'CT-WEB-20261122-BR003-000123456789'):
|
||||
check('%s 含 %s' % (tag, need[:10]), need in s,
|
||||
'内容丢了')
|
||||
check('%s 含 %s' % (tag, need[:10]), need in joined,
|
||||
'内容丢了:%r' % joined)
|
||||
# TEXT x,y,"font",0,1,1,"..." → 坐标都在纸内
|
||||
import re
|
||||
for m in re.finditer(r'TEXT (\d+),(\d+),"TSS(\d+)', s):
|
||||
|
||||
@@ -6521,9 +6521,12 @@ class App:
|
||||
hh = ln['font'] * s
|
||||
cv.create_rectangle(x0, y0, x0 + ln['w'] * s, y0 + hh * 0.92,
|
||||
outline='#dfe5ee')
|
||||
cv.create_text(x0 + 1, y0 + hh * 0.46, text=ln['text'],
|
||||
# 字号必须按像素给(负数),不能给磅值:Windows 显示缩放 125%/150%
|
||||
# 会把磅值放大一大截,预览里的字比实际打出来宽、看着像压住二维码,
|
||||
# 现场就不敢按了。按像素画,预览和打印 1:1 对应。
|
||||
cv.create_text(x0 + 1, y0 + hh * 0.5, text=ln['text'],
|
||||
anchor='w', fill='#17334f',
|
||||
font=('Microsoft YaHei', max(6, int(hh * 0.82))))
|
||||
font=('Microsoft YaHei', -max(6, int(hh))))
|
||||
if p['qr']:
|
||||
q = p['qr']
|
||||
x0 = ox + q['x'] * s
|
||||
|
||||
@@ -37,6 +37,12 @@
|
||||
· 老机器升级不会覆盖 config.json,纸型仍是原来的设置;需要时走
|
||||
「设置 → 标签打印机 → 标签纸… → 自动读取尺寸」重新读一次。
|
||||
|
||||
◆ 预览和实际打印一致,文字不会压到二维码
|
||||
· 文字与二维码之间固定留 3mm;长品名宁可多折一行,也不顶到码上。
|
||||
· ×、·、— 这类字符在 GBK 里是全角双字节,现在按全角算宽,不再低估。
|
||||
· 设置页预览改用像素字号:Windows 显示缩放 125%/150% 时,预览文字
|
||||
不再被放大成“压住二维码”的样子(实际打印本来就没问题)。
|
||||
|
||||
◆ 点待办卡不再抖、也不会两张同时亮
|
||||
· 所有卡片外框固定 4px,选中(琥珀)/ 确认(绿)只换颜色、不改粗细。
|
||||
以前确认行比别的卡粗 2px,一点卡整排卡片就被撑大、界面跟着跳。
|
||||
|
||||
Reference in New Issue
Block a user