efritz
efritz

Reputation: 5203

Ballpark Text Dimensions with Java FontMetrics

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.

Upvotes: 0

Views: 104

Answers (1)

trashgod
trashgod

Reputation: 205805

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.

Upvotes: 3

Related Questions