diff options
Diffstat (limited to 'lib/card.lua')
| -rw-r--r-- | lib/card.lua | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/lib/card.lua b/lib/card.lua index c83e6bc..f25c393 100644 --- a/lib/card.lua +++ b/lib/card.lua @@ -188,6 +188,7 @@ function M.ring(cr, cx, cy, r, frac, colour, colors, width) -- and cairo_arc() joins to it with a straight line, so without this every -- ring after a label is drawn with a stray chord to the text baseline. cairo_new_path(cr) + local prev_width = cairo_get_line_width(cr) cairo_set_line_width(cr, width) cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND) @@ -200,9 +201,11 @@ function M.ring(cr, cx, cy, r, frac, colour, colors, width) cairo_arc(cr, cx, cy, r, -math.pi / 2, -math.pi / 2 + math.pi * 2 * frac) cairo_stroke(cr) end - -- Leave the cap as it was found: a later stroke inheriting ROUND would get - -- visibly rounded ends on the card's hairlines. + -- Leave the line state as it was found. A later stroke inheriting ROUND gets + -- visibly rounded ends on the card's hairlines, and one inheriting this + -- width gets a hairline several pixels thick. cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT) + cairo_set_line_width(cr, prev_width) end -- The largest base size S at which every row of pieces still fits max_w, @@ -246,4 +249,25 @@ function M.human(bytes) return string.format('%.1f%s', n, units[i]) end +-- Shorten a string to `limit` characters, appending an ellipsis when it cuts. +-- +-- By character, never by byte: Lua's string.sub counts bytes, so slicing a +-- UTF-8 name mid-sequence emits an invalid byte, which Cairo draws as a +-- replacement box. A name long enough to need shortening is exactly the kind +-- likely to carry an accent. +-- +-- This counts codepoints, not rendered width, so it does not account for a +-- double-width CJK glyph. That is the right trade here: the strings it cuts +-- are cache directory names and hardware model strings. Measure with +-- M.measure when true rendered width matters. +function M.truncate(s, limit) + s = tostring(s or '') + limit = tonumber(limit) or 0 + if limit <= 0 then return '' end + if utf8.len(s) == nil then return s:sub(1, limit) end -- not valid UTF-8: byte-slice + if utf8.len(s) <= limit then return s end + local cut = utf8.offset(s, limit) -- byte index of the limit'th character + return s:sub(1, cut - 1) .. '\u{2026}' +end + return M |
