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.
Java Font Support
| Font Support Under Java |
| Font Type |
Extension |
Java 1.6 Windows |
Java 1.6 Linux |
Java 1.6 Fedora |
Old Java Windows |
Notes |
OpenType
(TrueType internally) |
otf |
|
|
|
|
High-end fonts for Windows. |
OpenType
(PostScript Adobe CCF internally) |
otf |
|
|
|
|
High end PostScript fonts. You can detect these by the file signature { 0x4F,
0x54, 0x54, 0x4F
} — the string "OTTO", at the head
of the file. |
| TrueType |
ttf |
|
|
|
|
Most common font for Windows. |
| PostScript |
pfm/pfb |
|
|
|
|
Older style PS fonts. Supported by PostScript printer hardware. Windows
itself supports PS fonts, at least with Adobe Type Manager, but Java ignores
them. |
| Bitmap |
fon |
|
|
|
|
Used primarily for small font sizes. Come only a small set of point sizes. |
| Vector outline |
fon |
|
|
|
|
These are obsolete. Used by Windows without Java. |
| 8-bit fonts |
any |
|
|
|
|
Java needs 16-bit fonts. It won’t use 8-bit fonts directly. Old or
specialty 8-bit fonts can be used by stitching them together with a Unicode
mapping, a daunting task. |
AWT will only support the five basic logical fonts, unless you paint on a Canvas,
however oddly under Fedora and AWT you can use up to 82 of your installed fonts. If you try to use more,
you get an ArrayIndexOutOfBoundsException. The above information may be incorrect
or may become incorrect at any time. Feel free to try any fonts with Java on any platform. The worst that
could happen is they won’t work.
Java’s Font Class
Sun’s Javadoc on the
Font class : available:
A Font does not have a colour attribute. It is
always painted in the current foreground colour.
If you accidentally reverse the second and third
constructor parameters, your code will compile, but the font display will be
microscopic or invisible.
Font Design Is An Art
Circa 1980, I decided to add the French accented letters we use in Canada to the
programmable font of an Okidata dot matrix printer. In isolation, my accented
letters looked beautiful, but when melded into the rest of the alphabet they
looked like a ransom note. I have great respect for the artists who design the
world’s classic fonts. Each shape has to have an artistic consistency that
says it belongs with the others. At the same time the glyphs must be easily
distinguishable from each other. The design has to look good even when rendered
crudely on a CRT in small font sizes. Some people like to look at Flemish
paintings. I love to look at beautiful fonts. When you understand how much work
goes into creating a great one, you would be less likely to pirate it. Peruse
the “art gallery” at Adobe
or BitStream MyFonts.
What Fonts Are Available Windows
To see what fonts are available to Windows, click Start ⇒
Control Panel ⇒ Appearance and Personalization ⇒ Fonts .
These fonts will work in Windows word
processors and many Windows programs including browsers. Not all of the fonts
will work in Java, or Java Applets however. To find
out the name of the corresponding file in C:\Windows, right
click Properties.
Installing a Font In Windows
To install a font in Windows Vista, usually TTF or OpenType-TTF:
- Click Start
- Control Panel
- Appearance and Personalisation
- Install or Remove a Font
- Click File. If you don’t see File, click Alt.
- Install New Font
- In the Add Fonts dialog box, under Drives, click the drive where the font that
you want to install is located
- Under Folders, double-click the folder containing the fonts that you want to add
- Under List of fonts, click the font that you want to add
- click Install
What Fonts Are Available On My Machine under Java?
- In Windows, Java ignores your vector, bitmap and PostScript fonts. It can only
use the TrueType and OpenType fonts. Further it will only use TrueType and
OpenType fonts with Unicode encodings. Older fonts or fonts with only a few
specialty characters will often not work because they come only with 8-bit
encodings. You might pester the font authors to add the Unicode support. Modern
fonts often come with several encodings. Java just uses the Unicode encoding.
- In AWT, you are limited to the five Java logical
fonts, unless you use Canvas.drawString.
- Use the FontShower Amanuensis
to show you what fonts are available, and what they look like in various styles,
sizes and colours, in both Swing and AWT.
- Browser Fonts available in HTML
and CSS.
- Check in Control Panel ⇒ fonts to see what fonts
your Operating System has installed and what the fonts are called. This list is
not necessarily identical to the list Java supports. In Vista Click
Start ⇒ Control Panel ⇒ Appearance and Personalisation ⇒ Fonts
to see what fonts are installed. The fonts themselves live in C:\windows\fonts.
- In JDK 1.4+ and perhaps earlier you also have: Lucida Bright,
Lucida Sans and Lucida Sans
Typewriter.
- The Java logical fonts work best for rendering the more exotic characters. In
addition these native fonts are fairly good: MS PGothic
and MSUIgothic.
- Font name vs family vs fontname.
- If you are using Vista, this list will be available:
- Defining a Bold Italic Font:
Font f = new Font( "SansSerif", Font.BOLD + Font.ITALIC, 12 );
- As a programmer you can find out what fonts are available. JDK 1.2+ you can
discover the supported fonts this way:
- In JDK 1.1, (Obsolete) use
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.
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. It does not have to be installed in the OS. 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.
If you wish to use PostScript Type 1 multiple master fonts with W2K/XP/W2K3/Vista, you need
to install Adobe Type Manager 4.1 or later. Do not install ATM 4.0 or earlier on
W2K/XP/W2K3/Vista.
W2K/XP/W2K3/Vista
have built-in support for ordinary PostScript Type 1 fonts, and OpenType, though
Java ignores the PostScript fonts.
Adding Fonts to JDK 1.2 (obsolete)
In JDK 1.2+ you can access fonts either by Java logical or native-physical name.
To make a new font accessible to JDK 1.2 you have three choices:
- 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 codepoint 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.
- 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.
- 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 Java logical Unicode font.
Units of Measure, Points and Pixels
Fonts are nominally measured in points, 1/72 of an inch tall. If Java truly did
this, the number of pixels tall a given font was would depend both on the screen
resolution and the size of the user’s monitor. Fonts would grow and shrink
all out of proportion to the surrounding graphical elements based on pixels. To
get around this problem, Java declares that one point equals one pixel. If you
ask for a 10 point font, you are actually getting one nominally 10 pixels high.
There will still be some characters taller and some shorter than 10 pixels.
Font Naming
Fonts have three names:
- The retail name used to sell the font, e.g. Century
Schoolbook.
- The family name, usually abbreviated, e.g. CentSchbook
BT. This is typically what you use in Java or in your CSS style sheet.
You must get the spelling exactly right including spaces.
- The precise font name, including adoments
(suffixes to describe weight, style, stretch etc.), e.g. CentSchbook
BT Roman, CentSchbook BT Italic, CentSchbook
BT Bold, CentSchbook BT Bold Italic, CentSchbook
BdCn BT Bold (bold condensed) or CentSchbook Mono BT
(monospaced).
Acquiring Fonts
When buying fonts be aware that usually you buy the bold, italic, light,
condensed etc. versions separately. They behave more or less as one font once
you install them. Sometimes you pay extra for the full character set. Check to
see if your font has upper and lower case, accented letters, the €,
ligatures, ornaments, small caps… Check carefully exactly what is
included in the bundle you buy.
Unfortunately, when you buy a font that usually gives you the right to use it on
only your computer, but not to let people download it to view your webpages, or
to include it in your programs. Usually you would not even be permitted to
include them in PDF documents. BitStream discontinued its scheme of downloadable
fonts called webfonts that let you include the font
in your web pages. CSS provides a scheme to include your fonts in your web pages,
but they have to be free fonts or fonts you have licenced to distribute.
- Ask Google to help you find free
fonts.
- Nicksfonts.com has a
collection of freeware fonts.
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 iI!|l o0O8¤[]()
qg Ww `'‘’“” ()[]{} ;,. 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. The generated font sample
at MyFonts.com does not display in the box in the upper right where you would
expect. Look about half way down the page.
-

- I bought the Bitstream 500-font CD collection which contains the following fonts
Unfortunately it is no longer sold. However there is a 200-font Cambridge
collection for
. There is also a 1450-font Odyssey
CD for
. It comes in Windows/Mac TTF/PostScript variants. It is licenced for 20 users
or workstations. Here are some of the fonts it contains:
Unfortunately you can’t use these fonts on
your website, other that my creating *.png files. You
can’t give them to your viewers to download to view your site.
- For Esperanto fonts, I recommend downloading
this small set of public domain Esperanto fonts from the Esperanto
Association of Scotland (Esperanto-Asocio Skotlando).
It includes:
| Ariel SudEuro (note, not Arial SudEuro) |
Ariena SudEuro |
Courier SudEuro |
Times New Roman SudEuro |
| ArielSudEuro Black |
Bookman SudEuro |
Garamond SudEuro |
Verena SudEuro |
Personal Picks
Here are some of my favourite fonts:
| Tiresias PCfont Z |
The letters are unusually distinctive so there is no confusing them. It is
very clean simple proportional font. It was designed for people with poor
eyesight so gives particularly smooth reading for people with normal eyesight.
Most fonts are poorly designed so it is hard to tell the characters iI!|l
o0O8¤[]() qg Ww `'‘’“” ()[]{} ;,. apart. Tiresias
is a special font family 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. I asked the designers to create a
monospace variant but they declined. |
| DPCustomMono2 |
A monospaced font designed expressly for proofreading. It makes it easy to
tell comma/period and colon/semicolon apart. You need anti-aliasing turned on
for it to look half-way decent. |
| Bookman
Old Style |
This has an old-fashioned, relaxed, hot-oatmeal for breakfast look. |
| Palatino |
This an elegant font, something like the font equivalent of Paul Revere
silver designs emphasising utility and simplicity. |
| Warnock
Pro Opticals |
These are the Porsches of fonts. I doubt I will ever own them. |
| Frutiger |
Microsoft ripped this elegant design off by changing it slightly and calling
it Segoe and reserving it as their corporate font. |
| OCR-B |
A monospaced font designed originally for optical character recognition. In
making the characters distinct enough for computers, they also made them
distinct for rapid human reading. There are no decent free ones around. |
|
| Keystrokes |
This lets you explain the keystrokes you need to get do something on your PC.
The problem is you are not allowed to use the font on your website, which
defeats the purpose of it. |
| Aquila
Regular |
Just a touch of eccentricity to make it interesting. |
| Cash
EF |
This a modern-looking monospace font. Further, even in the tiniest font
sizes it is eminently readable. It’s big problem is the zero and capital O
are identical making this font useless for programmers. I have written the Eslner+Flake
type foundry who created it asking them to create a variant suitable for programmers.
They ignored me. |
| Base
Nine and Twelve |
This font is particularly good at small point sizes. It is an open design so
the loops don’t clog. It offers small caps. It is somewhat heavy looking. |
| Segoe Print |
looks like hand printing. Comes bundled with Vista. |
Font Licensing
I have been trying to make sense of the legalese on the font sites 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 1 to 20 people, depending on the agreement for the particular
font, at your site to compose new messages or documents using the font.
As I understand it, you typically can do the following things without needing an
extra multi-user licence above and beyond buying the font:
- Use the font on your own computer
- Use the font on your own LAN for 1 to 20 computers at one site.
- Distribute a hard copy document using that font.
- Distribute a *.gif or Button containing some text in
that font.
- Use the fonts in portable documents. You may send a TrueDoc *.pfr
(Portable Font Resource) with the understanding the *.pfr
cannot be used for any other document.
- Embed fonts within PostScript *.ps and *.eps
files, Acrobat *.pdf files, and *.evy
files for distribution, viewing, and imaging to other parties, with the
understanding others may not extract the fonts to be used with any other
document.
As I understand it, you need an extra multi-user license to do the following
things:
- Distribute an electronic document using the font, e.g. Word *.doc,
Acrobat *.pdf, *.html etc.
with the font packaged separately.
- Attach or embed the font in an email.
- Use the font on your website.
- Send a document using that font to a typesetter who does not own his own copy of
the font. This is a an extra restriction on the general rule.
- Using a font in an Applet on your website.
- Embedding the font in a program you distribute.
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
⇒ Settings ⇒ Control Panel ⇒ Display ⇒ Settings ⇒
Advanced ⇒ General ⇒ Font Size. The catch is, if you increase
fonts to 120% bigger, some program such as ASO and PadCreator
will garble their layouts. You can of course lower the screen resolution to get
bigger fonts, but that impairs your ability to look at images.
To control the font and size of any individual item such as tooltip, click 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 with anti-aliasing.
Most fonts don’t support many of the national currency symbols. Tahoma is
better than most.
Rendering
There are three ways to render fonts in Java.
- AWT: limited to the 5 Java logical fonts.
Anti-aliasing is controlled by the OS. Easy to
program. Rendering is handled by the OS which renders the heavyweight peers
associated with each Component. For an example of
such rendering see FontShowerAWT.
- AWT Canvas: can use all the OS fonts. Can choose programmatically whether you
want Anti-aliasing. This is difficult to program
since you do all your rendering at the low-level drawString
level. For an example of such rendering see
com.mindprod.fontshowerawt. AntiAliastedFontedTextArea
or FontedTextArea.
- Swing: can use all the OS fonts. Easy to program. Rendering the fonts is managed
by the Swing runtime on lightweight JComponents. Anti-aliasing
is controlled by the OS. For an example of such rendering see FontShower
for Swing.
On my Vista machine, configured in the Control Panel to use ClearType anti-aliasing
to smooth font edges, under both AWT and Swing I see fonts fully anti-aliased.
The only time I see degraded fonts are when I view fonts rendered on an AWT Canvas
without anti-alias. Ditto for XP. With an LCD monitor, you want ClearType
subpixel anti-aliasing. To turn it on click Start ⇒
Control Panel ⇒ Appearance and Personalization ⇒ Personalization ⇒
Windows color and appearance ⇒ Open classic colour and appearance ⇒
Effects ⇒ ClearType.
Hinting
A separate matter from anti-aliasing is hinting.
PostScript and OpenType fonts include hints on how to render small font sizes.
You could think of it in principle as raster versions of the fonts for tiny font
sizes. In AWT, the OS renders the fonts using its native facilities for taking
hints. In Swing, Java renders the fonts, for all practical purposes ignoring the
hints. The result is Swing fonts can look ratty at small point sizes, but quite
decent at larger ones.
ClearType
Antialiasing creates the illusion of crisp smooth edges on rendered type
ironically, by fuzzy the edge, by filling in a jaggy pixel with a colour
intermediate between the colours on either side of the boundary. This anti-aliasing
can be done even to a sub-pixel level with the red, green and blue sub pixels on
an LCD screen. Fonts that can do this ultra-fine anti-aliasing are called
ClearType. To turn on the rendering which makes text crisper by activating the
extra rendering effort in Windows:
Here is how to activate ClearType font in Windows XP:
- Click Start
- Control Panel
- Appearance and Themes
- Display
- Appearance
- Effects
- Click Use the following method to smooth edges of screen fonts.
- Select ClearType in the list.
Here is how to activate ClearType font in Vista:
- Start
- Control Panel
- System and Maintenance
- Performance Information and Tools
- Adjust Visual Effects (on left)
- smooth edges of screen fonts
By default anti-aliasing is on. For some fonts, Vista even supports subpixel
anti-aliasing called Clear Type.
You can fine
tune the clear type by downloading
this powertuner from Microsoft.
Corrupt Fonts
Sometimes fonts will come out too tiny to see. Likely it means you have reversed
the last two parameters when you created the font, a error the compiler cannot
detect since Font does not use enums,
just enumerated int constants.
component.setFont( new Font( "Dialog", 12, Font.BOLD ));
component.setFont( new Font( "Dialog", Font.BOLD, 12 ));
On Windows, sometimes strange rendering problems are caused by a corrupt font
cache. You can simply delete the cache, sacrifice a small reptile and reboot.
Here’s how:
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.
Bundling Fonts In a Jar
If you want to include custom fonts in your application, you either have to get
the customer to install them or employ the following trick to use them directly
from a jar. This only works in Swing since AWT components are limited to the
Java logical fonts.
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:
- They consume large amounts of RAM.
- They clutter your menus.
- They cause Windows to crash more frequently.
You can use a tool like Adobe Type Manager to rapidly and globally install/uninstall
entire constellations of fonts.
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. Unfortunately, it has a rather lax definition of “can display”.
It will often return true and just display a blob
or empty rectangle.
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.
Technically, canDisplay is supposed to return true
for any code point in the range handled by the font, which is not very useful
information.
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.
Changing the 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.
Sun’s Javadoc on the
LookAndFeel class : available:
Sun’s Javadoc on the
UIDefaults class : available:
The approach is to write your own Look & Feel that extends some other one,
and just overrides a few font-defining methods or colour-defining methods. See
this sample
code for a writing a derived LAF.
Here are some resources
if you want to attempt multi-lingual fonts. Prior to JDK 1.4, I only managed to
get Unicode fonts to display properly with NT and Win2K and Internet Explorer.
Windows 98 displays accented letters above 255 without the accents.
Font Gotchas
- The gotcha most likely to bite you in that, in AWT, you are limited to the Java logical
fonts: Dialog, DialogInput,
Monospaced, Serif and SansSerif
unless you take special measures, described later. Peered AWT components, such
as Label and TextField,
can only use Java logical fonts. This means with AWT, you are pretty will stuck
with the basic set. You can get the others with drawString
and a Canvas. If you try to use some other font, AWT
will just quietly substitute one of the Java logical fonts at the last second.
- If you use drawString in a paint
method, you specify the baseline left corner of where you want the text to go,
not the top left as for most other painting. In most other Font uses, the
component deals with such details for you automatically.
- If you include an exotic character like '\u060b', the
Afghani currency sign, drawString, or Component
that uses it, will render your string from right to left, displaying the
characters in reverse order.
- Peered AWT components, such as Label and TextField
cannot display the full font complement. For example, using the Dialog
Java Java logical font, peered components can display 6 of 27 international
currency symbols. Using Canvas, you can display 18
of 27. The reason is Swing Components and Canvas/drawString
have access to Graphics2D where AWT peer Components
are limited by whatever the host GUI OS provides via the peer rendering.
- new Label.getFont()
will just give you null. A Component does not
acquire a default Font until it has a parent.
- \u0e3f in a JTextArea
inhibits antialiasing, ditto \ufdfc. \u0e3f
is a Thai Baht currency sign like a capital B with a line through it. \ufdfc
is the Yemeni Rial currency sign. It looks like Arabic script. Using one of
these characters turns off anti-aliasing for the entire JTextArea.
They seem to have no such effect in AWT with drawString
or with TextArea. This strange behaviour has been
observed both in Win2K and Linux. See antialiasing
for details.
- Microscopic fonts, too tiny to see, have two
common causes.
Books
 |
recommend book⇒Big Book of 5000 Fonts: (And Where to Get Them) |
| | hardcover |
|---|
| ISBN10: | 0-8230-0489-9 |
|---|
| ISBN13: | 978-0-8230-0489-8 |
|---|
| publisher: | Watson-Guptill Publications |
| published: | 2002-02 |
| by: | David Carter |
| also includes websites with free downloadable fonts. |
|
Learning More
Sun’s Javadoc on the
Font class : available:
Sun’s Javadoc on the
FontMetrics class : available:
Sun’s JDK Technote Guide on
Font Configuration Files : available: