Java Glossary : Font

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : F words : Font.

Font
A font is a triple e.g. SansSerif / Bold Italic / 11 point -- the combination of type Family, style and size encapsulated into a Font object. See the file font.properties. Inside it are the definitions that map the virtual Java Unicode fonts onto the 8-bit native fonts. Sun may have had to stitch together several 8-bit fonts to cover different regions of the Unicode character set in a Java virtual font. This allows the magic ability to simulate 16-bit Unicode fonts that can display more than 256 different characters when you only have 8-bit native fonts available. In JDK 1.1 you can't use a native font in Java unless it has entries in the font.properties file to hook it up to some Java virtual font name. In subsequent JDK versions, you can also use any native font installed on the target system. Fontlab Composer is a tool for stitching fonts together.

Adding Fonts to JDK 1.2+

In JDK 1.2+ you can access fonts either by logical-Java or native-physical name. To make a new font accessible to JDK 1.2 you have three choices:

  1. Install the font on your host by following the host's directions for installing fonts. On Windows, for example, you do this via control panel | Fonts. The font will be available both to your native Windows apps and your Java apps. Most TrueType fonts have a Unicode cmap index. You'll notice that only a few do not. For example, if you look in your font.properties files, only Wingdings and Symbol fonts have the NEED_CONVERTED tag on them, which indicates that they require a conversion from a Unicode code point to a different indexing scheme within the font. If they have a native cmap index, Java is able to use them without special entries in the font.properties file.
  2. Copy the font into your jre/lib/fonts subdirectory. The font will be available only to Java. Notice that there are a set of Lucida fonts in there already: Lucida Bright, Lucida Sans and Lucida Sans Typewriter.
  3. Install the font using the font.properties file as you would in JDK 1.1. You would need to use this technique if you needed to stitch several 8-bit fonts together to form one big logical Unicode font.

Adding Fonts to JDK 1.3+

Font f = Font.createFont( Font.TRUETYPE_FONT, inputStream );
let's you dynamically create a 1-point plain font from a TrueType font file. You can then deriveFont to create the fonts in the required sizes and styles. These fonts don't work well at small point sizes because they don't implement hinting. You could then fish fonts from the net, from the local hard disk or from the jar, much the way you can fetch images.

This does not permanently install the font. There is no platform-independent way to do that. In Windows 2000, you can copy the TrueType font to C:\WINNT\FONTS.

You would have to read your font licence agreement carefully to see if it permits you to use the font in this way.

Conserving Fonts

Font f = new Font( "Monospaced", Font.PLAIN, 12 )
is a very time consuming operation. Save your Font objects and reuse them rather than creating new ones. See FontSaver to reduce RAM usage by Java fonts.

Having too many fonts installed, (not the same thing as having too many duplicate Font objects), has several drawbacks:

You can use a tool like Adobe Type Manager to rapidly and globally install/uninstall entire constellations of fonts.

What Fonts Are Available?

Will this character Display?

boolean Font.canDisplay( char );
will let you know if there is a glyph matching a given Unicode character in a given font. It is up to you to find a font that can display the character you need. Unfortunately, fonts often lie about what glyphs they can display. For example if you ask them if they can display a euro, they say yes, then display a blob, which in their distorted view of things counts as displaying the character.

Happily, if you specify a font not installed on the target machine, Java simply reverts to the default font. There is no mechanism similar to CSS or HTML where you can specify a list of fonts in preference order. You have to code that yourself and feed setFont a specific Font.

Ugly Fonts -- Antialiasing

For some unknown reason, Sun decided to make fonts ugly by default, with jagged edges. To anti-alias them, you have to go through a gambit like this:

// antialiasing fonts to get rid of the jaggies
Graphics g =...;
Graphics2D g2d = null;
if ( g instanceof Graphics2D )
   {
   g2d = (Graphics2D)g;
   g2d.addRenderingHints( new RenderingHints( RenderingHints.KEY_ANTIALIASING ,
                                              RenderingHints.VALUE_ANTIALIAS_ON ));
   }

I don't know if this is sufficient to make the rendering engine pay attention to the font hints for small point sizes.

Default Fonts

To change the default fonts inside the AWT use code like this:

Font defaultFont = new Font( "Dialog", Font.PLAIN, 12 );
UIManager.put( "Button.font", new FontUIResource ( defaultFont ) );

