Java font problem

Status
Not open for further replies.

nhung

Seniorius Lurkius
7
This code was written to test font on different system. Lucida is a physical fonts that comes with every JDK. You can find it in jre/lib/font folder. I've tested it on three systems. They all gave me different results. Can anybody tell me why? How can I make font system independent?? (the 3 systems are window, alpha, and sun) Thanks in advance.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Metrics extends JFrame {
public Metrics()
{
super( "Demonstrating FontMetrics" );

setSize( 510, 500 );
show();
}

public void paint( Graphics g )
{
int h = 40;
g.setFont( new Font( "Lucida Sans", Font.PLAIN, 9 ) );
FontMetrics fm = g.getFontMetrics();
g.drawString( "Current font: " + g.getFont(), 10, h );
h += 15;
g.drawString( "Ascent: " + fm.getAscent(), 10, h );
h += 15;
g.drawString( "Descent: " + fm.getDescent(), 10, h );
h += 15;
g.drawString( "Height: " + fm.getHeight(), 10, h );
h += 15;
g.drawString( "Leading: " + fm.getLeading(), 10, h );
h += 15;
g.drawString( "charWidth: " + fm.charWidth('A'), 10, h );
h += 15;
String str = "123456789 123456789 123456789 123456789 123456789 ";
g.drawString( str + "width:" + fm.stringWidth(str), 10, h );
h += 15;
double b = fm.getStringBounds(str, g).getWidth();
g.drawString( "String Bound: " + b, 10, h );
h += 30;

Font font = new Font( "Lucida Bright", Font.PLAIN, 9 );
fm = g.getFontMetrics( font );
g.setFont( font );
g.drawString( "Current font: " + font, 10, h );
h += 15;
g.drawString( "Ascent: " + fm.getAscent(), 10, h );
h += 15;
g.drawString( "Descent: " + fm.getDescent(), 10, h );
h += 15;
g.drawString( "Height: " + fm.getHeight(), 10, h );
h += 15;
g.drawString( "Leading: " + fm.getLeading(), 10, h );
h += 15;
g.drawString( "Width: " + fm.charWidth('A'), 10, h );
h += 15;
g.drawString( str + "width:" + fm.stringWidth(str), 10, h );
h += 15;
b = fm.getStringBounds(str, g).getWidth();
g.drawString( "String Bound: " + b, 10, h );
h += 30;

font = new Font( "Lucida Sans Typewriter", Font.PLAIN, 9 );
fm = g.getFontMetrics( font );
g.setFont( font );
g.drawString( "Current font: " + font, 10, h );
h += 15;
g.drawString( "Ascent: " + fm.getAscent(), 10, h );
h += 15;
g.drawString( "Descent: " + fm.getDescent(), 10, h );
h += 15;
g.drawString( "Height: " + fm.getHeight(), 10, h );
h += 15;
g.drawString( "Leading: " + fm.getLeading(), 10, h );
h += 15;
g.drawString( "Width: " + fm.charWidth('A'), 10, h );
h += 15;
g.drawString( str + "width:" + fm.stringWidth(str), 10, h );
h += 15;
b = fm.getStringBounds(str, g).getWidth();
g.drawString( "String Bound: " + b, 10, h );
}

public static void main( String args[] )
{
Metrics app = new Metrics();

app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
 

nhung

Seniorius Lurkius
7
I've check the alpha and the sun system font by using xlsfonts command. It does have "Lucida" fonts, but I have no idea if it is pointing to the one in the JRE folder. I don't know how to check that. Therefore I can't really tell if their is a aliasing problem. I tried to force java to look in the JRE folder for the fonts, by using the command "java -Djava.awt.fonts=<JRE font directory> Metrics". That did help get the alpha and the sun system to display the same metrics, but the window system is still different. Therefore aliasing might of been the problem on the unix system, but I still don't understand why it's different in windows.

And thanks for your help Mingster.
 
Originally posted by nhung:
I've check the alpha and the sun system font by using xlsfonts command. It does have "Lucida" fonts, but I have no idea if it is pointing to the one in the JRE folder. I don't know how to check that. Therefore I can't really tell if their is a aliasing problem. I tried to force java to look in the JRE folder for the fonts, by using the command "java -Djava.awt.fonts=<JRE font directory> Metrics". That did help get the alpha and the sun system to display the same metrics, but the window system is still different. Therefore aliasing might of been the problem on the unix system, but I still don't understand why it's different in windows.

And thanks for your help Mingster.


You are welcome :p
A quick scan of my NT4.0 machine with jdk1.3.1 installed shows that,
1) $JAVA_JDK/jre/lib/font.properties does not specify any Lucida fonts.
2) Within the WINNT folders, there already exists Lucida fonts that are
different than Sun's own Lucida font.
--------------------------------------
My recommendation:
Copy the Sun's Lucida font to c:\Winnt\Fonts\, then try again.

Good luck.

--------------------------------------
Just out of curiosity, what do you do that needs the exact measurement of the fonts been displayed across all platforms?
 

nhung

Seniorius Lurkius
7
The Lucida Sans font I'm talking about is in the $JAVA_JDK/jre/lib/font directory. It's suppose to be shipped with all JRE, across all platform. Unfortunately it seem, using the same font in different system result in different graphics. (Actually the JDK 1.2 on is shipped with Lucida Sans, Lucida Bright, and Lucida Sans Typewriter, but if you only download the JRE, it only has the Lucida Sans.)

I'm working on a software that is supposed to be deployed on the windows and the alpha system. The software display graphical and tabular information, either coming in from a data stream or is archive on a server. When the user(s) print the plot or the table, we want it to look the same no matter what system the users are on. This makes it easier to compare the information (some people still looking at paper).
 
That's cool. I guessed your work relates to publishing. I was just curious what kind of application you are doing.

I know Lucida whatever fonts came with every Sun's JRE.
I gave you my jdk version number so there is a reference point.

Java virtual machine not only uses the fonts it comes with, but it also uses the fonts provided by the underlying OS. I have no resources telling me how JVM resolves which font to use if both OS and JVM contains the same font.

Under Unix, we already see that Java uses the font provided by the X Window System unless is forced by the command line option.

Given this observation, it is reasonable to assume somehow Windows is providing the Lucida whatever fonts to the Java Virtual Machine. There are 2 ways to confirm this problem. One is to copy the Sun's Lucida <whatever> fonts to Windows' font directory. Thus, this set of font is available to all application (including JVM). If now, your program is working fine. This confirms our problem. If not, we just disproved our theory. It's time to think of better reason-- or call the Sun's support team.

I told you to copy fonts to Windows directory just to understand if this is a problem with two fonts with same name. Or this is some other problem.

A good programmer should be able to observe the symptom and deduce the root cause. You can do it. Good luck.
 
Status
Not open for further replies.