I found the code here inadequate for my needs.
I needed to check an unknown input line to determine which font to use, therefore, I needed to check each character. (see below)
By the way, the font.canDisplayUpTo method will not work. It can approve a font that can only display some characters.
So just use this code below.
Font[] allFonts;
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
allFonts = env.getAllFonts();
Font targetFont = null;
for ( int i = 0; i < allFonts.length; i++ )
{
Font font = allFonts[ i ];
boolean canDisplayAll = true;
for (char c : text.toCharArray())
{
if (!font.canDisplay(c))
{
canDisplayAll = false;
break;
}
}
if (canDisplayAll)
{
logger.debug("font can display the text " + font.getName());
targetFont = font;
break;
}
else
{
logger.debug("cant display " + font.getName());
}
}
gregm source
share