How can I find all the supported weights of a Font in Java?

Posted by William on Stack Overflow See other posts from Stack Overflow or by William
Published on 2010-04-18T20:37:33Z Indexed on 2010/04/18 20:43 UTC
Read the original article Hit count: 171

Filed under:
|
|
|
|

How can I find all the available font weights for a given font in Java?

The TextAttribute for font weight lists 11 different weight constants, way more than just Font.PLAIN and Font.BOLD. I'd like to know which ones actually exist for a given font family, so I can make sure I'm only using weights for which a font face exists.

The getAttributes() and getAvailableAttributes() methods in Font will only show me whether or not the Font supports the FONT_WEIGHT attribute, not which weight values it supports.

If I just pass in a particular value to see what I get back, I don't have any guarantee that I'll get what I asked for. The TextAttribute class says "The values for WEIGHT, WIDTH, and POSTURE are interpolated by the system, which can select the 'nearest available' font or use other techniques to approximate the user's request."

© Stack Overflow or respective owner

Related posts about java

Related posts about awt