You could use the getWidths() method of the FontMetrics class. According to the JavaDoc:
Gets the advance widths of the first 256 characters in the Font. The advance is the distance from the leftmost point to the rightmost point on the character's baseline. Note that the advance of a String is not necessarily the sum of the advances of its characters.
You could use the charWidth(char)
method of the FontMetrics class. For example:
Set<String> monospaceFontFamilyNames = new HashSet<String>();
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontFamilyNames = graphicsEnvironment.getAvailableFontFamilyNames();
BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = bufferedImage.createGraphics();
for (String fontFamilyName : fontFamilyNames) {
boolean isMonospaced = true;
int fontStyle = Font.PLAIN;
int fontSize = 12;
Font font = new Font(fontFamilyName, fontStyle, fontSize);
FontMetrics fontMetrics = graphics.getFontMetrics(font);
int firstCharacterWidth = 0;
boolean hasFirstCharacterWidth = false;
for (int codePoint = 0; codePoint < 128; codePoint++) {
if (Character.isValidCodePoint(codePoint) && (Character.isLetter(codePoint) || Character.isDigit(codePoint))) {
char character = (char) codePoint;
int characterWidth = fontMetrics.charWidth(character);
if (hasFirstCharacterWidth) {
if (characterWidth != firstCharacterWidth) {
isMonospaced = false;
break;
}
} else {
firstCharacterWidth = characterWidth;
hasFirstCharacterWidth = true;
}
}
}
if (isMonospaced) {
monospaceFontFamilyNames.add(fontFamilyName);
}
}
graphics.dispose();