Skip to content

Calibrate font metrics against Labelary - #27

Draft
ValeHaas wants to merge 2 commits into
GOODBOY008:mainfrom
ValeHaas:feature/improve-diffs
Draft

Calibrate font metrics against Labelary#27
ValeHaas wants to merge 2 commits into
GOODBOY008:mainfrom
ValeHaas:feature/improve-diffs

Conversation

@ValeHaas

Copy link
Copy Markdown
Contributor

Calibrate font metrics against Labelary

Two renderer constants were off; both were tuned by sweeping over the full
golden corpus (110 fixtures) and taking the minimum of the total-diff curve.

  • Font 0 width-to-height ratio 0.900.925 — our Helvetica Bold
    substitute is narrower than Zebra's CG Triumvirate, so long strings drifted
    left of the reference and the error accumulated across the line.
  • ^FT baseline ascent 0.780.76 of cell height.

Also adds *.env to .gitignore so local Labelary API credentials can't be
committed.

Results

Total diff (110 fixtures) Mean Labels ≥5%
before 267.76 2.434 % 15
after 226.64 2.060 % 9
Fixture before after
fedex_express 13.47 11.33 −2.14
kmart 7.03 4.92 −2.11
ups_surepost 7.61 5.70 −1.91
fedex_ground 10.54 8.96 −1.58
usps_intl 8.29 6.74 −1.55
ups 5.74 4.28 −1.46
ups_import_control 10.98 9.54 −1.44
pnldpd 10.15 9.00 −1.15
jcpenney 4.64 3.53 −1.11
usps_apo 9.82 8.73 −1.09

Nine fixtures regress: porterbuddy +0.36, brtit +0.34, encodings_013 +0.13,
six text_fo/ft_* at +0.02…0.04 — all well inside their tolerances.

The bitmap-font advance constant (1.931) was swept too and is already optimal;
left unchanged.

⚠️ One deliberately failing test

unit_renderer::font_0_width_ratio_produces_narrower_text fails. Everything
else is green (16/17 test binaries, clippy clean).

It renders ^A0N,40,40^FDWWWWW and asserts the ink width is < 160. We now
render 162. Labelary renders that exact string 161 px — so the bound
excludes the reference value itself and makes parity unreachable. The test's
comment still describes a "~60 % width ratio" the code stopped using some time
ago; at 0.90 it rendered 158, which is why it passed.

Suggested fix — keep the original intent (guard against a 1:1 ratio, which
would be ~200 px) but allow the reference value:

assert!(
    (150..=175).contains(&text_width),
    "font 0 text width ({}) should stay near Labelary's 161 px, not full 1:1",
    text_width
);

The alternative is capping the ratio at 0.91, the largest value satisfying
the current bound — that yields 252.53 instead of 226.64, i.e. about a third
of the improvement.

Notes

0.925 is a corpus-wide compromise, not a per-glyph fix: CG Triumvirate is a
different typeface and the mismatch isn't uniform (at 0.90 our W was ~2 %
narrow while mixed proportional text was ~4 % narrow). A per-glyph advance
table would fix both but needs the real font metrics.

HAG-VHaas and others added 2 commits July 27, 2026 14:11
Two font metric constants were tuned by sweeping each over the full golden
corpus (110 fixtures) and taking the minimum of the total-diff curve.

Font 0 width-to-height ratio 0.90 -> 0.91: our Helvetica Bold substitute is
narrower than Zebra's CG Triumvirate, so long strings drifted left of the
reference. The curve keeps falling to ~0.925 (total 267.8 -> 226.6), but
"WWWWW" then renders 162 px and font_0_width_ratio_produces_narrower_text
asserts < 160 -- Labelary itself renders that string 161 px, so the bound is
stale. Capped at 0.91 to keep the suite green.

^FT baseline ascent 0.78 -> 0.76 of cell height.

Total pixel diff 267.76 -> 252.53 (mean 2.434% -> 2.296%). 5 fixtures regress
by <= 0.10 pp, all far inside tolerance; biggest gains fedex_express -0.71,
usps_apo -0.62, pnldpd -0.60, posteit -0.55.

Also ignore *.env so local Labelary API credentials cannot be committed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Raises the font 0 width-to-height ratio from the 0.91 cap to 0.925, the actual
minimum of the total-diff curve over the Labelary golden corpus.

Total pixel diff 252.53 -> 226.64 (mean 2.296% -> 2.060%); against 858fc1b the
combined calibration is 267.76 -> 226.64 (2.434% -> 2.060%). Labels drop from
15 to 9 moderate. Biggest gains fedex_express -2.14, kmart -2.11,
ups_surepost -1.91, fedex_ground -1.58, usps_intl -1.55.

KNOWN FAILING TEST: unit_renderer::font_0_width_ratio_produces_narrower_text.
It renders ^A0N,40,40^FDWWWWW and asserts the ink width is < 160; we now
render 162. Labelary renders that same string 161 px, so the bound rules out
matching the reference, and the test's comment still describes a "~60% width
ratio" the code has not used for some time. Left failing deliberately -- to be
resolved in review.

Nine fixtures regress, the largest by 0.36 pp (porterbuddy, brtit); all remain
well inside their tolerances.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@ValeHaas
ValeHaas marked this pull request as draft July 28, 2026 11:07
@GOODBOY008 GOODBOY008 added the enhancement New feature or request label Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants