Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Getting a font with a specific height in pixels

It’s easy to determine the rendered height of a font using FontMetrics, but what about the other way around? How can I obtain a font that will fit into a specific height in pixels?

"Give me Verdana in a size that is 30 pixels high from ascender to descender."

How do I ask Java for this?

like image 405
Tony the Pony Avatar asked Apr 29 '11 08:04

Tony the Pony


People also ask

Can font size be in pixels?

Font size specifications may come in points or pixels where: 1 pixel (px) is usually assumed to be 1/96th of an inch. 1 point (pt) is assumed to be 1/72nd of an inch. Therefore 16px = 12pt.

How do you find the height of a string in Java?

The Java String class length() method finds the length of a string.

How is font height calculated?

A font is often measured in pt (points). Points dictate the height of the lettering. There are approximately 72 (72.272) points in one inch or 2.54 cm. For example, the font size 72 would be about one inch tall, and 36 would be about a half of an inch.

How do you change the font size in a string in Java?

You can't actually change the size of an existing Font object. The best way to achieve a similar effect is to use the deriveFont(size) method to create a new almost identical Font that is a different size. Save this answer.


2 Answers

I know this is a very old question, but someone might still find it:

The font height in Java (and many other places) is given in "typographic points", which are defined as roughly 1/72nd of an inch.

To calculate the points needed for a certain pixel height, you should be able to use the following:

double fontSize= 72.0 * pixelSize / Toolkit.getDefaultToolkit().getScreenResolution();

I haven't tested this extensively yet, but it seems to work for the monitors that I've used. I'll report back if I ever find a case where it doesn't work.

For the standard system fonts I've used this with, this sets the height of a capital letter (i.e. the ascent) to the provided pixel size. If you need to set the ascent+descent to the pixel size, you can correct the value using the FontMetrics:

FontMetrics m= g.getFontMetrics(font); // g is your current Graphics object
double totalSize= fontSize * (m.getAscent() + m.getDescent()) / m.getAscent();

Of course, the actual pixel-height of some specific letters will depend on the letter and the font used, so if you want to make sure that your "H" is some exact number of pixels tall, you might still want to use the trial-and-error methods mentioned in the other answers. Just keep in mind that if you use these methods to get the size for each specific text you want to display (as @Bob suggested), you might end up with a random font-size-mess on your screen where a text like "ace" will have much bigger letters than "Tag". To avoid this, I would pick one specific letter or letter sequence ("T" or "Tg" or something) and fix that one to your pixel height once and then use the font size you get from that everywhere.

like image 141
Markus A. Avatar answered Sep 28 '22 15:09

Markus A.


I don't think there's a "direct" way to find a font by height; only an indirect way... by looping through the sizes, and testing the height of each is <= required height.

If you're doing this once, just loop through them... if you've doing it "on the fly" then do a binary search, it'll be quicker.

like image 45
corlettk Avatar answered Sep 28 '22 14:09

corlettk