I am running into some differences in string metrics for certain TrueType
fonts on my XP box with a sun.java2d.SunGraphics2D object verses a
sun.awt.windows.WPathGraphics (i.e., my actual printer) object. All I'm
trying to do is to right justify some text, and when I compute the x
coordinate based on the string width for a string, it always comes out
perfect on the sun.java2d.SunGraphics2D object for all fonts but sometimes
it also comes out perfect on a sun.awt.windows.WPathGraphics object (i.e.,
that is my printer (the piece of paper I ultimately hold in my hand) which
happens to be a LaserJet 6P for testing).
I use the following lines to compute the string width and that computation
is used to right justify the text:
FontRenderContext fontRenderContext = ((Graphics2D) pGraphics).
getFontRenderContext ();
Rectangle2D rectangle = pFont.getStringBounds(pText, fontRenderContext);
float stringWidth = ((float) (rectangle.getWidth () - rectangle.getX ()));
Afterwards I call the Graphics drawString method like this:
((Graphics2D) pGraphics).drawString(someString, x, y); where x and y are
float values.
I even tried experimenting with setting fractional font rendering hints like
this:
fFractionalMetricsRenderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
fFractionalMetricsRenderingHints.put(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
and setting it like this:
graphics2D.setRenderingHints(fFractionalMetricsRenderingHints);
But so far nothing has worked with certain fonts; Arial, Times Roman, etc.,
work fine, but other "fancier" TrueType fonts do not! Again with this
"problem" fonts, when I try to right justify the text, it looks perfect on
the screen (i.e., sun.java2d.SunGraphics2D) but the right end of the strings
do not line up at all for the printer (i.e., sun.awt.windows.WPathGraphics).
Does anyone have any ideas on how to solve this seemingly simple problem?
Thanx.
Karl
Harald Hein - 23 Dec 2003 07:05 GMT
> Does anyone have any ideas on how to solve this seemingly simple
> problem?
AFAIR this is a known bug with some Java versions. Check Sun's
bugparade.