I've been thinking about this for my UI library. It's an awkward problem that I've not found a perfect solution for but this might work for your use case.The following function will determine a font height so that it will fit in a box defined by w and h. It will start with the font at max size, if the text goes beyond the defined width, it will reduce the font size to fit.
It loads a font every time you call it so I would call it only when text changes.
function FontFit(fontpath, text, w, h)
local font = Font:Load(fontpath, h)
textwidth = font:GetTextWidth(text)
font:Release()
if textwidth > w then
return h * (w / textwidth)
else
return h
end
end
An example of loading a font that will fit some_text that will fit in an area of 100x30
local FontHeight = FontFit("Fonts/arial.ttf", some_text, 100, 30)
local font = Font:Load("Fonts/arial.ttf", FontHeight)