Skip to main content

How to check if font is monospaced text font

4 replies [Last post]
dvelst
Offline
Joined: 2007-10-05

Hi,

I'm faced with the following challenge: for all fonts I request from the platform I work on I want to check for each font separately if it's a monospaced text font or not. The platform I work on is SuSe Linux 10.1. I start with following code:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = ge.getAllFonts();

So now for each font in the variable fonts I want to check if it's a monospaced text font. How should I do this ?

Thanks in advance and best regards, Diederick van Elst

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
swv
Offline
Joined: 2007-05-28

Monospaced is one of the possible values of the family property (literally: attribute) of the Font. So you need to first get the font's attributes. This is a Map. Once you have the map, use the key "FAMILY". One possible value that may be returned for that key is "MONOSPACED". Done.

dvelst
Offline
Joined: 2007-10-05

Thanks for your reply. I used following code:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Vector fontListMonospaced = new java.util.Vector();
Font[] fonts = ge.getAllFonts();
Object key;
Object value;
for (int i=0; i < fonts.length; i++) {
java.util.Map attributes = fonts[i].getAttributes();
if (attributes.containsValue("Monospaced")) {
fontListMonospaced.addElement(fonts[i].getFontName());
}

for (java.util.Iterator it=attributes.entrySet().iterator(); it.hasNext(); ) {
java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
key = entry.getKey();
value = entry.getValue();
if (key instanceof String) {
if (((String)key).equals("family")) {
if (((String)value).equals("Monospaced")) {
fontListMonospaced.addElement(fonts[i].getFontName());
}
}
}
}
}

In this way you can indeed check for the family attribute and retrieve family names as key values. However, perhaps I should have been somewhat clearer in asking my question... what I want is that not only Monospaced.italic or Monospaced.bold are recognized as monospaced text fonts but by monospaced text fonts I also define Courier, Courier New, Monaco, Lucinda, etc. At first I expected to find the monospaced text fonts by calling getName() on all Fonts I get with ge.getAllFonts() because getName() should get me the logical name of which one of the 5 possible values is Monospaced. Then I should be able to check for Monospaced. When I tried calling getName() I got the font names instead. Is this perhaps Java SE version dependent (I use version "1.6.0_02") ? So, the problem is not solved yet....

fnl
Offline
Joined: 2006-08-11

I know this is a very old thread. But I have the same problem and tried the solution propose.

I just want to tell other people that reads this thread that the proposed solution does not work as the "Monospaced" attribute does not exists in the map over attributes. :-(

swv
Offline
Joined: 2007-05-28

LOL you got the attributes map, were you able to collect the monospacers amongst them? Future readers of this thread are dying to know!