Compare commits

...
5 Commits
Author SHA1 Message Date
雨天 fb5c88f90d fix(label): 二维码静区留 2mm,避免短标签掉字号
3mm 留白在无 60×40 短标签上挤掉一档字号(32→24);2mm 正好是
二维码 4 模块静区,配合严格不留容差已经不会压码。
2026-11-23 11:03:25 +08:00
雨天 827c8b4f42 fix(ui): 卡片高亮跟着手点走,和详情行同源
上一版改成跟引擎预选,导致非待机态点卡时亮的是旧预选、
详情却是刚点的那张。改为只看 _sel_rid,点哪张亮哪张。
2026-11-23 10:59:42 +08:00
雨天 6c0e5d7f57 fix(ui): 琥珀高亮只认引擎预选,杜绝两张同时亮
非待机态点卡、连点时旧 preselect 事件晚到都会造成两个高亮来源
不同步;改为单一口径并丢弃过期事件。
2026-11-23 10:55:47 +08:00
雨天 2ec14ad9c2 fix(label): 排版不再截断,二维码必须整块打出
排不下就折行、降字号、压行距,永不截字;二维码最小组 1 点/模块,
不再整块省略。用例改为严格断言内容一字不少、有码必有整块码。
2026-11-23 10:50:36 +08:00
雨天 a9d7f15e57 fix(label): 文字与二维码留 3mm,预览按像素排版
GBK 双字节字符(×、·、—)按全角算宽,长品名不再探到码上;
预览字号改用像素值,系统显示缩放不再把预览字放大、看着像压码。
2026-11-23 10:48:29 +08:00
5 changed files with 184 additions and 67 deletions
+57 -42
View File
@@ -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
@@ -215,24 +220,33 @@ def plan_label(cfg, title, fields, qr_data=''):
for t in (fields or [])]
n = max(len(texts), 1)
# ---- 二维码:先看放不放得下,放不下就整块不打,绝不打半个码 ----
# ---- 二维码:必须整块打出来(打半个码等于没打,绝不省略)----
qr_data = _safe_text(qr_data)
qr = None
qr_w = 0
qr_gap = 0
if qr_data:
mods = _qr_modules(len(str(qr_data).encode('gbk', 'ignore')))
cap = min(h - 2 * my, int(w * 0.34))
# 文字和二维码之间留 2mm(=二维码左侧 4 个模块静区,扫码不受干扰);
# 同时给文字留够纸宽的 40%,小标签上宁可码小一点,也不能把字挤成一列一个字。
qr_gap = int(round(2 * dpm))
cap = min(h - 2 * my, int(w * 0.34),
w - 2 * mx - qr_gap - int(w * 0.40))
cell = 4
while cell >= 2 and mods * cell > cap:
while cell > 1 and mods * cell > cap:
cell -= 1
if cell >= 2 and mods * cell <= cap:
qr_w = mods * cell
qr = {'modules': mods, 'cell': cell, 'size': qr_w,
'x': w - mx - qr_w, 'y': my}
else:
warn.append('标签太小,二维码放不下(已省略);建议加大宽度/高度')
if mods * cell > cap:
# 连 1 点/模块都放不下(极小纸型):按 1 点打,不省略、不裁
cell = 1
warn.append('标签太小,二维码只能按最小点阵打印(可能扫不出);'
'建议加大标签宽/高')
qr_w = mods * cell
qr = {'modules': mods, 'cell': cell, 'size': qr_w,
'x': w - mx - qr_w, 'y': my}
if qr['x'] < mx:
warn.append('标签太窄,二维码可能超出右边界;请加大标签宽度')
text_w = w - mx * 2 - (qr_w + int(round(2 * dpm)) if qr else 0)
text_w = w - mx * 2 - (qr_w + qr_gap if qr else 0)
# ---- 字号 + 折行:从大到小试,取「不丢字、放得下」的最大字号 ----
# 三档退让,优先保版面整齐:
@@ -246,65 +260,66 @@ 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
tmp = []
for i, t in enumerate(texts):
if _text_width(t, f) <= text_w * slack:
tmp.append(t) # 容差内原样放,吃掉右侧留白,不截字
continue
if i == 0:
tmp.append(_cut(t, f, text_w)) # 标题永远单行
continue
if i in allow:
part = _wrap(t, f, text_w, 2)
if _text_width(part[-1], f) > text_w:
return None
tmp.extend(part)
folds += 1
else:
if i not in allow:
return None
# max_lines 给足:宁可折很多行/降字号,也绝不截字
tmp.extend(_wrap(t, f, text_w, 999))
if len(tmp) * f * _LINE_RATIO > avail_h:
return None
return tmp, folds
return tmp
def best_in_tier(allow):
"""这一档里能放下的最大字号;一档都没有就返回 None"""
for f in _FONT_STEPS_CAP:
got = build(f, allow)
if got:
return f, got[0]
if got is not None:
return f, got
return None
# 在先保证「日期/时间/重量/数量/订单号不折」的前提下排版:
# ① 全部单行 / ② 只折品名 —— 两档里取字号大的(不折客户名)
# ③ ① ② 都放不下时,才允许客户/供应商也折(原子串仍不折)
# ④ 依然放不下:所有字段都允许折(原子串也折)——能折就不截。
# 以前 ③ 允许所有字段折,32 号字会把日期劈成两行,现场根本看不懂。
best = None
for allow in ((), (longest,)):
got = best_in_tier(allow)
if got and (best is None or got[0] > best[1]):
best = (0, got[0], got[1])
if got and (best is None or got[0] > best[0]):
best = got
if best is None:
allow_all = tuple(i for i in range(len(texts))
if not _is_atomic_line(texts[i]))
if allow_all:
got = best_in_tier(allow_all)
if got:
best = (0, got[0], got[1])
got = best_in_tier(tuple(range(len(texts))))
if got:
best = got
ratio = _LINE_RATIO
if best is not None:
font, rows = best[1], best[2]
font, rows = best
else:
# 实在太小:退到最小字号,每个字段只留一行并截断
# 怎么排都放不下:最小字号 + 所有字段不限行数折开(绝不截字),
# 行距压到刚好放得下;真排不下时明确告警,让现场加大标签。
font = _FONT_STEPS_CAP[-1]
rows = [_cut(t, font, text_w) for t in texts]
warn.append('标签太小,部分文字已截断(字号 %d);'
'想打全请加大标签宽度/高度' % font)
rows = []
for t in texts:
rows.extend(_wrap(t, font, text_w, 999))
if len(rows) * font * ratio > avail_h:
ratio = max(0.95, float(avail_h) / max(1, len(rows) * font))
if len(rows) * font * ratio > avail_h + 1:
warn.append('内容太长,这张标签排不下全部文字;'
'请加大标签或缩短商品名/客户名')
else:
warn.append('内容较长,已压缩行距排满整张标签')
# 最后一行只占一个字号的高度,不占整行行距,垂直居中才不会显得偏上
block_h = (len(rows) - 1) * font * _LINE_RATIO + font
block_h = (len(rows) - 1) * font * ratio + font
if qr:
# 有二维码时文字贴顶:和右上角的码同一条水平线,读起来是“左右布局”。
# 以前一律垂直居中,码在上、字块在中,看着像上下两段(现场反馈)。
@@ -315,7 +330,7 @@ def plan_label(cfg, title, fields, qr_data=''):
for t in rows:
lines.append({'text': t, 'font': font, 'x': mx, 'y': int(y),
'w': int(_text_width(t, font))})
y += font * _LINE_RATIO
y += font * ratio
gap_mm = _num(cfg.get('gap_mm'), 2)
if gap_mm < 0:
+43 -7
View File
@@ -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(2 * 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']
@@ -86,11 +87,16 @@ def fail_reasons(p, cfg, qr):
def content_ok(p, title, fields):
"""内容必须一字不少地打出来:不允许任何形式的截断"""
src = L._safe_text(title or '') + ''.join(
L._safe_text(f or '') for f in fields)
got = ''.join(ln['text'] for ln in p['lines'])
truncated = any('截断' in str(x) for x in (p.get('warnings') or []))
return (got == src) or truncated, src, got
return got == src, src, got
def no_truncate(p):
return not any('截断' in str(x) for x in (p.get('warnings') or [])), \
str(p.get('warnings'))
for (wmm, hmm) in SIZES:
@@ -105,9 +111,34 @@ for (wmm, hmm) in SIZES:
good, src, got = content_ok(p, title, fields)
check('%s 内容完整' % tag, good,
'源=%r 得=%r' % (src, got))
nt, ww = no_truncate(p)
check('%s 无截断' % tag, nt, ww)
check('%s 有码必打出整块' % tag, bool(p['qr']) == bool(qr),
'qr=%r' % (p['qr'],))
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)))
# ---- 设置页预览那组数据:文字右缘离码至少 2mm(二维码静区) ----
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('示例文字离码 ≥2mm(16 dots)', _gap >= 16, '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 +148,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):
+40 -1
View File
@@ -478,6 +478,7 @@ def _positions():
# 点一张 → 只有它亮,位置/尺寸不动
p_before = _positions()
app._pre_rid = None
app._sel_rid = 'c1'
app._repaint_cards(['c1'])
root.update()
@@ -490,13 +491,51 @@ check('选中不改边框粗细',
== {A.CARD_HL},
str({c.cget('highlightthickness') for c in app.todo_cards.values()}))
# 引擎接管确认行 → 旧的琥珀选中被清掉,只剩一张绿的
# 高亮只跟手点那张走(现场:亮盐、详情却是刚点的腐竹)
app._pre_rid = 'c0'
app._sel_rid = 'c2'
app._repaint_cards(['c0', 'c1', 'c2', 'c3'])
root.update()
lit = {r: c for r, c in app._paint_cache.items() if c != A.C.line}
check('高亮跟最后点过的那张走', set(lit) == {'c2'}, str(lit))
# 连点两张:先点那张的 preselect 事件晚到,不能把高亮挪回去
app.engine.state = 'IDLE'
app._pre_rid = None
app._sel_rid = None
app._repaint_cards(['c0', 'c1', 'c2', 'c3'])
app._select_card('c0')
app._select_card('c1')
app._on_preselect('c0') # 迟到的旧事件
root.update()
lit = {r: c for r, c in app._paint_cache.items() if c != A.C.line}
check('迟到的预选事件不挪高亮', set(lit) == {'c1'}, str(lit))
# 非待机态点卡:引擎不收预选,但高亮仍跟着手点走(只能一张)
app.engine.state = 'WEIGHING'
app.engine._pre_pick_rid = 'c0' # 引擎真正记着的预选
app._pre_rid = 'c0'
app._sel_rid = None
app._repaint_cards(['c0', 'c1', 'c2', 'c3'])
app._select_card('c2')
root.update()
lit = {r: c for r, c in app._paint_cache.items() if c != A.C.line}
check('非待机态点卡也只亮手点那张', set(lit) == {'c2'}, str(lit))
app.engine.state = 'IDLE'
app.engine._pre_pick_rid = None
# 引擎接管确认行 → 旧琥珀被清掉,只剩一张绿的
app._pre_rid = 'c1'
app._sel_rid = 'c1'
app._repaint_cards(['c0', 'c1', 'c2', 'c3'])
root.update()
p2 = _positions()
app._on_focus({'row_id': 'c3', 'mode': 'weigh'})
root.update()
lit = {r: c for r, c in app._paint_cache.items() if c != A.C.line}
check('确认时只剩一张高亮', len(lit) == 1 and 'c3' in lit, str(lit))
check('旧的手动选中已清', app._sel_rid is None, str(app._sel_rid))
check('旧的预选已清', app._pre_rid is None, str(app._pre_rid))
p3 = _positions()
check('确认行高亮不引起抖动', p2 == p3,
str({k: (p2[k], p3[k]) for k in p3 if p2[k] != p3[k]}))
+32 -17
View File
@@ -2943,18 +2943,20 @@ class App:
def _card_style(self, rid):
"""这张卡此刻应有的边框颜色
优先级:确认态那一行(绿)> 空秤预选 / 手动选中(琥珀)> 普通
优先级:确认态那一行(绿)> 最后点过的那张(琥珀)> 普通
v1.0.69:只变颜色、**不变边框粗细**。以前确认行是 4px、其余 2px,
一张卡变粗就把它那一行撑高 4px,整列跟着重排 —— 现场就是
「一点卡片界面就抖」。现在所有卡片统一 CARD_HL 浅灰,选中/确认只换色。
琥珀只有一个来源:`_sel_rid`(最后点过的那张),和上方详情行
同源——点哪张就亮哪张,不会出现“亮的是盐、详情写腐竹”。
以前看两个变量(手点 + 引擎预选),两者不同步时就是两张同时亮。
"""
rid = str(rid)
fp = getattr(self, '_focus_p', None) or {}
if fp.get('row_id') is not None and str(fp.get('row_id')) == rid:
return C.green
if str(getattr(self, '_pre_rid', '') or '') == rid:
return C.amber
if str(getattr(self, '_sel_rid', '') or '') == rid:
return C.amber
return C.line
@@ -2998,10 +3000,21 @@ class App:
self._repaint_cards([old, self._focus_rid])
def _on_preselect(self, rid):
"""引擎回的「空秤预选」:只高亮,不重复调 preselect(会绕圈)"""
old = getattr(self, '_pre_rid', None)
self._pre_rid = str(rid)
self._repaint_cards([old, self._pre_rid])
"""引擎回的「空秤预选」:只记录,不重画
高亮已经由 _select_card 当场画好了(点哪张亮哪张),这里再动会和
「最后点过谁」打架。事件是排队来的,旧事件晚到时直接丢掉。
只有一种例外:界面还没有选中行(比如刚从确认态回来),才把引擎
的预选补成选中,别让预选变哑炮。
"""
rid = str(rid)
cur = str(getattr(self.engine, '_pre_pick_rid', None) or '')
if cur != rid:
return # 过期的旧事件,丢掉
self._pre_rid = rid
if not str(getattr(self, '_sel_rid', '') or ''):
self._sel_rid = rid
self._repaint_cards([rid])
def _scroll_to(self, card, _retry=0):
"""v1.0.58:名单不滚动了,「跳过去」= 翻到这张卡所在那一页"""
@@ -3949,19 +3962,18 @@ class App:
return
# v1.0.40:空秤(待机)点卡片 = 预选这一行 —— 放货识别后以 AI
# 命中为准,识别不出仍走人工确认页。高亮下面统一画。
old_pre = getattr(self, '_pre_rid', None)
# v1.0.69:高亮和详情行同源,永远跟着「最后点过的那张」;引擎收不收
# 预选只影响 _pre_rid(记账参考),不能再决定画哪张,否则会出现
# 「亮的是旧预选、详情是新点的」两张错位。
if self.engine.state == 'IDLE':
try:
# 预选高亮当场挪过去,别等引擎事件回来 —— 否则连点两张时
# 会有一帧「两张同时亮」(pending 事件要过一轮 _poll 才到)。
if self.engine.preselect(rid):
self._pre_rid = rid
self._pre_rid = rid if self.engine.preselect(rid) else None
except Exception:
pass
self._pre_rid = None
old = getattr(self, '_sel_rid', None)
self._sel_rid = rid
# 只重画变了的这几张(原来遍历整张名单 configure,120 行 35ms)
self._repaint_cards([old, old_pre, rid])
# 只重画变了的这两张(原来遍历整张名单 configure,120 行 35ms)
self._repaint_cards([old, rid])
r = self.todo_rows.get(rid) or {}
bits = [r.get('item_name') or '—']
if r.get('spec'):
@@ -6521,9 +6533,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
+12
View File
@@ -37,10 +37,22 @@
· 老机器升级不会覆盖 config.json,纸型仍是原来的设置;需要时走
「设置 → 标签打印机 → 标签纸… → 自动读取尺寸」重新读一次。
◆ 预览和实际打印一致,文字不会压到二维码
· 文字与二维码之间留出二维码的 4 模块静区(约 2mm),长品名宁可多折一行,
也不顶到码上。
· 排版不再截断:排不下就折行、降字号、必要时压缩行距,每个字都会打出来;
二维码永远整块打印(最小 1 点/模块),不会省略或缺角。
· 如果标签实在太小、内容根本排不下,日志会提示加大标签,不会默默丢字。
· ×、·、— 这类字符在 GBK 里是全角双字节,现在按全角算宽,不再低估。
· 设置页预览改用像素字号:Windows 显示缩放 125%/150% 时,预览文字
不再被放大成“压住二维码”的样子(实际打印本来就没问题)。
◆ 点待办卡不再抖、也不会两张同时亮
· 所有卡片外框固定 4px,选中(琥珀)/ 确认(绿)只换颜色、不改粗细。
以前确认行比别的卡粗 2px,一点卡整排卡片就被撑大、界面跟着跳。
· 引擎接管确认行时会清掉手动点过 / 预选的琥珀框,只亮一张。
· 琥珀高亮只看「最后点过的那张」,和上方详情行同源:点哪张就亮哪张,
且永远只有一张;连点时旧事件晚到、非待机态点卡都不会再两张同时亮。
· 连点两张也不会出现一帧“两张同时亮”。
【v1.0.68 整组短收按部分成功处理(2026-11-22)】