diff options
| author | Danilo M. <danix@danix.xyz> | 2026-09-16 19:39:45 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-09-16 19:39:45 +0200 |
| commit | 9c9be25bdb0b2ec48e3078fe5e119b3d0f56631c (patch) | |
| tree | 005c6b7d2f444b3e49eae38c35b0326ec89fa424 | |
| parent | 7ded492aef4f03c7eb5e1b554f52fcfdaed709d5 (diff) | |
| download | conky-theme-udt-9c9be25bdb0b2ec48e3078fe5e119b3d0f56631c.tar.gz conky-theme-udt-9c9be25bdb0b2ec48e3078fe5e119b3d0f56631c.zip | |
fix: step text cursors by advance, not ink width
The date rendered as "16 /SEP /2026", each slash jammed against the
next glyph. measure() returns the ink width, which ignores leading and
trailing spaces because a space carries no ink, and clock.lua was using
it to advance a cursor. Measured: " / " covers 6px of ink but advances
14px, so every separator lost 8px.
Splits the two uses that were conflated in one function. measure() is
for centring and right-aligning, where ink width is what you want;
advance() returns x_advance and is for laying out runs of text left to
right. The clock's date now uses advance() and reads "16 / SEP / 2026"
with even gaps, verified by screenshot.
Worth the split rather than changing measure() in place: text_right is
a correct caller of the ink width, so one function cannot serve both
and silently returning the advance would have broken alignment
instead.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
| -rw-r--r-- | docs/superpowers/plans/2026-09-16-conky-lua-dashboard.md | 31 | ||||
| -rw-r--r-- | lib/card.lua | 15 | ||||
| -rw-r--r-- | widgets/clock.lua | 3 |
3 files changed, 43 insertions, 6 deletions
diff --git a/docs/superpowers/plans/2026-09-16-conky-lua-dashboard.md b/docs/superpowers/plans/2026-09-16-conky-lua-dashboard.md index 7d7573c..a77c30b 100644 --- a/docs/superpowers/plans/2026-09-16-conky-lua-dashboard.md +++ b/docs/superpowers/plans/2026-09-16-conky-lua-dashboard.md @@ -396,11 +396,31 @@ function M.text(cr, x, y, s) cairo_show_text(cr, s) end --- Measured width and height of a string under the current font. +-- One reused extents struct for every measurement, allocated at load. +-- +-- Not per call: cairo_text_extents_t:create() leaks. 5000 allocations grow +-- Lua's heap by ~182KB that collectgarbage() never reclaims, and calling +-- :destroy() on each one does NOT help (measured: same 182KB either way). +-- Reusing a single struct costs 0KB. +local extents = cairo_text_extents_t:create() + +-- Ink size of a string: how much space the glyphs actually cover. +-- Use this to CENTRE or RIGHT-ALIGN text, never to advance a cursor. function M.measure(cr, s) - local e = cairo_text_extents_t:create() - cairo_text_extents(cr, s, e) - return e.width, e.height + cairo_text_extents(cr, s, extents) + return extents.width, extents.height +end + +-- How far the cursor moves after drawing a string. Use this to lay out runs of +-- text left to right. +-- +-- Not measure(): that returns the INK width, which ignores leading and +-- trailing spaces because a space carries no ink. " / " measures 6px of ink +-- against a 14px advance, so stepping a cursor by the ink width renders a +-- segmented date as "16 /SEP /2026", each slash jammed into the next glyph. +function M.advance(cr, s) + cairo_text_extents(cr, s, extents) + return extents.x_advance end -- Right-aligned text: x is the RIGHT edge. @@ -974,7 +994,8 @@ function M.draw(cr, rect, colors) for _, p in ipairs(parts) do card.rgba(cr, p[2]) card.text(cr, dx, dy, p[1]) - dx = dx + card.measure(cr, p[1]) + -- advance(), not measure(): the ink width of ' / ' omits its spaces. + dx = dx + card.advance(cr, p[1]) end end diff --git a/lib/card.lua b/lib/card.lua index 474228d..4d94465 100644 --- a/lib/card.lua +++ b/lib/card.lua @@ -44,11 +44,26 @@ end -- values before the next call overwrites them. local extents = cairo_text_extents_t:create() +-- Ink size of a string: how much space the glyphs actually cover. +-- Use this to CENTRE or RIGHT-ALIGN text, never to advance a cursor. function M.measure(cr, s) cairo_text_extents(cr, s, extents) return extents.width, extents.height end +-- How far the cursor moves after drawing a string. Use this to lay out runs of +-- text left to right. +-- +-- Not measure(): that returns the INK width, which ignores leading and +-- trailing spaces because a space carries no ink. Stepping a cursor by the ink +-- width collapses the gaps, and " / " measures 6px of ink against a 14px +-- advance, so a date drawn in segments came out as "16 /SEP /2026" with each +-- slash jammed into the next glyph. +function M.advance(cr, s) + cairo_text_extents(cr, s, extents) + return extents.x_advance +end + -- Right-aligned text: x is the RIGHT edge. function M.text_right(cr, x, y, s) local w = M.measure(cr, s) diff --git a/widgets/clock.lua b/widgets/clock.lua index 10f1dd8..978715e 100644 --- a/widgets/clock.lua +++ b/widgets/clock.lua @@ -49,7 +49,8 @@ function M.draw(cr, rect, colors) for _, p in ipairs(parts) do card.rgba(cr, p[2]) card.text(cr, dx, dy, p[1]) - dx = dx + card.measure(cr, p[1]) + -- advance(), not measure(): the ink width of ' / ' omits its spaces. + dx = dx + card.advance(cr, p[1]) end end |
