aboutsummaryrefslogtreecommitdiffstats
path: root/lib/card.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/card.lua')
-rw-r--r--lib/card.lua28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/card.lua b/lib/card.lua
index 185703c..c83e6bc 100644
--- a/lib/card.lua
+++ b/lib/card.lua
@@ -205,6 +205,34 @@ function M.ring(cr, cx, cy, r, frac, colour, colors, width)
cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT)
end
+-- The largest base size S at which every row of pieces still fits max_w,
+-- measured at a reference size and scaled.
+--
+-- `groups` is a list of rows; each row is a list of { text, font, factor }
+-- pieces drawn at factor*S on that row. A widget uses this to derive one
+-- fluid size for the whole card: the cell's height decides how large the
+-- content grows, and this pulls S back down only where a row would overrun
+-- the width, so type fills the card instead of leaving empty space and never
+-- collides. Returns math.huge when there is nothing to measure, leaving the
+-- caller's height budget in charge.
+function M.fit_unit(cr, max_w, groups, ref)
+ if not (max_w and max_w > 0) then return math.huge end
+ ref = ref or 100
+ local S = math.huge
+ for _, row in ipairs(groups) do
+ local w = 0
+ for _, piece in ipairs(row) do
+ M.font(cr, piece[2], ref * piece[3], false)
+ w = w + (M.measure(cr, piece[1]))
+ end
+ if w > 0 then
+ local fit = ref * max_w / w
+ if fit < S then S = fit end
+ end
+ end
+ return S
+end
+
-- Bytes to a short human string: 1181116006 -> '1.1G'.
-- The card formats its own numbers because the samplers pass raw bytes.
function M.human(bytes)