0

I'm currently drawing a string to a canvas with a specified font. I would, however, like to scale this font based on the window size.

Given a target string, how do I find the point size of a particular font face so that printing the target string will be either h units tall, or w units wide? Is there a linear relationship between point size and font dimensions?

I can think of very smelly ways to determine a relative point size (pick an arbitrary size and shrink / grow until the dimensions are within some epsilon of the target), but would rather do it more cleanly.

I want to do this with fonts-only, if possible, and not resort to affine transformations.

efritz
  • 5,125
  • 4
  • 24
  • 33
  • Does this answer your question? [Need a way to scale a font to fit a rectangle](https://stackoverflow.com/questions/876234/need-a-way-to-scale-a-font-to-fit-a-rectangle) – Dave Jarvis Jul 18 '20 at 22:31

1 Answers1

3

For the best metrics, I prefer TextLayout, illustrated here, but deriveFont(), suggested by @StanislavL among the answers here, is surprisingly agile and not at all malodorous.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045