How can I find all the supported weights of a Font in Java?
- by William
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."