feat(label): 文字纵向均匀铺开,码下方行用整宽

有二维码时行距按整张纸均匀分配,底部不再留大块空白;
落在二维码静区以下的行改用整条纸宽,长品名和日期排得更开。
This commit is contained in:
雨天
2026-11-23 12:39:54 +08:00
parent fb5c88f90d
commit 5e980adfd3
3 changed files with 35 additions and 11 deletions
+25 -9
View File
@@ -182,8 +182,13 @@ def _wrap(text, font, max_w, max_lines=2):
if not cur or _text_width(cur + ch, font) <= max_w:
cur += ch
else:
out.append(cur)
cur = ch
# 在空格处换行时把空格留在上一行尾部,下一行不要顶着缩进
if ch.isspace():
out.append(cur + ch)
cur = ''
else:
out.append(cur)
cur = ch
if cur:
out.append(cur)
if len(out) > max_lines:
@@ -265,14 +270,21 @@ def plan_label(cfg, title, fields, qr_data=''):
def build(f, allow):
tmp = []
y = my
full_w = w - mx * 2 # 二维码静区以下的行可以用整宽
qr_safe = (qr['y'] + qr['size'] + 4 * qr['cell']) if qr else 0
for i, t in enumerate(texts):
if _text_width(t, f) <= text_w * slack:
tmp.append(t) # 容差内原样放,吃掉右侧留白,不截字
wid = full_w if (qr and y >= qr_safe) else text_w
if _text_width(t, f) <= wid * slack:
tmp.append(t) # 容差内原样放,吃掉右侧留白,不截字
y += f * _LINE_RATIO
continue
if i not in allow:
return None
# max_lines 给足:宁可折很多行/降字号,也绝不截字
tmp.extend(_wrap(t, f, text_w, 999))
part = _wrap(t, f, wid, 999)
tmp.extend(part)
y += len(part) * f * _LINE_RATIO
if len(tmp) * f * _LINE_RATIO > avail_h:
return None
return tmp
@@ -318,8 +330,12 @@ def plan_label(cfg, title, fields, qr_data=''):
else:
warn.append('内容较长,已压缩行距排满整张标签')
# 最后一行只占一个字号的高度,不占整行行距,垂直居中才不会显得偏上
block_h = (len(rows) - 1) * font * ratio + font
# 行距:有二维码时把文字从上到下均匀铺开(现场反馈底部空太多),
# 没二维码的标签仍垂直居中。
step = font * ratio
if qr and best is not None and len(rows) > 1:
step = max(step, (avail_h - font) / float(len(rows) - 1))
block_h = (len(rows) - 1) * step + font
if qr:
# 有二维码时文字贴顶:和右上角的码同一条水平线,读起来是“左右布局”。
# 以前一律垂直居中,码在上、字块在中,看着像上下两段(现场反馈)。
@@ -329,8 +345,8 @@ def plan_label(cfg, title, fields, qr_data=''):
lines = []
for t in rows:
lines.append({'text': t, 'font': font, 'x': mx, 'y': int(y),
'w': int(_text_width(t, font))})
y += font * ratio
'w': int(_text_width(t.rstrip(), font))})
y += step
gap_mm = _num(cfg.get('gap_mm'), 2)
if gap_mm < 0:
+8 -2
View File
@@ -61,12 +61,17 @@ def fail_reasons(p, cfg, qr):
q = p['qr']
out = []
gap = int(round(2 * dpm)) if q else 0
quiet = (4 * q['cell']) if q else 0
qr_bottom = (q['y'] + q['size']) 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'] - gap:
# 只有与码垂直范围相交(含下方静区)的行才需要让出码区;
# 码下方更远的行可以用整宽。
if q and ln['y'] < qr_bottom + quiet \
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']:
@@ -134,7 +139,8 @@ _p = L.plan_label({'width_mm': 60, 'height_mm': 40, 'dpi': 203}, '分拣标签',
'数量: 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'])
_gap = min([_q['x'] - (ln['x'] + ln['w']) for ln in _p['lines']
if ln['y'] < _q['y'] + _q['size']] or [_q['x']])
check('示例文字离码 ≥2mm(16 dots)', _gap >= 16, 'gap=%d' % _gap)
check('示例字没被截断', not any('截断' in str(x) for x in _p['warnings']),
str(_p['warnings']))
+2
View File
@@ -32,6 +32,8 @@
◆ 标签改成左右布局(文字在左、二维码在右)
· 文字块改成贴顶排版,和右上角二维码在同一条水平线上;以前文字块
垂直居中,看着像“上面一块码、中间一块字”两段。
· 文字从第一行到最后一行均匀铺满整张纸的高度,底部不再空一大块。
· 落在二维码下方(静区以外)的行用整条纸宽,长品名/日期能排得更开。
· 没有二维码的标签(调试预览)仍按原来的垂直居中,版面不变。
· 安装包出厂纸型改为 60×40mm(203dpi 下 480×320 点)。
· 老机器升级不会覆盖 config.json,纸型仍是原来的设置;需要时走