Suppose I have a fixed sized frame, say 3 y 4 cm. And I have a variable-length text.

Would it be possible to write a macro that scaled the font and line height such that the text will fit in the frame?

What I have now is very basic:
Fixed width
Typeset (METAPOST textext())
If too high
METAPOST: scale down so it fits in frame

But the scaling wil scale both height and width so it doesn’t only go to the correct height, it also gets too narrow. Just scaling in the vertical direction squashes the text/font.

What would be great is some way to have the font size and lineheight result from TeX trying to fit it inside a predetermined size.

Doesn’t need to be perfect, a good approximation will do. I’ve been thinking about using the surface size difference between desired box size and the result of textext() as an approach to guess a right fontsize and line height.

What could make it possible for me too is a way to do a typeset command from lua that gives me the box sizes of typesetting without actually typesetting it to the true output of the job. Something like
Is that doable? Or is there a better way?
 

G