You can make similar default font changes to these elements:

Button.font List.font PasswordField.font TableHeader.font ToggleButton.font
Checkbox.font Menu.font PopupMenu.font Text.font ToolBar.font
ColorChooser.font MenuBar.font ProgressBar.font TextArea.font ToolTip.font
ComboBox.font MenuItem.font RadioButton.font TextField.font Tree.font
EditorPane.font OptionPane.font ScrollPane.font TextPane.font  
Label.font Panel.font Table.font TitledBorder.font  
For Swing there is a even more sweeping system of defaults called LAF Look and Feel. see javax.swing.LookAndFeel and javax.swing.UIDefaults.

Here are some resources if you want to attempt multi-lingual fonts. I have only got Unicode fonts to display properly with NT and Win2K and Internet Explorer. Windows 98 else displays accented letters above 255 without the accents.

Acquiring Fonts

Unfortunately, when you buy a font that usually gives you the right to use it on your computer, but not to let people download it to view your webpages, or to include it in your programs. Usually you would be permitted to include them in PDF documents. Bitstream has a scheme of downloadable fonts called webfonts that you can include in your web pages.

myfonts.com lets you test drive fonts before you buy by typing in sample text to see how it will look. This also lets you check if Il1i|, wW, O0o8 or gq look too much alike. It also lets you check for kerning errors in the font design e.g. do WA nestle properly. Just seeing letters in isolation does not give you a sense of what the font looks like in use.

Most fonts are poorly designed so it is hard to tell the characters iI!|l and 0O8 apart. Tiresias is a special font designed so that even the visually impaired can distinguish them. It looks like this:

tiresias font sample

If you already have it installed, all the type on this page will look similar. It is the default font for my website.

Nicksfonts.com has a collection of freeware fonts.

Readable Fonts

In Windows, you can increase or decrease the size of fonts universally for all applications, dialog boxes, menus, icon titles etc. click Start | Start | Settings Control Panel | Display | Settings | Advanced | General | Font Size. To control the font and size of any individual item such as tooltip, click Start | Start | Settings Control Panel | Display | Appearance | Item . You can then select Active Title Bar, Inactive Title Bar, Palette Title, Message Box, Menu, Selected Item or Icon and set the font and size.

Neither of these techniques will change the font sizes used by applications. For that you need to look to custom ways in each application to customise the fonts and sizes.

Java fonts look terrible because by default they do no antialiasing and ignore the hints. You can improve them somewhat with graphics2D.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); as described earlier.

Most fonts don't support many of the national currency symbols. Tahoma is better than most.

Corrupt Fonts

On Windows, sometimes strange rendering problems are caused by a corrupt font cache. You can simply delete it, sacrifice a small reptile and reboot.

del C:\WINNT\System32\fntcache.dat

Other times the problem is a defective font. Before you pull your hair out, check to see if your problems go away if you try one of the standard fonts instead. Defective font problems can manifest in bizarre ways -- e.g. cursor offset from where it should be, duplicate rendering and misplaced text.

Font Licensing

I have been trying to make sense of the legalese on the font sites such as BitStream, and talking with company representatives. I think the basic idea is, you can allow an many people as you please to view your document using the font, but you can't allow more than 20 people at your site to compose new messages or documents using the font without a multi-user licence.

As I understand it, you typically can do the following things without needing an extra multi-user licence above and beyond buying the font:

As I understand it, you need an extra multi-user license to do the following things:

Learning More

Ask Google to help you find free fonts.
Adobe Type Manager ¤ Canvas ¤ dynamic fonts ¤ encoding ¤ font finder student project ¤ Font Shower Amanuensis ¤ Font Shower Student Project ¤ FontMetrics ¤ FontSaver to reduce RAM usage by Java fonts ¤ Hershey Fonts (platform independent and rotatable) ¤ HTML Cheat Sheet: for a list of fonts supported in your browser ¤ Java Foundation Classes in a Nutshell on Fonts ¤ MICR ¤ OpenType ¤ point ¤ PostScript ¤ Suitcase ¤ Sun's essay on how to add new fonts to JDK 1.2. To quickly switch between sets of fonts ¤ Tiresias ¤ TrueType ¤ Webfont


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 6050.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/font.html J:\mindprod\jgloss\font.html