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.
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:
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.
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:
// now deprecated String[] files = java.awt.Toolkit.getDefaultToolkit().getFontList();to discover the available fonts. They will include Serif (formerly known as TimesRoman), SansSerif (formerly known as Helvetica) or Monospaced (formerly known as Courier). In JDK 1.2+, the native fonts installed in the OS are also supported. The font names you feed to setFont must exactly match ones on that list. The ZapfDingbats font is deprecated in 1.1.
import java.awt.GraphicsEnvironment; public class ShowFonts { public static void main ( String [] args ) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] names = ge.getAvailableFontFamilyNames(); for ( int i=0; i<names.length; i++ ) { System.out.println( names[i] ); } } }
// font name vs family vs fontname Font f = new Font( "Courier", Font.ITALIC , 18 ); // prints "Courier", what you originally asked for System.out.println( f.getName() ); // prints "monospaced", the family you actually got System.out.println( f.getFamily() ); // prints "monospaced.italic", includes style you got System.out.println( f.getFontName());
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.
// 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.
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 |
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.
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:
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.
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.
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.
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:
home |
Canadian Mind Products | |||
| 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 | |||