fix(label): 排版不再截断,二维码必须整块打出

排不下就折行、降字号、压行距,永不截字;二维码最小组 1 点/模块,
不再整块省略。用例改为严格断言内容一字不少、有码必有整块码。
This commit is contained in:
雨天
2026-11-23 10:50:36 +08:00
parent a9d7f15e57
commit 2ec14ad9c2
3 changed files with 61 additions and 43 deletions
+47 -41
View File
@@ -220,26 +220,32 @@ 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))
# 文字和二维码之间固定留 3mm;同时给文字留够纸宽的 40%,
# 小标签上宁可码小一点,也不能把字挤成一列一个字。
qr_gap = int(round(3 * 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('标签太窄,二维码可能超出右边界;请加大标签宽度')
# 文字和二维码之间至少留 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)
# ---- 字号 + 折行:从大到小试,取「不丢字、放得下」的最大字号 ----
@@ -258,62 +264,62 @@ def plan_label(cfg, title, fields, qr_data=''):
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:
# 有二维码时文字贴顶:和右上角的码同一条水平线,读起来是“左右布局”。
# 以前一律垂直居中,码在上、字块在中,看着像上下两段(现场反馈)。
@@ -324,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:
+11 -2
View File
@@ -87,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:
@@ -106,6 +111,10 @@ 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))
+3
View File
@@ -39,6 +39,9 @@
◆ 预览和实际打印一致,文字不会压到二维码
· 文字与二维码之间固定留 3mm;长品名宁可多折一行,也不顶到码上。
· 排版不再截断:排不下就折行、降字号、必要时压缩行距,每个字都会打出来;
二维码永远整块打印(最小 1 点/模块),不会省略或缺角。
· 如果标签实在太小、内容根本排不下,日志会提示加大标签,不会默默丢字。
· ×、·、— 这类字符在 GBK 里是全角双字节,现在按全角算宽,不再低估。
· 设置页预览改用像素字号:Windows 显示缩放 125%/150% 时,预览文字
不再被放大成“压住二维码”的样子(实际打印本来就没问题)。